blob: b6f302605b1e600311ce1484a87b96c3f654a34c [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-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 Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000038#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
Sebastian Redlade50002010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Chris Lattner5f9e2722011-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 Kramer6e089c62011-04-24 17:44:50 +000059 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000060}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000061
62template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000063static StringRef data(const SmallVectorImpl<T> &v) {
64 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000066}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000071
Douglas Gregor2cf26342009-04-09 22:27:44 +000072namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000080
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-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 Gregor2cf26342009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl3397c552010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000095 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000096}
97
Sebastian Redl3397c552010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101}
102
Sebastian Redl3397c552010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106}
107
Sebastian Redl3397c552010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111}
112
Sebastian Redl3397c552010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117}
118
Sebastian Redl3397c552010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000122}
123
Sebastian Redl3397c552010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128}
129
Sebastian Redl3397c552010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134}
135
Sebastian Redl3397c552010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140}
141
Sebastian Redl3397c552010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145}
146
Sebastian Redl3397c552010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153}
154
Sebastian Redl3397c552010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160}
161
Sebastian Redl3397c552010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176}
177
Sebastian Redl3397c552010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181}
182
Sebastian Redl3397c552010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-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 Smitheefb3d52012-02-10 09:58:53 +0000189 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000190 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000191 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-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 Smith7bb698a2012-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 Smithb9d0b762012-07-27 04:22:15 +0000202 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000204 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000205 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206}
207
Sebastian Redl3397c552010-08-18 23:56:27 +0000208void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000209 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000210 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000211}
John McCalled976492009-12-04 22:46:56 +0000212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000214 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000215 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
216 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000217 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000218}
219
Sebastian Redl3397c552010-08-18 23:56:27 +0000220void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000221 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000222 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223}
224
Sebastian Redl3397c552010-08-18 23:56:27 +0000225void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000227 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228}
229
Sebastian Redl3397c552010-08-18 23:56:27 +0000230void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000232 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000233 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000234}
235
Sean Huntca63c202011-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 Smith34b41d92011-02-20 03:19:35 +0000243void ASTTypeWriter::VisitAutoType(const AutoType *T) {
244 Writer.AddTypeRef(T->getDeducedType(), Record);
245 Code = TYPE_AUTO;
246}
247
Sebastian Redl3397c552010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000249 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000250 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000251 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000252 "Cannot serialize in the middle of a type definition");
253}
254
Sebastian Redl3397c552010-08-18 23:56:27 +0000255void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000257 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258}
259
Sebastian Redl3397c552010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000262 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263}
264
John McCall9d156a72011-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 Stump1eb44332009-09-09 15:08:12 +0000272void
Sebastian Redl3397c552010-08-18 23:56:27 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000274 const SubstTemplateTypeParmType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000278}
279
280void
Douglas Gregorc3069d62011-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 Redl3397c552010-08-18 23:56:27 +0000289ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000290 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000291 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-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 Smith3e4c6c42011-05-05 21:57:07 +0000297 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
298 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000299 : T->getCanonicalTypeInternal(),
300 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl3397c552010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000306 VisitArrayType(T);
307 Writer.AddStmt(T->getSizeExpr());
308 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000309 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000310}
311
312void
Sebastian Redl3397c552010-08-18 23:56:27 +0000313ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314 const DependentSizedExtVectorType *T) {
315 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000316 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317}
318
319void
Sebastian Redl3397c552010-08-18 23:56:27 +0000320ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000321 Record.push_back(T->getDepth());
322 Record.push_back(T->getIndex());
323 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000324 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000325 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000326}
327
328void
Sebastian Redl3397c552010-08-18 23:56:27 +0000329ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000330 Record.push_back(T->getKeyword());
331 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
332 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000333 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
334 : T->getCanonicalTypeInternal(),
335 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000336 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000337}
338
339void
Sebastian Redl3397c552010-08-18 23:56:27 +0000340ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000341 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-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 Redl8538e8d2010-08-18 23:57:32 +0000349 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000350}
351
Douglas Gregor7536dd52010-12-20 02:24:11 +0000352void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
353 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-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 Gregor7536dd52010-12-20 02:24:11 +0000358 Code = TYPE_PACK_EXPANSION;
359}
360
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000361void ASTTypeWriter::VisitParenType(const ParenType *T) {
362 Writer.AddTypeRef(T->getInnerType(), Record);
363 Code = TYPE_PAREN;
364}
365
Sebastian Redl3397c552010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000367 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000368 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
369 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000370 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000371}
372
Sebastian Redl3397c552010-08-18 23:56:27 +0000373void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000374 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000375 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000376 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000377}
378
Sebastian Redl3397c552010-08-18 23:56:27 +0000379void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000380 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000381 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000382}
383
Sebastian Redl3397c552010-08-18 23:56:27 +0000384void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000385 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000386 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000387 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000388 E = T->qual_end(); I != E; ++I)
389 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000390 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000391}
392
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000393void
Sebastian Redl3397c552010-08-18 23:56:27 +0000394ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000395 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000396 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000397}
398
Eli Friedmanb001de72011-10-06 23:00:33 +0000399void
400ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
401 Writer.AddTypeRef(T->getValueType(), Record);
402 Code = TYPE_ATOMIC;
403}
404
John McCalla1ee0c52009-10-16 21:56:05 +0000405namespace {
406
407class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000408 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000409 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000410
411public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000412 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000413 : Writer(Writer), Record(Record) { }
414
John McCall51bd8032009-10-18 01:05:36 +0000415#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000416#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000417 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000418#include "clang/AST/TypeLocNodes.def"
419
John McCall51bd8032009-10-18 01:05:36 +0000420 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
421 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000422};
423
424}
425
John McCall51bd8032009-10-18 01:05:36 +0000426void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
427 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000428}
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-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 McCalla1ee0c52009-10-16 21:56:05 +0000437}
John McCall51bd8032009-10-18 01:05:36 +0000438void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000446}
John McCall51bd8032009-10-18 01:05:36 +0000447void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000455 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000456}
John McCall51bd8032009-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 McCalla1ee0c52009-10-16 21:56:05 +0000463}
John McCall51bd8032009-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 Bagnara796aa442011-03-12 11:17:06 +0000488 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000489 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
490 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000491 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-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 McCalled976492009-12-04 22:46:56 +0000501void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
John McCall51bd8032009-10-18 01:05:36 +0000504void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
507void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000508 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000511}
512void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-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 McCall51bd8032009-10-18 01:05:36 +0000517}
518void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
Sean Huntca63c202011-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 Smith34b41d92011-02-20 03:19:35 +0000527void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
John McCall51bd8032009-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 McCall9d156a72011-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 McCall51bd8032009-10-18 01:05:36 +0000551void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
553}
John McCall49a832b2009-10-18 09:09:24 +0000554void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
555 SubstTemplateTypeParmTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000558void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
559 SubstTemplateTypeParmPackTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561}
John McCall51bd8032009-10-18 01:05:36 +0000562void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
563 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000564 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-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 Kyrtzidis44f8c372010-06-22 09:54:59 +0000569 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
570 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000571}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000572void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
573 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
574 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
575}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000576void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000577 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000578 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000579}
John McCall3cb0ebd2010-03-10 03:28:59 +0000580void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
Douglas Gregor4714c122010-03-31 17:34:00 +0000583void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000584 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000585 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000586 Writer.AddSourceLocation(TL.getNameLoc(), Record);
587}
John McCall33500952010-06-11 00:33:02 +0000588void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
589 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000590 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000591 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000592 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-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 Kyrtzidis44f8c372010-06-22 09:54:59 +0000597 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
598 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000599}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000600void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
601 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
602}
John McCall51bd8032009-10-18 01:05:36 +0000603void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000605}
606void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
607 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-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 McCalla1ee0c52009-10-16 21:56:05 +0000612}
John McCall54e14c42009-10-22 22:37:11 +0000613void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
614 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000615}
Eli Friedmanb001de72011-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 McCalla1ee0c52009-10-16 21:56:05 +0000621
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000622//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000623// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000624//===----------------------------------------------------------------------===//
625
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000626static void EmitBlockID(unsigned ID, const char *Name,
627 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000628 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-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 Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000643 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-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 Lattner0558df22009-04-27 00:49:53 +0000649}
650
651static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000652 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000653#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-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 Smith534986f2012-04-14 00:33:13 +0000661 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-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 Rosierdf5faf52012-08-25 00:11:56 +0000673 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000674 RECORD(STMT_MSASM);
Chris Lattner0558df22009-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 Lattner0558df22009-04-27 00:49:53 +0000701 RECORD(EXPR_CHOOSE);
702 RECORD(EXPR_GNU_NULL);
703 RECORD(EXPR_SHUFFLE_VECTOR);
704 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000705 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000706 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000707 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000708 RECORD(EXPR_OBJC_ARRAY_LITERAL);
709 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-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 Lattner0558df22009-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 Kremenekebcb57a2012-03-06 20:05:56 +0000723 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-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 Smith9fcce652012-03-07 08:35:16 +0000731 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000732 RECORD(EXPR_CXX_BOOL_LITERAL);
733 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-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 Collingbournee08ce652011-02-09 21:07:24 +0000759 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000760#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761}
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Sebastian Redla4232eb2010-08-18 23:56:21 +0000763void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764 RecordData Record;
765 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Sebastian Redl8538e8d2010-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 Stump1eb44332009-09-09 15:08:12 +0000769
Sebastian Redl3397c552010-08-18 23:56:27 +0000770 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000771 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000772 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000773 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000774 RECORD(TYPE_OFFSET);
775 RECORD(DECL_OFFSET);
776 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000777 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000778 RECORD(IDENTIFIER_OFFSET);
779 RECORD(IDENTIFIER_TABLE);
780 RECORD(EXTERNAL_DEFINITIONS);
781 RECORD(SPECIAL_TYPES);
782 RECORD(STATISTICS);
783 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000784 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000785 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
786 RECORD(SELECTOR_OFFSETS);
787 RECORD(METHOD_POOL);
788 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000789 RECORD(SOURCE_LOCATION_OFFSETS);
790 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000791 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000792 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000793 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000794 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000795 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000796 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000797 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000798 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000799 RECORD(SEMA_DECL_REFS);
800 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
801 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
802 RECORD(DECL_REPLACEMENTS);
803 RECORD(UPDATE_VISIBLE);
804 RECORD(DECL_UPDATE_OFFSETS);
805 RECORD(DECL_UPDATES);
806 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
807 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000808 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000809 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000810 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000811 RECORD(FP_PRAGMA_OPTIONS);
812 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000813 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000814 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
815 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000816 RECORD(MODULE_OFFSET_MAP);
817 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000818 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000819 RECORD(FILE_SORTED_DECLS);
820 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000821 RECORD(MERGED_DECLARATIONS);
822 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000823 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000824 RECORD(MACRO_OFFSET);
825 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000826
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000827 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000828 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000829 RECORD(SM_SLOC_FILE_ENTRY);
830 RECORD(SM_SLOC_BUFFER_ENTRY);
831 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000832 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000834 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000835 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000836 RECORD(PP_MACRO_OBJECT_LIKE);
837 RECORD(PP_MACRO_FUNCTION_LIKE);
838 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000839
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000840 // Decls and Types block.
841 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000842 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000843 RECORD(TYPE_COMPLEX);
844 RECORD(TYPE_POINTER);
845 RECORD(TYPE_BLOCK_POINTER);
846 RECORD(TYPE_LVALUE_REFERENCE);
847 RECORD(TYPE_RVALUE_REFERENCE);
848 RECORD(TYPE_MEMBER_POINTER);
849 RECORD(TYPE_CONSTANT_ARRAY);
850 RECORD(TYPE_INCOMPLETE_ARRAY);
851 RECORD(TYPE_VARIABLE_ARRAY);
852 RECORD(TYPE_VECTOR);
853 RECORD(TYPE_EXT_VECTOR);
854 RECORD(TYPE_FUNCTION_PROTO);
855 RECORD(TYPE_FUNCTION_NO_PROTO);
856 RECORD(TYPE_TYPEDEF);
857 RECORD(TYPE_TYPEOF_EXPR);
858 RECORD(TYPE_TYPEOF);
859 RECORD(TYPE_RECORD);
860 RECORD(TYPE_ENUM);
861 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000862 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000863 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000864 RECORD(TYPE_DECLTYPE);
865 RECORD(TYPE_ELABORATED);
866 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
867 RECORD(TYPE_UNRESOLVED_USING);
868 RECORD(TYPE_INJECTED_CLASS_NAME);
869 RECORD(TYPE_OBJC_OBJECT);
870 RECORD(TYPE_TEMPLATE_TYPE_PARM);
871 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
872 RECORD(TYPE_DEPENDENT_NAME);
873 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
874 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
875 RECORD(TYPE_PAREN);
876 RECORD(TYPE_PACK_EXPANSION);
877 RECORD(TYPE_ATTRIBUTED);
878 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000879 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000880 RECORD(DECL_TYPEDEF);
881 RECORD(DECL_ENUM);
882 RECORD(DECL_RECORD);
883 RECORD(DECL_ENUM_CONSTANT);
884 RECORD(DECL_FUNCTION);
885 RECORD(DECL_OBJC_METHOD);
886 RECORD(DECL_OBJC_INTERFACE);
887 RECORD(DECL_OBJC_PROTOCOL);
888 RECORD(DECL_OBJC_IVAR);
889 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000890 RECORD(DECL_OBJC_CATEGORY);
891 RECORD(DECL_OBJC_CATEGORY_IMPL);
892 RECORD(DECL_OBJC_IMPLEMENTATION);
893 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
894 RECORD(DECL_OBJC_PROPERTY);
895 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000896 RECORD(DECL_FIELD);
897 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000898 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000899 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000900 RECORD(DECL_FILE_SCOPE_ASM);
901 RECORD(DECL_BLOCK);
902 RECORD(DECL_CONTEXT_LEXICAL);
903 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000904 RECORD(DECL_NAMESPACE);
905 RECORD(DECL_NAMESPACE_ALIAS);
906 RECORD(DECL_USING);
907 RECORD(DECL_USING_SHADOW);
908 RECORD(DECL_USING_DIRECTIVE);
909 RECORD(DECL_UNRESOLVED_USING_VALUE);
910 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
911 RECORD(DECL_LINKAGE_SPEC);
912 RECORD(DECL_CXX_RECORD);
913 RECORD(DECL_CXX_METHOD);
914 RECORD(DECL_CXX_CONSTRUCTOR);
915 RECORD(DECL_CXX_DESTRUCTOR);
916 RECORD(DECL_CXX_CONVERSION);
917 RECORD(DECL_ACCESS_SPEC);
918 RECORD(DECL_FRIEND);
919 RECORD(DECL_FRIEND_TEMPLATE);
920 RECORD(DECL_CLASS_TEMPLATE);
921 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
922 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
923 RECORD(DECL_FUNCTION_TEMPLATE);
924 RECORD(DECL_TEMPLATE_TYPE_PARM);
925 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
926 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
927 RECORD(DECL_STATIC_ASSERT);
928 RECORD(DECL_CXX_BASE_SPECIFIERS);
929 RECORD(DECL_INDIRECTFIELD);
930 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
931
Douglas Gregora72d8c42011-06-03 02:27:19 +0000932 // Statements and Exprs can occur in the Decls and Types block.
933 AddStmtsExprs(Stream, Record);
934
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000935 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000936 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000937 RECORD(PPD_MACRO_DEFINITION);
938 RECORD(PPD_INCLUSION_DIRECTIVE);
939
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000940#undef RECORD
941#undef BLOCK
942 Stream.ExitBlock();
943}
944
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945/// \brief Adjusts the given filename to only write out the portion of the
946/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000947///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000948/// \param Filename the file name to adjust.
949///
950/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
951/// the returned filename will be adjusted by this system root.
952///
953/// \returns either the original filename (if it needs no adjustment) or the
954/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000955static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000956adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Douglas Gregor832d6202011-07-22 16:35:34 +0000959 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000960 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 // Verify that the filename and the system root have the same prefix.
963 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000964 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000965 if (Filename[Pos] != isysroot[Pos])
966 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 // We hit the end of the filename before we hit the end of the system root.
969 if (!Filename[Pos])
970 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 // If the file name has a '/' at the current position, skip over the '/'.
973 // We distinguish sysroot-based includes from absolute includes by the
974 // absence of '/' at the beginning of sysroot-based includes.
975 if (Filename[Pos] == '/')
976 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978 return Filename + Pos;
979}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000980
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000981/// \brief Write the control block.
982void ASTWriter::WriteControlBlock(ASTContext &Context, StringRef isysroot,
983 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000984 using namespace llvm;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000985 Stream.EnterSubblock(CONTROL_BLOCK_ID, 4);
986
Douglas Gregore650c8c2009-07-07 00:12:59 +0000987 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000988 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregor57016dd2012-10-16 23:40:58 +0000989 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000990 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000991 Record.push_back(VERSION_MAJOR);
992 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000993 Record.push_back(CLANG_VERSION_MAJOR);
994 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000995 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000996 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor57016dd2012-10-16 23:40:58 +0000997 AddString(TargetOpts.Triple, Record);
998 AddString(TargetOpts.CPU, Record);
999 AddString(TargetOpts.ABI, Record);
1000 AddString(TargetOpts.CXXABI, Record);
1001 AddString(TargetOpts.LinkerVersion, Record);
1002 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1003 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1004 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1005 }
1006 Record.push_back(TargetOpts.Features.size());
1007 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1008 AddString(TargetOpts.Features[I], Record);
1009 }
1010 Stream.EmitRecord(METADATA, Record);
Douglas Gregore95b9192011-08-17 21:07:30 +00001011
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001012 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001013 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001014 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1015 llvm::SmallVector<char, 128> ModulePaths;
1016 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001017
1018 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1019 M != MEnd; ++M) {
1020 // Skip modules that weren't directly imported.
1021 if (!(*M)->isDirectlyImported())
1022 continue;
1023
1024 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1025 // FIXME: Write import location, once it matters.
1026 // FIXME: This writes the absolute path for AST files we depend on.
1027 const std::string &FileName = (*M)->FileName;
1028 Record.push_back(FileName.size());
1029 Record.append(FileName.begin(), FileName.end());
1030 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001031 Stream.EmitRecord(IMPORTS, Record);
1032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001034 // Language options.
1035 Record.clear();
1036 const LangOptions &LangOpts = Context.getLangOpts();
1037#define LANGOPT(Name, Bits, Default, Description) \
1038 Record.push_back(LangOpts.Name);
1039#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1040 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1041#include "clang/Basic/LangOptions.def"
1042
1043 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1044 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1045
1046 Record.push_back(LangOpts.CurrentModule.size());
1047 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1048 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1049
Douglas Gregor31d375f2011-05-06 21:43:30 +00001050 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001051 SourceManager &SM = Context.getSourceManager();
1052 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1053 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001054 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001055 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1056 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1057
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001058 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001060 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001061
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001062 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001063 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001064 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001065 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001066 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001067 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001068
1069 Record.clear();
1070 Record.push_back(SM.getMainFileID().getOpaqueValue());
1071 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001072 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001073
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001074 // Original PCH directory
1075 if (!OutputFile.empty() && OutputFile != "-") {
1076 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1077 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1078 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1079 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1080
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001081 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001082
1083 llvm::sys::fs::make_absolute(OutputPath);
1084 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1085
1086 RecordData Record;
1087 Record.push_back(ORIGINAL_PCH_DIR);
1088 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1089 }
1090
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001091 // Repository branch/version information.
1092 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001093 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001094 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1095 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001096 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001097 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001098 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1099 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001100
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001101 Stream.ExitBlock();
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001102}
1103
Douglas Gregor14f79002009-04-10 03:52:48 +00001104//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001105// stat cache Serialization
1106//===----------------------------------------------------------------------===//
1107
1108namespace {
1109// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001110class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001111public:
1112 typedef const char * key_type;
1113 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Chris Lattner74e976b2010-11-23 19:28:12 +00001115 typedef struct stat data_type;
1116 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001117
1118 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001119 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
1122 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001123 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001124 data_type_ref Data) {
1125 unsigned StrLen = strlen(path);
1126 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001127 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128 clang::io::Emit8(Out, DataLen);
1129 return std::make_pair(StrLen + 1, DataLen);
1130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Chris Lattner5f9e2722011-07-23 10:55:15 +00001132 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001133 Out.write(path, KeyLen);
1134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner5f9e2722011-07-23 10:55:15 +00001136 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001137 data_type_ref Data, unsigned DataLen) {
1138 using namespace clang::io;
1139 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Chris Lattner74e976b2010-11-23 19:28:12 +00001141 Emit32(Out, (uint32_t) Data.st_ino);
1142 Emit32(Out, (uint32_t) Data.st_dev);
1143 Emit16(Out, (uint16_t) Data.st_mode);
1144 Emit64(Out, (uint64_t) Data.st_mtime);
1145 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001146
1147 assert(Out.tell() - Start == DataLen && "Wrong data length");
1148 }
1149};
1150} // end anonymous namespace
1151
Sebastian Redl3397c552010-08-18 23:56:27 +00001152/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001153void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001154 // Build the on-disk hash table containing information about every
1155 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001156 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001157 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001158 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001159 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001160 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001161 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001162 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001165 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001166 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001167 uint32_t BucketOffset;
1168 {
1169 llvm::raw_svector_ostream Out(StatCacheData);
1170 // Make sure that no bucket is at offset 0
1171 clang::io::Emit32(Out, 0);
1172 BucketOffset = Generator.Emit(Out);
1173 }
1174
1175 // Create a blob abbreviation
1176 using namespace llvm;
1177 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001178 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1182 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1183
1184 // Write the stat cache
1185 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001186 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001187 Record.push_back(BucketOffset);
1188 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001189 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001190}
1191
1192//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001193// Source Manager Serialization
1194//===----------------------------------------------------------------------===//
1195
1196/// \brief Create an abbreviation for the SLocEntry that refers to a
1197/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001198static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001199 using namespace llvm;
1200 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001201 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001206 // FileEntry fields.
1207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001214 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001215}
1216
1217/// \brief Create an abbreviation for the SLocEntry that refers to a
1218/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001219static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001228 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001229}
1230
1231/// \brief Create an abbreviation for the SLocEntry that refers to a
1232/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001233static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001234 using namespace llvm;
1235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001236 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001238 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001239}
1240
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001241/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1242/// expansion.
1243static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001244 using namespace llvm;
1245 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001246 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1250 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001252 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001253}
1254
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001255namespace {
1256 // Trait used for the on-disk hash table of header search information.
1257 class HeaderFileInfoTrait {
1258 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001259
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001260 // Keep track of the framework names we've used during serialization.
1261 SmallVector<char, 128> FrameworkStringData;
1262 llvm::StringMap<unsigned> FrameworkNameOffset;
1263
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001264 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001265 HeaderFileInfoTrait(ASTWriter &Writer)
1266 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001267
1268 typedef const char *key_type;
1269 typedef key_type key_type_ref;
1270
1271 typedef HeaderFileInfo data_type;
1272 typedef const data_type &data_type_ref;
1273
1274 static unsigned ComputeHash(const char *path) {
1275 // The hash is based only on the filename portion of the key, so that the
1276 // reader can match based on filenames when symlinking or excess path
1277 // elements ("foo/../", "../") change the form of the name. However,
1278 // complete path is still the key.
1279 return llvm::HashString(llvm::sys::path::filename(path));
1280 }
1281
1282 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001283 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001284 data_type_ref Data) {
1285 unsigned StrLen = strlen(path);
1286 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001287 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001288 clang::io::Emit8(Out, DataLen);
1289 return std::make_pair(StrLen + 1, DataLen);
1290 }
1291
Chris Lattner5f9e2722011-07-23 10:55:15 +00001292 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001293 Out.write(path, KeyLen);
1294 }
1295
Chris Lattner5f9e2722011-07-23 10:55:15 +00001296 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001297 data_type_ref Data, unsigned DataLen) {
1298 using namespace clang::io;
1299 uint64_t Start = Out.tell(); (void)Start;
1300
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001301 unsigned char Flags = (Data.isImport << 5)
1302 | (Data.isPragmaOnce << 4)
1303 | (Data.DirInfo << 2)
1304 | (Data.Resolved << 1)
1305 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001306 Emit8(Out, (uint8_t)Flags);
1307 Emit16(Out, (uint16_t) Data.NumIncludes);
1308
1309 if (!Data.ControllingMacro)
1310 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1311 else
1312 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001313
1314 unsigned Offset = 0;
1315 if (!Data.Framework.empty()) {
1316 // If this header refers into a framework, save the framework name.
1317 llvm::StringMap<unsigned>::iterator Pos
1318 = FrameworkNameOffset.find(Data.Framework);
1319 if (Pos == FrameworkNameOffset.end()) {
1320 Offset = FrameworkStringData.size() + 1;
1321 FrameworkStringData.append(Data.Framework.begin(),
1322 Data.Framework.end());
1323 FrameworkStringData.push_back(0);
1324
1325 FrameworkNameOffset[Data.Framework] = Offset;
1326 } else
1327 Offset = Pos->second;
1328 }
1329 Emit32(Out, Offset);
1330
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001331 assert(Out.tell() - Start == DataLen && "Wrong data length");
1332 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001333
1334 const char *strings_begin() const { return FrameworkStringData.begin(); }
1335 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001336 };
1337} // end anonymous namespace
1338
1339/// \brief Write the header search block for the list of files that
1340///
1341/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001342void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001343 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001344 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1345
1346 if (FilesByUID.size() > HS.header_file_size())
1347 FilesByUID.resize(HS.header_file_size());
1348
Benjamin Kramerfacde172012-06-06 17:32:50 +00001349 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001350 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001351 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001352 unsigned NumHeaderSearchEntries = 0;
1353 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1354 const FileEntry *File = FilesByUID[UID];
1355 if (!File)
1356 continue;
1357
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001358 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1359 // from the external source if it was not provided already.
1360 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001361 if (HFI.External && Chain)
1362 continue;
1363
1364 // Turn the file name into an absolute path, if it isn't already.
1365 const char *Filename = File->getName();
1366 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1367
1368 // If we performed any translation on the file name at all, we need to
1369 // save this string, since the generator will refer to it later.
1370 if (Filename != File->getName()) {
1371 Filename = strdup(Filename);
1372 SavedStrings.push_back(Filename);
1373 }
1374
1375 Generator.insert(Filename, HFI, GeneratorTrait);
1376 ++NumHeaderSearchEntries;
1377 }
1378
1379 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001380 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001381 uint32_t BucketOffset;
1382 {
1383 llvm::raw_svector_ostream Out(TableData);
1384 // Make sure that no bucket is at offset 0
1385 clang::io::Emit32(Out, 0);
1386 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1387 }
1388
1389 // Create a blob abbreviation
1390 using namespace llvm;
1391 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1392 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1397 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1398
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001399 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001400 RecordData Record;
1401 Record.push_back(HEADER_SEARCH_TABLE);
1402 Record.push_back(BucketOffset);
1403 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001404 Record.push_back(TableData.size());
1405 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001406 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1407
1408 // Free all of the strings we had to duplicate.
1409 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1410 free((void*)SavedStrings[I]);
1411}
1412
Douglas Gregor14f79002009-04-10 03:52:48 +00001413/// \brief Writes the block containing the serialized form of the
1414/// source manager.
1415///
1416/// TODO: We should probably use an on-disk hash table (stored in a
1417/// blob), indexed based on the file name, so that we only create
1418/// entries for files that we actually need. In the common case (no
1419/// errors), we probably won't have to create file entries for any of
1420/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001421void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001422 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001423 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001424 RecordData Record;
1425
Chris Lattnerf04ad692009-04-10 17:16:57 +00001426 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001427 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001428
1429 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001430 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1431 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1432 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001433 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001434
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001435 // Write out the source location entry table. We skip the first
1436 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001437 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001438 // Write out the offsets of only source location file entries.
1439 // We will go through them in ASTReader::validateFileEntries().
1440 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001441 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001442 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1443 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001444 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001445 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001446 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001447 FileID FID = FileID::get(I);
1448 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001449
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001450 // Record the offset of this source-location entry.
1451 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1452
1453 // Figure out which record code to use.
1454 unsigned Code;
1455 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001456 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1457 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001458 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001459 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1460 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001461 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001462 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001463 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001464 Record.clear();
1465 Record.push_back(Code);
1466
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001467 // Starting offset of this entry within this module, so skip the dummy.
1468 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001469 if (SLoc->isFile()) {
1470 const SrcMgr::FileInfo &File = SLoc->getFile();
1471 Record.push_back(File.getIncludeLoc().getRawEncoding());
1472 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1473 Record.push_back(File.hasLineDirectives());
1474
1475 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001476 if (Content->OrigEntry) {
1477 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001478 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001479
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001480 // The source location entry is a file. The blob associated
1481 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Douglas Gregor2d52be52010-03-21 22:49:54 +00001483 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001484 Record.push_back(Content->OrigEntry->getSize());
1485 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001486 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001487 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001488
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001489 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001490 if (FDI != FileDeclIDs.end()) {
1491 Record.push_back(FDI->second->FirstDeclIndex);
1492 Record.push_back(FDI->second->DeclIDs.size());
1493 } else {
1494 Record.push_back(0);
1495 Record.push_back(0);
1496 }
Douglas Gregora081da52011-11-16 20:05:18 +00001497
Douglas Gregore650c8c2009-07-07 00:12:59 +00001498 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001499 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001500 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001501
1502 // Ask the file manager to fixup the relative path for us. This will
1503 // honor the working directory.
1504 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1505
1506 // FIXME: This call to make_absolute shouldn't be necessary, the
1507 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001508 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001509 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregore650c8c2009-07-07 00:12:59 +00001511 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001512 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001513
1514 if (Content->BufferOverridden) {
1515 Record.clear();
1516 Record.push_back(SM_SLOC_BUFFER_BLOB);
1517 const llvm::MemoryBuffer *Buffer
1518 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1519 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1520 StringRef(Buffer->getBufferStart(),
1521 Buffer->getBufferSize() + 1));
1522 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523 } else {
1524 // The source location entry is a buffer. The blob associated
1525 // with this entry contains the contents of the buffer.
1526
1527 // We add one to the size so that we capture the trailing NULL
1528 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1529 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001530 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001531 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001533 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001534 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001535 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001536 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001537 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001538 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001539 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001540
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001541 if (strcmp(Name, "<built-in>") == 0) {
1542 PreloadSLocs.push_back(SLocEntryOffsets.size());
1543 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001544 }
1545 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001546 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001547 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001548 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1549 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001550 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1551 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552
1553 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001554 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001555 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001556 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001557 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001558 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001559 }
1560 }
1561
Douglas Gregorc9490c02009-04-16 22:23:12 +00001562 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001563
1564 if (SLocEntryOffsets.empty())
1565 return;
1566
Sebastian Redl3397c552010-08-18 23:56:27 +00001567 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001568 // table is used for lazily loading source-location information.
1569 using namespace llvm;
1570 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001571 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1575 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001577 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001578 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001579 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001580 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001581 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001582
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001583 Abbrev = new BitCodeAbbrev();
1584 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1587 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1588
1589 Record.clear();
1590 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1591 Record.push_back(SLocFileEntryOffsets.size());
1592 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1593 data(SLocFileEntryOffsets));
1594
Sebastian Redl3397c552010-08-18 23:56:27 +00001595 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001596 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001597 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001598
1599 // Write the line table. It depends on remapping working, so it must come
1600 // after the source location offsets.
1601 if (SourceMgr.hasLineTable()) {
1602 LineTableInfo &LineTable = SourceMgr.getLineTable();
1603
1604 Record.clear();
1605 // Emit the file names
1606 Record.push_back(LineTable.getNumFilenames());
1607 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1608 // Emit the file name
1609 const char *Filename = LineTable.getFilename(I);
1610 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1611 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1612 Record.push_back(FilenameLen);
1613 if (FilenameLen)
1614 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1615 }
1616
1617 // Emit the line entries
1618 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1619 L != LEnd; ++L) {
1620 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001621 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001622 continue;
1623
1624 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001625 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001626
1627 // Emit the line entries
1628 Record.push_back(L->second.size());
1629 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1630 LEEnd = L->second.end();
1631 LE != LEEnd; ++LE) {
1632 Record.push_back(LE->FileOffset);
1633 Record.push_back(LE->LineNo);
1634 Record.push_back(LE->FilenameID);
1635 Record.push_back((unsigned)LE->FileKind);
1636 Record.push_back(LE->IncludeOffset);
1637 }
1638 }
1639 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1640 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001641}
1642
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001643//===----------------------------------------------------------------------===//
1644// Preprocessor Serialization
1645//===----------------------------------------------------------------------===//
1646
Douglas Gregor9c736102011-02-10 18:20:09 +00001647static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1648 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1649 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1650 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1651 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1652 return X.first->getName().compare(Y.first->getName());
1653}
1654
Chris Lattner0b1fb982009-04-10 17:15:23 +00001655/// \brief Writes the block containing the serialized form of the
1656/// preprocessor.
1657///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001658void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001659 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1660 if (PPRec)
1661 WritePreprocessorDetail(*PPRec);
1662
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001663 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001664
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001665 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1666 if (PP.getCounterValue() != 0) {
1667 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001668 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001669 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001670 }
1671
1672 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001673 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Sebastian Redl3397c552010-08-18 23:56:27 +00001675 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001676 // FIXME: use diagnostics subsystem for localization etc.
1677 if (PP.SawDateOrTime())
1678 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Douglas Gregorecdcb882010-10-20 22:00:55 +00001680
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001681 // Loop over all the macro definitions that are live at the end of the file,
1682 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001683
Douglas Gregor9c736102011-02-10 18:20:09 +00001684 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001685 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001686 MacrosToEmit;
1687 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001688 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001689 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001690 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001691 if (!IsModule || I->second->isPublic()) {
1692 MacroDefinitionsSeen.insert(I->first);
1693 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001694 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001695 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001696
Douglas Gregor9c736102011-02-10 18:20:09 +00001697 // Sort the set of macro definitions that need to be serialized by the
1698 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001699 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001700 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001701
Douglas Gregora8235d62012-10-09 23:05:51 +00001702 /// \brief Offsets of each of the macros into the bitstream, indexed by
1703 /// the local macro ID
1704 ///
1705 /// For each identifier that is associated with a macro, this map
1706 /// provides the offset into the bitstream where that macro is
1707 /// defined.
1708 std::vector<uint32_t> MacroOffsets;
1709
Douglas Gregor9c736102011-02-10 18:20:09 +00001710 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1711 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001712
Douglas Gregora8235d62012-10-09 23:05:51 +00001713 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1714 MI = MI->getPreviousDefinition()) {
1715 MacroID ID = getMacroRef(MI);
1716 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001717 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Douglas Gregora8235d62012-10-09 23:05:51 +00001719 // Skip macros from a AST file if we're chaining.
1720 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1721 continue;
1722
1723 if (ID < FirstMacroID) {
1724 // This will have been dealt with via an update record.
1725 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1726 continue;
1727 }
1728
1729 // Record the local offset of this macro.
1730 unsigned Index = ID - FirstMacroID;
1731 if (Index == MacroOffsets.size())
1732 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1733 else {
1734 if (Index > MacroOffsets.size())
1735 MacroOffsets.resize(Index + 1);
1736
1737 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1738 }
1739
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001740 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001741 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001742 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001743 AddSourceLocation(MI->getDefinitionLoc(), Record);
1744 AddSourceLocation(MI->getUndefLoc(), Record);
1745 Record.push_back(MI->isUsed());
1746 Record.push_back(MI->isPublic());
1747 AddSourceLocation(MI->getVisibilityLocation(), Record);
1748 unsigned Code;
1749 if (MI->isObjectLike()) {
1750 Code = PP_MACRO_OBJECT_LIKE;
1751 } else {
1752 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001753
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001754 Record.push_back(MI->isC99Varargs());
1755 Record.push_back(MI->isGNUVarargs());
1756 Record.push_back(MI->getNumArgs());
1757 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1758 I != E; ++I)
1759 AddIdentifierRef(*I, Record);
1760 }
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001762 // If we have a detailed preprocessing record, record the macro definition
1763 // ID that corresponds to this macro.
1764 if (PPRec)
1765 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1766
1767 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001768 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001769
1770 // Emit the tokens array.
1771 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1772 // Note that we know that the preprocessor does not have any annotation
1773 // tokens in it because they are created by the parser, and thus can't
1774 // be in a macro definition.
1775 const Token &Tok = MI->getReplacementToken(TokNo);
1776
1777 Record.push_back(Tok.getLocation().getRawEncoding());
1778 Record.push_back(Tok.getLength());
1779
1780 // FIXME: When reading literal tokens, reconstruct the literal pointer
1781 // if it is needed.
1782 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1783 // FIXME: Should translate token kind to a stable encoding.
1784 Record.push_back(Tok.getKind());
1785 // FIXME: Should translate token flags to a stable encoding.
1786 Record.push_back(Tok.getFlags());
1787
1788 Stream.EmitRecord(PP_TOKEN, Record);
1789 Record.clear();
1790 }
1791 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001792 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001793 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001794 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001795
1796 // Write the offsets table for macro IDs.
1797 using namespace llvm;
1798 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1799 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1800 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1801 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1802 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1803
1804 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1805 Record.clear();
1806 Record.push_back(MACRO_OFFSET);
1807 Record.push_back(MacroOffsets.size());
1808 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1809 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1810 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001811}
1812
1813void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001814 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001815 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001816
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001817 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001818
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001819 // Enter the preprocessor block.
1820 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001821
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001822 // If the preprocessor has a preprocessing record, emit it.
1823 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001824 using namespace llvm;
1825
1826 // Set up the abbreviation for
1827 unsigned InclusionAbbrev = 0;
1828 {
1829 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1830 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001834 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001835 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1836 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1837 }
1838
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001839 unsigned FirstPreprocessorEntityID
1840 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1841 + NUM_PREDEF_PP_ENTITY_IDS;
1842 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001843 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001844 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1845 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001846 E != EEnd;
1847 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001848 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001849
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001850 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1851 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001852
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001853 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001854 // Record this macro definition's ID.
1855 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001856
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001857 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001858 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1859 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001860 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001861
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001862 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001863 Record.push_back(ME->isBuiltinMacro());
1864 if (ME->isBuiltinMacro())
1865 AddIdentifierRef(ME->getName(), Record);
1866 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001867 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001868 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001869 continue;
1870 }
1871
1872 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1873 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001874 Record.push_back(ID->getFileName().size());
1875 Record.push_back(ID->wasInQuotes());
1876 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001877 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001878 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001879 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001880 // Check that the FileEntry is not null because it was not resolved and
1881 // we create a PCH even with compiler errors.
1882 if (ID->getFile())
1883 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001884 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1885 continue;
1886 }
1887
1888 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1889 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001890 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001891
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001892 // Write the offsets table for the preprocessing record.
1893 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001894 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1895
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001896 // Write the offsets table for identifier IDs.
1897 using namespace llvm;
1898 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001899 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001902 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001903
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001904 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001905 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001906 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001907 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1908 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001909 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001910}
1911
Douglas Gregore209e502011-12-06 01:10:29 +00001912unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1913 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1914 if (Known != SubmoduleIDs.end())
1915 return Known->second;
1916
1917 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1918}
1919
Douglas Gregor26ced122011-12-01 00:59:36 +00001920/// \brief Compute the number of modules within the given tree (including the
1921/// given module).
1922static unsigned getNumberOfModules(Module *Mod) {
1923 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001924 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1925 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001926 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001927 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001928
1929 return ChildModules + 1;
1930}
1931
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001932void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001933 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001934 // FIXME: This feels like it belongs somewhere else, but there are no
1935 // other consumers of this information.
1936 SourceManager &SrcMgr = PP->getSourceManager();
1937 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1938 for (ASTContext::import_iterator I = Context->local_import_begin(),
1939 IEnd = Context->local_import_end();
1940 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001941 if (Module *ImportedFrom
1942 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1943 SrcMgr))) {
1944 ImportedFrom->Imports.push_back(I->getImportedModule());
1945 }
1946 }
1947
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001948 // Enter the submodule description block.
1949 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1950
1951 // Write the abbreviations needed for the submodules block.
1952 using namespace llvm;
1953 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1954 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1964 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1965
1966 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001967 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1969 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1970
1971 Abbrev = new BitCodeAbbrev();
1972 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1974 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001975
1976 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00001977 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
1978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1979 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
1980
1981 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001982 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1984 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1985
Douglas Gregor51f564f2011-12-31 04:05:44 +00001986 Abbrev = new BitCodeAbbrev();
1987 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1989 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1990
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001991 Abbrev = new BitCodeAbbrev();
1992 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
1993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1994 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
1995
Douglas Gregor26ced122011-12-01 00:59:36 +00001996 // Write the submodule metadata block.
1997 RecordData Record;
1998 Record.push_back(getNumberOfModules(WritingModule));
1999 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2000 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2001
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002002 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002003 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002004 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002005 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002006 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002007 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002008 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002009
2010 // Emit the definition of the block.
2011 Record.clear();
2012 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002013 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002014 if (Mod->Parent) {
2015 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2016 Record.push_back(SubmoduleIDs[Mod->Parent]);
2017 } else {
2018 Record.push_back(0);
2019 }
2020 Record.push_back(Mod->IsFramework);
2021 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002022 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002023 Record.push_back(Mod->InferSubmodules);
2024 Record.push_back(Mod->InferExplicitSubmodules);
2025 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002026 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2027
Douglas Gregor51f564f2011-12-31 04:05:44 +00002028 // Emit the requirements.
2029 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2030 Record.clear();
2031 Record.push_back(SUBMODULE_REQUIRES);
2032 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2033 Mod->Requires[I].data(),
2034 Mod->Requires[I].size());
2035 }
2036
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002037 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002038 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002039 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002040 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002041 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002042 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002043 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2044 Record.clear();
2045 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2046 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2047 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002048 }
2049
2050 // Emit the headers.
2051 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2052 Record.clear();
2053 Record.push_back(SUBMODULE_HEADER);
2054 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2055 Mod->Headers[I]->getName());
2056 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002057 // Emit the excluded headers.
2058 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2059 Record.clear();
2060 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2061 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2062 Mod->ExcludedHeaders[I]->getName());
2063 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002064 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2065 Record.clear();
2066 Record.push_back(SUBMODULE_TOPHEADER);
2067 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2068 Mod->TopHeaders[I]->getName());
2069 }
Douglas Gregor55988682011-12-05 16:33:54 +00002070
2071 // Emit the imports.
2072 if (!Mod->Imports.empty()) {
2073 Record.clear();
2074 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002075 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002076 assert(ImportedID && "Unknown submodule!");
2077 Record.push_back(ImportedID);
2078 }
2079 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2080 }
2081
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002082 // Emit the exports.
2083 if (!Mod->Exports.empty()) {
2084 Record.clear();
2085 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002086 if (Module *Exported = Mod->Exports[I].getPointer()) {
2087 unsigned ExportedID = SubmoduleIDs[Exported];
2088 assert(ExportedID > 0 && "Unknown submodule ID?");
2089 Record.push_back(ExportedID);
2090 } else {
2091 Record.push_back(0);
2092 }
2093
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002094 Record.push_back(Mod->Exports[I].getInt());
2095 }
2096 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2097 }
2098
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002099 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002100 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2101 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002102 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002103 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002104 }
2105
2106 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002107
2108 assert((NextSubmoduleID - FirstSubmoduleID
2109 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002110}
2111
Douglas Gregor185dbd72011-12-01 02:07:58 +00002112serialization::SubmoduleID
2113ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002114 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002115 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002116
2117 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002118 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002119 Module *OwningMod
2120 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002121 if (!OwningMod)
2122 return 0;
2123
Douglas Gregore209e502011-12-06 01:10:29 +00002124 // Check whether this submodule is part of our own module.
2125 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002126 return 0;
2127
Douglas Gregore209e502011-12-06 01:10:29 +00002128 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002129}
2130
David Blaikied6471f72011-09-25 23:23:43 +00002131void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002132 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002133 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002134 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2135 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002136 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002137 if (point.Loc.isInvalid())
2138 continue;
2139
2140 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002141 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002142 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002143 if (I->second.isPragma()) {
2144 Record.push_back(I->first);
2145 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002146 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002147 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002148 Record.push_back(-1); // mark the end of the diag/map pairs for this
2149 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002150 }
2151
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002152 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002153 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002154}
2155
Anders Carlssonc8505782011-03-06 18:41:18 +00002156void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2157 if (CXXBaseSpecifiersOffsets.empty())
2158 return;
2159
2160 RecordData Record;
2161
2162 // Create a blob abbreviation for the C++ base specifiers offsets.
2163 using namespace llvm;
2164
2165 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2166 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2169 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2170
Douglas Gregore92b8a12011-08-04 00:01:48 +00002171 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002172 Record.clear();
2173 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2174 Record.push_back(CXXBaseSpecifiersOffsets.size());
2175 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002176 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002177}
2178
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002179//===----------------------------------------------------------------------===//
2180// Type Serialization
2181//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002182
Sebastian Redl3397c552010-08-18 23:56:27 +00002183/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002184void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002185 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002186 if (Idx.getIndex() == 0) // we haven't seen this type before.
2187 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002188
Douglas Gregor97475832010-10-05 18:37:06 +00002189 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002190
Douglas Gregor2cf26342009-04-09 22:27:44 +00002191 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002192 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002193 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002194 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002195 else if (TypeOffsets.size() < Index) {
2196 TypeOffsets.resize(Index + 1);
2197 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002198 }
2199
2200 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Douglas Gregor2cf26342009-04-09 22:27:44 +00002202 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002203 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002204
Douglas Gregora4923eb2009-11-16 21:35:15 +00002205 if (T.hasLocalNonFastQualifiers()) {
2206 Qualifiers Qs = T.getLocalQualifiers();
2207 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002208 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002209 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002210 } else {
2211 switch (T->getTypeClass()) {
2212 // For all of the concrete, non-dependent types, call the
2213 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002214#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002215 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002216#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002217#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002218 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002219 }
2220
2221 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002222 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002223
2224 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002225 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002226}
2227
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002228//===----------------------------------------------------------------------===//
2229// Declaration Serialization
2230//===----------------------------------------------------------------------===//
2231
Douglas Gregor2cf26342009-04-09 22:27:44 +00002232/// \brief Write the block containing all of the declaration IDs
2233/// lexically declared within the given DeclContext.
2234///
2235/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2236/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002237uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002238 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002239 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002240 return 0;
2241
Douglas Gregorc9490c02009-04-16 22:23:12 +00002242 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002243 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002244 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002245 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002246 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2247 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002248 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002249
Douglas Gregor25123082009-04-22 22:34:57 +00002250 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002251 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002252 return Offset;
2253}
2254
Sebastian Redla4232eb2010-08-18 23:56:21 +00002255void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002256 using namespace llvm;
2257 RecordData Record;
2258
2259 // Write the type offsets array
2260 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002261 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2265 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2266 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002267 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002268 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002269 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002270 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002271
2272 // Write the declaration offsets array
2273 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002274 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2278 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2279 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002280 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002281 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002282 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002283 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002284}
2285
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002286void ASTWriter::WriteFileDeclIDsMap() {
2287 using namespace llvm;
2288 RecordData Record;
2289
2290 // Join the vectors of DeclIDs from all files.
2291 SmallVector<DeclID, 256> FileSortedIDs;
2292 for (FileDeclIDsTy::iterator
2293 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2294 DeclIDInFileInfo &Info = *FI->second;
2295 Info.FirstDeclIndex = FileSortedIDs.size();
2296 for (LocDeclIDsTy::iterator
2297 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2298 FileSortedIDs.push_back(DI->second);
2299 }
2300
2301 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2302 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2305 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2306 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002307 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002308 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2309}
2310
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002311void ASTWriter::WriteComments() {
2312 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002313 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002314 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002315 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2316 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002317 I != E; ++I) {
2318 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002319 AddSourceRange((*I)->getSourceRange(), Record);
2320 Record.push_back((*I)->getKind());
2321 Record.push_back((*I)->isTrailingComment());
2322 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002323 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2324 }
2325 Stream.ExitBlock();
2326}
2327
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002328//===----------------------------------------------------------------------===//
2329// Global Method Pool and Selector Serialization
2330//===----------------------------------------------------------------------===//
2331
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002332namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002333// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002334class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002335 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002336
2337public:
2338 typedef Selector key_type;
2339 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002340
Sebastian Redl5d050072010-08-04 17:20:04 +00002341 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002342 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002343 ObjCMethodList Instance, Factory;
2344 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002345 typedef const data_type& data_type_ref;
2346
Sebastian Redl3397c552010-08-18 23:56:27 +00002347 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002349 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002350 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002351 }
Mike Stump1eb44332009-09-09 15:08:12 +00002352
2353 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002354 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002355 data_type_ref Methods) {
2356 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2357 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002358 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2359 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002360 Method = Method->Next)
2361 if (Method->Method)
2362 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002363 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002364 Method = Method->Next)
2365 if (Method->Method)
2366 DataLen += 4;
2367 clang::io::Emit16(Out, DataLen);
2368 return std::make_pair(KeyLen, DataLen);
2369 }
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Chris Lattner5f9e2722011-07-23 10:55:15 +00002371 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002372 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002373 assert((Start >> 32) == 0 && "Selector key offset too large");
2374 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002375 unsigned N = Sel.getNumArgs();
2376 clang::io::Emit16(Out, N);
2377 if (N == 0)
2378 N = 1;
2379 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002380 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002381 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2382 }
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Chris Lattner5f9e2722011-07-23 10:55:15 +00002384 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002385 data_type_ref Methods, unsigned DataLen) {
2386 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002387 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002388 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002389 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002390 Method = Method->Next)
2391 if (Method->Method)
2392 ++NumInstanceMethods;
2393
2394 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002395 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002396 Method = Method->Next)
2397 if (Method->Method)
2398 ++NumFactoryMethods;
2399
2400 clang::io::Emit16(Out, NumInstanceMethods);
2401 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002402 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002403 Method = Method->Next)
2404 if (Method->Method)
2405 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002406 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002407 Method = Method->Next)
2408 if (Method->Method)
2409 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002410
2411 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002412 }
2413};
2414} // end anonymous namespace
2415
Sebastian Redl059612d2010-08-03 21:58:15 +00002416/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002417///
2418/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002419/// in an on-disk hash table indexed by the selector. The hash table also
2420/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002421void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002422 using namespace llvm;
2423
Sebastian Redl059612d2010-08-03 21:58:15 +00002424 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002425 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002426 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002427 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002428 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002429 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002430 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002431 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Sebastian Redl059612d2010-08-03 21:58:15 +00002433 // Create the on-disk hash table representation. We walk through every
2434 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002435 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002436 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002437 I = SelectorIDs.begin(), E = SelectorIDs.end();
2438 I != E; ++I) {
2439 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002440 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002441 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002442 I->second,
2443 ObjCMethodList(),
2444 ObjCMethodList()
2445 };
2446 if (F != SemaRef.MethodPool.end()) {
2447 Data.Instance = F->second.first;
2448 Data.Factory = F->second.second;
2449 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002450 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002451 // changed.
2452 if (Chain && I->second < FirstSelectorID) {
2453 // Selector already exists. Did it change?
2454 bool changed = false;
2455 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2456 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002457 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002458 changed = true;
2459 }
2460 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2461 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002462 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002463 changed = true;
2464 }
2465 if (!changed)
2466 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002467 } else if (Data.Instance.Method || Data.Factory.Method) {
2468 // A new method pool entry.
2469 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002470 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002471 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002472 }
2473
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002474 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002475 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002476 uint32_t BucketOffset;
2477 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002478 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002479 llvm::raw_svector_ostream Out(MethodPool);
2480 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002481 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002482 BucketOffset = Generator.Emit(Out, Trait);
2483 }
2484
2485 // Create a blob abbreviation
2486 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002487 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002489 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002490 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2491 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2492
Douglas Gregor83941df2009-04-25 17:48:32 +00002493 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002494 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002495 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002496 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002497 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002498 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002499
2500 // Create a blob abbreviation for the selector table offsets.
2501 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002502 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002505 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2506 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2507
2508 // Write the selector offsets table.
2509 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002510 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002511 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002512 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002513 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002514 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002515 }
2516}
2517
Sebastian Redl3397c552010-08-18 23:56:27 +00002518/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002519void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002520 using namespace llvm;
2521 if (SemaRef.ReferencedSelectors.empty())
2522 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002523
Fariborz Jahanian32019832010-07-23 19:11:11 +00002524 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002525
Sebastian Redl3397c552010-08-18 23:56:27 +00002526 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002527 // very tricky to fix, and given that @selector shouldn't really appear in
2528 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002529 for (DenseMap<Selector, SourceLocation>::iterator S =
2530 SemaRef.ReferencedSelectors.begin(),
2531 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2532 Selector Sel = (*S).first;
2533 SourceLocation Loc = (*S).second;
2534 AddSelectorRef(Sel, Record);
2535 AddSourceLocation(Loc, Record);
2536 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002537 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002538}
2539
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002540//===----------------------------------------------------------------------===//
2541// Identifier Table Serialization
2542//===----------------------------------------------------------------------===//
2543
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002544namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002545class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002546 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002547 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002548 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002549 bool IsModule;
2550
Douglas Gregora92193e2009-04-28 21:18:29 +00002551 /// \brief Determines whether this is an "interesting" identifier
2552 /// that needs a full IdentifierInfo structure written into the hash
2553 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002554 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002555 if (II->isPoisoned() ||
2556 II->isExtensionToken() ||
2557 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002558 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002559 II->getFETokenInfo<void>())
2560 return true;
2561
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002562 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002563 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002564
2565 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2566 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002567 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002568
2569 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002570 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002571
2572 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002573 }
2574
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002575public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002576 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002577 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002578
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002579 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002580 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Douglas Gregoreee242f2011-10-27 09:33:13 +00002582 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2583 IdentifierResolver &IdResolver, bool IsModule)
2584 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002585
2586 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002587 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002588 }
Mike Stump1eb44332009-09-09 15:08:12 +00002589
2590 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002591 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002592 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002593 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002594 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002595 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002596 DataLen += 2; // 2 bytes for builtin ID
2597 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002598 if (hadMacroDefinition(II, Macro)) {
2599 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2600 if (Writer.getMacroRef(M) != 0)
2601 DataLen += 4;
2602 }
2603
2604 DataLen += 4;
2605 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002606
Douglas Gregoreee242f2011-10-27 09:33:13 +00002607 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2608 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002609 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002610 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002611 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002612 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002613 // We emit the key length after the data length so that every
2614 // string is preceded by a 16-bit length. This matches the PTH
2615 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002616 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002617 return std::make_pair(KeyLen, DataLen);
2618 }
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Chris Lattner5f9e2722011-07-23 10:55:15 +00002620 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002621 unsigned KeyLen) {
2622 // Record the location of the key data. This is used when generating
2623 // the mapping from persistent IDs to strings.
2624 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002625 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002626 }
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Douglas Gregor7143aab2011-09-01 17:04:32 +00002628 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002629 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002630 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002631 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002632 clang::io::Emit32(Out, ID << 1);
2633 return;
2634 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002635
Douglas Gregora92193e2009-04-28 21:18:29 +00002636 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002637 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2638 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2639 clang::io::Emit16(Out, Bits);
2640 Bits = 0;
2641 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002642 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002643 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2644 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002645 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002646 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002647 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002648
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002649 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002650 // Write all of the macro IDs associated with this identifier.
2651 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2652 if (MacroID ID = Writer.getMacroRef(M))
2653 clang::io::Emit32(Out, ID);
2654 }
2655
2656 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002657 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002658
Douglas Gregor668c1a42009-04-21 22:25:48 +00002659 // Emit the declaration IDs in reverse order, because the
2660 // IdentifierResolver provides the declarations as they would be
2661 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002662 // "stat"), but the ASTReader adds declarations to the end of the list
2663 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002664 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002665 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2666 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002667 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002668 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002669 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002670 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002671 }
2672};
2673} // end anonymous namespace
2674
Sebastian Redl3397c552010-08-18 23:56:27 +00002675/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002676///
2677/// The identifier table consists of a blob containing string data
2678/// (the actual identifiers themselves) and a separate "offsets" index
2679/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002680void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2681 IdentifierResolver &IdResolver,
2682 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002683 using namespace llvm;
2684
2685 // Create and write out the blob that contains the identifier
2686 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002687 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002688 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002689 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002690
Douglas Gregor92b059e2009-04-28 20:33:11 +00002691 // Look for any identifiers that were named while processing the
2692 // headers, but are otherwise not needed. We add these to the hash
2693 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002694 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002695 // file.
2696 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2697 IDEnd = PP.getIdentifierTable().end();
2698 ID != IDEnd; ++ID)
2699 getIdentifierRef(ID->second);
2700
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002701 // Create the on-disk hash table representation. We only store offsets
2702 // for identifiers that appear here for the first time.
2703 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002704 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002705 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2706 ID != IDEnd; ++ID) {
2707 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002708 if (!Chain || !ID->first->isFromAST() ||
2709 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002710 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2711 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002712 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002713
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002714 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002715 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002716 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002717 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002718 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002719 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002720 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002721 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002722 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002723 }
2724
2725 // Create a blob abbreviation
2726 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002727 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002730 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002731
2732 // Write the identifier table
2733 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002734 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002735 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002736 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002737 }
2738
2739 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002740 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002741 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2745 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2746
2747 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002748 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002749 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002750 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002751 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002752 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002753}
2754
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002755//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002756// DeclContext's Name Lookup Table Serialization
2757//===----------------------------------------------------------------------===//
2758
2759namespace {
2760// Trait used for the on-disk hash table used in the method pool.
2761class ASTDeclContextNameLookupTrait {
2762 ASTWriter &Writer;
2763
2764public:
2765 typedef DeclarationName key_type;
2766 typedef key_type key_type_ref;
2767
2768 typedef DeclContext::lookup_result data_type;
2769 typedef const data_type& data_type_ref;
2770
2771 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2772
2773 unsigned ComputeHash(DeclarationName Name) {
2774 llvm::FoldingSetNodeID ID;
2775 ID.AddInteger(Name.getNameKind());
2776
2777 switch (Name.getNameKind()) {
2778 case DeclarationName::Identifier:
2779 ID.AddString(Name.getAsIdentifierInfo()->getName());
2780 break;
2781 case DeclarationName::ObjCZeroArgSelector:
2782 case DeclarationName::ObjCOneArgSelector:
2783 case DeclarationName::ObjCMultiArgSelector:
2784 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2785 break;
2786 case DeclarationName::CXXConstructorName:
2787 case DeclarationName::CXXDestructorName:
2788 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002789 break;
2790 case DeclarationName::CXXOperatorName:
2791 ID.AddInteger(Name.getCXXOverloadedOperator());
2792 break;
2793 case DeclarationName::CXXLiteralOperatorName:
2794 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2795 case DeclarationName::CXXUsingDirective:
2796 break;
2797 }
2798
2799 return ID.ComputeHash();
2800 }
2801
2802 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002803 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002804 data_type_ref Lookup) {
2805 unsigned KeyLen = 1;
2806 switch (Name.getNameKind()) {
2807 case DeclarationName::Identifier:
2808 case DeclarationName::ObjCZeroArgSelector:
2809 case DeclarationName::ObjCOneArgSelector:
2810 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002811 case DeclarationName::CXXLiteralOperatorName:
2812 KeyLen += 4;
2813 break;
2814 case DeclarationName::CXXOperatorName:
2815 KeyLen += 1;
2816 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002817 case DeclarationName::CXXConstructorName:
2818 case DeclarationName::CXXDestructorName:
2819 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002820 case DeclarationName::CXXUsingDirective:
2821 break;
2822 }
2823 clang::io::Emit16(Out, KeyLen);
2824
2825 // 2 bytes for num of decls and 4 for each DeclID.
2826 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2827 clang::io::Emit16(Out, DataLen);
2828
2829 return std::make_pair(KeyLen, DataLen);
2830 }
2831
Chris Lattner5f9e2722011-07-23 10:55:15 +00002832 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002833 using namespace clang::io;
2834
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002835 Emit8(Out, Name.getNameKind());
2836 switch (Name.getNameKind()) {
2837 case DeclarationName::Identifier:
2838 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002839 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002840 case DeclarationName::ObjCZeroArgSelector:
2841 case DeclarationName::ObjCOneArgSelector:
2842 case DeclarationName::ObjCMultiArgSelector:
2843 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002844 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002845 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002846 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2847 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002848 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002849 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002850 case DeclarationName::CXXLiteralOperatorName:
2851 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002852 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002853 case DeclarationName::CXXConstructorName:
2854 case DeclarationName::CXXDestructorName:
2855 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002856 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002857 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002858 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002859
2860 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002861 }
2862
Chris Lattner5f9e2722011-07-23 10:55:15 +00002863 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002864 data_type Lookup, unsigned DataLen) {
2865 uint64_t Start = Out.tell(); (void)Start;
2866 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2867 for (; Lookup.first != Lookup.second; ++Lookup.first)
2868 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2869
2870 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2871 }
2872};
2873} // end anonymous namespace
2874
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002875/// \brief Write the block containing all of the declaration IDs
2876/// visible from the given DeclContext.
2877///
2878/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002879/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002880uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2881 DeclContext *DC) {
2882 if (DC->getPrimaryContext() != DC)
2883 return 0;
2884
2885 // Since there is no name lookup into functions or methods, don't bother to
2886 // build a visible-declarations table for these entities.
2887 if (DC->isFunctionOrMethod())
2888 return 0;
2889
2890 // If not in C++, we perform name lookup for the translation unit via the
2891 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2892 // FIXME: In C++ we need the visible declarations in order to "see" the
2893 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002894 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002895 return 0;
2896
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002897 // Serialize the contents of the mapping used for lookup. Note that,
2898 // although we have two very different code paths, the serialized
2899 // representation is the same for both cases: a declaration name,
2900 // followed by a size, followed by references to the visible
2901 // declarations that have that name.
2902 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002903 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002904 if (!Map || Map->empty())
2905 return 0;
2906
2907 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2908 ASTDeclContextNameLookupTrait Trait(*this);
2909
2910 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002911 DeclarationName ConversionName;
2912 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002913 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2914 D != DEnd; ++D) {
2915 DeclarationName Name = D->first;
2916 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002917 if (Result.first != Result.second) {
2918 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2919 // Hash all conversion function names to the same name. The actual
2920 // type information in conversion function name is not used in the
2921 // key (since such type information is not stable across different
2922 // modules), so the intended effect is to coalesce all of the conversion
2923 // functions under a single key.
2924 if (!ConversionName)
2925 ConversionName = Name;
2926 ConversionDecls.append(Result.first, Result.second);
2927 continue;
2928 }
2929
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002930 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002931 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002932 }
2933
Douglas Gregore5a54b62011-08-30 20:49:19 +00002934 // Add the conversion functions
2935 if (!ConversionDecls.empty()) {
2936 Generator.insert(ConversionName,
2937 DeclContext::lookup_result(ConversionDecls.begin(),
2938 ConversionDecls.end()),
2939 Trait);
2940 }
2941
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002942 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002943 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002944 uint32_t BucketOffset;
2945 {
2946 llvm::raw_svector_ostream Out(LookupTable);
2947 // Make sure that no bucket is at offset 0
2948 clang::io::Emit32(Out, 0);
2949 BucketOffset = Generator.Emit(Out, Trait);
2950 }
2951
2952 // Write the lookup table
2953 RecordData Record;
2954 Record.push_back(DECL_CONTEXT_VISIBLE);
2955 Record.push_back(BucketOffset);
2956 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2957 LookupTable.str());
2958
2959 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2960 ++NumVisibleDeclContexts;
2961 return Offset;
2962}
2963
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002964/// \brief Write an UPDATE_VISIBLE block for the given context.
2965///
2966/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2967/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00002968/// (in C++), for namespaces, and for classes with forward-declared unscoped
2969/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002970void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002971 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2972 if (!Map || Map->empty())
2973 return;
2974
2975 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2976 ASTDeclContextNameLookupTrait Trait(*this);
2977
2978 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002979 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2980 D != DEnd; ++D) {
2981 DeclarationName Name = D->first;
2982 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002983 // For any name that appears in this table, the results are complete, i.e.
2984 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002985 if (Result.first != Result.second)
2986 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002987 }
2988
2989 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002990 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +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(UPDATE_VISIBLE);
3002 Record.push_back(getDeclID(cast<Decl>(DC)));
3003 Record.push_back(BucketOffset);
3004 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3005}
3006
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003007/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3008void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3009 RecordData Record;
3010 Record.push_back(Opts.fp_contract);
3011 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3012}
3013
3014/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3015void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003016 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003017 return;
3018
3019 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3020 RecordData Record;
3021#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3022#include "clang/Basic/OpenCLExtensions.def"
3023 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3024}
3025
Douglas Gregor2171bf12012-01-15 16:58:34 +00003026void ASTWriter::WriteRedeclarations() {
3027 RecordData LocalRedeclChains;
3028 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3029
3030 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3031 Decl *First = Redeclarations[I];
3032 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3033
3034 Decl *MostRecent = First->getMostRecentDecl();
3035
3036 // If we only have a single declaration, there is no point in storing
3037 // a redeclaration chain.
3038 if (First == MostRecent)
3039 continue;
3040
3041 unsigned Offset = LocalRedeclChains.size();
3042 unsigned Size = 0;
3043 LocalRedeclChains.push_back(0); // Placeholder for the size.
3044
3045 // Collect the set of local redeclarations of this declaration.
3046 for (Decl *Prev = MostRecent; Prev != First;
3047 Prev = Prev->getPreviousDecl()) {
3048 if (!Prev->isFromASTFile()) {
3049 AddDeclRef(Prev, LocalRedeclChains);
3050 ++Size;
3051 }
3052 }
3053 LocalRedeclChains[Offset] = Size;
3054
3055 // Reverse the set of local redeclarations, so that we store them in
3056 // order (since we found them in reverse order).
3057 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3058
3059 // Add the mapping from the first ID to the set of local declarations.
3060 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3061 LocalRedeclsMap.push_back(Info);
3062
3063 assert(N == Redeclarations.size() &&
3064 "Deserialized a declaration we shouldn't have");
3065 }
3066
3067 if (LocalRedeclChains.empty())
3068 return;
3069
3070 // Sort the local redeclarations map by the first declaration ID,
3071 // since the reader will be performing binary searches on this information.
3072 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3073
3074 // Emit the local redeclarations map.
3075 using namespace llvm;
3076 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3077 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3078 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3079 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3080 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3081
3082 RecordData Record;
3083 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3084 Record.push_back(LocalRedeclsMap.size());
3085 Stream.EmitRecordWithBlob(AbbrevID, Record,
3086 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3087 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3088
3089 // Emit the redeclaration chains.
3090 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3091}
3092
Douglas Gregorcff9f262012-01-27 01:47:08 +00003093void ASTWriter::WriteObjCCategories() {
3094 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3095 RecordData Categories;
3096
3097 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3098 unsigned Size = 0;
3099 unsigned StartIndex = Categories.size();
3100
3101 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3102
3103 // Allocate space for the size.
3104 Categories.push_back(0);
3105
3106 // Add the categories.
3107 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3108 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3109 assert(getDeclID(Cat) != 0 && "Bogus category");
3110 AddDeclRef(Cat, Categories);
3111 }
3112
3113 // Update the size.
3114 Categories[StartIndex] = Size;
3115
3116 // Record this interface -> category map.
3117 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3118 CategoriesMap.push_back(CatInfo);
3119 }
3120
3121 // Sort the categories map by the definition ID, since the reader will be
3122 // performing binary searches on this information.
3123 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3124
3125 // Emit the categories map.
3126 using namespace llvm;
3127 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3128 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3129 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3130 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3131 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3132
3133 RecordData Record;
3134 Record.push_back(OBJC_CATEGORIES_MAP);
3135 Record.push_back(CategoriesMap.size());
3136 Stream.EmitRecordWithBlob(AbbrevID, Record,
3137 reinterpret_cast<char*>(CategoriesMap.data()),
3138 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3139
3140 // Emit the category lists.
3141 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3142}
3143
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003144void ASTWriter::WriteMergedDecls() {
3145 if (!Chain || Chain->MergedDecls.empty())
3146 return;
3147
3148 RecordData Record;
3149 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3150 IEnd = Chain->MergedDecls.end();
3151 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003152 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003153 : getDeclID(I->first);
3154 assert(CanonID && "Merged declaration not known?");
3155
3156 Record.push_back(CanonID);
3157 Record.push_back(I->second.size());
3158 Record.append(I->second.begin(), I->second.end());
3159 }
3160 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3161}
3162
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003163//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003164// General Serialization Routines
3165//===----------------------------------------------------------------------===//
3166
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003167/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003168void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3169 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003170 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003171 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3172 e = Attrs.end(); i != e; ++i){
3173 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003174 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003175 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003176
Sean Huntcf807c42010-08-18 23:23:40 +00003177#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003178
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003179 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003180}
3181
Chris Lattner5f9e2722011-07-23 10:55:15 +00003182void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003183 Record.push_back(Str.size());
3184 Record.insert(Record.end(), Str.begin(), Str.end());
3185}
3186
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003187void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3188 RecordDataImpl &Record) {
3189 Record.push_back(Version.getMajor());
3190 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3191 Record.push_back(*Minor + 1);
3192 else
3193 Record.push_back(0);
3194 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3195 Record.push_back(*Subminor + 1);
3196 else
3197 Record.push_back(0);
3198}
3199
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003200/// \brief Note that the identifier II occurs at the given offset
3201/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003202void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003203 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003204 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003205 // up earlier in the chain and thus don't need an offset.
3206 if (ID >= FirstIdentID)
3207 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003208}
3209
Douglas Gregor83941df2009-04-25 17:48:32 +00003210/// \brief Note that the selector Sel occurs at the given offset
3211/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003212void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003213 unsigned ID = SelectorIDs[Sel];
3214 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003215 // Don't record offsets for selectors that are also available in a different
3216 // file.
3217 if (ID < FirstSelectorID)
3218 return;
3219 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003220}
3221
Sebastian Redla4232eb2010-08-18 23:56:21 +00003222ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003223 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003224 WritingAST(false), DoneWritingDeclsAndTypes(false),
3225 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003226 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003227 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003228 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3229 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003230 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3231 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003232 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003233 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003234 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003235 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003236 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003237 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003238 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3239 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3240 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003241 DeclTypedefAbbrev(0),
3242 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3243 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003244{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003245}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003246
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003247ASTWriter::~ASTWriter() {
3248 for (FileDeclIDsTy::iterator
3249 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3250 delete I->second;
3251}
3252
Sebastian Redla4232eb2010-08-18 23:56:21 +00003253void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003254 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003255 Module *WritingModule, StringRef isysroot,
3256 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003257 WritingAST = true;
3258
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003259 ASTHasCompilerErrors = hasErrors;
3260
Douglas Gregor2cf26342009-04-09 22:27:44 +00003261 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003262 Stream.Emit((unsigned)'C', 8);
3263 Stream.Emit((unsigned)'P', 8);
3264 Stream.Emit((unsigned)'C', 8);
3265 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003266
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003267 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003268
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003269 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003270 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003271 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003272 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003273 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003274 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003275 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003276
3277 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003278}
3279
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003280template<typename Vector>
3281static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3282 ASTWriter::RecordData &Record) {
3283 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3284 I != E; ++I) {
3285 Writer.AddDeclRef(*I, Record);
3286 }
3287}
3288
Sebastian Redla4232eb2010-08-18 23:56:21 +00003289void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003290 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003291 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003292 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003293 using namespace llvm;
3294
Douglas Gregorecc2c092011-12-01 22:20:10 +00003295 // Make sure that the AST reader knows to finalize itself.
3296 if (Chain)
3297 Chain->finalizeForWriting();
3298
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003299 ASTContext &Context = SemaRef.Context;
3300 Preprocessor &PP = SemaRef.PP;
3301
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003302 // Set up predefined declaration IDs.
3303 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003304 if (Context.ObjCIdDecl)
3305 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003306 if (Context.ObjCSelDecl)
3307 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003308 if (Context.ObjCClassDecl)
3309 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003310 if (Context.ObjCProtocolClassDecl)
3311 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003312 if (Context.Int128Decl)
3313 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3314 if (Context.UInt128Decl)
3315 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003316 if (Context.ObjCInstanceTypeDecl)
3317 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003318 if (Context.BuiltinVaListDecl)
3319 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3320
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003321 if (!Chain) {
3322 // Make sure that we emit IdentifierInfos (and any attached
3323 // declarations) for builtins. We don't need to do this when we're
3324 // emitting chained PCH files, because all of the builtins will be
3325 // in the original PCH file.
3326 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003327 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003328 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003329 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003330 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003331 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3332 getIdentifierRef(&Table.get(BuiltinNames[I]));
3333 }
3334
Douglas Gregoreee242f2011-10-27 09:33:13 +00003335 // If there are any out-of-date identifiers, bring them up to date.
3336 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3337 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3338 IDEnd = PP.getIdentifierTable().end();
3339 ID != IDEnd; ++ID)
3340 if (ID->second->isOutOfDate())
3341 ExtSource->updateOutOfDateIdentifier(*ID->second);
3342 }
3343
Chris Lattner63d65f82009-09-08 18:19:27 +00003344 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003345 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003346 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003347 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003348 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003349
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003350 // Build a record containing all of the file scoped decls in this file.
3351 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003352 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3353 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003354
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003355 // Build a record containing all of the delegating constructors we still need
3356 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003357 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003358 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003359
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003360 // Write the set of weak, undeclared identifiers. We always write the
3361 // entire table, since later PCH files in a PCH chain are only interested in
3362 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003363 RecordData WeakUndeclaredIdentifiers;
3364 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003365 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003366 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3367 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3368 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3369 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3370 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3371 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3372 }
3373 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003374
Douglas Gregor14c22f22009-04-22 22:18:58 +00003375 // Build a record containing all of the locally-scoped external
3376 // declarations in this header file. Generally, this record will be
3377 // empty.
3378 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003379 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003380 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003381 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003382 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3383 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003384 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003385 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003386 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3387 }
3388
Douglas Gregorb81c1702009-04-27 20:06:05 +00003389 // Build a record containing all of the ext_vector declarations.
3390 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003391 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003392
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003393 // Build a record containing all of the VTable uses information.
3394 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003395 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003396 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3397 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3398 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3399 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3400 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003401 }
3402
3403 // Build a record containing all of dynamic classes declarations.
3404 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003405 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003406
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003407 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003408 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003409 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003410 I = SemaRef.PendingInstantiations.begin(),
3411 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3412 AddDeclRef(I->first, PendingInstantiations);
3413 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003414 }
3415 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3416 "There are local ones at end of translation unit!");
3417
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003418 // Build a record containing some declaration references.
3419 RecordData SemaDeclRefs;
3420 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3421 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3422 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3423 }
3424
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003425 RecordData CUDASpecialDeclRefs;
3426 if (Context.getcudaConfigureCallDecl()) {
3427 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3428 }
3429
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003430 // Build a record containing all of the known namespaces.
3431 RecordData KnownNamespaces;
3432 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3433 I = SemaRef.KnownNamespaces.begin(),
3434 IEnd = SemaRef.KnownNamespaces.end();
3435 I != IEnd; ++I) {
3436 if (!I->second)
3437 AddDeclRef(I->first, KnownNamespaces);
3438 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003439
3440 // Write the control block
3441 WriteControlBlock(Context, isysroot, OutputFile);
3442
Sebastian Redl3397c552010-08-18 23:56:27 +00003443 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003444 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003445 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor832d6202011-07-22 16:35:34 +00003446 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003447 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003448
3449 // Create a lexical update block containing all of the declarations in the
3450 // translation unit that do not come from other AST files.
3451 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3452 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3453 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3454 E = TU->noload_decls_end();
3455 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003456 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003457 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003458 }
3459
3460 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3461 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3462 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3463 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3464 Record.clear();
3465 Record.push_back(TU_UPDATE_LEXICAL);
3466 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3467 data(NewGlobalDecls));
3468
3469 // And a visible updates block for the translation unit.
3470 Abv = new llvm::BitCodeAbbrev();
3471 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3472 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3473 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3474 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3475 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3476 WriteDeclContextVisibleUpdate(TU);
3477
3478 // If the translation unit has an anonymous namespace, and we don't already
3479 // have an update block for it, write it as an update block.
3480 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3481 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3482 if (Record.empty()) {
3483 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003484 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003485 }
3486 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003487
3488 // Make sure visible decls, added to DeclContexts previously loaded from
3489 // an AST file, are registered for serialization.
3490 for (SmallVector<const Decl *, 16>::iterator
3491 I = UpdatingVisibleDecls.begin(),
3492 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3493 GetDeclRef(*I);
3494 }
3495
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003496 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003497 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003498
Douglas Gregora119da02011-08-02 16:26:37 +00003499 // Form the record of special types.
3500 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003501 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003502 AddTypeRef(Context.getFILEType(), SpecialTypes);
3503 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3504 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3505 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3506 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003507 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003508 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003509
Douglas Gregor366809a2009-04-26 03:49:13 +00003510 // Keep writing types and declarations until all types and
3511 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003512 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003513 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003514 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3515 E = DeclsToRewrite.end();
3516 I != E; ++I)
3517 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003518 while (!DeclTypesToEmit.empty()) {
3519 DeclOrType DOT = DeclTypesToEmit.front();
3520 DeclTypesToEmit.pop();
3521 if (DOT.isType())
3522 WriteType(DOT.getType());
3523 else
3524 WriteDecl(Context, DOT.getDecl());
3525 }
3526 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003527
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003528 DoneWritingDeclsAndTypes = true;
3529
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003530 WriteFileDeclIDsMap();
3531 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003532 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003533
3534 if (Chain) {
3535 // Write the mapping information describing our module dependencies and how
3536 // each of those modules were mapped into our own offset/ID space, so that
3537 // the reader can build the appropriate mapping to its own offset/ID space.
3538 // The map consists solely of a blob with the following format:
3539 // *(module-name-len:i16 module-name:len*i8
3540 // source-location-offset:i32
3541 // identifier-id:i32
3542 // preprocessed-entity-id:i32
3543 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003544 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003545 // selector-id:i32
3546 // declaration-id:i32
3547 // c++-base-specifiers-id:i32
3548 // type-id:i32)
3549 //
3550 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3551 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3553 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003554 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003555 {
3556 llvm::raw_svector_ostream Out(Buffer);
3557 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003558 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003559 M != MEnd; ++M) {
3560 StringRef FileName = (*M)->FileName;
3561 io::Emit16(Out, FileName.size());
3562 Out.write(FileName.data(), FileName.size());
3563 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3564 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003565 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003566 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003567 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003568 io::Emit32(Out, (*M)->BaseSelectorID);
3569 io::Emit32(Out, (*M)->BaseDeclID);
3570 io::Emit32(Out, (*M)->BaseTypeIndex);
3571 }
3572 }
3573 Record.clear();
3574 Record.push_back(MODULE_OFFSET_MAP);
3575 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3576 Buffer.data(), Buffer.size());
3577 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003578 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003579 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003580 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003581 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003582 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003583 WriteFPPragmaOptions(SemaRef.getFPOptions());
3584 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003585
Sebastian Redl1476ed42010-07-16 16:36:56 +00003586 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003587 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003588
Anders Carlssonc8505782011-03-06 18:41:18 +00003589 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003590
Douglas Gregore209e502011-12-06 01:10:29 +00003591 // If we're emitting a module, write out the submodule information.
3592 if (WritingModule)
3593 WriteSubmodules(WritingModule);
3594
Douglas Gregora119da02011-08-02 16:26:37 +00003595 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3596
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003597 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003598 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003599 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003600
3601 // Write the record containing tentative definitions.
3602 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003603 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003604
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003605 // Write the record containing unused file scoped decls.
3606 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003607 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003608
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003609 // Write the record containing weak undeclared identifiers.
3610 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003611 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003612 WeakUndeclaredIdentifiers);
3613
Douglas Gregor14c22f22009-04-22 22:18:58 +00003614 // Write the record containing locally-scoped external definitions.
3615 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003616 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003617 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003618
3619 // Write the record containing ext_vector type names.
3620 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003621 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003622
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003623 // Write the record containing VTable uses information.
3624 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003625 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003626
3627 // Write the record containing dynamic classes declarations.
3628 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003629 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003630
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003631 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003632 if (!PendingInstantiations.empty())
3633 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003634
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003635 // Write the record containing declaration references of Sema.
3636 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003637 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003638
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003639 // Write the record containing CUDA-specific declaration references.
3640 if (!CUDASpecialDeclRefs.empty())
3641 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003642
3643 // Write the delegating constructors.
3644 if (!DelegatingCtorDecls.empty())
3645 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003646
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003647 // Write the known namespaces.
3648 if (!KnownNamespaces.empty())
3649 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3650
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003651 // Write the visible updates to DeclContexts.
3652 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3653 I = UpdatedDeclContexts.begin(),
3654 E = UpdatedDeclContexts.end();
3655 I != E; ++I)
3656 WriteDeclContextVisibleUpdate(*I);
3657
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003658 if (!WritingModule) {
3659 // Write the submodules that were imported, if any.
3660 RecordData ImportedModules;
3661 for (ASTContext::import_iterator I = Context.local_import_begin(),
3662 IEnd = Context.local_import_end();
3663 I != IEnd; ++I) {
3664 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3665 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3666 }
3667 if (!ImportedModules.empty()) {
3668 // Sort module IDs.
3669 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3670
3671 // Unique module IDs.
3672 ImportedModules.erase(std::unique(ImportedModules.begin(),
3673 ImportedModules.end()),
3674 ImportedModules.end());
3675
3676 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3677 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003678 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003679
3680 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003681 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003682 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003683 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003684 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003685 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003686
Douglas Gregor3e1af842009-04-17 22:13:46 +00003687 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003688 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003689 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003690 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003691 Record.push_back(NumLexicalDeclContexts);
3692 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003693 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003694 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003695}
3696
Douglas Gregora8235d62012-10-09 23:05:51 +00003697void ASTWriter::WriteMacroUpdates() {
3698 if (MacroUpdates.empty())
3699 return;
3700
3701 RecordData Record;
3702 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3703 E = MacroUpdates.end();
3704 I != E; ++I) {
3705 addMacroRef(I->first, Record);
3706 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003707 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003708 }
3709 Stream.EmitRecord(MACRO_UPDATES, Record);
3710}
3711
Douglas Gregor61c5e342011-09-17 00:05:03 +00003712/// \brief Go through the declaration update blocks and resolve declaration
3713/// pointers into declaration IDs.
3714void ASTWriter::ResolveDeclUpdatesBlocks() {
3715 for (DeclUpdateMap::iterator
3716 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3717 const Decl *D = I->first;
3718 UpdateRecord &URec = I->second;
3719
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003720 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003721 continue; // The decl will be written completely
3722
3723 unsigned Idx = 0, N = URec.size();
3724 while (Idx < N) {
3725 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003726 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3727 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3728 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3729 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3730 ++Idx;
3731 break;
3732
3733 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3734 ++Idx;
3735 break;
3736 }
3737 }
3738 }
3739}
3740
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003741void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003742 if (DeclUpdates.empty())
3743 return;
3744
3745 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003746 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003747 for (DeclUpdateMap::iterator
3748 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3749 const Decl *D = I->first;
3750 UpdateRecord &URec = I->second;
3751
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003752 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003753 continue; // The decl will be written completely,no need to store updates.
3754
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003755 uint64_t Offset = Stream.GetCurrentBitNo();
3756 Stream.EmitRecord(DECL_UPDATES, URec);
3757
3758 OffsetsRecord.push_back(GetDeclRef(D));
3759 OffsetsRecord.push_back(Offset);
3760 }
3761 Stream.ExitBlock();
3762 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3763}
3764
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003765void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003766 if (ReplacedDecls.empty())
3767 return;
3768
3769 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003770 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003771 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003772 Record.push_back(I->ID);
3773 Record.push_back(I->Offset);
3774 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003775 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003776 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003777}
3778
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003779void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003780 Record.push_back(Loc.getRawEncoding());
3781}
3782
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003783void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003784 AddSourceLocation(Range.getBegin(), Record);
3785 AddSourceLocation(Range.getEnd(), Record);
3786}
3787
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003788void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003789 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003790 const uint64_t *Words = Value.getRawData();
3791 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003792}
3793
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003794void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003795 Record.push_back(Value.isUnsigned());
3796 AddAPInt(Value, Record);
3797}
3798
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003799void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003800 AddAPInt(Value.bitcastToAPInt(), Record);
3801}
3802
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003803void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003804 Record.push_back(getIdentifierRef(II));
3805}
3806
Douglas Gregora8235d62012-10-09 23:05:51 +00003807void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3808 Record.push_back(getMacroRef(MI));
3809}
3810
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003811IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003812 if (II == 0)
3813 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003814
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003815 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003816 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003817 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003818 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003819}
3820
Douglas Gregora8235d62012-10-09 23:05:51 +00003821MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3822 // Don't emit builtin macros like __LINE__ to the AST file unless they
3823 // have been redefined by the header (in which case they are not
3824 // isBuiltinMacro).
3825 if (MI == 0 || MI->isBuiltinMacro())
3826 return 0;
3827
3828 MacroID &ID = MacroIDs[MI];
3829 if (ID == 0)
3830 ID = NextMacroID++;
3831 return ID;
3832}
3833
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003834void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003835 Record.push_back(getSelectorRef(SelRef));
3836}
3837
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003838SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003839 if (Sel.getAsOpaquePtr() == 0) {
3840 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003841 }
3842
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003843 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003844 if (SID == 0 && Chain) {
3845 // This might trigger a ReadSelector callback, which will set the ID for
3846 // this selector.
3847 Chain->LoadSelector(Sel);
3848 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003849 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003850 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003851 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003852 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003853}
3854
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003855void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003856 AddDeclRef(Temp->getDestructor(), Record);
3857}
3858
Douglas Gregor7c789c12010-10-29 22:39:52 +00003859void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3860 CXXBaseSpecifier const *BasesEnd,
3861 RecordDataImpl &Record) {
3862 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3863 CXXBaseSpecifiersToWrite.push_back(
3864 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3865 Bases, BasesEnd));
3866 Record.push_back(NextCXXBaseSpecifiersID++);
3867}
3868
Sebastian Redla4232eb2010-08-18 23:56:21 +00003869void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003870 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003871 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003872 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003873 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003874 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003875 break;
3876 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003877 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003878 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003879 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003880 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003881 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003882 break;
3883 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003884 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003885 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003886 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003887 break;
John McCall833ca992009-10-29 08:12:44 +00003888 case TemplateArgument::Null:
3889 case TemplateArgument::Integral:
3890 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003891 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003892 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003893 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003894 break;
3895 }
3896}
3897
Sebastian Redla4232eb2010-08-18 23:56:21 +00003898void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003899 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003900 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003901
3902 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3903 bool InfoHasSameExpr
3904 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3905 Record.push_back(InfoHasSameExpr);
3906 if (InfoHasSameExpr)
3907 return; // Avoid storing the same expr twice.
3908 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003909 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3910 Record);
3911}
3912
Douglas Gregordc355712011-02-25 00:36:19 +00003913void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3914 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003915 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003916 AddTypeRef(QualType(), Record);
3917 return;
3918 }
3919
Douglas Gregordc355712011-02-25 00:36:19 +00003920 AddTypeLoc(TInfo->getTypeLoc(), Record);
3921}
3922
3923void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3924 AddTypeRef(TL.getType(), Record);
3925
John McCalla1ee0c52009-10-16 21:56:05 +00003926 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003927 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003928 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003929}
3930
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003931void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003932 Record.push_back(GetOrCreateTypeID(T));
3933}
3934
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003935TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3936 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003937 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3938}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003939
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003940TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003941 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003942 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003943}
3944
3945TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3946 if (T.isNull())
3947 return TypeIdx();
3948 assert(!T.getLocalFastQualifiers());
3949
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003950 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003951 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003952 if (DoneWritingDeclsAndTypes) {
3953 assert(0 && "New type seen after serializing all the types to emit!");
3954 return TypeIdx();
3955 }
3956
Douglas Gregor366809a2009-04-26 03:49:13 +00003957 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003958 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003959 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003960 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003961 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003962 return Idx;
3963}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003964
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003965TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003966 if (T.isNull())
3967 return TypeIdx();
3968 assert(!T.getLocalFastQualifiers());
3969
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003970 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3971 assert(I != TypeIdxs.end() && "Type not emitted!");
3972 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003973}
3974
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003975void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003976 Record.push_back(GetDeclRef(D));
3977}
3978
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003979DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003980 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3981
Douglas Gregor2cf26342009-04-09 22:27:44 +00003982 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003983 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003984 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003985
3986 // If D comes from an AST file, its declaration ID is already known and
3987 // fixed.
3988 if (D->isFromASTFile())
3989 return D->getGlobalID();
3990
Douglas Gregor97475832010-10-05 18:37:06 +00003991 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003992 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003993 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003994 if (DoneWritingDeclsAndTypes) {
3995 assert(0 && "New decl seen after serializing all the decls to emit!");
3996 return 0;
3997 }
3998
Douglas Gregor2cf26342009-04-09 22:27:44 +00003999 // We haven't seen this declaration before. Give it a new ID and
4000 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004001 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004002 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004003 }
4004
Sebastian Redl681d7232010-07-27 00:17:23 +00004005 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004006}
4007
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004008DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004009 if (D == 0)
4010 return 0;
4011
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004012 // If D comes from an AST file, its declaration ID is already known and
4013 // fixed.
4014 if (D->isFromASTFile())
4015 return D->getGlobalID();
4016
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004017 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4018 return DeclIDs[D];
4019}
4020
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004021static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4022 std::pair<unsigned, serialization::DeclID> R) {
4023 return L.first < R.first;
4024}
4025
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004026void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004027 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004028 assert(D);
4029
4030 SourceLocation Loc = D->getLocation();
4031 if (Loc.isInvalid())
4032 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004033
4034 // We only keep track of the file-level declarations of each file.
4035 if (!D->getLexicalDeclContext()->isFileContext())
4036 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004037 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4038 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004039 if (isa<ParmVarDecl>(D))
4040 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004041
4042 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004043 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004044 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004045 FileID FID;
4046 unsigned Offset;
4047 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004048 if (FID.isInvalid())
4049 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004050 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004051
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004052 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004053 if (!Info)
4054 Info = new DeclIDInFileInfo();
4055
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004056 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004057 LocDeclIDsTy &Decls = Info->DeclIDs;
4058
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004059 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004060 Decls.push_back(LocDecl);
4061 return;
4062 }
4063
4064 LocDeclIDsTy::iterator
4065 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4066
4067 Decls.insert(I, LocDecl);
4068}
4069
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004070void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004071 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004072 Record.push_back(Name.getNameKind());
4073 switch (Name.getNameKind()) {
4074 case DeclarationName::Identifier:
4075 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4076 break;
4077
4078 case DeclarationName::ObjCZeroArgSelector:
4079 case DeclarationName::ObjCOneArgSelector:
4080 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004081 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004082 break;
4083
4084 case DeclarationName::CXXConstructorName:
4085 case DeclarationName::CXXDestructorName:
4086 case DeclarationName::CXXConversionFunctionName:
4087 AddTypeRef(Name.getCXXNameType(), Record);
4088 break;
4089
4090 case DeclarationName::CXXOperatorName:
4091 Record.push_back(Name.getCXXOverloadedOperator());
4092 break;
4093
Sean Hunt3e518bd2009-11-29 07:34:05 +00004094 case DeclarationName::CXXLiteralOperatorName:
4095 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4096 break;
4097
Douglas Gregor2cf26342009-04-09 22:27:44 +00004098 case DeclarationName::CXXUsingDirective:
4099 // No extra data to emit
4100 break;
4101 }
4102}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004103
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004104void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004105 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004106 switch (Name.getNameKind()) {
4107 case DeclarationName::CXXConstructorName:
4108 case DeclarationName::CXXDestructorName:
4109 case DeclarationName::CXXConversionFunctionName:
4110 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4111 break;
4112
4113 case DeclarationName::CXXOperatorName:
4114 AddSourceLocation(
4115 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4116 Record);
4117 AddSourceLocation(
4118 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4119 Record);
4120 break;
4121
4122 case DeclarationName::CXXLiteralOperatorName:
4123 AddSourceLocation(
4124 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4125 Record);
4126 break;
4127
4128 case DeclarationName::Identifier:
4129 case DeclarationName::ObjCZeroArgSelector:
4130 case DeclarationName::ObjCOneArgSelector:
4131 case DeclarationName::ObjCMultiArgSelector:
4132 case DeclarationName::CXXUsingDirective:
4133 break;
4134 }
4135}
4136
4137void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004138 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004139 AddDeclarationName(NameInfo.getName(), Record);
4140 AddSourceLocation(NameInfo.getLoc(), Record);
4141 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4142}
4143
4144void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004145 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004146 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004147 Record.push_back(Info.NumTemplParamLists);
4148 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4149 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4150}
4151
Sebastian Redla4232eb2010-08-18 23:56:21 +00004152void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004153 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004154 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004155 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004156 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004157
4158 // Push each of the NNS's onto a stack for serialization in reverse order.
4159 while (NNS) {
4160 NestedNames.push_back(NNS);
4161 NNS = NNS->getPrefix();
4162 }
4163
4164 Record.push_back(NestedNames.size());
4165 while(!NestedNames.empty()) {
4166 NNS = NestedNames.pop_back_val();
4167 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4168 Record.push_back(Kind);
4169 switch (Kind) {
4170 case NestedNameSpecifier::Identifier:
4171 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4172 break;
4173
4174 case NestedNameSpecifier::Namespace:
4175 AddDeclRef(NNS->getAsNamespace(), Record);
4176 break;
4177
Douglas Gregor14aba762011-02-24 02:36:08 +00004178 case NestedNameSpecifier::NamespaceAlias:
4179 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4180 break;
4181
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004182 case NestedNameSpecifier::TypeSpec:
4183 case NestedNameSpecifier::TypeSpecWithTemplate:
4184 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4185 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4186 break;
4187
4188 case NestedNameSpecifier::Global:
4189 // Don't need to write an associated value.
4190 break;
4191 }
4192 }
4193}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004194
Douglas Gregordc355712011-02-25 00:36:19 +00004195void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4196 RecordDataImpl &Record) {
4197 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004198 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004199 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004200
4201 // Push each of the nested-name-specifiers's onto a stack for
4202 // serialization in reverse order.
4203 while (NNS) {
4204 NestedNames.push_back(NNS);
4205 NNS = NNS.getPrefix();
4206 }
4207
4208 Record.push_back(NestedNames.size());
4209 while(!NestedNames.empty()) {
4210 NNS = NestedNames.pop_back_val();
4211 NestedNameSpecifier::SpecifierKind Kind
4212 = NNS.getNestedNameSpecifier()->getKind();
4213 Record.push_back(Kind);
4214 switch (Kind) {
4215 case NestedNameSpecifier::Identifier:
4216 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4217 AddSourceRange(NNS.getLocalSourceRange(), Record);
4218 break;
4219
4220 case NestedNameSpecifier::Namespace:
4221 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4222 AddSourceRange(NNS.getLocalSourceRange(), Record);
4223 break;
4224
4225 case NestedNameSpecifier::NamespaceAlias:
4226 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4227 AddSourceRange(NNS.getLocalSourceRange(), Record);
4228 break;
4229
4230 case NestedNameSpecifier::TypeSpec:
4231 case NestedNameSpecifier::TypeSpecWithTemplate:
4232 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4233 AddTypeLoc(NNS.getTypeLoc(), Record);
4234 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4235 break;
4236
4237 case NestedNameSpecifier::Global:
4238 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4239 break;
4240 }
4241 }
4242}
4243
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004244void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004245 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004246 Record.push_back(Kind);
4247 switch (Kind) {
4248 case TemplateName::Template:
4249 AddDeclRef(Name.getAsTemplateDecl(), Record);
4250 break;
4251
4252 case TemplateName::OverloadedTemplate: {
4253 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4254 Record.push_back(OvT->size());
4255 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4256 I != E; ++I)
4257 AddDeclRef(*I, Record);
4258 break;
4259 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004260
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004261 case TemplateName::QualifiedTemplate: {
4262 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4263 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4264 Record.push_back(QualT->hasTemplateKeyword());
4265 AddDeclRef(QualT->getTemplateDecl(), Record);
4266 break;
4267 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004268
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004269 case TemplateName::DependentTemplate: {
4270 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4271 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4272 Record.push_back(DepT->isIdentifier());
4273 if (DepT->isIdentifier())
4274 AddIdentifierRef(DepT->getIdentifier(), Record);
4275 else
4276 Record.push_back(DepT->getOperator());
4277 break;
4278 }
John McCall14606042011-06-30 08:33:18 +00004279
4280 case TemplateName::SubstTemplateTemplateParm: {
4281 SubstTemplateTemplateParmStorage *subst
4282 = Name.getAsSubstTemplateTemplateParm();
4283 AddDeclRef(subst->getParameter(), Record);
4284 AddTemplateName(subst->getReplacement(), Record);
4285 break;
4286 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004287
4288 case TemplateName::SubstTemplateTemplateParmPack: {
4289 SubstTemplateTemplateParmPackStorage *SubstPack
4290 = Name.getAsSubstTemplateTemplateParmPack();
4291 AddDeclRef(SubstPack->getParameterPack(), Record);
4292 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4293 break;
4294 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004295 }
4296}
4297
Michael J. Spencer20249a12010-10-21 03:16:25 +00004298void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004299 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004300 Record.push_back(Arg.getKind());
4301 switch (Arg.getKind()) {
4302 case TemplateArgument::Null:
4303 break;
4304 case TemplateArgument::Type:
4305 AddTypeRef(Arg.getAsType(), Record);
4306 break;
4307 case TemplateArgument::Declaration:
4308 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004309 Record.push_back(Arg.isDeclForReferenceParam());
4310 break;
4311 case TemplateArgument::NullPtr:
4312 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004313 break;
4314 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004315 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004316 AddTypeRef(Arg.getIntegralType(), Record);
4317 break;
4318 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004319 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4320 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004321 case TemplateArgument::TemplateExpansion:
4322 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004323 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4324 Record.push_back(*NumExpansions + 1);
4325 else
4326 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004327 break;
4328 case TemplateArgument::Expression:
4329 AddStmt(Arg.getAsExpr());
4330 break;
4331 case TemplateArgument::Pack:
4332 Record.push_back(Arg.pack_size());
4333 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4334 I != E; ++I)
4335 AddTemplateArgument(*I, Record);
4336 break;
4337 }
4338}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004339
4340void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004341ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004342 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004343 assert(TemplateParams && "No TemplateParams!");
4344 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4345 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4346 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4347 Record.push_back(TemplateParams->size());
4348 for (TemplateParameterList::const_iterator
4349 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4350 P != PEnd; ++P)
4351 AddDeclRef(*P, Record);
4352}
4353
4354/// \brief Emit a template argument list.
4355void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004356ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004357 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004358 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004359 Record.push_back(TemplateArgs->size());
4360 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004361 AddTemplateArgument(TemplateArgs->get(i), Record);
4362}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004363
4364
4365void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004366ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004367 Record.push_back(Set.size());
4368 for (UnresolvedSetImpl::const_iterator
4369 I = Set.begin(), E = Set.end(); I != E; ++I) {
4370 AddDeclRef(I.getDecl(), Record);
4371 Record.push_back(I.getAccess());
4372 }
4373}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004374
Sebastian Redla4232eb2010-08-18 23:56:21 +00004375void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004376 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004377 Record.push_back(Base.isVirtual());
4378 Record.push_back(Base.isBaseOfClass());
4379 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004380 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004381 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004382 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004383 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4384 : SourceLocation(),
4385 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004386}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004387
Douglas Gregor7c789c12010-10-29 22:39:52 +00004388void ASTWriter::FlushCXXBaseSpecifiers() {
4389 RecordData Record;
4390 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4391 Record.clear();
4392
4393 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004394 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004395 if (Index == CXXBaseSpecifiersOffsets.size())
4396 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4397 else {
4398 if (Index > CXXBaseSpecifiersOffsets.size())
4399 CXXBaseSpecifiersOffsets.resize(Index + 1);
4400 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4401 }
4402
4403 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4404 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4405 Record.push_back(BEnd - B);
4406 for (; B != BEnd; ++B)
4407 AddCXXBaseSpecifier(*B, Record);
4408 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004409
4410 // Flush any expressions that were written as part of the base specifiers.
4411 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004412 }
4413
4414 CXXBaseSpecifiersToWrite.clear();
4415}
4416
Sean Huntcbb67482011-01-08 20:30:50 +00004417void ASTWriter::AddCXXCtorInitializers(
4418 const CXXCtorInitializer * const *CtorInitializers,
4419 unsigned NumCtorInitializers,
4420 RecordDataImpl &Record) {
4421 Record.push_back(NumCtorInitializers);
4422 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4423 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004424
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004425 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004426 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004427 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004428 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004429 } else if (Init->isDelegatingInitializer()) {
4430 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004431 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004432 } else if (Init->isMemberInitializer()){
4433 Record.push_back(CTOR_INITIALIZER_MEMBER);
4434 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004435 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004436 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4437 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004438 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004439
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004440 AddSourceLocation(Init->getMemberLocation(), Record);
4441 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004442 AddSourceLocation(Init->getLParenLoc(), Record);
4443 AddSourceLocation(Init->getRParenLoc(), Record);
4444 Record.push_back(Init->isWritten());
4445 if (Init->isWritten()) {
4446 Record.push_back(Init->getSourceOrder());
4447 } else {
4448 Record.push_back(Init->getNumArrayIndices());
4449 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4450 AddDeclRef(Init->getArrayIndex(i), Record);
4451 }
4452 }
4453}
4454
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004455void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4456 assert(D->DefinitionData);
4457 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004458 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004459 Record.push_back(Data.UserDeclaredConstructor);
4460 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004461 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004462 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004463 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004464 Record.push_back(Data.UserDeclaredDestructor);
4465 Record.push_back(Data.Aggregate);
4466 Record.push_back(Data.PlainOldData);
4467 Record.push_back(Data.Empty);
4468 Record.push_back(Data.Polymorphic);
4469 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004470 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004471 Record.push_back(Data.HasNoNonEmptyBases);
4472 Record.push_back(Data.HasPrivateFields);
4473 Record.push_back(Data.HasProtectedFields);
4474 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004475 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004476 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004477 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004478 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004479 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004480 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004481 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004482 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004483 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004484 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004485 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004486 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004487 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004488 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004489 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004490 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004491 Record.push_back(Data.DeclaredDefaultConstructor);
4492 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004493 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004494 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004495 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004496 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004497 Record.push_back(Data.FailedImplicitMoveConstructor);
4498 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004499 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004500
4501 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004502 if (Data.NumBases > 0)
4503 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4504 Record);
4505
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004506 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4507 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004508 if (Data.NumVBases > 0)
4509 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4510 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004511
4512 AddUnresolvedSet(Data.Conversions, Record);
4513 AddUnresolvedSet(Data.VisibleConversions, Record);
4514 // Data.Definition is the owning decl, no need to write it.
4515 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004516
4517 // Add lambda-specific data.
4518 if (Data.IsLambda) {
4519 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004520 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004521 Record.push_back(Lambda.NumCaptures);
4522 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004523 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004524 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004525 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004526 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4527 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4528 AddSourceLocation(Capture.getLocation(), Record);
4529 Record.push_back(Capture.isImplicit());
4530 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4531 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4532 AddDeclRef(Var, Record);
4533 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4534 : SourceLocation(),
4535 Record);
4536 }
4537 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004538}
4539
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004540void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004541 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004542 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004543 assert(FirstDeclID == NextDeclID &&
4544 FirstTypeID == NextTypeID &&
4545 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004546 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004547 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004548 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004549 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004550
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004551 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004552
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004553 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4554 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4555 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004556 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004557 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004558 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004559 NextDeclID = FirstDeclID;
4560 NextTypeID = FirstTypeID;
4561 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004562 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004563 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004564 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004565}
4566
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004567void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004568 IdentifierIDs[II] = ID;
4569}
4570
Douglas Gregora8235d62012-10-09 23:05:51 +00004571void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4572 MacroIDs[MI] = ID;
4573}
4574
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004575void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004576 // Always take the highest-numbered type index. This copes with an interesting
4577 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004578 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004579 // keep the higher-numbered entry so that we can properly write it out to
4580 // the AST file.
4581 TypeIdx &StoredIdx = TypeIdxs[T];
4582 if (Idx.getIndex() >= StoredIdx.getIndex())
4583 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004584}
4585
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004586void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004587 SelectorIDs[S] = ID;
4588}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004589
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004590void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004591 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004592 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004593 MacroDefinitions[MD] = ID;
4594}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004595
Douglas Gregora015cab2011-12-02 17:30:13 +00004596void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4597 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4598 SubmoduleIDs[Mod] = ID;
4599}
4600
Douglas Gregora8235d62012-10-09 23:05:51 +00004601void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4602 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4603}
4604
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004605void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004606 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004607 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004608 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4609 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004610 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004611 // A forward reference was mutated into a definition. Rewrite it.
4612 // FIXME: This happens during template instantiation, should we
4613 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004614 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004615 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004616 }
4617}
Douglas Gregora8235d62012-10-09 23:05:51 +00004618
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004619void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004620 assert(!WritingAST && "Already writing the AST!");
4621
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004622 // TU and namespaces are handled elsewhere.
4623 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4624 return;
4625
Douglas Gregor919814d2011-09-09 23:01:35 +00004626 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004627 return; // Not a source decl added to a DeclContext from PCH.
4628
4629 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004630 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004631}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004632
4633void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004634 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004635 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004636 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004637 return; // Not a source member added to a class from PCH.
4638 if (!isa<CXXMethodDecl>(D))
4639 return; // We are interested in lazily declared implicit methods.
4640
4641 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004642 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004643 UpdateRecord &Record = DeclUpdates[RD];
4644 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004645 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004646}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004647
4648void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4649 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004650 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004651 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004652 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004653 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004654 return; // Not a source specialization added to a template from PCH.
4655
4656 UpdateRecord &Record = DeclUpdates[TD];
4657 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004658 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004659}
Douglas Gregor89d99802010-11-30 06:16:57 +00004660
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004661void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4662 const FunctionDecl *D) {
4663 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004664 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004665 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004666 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004667 return; // Not a source specialization added to a template from PCH.
4668
4669 UpdateRecord &Record = DeclUpdates[TD];
4670 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004671 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004672}
4673
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004674void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004675 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004676 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004677 return; // Declaration not imported from PCH.
4678
4679 // Implicit decl from a PCH was defined.
4680 // FIXME: Should implicit definition be a separate FunctionDecl?
4681 RewriteDecl(D);
4682}
4683
Sebastian Redlf79a7192011-04-29 08:19:30 +00004684void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004685 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004686 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004687 return;
4688
4689 // Since the actual instantiation is delayed, this really means that we need
4690 // to update the instantiation location.
4691 UpdateRecord &Record = DeclUpdates[D];
4692 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4693 AddSourceLocation(
4694 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4695}
4696
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004697void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4698 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004699 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004700 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004701 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004702
4703 assert(IFD->getDefinition() && "Category on a class without a definition?");
4704 ObjCClassesWithCategories.insert(
4705 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004706}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004707
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004708
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004709void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4710 const ObjCPropertyDecl *OrigProp,
4711 const ObjCCategoryDecl *ClassExt) {
4712 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4713 if (!D)
4714 return;
4715
4716 assert(!WritingAST && "Already writing the AST!");
4717 if (!D->isFromASTFile())
4718 return; // Declaration not imported from PCH.
4719
4720 RewriteDecl(D);
4721}