blob: c37f881d9829fb867e8d2f7311994ab5b43c6e34 [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
Sebastian Redl3397c552010-08-18 23:56:27 +0000981/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000982void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000983 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000984 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000985
Douglas Gregore650c8c2009-07-07 00:12:59 +0000986 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000987 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregor57016dd2012-10-16 23:40:58 +0000988 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000989 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000990 Record.push_back(VERSION_MAJOR);
991 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000992 Record.push_back(CLANG_VERSION_MAJOR);
993 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000994 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000995 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor57016dd2012-10-16 23:40:58 +0000996 AddString(TargetOpts.Triple, Record);
997 AddString(TargetOpts.CPU, Record);
998 AddString(TargetOpts.ABI, Record);
999 AddString(TargetOpts.CXXABI, Record);
1000 AddString(TargetOpts.LinkerVersion, Record);
1001 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1002 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1003 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1004 }
1005 Record.push_back(TargetOpts.Features.size());
1006 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1007 AddString(TargetOpts.Features[I], Record);
1008 }
1009 Stream.EmitRecord(METADATA, Record);
Douglas Gregore95b9192011-08-17 21:07:30 +00001010
1011 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001012 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1013 llvm::SmallVector<char, 128> ModulePaths;
1014 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001015
1016 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1017 M != MEnd; ++M) {
1018 // Skip modules that weren't directly imported.
1019 if (!(*M)->isDirectlyImported())
1020 continue;
1021
1022 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1023 // FIXME: Write import location, once it matters.
1024 // FIXME: This writes the absolute path for AST files we depend on.
1025 const std::string &FileName = (*M)->FileName;
1026 Record.push_back(FileName.size());
1027 Record.append(FileName.begin(), FileName.end());
1028 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001029 Stream.EmitRecord(IMPORTS, Record);
1030 }
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Douglas Gregor31d375f2011-05-06 21:43:30 +00001032 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001033 SourceManager &SM = Context.getSourceManager();
1034 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1035 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001036 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001037 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1038 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1039
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001040 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001042 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001043
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001044 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001045 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001046 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001047 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001048 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001049 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001050
1051 Record.clear();
1052 Record.push_back(SM.getMainFileID().getOpaqueValue());
1053 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001054 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001055
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001056 // Original PCH directory
1057 if (!OutputFile.empty() && OutputFile != "-") {
1058 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1059 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1061 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1062
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001063 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001064
1065 llvm::sys::fs::make_absolute(OutputPath);
1066 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1067
1068 RecordData Record;
1069 Record.push_back(ORIGINAL_PCH_DIR);
1070 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1071 }
1072
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001073 // Repository branch/version information.
1074 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001075 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001076 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1077 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001078 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001079 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001080 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1081 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001082}
1083
1084/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001085void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001086 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001087#define LANGOPT(Name, Bits, Default, Description) \
1088 Record.push_back(LangOpts.Name);
1089#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1090 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1091#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00001092
1093 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1094 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001095
1096 Record.push_back(LangOpts.CurrentModule.size());
1097 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001098 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001099}
1100
Douglas Gregor14f79002009-04-10 03:52:48 +00001101//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102// stat cache Serialization
1103//===----------------------------------------------------------------------===//
1104
1105namespace {
1106// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001107class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001108public:
1109 typedef const char * key_type;
1110 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner74e976b2010-11-23 19:28:12 +00001112 typedef struct stat data_type;
1113 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001114
1115 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001116 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
1119 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001120 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001121 data_type_ref Data) {
1122 unsigned StrLen = strlen(path);
1123 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001124 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001125 clang::io::Emit8(Out, DataLen);
1126 return std::make_pair(StrLen + 1, DataLen);
1127 }
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Chris Lattner5f9e2722011-07-23 10:55:15 +00001129 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001130 Out.write(path, KeyLen);
1131 }
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Chris Lattner5f9e2722011-07-23 10:55:15 +00001133 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001134 data_type_ref Data, unsigned DataLen) {
1135 using namespace clang::io;
1136 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Chris Lattner74e976b2010-11-23 19:28:12 +00001138 Emit32(Out, (uint32_t) Data.st_ino);
1139 Emit32(Out, (uint32_t) Data.st_dev);
1140 Emit16(Out, (uint16_t) Data.st_mode);
1141 Emit64(Out, (uint64_t) Data.st_mtime);
1142 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001143
1144 assert(Out.tell() - Start == DataLen && "Wrong data length");
1145 }
1146};
1147} // end anonymous namespace
1148
Sebastian Redl3397c552010-08-18 23:56:27 +00001149/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001150void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001151 // Build the on-disk hash table containing information about every
1152 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001153 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001154 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001155 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001156 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001157 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001158 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001159 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001162 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001163 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001164 uint32_t BucketOffset;
1165 {
1166 llvm::raw_svector_ostream Out(StatCacheData);
1167 // Make sure that no bucket is at offset 0
1168 clang::io::Emit32(Out, 0);
1169 BucketOffset = Generator.Emit(Out);
1170 }
1171
1172 // Create a blob abbreviation
1173 using namespace llvm;
1174 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001175 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1179 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1180
1181 // Write the stat cache
1182 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001183 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001184 Record.push_back(BucketOffset);
1185 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001186 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001187}
1188
1189//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001190// Source Manager Serialization
1191//===----------------------------------------------------------------------===//
1192
1193/// \brief Create an abbreviation for the SLocEntry that refers to a
1194/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001195static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001196 using namespace llvm;
1197 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001198 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001203 // FileEntry fields.
1204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001211 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001212}
1213
1214/// \brief Create an abbreviation for the SLocEntry that refers to a
1215/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001216static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001217 using namespace llvm;
1218 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001219 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001225 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001226}
1227
1228/// \brief Create an abbreviation for the SLocEntry that refers to a
1229/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001230static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001231 using namespace llvm;
1232 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001233 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001235 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001236}
1237
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001238/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1239/// expansion.
1240static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001241 using namespace llvm;
1242 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001243 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001249 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001250}
1251
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001252namespace {
1253 // Trait used for the on-disk hash table of header search information.
1254 class HeaderFileInfoTrait {
1255 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001256
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001257 // Keep track of the framework names we've used during serialization.
1258 SmallVector<char, 128> FrameworkStringData;
1259 llvm::StringMap<unsigned> FrameworkNameOffset;
1260
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001261 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001262 HeaderFileInfoTrait(ASTWriter &Writer)
1263 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001264
1265 typedef const char *key_type;
1266 typedef key_type key_type_ref;
1267
1268 typedef HeaderFileInfo data_type;
1269 typedef const data_type &data_type_ref;
1270
1271 static unsigned ComputeHash(const char *path) {
1272 // The hash is based only on the filename portion of the key, so that the
1273 // reader can match based on filenames when symlinking or excess path
1274 // elements ("foo/../", "../") change the form of the name. However,
1275 // complete path is still the key.
1276 return llvm::HashString(llvm::sys::path::filename(path));
1277 }
1278
1279 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001280 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001281 data_type_ref Data) {
1282 unsigned StrLen = strlen(path);
1283 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001284 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001285 clang::io::Emit8(Out, DataLen);
1286 return std::make_pair(StrLen + 1, DataLen);
1287 }
1288
Chris Lattner5f9e2722011-07-23 10:55:15 +00001289 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001290 Out.write(path, KeyLen);
1291 }
1292
Chris Lattner5f9e2722011-07-23 10:55:15 +00001293 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001294 data_type_ref Data, unsigned DataLen) {
1295 using namespace clang::io;
1296 uint64_t Start = Out.tell(); (void)Start;
1297
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001298 unsigned char Flags = (Data.isImport << 5)
1299 | (Data.isPragmaOnce << 4)
1300 | (Data.DirInfo << 2)
1301 | (Data.Resolved << 1)
1302 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001303 Emit8(Out, (uint8_t)Flags);
1304 Emit16(Out, (uint16_t) Data.NumIncludes);
1305
1306 if (!Data.ControllingMacro)
1307 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1308 else
1309 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001310
1311 unsigned Offset = 0;
1312 if (!Data.Framework.empty()) {
1313 // If this header refers into a framework, save the framework name.
1314 llvm::StringMap<unsigned>::iterator Pos
1315 = FrameworkNameOffset.find(Data.Framework);
1316 if (Pos == FrameworkNameOffset.end()) {
1317 Offset = FrameworkStringData.size() + 1;
1318 FrameworkStringData.append(Data.Framework.begin(),
1319 Data.Framework.end());
1320 FrameworkStringData.push_back(0);
1321
1322 FrameworkNameOffset[Data.Framework] = Offset;
1323 } else
1324 Offset = Pos->second;
1325 }
1326 Emit32(Out, Offset);
1327
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001328 assert(Out.tell() - Start == DataLen && "Wrong data length");
1329 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001330
1331 const char *strings_begin() const { return FrameworkStringData.begin(); }
1332 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001333 };
1334} // end anonymous namespace
1335
1336/// \brief Write the header search block for the list of files that
1337///
1338/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001339void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001340 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001341 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1342
1343 if (FilesByUID.size() > HS.header_file_size())
1344 FilesByUID.resize(HS.header_file_size());
1345
Benjamin Kramerfacde172012-06-06 17:32:50 +00001346 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001347 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001348 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001349 unsigned NumHeaderSearchEntries = 0;
1350 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1351 const FileEntry *File = FilesByUID[UID];
1352 if (!File)
1353 continue;
1354
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001355 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1356 // from the external source if it was not provided already.
1357 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001358 if (HFI.External && Chain)
1359 continue;
1360
1361 // Turn the file name into an absolute path, if it isn't already.
1362 const char *Filename = File->getName();
1363 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1364
1365 // If we performed any translation on the file name at all, we need to
1366 // save this string, since the generator will refer to it later.
1367 if (Filename != File->getName()) {
1368 Filename = strdup(Filename);
1369 SavedStrings.push_back(Filename);
1370 }
1371
1372 Generator.insert(Filename, HFI, GeneratorTrait);
1373 ++NumHeaderSearchEntries;
1374 }
1375
1376 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001377 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001378 uint32_t BucketOffset;
1379 {
1380 llvm::raw_svector_ostream Out(TableData);
1381 // Make sure that no bucket is at offset 0
1382 clang::io::Emit32(Out, 0);
1383 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1384 }
1385
1386 // Create a blob abbreviation
1387 using namespace llvm;
1388 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1389 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1394 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1395
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001396 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001397 RecordData Record;
1398 Record.push_back(HEADER_SEARCH_TABLE);
1399 Record.push_back(BucketOffset);
1400 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001401 Record.push_back(TableData.size());
1402 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001403 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1404
1405 // Free all of the strings we had to duplicate.
1406 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1407 free((void*)SavedStrings[I]);
1408}
1409
Douglas Gregor14f79002009-04-10 03:52:48 +00001410/// \brief Writes the block containing the serialized form of the
1411/// source manager.
1412///
1413/// TODO: We should probably use an on-disk hash table (stored in a
1414/// blob), indexed based on the file name, so that we only create
1415/// entries for files that we actually need. In the common case (no
1416/// errors), we probably won't have to create file entries for any of
1417/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001418void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001419 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001420 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001421 RecordData Record;
1422
Chris Lattnerf04ad692009-04-10 17:16:57 +00001423 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001424 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001425
1426 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001427 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1428 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1429 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001430 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001431
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001432 // Write out the source location entry table. We skip the first
1433 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001434 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001435 // Write out the offsets of only source location file entries.
1436 // We will go through them in ASTReader::validateFileEntries().
1437 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001438 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001439 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1440 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001441 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001442 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001443 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001444 FileID FID = FileID::get(I);
1445 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001446
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001447 // Record the offset of this source-location entry.
1448 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1449
1450 // Figure out which record code to use.
1451 unsigned Code;
1452 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001453 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1454 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001455 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001456 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1457 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001458 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001459 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001460 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001461 Record.clear();
1462 Record.push_back(Code);
1463
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001464 // Starting offset of this entry within this module, so skip the dummy.
1465 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001466 if (SLoc->isFile()) {
1467 const SrcMgr::FileInfo &File = SLoc->getFile();
1468 Record.push_back(File.getIncludeLoc().getRawEncoding());
1469 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1470 Record.push_back(File.hasLineDirectives());
1471
1472 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001473 if (Content->OrigEntry) {
1474 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001475 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001476
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001477 // The source location entry is a file. The blob associated
1478 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Douglas Gregor2d52be52010-03-21 22:49:54 +00001480 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001481 Record.push_back(Content->OrigEntry->getSize());
1482 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001483 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001484 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001485
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001486 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001487 if (FDI != FileDeclIDs.end()) {
1488 Record.push_back(FDI->second->FirstDeclIndex);
1489 Record.push_back(FDI->second->DeclIDs.size());
1490 } else {
1491 Record.push_back(0);
1492 Record.push_back(0);
1493 }
Douglas Gregora081da52011-11-16 20:05:18 +00001494
Douglas Gregore650c8c2009-07-07 00:12:59 +00001495 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001496 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001497 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001498
1499 // Ask the file manager to fixup the relative path for us. This will
1500 // honor the working directory.
1501 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1502
1503 // FIXME: This call to make_absolute shouldn't be necessary, the
1504 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001505 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001506 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Douglas Gregore650c8c2009-07-07 00:12:59 +00001508 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001509 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001510
1511 if (Content->BufferOverridden) {
1512 Record.clear();
1513 Record.push_back(SM_SLOC_BUFFER_BLOB);
1514 const llvm::MemoryBuffer *Buffer
1515 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1516 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1517 StringRef(Buffer->getBufferStart(),
1518 Buffer->getBufferSize() + 1));
1519 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001520 } else {
1521 // The source location entry is a buffer. The blob associated
1522 // with this entry contains the contents of the buffer.
1523
1524 // We add one to the size so that we capture the trailing NULL
1525 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1526 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001527 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001528 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001529 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001530 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001531 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001533 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001534 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001535 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001536 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001537
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001538 if (strcmp(Name, "<built-in>") == 0) {
1539 PreloadSLocs.push_back(SLocEntryOffsets.size());
1540 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001541 }
1542 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001543 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001544 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001545 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1546 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001547 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1548 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001549
1550 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001551 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001552 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001553 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001554 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001555 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001556 }
1557 }
1558
Douglas Gregorc9490c02009-04-16 22:23:12 +00001559 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001560
1561 if (SLocEntryOffsets.empty())
1562 return;
1563
Sebastian Redl3397c552010-08-18 23:56:27 +00001564 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001565 // table is used for lazily loading source-location information.
1566 using namespace llvm;
1567 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001568 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1572 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001574 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001575 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001576 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001577 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001578 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001579
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001580 Abbrev = new BitCodeAbbrev();
1581 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1584 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1585
1586 Record.clear();
1587 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1588 Record.push_back(SLocFileEntryOffsets.size());
1589 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1590 data(SLocFileEntryOffsets));
1591
Sebastian Redl3397c552010-08-18 23:56:27 +00001592 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001593 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001594 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001595
1596 // Write the line table. It depends on remapping working, so it must come
1597 // after the source location offsets.
1598 if (SourceMgr.hasLineTable()) {
1599 LineTableInfo &LineTable = SourceMgr.getLineTable();
1600
1601 Record.clear();
1602 // Emit the file names
1603 Record.push_back(LineTable.getNumFilenames());
1604 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1605 // Emit the file name
1606 const char *Filename = LineTable.getFilename(I);
1607 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1608 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1609 Record.push_back(FilenameLen);
1610 if (FilenameLen)
1611 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1612 }
1613
1614 // Emit the line entries
1615 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1616 L != LEnd; ++L) {
1617 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001618 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001619 continue;
1620
1621 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001622 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001623
1624 // Emit the line entries
1625 Record.push_back(L->second.size());
1626 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1627 LEEnd = L->second.end();
1628 LE != LEEnd; ++LE) {
1629 Record.push_back(LE->FileOffset);
1630 Record.push_back(LE->LineNo);
1631 Record.push_back(LE->FilenameID);
1632 Record.push_back((unsigned)LE->FileKind);
1633 Record.push_back(LE->IncludeOffset);
1634 }
1635 }
1636 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1637 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001638}
1639
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001640//===----------------------------------------------------------------------===//
1641// Preprocessor Serialization
1642//===----------------------------------------------------------------------===//
1643
Douglas Gregor9c736102011-02-10 18:20:09 +00001644static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1645 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1646 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1647 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1648 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1649 return X.first->getName().compare(Y.first->getName());
1650}
1651
Chris Lattner0b1fb982009-04-10 17:15:23 +00001652/// \brief Writes the block containing the serialized form of the
1653/// preprocessor.
1654///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001655void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001656 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1657 if (PPRec)
1658 WritePreprocessorDetail(*PPRec);
1659
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001660 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001661
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001662 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1663 if (PP.getCounterValue() != 0) {
1664 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001665 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001666 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001667 }
1668
1669 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001670 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Sebastian Redl3397c552010-08-18 23:56:27 +00001672 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001673 // FIXME: use diagnostics subsystem for localization etc.
1674 if (PP.SawDateOrTime())
1675 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Douglas Gregorecdcb882010-10-20 22:00:55 +00001677
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001678 // Loop over all the macro definitions that are live at the end of the file,
1679 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001680
Douglas Gregor9c736102011-02-10 18:20:09 +00001681 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001682 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001683 MacrosToEmit;
1684 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001685 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001686 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001687 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001688 if (!IsModule || I->second->isPublic()) {
1689 MacroDefinitionsSeen.insert(I->first);
1690 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001691 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001692 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001693
Douglas Gregor9c736102011-02-10 18:20:09 +00001694 // Sort the set of macro definitions that need to be serialized by the
1695 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001696 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001697 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001698
Douglas Gregora8235d62012-10-09 23:05:51 +00001699 /// \brief Offsets of each of the macros into the bitstream, indexed by
1700 /// the local macro ID
1701 ///
1702 /// For each identifier that is associated with a macro, this map
1703 /// provides the offset into the bitstream where that macro is
1704 /// defined.
1705 std::vector<uint32_t> MacroOffsets;
1706
Douglas Gregor9c736102011-02-10 18:20:09 +00001707 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1708 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001709
Douglas Gregora8235d62012-10-09 23:05:51 +00001710 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1711 MI = MI->getPreviousDefinition()) {
1712 MacroID ID = getMacroRef(MI);
1713 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001714 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Douglas Gregora8235d62012-10-09 23:05:51 +00001716 // Skip macros from a AST file if we're chaining.
1717 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1718 continue;
1719
1720 if (ID < FirstMacroID) {
1721 // This will have been dealt with via an update record.
1722 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1723 continue;
1724 }
1725
1726 // Record the local offset of this macro.
1727 unsigned Index = ID - FirstMacroID;
1728 if (Index == MacroOffsets.size())
1729 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1730 else {
1731 if (Index > MacroOffsets.size())
1732 MacroOffsets.resize(Index + 1);
1733
1734 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1735 }
1736
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001737 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001738 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001739 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001740 AddSourceLocation(MI->getDefinitionLoc(), Record);
1741 AddSourceLocation(MI->getUndefLoc(), Record);
1742 Record.push_back(MI->isUsed());
1743 Record.push_back(MI->isPublic());
1744 AddSourceLocation(MI->getVisibilityLocation(), Record);
1745 unsigned Code;
1746 if (MI->isObjectLike()) {
1747 Code = PP_MACRO_OBJECT_LIKE;
1748 } else {
1749 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001750
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001751 Record.push_back(MI->isC99Varargs());
1752 Record.push_back(MI->isGNUVarargs());
1753 Record.push_back(MI->getNumArgs());
1754 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1755 I != E; ++I)
1756 AddIdentifierRef(*I, Record);
1757 }
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001759 // If we have a detailed preprocessing record, record the macro definition
1760 // ID that corresponds to this macro.
1761 if (PPRec)
1762 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1763
1764 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001765 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001766
1767 // Emit the tokens array.
1768 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1769 // Note that we know that the preprocessor does not have any annotation
1770 // tokens in it because they are created by the parser, and thus can't
1771 // be in a macro definition.
1772 const Token &Tok = MI->getReplacementToken(TokNo);
1773
1774 Record.push_back(Tok.getLocation().getRawEncoding());
1775 Record.push_back(Tok.getLength());
1776
1777 // FIXME: When reading literal tokens, reconstruct the literal pointer
1778 // if it is needed.
1779 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1780 // FIXME: Should translate token kind to a stable encoding.
1781 Record.push_back(Tok.getKind());
1782 // FIXME: Should translate token flags to a stable encoding.
1783 Record.push_back(Tok.getFlags());
1784
1785 Stream.EmitRecord(PP_TOKEN, Record);
1786 Record.clear();
1787 }
1788 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001789 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001790 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001791 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001792
1793 // Write the offsets table for macro IDs.
1794 using namespace llvm;
1795 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1796 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1800
1801 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1802 Record.clear();
1803 Record.push_back(MACRO_OFFSET);
1804 Record.push_back(MacroOffsets.size());
1805 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1806 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1807 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001808}
1809
1810void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001811 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001812 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001813
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001814 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001815
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001816 // Enter the preprocessor block.
1817 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001818
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001819 // If the preprocessor has a preprocessing record, emit it.
1820 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001821 using namespace llvm;
1822
1823 // Set up the abbreviation for
1824 unsigned InclusionAbbrev = 0;
1825 {
1826 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1827 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1829 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1833 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1834 }
1835
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001836 unsigned FirstPreprocessorEntityID
1837 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1838 + NUM_PREDEF_PP_ENTITY_IDS;
1839 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001840 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001841 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1842 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001843 E != EEnd;
1844 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001845 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001846
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001847 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1848 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001849
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001850 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001851 // Record this macro definition's ID.
1852 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001853
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001854 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001855 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1856 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001857 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001858
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001859 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001860 Record.push_back(ME->isBuiltinMacro());
1861 if (ME->isBuiltinMacro())
1862 AddIdentifierRef(ME->getName(), Record);
1863 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001864 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001865 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001866 continue;
1867 }
1868
1869 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1870 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001871 Record.push_back(ID->getFileName().size());
1872 Record.push_back(ID->wasInQuotes());
1873 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001874 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001875 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001876 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001877 // Check that the FileEntry is not null because it was not resolved and
1878 // we create a PCH even with compiler errors.
1879 if (ID->getFile())
1880 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001881 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1882 continue;
1883 }
1884
1885 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1886 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001887 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001888
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001889 // Write the offsets table for the preprocessing record.
1890 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001891 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1892
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001893 // Write the offsets table for identifier IDs.
1894 using namespace llvm;
1895 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001896 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001899 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001900
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001901 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001902 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001903 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001904 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1905 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001906 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001907}
1908
Douglas Gregore209e502011-12-06 01:10:29 +00001909unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1910 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1911 if (Known != SubmoduleIDs.end())
1912 return Known->second;
1913
1914 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1915}
1916
Douglas Gregor26ced122011-12-01 00:59:36 +00001917/// \brief Compute the number of modules within the given tree (including the
1918/// given module).
1919static unsigned getNumberOfModules(Module *Mod) {
1920 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001921 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1922 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001923 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001924 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001925
1926 return ChildModules + 1;
1927}
1928
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001929void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001930 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001931 // FIXME: This feels like it belongs somewhere else, but there are no
1932 // other consumers of this information.
1933 SourceManager &SrcMgr = PP->getSourceManager();
1934 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1935 for (ASTContext::import_iterator I = Context->local_import_begin(),
1936 IEnd = Context->local_import_end();
1937 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001938 if (Module *ImportedFrom
1939 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1940 SrcMgr))) {
1941 ImportedFrom->Imports.push_back(I->getImportedModule());
1942 }
1943 }
1944
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001945 // Enter the submodule description block.
1946 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1947
1948 // Write the abbreviations needed for the submodules block.
1949 using namespace llvm;
1950 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1951 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1961 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1962
1963 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001964 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1966 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1967
1968 Abbrev = new BitCodeAbbrev();
1969 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1971 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001972
1973 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00001974 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
1975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1976 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
1977
1978 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001979 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1981 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1982
Douglas Gregor51f564f2011-12-31 04:05:44 +00001983 Abbrev = new BitCodeAbbrev();
1984 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1986 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1987
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001988 Abbrev = new BitCodeAbbrev();
1989 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
1990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1991 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
1992
Douglas Gregor26ced122011-12-01 00:59:36 +00001993 // Write the submodule metadata block.
1994 RecordData Record;
1995 Record.push_back(getNumberOfModules(WritingModule));
1996 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1997 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1998
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001999 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002000 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002001 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002002 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002003 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002004 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002005 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002006
2007 // Emit the definition of the block.
2008 Record.clear();
2009 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002010 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002011 if (Mod->Parent) {
2012 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2013 Record.push_back(SubmoduleIDs[Mod->Parent]);
2014 } else {
2015 Record.push_back(0);
2016 }
2017 Record.push_back(Mod->IsFramework);
2018 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002019 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002020 Record.push_back(Mod->InferSubmodules);
2021 Record.push_back(Mod->InferExplicitSubmodules);
2022 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002023 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2024
Douglas Gregor51f564f2011-12-31 04:05:44 +00002025 // Emit the requirements.
2026 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2027 Record.clear();
2028 Record.push_back(SUBMODULE_REQUIRES);
2029 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2030 Mod->Requires[I].data(),
2031 Mod->Requires[I].size());
2032 }
2033
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002034 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002035 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002036 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002037 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002038 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002039 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002040 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2041 Record.clear();
2042 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2043 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2044 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002045 }
2046
2047 // Emit the headers.
2048 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2049 Record.clear();
2050 Record.push_back(SUBMODULE_HEADER);
2051 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2052 Mod->Headers[I]->getName());
2053 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002054 // Emit the excluded headers.
2055 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2056 Record.clear();
2057 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2058 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2059 Mod->ExcludedHeaders[I]->getName());
2060 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002061 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2062 Record.clear();
2063 Record.push_back(SUBMODULE_TOPHEADER);
2064 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2065 Mod->TopHeaders[I]->getName());
2066 }
Douglas Gregor55988682011-12-05 16:33:54 +00002067
2068 // Emit the imports.
2069 if (!Mod->Imports.empty()) {
2070 Record.clear();
2071 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002072 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002073 assert(ImportedID && "Unknown submodule!");
2074 Record.push_back(ImportedID);
2075 }
2076 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2077 }
2078
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002079 // Emit the exports.
2080 if (!Mod->Exports.empty()) {
2081 Record.clear();
2082 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002083 if (Module *Exported = Mod->Exports[I].getPointer()) {
2084 unsigned ExportedID = SubmoduleIDs[Exported];
2085 assert(ExportedID > 0 && "Unknown submodule ID?");
2086 Record.push_back(ExportedID);
2087 } else {
2088 Record.push_back(0);
2089 }
2090
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002091 Record.push_back(Mod->Exports[I].getInt());
2092 }
2093 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2094 }
2095
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002096 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002097 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2098 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002099 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002100 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002101 }
2102
2103 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002104
2105 assert((NextSubmoduleID - FirstSubmoduleID
2106 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002107}
2108
Douglas Gregor185dbd72011-12-01 02:07:58 +00002109serialization::SubmoduleID
2110ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002111 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002112 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002113
2114 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002115 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002116 Module *OwningMod
2117 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002118 if (!OwningMod)
2119 return 0;
2120
Douglas Gregore209e502011-12-06 01:10:29 +00002121 // Check whether this submodule is part of our own module.
2122 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002123 return 0;
2124
Douglas Gregore209e502011-12-06 01:10:29 +00002125 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002126}
2127
David Blaikied6471f72011-09-25 23:23:43 +00002128void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002129 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002130 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002131 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2132 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002133 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002134 if (point.Loc.isInvalid())
2135 continue;
2136
2137 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002138 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002139 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002140 if (I->second.isPragma()) {
2141 Record.push_back(I->first);
2142 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002143 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002144 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002145 Record.push_back(-1); // mark the end of the diag/map pairs for this
2146 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002147 }
2148
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002149 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002150 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002151}
2152
Anders Carlssonc8505782011-03-06 18:41:18 +00002153void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2154 if (CXXBaseSpecifiersOffsets.empty())
2155 return;
2156
2157 RecordData Record;
2158
2159 // Create a blob abbreviation for the C++ base specifiers offsets.
2160 using namespace llvm;
2161
2162 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2163 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2166 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2167
Douglas Gregore92b8a12011-08-04 00:01:48 +00002168 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002169 Record.clear();
2170 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2171 Record.push_back(CXXBaseSpecifiersOffsets.size());
2172 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002173 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002174}
2175
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002176//===----------------------------------------------------------------------===//
2177// Type Serialization
2178//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002179
Sebastian Redl3397c552010-08-18 23:56:27 +00002180/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002181void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002182 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002183 if (Idx.getIndex() == 0) // we haven't seen this type before.
2184 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Douglas Gregor97475832010-10-05 18:37:06 +00002186 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002187
Douglas Gregor2cf26342009-04-09 22:27:44 +00002188 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002189 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002190 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002191 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002192 else if (TypeOffsets.size() < Index) {
2193 TypeOffsets.resize(Index + 1);
2194 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002195 }
2196
2197 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002198
Douglas Gregor2cf26342009-04-09 22:27:44 +00002199 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002200 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002201
Douglas Gregora4923eb2009-11-16 21:35:15 +00002202 if (T.hasLocalNonFastQualifiers()) {
2203 Qualifiers Qs = T.getLocalQualifiers();
2204 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002205 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002206 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002207 } else {
2208 switch (T->getTypeClass()) {
2209 // For all of the concrete, non-dependent types, call the
2210 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002211#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002212 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002213#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002214#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002215 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002216 }
2217
2218 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002219 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002220
2221 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002222 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002223}
2224
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002225//===----------------------------------------------------------------------===//
2226// Declaration Serialization
2227//===----------------------------------------------------------------------===//
2228
Douglas Gregor2cf26342009-04-09 22:27:44 +00002229/// \brief Write the block containing all of the declaration IDs
2230/// lexically declared within the given DeclContext.
2231///
2232/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2233/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002234uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002235 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002236 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002237 return 0;
2238
Douglas Gregorc9490c02009-04-16 22:23:12 +00002239 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002240 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002241 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002242 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002243 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2244 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002245 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002246
Douglas Gregor25123082009-04-22 22:34:57 +00002247 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002248 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002249 return Offset;
2250}
2251
Sebastian Redla4232eb2010-08-18 23:56:21 +00002252void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002253 using namespace llvm;
2254 RecordData Record;
2255
2256 // Write the type offsets array
2257 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002258 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2262 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2263 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002264 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002265 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002266 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002267 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002268
2269 // Write the declaration offsets array
2270 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002271 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2275 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2276 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002277 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002278 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002279 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002280 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002281}
2282
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002283void ASTWriter::WriteFileDeclIDsMap() {
2284 using namespace llvm;
2285 RecordData Record;
2286
2287 // Join the vectors of DeclIDs from all files.
2288 SmallVector<DeclID, 256> FileSortedIDs;
2289 for (FileDeclIDsTy::iterator
2290 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2291 DeclIDInFileInfo &Info = *FI->second;
2292 Info.FirstDeclIndex = FileSortedIDs.size();
2293 for (LocDeclIDsTy::iterator
2294 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2295 FileSortedIDs.push_back(DI->second);
2296 }
2297
2298 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2299 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2302 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2303 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002304 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002305 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2306}
2307
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002308void ASTWriter::WriteComments() {
2309 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002310 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002311 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002312 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2313 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002314 I != E; ++I) {
2315 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002316 AddSourceRange((*I)->getSourceRange(), Record);
2317 Record.push_back((*I)->getKind());
2318 Record.push_back((*I)->isTrailingComment());
2319 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002320 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2321 }
2322 Stream.ExitBlock();
2323}
2324
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002325//===----------------------------------------------------------------------===//
2326// Global Method Pool and Selector Serialization
2327//===----------------------------------------------------------------------===//
2328
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002329namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002330// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002331class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002332 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002333
2334public:
2335 typedef Selector key_type;
2336 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002337
Sebastian Redl5d050072010-08-04 17:20:04 +00002338 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002339 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002340 ObjCMethodList Instance, Factory;
2341 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002342 typedef const data_type& data_type_ref;
2343
Sebastian Redl3397c552010-08-18 23:56:27 +00002344 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002345
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002346 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002347 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002348 }
Mike Stump1eb44332009-09-09 15:08:12 +00002349
2350 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002351 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002352 data_type_ref Methods) {
2353 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2354 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002355 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2356 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002357 Method = Method->Next)
2358 if (Method->Method)
2359 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002360 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002361 Method = Method->Next)
2362 if (Method->Method)
2363 DataLen += 4;
2364 clang::io::Emit16(Out, DataLen);
2365 return std::make_pair(KeyLen, DataLen);
2366 }
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Chris Lattner5f9e2722011-07-23 10:55:15 +00002368 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002369 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002370 assert((Start >> 32) == 0 && "Selector key offset too large");
2371 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002372 unsigned N = Sel.getNumArgs();
2373 clang::io::Emit16(Out, N);
2374 if (N == 0)
2375 N = 1;
2376 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002377 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002378 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2379 }
Mike Stump1eb44332009-09-09 15:08:12 +00002380
Chris Lattner5f9e2722011-07-23 10:55:15 +00002381 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002382 data_type_ref Methods, unsigned DataLen) {
2383 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002384 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002385 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002386 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002387 Method = Method->Next)
2388 if (Method->Method)
2389 ++NumInstanceMethods;
2390
2391 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002392 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002393 Method = Method->Next)
2394 if (Method->Method)
2395 ++NumFactoryMethods;
2396
2397 clang::io::Emit16(Out, NumInstanceMethods);
2398 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002399 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002400 Method = Method->Next)
2401 if (Method->Method)
2402 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002403 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002404 Method = Method->Next)
2405 if (Method->Method)
2406 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002407
2408 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002409 }
2410};
2411} // end anonymous namespace
2412
Sebastian Redl059612d2010-08-03 21:58:15 +00002413/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002414///
2415/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002416/// in an on-disk hash table indexed by the selector. The hash table also
2417/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002418void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002419 using namespace llvm;
2420
Sebastian Redl059612d2010-08-03 21:58:15 +00002421 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002422 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002423 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002424 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002425 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002426 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002427 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002428 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002429
Sebastian Redl059612d2010-08-03 21:58:15 +00002430 // Create the on-disk hash table representation. We walk through every
2431 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002432 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002433 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002434 I = SelectorIDs.begin(), E = SelectorIDs.end();
2435 I != E; ++I) {
2436 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002437 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002438 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002439 I->second,
2440 ObjCMethodList(),
2441 ObjCMethodList()
2442 };
2443 if (F != SemaRef.MethodPool.end()) {
2444 Data.Instance = F->second.first;
2445 Data.Factory = F->second.second;
2446 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002447 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002448 // changed.
2449 if (Chain && I->second < FirstSelectorID) {
2450 // Selector already exists. Did it change?
2451 bool changed = false;
2452 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2453 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002454 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002455 changed = true;
2456 }
2457 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2458 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002459 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002460 changed = true;
2461 }
2462 if (!changed)
2463 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002464 } else if (Data.Instance.Method || Data.Factory.Method) {
2465 // A new method pool entry.
2466 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002467 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002468 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002469 }
2470
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002471 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002472 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002473 uint32_t BucketOffset;
2474 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002475 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002476 llvm::raw_svector_ostream Out(MethodPool);
2477 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002478 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002479 BucketOffset = Generator.Emit(Out, Trait);
2480 }
2481
2482 // Create a blob abbreviation
2483 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002484 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002486 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002487 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2488 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2489
Douglas Gregor83941df2009-04-25 17:48:32 +00002490 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002491 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002492 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002493 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002494 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002495 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002496
2497 // Create a blob abbreviation for the selector table offsets.
2498 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002499 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2503 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2504
2505 // Write the selector offsets table.
2506 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002507 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002508 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002509 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002510 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002511 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002512 }
2513}
2514
Sebastian Redl3397c552010-08-18 23:56:27 +00002515/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002516void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002517 using namespace llvm;
2518 if (SemaRef.ReferencedSelectors.empty())
2519 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002520
Fariborz Jahanian32019832010-07-23 19:11:11 +00002521 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002522
Sebastian Redl3397c552010-08-18 23:56:27 +00002523 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002524 // very tricky to fix, and given that @selector shouldn't really appear in
2525 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002526 for (DenseMap<Selector, SourceLocation>::iterator S =
2527 SemaRef.ReferencedSelectors.begin(),
2528 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2529 Selector Sel = (*S).first;
2530 SourceLocation Loc = (*S).second;
2531 AddSelectorRef(Sel, Record);
2532 AddSourceLocation(Loc, Record);
2533 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002534 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002535}
2536
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002537//===----------------------------------------------------------------------===//
2538// Identifier Table Serialization
2539//===----------------------------------------------------------------------===//
2540
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002541namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002542class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002543 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002544 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002545 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002546 bool IsModule;
2547
Douglas Gregora92193e2009-04-28 21:18:29 +00002548 /// \brief Determines whether this is an "interesting" identifier
2549 /// that needs a full IdentifierInfo structure written into the hash
2550 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002551 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002552 if (II->isPoisoned() ||
2553 II->isExtensionToken() ||
2554 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002555 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002556 II->getFETokenInfo<void>())
2557 return true;
2558
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002559 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002560 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002561
2562 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2563 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002564 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002565
2566 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002567 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002568
2569 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002570 }
2571
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002572public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002573 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002574 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002575
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002576 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002577 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002578
Douglas Gregoreee242f2011-10-27 09:33:13 +00002579 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2580 IdentifierResolver &IdResolver, bool IsModule)
2581 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002582
2583 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002584 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002585 }
Mike Stump1eb44332009-09-09 15:08:12 +00002586
2587 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002588 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002589 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002590 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002591 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002592 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002593 DataLen += 2; // 2 bytes for builtin ID
2594 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002595 if (hadMacroDefinition(II, Macro)) {
2596 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2597 if (Writer.getMacroRef(M) != 0)
2598 DataLen += 4;
2599 }
2600
2601 DataLen += 4;
2602 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002603
Douglas Gregoreee242f2011-10-27 09:33:13 +00002604 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2605 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002606 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002607 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002608 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002609 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002610 // We emit the key length after the data length so that every
2611 // string is preceded by a 16-bit length. This matches the PTH
2612 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002613 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002614 return std::make_pair(KeyLen, DataLen);
2615 }
Mike Stump1eb44332009-09-09 15:08:12 +00002616
Chris Lattner5f9e2722011-07-23 10:55:15 +00002617 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002618 unsigned KeyLen) {
2619 // Record the location of the key data. This is used when generating
2620 // the mapping from persistent IDs to strings.
2621 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002622 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002623 }
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Douglas Gregor7143aab2011-09-01 17:04:32 +00002625 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002626 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002627 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002628 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002629 clang::io::Emit32(Out, ID << 1);
2630 return;
2631 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002632
Douglas Gregora92193e2009-04-28 21:18:29 +00002633 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002634 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2635 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2636 clang::io::Emit16(Out, Bits);
2637 Bits = 0;
2638 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002639 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002640 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2641 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002642 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002643 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002644 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002645
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002646 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002647 // Write all of the macro IDs associated with this identifier.
2648 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2649 if (MacroID ID = Writer.getMacroRef(M))
2650 clang::io::Emit32(Out, ID);
2651 }
2652
2653 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002654 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002655
Douglas Gregor668c1a42009-04-21 22:25:48 +00002656 // Emit the declaration IDs in reverse order, because the
2657 // IdentifierResolver provides the declarations as they would be
2658 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002659 // "stat"), but the ASTReader adds declarations to the end of the list
2660 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002661 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002662 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2663 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002664 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002665 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002666 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002667 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002668 }
2669};
2670} // end anonymous namespace
2671
Sebastian Redl3397c552010-08-18 23:56:27 +00002672/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002673///
2674/// The identifier table consists of a blob containing string data
2675/// (the actual identifiers themselves) and a separate "offsets" index
2676/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002677void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2678 IdentifierResolver &IdResolver,
2679 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002680 using namespace llvm;
2681
2682 // Create and write out the blob that contains the identifier
2683 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002684 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002685 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002686 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002687
Douglas Gregor92b059e2009-04-28 20:33:11 +00002688 // Look for any identifiers that were named while processing the
2689 // headers, but are otherwise not needed. We add these to the hash
2690 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002691 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002692 // file.
2693 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2694 IDEnd = PP.getIdentifierTable().end();
2695 ID != IDEnd; ++ID)
2696 getIdentifierRef(ID->second);
2697
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002698 // Create the on-disk hash table representation. We only store offsets
2699 // for identifiers that appear here for the first time.
2700 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002701 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002702 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2703 ID != IDEnd; ++ID) {
2704 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002705 if (!Chain || !ID->first->isFromAST() ||
2706 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002707 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2708 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002709 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002710
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002711 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002712 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002713 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002714 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002715 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002716 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002717 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002718 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002719 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002720 }
2721
2722 // Create a blob abbreviation
2723 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002724 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002727 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002728
2729 // Write the identifier table
2730 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002731 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002732 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002733 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002734 }
2735
2736 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002737 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002738 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2742 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2743
2744 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002745 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002746 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002747 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002748 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002749 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002750}
2751
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002752//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002753// DeclContext's Name Lookup Table Serialization
2754//===----------------------------------------------------------------------===//
2755
2756namespace {
2757// Trait used for the on-disk hash table used in the method pool.
2758class ASTDeclContextNameLookupTrait {
2759 ASTWriter &Writer;
2760
2761public:
2762 typedef DeclarationName key_type;
2763 typedef key_type key_type_ref;
2764
2765 typedef DeclContext::lookup_result data_type;
2766 typedef const data_type& data_type_ref;
2767
2768 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2769
2770 unsigned ComputeHash(DeclarationName Name) {
2771 llvm::FoldingSetNodeID ID;
2772 ID.AddInteger(Name.getNameKind());
2773
2774 switch (Name.getNameKind()) {
2775 case DeclarationName::Identifier:
2776 ID.AddString(Name.getAsIdentifierInfo()->getName());
2777 break;
2778 case DeclarationName::ObjCZeroArgSelector:
2779 case DeclarationName::ObjCOneArgSelector:
2780 case DeclarationName::ObjCMultiArgSelector:
2781 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2782 break;
2783 case DeclarationName::CXXConstructorName:
2784 case DeclarationName::CXXDestructorName:
2785 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002786 break;
2787 case DeclarationName::CXXOperatorName:
2788 ID.AddInteger(Name.getCXXOverloadedOperator());
2789 break;
2790 case DeclarationName::CXXLiteralOperatorName:
2791 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2792 case DeclarationName::CXXUsingDirective:
2793 break;
2794 }
2795
2796 return ID.ComputeHash();
2797 }
2798
2799 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002800 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002801 data_type_ref Lookup) {
2802 unsigned KeyLen = 1;
2803 switch (Name.getNameKind()) {
2804 case DeclarationName::Identifier:
2805 case DeclarationName::ObjCZeroArgSelector:
2806 case DeclarationName::ObjCOneArgSelector:
2807 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002808 case DeclarationName::CXXLiteralOperatorName:
2809 KeyLen += 4;
2810 break;
2811 case DeclarationName::CXXOperatorName:
2812 KeyLen += 1;
2813 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002814 case DeclarationName::CXXConstructorName:
2815 case DeclarationName::CXXDestructorName:
2816 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002817 case DeclarationName::CXXUsingDirective:
2818 break;
2819 }
2820 clang::io::Emit16(Out, KeyLen);
2821
2822 // 2 bytes for num of decls and 4 for each DeclID.
2823 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2824 clang::io::Emit16(Out, DataLen);
2825
2826 return std::make_pair(KeyLen, DataLen);
2827 }
2828
Chris Lattner5f9e2722011-07-23 10:55:15 +00002829 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002830 using namespace clang::io;
2831
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002832 Emit8(Out, Name.getNameKind());
2833 switch (Name.getNameKind()) {
2834 case DeclarationName::Identifier:
2835 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002836 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002837 case DeclarationName::ObjCZeroArgSelector:
2838 case DeclarationName::ObjCOneArgSelector:
2839 case DeclarationName::ObjCMultiArgSelector:
2840 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002841 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002842 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002843 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2844 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002845 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002846 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002847 case DeclarationName::CXXLiteralOperatorName:
2848 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002849 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002850 case DeclarationName::CXXConstructorName:
2851 case DeclarationName::CXXDestructorName:
2852 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002853 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002854 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002855 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002856
2857 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002858 }
2859
Chris Lattner5f9e2722011-07-23 10:55:15 +00002860 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002861 data_type Lookup, unsigned DataLen) {
2862 uint64_t Start = Out.tell(); (void)Start;
2863 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2864 for (; Lookup.first != Lookup.second; ++Lookup.first)
2865 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2866
2867 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2868 }
2869};
2870} // end anonymous namespace
2871
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002872/// \brief Write the block containing all of the declaration IDs
2873/// visible from the given DeclContext.
2874///
2875/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002876/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002877uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2878 DeclContext *DC) {
2879 if (DC->getPrimaryContext() != DC)
2880 return 0;
2881
2882 // Since there is no name lookup into functions or methods, don't bother to
2883 // build a visible-declarations table for these entities.
2884 if (DC->isFunctionOrMethod())
2885 return 0;
2886
2887 // If not in C++, we perform name lookup for the translation unit via the
2888 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2889 // FIXME: In C++ we need the visible declarations in order to "see" the
2890 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002891 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002892 return 0;
2893
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002894 // Serialize the contents of the mapping used for lookup. Note that,
2895 // although we have two very different code paths, the serialized
2896 // representation is the same for both cases: a declaration name,
2897 // followed by a size, followed by references to the visible
2898 // declarations that have that name.
2899 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002900 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002901 if (!Map || Map->empty())
2902 return 0;
2903
2904 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2905 ASTDeclContextNameLookupTrait Trait(*this);
2906
2907 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002908 DeclarationName ConversionName;
2909 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002910 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2911 D != DEnd; ++D) {
2912 DeclarationName Name = D->first;
2913 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002914 if (Result.first != Result.second) {
2915 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2916 // Hash all conversion function names to the same name. The actual
2917 // type information in conversion function name is not used in the
2918 // key (since such type information is not stable across different
2919 // modules), so the intended effect is to coalesce all of the conversion
2920 // functions under a single key.
2921 if (!ConversionName)
2922 ConversionName = Name;
2923 ConversionDecls.append(Result.first, Result.second);
2924 continue;
2925 }
2926
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002927 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002928 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002929 }
2930
Douglas Gregore5a54b62011-08-30 20:49:19 +00002931 // Add the conversion functions
2932 if (!ConversionDecls.empty()) {
2933 Generator.insert(ConversionName,
2934 DeclContext::lookup_result(ConversionDecls.begin(),
2935 ConversionDecls.end()),
2936 Trait);
2937 }
2938
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002939 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002940 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002941 uint32_t BucketOffset;
2942 {
2943 llvm::raw_svector_ostream Out(LookupTable);
2944 // Make sure that no bucket is at offset 0
2945 clang::io::Emit32(Out, 0);
2946 BucketOffset = Generator.Emit(Out, Trait);
2947 }
2948
2949 // Write the lookup table
2950 RecordData Record;
2951 Record.push_back(DECL_CONTEXT_VISIBLE);
2952 Record.push_back(BucketOffset);
2953 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2954 LookupTable.str());
2955
2956 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2957 ++NumVisibleDeclContexts;
2958 return Offset;
2959}
2960
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002961/// \brief Write an UPDATE_VISIBLE block for the given context.
2962///
2963/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2964/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00002965/// (in C++), for namespaces, and for classes with forward-declared unscoped
2966/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002967void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002968 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2969 if (!Map || Map->empty())
2970 return;
2971
2972 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2973 ASTDeclContextNameLookupTrait Trait(*this);
2974
2975 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002976 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2977 D != DEnd; ++D) {
2978 DeclarationName Name = D->first;
2979 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002980 // For any name that appears in this table, the results are complete, i.e.
2981 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002982 if (Result.first != Result.second)
2983 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002984 }
2985
2986 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002987 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002988 uint32_t BucketOffset;
2989 {
2990 llvm::raw_svector_ostream Out(LookupTable);
2991 // Make sure that no bucket is at offset 0
2992 clang::io::Emit32(Out, 0);
2993 BucketOffset = Generator.Emit(Out, Trait);
2994 }
2995
2996 // Write the lookup table
2997 RecordData Record;
2998 Record.push_back(UPDATE_VISIBLE);
2999 Record.push_back(getDeclID(cast<Decl>(DC)));
3000 Record.push_back(BucketOffset);
3001 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3002}
3003
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003004/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3005void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3006 RecordData Record;
3007 Record.push_back(Opts.fp_contract);
3008 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3009}
3010
3011/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3012void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003013 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003014 return;
3015
3016 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3017 RecordData Record;
3018#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3019#include "clang/Basic/OpenCLExtensions.def"
3020 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3021}
3022
Douglas Gregor2171bf12012-01-15 16:58:34 +00003023void ASTWriter::WriteRedeclarations() {
3024 RecordData LocalRedeclChains;
3025 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3026
3027 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3028 Decl *First = Redeclarations[I];
3029 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3030
3031 Decl *MostRecent = First->getMostRecentDecl();
3032
3033 // If we only have a single declaration, there is no point in storing
3034 // a redeclaration chain.
3035 if (First == MostRecent)
3036 continue;
3037
3038 unsigned Offset = LocalRedeclChains.size();
3039 unsigned Size = 0;
3040 LocalRedeclChains.push_back(0); // Placeholder for the size.
3041
3042 // Collect the set of local redeclarations of this declaration.
3043 for (Decl *Prev = MostRecent; Prev != First;
3044 Prev = Prev->getPreviousDecl()) {
3045 if (!Prev->isFromASTFile()) {
3046 AddDeclRef(Prev, LocalRedeclChains);
3047 ++Size;
3048 }
3049 }
3050 LocalRedeclChains[Offset] = Size;
3051
3052 // Reverse the set of local redeclarations, so that we store them in
3053 // order (since we found them in reverse order).
3054 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3055
3056 // Add the mapping from the first ID to the set of local declarations.
3057 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3058 LocalRedeclsMap.push_back(Info);
3059
3060 assert(N == Redeclarations.size() &&
3061 "Deserialized a declaration we shouldn't have");
3062 }
3063
3064 if (LocalRedeclChains.empty())
3065 return;
3066
3067 // Sort the local redeclarations map by the first declaration ID,
3068 // since the reader will be performing binary searches on this information.
3069 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3070
3071 // Emit the local redeclarations map.
3072 using namespace llvm;
3073 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3074 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3077 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3078
3079 RecordData Record;
3080 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3081 Record.push_back(LocalRedeclsMap.size());
3082 Stream.EmitRecordWithBlob(AbbrevID, Record,
3083 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3084 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3085
3086 // Emit the redeclaration chains.
3087 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3088}
3089
Douglas Gregorcff9f262012-01-27 01:47:08 +00003090void ASTWriter::WriteObjCCategories() {
3091 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3092 RecordData Categories;
3093
3094 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3095 unsigned Size = 0;
3096 unsigned StartIndex = Categories.size();
3097
3098 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3099
3100 // Allocate space for the size.
3101 Categories.push_back(0);
3102
3103 // Add the categories.
3104 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3105 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3106 assert(getDeclID(Cat) != 0 && "Bogus category");
3107 AddDeclRef(Cat, Categories);
3108 }
3109
3110 // Update the size.
3111 Categories[StartIndex] = Size;
3112
3113 // Record this interface -> category map.
3114 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3115 CategoriesMap.push_back(CatInfo);
3116 }
3117
3118 // Sort the categories map by the definition ID, since the reader will be
3119 // performing binary searches on this information.
3120 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3121
3122 // Emit the categories map.
3123 using namespace llvm;
3124 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3125 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3128 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3129
3130 RecordData Record;
3131 Record.push_back(OBJC_CATEGORIES_MAP);
3132 Record.push_back(CategoriesMap.size());
3133 Stream.EmitRecordWithBlob(AbbrevID, Record,
3134 reinterpret_cast<char*>(CategoriesMap.data()),
3135 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3136
3137 // Emit the category lists.
3138 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3139}
3140
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003141void ASTWriter::WriteMergedDecls() {
3142 if (!Chain || Chain->MergedDecls.empty())
3143 return;
3144
3145 RecordData Record;
3146 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3147 IEnd = Chain->MergedDecls.end();
3148 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003149 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003150 : getDeclID(I->first);
3151 assert(CanonID && "Merged declaration not known?");
3152
3153 Record.push_back(CanonID);
3154 Record.push_back(I->second.size());
3155 Record.append(I->second.begin(), I->second.end());
3156 }
3157 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3158}
3159
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003160//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003161// General Serialization Routines
3162//===----------------------------------------------------------------------===//
3163
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003164/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003165void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3166 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003167 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003168 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3169 e = Attrs.end(); i != e; ++i){
3170 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003171 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003172 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003173
Sean Huntcf807c42010-08-18 23:23:40 +00003174#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003175
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003176 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003177}
3178
Chris Lattner5f9e2722011-07-23 10:55:15 +00003179void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003180 Record.push_back(Str.size());
3181 Record.insert(Record.end(), Str.begin(), Str.end());
3182}
3183
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003184void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3185 RecordDataImpl &Record) {
3186 Record.push_back(Version.getMajor());
3187 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3188 Record.push_back(*Minor + 1);
3189 else
3190 Record.push_back(0);
3191 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3192 Record.push_back(*Subminor + 1);
3193 else
3194 Record.push_back(0);
3195}
3196
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003197/// \brief Note that the identifier II occurs at the given offset
3198/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003199void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003200 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003201 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003202 // up earlier in the chain and thus don't need an offset.
3203 if (ID >= FirstIdentID)
3204 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003205}
3206
Douglas Gregor83941df2009-04-25 17:48:32 +00003207/// \brief Note that the selector Sel occurs at the given offset
3208/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003209void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003210 unsigned ID = SelectorIDs[Sel];
3211 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003212 // Don't record offsets for selectors that are also available in a different
3213 // file.
3214 if (ID < FirstSelectorID)
3215 return;
3216 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003217}
3218
Sebastian Redla4232eb2010-08-18 23:56:21 +00003219ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003220 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003221 WritingAST(false), DoneWritingDeclsAndTypes(false),
3222 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003223 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003224 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003225 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3226 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003227 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3228 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003229 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003230 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003231 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003232 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003233 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003234 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003235 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3236 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3237 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003238 DeclTypedefAbbrev(0),
3239 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3240 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003241{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003242}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003243
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003244ASTWriter::~ASTWriter() {
3245 for (FileDeclIDsTy::iterator
3246 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3247 delete I->second;
3248}
3249
Sebastian Redla4232eb2010-08-18 23:56:21 +00003250void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003251 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003252 Module *WritingModule, StringRef isysroot,
3253 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003254 WritingAST = true;
3255
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003256 ASTHasCompilerErrors = hasErrors;
3257
Douglas Gregor2cf26342009-04-09 22:27:44 +00003258 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003259 Stream.Emit((unsigned)'C', 8);
3260 Stream.Emit((unsigned)'P', 8);
3261 Stream.Emit((unsigned)'C', 8);
3262 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003263
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003264 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003265
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003266 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003267 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003268 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003269 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003270 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003271 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003272 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003273
3274 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003275}
3276
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003277template<typename Vector>
3278static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3279 ASTWriter::RecordData &Record) {
3280 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3281 I != E; ++I) {
3282 Writer.AddDeclRef(*I, Record);
3283 }
3284}
3285
Sebastian Redla4232eb2010-08-18 23:56:21 +00003286void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003287 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003288 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003289 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003290 using namespace llvm;
3291
Douglas Gregorecc2c092011-12-01 22:20:10 +00003292 // Make sure that the AST reader knows to finalize itself.
3293 if (Chain)
3294 Chain->finalizeForWriting();
3295
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003296 ASTContext &Context = SemaRef.Context;
3297 Preprocessor &PP = SemaRef.PP;
3298
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003299 // Set up predefined declaration IDs.
3300 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003301 if (Context.ObjCIdDecl)
3302 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003303 if (Context.ObjCSelDecl)
3304 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003305 if (Context.ObjCClassDecl)
3306 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003307 if (Context.ObjCProtocolClassDecl)
3308 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003309 if (Context.Int128Decl)
3310 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3311 if (Context.UInt128Decl)
3312 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003313 if (Context.ObjCInstanceTypeDecl)
3314 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003315 if (Context.BuiltinVaListDecl)
3316 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3317
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003318 if (!Chain) {
3319 // Make sure that we emit IdentifierInfos (and any attached
3320 // declarations) for builtins. We don't need to do this when we're
3321 // emitting chained PCH files, because all of the builtins will be
3322 // in the original PCH file.
3323 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003324 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003325 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003326 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003327 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003328 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3329 getIdentifierRef(&Table.get(BuiltinNames[I]));
3330 }
3331
Douglas Gregoreee242f2011-10-27 09:33:13 +00003332 // If there are any out-of-date identifiers, bring them up to date.
3333 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3334 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3335 IDEnd = PP.getIdentifierTable().end();
3336 ID != IDEnd; ++ID)
3337 if (ID->second->isOutOfDate())
3338 ExtSource->updateOutOfDateIdentifier(*ID->second);
3339 }
3340
Chris Lattner63d65f82009-09-08 18:19:27 +00003341 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003342 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003343 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003344 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003345 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003346
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003347 // Build a record containing all of the file scoped decls in this file.
3348 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003349 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3350 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003351
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003352 // Build a record containing all of the delegating constructors we still need
3353 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003354 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003355 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003356
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003357 // Write the set of weak, undeclared identifiers. We always write the
3358 // entire table, since later PCH files in a PCH chain are only interested in
3359 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003360 RecordData WeakUndeclaredIdentifiers;
3361 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003362 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003363 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3364 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3365 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3366 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3367 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3368 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3369 }
3370 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003371
Douglas Gregor14c22f22009-04-22 22:18:58 +00003372 // Build a record containing all of the locally-scoped external
3373 // declarations in this header file. Generally, this record will be
3374 // empty.
3375 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003376 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003377 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003378 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003379 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3380 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003381 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003382 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003383 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3384 }
3385
Douglas Gregorb81c1702009-04-27 20:06:05 +00003386 // Build a record containing all of the ext_vector declarations.
3387 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003388 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003389
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003390 // Build a record containing all of the VTable uses information.
3391 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003392 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003393 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3394 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3395 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3396 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3397 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003398 }
3399
3400 // Build a record containing all of dynamic classes declarations.
3401 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003402 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003403
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003404 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003405 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003406 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003407 I = SemaRef.PendingInstantiations.begin(),
3408 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3409 AddDeclRef(I->first, PendingInstantiations);
3410 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003411 }
3412 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3413 "There are local ones at end of translation unit!");
3414
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003415 // Build a record containing some declaration references.
3416 RecordData SemaDeclRefs;
3417 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3418 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3419 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3420 }
3421
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003422 RecordData CUDASpecialDeclRefs;
3423 if (Context.getcudaConfigureCallDecl()) {
3424 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3425 }
3426
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003427 // Build a record containing all of the known namespaces.
3428 RecordData KnownNamespaces;
3429 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3430 I = SemaRef.KnownNamespaces.begin(),
3431 IEnd = SemaRef.KnownNamespaces.end();
3432 I != IEnd; ++I) {
3433 if (!I->second)
3434 AddDeclRef(I->first, KnownNamespaces);
3435 }
3436
Sebastian Redl3397c552010-08-18 23:56:27 +00003437 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003438 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003439 Stream.EnterSubblock(AST_BLOCK_ID, 5);
David Blaikie4e4d0842012-03-11 07:00:24 +00003440 WriteLanguageOptions(Context.getLangOpts());
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003441 WriteMetadata(Context, isysroot, OutputFile);
Douglas Gregor832d6202011-07-22 16:35:34 +00003442 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003443 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003444
3445 // Create a lexical update block containing all of the declarations in the
3446 // translation unit that do not come from other AST files.
3447 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3448 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3449 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3450 E = TU->noload_decls_end();
3451 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003452 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003453 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003454 }
3455
3456 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3457 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3458 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3459 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3460 Record.clear();
3461 Record.push_back(TU_UPDATE_LEXICAL);
3462 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3463 data(NewGlobalDecls));
3464
3465 // And a visible updates block for the translation unit.
3466 Abv = new llvm::BitCodeAbbrev();
3467 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3468 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3469 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3470 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3471 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3472 WriteDeclContextVisibleUpdate(TU);
3473
3474 // If the translation unit has an anonymous namespace, and we don't already
3475 // have an update block for it, write it as an update block.
3476 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3477 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3478 if (Record.empty()) {
3479 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003480 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003481 }
3482 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003483
3484 // Make sure visible decls, added to DeclContexts previously loaded from
3485 // an AST file, are registered for serialization.
3486 for (SmallVector<const Decl *, 16>::iterator
3487 I = UpdatingVisibleDecls.begin(),
3488 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3489 GetDeclRef(*I);
3490 }
3491
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003492 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003493 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003494
Douglas Gregora119da02011-08-02 16:26:37 +00003495 // Form the record of special types.
3496 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003497 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003498 AddTypeRef(Context.getFILEType(), SpecialTypes);
3499 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3500 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3501 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3502 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003503 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003504 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003505
Douglas Gregor366809a2009-04-26 03:49:13 +00003506 // Keep writing types and declarations until all types and
3507 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003508 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003509 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003510 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3511 E = DeclsToRewrite.end();
3512 I != E; ++I)
3513 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003514 while (!DeclTypesToEmit.empty()) {
3515 DeclOrType DOT = DeclTypesToEmit.front();
3516 DeclTypesToEmit.pop();
3517 if (DOT.isType())
3518 WriteType(DOT.getType());
3519 else
3520 WriteDecl(Context, DOT.getDecl());
3521 }
3522 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003523
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003524 DoneWritingDeclsAndTypes = true;
3525
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003526 WriteFileDeclIDsMap();
3527 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003528 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003529
3530 if (Chain) {
3531 // Write the mapping information describing our module dependencies and how
3532 // each of those modules were mapped into our own offset/ID space, so that
3533 // the reader can build the appropriate mapping to its own offset/ID space.
3534 // The map consists solely of a blob with the following format:
3535 // *(module-name-len:i16 module-name:len*i8
3536 // source-location-offset:i32
3537 // identifier-id:i32
3538 // preprocessed-entity-id:i32
3539 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003540 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003541 // selector-id:i32
3542 // declaration-id:i32
3543 // c++-base-specifiers-id:i32
3544 // type-id:i32)
3545 //
3546 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3547 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3549 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003550 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003551 {
3552 llvm::raw_svector_ostream Out(Buffer);
3553 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003554 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003555 M != MEnd; ++M) {
3556 StringRef FileName = (*M)->FileName;
3557 io::Emit16(Out, FileName.size());
3558 Out.write(FileName.data(), FileName.size());
3559 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3560 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003561 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003562 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003563 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003564 io::Emit32(Out, (*M)->BaseSelectorID);
3565 io::Emit32(Out, (*M)->BaseDeclID);
3566 io::Emit32(Out, (*M)->BaseTypeIndex);
3567 }
3568 }
3569 Record.clear();
3570 Record.push_back(MODULE_OFFSET_MAP);
3571 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3572 Buffer.data(), Buffer.size());
3573 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003574 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003575 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003576 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003577 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003578 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003579 WriteFPPragmaOptions(SemaRef.getFPOptions());
3580 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003581
Sebastian Redl1476ed42010-07-16 16:36:56 +00003582 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003583 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003584
Anders Carlssonc8505782011-03-06 18:41:18 +00003585 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003586
Douglas Gregore209e502011-12-06 01:10:29 +00003587 // If we're emitting a module, write out the submodule information.
3588 if (WritingModule)
3589 WriteSubmodules(WritingModule);
3590
Douglas Gregora119da02011-08-02 16:26:37 +00003591 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3592
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003593 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003594 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003595 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003596
3597 // Write the record containing tentative definitions.
3598 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003599 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003600
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003601 // Write the record containing unused file scoped decls.
3602 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003603 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003604
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003605 // Write the record containing weak undeclared identifiers.
3606 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003607 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003608 WeakUndeclaredIdentifiers);
3609
Douglas Gregor14c22f22009-04-22 22:18:58 +00003610 // Write the record containing locally-scoped external definitions.
3611 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003612 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003613 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003614
3615 // Write the record containing ext_vector type names.
3616 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003617 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003618
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003619 // Write the record containing VTable uses information.
3620 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003621 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003622
3623 // Write the record containing dynamic classes declarations.
3624 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003625 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003626
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003627 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003628 if (!PendingInstantiations.empty())
3629 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003630
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003631 // Write the record containing declaration references of Sema.
3632 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003633 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003634
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003635 // Write the record containing CUDA-specific declaration references.
3636 if (!CUDASpecialDeclRefs.empty())
3637 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003638
3639 // Write the delegating constructors.
3640 if (!DelegatingCtorDecls.empty())
3641 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003642
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003643 // Write the known namespaces.
3644 if (!KnownNamespaces.empty())
3645 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3646
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003647 // Write the visible updates to DeclContexts.
3648 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3649 I = UpdatedDeclContexts.begin(),
3650 E = UpdatedDeclContexts.end();
3651 I != E; ++I)
3652 WriteDeclContextVisibleUpdate(*I);
3653
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003654 if (!WritingModule) {
3655 // Write the submodules that were imported, if any.
3656 RecordData ImportedModules;
3657 for (ASTContext::import_iterator I = Context.local_import_begin(),
3658 IEnd = Context.local_import_end();
3659 I != IEnd; ++I) {
3660 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3661 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3662 }
3663 if (!ImportedModules.empty()) {
3664 // Sort module IDs.
3665 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3666
3667 // Unique module IDs.
3668 ImportedModules.erase(std::unique(ImportedModules.begin(),
3669 ImportedModules.end()),
3670 ImportedModules.end());
3671
3672 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3673 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003674 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003675
3676 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003677 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003678 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003679 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003680 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003681 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003682
Douglas Gregor3e1af842009-04-17 22:13:46 +00003683 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003684 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003685 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003686 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003687 Record.push_back(NumLexicalDeclContexts);
3688 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003689 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003690 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003691}
3692
Douglas Gregora8235d62012-10-09 23:05:51 +00003693void ASTWriter::WriteMacroUpdates() {
3694 if (MacroUpdates.empty())
3695 return;
3696
3697 RecordData Record;
3698 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3699 E = MacroUpdates.end();
3700 I != E; ++I) {
3701 addMacroRef(I->first, Record);
3702 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003703 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003704 }
3705 Stream.EmitRecord(MACRO_UPDATES, Record);
3706}
3707
Douglas Gregor61c5e342011-09-17 00:05:03 +00003708/// \brief Go through the declaration update blocks and resolve declaration
3709/// pointers into declaration IDs.
3710void ASTWriter::ResolveDeclUpdatesBlocks() {
3711 for (DeclUpdateMap::iterator
3712 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3713 const Decl *D = I->first;
3714 UpdateRecord &URec = I->second;
3715
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003716 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003717 continue; // The decl will be written completely
3718
3719 unsigned Idx = 0, N = URec.size();
3720 while (Idx < N) {
3721 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003722 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3723 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3724 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3725 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3726 ++Idx;
3727 break;
3728
3729 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3730 ++Idx;
3731 break;
3732 }
3733 }
3734 }
3735}
3736
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003737void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003738 if (DeclUpdates.empty())
3739 return;
3740
3741 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003742 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003743 for (DeclUpdateMap::iterator
3744 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3745 const Decl *D = I->first;
3746 UpdateRecord &URec = I->second;
3747
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003748 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003749 continue; // The decl will be written completely,no need to store updates.
3750
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003751 uint64_t Offset = Stream.GetCurrentBitNo();
3752 Stream.EmitRecord(DECL_UPDATES, URec);
3753
3754 OffsetsRecord.push_back(GetDeclRef(D));
3755 OffsetsRecord.push_back(Offset);
3756 }
3757 Stream.ExitBlock();
3758 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3759}
3760
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003761void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003762 if (ReplacedDecls.empty())
3763 return;
3764
3765 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003766 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003767 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003768 Record.push_back(I->ID);
3769 Record.push_back(I->Offset);
3770 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003771 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003772 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003773}
3774
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003775void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003776 Record.push_back(Loc.getRawEncoding());
3777}
3778
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003779void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003780 AddSourceLocation(Range.getBegin(), Record);
3781 AddSourceLocation(Range.getEnd(), Record);
3782}
3783
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003784void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003785 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003786 const uint64_t *Words = Value.getRawData();
3787 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003788}
3789
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003790void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003791 Record.push_back(Value.isUnsigned());
3792 AddAPInt(Value, Record);
3793}
3794
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003795void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003796 AddAPInt(Value.bitcastToAPInt(), Record);
3797}
3798
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003799void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003800 Record.push_back(getIdentifierRef(II));
3801}
3802
Douglas Gregora8235d62012-10-09 23:05:51 +00003803void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3804 Record.push_back(getMacroRef(MI));
3805}
3806
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003807IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003808 if (II == 0)
3809 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003810
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003811 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003812 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003813 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003814 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003815}
3816
Douglas Gregora8235d62012-10-09 23:05:51 +00003817MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3818 // Don't emit builtin macros like __LINE__ to the AST file unless they
3819 // have been redefined by the header (in which case they are not
3820 // isBuiltinMacro).
3821 if (MI == 0 || MI->isBuiltinMacro())
3822 return 0;
3823
3824 MacroID &ID = MacroIDs[MI];
3825 if (ID == 0)
3826 ID = NextMacroID++;
3827 return ID;
3828}
3829
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003830void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003831 Record.push_back(getSelectorRef(SelRef));
3832}
3833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003834SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003835 if (Sel.getAsOpaquePtr() == 0) {
3836 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003837 }
3838
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003839 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003840 if (SID == 0 && Chain) {
3841 // This might trigger a ReadSelector callback, which will set the ID for
3842 // this selector.
3843 Chain->LoadSelector(Sel);
3844 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003845 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003846 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003847 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003848 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003849}
3850
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003851void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003852 AddDeclRef(Temp->getDestructor(), Record);
3853}
3854
Douglas Gregor7c789c12010-10-29 22:39:52 +00003855void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3856 CXXBaseSpecifier const *BasesEnd,
3857 RecordDataImpl &Record) {
3858 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3859 CXXBaseSpecifiersToWrite.push_back(
3860 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3861 Bases, BasesEnd));
3862 Record.push_back(NextCXXBaseSpecifiersID++);
3863}
3864
Sebastian Redla4232eb2010-08-18 23:56:21 +00003865void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003866 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003867 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003868 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003869 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003870 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003871 break;
3872 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003873 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003874 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003875 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003876 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003877 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003878 break;
3879 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003880 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003881 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003882 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003883 break;
John McCall833ca992009-10-29 08:12:44 +00003884 case TemplateArgument::Null:
3885 case TemplateArgument::Integral:
3886 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003887 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003888 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003889 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003890 break;
3891 }
3892}
3893
Sebastian Redla4232eb2010-08-18 23:56:21 +00003894void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003895 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003896 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003897
3898 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3899 bool InfoHasSameExpr
3900 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3901 Record.push_back(InfoHasSameExpr);
3902 if (InfoHasSameExpr)
3903 return; // Avoid storing the same expr twice.
3904 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003905 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3906 Record);
3907}
3908
Douglas Gregordc355712011-02-25 00:36:19 +00003909void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3910 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003911 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003912 AddTypeRef(QualType(), Record);
3913 return;
3914 }
3915
Douglas Gregordc355712011-02-25 00:36:19 +00003916 AddTypeLoc(TInfo->getTypeLoc(), Record);
3917}
3918
3919void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3920 AddTypeRef(TL.getType(), Record);
3921
John McCalla1ee0c52009-10-16 21:56:05 +00003922 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003923 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003924 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003925}
3926
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003927void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003928 Record.push_back(GetOrCreateTypeID(T));
3929}
3930
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003931TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3932 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003933 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3934}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003935
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003936TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003937 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003938 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003939}
3940
3941TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3942 if (T.isNull())
3943 return TypeIdx();
3944 assert(!T.getLocalFastQualifiers());
3945
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003946 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003947 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003948 if (DoneWritingDeclsAndTypes) {
3949 assert(0 && "New type seen after serializing all the types to emit!");
3950 return TypeIdx();
3951 }
3952
Douglas Gregor366809a2009-04-26 03:49:13 +00003953 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003954 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003955 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003956 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003957 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003958 return Idx;
3959}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003960
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003961TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003962 if (T.isNull())
3963 return TypeIdx();
3964 assert(!T.getLocalFastQualifiers());
3965
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003966 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3967 assert(I != TypeIdxs.end() && "Type not emitted!");
3968 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003969}
3970
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003971void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003972 Record.push_back(GetDeclRef(D));
3973}
3974
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003975DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003976 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3977
Douglas Gregor2cf26342009-04-09 22:27:44 +00003978 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003979 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003980 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003981
3982 // If D comes from an AST file, its declaration ID is already known and
3983 // fixed.
3984 if (D->isFromASTFile())
3985 return D->getGlobalID();
3986
Douglas Gregor97475832010-10-05 18:37:06 +00003987 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003988 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003989 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003990 if (DoneWritingDeclsAndTypes) {
3991 assert(0 && "New decl seen after serializing all the decls to emit!");
3992 return 0;
3993 }
3994
Douglas Gregor2cf26342009-04-09 22:27:44 +00003995 // We haven't seen this declaration before. Give it a new ID and
3996 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003997 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003998 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003999 }
4000
Sebastian Redl681d7232010-07-27 00:17:23 +00004001 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004002}
4003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004004DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004005 if (D == 0)
4006 return 0;
4007
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004008 // If D comes from an AST file, its declaration ID is already known and
4009 // fixed.
4010 if (D->isFromASTFile())
4011 return D->getGlobalID();
4012
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004013 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4014 return DeclIDs[D];
4015}
4016
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004017static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4018 std::pair<unsigned, serialization::DeclID> R) {
4019 return L.first < R.first;
4020}
4021
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004022void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004023 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004024 assert(D);
4025
4026 SourceLocation Loc = D->getLocation();
4027 if (Loc.isInvalid())
4028 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004029
4030 // We only keep track of the file-level declarations of each file.
4031 if (!D->getLexicalDeclContext()->isFileContext())
4032 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004033 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4034 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004035 if (isa<ParmVarDecl>(D))
4036 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004037
4038 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004039 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004040 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004041 FileID FID;
4042 unsigned Offset;
4043 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004044 if (FID.isInvalid())
4045 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004046 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004047
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004048 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004049 if (!Info)
4050 Info = new DeclIDInFileInfo();
4051
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004052 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004053 LocDeclIDsTy &Decls = Info->DeclIDs;
4054
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004055 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004056 Decls.push_back(LocDecl);
4057 return;
4058 }
4059
4060 LocDeclIDsTy::iterator
4061 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4062
4063 Decls.insert(I, LocDecl);
4064}
4065
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004066void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004067 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004068 Record.push_back(Name.getNameKind());
4069 switch (Name.getNameKind()) {
4070 case DeclarationName::Identifier:
4071 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4072 break;
4073
4074 case DeclarationName::ObjCZeroArgSelector:
4075 case DeclarationName::ObjCOneArgSelector:
4076 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004077 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004078 break;
4079
4080 case DeclarationName::CXXConstructorName:
4081 case DeclarationName::CXXDestructorName:
4082 case DeclarationName::CXXConversionFunctionName:
4083 AddTypeRef(Name.getCXXNameType(), Record);
4084 break;
4085
4086 case DeclarationName::CXXOperatorName:
4087 Record.push_back(Name.getCXXOverloadedOperator());
4088 break;
4089
Sean Hunt3e518bd2009-11-29 07:34:05 +00004090 case DeclarationName::CXXLiteralOperatorName:
4091 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4092 break;
4093
Douglas Gregor2cf26342009-04-09 22:27:44 +00004094 case DeclarationName::CXXUsingDirective:
4095 // No extra data to emit
4096 break;
4097 }
4098}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004099
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004100void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004101 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004102 switch (Name.getNameKind()) {
4103 case DeclarationName::CXXConstructorName:
4104 case DeclarationName::CXXDestructorName:
4105 case DeclarationName::CXXConversionFunctionName:
4106 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4107 break;
4108
4109 case DeclarationName::CXXOperatorName:
4110 AddSourceLocation(
4111 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4112 Record);
4113 AddSourceLocation(
4114 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4115 Record);
4116 break;
4117
4118 case DeclarationName::CXXLiteralOperatorName:
4119 AddSourceLocation(
4120 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4121 Record);
4122 break;
4123
4124 case DeclarationName::Identifier:
4125 case DeclarationName::ObjCZeroArgSelector:
4126 case DeclarationName::ObjCOneArgSelector:
4127 case DeclarationName::ObjCMultiArgSelector:
4128 case DeclarationName::CXXUsingDirective:
4129 break;
4130 }
4131}
4132
4133void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004134 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004135 AddDeclarationName(NameInfo.getName(), Record);
4136 AddSourceLocation(NameInfo.getLoc(), Record);
4137 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4138}
4139
4140void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004141 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004142 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004143 Record.push_back(Info.NumTemplParamLists);
4144 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4145 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4146}
4147
Sebastian Redla4232eb2010-08-18 23:56:21 +00004148void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004149 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004150 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004151 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004152 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004153
4154 // Push each of the NNS's onto a stack for serialization in reverse order.
4155 while (NNS) {
4156 NestedNames.push_back(NNS);
4157 NNS = NNS->getPrefix();
4158 }
4159
4160 Record.push_back(NestedNames.size());
4161 while(!NestedNames.empty()) {
4162 NNS = NestedNames.pop_back_val();
4163 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4164 Record.push_back(Kind);
4165 switch (Kind) {
4166 case NestedNameSpecifier::Identifier:
4167 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4168 break;
4169
4170 case NestedNameSpecifier::Namespace:
4171 AddDeclRef(NNS->getAsNamespace(), Record);
4172 break;
4173
Douglas Gregor14aba762011-02-24 02:36:08 +00004174 case NestedNameSpecifier::NamespaceAlias:
4175 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4176 break;
4177
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004178 case NestedNameSpecifier::TypeSpec:
4179 case NestedNameSpecifier::TypeSpecWithTemplate:
4180 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4181 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4182 break;
4183
4184 case NestedNameSpecifier::Global:
4185 // Don't need to write an associated value.
4186 break;
4187 }
4188 }
4189}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004190
Douglas Gregordc355712011-02-25 00:36:19 +00004191void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4192 RecordDataImpl &Record) {
4193 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004194 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004195 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004196
4197 // Push each of the nested-name-specifiers's onto a stack for
4198 // serialization in reverse order.
4199 while (NNS) {
4200 NestedNames.push_back(NNS);
4201 NNS = NNS.getPrefix();
4202 }
4203
4204 Record.push_back(NestedNames.size());
4205 while(!NestedNames.empty()) {
4206 NNS = NestedNames.pop_back_val();
4207 NestedNameSpecifier::SpecifierKind Kind
4208 = NNS.getNestedNameSpecifier()->getKind();
4209 Record.push_back(Kind);
4210 switch (Kind) {
4211 case NestedNameSpecifier::Identifier:
4212 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4213 AddSourceRange(NNS.getLocalSourceRange(), Record);
4214 break;
4215
4216 case NestedNameSpecifier::Namespace:
4217 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4218 AddSourceRange(NNS.getLocalSourceRange(), Record);
4219 break;
4220
4221 case NestedNameSpecifier::NamespaceAlias:
4222 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4223 AddSourceRange(NNS.getLocalSourceRange(), Record);
4224 break;
4225
4226 case NestedNameSpecifier::TypeSpec:
4227 case NestedNameSpecifier::TypeSpecWithTemplate:
4228 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4229 AddTypeLoc(NNS.getTypeLoc(), Record);
4230 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4231 break;
4232
4233 case NestedNameSpecifier::Global:
4234 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4235 break;
4236 }
4237 }
4238}
4239
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004240void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004241 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004242 Record.push_back(Kind);
4243 switch (Kind) {
4244 case TemplateName::Template:
4245 AddDeclRef(Name.getAsTemplateDecl(), Record);
4246 break;
4247
4248 case TemplateName::OverloadedTemplate: {
4249 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4250 Record.push_back(OvT->size());
4251 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4252 I != E; ++I)
4253 AddDeclRef(*I, Record);
4254 break;
4255 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004256
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004257 case TemplateName::QualifiedTemplate: {
4258 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4259 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4260 Record.push_back(QualT->hasTemplateKeyword());
4261 AddDeclRef(QualT->getTemplateDecl(), Record);
4262 break;
4263 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004264
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004265 case TemplateName::DependentTemplate: {
4266 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4267 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4268 Record.push_back(DepT->isIdentifier());
4269 if (DepT->isIdentifier())
4270 AddIdentifierRef(DepT->getIdentifier(), Record);
4271 else
4272 Record.push_back(DepT->getOperator());
4273 break;
4274 }
John McCall14606042011-06-30 08:33:18 +00004275
4276 case TemplateName::SubstTemplateTemplateParm: {
4277 SubstTemplateTemplateParmStorage *subst
4278 = Name.getAsSubstTemplateTemplateParm();
4279 AddDeclRef(subst->getParameter(), Record);
4280 AddTemplateName(subst->getReplacement(), Record);
4281 break;
4282 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004283
4284 case TemplateName::SubstTemplateTemplateParmPack: {
4285 SubstTemplateTemplateParmPackStorage *SubstPack
4286 = Name.getAsSubstTemplateTemplateParmPack();
4287 AddDeclRef(SubstPack->getParameterPack(), Record);
4288 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4289 break;
4290 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004291 }
4292}
4293
Michael J. Spencer20249a12010-10-21 03:16:25 +00004294void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004295 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004296 Record.push_back(Arg.getKind());
4297 switch (Arg.getKind()) {
4298 case TemplateArgument::Null:
4299 break;
4300 case TemplateArgument::Type:
4301 AddTypeRef(Arg.getAsType(), Record);
4302 break;
4303 case TemplateArgument::Declaration:
4304 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004305 Record.push_back(Arg.isDeclForReferenceParam());
4306 break;
4307 case TemplateArgument::NullPtr:
4308 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004309 break;
4310 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004311 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004312 AddTypeRef(Arg.getIntegralType(), Record);
4313 break;
4314 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004315 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4316 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004317 case TemplateArgument::TemplateExpansion:
4318 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004319 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4320 Record.push_back(*NumExpansions + 1);
4321 else
4322 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004323 break;
4324 case TemplateArgument::Expression:
4325 AddStmt(Arg.getAsExpr());
4326 break;
4327 case TemplateArgument::Pack:
4328 Record.push_back(Arg.pack_size());
4329 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4330 I != E; ++I)
4331 AddTemplateArgument(*I, Record);
4332 break;
4333 }
4334}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004335
4336void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004337ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004338 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004339 assert(TemplateParams && "No TemplateParams!");
4340 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4341 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4342 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4343 Record.push_back(TemplateParams->size());
4344 for (TemplateParameterList::const_iterator
4345 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4346 P != PEnd; ++P)
4347 AddDeclRef(*P, Record);
4348}
4349
4350/// \brief Emit a template argument list.
4351void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004352ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004353 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004354 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004355 Record.push_back(TemplateArgs->size());
4356 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004357 AddTemplateArgument(TemplateArgs->get(i), Record);
4358}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004359
4360
4361void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004362ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004363 Record.push_back(Set.size());
4364 for (UnresolvedSetImpl::const_iterator
4365 I = Set.begin(), E = Set.end(); I != E; ++I) {
4366 AddDeclRef(I.getDecl(), Record);
4367 Record.push_back(I.getAccess());
4368 }
4369}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004370
Sebastian Redla4232eb2010-08-18 23:56:21 +00004371void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004372 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004373 Record.push_back(Base.isVirtual());
4374 Record.push_back(Base.isBaseOfClass());
4375 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004376 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004377 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004378 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004379 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4380 : SourceLocation(),
4381 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004382}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004383
Douglas Gregor7c789c12010-10-29 22:39:52 +00004384void ASTWriter::FlushCXXBaseSpecifiers() {
4385 RecordData Record;
4386 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4387 Record.clear();
4388
4389 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004390 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004391 if (Index == CXXBaseSpecifiersOffsets.size())
4392 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4393 else {
4394 if (Index > CXXBaseSpecifiersOffsets.size())
4395 CXXBaseSpecifiersOffsets.resize(Index + 1);
4396 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4397 }
4398
4399 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4400 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4401 Record.push_back(BEnd - B);
4402 for (; B != BEnd; ++B)
4403 AddCXXBaseSpecifier(*B, Record);
4404 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004405
4406 // Flush any expressions that were written as part of the base specifiers.
4407 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004408 }
4409
4410 CXXBaseSpecifiersToWrite.clear();
4411}
4412
Sean Huntcbb67482011-01-08 20:30:50 +00004413void ASTWriter::AddCXXCtorInitializers(
4414 const CXXCtorInitializer * const *CtorInitializers,
4415 unsigned NumCtorInitializers,
4416 RecordDataImpl &Record) {
4417 Record.push_back(NumCtorInitializers);
4418 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4419 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004420
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004421 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004422 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004423 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004424 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004425 } else if (Init->isDelegatingInitializer()) {
4426 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004427 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004428 } else if (Init->isMemberInitializer()){
4429 Record.push_back(CTOR_INITIALIZER_MEMBER);
4430 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004431 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004432 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4433 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004434 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004435
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004436 AddSourceLocation(Init->getMemberLocation(), Record);
4437 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004438 AddSourceLocation(Init->getLParenLoc(), Record);
4439 AddSourceLocation(Init->getRParenLoc(), Record);
4440 Record.push_back(Init->isWritten());
4441 if (Init->isWritten()) {
4442 Record.push_back(Init->getSourceOrder());
4443 } else {
4444 Record.push_back(Init->getNumArrayIndices());
4445 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4446 AddDeclRef(Init->getArrayIndex(i), Record);
4447 }
4448 }
4449}
4450
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004451void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4452 assert(D->DefinitionData);
4453 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004454 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004455 Record.push_back(Data.UserDeclaredConstructor);
4456 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004457 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004458 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004459 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004460 Record.push_back(Data.UserDeclaredDestructor);
4461 Record.push_back(Data.Aggregate);
4462 Record.push_back(Data.PlainOldData);
4463 Record.push_back(Data.Empty);
4464 Record.push_back(Data.Polymorphic);
4465 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004466 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004467 Record.push_back(Data.HasNoNonEmptyBases);
4468 Record.push_back(Data.HasPrivateFields);
4469 Record.push_back(Data.HasProtectedFields);
4470 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004471 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004472 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004473 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004474 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004475 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004476 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004477 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004478 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004479 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004480 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004481 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004482 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004483 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004484 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004485 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004486 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004487 Record.push_back(Data.DeclaredDefaultConstructor);
4488 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004489 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004490 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004491 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004492 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004493 Record.push_back(Data.FailedImplicitMoveConstructor);
4494 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004495 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004496
4497 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004498 if (Data.NumBases > 0)
4499 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4500 Record);
4501
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004502 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4503 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004504 if (Data.NumVBases > 0)
4505 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4506 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004507
4508 AddUnresolvedSet(Data.Conversions, Record);
4509 AddUnresolvedSet(Data.VisibleConversions, Record);
4510 // Data.Definition is the owning decl, no need to write it.
4511 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004512
4513 // Add lambda-specific data.
4514 if (Data.IsLambda) {
4515 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004516 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004517 Record.push_back(Lambda.NumCaptures);
4518 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004519 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004520 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004521 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004522 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4523 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4524 AddSourceLocation(Capture.getLocation(), Record);
4525 Record.push_back(Capture.isImplicit());
4526 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4527 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4528 AddDeclRef(Var, Record);
4529 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4530 : SourceLocation(),
4531 Record);
4532 }
4533 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004534}
4535
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004536void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004537 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004538 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004539 assert(FirstDeclID == NextDeclID &&
4540 FirstTypeID == NextTypeID &&
4541 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004542 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004543 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004544 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004545 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004546
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004547 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004548
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004549 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4550 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4551 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004552 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004553 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004554 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004555 NextDeclID = FirstDeclID;
4556 NextTypeID = FirstTypeID;
4557 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004558 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004559 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004560 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004561}
4562
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004563void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004564 IdentifierIDs[II] = ID;
4565}
4566
Douglas Gregora8235d62012-10-09 23:05:51 +00004567void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4568 MacroIDs[MI] = ID;
4569}
4570
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004571void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004572 // Always take the highest-numbered type index. This copes with an interesting
4573 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004574 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004575 // keep the higher-numbered entry so that we can properly write it out to
4576 // the AST file.
4577 TypeIdx &StoredIdx = TypeIdxs[T];
4578 if (Idx.getIndex() >= StoredIdx.getIndex())
4579 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004580}
4581
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004582void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004583 SelectorIDs[S] = ID;
4584}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004585
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004586void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004587 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004588 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004589 MacroDefinitions[MD] = ID;
4590}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004591
Douglas Gregora015cab2011-12-02 17:30:13 +00004592void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4593 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4594 SubmoduleIDs[Mod] = ID;
4595}
4596
Douglas Gregora8235d62012-10-09 23:05:51 +00004597void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4598 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4599}
4600
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004601void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004602 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004603 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004604 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4605 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004606 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004607 // A forward reference was mutated into a definition. Rewrite it.
4608 // FIXME: This happens during template instantiation, should we
4609 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004610 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004611 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004612 }
4613}
Douglas Gregora8235d62012-10-09 23:05:51 +00004614
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004615void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004616 assert(!WritingAST && "Already writing the AST!");
4617
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004618 // TU and namespaces are handled elsewhere.
4619 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4620 return;
4621
Douglas Gregor919814d2011-09-09 23:01:35 +00004622 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004623 return; // Not a source decl added to a DeclContext from PCH.
4624
4625 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004626 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004627}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004628
4629void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004630 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004631 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004632 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004633 return; // Not a source member added to a class from PCH.
4634 if (!isa<CXXMethodDecl>(D))
4635 return; // We are interested in lazily declared implicit methods.
4636
4637 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004638 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004639 UpdateRecord &Record = DeclUpdates[RD];
4640 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004641 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004642}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004643
4644void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4645 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004646 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004647 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004648 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004649 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004650 return; // Not a source specialization added to a template from PCH.
4651
4652 UpdateRecord &Record = DeclUpdates[TD];
4653 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004654 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004655}
Douglas Gregor89d99802010-11-30 06:16:57 +00004656
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004657void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4658 const FunctionDecl *D) {
4659 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004660 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004661 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004662 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004663 return; // Not a source specialization added to a template from PCH.
4664
4665 UpdateRecord &Record = DeclUpdates[TD];
4666 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004667 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004668}
4669
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004670void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004671 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004672 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004673 return; // Declaration not imported from PCH.
4674
4675 // Implicit decl from a PCH was defined.
4676 // FIXME: Should implicit definition be a separate FunctionDecl?
4677 RewriteDecl(D);
4678}
4679
Sebastian Redlf79a7192011-04-29 08:19:30 +00004680void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004681 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004682 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004683 return;
4684
4685 // Since the actual instantiation is delayed, this really means that we need
4686 // to update the instantiation location.
4687 UpdateRecord &Record = DeclUpdates[D];
4688 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4689 AddSourceLocation(
4690 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4691}
4692
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004693void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4694 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004695 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004696 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004697 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004698
4699 assert(IFD->getDefinition() && "Category on a class without a definition?");
4700 ObjCClassesWithCategories.insert(
4701 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004702}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004703
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004704
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004705void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4706 const ObjCPropertyDecl *OrigProp,
4707 const ObjCCategoryDecl *ClassExt) {
4708 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4709 if (!D)
4710 return;
4711
4712 assert(!WritingAST && "Already writing the AST!");
4713 if (!D->isFromASTFile())
4714 return; // Declaration not imported from PCH.
4715
4716 RewriteDecl(D);
4717}