blob: ea3db882d7c0901eea31d94158166e4735e0f4d3 [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
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000770 // Control Block.
771 BLOCK(CONTROL_BLOCK);
772 RECORD(METADATA);
773 RECORD(IMPORTS);
774 RECORD(LANGUAGE_OPTIONS);
775 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000776 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000777 RECORD(ORIGINAL_PCH_DIR);
778
779 // AST Top-Level Block.
780 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000781 RECORD(TYPE_OFFSET);
782 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000783 RECORD(IDENTIFIER_OFFSET);
784 RECORD(IDENTIFIER_TABLE);
785 RECORD(EXTERNAL_DEFINITIONS);
786 RECORD(SPECIAL_TYPES);
787 RECORD(STATISTICS);
788 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000789 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000790 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
791 RECORD(SELECTOR_OFFSETS);
792 RECORD(METHOD_POOL);
793 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000794 RECORD(SOURCE_LOCATION_OFFSETS);
795 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000796 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000797 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000798 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000799 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000800 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000801 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000802 RECORD(SEMA_DECL_REFS);
803 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
804 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
805 RECORD(DECL_REPLACEMENTS);
806 RECORD(UPDATE_VISIBLE);
807 RECORD(DECL_UPDATE_OFFSETS);
808 RECORD(DECL_UPDATES);
809 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
810 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000811 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000812 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000813 RECORD(FP_PRAGMA_OPTIONS);
814 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000815 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000816 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
817 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000818 RECORD(MODULE_OFFSET_MAP);
819 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000820 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000821 RECORD(FILE_SORTED_DECLS);
822 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000823 RECORD(MERGED_DECLARATIONS);
824 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000825 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000826 RECORD(MACRO_OFFSET);
827 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000828
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000829 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000830 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000831 RECORD(SM_SLOC_FILE_ENTRY);
832 RECORD(SM_SLOC_BUFFER_ENTRY);
833 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000834 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000836 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000837 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000838 RECORD(PP_MACRO_OBJECT_LIKE);
839 RECORD(PP_MACRO_FUNCTION_LIKE);
840 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000841
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000842 // Decls and Types block.
843 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000844 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000845 RECORD(TYPE_COMPLEX);
846 RECORD(TYPE_POINTER);
847 RECORD(TYPE_BLOCK_POINTER);
848 RECORD(TYPE_LVALUE_REFERENCE);
849 RECORD(TYPE_RVALUE_REFERENCE);
850 RECORD(TYPE_MEMBER_POINTER);
851 RECORD(TYPE_CONSTANT_ARRAY);
852 RECORD(TYPE_INCOMPLETE_ARRAY);
853 RECORD(TYPE_VARIABLE_ARRAY);
854 RECORD(TYPE_VECTOR);
855 RECORD(TYPE_EXT_VECTOR);
856 RECORD(TYPE_FUNCTION_PROTO);
857 RECORD(TYPE_FUNCTION_NO_PROTO);
858 RECORD(TYPE_TYPEDEF);
859 RECORD(TYPE_TYPEOF_EXPR);
860 RECORD(TYPE_TYPEOF);
861 RECORD(TYPE_RECORD);
862 RECORD(TYPE_ENUM);
863 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000864 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000865 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000866 RECORD(TYPE_DECLTYPE);
867 RECORD(TYPE_ELABORATED);
868 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
869 RECORD(TYPE_UNRESOLVED_USING);
870 RECORD(TYPE_INJECTED_CLASS_NAME);
871 RECORD(TYPE_OBJC_OBJECT);
872 RECORD(TYPE_TEMPLATE_TYPE_PARM);
873 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
874 RECORD(TYPE_DEPENDENT_NAME);
875 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
876 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
877 RECORD(TYPE_PAREN);
878 RECORD(TYPE_PACK_EXPANSION);
879 RECORD(TYPE_ATTRIBUTED);
880 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000881 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000882 RECORD(DECL_TYPEDEF);
883 RECORD(DECL_ENUM);
884 RECORD(DECL_RECORD);
885 RECORD(DECL_ENUM_CONSTANT);
886 RECORD(DECL_FUNCTION);
887 RECORD(DECL_OBJC_METHOD);
888 RECORD(DECL_OBJC_INTERFACE);
889 RECORD(DECL_OBJC_PROTOCOL);
890 RECORD(DECL_OBJC_IVAR);
891 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000892 RECORD(DECL_OBJC_CATEGORY);
893 RECORD(DECL_OBJC_CATEGORY_IMPL);
894 RECORD(DECL_OBJC_IMPLEMENTATION);
895 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
896 RECORD(DECL_OBJC_PROPERTY);
897 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000898 RECORD(DECL_FIELD);
899 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000900 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000901 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000902 RECORD(DECL_FILE_SCOPE_ASM);
903 RECORD(DECL_BLOCK);
904 RECORD(DECL_CONTEXT_LEXICAL);
905 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000906 RECORD(DECL_NAMESPACE);
907 RECORD(DECL_NAMESPACE_ALIAS);
908 RECORD(DECL_USING);
909 RECORD(DECL_USING_SHADOW);
910 RECORD(DECL_USING_DIRECTIVE);
911 RECORD(DECL_UNRESOLVED_USING_VALUE);
912 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
913 RECORD(DECL_LINKAGE_SPEC);
914 RECORD(DECL_CXX_RECORD);
915 RECORD(DECL_CXX_METHOD);
916 RECORD(DECL_CXX_CONSTRUCTOR);
917 RECORD(DECL_CXX_DESTRUCTOR);
918 RECORD(DECL_CXX_CONVERSION);
919 RECORD(DECL_ACCESS_SPEC);
920 RECORD(DECL_FRIEND);
921 RECORD(DECL_FRIEND_TEMPLATE);
922 RECORD(DECL_CLASS_TEMPLATE);
923 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
924 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
925 RECORD(DECL_FUNCTION_TEMPLATE);
926 RECORD(DECL_TEMPLATE_TYPE_PARM);
927 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
928 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
929 RECORD(DECL_STATIC_ASSERT);
930 RECORD(DECL_CXX_BASE_SPECIFIERS);
931 RECORD(DECL_INDIRECTFIELD);
932 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
933
Douglas Gregora72d8c42011-06-03 02:27:19 +0000934 // Statements and Exprs can occur in the Decls and Types block.
935 AddStmtsExprs(Stream, Record);
936
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000937 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000938 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000939 RECORD(PPD_MACRO_DEFINITION);
940 RECORD(PPD_INCLUSION_DIRECTIVE);
941
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000942#undef RECORD
943#undef BLOCK
944 Stream.ExitBlock();
945}
946
Douglas Gregore650c8c2009-07-07 00:12:59 +0000947/// \brief Adjusts the given filename to only write out the portion of the
948/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000949///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000950/// \param Filename the file name to adjust.
951///
952/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
953/// the returned filename will be adjusted by this system root.
954///
955/// \returns either the original filename (if it needs no adjustment) or the
956/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000957static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000958adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000959 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregor832d6202011-07-22 16:35:34 +0000961 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 // Verify that the filename and the system root have the same prefix.
965 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000966 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 if (Filename[Pos] != isysroot[Pos])
968 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 // We hit the end of the filename before we hit the end of the system root.
971 if (!Filename[Pos])
972 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974 // If the file name has a '/' at the current position, skip over the '/'.
975 // We distinguish sysroot-based includes from absolute includes by the
976 // absence of '/' at the beginning of sysroot-based includes.
977 if (Filename[Pos] == '/')
978 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregore650c8c2009-07-07 00:12:59 +0000980 return Filename + Pos;
981}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000982
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000983/// \brief Write the control block.
984void ASTWriter::WriteControlBlock(ASTContext &Context, StringRef isysroot,
985 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000986 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000987 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
988 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000989
Douglas Gregore650c8c2009-07-07 00:12:59 +0000990 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000991 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
992 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
993 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
994 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
995 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
996 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
997 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
998 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
999 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1000 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1001 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001002 Record.push_back(VERSION_MAJOR);
1003 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001004 Record.push_back(CLANG_VERSION_MAJOR);
1005 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001006 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001007 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001008 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1009 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001010
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001011 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001012 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001013 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1014 llvm::SmallVector<char, 128> ModulePaths;
1015 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001016
1017 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1018 M != MEnd; ++M) {
1019 // Skip modules that weren't directly imported.
1020 if (!(*M)->isDirectlyImported())
1021 continue;
1022
1023 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1024 // FIXME: Write import location, once it matters.
1025 // FIXME: This writes the absolute path for AST files we depend on.
1026 const std::string &FileName = (*M)->FileName;
1027 Record.push_back(FileName.size());
1028 Record.append(FileName.begin(), FileName.end());
1029 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001030 Stream.EmitRecord(IMPORTS, Record);
1031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001033 // Language options.
1034 Record.clear();
1035 const LangOptions &LangOpts = Context.getLangOpts();
1036#define LANGOPT(Name, Bits, Default, Description) \
1037 Record.push_back(LangOpts.Name);
1038#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1039 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1040#include "clang/Basic/LangOptions.def"
1041
1042 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1043 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1044
1045 Record.push_back(LangOpts.CurrentModule.size());
1046 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1047 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1048
Douglas Gregoree097c12012-10-18 17:58:09 +00001049 // Target options.
1050 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001051 const TargetInfo &Target = Context.getTargetInfo();
1052 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001053 AddString(TargetOpts.Triple, Record);
1054 AddString(TargetOpts.CPU, Record);
1055 AddString(TargetOpts.ABI, Record);
1056 AddString(TargetOpts.CXXABI, Record);
1057 AddString(TargetOpts.LinkerVersion, Record);
1058 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1059 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1060 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1061 }
1062 Record.push_back(TargetOpts.Features.size());
1063 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1064 AddString(TargetOpts.Features[I], Record);
1065 }
1066 Stream.EmitRecord(TARGET_OPTIONS, Record);
1067
Douglas Gregor31d375f2011-05-06 21:43:30 +00001068 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001069 SourceManager &SM = Context.getSourceManager();
1070 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1071 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001072 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1073 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001074 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1075 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1076
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001077 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001079 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001080
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001081 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001082 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001083 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001084 RecordData Record;
Douglas Gregor39c497b2012-10-18 18:36:53 +00001085 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001086 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001087 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
1088 Record.clear();
Douglas Gregorb64c1932009-05-12 01:31:05 +00001089 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001090
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001091 // Original PCH directory
1092 if (!OutputFile.empty() && OutputFile != "-") {
1093 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1094 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1096 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1097
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001098 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001099
1100 llvm::sys::fs::make_absolute(OutputPath);
1101 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1102
1103 RecordData Record;
1104 Record.push_back(ORIGINAL_PCH_DIR);
1105 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1106 }
1107
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001108 Stream.ExitBlock();
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001109}
1110
Douglas Gregor14f79002009-04-10 03:52:48 +00001111//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001112// stat cache Serialization
1113//===----------------------------------------------------------------------===//
1114
1115namespace {
1116// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001117class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001118public:
1119 typedef const char * key_type;
1120 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Chris Lattner74e976b2010-11-23 19:28:12 +00001122 typedef struct stat data_type;
1123 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001124
1125 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001126 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001127 }
Mike Stump1eb44332009-09-09 15:08:12 +00001128
1129 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001130 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001131 data_type_ref Data) {
1132 unsigned StrLen = strlen(path);
1133 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001134 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001135 clang::io::Emit8(Out, DataLen);
1136 return std::make_pair(StrLen + 1, DataLen);
1137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Chris Lattner5f9e2722011-07-23 10:55:15 +00001139 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001140 Out.write(path, KeyLen);
1141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Chris Lattner5f9e2722011-07-23 10:55:15 +00001143 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001144 data_type_ref Data, unsigned DataLen) {
1145 using namespace clang::io;
1146 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Chris Lattner74e976b2010-11-23 19:28:12 +00001148 Emit32(Out, (uint32_t) Data.st_ino);
1149 Emit32(Out, (uint32_t) Data.st_dev);
1150 Emit16(Out, (uint16_t) Data.st_mode);
1151 Emit64(Out, (uint64_t) Data.st_mtime);
1152 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001153
1154 assert(Out.tell() - Start == DataLen && "Wrong data length");
1155 }
1156};
1157} // end anonymous namespace
1158
Sebastian Redl3397c552010-08-18 23:56:27 +00001159/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001160void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001161 // Build the on-disk hash table containing information about every
1162 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001163 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001164 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001165 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001166 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001167 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001168 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001169 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001170 }
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001172 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001173 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001174 uint32_t BucketOffset;
1175 {
1176 llvm::raw_svector_ostream Out(StatCacheData);
1177 // Make sure that no bucket is at offset 0
1178 clang::io::Emit32(Out, 0);
1179 BucketOffset = Generator.Emit(Out);
1180 }
1181
1182 // Create a blob abbreviation
1183 using namespace llvm;
1184 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001185 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1189 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1190
1191 // Write the stat cache
1192 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001193 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001194 Record.push_back(BucketOffset);
1195 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001196 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001197}
1198
1199//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001200// Source Manager Serialization
1201//===----------------------------------------------------------------------===//
1202
1203/// \brief Create an abbreviation for the SLocEntry that refers to a
1204/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001205static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001206 using namespace llvm;
1207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001208 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001213 // FileEntry fields.
1214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001221 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001222}
1223
1224/// \brief Create an abbreviation for the SLocEntry that refers to a
1225/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001226static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001227 using namespace llvm;
1228 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001229 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001235 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001236}
1237
1238/// \brief Create an abbreviation for the SLocEntry that refers to a
1239/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001240static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001241 using namespace llvm;
1242 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001243 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001245 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001246}
1247
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001248/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1249/// expansion.
1250static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001251 using namespace llvm;
1252 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001253 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001259 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001260}
1261
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001262namespace {
1263 // Trait used for the on-disk hash table of header search information.
1264 class HeaderFileInfoTrait {
1265 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001266
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001267 // Keep track of the framework names we've used during serialization.
1268 SmallVector<char, 128> FrameworkStringData;
1269 llvm::StringMap<unsigned> FrameworkNameOffset;
1270
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001271 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001272 HeaderFileInfoTrait(ASTWriter &Writer)
1273 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001274
1275 typedef const char *key_type;
1276 typedef key_type key_type_ref;
1277
1278 typedef HeaderFileInfo data_type;
1279 typedef const data_type &data_type_ref;
1280
1281 static unsigned ComputeHash(const char *path) {
1282 // The hash is based only on the filename portion of the key, so that the
1283 // reader can match based on filenames when symlinking or excess path
1284 // elements ("foo/../", "../") change the form of the name. However,
1285 // complete path is still the key.
1286 return llvm::HashString(llvm::sys::path::filename(path));
1287 }
1288
1289 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001290 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001291 data_type_ref Data) {
1292 unsigned StrLen = strlen(path);
1293 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001294 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001295 clang::io::Emit8(Out, DataLen);
1296 return std::make_pair(StrLen + 1, DataLen);
1297 }
1298
Chris Lattner5f9e2722011-07-23 10:55:15 +00001299 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001300 Out.write(path, KeyLen);
1301 }
1302
Chris Lattner5f9e2722011-07-23 10:55:15 +00001303 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001304 data_type_ref Data, unsigned DataLen) {
1305 using namespace clang::io;
1306 uint64_t Start = Out.tell(); (void)Start;
1307
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001308 unsigned char Flags = (Data.isImport << 5)
1309 | (Data.isPragmaOnce << 4)
1310 | (Data.DirInfo << 2)
1311 | (Data.Resolved << 1)
1312 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001313 Emit8(Out, (uint8_t)Flags);
1314 Emit16(Out, (uint16_t) Data.NumIncludes);
1315
1316 if (!Data.ControllingMacro)
1317 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1318 else
1319 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001320
1321 unsigned Offset = 0;
1322 if (!Data.Framework.empty()) {
1323 // If this header refers into a framework, save the framework name.
1324 llvm::StringMap<unsigned>::iterator Pos
1325 = FrameworkNameOffset.find(Data.Framework);
1326 if (Pos == FrameworkNameOffset.end()) {
1327 Offset = FrameworkStringData.size() + 1;
1328 FrameworkStringData.append(Data.Framework.begin(),
1329 Data.Framework.end());
1330 FrameworkStringData.push_back(0);
1331
1332 FrameworkNameOffset[Data.Framework] = Offset;
1333 } else
1334 Offset = Pos->second;
1335 }
1336 Emit32(Out, Offset);
1337
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001338 assert(Out.tell() - Start == DataLen && "Wrong data length");
1339 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001340
1341 const char *strings_begin() const { return FrameworkStringData.begin(); }
1342 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001343 };
1344} // end anonymous namespace
1345
1346/// \brief Write the header search block for the list of files that
1347///
1348/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001349void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001350 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001351 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1352
1353 if (FilesByUID.size() > HS.header_file_size())
1354 FilesByUID.resize(HS.header_file_size());
1355
Benjamin Kramerfacde172012-06-06 17:32:50 +00001356 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001357 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001358 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001359 unsigned NumHeaderSearchEntries = 0;
1360 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1361 const FileEntry *File = FilesByUID[UID];
1362 if (!File)
1363 continue;
1364
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001365 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1366 // from the external source if it was not provided already.
1367 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001368 if (HFI.External && Chain)
1369 continue;
1370
1371 // Turn the file name into an absolute path, if it isn't already.
1372 const char *Filename = File->getName();
1373 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1374
1375 // If we performed any translation on the file name at all, we need to
1376 // save this string, since the generator will refer to it later.
1377 if (Filename != File->getName()) {
1378 Filename = strdup(Filename);
1379 SavedStrings.push_back(Filename);
1380 }
1381
1382 Generator.insert(Filename, HFI, GeneratorTrait);
1383 ++NumHeaderSearchEntries;
1384 }
1385
1386 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001387 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001388 uint32_t BucketOffset;
1389 {
1390 llvm::raw_svector_ostream Out(TableData);
1391 // Make sure that no bucket is at offset 0
1392 clang::io::Emit32(Out, 0);
1393 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1394 }
1395
1396 // Create a blob abbreviation
1397 using namespace llvm;
1398 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1399 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1404 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1405
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001406 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001407 RecordData Record;
1408 Record.push_back(HEADER_SEARCH_TABLE);
1409 Record.push_back(BucketOffset);
1410 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001411 Record.push_back(TableData.size());
1412 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001413 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1414
1415 // Free all of the strings we had to duplicate.
1416 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1417 free((void*)SavedStrings[I]);
1418}
1419
Douglas Gregor14f79002009-04-10 03:52:48 +00001420/// \brief Writes the block containing the serialized form of the
1421/// source manager.
1422///
1423/// TODO: We should probably use an on-disk hash table (stored in a
1424/// blob), indexed based on the file name, so that we only create
1425/// entries for files that we actually need. In the common case (no
1426/// errors), we probably won't have to create file entries for any of
1427/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001428void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001429 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001430 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001431 RecordData Record;
1432
Chris Lattnerf04ad692009-04-10 17:16:57 +00001433 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001434 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001435
1436 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001437 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1438 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1439 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001440 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001441
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001442 // Write out the source location entry table. We skip the first
1443 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001444 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001445 // Write out the offsets of only source location file entries.
1446 // We will go through them in ASTReader::validateFileEntries().
1447 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001448 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001449 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1450 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001451 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001452 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001453 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001454 FileID FID = FileID::get(I);
1455 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001456
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001457 // Record the offset of this source-location entry.
1458 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1459
1460 // Figure out which record code to use.
1461 unsigned Code;
1462 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001463 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1464 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001465 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001466 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1467 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001468 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001469 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001470 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001471 Record.clear();
1472 Record.push_back(Code);
1473
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001474 // Starting offset of this entry within this module, so skip the dummy.
1475 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001476 if (SLoc->isFile()) {
1477 const SrcMgr::FileInfo &File = SLoc->getFile();
1478 Record.push_back(File.getIncludeLoc().getRawEncoding());
1479 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1480 Record.push_back(File.hasLineDirectives());
1481
1482 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001483 if (Content->OrigEntry) {
1484 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001485 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001486
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001487 // The source location entry is a file. The blob associated
1488 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Douglas Gregor2d52be52010-03-21 22:49:54 +00001490 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001491 Record.push_back(Content->OrigEntry->getSize());
1492 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001493 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001494 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001495
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001496 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001497 if (FDI != FileDeclIDs.end()) {
1498 Record.push_back(FDI->second->FirstDeclIndex);
1499 Record.push_back(FDI->second->DeclIDs.size());
1500 } else {
1501 Record.push_back(0);
1502 Record.push_back(0);
1503 }
Douglas Gregora081da52011-11-16 20:05:18 +00001504
Douglas Gregore650c8c2009-07-07 00:12:59 +00001505 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001506 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001507 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001508
1509 // Ask the file manager to fixup the relative path for us. This will
1510 // honor the working directory.
1511 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1512
1513 // FIXME: This call to make_absolute shouldn't be necessary, the
1514 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001515 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001516 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregore650c8c2009-07-07 00:12:59 +00001518 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001519 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001520
1521 if (Content->BufferOverridden) {
1522 Record.clear();
1523 Record.push_back(SM_SLOC_BUFFER_BLOB);
1524 const llvm::MemoryBuffer *Buffer
1525 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1526 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1527 StringRef(Buffer->getBufferStart(),
1528 Buffer->getBufferSize() + 1));
1529 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001530 } else {
1531 // The source location entry is a buffer. The blob associated
1532 // with this entry contains the contents of the buffer.
1533
1534 // We add one to the size so that we capture the trailing NULL
1535 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1536 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001537 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001538 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001539 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001540 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001541 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001542 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001543 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001544 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001545 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001546 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001548 if (strcmp(Name, "<built-in>") == 0) {
1549 PreloadSLocs.push_back(SLocEntryOffsets.size());
1550 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001551 }
1552 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001553 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001554 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001555 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1556 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001557 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1558 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001559
1560 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001561 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001562 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001563 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001564 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001565 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001566 }
1567 }
1568
Douglas Gregorc9490c02009-04-16 22:23:12 +00001569 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001570
1571 if (SLocEntryOffsets.empty())
1572 return;
1573
Sebastian Redl3397c552010-08-18 23:56:27 +00001574 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001575 // table is used for lazily loading source-location information.
1576 using namespace llvm;
1577 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001578 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1582 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001584 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001585 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001586 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001587 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001588 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001589
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001590 Abbrev = new BitCodeAbbrev();
1591 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1592 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1594 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1595
1596 Record.clear();
1597 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1598 Record.push_back(SLocFileEntryOffsets.size());
1599 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1600 data(SLocFileEntryOffsets));
1601
Sebastian Redl3397c552010-08-18 23:56:27 +00001602 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001603 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001604 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001605
1606 // Write the line table. It depends on remapping working, so it must come
1607 // after the source location offsets.
1608 if (SourceMgr.hasLineTable()) {
1609 LineTableInfo &LineTable = SourceMgr.getLineTable();
1610
1611 Record.clear();
1612 // Emit the file names
1613 Record.push_back(LineTable.getNumFilenames());
1614 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1615 // Emit the file name
1616 const char *Filename = LineTable.getFilename(I);
1617 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1618 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1619 Record.push_back(FilenameLen);
1620 if (FilenameLen)
1621 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1622 }
1623
1624 // Emit the line entries
1625 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1626 L != LEnd; ++L) {
1627 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001628 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001629 continue;
1630
1631 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001632 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001633
1634 // Emit the line entries
1635 Record.push_back(L->second.size());
1636 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1637 LEEnd = L->second.end();
1638 LE != LEEnd; ++LE) {
1639 Record.push_back(LE->FileOffset);
1640 Record.push_back(LE->LineNo);
1641 Record.push_back(LE->FilenameID);
1642 Record.push_back((unsigned)LE->FileKind);
1643 Record.push_back(LE->IncludeOffset);
1644 }
1645 }
1646 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1647 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001648}
1649
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001650//===----------------------------------------------------------------------===//
1651// Preprocessor Serialization
1652//===----------------------------------------------------------------------===//
1653
Douglas Gregor9c736102011-02-10 18:20:09 +00001654static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1655 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1656 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1657 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1658 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1659 return X.first->getName().compare(Y.first->getName());
1660}
1661
Chris Lattner0b1fb982009-04-10 17:15:23 +00001662/// \brief Writes the block containing the serialized form of the
1663/// preprocessor.
1664///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001665void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001666 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1667 if (PPRec)
1668 WritePreprocessorDetail(*PPRec);
1669
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001670 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001671
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001672 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1673 if (PP.getCounterValue() != 0) {
1674 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001675 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001676 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001677 }
1678
1679 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001680 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Sebastian Redl3397c552010-08-18 23:56:27 +00001682 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001683 // FIXME: use diagnostics subsystem for localization etc.
1684 if (PP.SawDateOrTime())
1685 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Douglas Gregorecdcb882010-10-20 22:00:55 +00001687
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001688 // Loop over all the macro definitions that are live at the end of the file,
1689 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001690
Douglas Gregor9c736102011-02-10 18:20:09 +00001691 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001692 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001693 MacrosToEmit;
1694 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001695 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001696 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001697 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001698 if (!IsModule || I->second->isPublic()) {
1699 MacroDefinitionsSeen.insert(I->first);
1700 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001701 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001702 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001703
Douglas Gregor9c736102011-02-10 18:20:09 +00001704 // Sort the set of macro definitions that need to be serialized by the
1705 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001706 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001707 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001708
Douglas Gregora8235d62012-10-09 23:05:51 +00001709 /// \brief Offsets of each of the macros into the bitstream, indexed by
1710 /// the local macro ID
1711 ///
1712 /// For each identifier that is associated with a macro, this map
1713 /// provides the offset into the bitstream where that macro is
1714 /// defined.
1715 std::vector<uint32_t> MacroOffsets;
1716
Douglas Gregor9c736102011-02-10 18:20:09 +00001717 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1718 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001719
Douglas Gregora8235d62012-10-09 23:05:51 +00001720 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1721 MI = MI->getPreviousDefinition()) {
1722 MacroID ID = getMacroRef(MI);
1723 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001724 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Douglas Gregora8235d62012-10-09 23:05:51 +00001726 // Skip macros from a AST file if we're chaining.
1727 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1728 continue;
1729
1730 if (ID < FirstMacroID) {
1731 // This will have been dealt with via an update record.
1732 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1733 continue;
1734 }
1735
1736 // Record the local offset of this macro.
1737 unsigned Index = ID - FirstMacroID;
1738 if (Index == MacroOffsets.size())
1739 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1740 else {
1741 if (Index > MacroOffsets.size())
1742 MacroOffsets.resize(Index + 1);
1743
1744 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1745 }
1746
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001747 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001748 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001749 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001750 AddSourceLocation(MI->getDefinitionLoc(), Record);
1751 AddSourceLocation(MI->getUndefLoc(), Record);
1752 Record.push_back(MI->isUsed());
1753 Record.push_back(MI->isPublic());
1754 AddSourceLocation(MI->getVisibilityLocation(), Record);
1755 unsigned Code;
1756 if (MI->isObjectLike()) {
1757 Code = PP_MACRO_OBJECT_LIKE;
1758 } else {
1759 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001760
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001761 Record.push_back(MI->isC99Varargs());
1762 Record.push_back(MI->isGNUVarargs());
1763 Record.push_back(MI->getNumArgs());
1764 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1765 I != E; ++I)
1766 AddIdentifierRef(*I, Record);
1767 }
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001769 // If we have a detailed preprocessing record, record the macro definition
1770 // ID that corresponds to this macro.
1771 if (PPRec)
1772 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1773
1774 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001775 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001776
1777 // Emit the tokens array.
1778 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1779 // Note that we know that the preprocessor does not have any annotation
1780 // tokens in it because they are created by the parser, and thus can't
1781 // be in a macro definition.
1782 const Token &Tok = MI->getReplacementToken(TokNo);
1783
1784 Record.push_back(Tok.getLocation().getRawEncoding());
1785 Record.push_back(Tok.getLength());
1786
1787 // FIXME: When reading literal tokens, reconstruct the literal pointer
1788 // if it is needed.
1789 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1790 // FIXME: Should translate token kind to a stable encoding.
1791 Record.push_back(Tok.getKind());
1792 // FIXME: Should translate token flags to a stable encoding.
1793 Record.push_back(Tok.getFlags());
1794
1795 Stream.EmitRecord(PP_TOKEN, Record);
1796 Record.clear();
1797 }
1798 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001799 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001800 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001801 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001802
1803 // Write the offsets table for macro IDs.
1804 using namespace llvm;
1805 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1806 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1810
1811 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1812 Record.clear();
1813 Record.push_back(MACRO_OFFSET);
1814 Record.push_back(MacroOffsets.size());
1815 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1816 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1817 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001818}
1819
1820void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001821 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001822 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001823
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001824 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001825
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001826 // Enter the preprocessor block.
1827 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001828
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001829 // If the preprocessor has a preprocessing record, emit it.
1830 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001831 using namespace llvm;
1832
1833 // Set up the abbreviation for
1834 unsigned InclusionAbbrev = 0;
1835 {
1836 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1837 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1843 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1844 }
1845
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001846 unsigned FirstPreprocessorEntityID
1847 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1848 + NUM_PREDEF_PP_ENTITY_IDS;
1849 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001850 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001851 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1852 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001853 E != EEnd;
1854 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001855 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001856
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001857 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1858 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001859
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001860 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001861 // Record this macro definition's ID.
1862 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001863
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001864 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001865 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1866 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001867 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001868
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001869 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001870 Record.push_back(ME->isBuiltinMacro());
1871 if (ME->isBuiltinMacro())
1872 AddIdentifierRef(ME->getName(), Record);
1873 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001874 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001875 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001876 continue;
1877 }
1878
1879 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1880 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001881 Record.push_back(ID->getFileName().size());
1882 Record.push_back(ID->wasInQuotes());
1883 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001884 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001885 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001886 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001887 // Check that the FileEntry is not null because it was not resolved and
1888 // we create a PCH even with compiler errors.
1889 if (ID->getFile())
1890 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001891 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1892 continue;
1893 }
1894
1895 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1896 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001897 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001898
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001899 // Write the offsets table for the preprocessing record.
1900 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001901 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1902
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001903 // Write the offsets table for identifier IDs.
1904 using namespace llvm;
1905 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001906 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001909 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001910
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001911 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001912 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001913 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001914 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1915 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001916 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001917}
1918
Douglas Gregore209e502011-12-06 01:10:29 +00001919unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1920 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1921 if (Known != SubmoduleIDs.end())
1922 return Known->second;
1923
1924 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1925}
1926
Douglas Gregor26ced122011-12-01 00:59:36 +00001927/// \brief Compute the number of modules within the given tree (including the
1928/// given module).
1929static unsigned getNumberOfModules(Module *Mod) {
1930 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001931 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1932 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001933 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001934 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001935
1936 return ChildModules + 1;
1937}
1938
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001939void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001940 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001941 // FIXME: This feels like it belongs somewhere else, but there are no
1942 // other consumers of this information.
1943 SourceManager &SrcMgr = PP->getSourceManager();
1944 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1945 for (ASTContext::import_iterator I = Context->local_import_begin(),
1946 IEnd = Context->local_import_end();
1947 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001948 if (Module *ImportedFrom
1949 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1950 SrcMgr))) {
1951 ImportedFrom->Imports.push_back(I->getImportedModule());
1952 }
1953 }
1954
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001955 // Enter the submodule description block.
1956 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1957
1958 // Write the abbreviations needed for the submodules block.
1959 using namespace llvm;
1960 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1961 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1971 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1972
1973 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001974 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1976 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1977
1978 Abbrev = new BitCodeAbbrev();
1979 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1981 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001982
1983 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00001984 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
1985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1986 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
1987
1988 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001989 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1991 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1992
Douglas Gregor51f564f2011-12-31 04:05:44 +00001993 Abbrev = new BitCodeAbbrev();
1994 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1996 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1997
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00001998 Abbrev = new BitCodeAbbrev();
1999 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2001 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2002
Douglas Gregor26ced122011-12-01 00:59:36 +00002003 // Write the submodule metadata block.
2004 RecordData Record;
2005 Record.push_back(getNumberOfModules(WritingModule));
2006 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2007 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2008
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002009 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002010 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002011 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002012 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002013 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002014 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002015 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002016
2017 // Emit the definition of the block.
2018 Record.clear();
2019 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002020 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002021 if (Mod->Parent) {
2022 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2023 Record.push_back(SubmoduleIDs[Mod->Parent]);
2024 } else {
2025 Record.push_back(0);
2026 }
2027 Record.push_back(Mod->IsFramework);
2028 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002029 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002030 Record.push_back(Mod->InferSubmodules);
2031 Record.push_back(Mod->InferExplicitSubmodules);
2032 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002033 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2034
Douglas Gregor51f564f2011-12-31 04:05:44 +00002035 // Emit the requirements.
2036 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2037 Record.clear();
2038 Record.push_back(SUBMODULE_REQUIRES);
2039 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2040 Mod->Requires[I].data(),
2041 Mod->Requires[I].size());
2042 }
2043
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002044 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002045 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002046 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002047 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002048 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002049 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002050 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2051 Record.clear();
2052 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2053 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2054 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002055 }
2056
2057 // Emit the headers.
2058 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2059 Record.clear();
2060 Record.push_back(SUBMODULE_HEADER);
2061 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2062 Mod->Headers[I]->getName());
2063 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002064 // Emit the excluded headers.
2065 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2066 Record.clear();
2067 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2068 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2069 Mod->ExcludedHeaders[I]->getName());
2070 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002071 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2072 Record.clear();
2073 Record.push_back(SUBMODULE_TOPHEADER);
2074 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2075 Mod->TopHeaders[I]->getName());
2076 }
Douglas Gregor55988682011-12-05 16:33:54 +00002077
2078 // Emit the imports.
2079 if (!Mod->Imports.empty()) {
2080 Record.clear();
2081 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002082 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002083 assert(ImportedID && "Unknown submodule!");
2084 Record.push_back(ImportedID);
2085 }
2086 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2087 }
2088
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002089 // Emit the exports.
2090 if (!Mod->Exports.empty()) {
2091 Record.clear();
2092 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002093 if (Module *Exported = Mod->Exports[I].getPointer()) {
2094 unsigned ExportedID = SubmoduleIDs[Exported];
2095 assert(ExportedID > 0 && "Unknown submodule ID?");
2096 Record.push_back(ExportedID);
2097 } else {
2098 Record.push_back(0);
2099 }
2100
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002101 Record.push_back(Mod->Exports[I].getInt());
2102 }
2103 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2104 }
2105
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002106 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002107 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2108 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002109 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002110 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002111 }
2112
2113 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002114
2115 assert((NextSubmoduleID - FirstSubmoduleID
2116 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002117}
2118
Douglas Gregor185dbd72011-12-01 02:07:58 +00002119serialization::SubmoduleID
2120ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002121 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002122 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002123
2124 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002125 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002126 Module *OwningMod
2127 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002128 if (!OwningMod)
2129 return 0;
2130
Douglas Gregore209e502011-12-06 01:10:29 +00002131 // Check whether this submodule is part of our own module.
2132 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002133 return 0;
2134
Douglas Gregore209e502011-12-06 01:10:29 +00002135 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002136}
2137
David Blaikied6471f72011-09-25 23:23:43 +00002138void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002139 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002140 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002141 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2142 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002143 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002144 if (point.Loc.isInvalid())
2145 continue;
2146
2147 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002148 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002149 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002150 if (I->second.isPragma()) {
2151 Record.push_back(I->first);
2152 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002153 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002154 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002155 Record.push_back(-1); // mark the end of the diag/map pairs for this
2156 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002157 }
2158
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002159 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002160 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002161}
2162
Anders Carlssonc8505782011-03-06 18:41:18 +00002163void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2164 if (CXXBaseSpecifiersOffsets.empty())
2165 return;
2166
2167 RecordData Record;
2168
2169 // Create a blob abbreviation for the C++ base specifiers offsets.
2170 using namespace llvm;
2171
2172 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2173 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2176 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2177
Douglas Gregore92b8a12011-08-04 00:01:48 +00002178 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002179 Record.clear();
2180 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2181 Record.push_back(CXXBaseSpecifiersOffsets.size());
2182 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002183 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002184}
2185
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002186//===----------------------------------------------------------------------===//
2187// Type Serialization
2188//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002189
Sebastian Redl3397c552010-08-18 23:56:27 +00002190/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002191void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002192 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002193 if (Idx.getIndex() == 0) // we haven't seen this type before.
2194 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Douglas Gregor97475832010-10-05 18:37:06 +00002196 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002197
Douglas Gregor2cf26342009-04-09 22:27:44 +00002198 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002199 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002200 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002201 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002202 else if (TypeOffsets.size() < Index) {
2203 TypeOffsets.resize(Index + 1);
2204 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002205 }
2206
2207 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002208
Douglas Gregor2cf26342009-04-09 22:27:44 +00002209 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002210 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002211
Douglas Gregora4923eb2009-11-16 21:35:15 +00002212 if (T.hasLocalNonFastQualifiers()) {
2213 Qualifiers Qs = T.getLocalQualifiers();
2214 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002215 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002216 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002217 } else {
2218 switch (T->getTypeClass()) {
2219 // For all of the concrete, non-dependent types, call the
2220 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002221#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002222 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002223#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002224#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002225 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002226 }
2227
2228 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002229 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002230
2231 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002232 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002233}
2234
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002235//===----------------------------------------------------------------------===//
2236// Declaration Serialization
2237//===----------------------------------------------------------------------===//
2238
Douglas Gregor2cf26342009-04-09 22:27:44 +00002239/// \brief Write the block containing all of the declaration IDs
2240/// lexically declared within the given DeclContext.
2241///
2242/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2243/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002244uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002245 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002246 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002247 return 0;
2248
Douglas Gregorc9490c02009-04-16 22:23:12 +00002249 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002250 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002251 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002252 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002253 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2254 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002255 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002256
Douglas Gregor25123082009-04-22 22:34:57 +00002257 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002258 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002259 return Offset;
2260}
2261
Sebastian Redla4232eb2010-08-18 23:56:21 +00002262void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002263 using namespace llvm;
2264 RecordData Record;
2265
2266 // Write the type offsets array
2267 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002268 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2272 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2273 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002274 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002275 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002276 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002277 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002278
2279 // Write the declaration offsets array
2280 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002281 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2285 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2286 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002287 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002288 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002289 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002290 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002291}
2292
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002293void ASTWriter::WriteFileDeclIDsMap() {
2294 using namespace llvm;
2295 RecordData Record;
2296
2297 // Join the vectors of DeclIDs from all files.
2298 SmallVector<DeclID, 256> FileSortedIDs;
2299 for (FileDeclIDsTy::iterator
2300 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2301 DeclIDInFileInfo &Info = *FI->second;
2302 Info.FirstDeclIndex = FileSortedIDs.size();
2303 for (LocDeclIDsTy::iterator
2304 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2305 FileSortedIDs.push_back(DI->second);
2306 }
2307
2308 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2309 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2312 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2313 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002314 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002315 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2316}
2317
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002318void ASTWriter::WriteComments() {
2319 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002320 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002321 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002322 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2323 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002324 I != E; ++I) {
2325 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002326 AddSourceRange((*I)->getSourceRange(), Record);
2327 Record.push_back((*I)->getKind());
2328 Record.push_back((*I)->isTrailingComment());
2329 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002330 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2331 }
2332 Stream.ExitBlock();
2333}
2334
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002335//===----------------------------------------------------------------------===//
2336// Global Method Pool and Selector Serialization
2337//===----------------------------------------------------------------------===//
2338
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002339namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002340// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002341class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002342 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002343
2344public:
2345 typedef Selector key_type;
2346 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002347
Sebastian Redl5d050072010-08-04 17:20:04 +00002348 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002349 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002350 ObjCMethodList Instance, Factory;
2351 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002352 typedef const data_type& data_type_ref;
2353
Sebastian Redl3397c552010-08-18 23:56:27 +00002354 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002356 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002357 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002358 }
Mike Stump1eb44332009-09-09 15:08:12 +00002359
2360 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002361 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002362 data_type_ref Methods) {
2363 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2364 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002365 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2366 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002367 Method = Method->Next)
2368 if (Method->Method)
2369 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002370 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002371 Method = Method->Next)
2372 if (Method->Method)
2373 DataLen += 4;
2374 clang::io::Emit16(Out, DataLen);
2375 return std::make_pair(KeyLen, DataLen);
2376 }
Mike Stump1eb44332009-09-09 15:08:12 +00002377
Chris Lattner5f9e2722011-07-23 10:55:15 +00002378 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002379 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002380 assert((Start >> 32) == 0 && "Selector key offset too large");
2381 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002382 unsigned N = Sel.getNumArgs();
2383 clang::io::Emit16(Out, N);
2384 if (N == 0)
2385 N = 1;
2386 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002387 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002388 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2389 }
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Chris Lattner5f9e2722011-07-23 10:55:15 +00002391 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002392 data_type_ref Methods, unsigned DataLen) {
2393 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002394 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002395 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002396 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002397 Method = Method->Next)
2398 if (Method->Method)
2399 ++NumInstanceMethods;
2400
2401 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002402 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002403 Method = Method->Next)
2404 if (Method->Method)
2405 ++NumFactoryMethods;
2406
2407 clang::io::Emit16(Out, NumInstanceMethods);
2408 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002409 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002410 Method = Method->Next)
2411 if (Method->Method)
2412 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002413 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002414 Method = Method->Next)
2415 if (Method->Method)
2416 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002417
2418 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002419 }
2420};
2421} // end anonymous namespace
2422
Sebastian Redl059612d2010-08-03 21:58:15 +00002423/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002424///
2425/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002426/// in an on-disk hash table indexed by the selector. The hash table also
2427/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002428void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002429 using namespace llvm;
2430
Sebastian Redl059612d2010-08-03 21:58:15 +00002431 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002432 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002433 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002434 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002435 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002436 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002437 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002438 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Sebastian Redl059612d2010-08-03 21:58:15 +00002440 // Create the on-disk hash table representation. We walk through every
2441 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002442 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002443 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002444 I = SelectorIDs.begin(), E = SelectorIDs.end();
2445 I != E; ++I) {
2446 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002447 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002448 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002449 I->second,
2450 ObjCMethodList(),
2451 ObjCMethodList()
2452 };
2453 if (F != SemaRef.MethodPool.end()) {
2454 Data.Instance = F->second.first;
2455 Data.Factory = F->second.second;
2456 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002457 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002458 // changed.
2459 if (Chain && I->second < FirstSelectorID) {
2460 // Selector already exists. Did it change?
2461 bool changed = false;
2462 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2463 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002464 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002465 changed = true;
2466 }
2467 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2468 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002469 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002470 changed = true;
2471 }
2472 if (!changed)
2473 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002474 } else if (Data.Instance.Method || Data.Factory.Method) {
2475 // A new method pool entry.
2476 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002477 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002478 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002479 }
2480
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002481 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002482 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002483 uint32_t BucketOffset;
2484 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002485 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002486 llvm::raw_svector_ostream Out(MethodPool);
2487 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002488 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002489 BucketOffset = Generator.Emit(Out, Trait);
2490 }
2491
2492 // Create a blob abbreviation
2493 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002494 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002497 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2498 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2499
Douglas Gregor83941df2009-04-25 17:48:32 +00002500 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002501 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002502 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002503 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002504 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002505 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002506
2507 // Create a blob abbreviation for the selector table offsets.
2508 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002509 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2513 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2514
2515 // Write the selector offsets table.
2516 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002517 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002518 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002519 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002520 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002521 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002522 }
2523}
2524
Sebastian Redl3397c552010-08-18 23:56:27 +00002525/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002526void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002527 using namespace llvm;
2528 if (SemaRef.ReferencedSelectors.empty())
2529 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002530
Fariborz Jahanian32019832010-07-23 19:11:11 +00002531 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002532
Sebastian Redl3397c552010-08-18 23:56:27 +00002533 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002534 // very tricky to fix, and given that @selector shouldn't really appear in
2535 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002536 for (DenseMap<Selector, SourceLocation>::iterator S =
2537 SemaRef.ReferencedSelectors.begin(),
2538 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2539 Selector Sel = (*S).first;
2540 SourceLocation Loc = (*S).second;
2541 AddSelectorRef(Sel, Record);
2542 AddSourceLocation(Loc, Record);
2543 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002544 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002545}
2546
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002547//===----------------------------------------------------------------------===//
2548// Identifier Table Serialization
2549//===----------------------------------------------------------------------===//
2550
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002551namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002552class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002553 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002554 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002555 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002556 bool IsModule;
2557
Douglas Gregora92193e2009-04-28 21:18:29 +00002558 /// \brief Determines whether this is an "interesting" identifier
2559 /// that needs a full IdentifierInfo structure written into the hash
2560 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002561 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002562 if (II->isPoisoned() ||
2563 II->isExtensionToken() ||
2564 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002565 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002566 II->getFETokenInfo<void>())
2567 return true;
2568
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002569 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002570 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002571
2572 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2573 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002574 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002575
2576 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002577 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002578
2579 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002580 }
2581
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002582public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002583 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002584 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002586 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002587 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002588
Douglas Gregoreee242f2011-10-27 09:33:13 +00002589 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2590 IdentifierResolver &IdResolver, bool IsModule)
2591 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002592
2593 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002594 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002595 }
Mike Stump1eb44332009-09-09 15:08:12 +00002596
2597 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002598 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002599 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002600 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002601 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002602 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002603 DataLen += 2; // 2 bytes for builtin ID
2604 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002605 if (hadMacroDefinition(II, Macro)) {
2606 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2607 if (Writer.getMacroRef(M) != 0)
2608 DataLen += 4;
2609 }
2610
2611 DataLen += 4;
2612 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002613
Douglas Gregoreee242f2011-10-27 09:33:13 +00002614 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2615 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002616 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002617 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002618 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002619 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002620 // We emit the key length after the data length so that every
2621 // string is preceded by a 16-bit length. This matches the PTH
2622 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002623 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002624 return std::make_pair(KeyLen, DataLen);
2625 }
Mike Stump1eb44332009-09-09 15:08:12 +00002626
Chris Lattner5f9e2722011-07-23 10:55:15 +00002627 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002628 unsigned KeyLen) {
2629 // Record the location of the key data. This is used when generating
2630 // the mapping from persistent IDs to strings.
2631 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002632 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002633 }
Mike Stump1eb44332009-09-09 15:08:12 +00002634
Douglas Gregor7143aab2011-09-01 17:04:32 +00002635 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002636 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002637 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002638 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002639 clang::io::Emit32(Out, ID << 1);
2640 return;
2641 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002642
Douglas Gregora92193e2009-04-28 21:18:29 +00002643 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002644 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2645 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2646 clang::io::Emit16(Out, Bits);
2647 Bits = 0;
2648 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002649 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002650 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2651 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002652 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002653 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002654 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002655
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002656 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002657 // Write all of the macro IDs associated with this identifier.
2658 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2659 if (MacroID ID = Writer.getMacroRef(M))
2660 clang::io::Emit32(Out, ID);
2661 }
2662
2663 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002664 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002665
Douglas Gregor668c1a42009-04-21 22:25:48 +00002666 // Emit the declaration IDs in reverse order, because the
2667 // IdentifierResolver provides the declarations as they would be
2668 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002669 // "stat"), but the ASTReader adds declarations to the end of the list
2670 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002671 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002672 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2673 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002674 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002675 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002676 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002677 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002678 }
2679};
2680} // end anonymous namespace
2681
Sebastian Redl3397c552010-08-18 23:56:27 +00002682/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002683///
2684/// The identifier table consists of a blob containing string data
2685/// (the actual identifiers themselves) and a separate "offsets" index
2686/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002687void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2688 IdentifierResolver &IdResolver,
2689 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002690 using namespace llvm;
2691
2692 // Create and write out the blob that contains the identifier
2693 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002694 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002695 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002696 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002697
Douglas Gregor92b059e2009-04-28 20:33:11 +00002698 // Look for any identifiers that were named while processing the
2699 // headers, but are otherwise not needed. We add these to the hash
2700 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002701 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002702 // file.
2703 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2704 IDEnd = PP.getIdentifierTable().end();
2705 ID != IDEnd; ++ID)
2706 getIdentifierRef(ID->second);
2707
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002708 // Create the on-disk hash table representation. We only store offsets
2709 // for identifiers that appear here for the first time.
2710 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002711 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002712 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2713 ID != IDEnd; ++ID) {
2714 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002715 if (!Chain || !ID->first->isFromAST() ||
2716 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002717 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2718 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002719 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002720
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002721 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002722 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002723 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002724 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002725 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002726 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002727 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002728 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002729 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002730 }
2731
2732 // Create a blob abbreviation
2733 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002734 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002737 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002738
2739 // Write the identifier table
2740 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002741 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002742 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002743 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002744 }
2745
2746 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002747 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002748 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2752 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2753
2754 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002755 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002756 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002757 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002758 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002759 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002760}
2761
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002762//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002763// DeclContext's Name Lookup Table Serialization
2764//===----------------------------------------------------------------------===//
2765
2766namespace {
2767// Trait used for the on-disk hash table used in the method pool.
2768class ASTDeclContextNameLookupTrait {
2769 ASTWriter &Writer;
2770
2771public:
2772 typedef DeclarationName key_type;
2773 typedef key_type key_type_ref;
2774
2775 typedef DeclContext::lookup_result data_type;
2776 typedef const data_type& data_type_ref;
2777
2778 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2779
2780 unsigned ComputeHash(DeclarationName Name) {
2781 llvm::FoldingSetNodeID ID;
2782 ID.AddInteger(Name.getNameKind());
2783
2784 switch (Name.getNameKind()) {
2785 case DeclarationName::Identifier:
2786 ID.AddString(Name.getAsIdentifierInfo()->getName());
2787 break;
2788 case DeclarationName::ObjCZeroArgSelector:
2789 case DeclarationName::ObjCOneArgSelector:
2790 case DeclarationName::ObjCMultiArgSelector:
2791 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2792 break;
2793 case DeclarationName::CXXConstructorName:
2794 case DeclarationName::CXXDestructorName:
2795 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002796 break;
2797 case DeclarationName::CXXOperatorName:
2798 ID.AddInteger(Name.getCXXOverloadedOperator());
2799 break;
2800 case DeclarationName::CXXLiteralOperatorName:
2801 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2802 case DeclarationName::CXXUsingDirective:
2803 break;
2804 }
2805
2806 return ID.ComputeHash();
2807 }
2808
2809 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002810 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002811 data_type_ref Lookup) {
2812 unsigned KeyLen = 1;
2813 switch (Name.getNameKind()) {
2814 case DeclarationName::Identifier:
2815 case DeclarationName::ObjCZeroArgSelector:
2816 case DeclarationName::ObjCOneArgSelector:
2817 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002818 case DeclarationName::CXXLiteralOperatorName:
2819 KeyLen += 4;
2820 break;
2821 case DeclarationName::CXXOperatorName:
2822 KeyLen += 1;
2823 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002824 case DeclarationName::CXXConstructorName:
2825 case DeclarationName::CXXDestructorName:
2826 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002827 case DeclarationName::CXXUsingDirective:
2828 break;
2829 }
2830 clang::io::Emit16(Out, KeyLen);
2831
2832 // 2 bytes for num of decls and 4 for each DeclID.
2833 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2834 clang::io::Emit16(Out, DataLen);
2835
2836 return std::make_pair(KeyLen, DataLen);
2837 }
2838
Chris Lattner5f9e2722011-07-23 10:55:15 +00002839 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002840 using namespace clang::io;
2841
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002842 Emit8(Out, Name.getNameKind());
2843 switch (Name.getNameKind()) {
2844 case DeclarationName::Identifier:
2845 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002846 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002847 case DeclarationName::ObjCZeroArgSelector:
2848 case DeclarationName::ObjCOneArgSelector:
2849 case DeclarationName::ObjCMultiArgSelector:
2850 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002851 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002852 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002853 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2854 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002855 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002856 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002857 case DeclarationName::CXXLiteralOperatorName:
2858 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002859 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002860 case DeclarationName::CXXConstructorName:
2861 case DeclarationName::CXXDestructorName:
2862 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002863 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002864 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002865 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002866
2867 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002868 }
2869
Chris Lattner5f9e2722011-07-23 10:55:15 +00002870 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002871 data_type Lookup, unsigned DataLen) {
2872 uint64_t Start = Out.tell(); (void)Start;
2873 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2874 for (; Lookup.first != Lookup.second; ++Lookup.first)
2875 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2876
2877 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2878 }
2879};
2880} // end anonymous namespace
2881
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002882/// \brief Write the block containing all of the declaration IDs
2883/// visible from the given DeclContext.
2884///
2885/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002886/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002887uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2888 DeclContext *DC) {
2889 if (DC->getPrimaryContext() != DC)
2890 return 0;
2891
2892 // Since there is no name lookup into functions or methods, don't bother to
2893 // build a visible-declarations table for these entities.
2894 if (DC->isFunctionOrMethod())
2895 return 0;
2896
2897 // If not in C++, we perform name lookup for the translation unit via the
2898 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2899 // FIXME: In C++ we need the visible declarations in order to "see" the
2900 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002901 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002902 return 0;
2903
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002904 // Serialize the contents of the mapping used for lookup. Note that,
2905 // although we have two very different code paths, the serialized
2906 // representation is the same for both cases: a declaration name,
2907 // followed by a size, followed by references to the visible
2908 // declarations that have that name.
2909 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002910 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002911 if (!Map || Map->empty())
2912 return 0;
2913
2914 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2915 ASTDeclContextNameLookupTrait Trait(*this);
2916
2917 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002918 DeclarationName ConversionName;
2919 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002920 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2921 D != DEnd; ++D) {
2922 DeclarationName Name = D->first;
2923 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002924 if (Result.first != Result.second) {
2925 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2926 // Hash all conversion function names to the same name. The actual
2927 // type information in conversion function name is not used in the
2928 // key (since such type information is not stable across different
2929 // modules), so the intended effect is to coalesce all of the conversion
2930 // functions under a single key.
2931 if (!ConversionName)
2932 ConversionName = Name;
2933 ConversionDecls.append(Result.first, Result.second);
2934 continue;
2935 }
2936
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002937 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002938 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002939 }
2940
Douglas Gregore5a54b62011-08-30 20:49:19 +00002941 // Add the conversion functions
2942 if (!ConversionDecls.empty()) {
2943 Generator.insert(ConversionName,
2944 DeclContext::lookup_result(ConversionDecls.begin(),
2945 ConversionDecls.end()),
2946 Trait);
2947 }
2948
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002949 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002950 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002951 uint32_t BucketOffset;
2952 {
2953 llvm::raw_svector_ostream Out(LookupTable);
2954 // Make sure that no bucket is at offset 0
2955 clang::io::Emit32(Out, 0);
2956 BucketOffset = Generator.Emit(Out, Trait);
2957 }
2958
2959 // Write the lookup table
2960 RecordData Record;
2961 Record.push_back(DECL_CONTEXT_VISIBLE);
2962 Record.push_back(BucketOffset);
2963 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2964 LookupTable.str());
2965
2966 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2967 ++NumVisibleDeclContexts;
2968 return Offset;
2969}
2970
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002971/// \brief Write an UPDATE_VISIBLE block for the given context.
2972///
2973/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2974/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00002975/// (in C++), for namespaces, and for classes with forward-declared unscoped
2976/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002977void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002978 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2979 if (!Map || Map->empty())
2980 return;
2981
2982 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2983 ASTDeclContextNameLookupTrait Trait(*this);
2984
2985 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002986 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2987 D != DEnd; ++D) {
2988 DeclarationName Name = D->first;
2989 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002990 // For any name that appears in this table, the results are complete, i.e.
2991 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002992 if (Result.first != Result.second)
2993 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002994 }
2995
2996 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002997 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002998 uint32_t BucketOffset;
2999 {
3000 llvm::raw_svector_ostream Out(LookupTable);
3001 // Make sure that no bucket is at offset 0
3002 clang::io::Emit32(Out, 0);
3003 BucketOffset = Generator.Emit(Out, Trait);
3004 }
3005
3006 // Write the lookup table
3007 RecordData Record;
3008 Record.push_back(UPDATE_VISIBLE);
3009 Record.push_back(getDeclID(cast<Decl>(DC)));
3010 Record.push_back(BucketOffset);
3011 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3012}
3013
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003014/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3015void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3016 RecordData Record;
3017 Record.push_back(Opts.fp_contract);
3018 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3019}
3020
3021/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3022void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003023 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003024 return;
3025
3026 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3027 RecordData Record;
3028#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3029#include "clang/Basic/OpenCLExtensions.def"
3030 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3031}
3032
Douglas Gregor2171bf12012-01-15 16:58:34 +00003033void ASTWriter::WriteRedeclarations() {
3034 RecordData LocalRedeclChains;
3035 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3036
3037 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3038 Decl *First = Redeclarations[I];
3039 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3040
3041 Decl *MostRecent = First->getMostRecentDecl();
3042
3043 // If we only have a single declaration, there is no point in storing
3044 // a redeclaration chain.
3045 if (First == MostRecent)
3046 continue;
3047
3048 unsigned Offset = LocalRedeclChains.size();
3049 unsigned Size = 0;
3050 LocalRedeclChains.push_back(0); // Placeholder for the size.
3051
3052 // Collect the set of local redeclarations of this declaration.
3053 for (Decl *Prev = MostRecent; Prev != First;
3054 Prev = Prev->getPreviousDecl()) {
3055 if (!Prev->isFromASTFile()) {
3056 AddDeclRef(Prev, LocalRedeclChains);
3057 ++Size;
3058 }
3059 }
3060 LocalRedeclChains[Offset] = Size;
3061
3062 // Reverse the set of local redeclarations, so that we store them in
3063 // order (since we found them in reverse order).
3064 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3065
3066 // Add the mapping from the first ID to the set of local declarations.
3067 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3068 LocalRedeclsMap.push_back(Info);
3069
3070 assert(N == Redeclarations.size() &&
3071 "Deserialized a declaration we shouldn't have");
3072 }
3073
3074 if (LocalRedeclChains.empty())
3075 return;
3076
3077 // Sort the local redeclarations map by the first declaration ID,
3078 // since the reader will be performing binary searches on this information.
3079 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3080
3081 // Emit the local redeclarations map.
3082 using namespace llvm;
3083 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3084 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3087 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3088
3089 RecordData Record;
3090 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3091 Record.push_back(LocalRedeclsMap.size());
3092 Stream.EmitRecordWithBlob(AbbrevID, Record,
3093 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3094 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3095
3096 // Emit the redeclaration chains.
3097 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3098}
3099
Douglas Gregorcff9f262012-01-27 01:47:08 +00003100void ASTWriter::WriteObjCCategories() {
3101 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3102 RecordData Categories;
3103
3104 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3105 unsigned Size = 0;
3106 unsigned StartIndex = Categories.size();
3107
3108 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3109
3110 // Allocate space for the size.
3111 Categories.push_back(0);
3112
3113 // Add the categories.
3114 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3115 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3116 assert(getDeclID(Cat) != 0 && "Bogus category");
3117 AddDeclRef(Cat, Categories);
3118 }
3119
3120 // Update the size.
3121 Categories[StartIndex] = Size;
3122
3123 // Record this interface -> category map.
3124 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3125 CategoriesMap.push_back(CatInfo);
3126 }
3127
3128 // Sort the categories map by the definition ID, since the reader will be
3129 // performing binary searches on this information.
3130 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3131
3132 // Emit the categories map.
3133 using namespace llvm;
3134 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3135 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3138 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3139
3140 RecordData Record;
3141 Record.push_back(OBJC_CATEGORIES_MAP);
3142 Record.push_back(CategoriesMap.size());
3143 Stream.EmitRecordWithBlob(AbbrevID, Record,
3144 reinterpret_cast<char*>(CategoriesMap.data()),
3145 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3146
3147 // Emit the category lists.
3148 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3149}
3150
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003151void ASTWriter::WriteMergedDecls() {
3152 if (!Chain || Chain->MergedDecls.empty())
3153 return;
3154
3155 RecordData Record;
3156 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3157 IEnd = Chain->MergedDecls.end();
3158 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003159 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003160 : getDeclID(I->first);
3161 assert(CanonID && "Merged declaration not known?");
3162
3163 Record.push_back(CanonID);
3164 Record.push_back(I->second.size());
3165 Record.append(I->second.begin(), I->second.end());
3166 }
3167 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3168}
3169
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003170//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003171// General Serialization Routines
3172//===----------------------------------------------------------------------===//
3173
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003174/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003175void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3176 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003177 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003178 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3179 e = Attrs.end(); i != e; ++i){
3180 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003181 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003182 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003183
Sean Huntcf807c42010-08-18 23:23:40 +00003184#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003185
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003186 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003187}
3188
Chris Lattner5f9e2722011-07-23 10:55:15 +00003189void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003190 Record.push_back(Str.size());
3191 Record.insert(Record.end(), Str.begin(), Str.end());
3192}
3193
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003194void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3195 RecordDataImpl &Record) {
3196 Record.push_back(Version.getMajor());
3197 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3198 Record.push_back(*Minor + 1);
3199 else
3200 Record.push_back(0);
3201 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3202 Record.push_back(*Subminor + 1);
3203 else
3204 Record.push_back(0);
3205}
3206
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003207/// \brief Note that the identifier II occurs at the given offset
3208/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003209void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003210 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003211 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003212 // up earlier in the chain and thus don't need an offset.
3213 if (ID >= FirstIdentID)
3214 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003215}
3216
Douglas Gregor83941df2009-04-25 17:48:32 +00003217/// \brief Note that the selector Sel occurs at the given offset
3218/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003219void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003220 unsigned ID = SelectorIDs[Sel];
3221 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003222 // Don't record offsets for selectors that are also available in a different
3223 // file.
3224 if (ID < FirstSelectorID)
3225 return;
3226 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003227}
3228
Sebastian Redla4232eb2010-08-18 23:56:21 +00003229ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003230 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003231 WritingAST(false), DoneWritingDeclsAndTypes(false),
3232 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003233 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003234 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003235 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3236 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003237 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3238 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003239 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003240 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003241 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003242 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003243 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003244 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003245 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3246 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3247 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003248 DeclTypedefAbbrev(0),
3249 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3250 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003251{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003252}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003253
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003254ASTWriter::~ASTWriter() {
3255 for (FileDeclIDsTy::iterator
3256 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3257 delete I->second;
3258}
3259
Sebastian Redla4232eb2010-08-18 23:56:21 +00003260void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003261 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003262 Module *WritingModule, StringRef isysroot,
3263 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003264 WritingAST = true;
3265
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003266 ASTHasCompilerErrors = hasErrors;
3267
Douglas Gregor2cf26342009-04-09 22:27:44 +00003268 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003269 Stream.Emit((unsigned)'C', 8);
3270 Stream.Emit((unsigned)'P', 8);
3271 Stream.Emit((unsigned)'C', 8);
3272 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003273
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003274 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003275
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003276 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003277 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003278 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003279 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003280 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003281 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003282 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003283
3284 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003285}
3286
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003287template<typename Vector>
3288static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3289 ASTWriter::RecordData &Record) {
3290 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3291 I != E; ++I) {
3292 Writer.AddDeclRef(*I, Record);
3293 }
3294}
3295
Sebastian Redla4232eb2010-08-18 23:56:21 +00003296void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003297 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003298 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003299 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003300 using namespace llvm;
3301
Douglas Gregorecc2c092011-12-01 22:20:10 +00003302 // Make sure that the AST reader knows to finalize itself.
3303 if (Chain)
3304 Chain->finalizeForWriting();
3305
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003306 ASTContext &Context = SemaRef.Context;
3307 Preprocessor &PP = SemaRef.PP;
3308
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003309 // Set up predefined declaration IDs.
3310 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003311 if (Context.ObjCIdDecl)
3312 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003313 if (Context.ObjCSelDecl)
3314 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003315 if (Context.ObjCClassDecl)
3316 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003317 if (Context.ObjCProtocolClassDecl)
3318 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003319 if (Context.Int128Decl)
3320 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3321 if (Context.UInt128Decl)
3322 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003323 if (Context.ObjCInstanceTypeDecl)
3324 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003325 if (Context.BuiltinVaListDecl)
3326 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3327
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003328 if (!Chain) {
3329 // Make sure that we emit IdentifierInfos (and any attached
3330 // declarations) for builtins. We don't need to do this when we're
3331 // emitting chained PCH files, because all of the builtins will be
3332 // in the original PCH file.
3333 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003334 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003335 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003336 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003337 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003338 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3339 getIdentifierRef(&Table.get(BuiltinNames[I]));
3340 }
3341
Douglas Gregoreee242f2011-10-27 09:33:13 +00003342 // If there are any out-of-date identifiers, bring them up to date.
3343 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3344 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3345 IDEnd = PP.getIdentifierTable().end();
3346 ID != IDEnd; ++ID)
3347 if (ID->second->isOutOfDate())
3348 ExtSource->updateOutOfDateIdentifier(*ID->second);
3349 }
3350
Chris Lattner63d65f82009-09-08 18:19:27 +00003351 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003352 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003353 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003354 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003355 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003356
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003357 // Build a record containing all of the file scoped decls in this file.
3358 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003359 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3360 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003361
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003362 // Build a record containing all of the delegating constructors we still need
3363 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003364 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003365 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003366
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003367 // Write the set of weak, undeclared identifiers. We always write the
3368 // entire table, since later PCH files in a PCH chain are only interested in
3369 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003370 RecordData WeakUndeclaredIdentifiers;
3371 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003372 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003373 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3374 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3375 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3376 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3377 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3378 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3379 }
3380 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003381
Douglas Gregor14c22f22009-04-22 22:18:58 +00003382 // Build a record containing all of the locally-scoped external
3383 // declarations in this header file. Generally, this record will be
3384 // empty.
3385 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003386 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003387 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003388 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003389 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3390 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003391 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003392 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003393 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3394 }
3395
Douglas Gregorb81c1702009-04-27 20:06:05 +00003396 // Build a record containing all of the ext_vector declarations.
3397 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003398 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003399
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003400 // Build a record containing all of the VTable uses information.
3401 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003402 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003403 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3404 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3405 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3406 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3407 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003408 }
3409
3410 // Build a record containing all of dynamic classes declarations.
3411 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003412 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003413
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003414 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003415 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003416 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003417 I = SemaRef.PendingInstantiations.begin(),
3418 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3419 AddDeclRef(I->first, PendingInstantiations);
3420 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003421 }
3422 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3423 "There are local ones at end of translation unit!");
3424
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003425 // Build a record containing some declaration references.
3426 RecordData SemaDeclRefs;
3427 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3428 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3429 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3430 }
3431
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003432 RecordData CUDASpecialDeclRefs;
3433 if (Context.getcudaConfigureCallDecl()) {
3434 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3435 }
3436
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003437 // Build a record containing all of the known namespaces.
3438 RecordData KnownNamespaces;
3439 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3440 I = SemaRef.KnownNamespaces.begin(),
3441 IEnd = SemaRef.KnownNamespaces.end();
3442 I != IEnd; ++I) {
3443 if (!I->second)
3444 AddDeclRef(I->first, KnownNamespaces);
3445 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003446
3447 // Write the control block
3448 WriteControlBlock(Context, isysroot, OutputFile);
3449
Sebastian Redl3397c552010-08-18 23:56:27 +00003450 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003451 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003452 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor832d6202011-07-22 16:35:34 +00003453 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003454 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003455
3456 // Create a lexical update block containing all of the declarations in the
3457 // translation unit that do not come from other AST files.
3458 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3459 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3460 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3461 E = TU->noload_decls_end();
3462 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003463 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003464 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003465 }
3466
3467 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3468 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3469 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3470 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3471 Record.clear();
3472 Record.push_back(TU_UPDATE_LEXICAL);
3473 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3474 data(NewGlobalDecls));
3475
3476 // And a visible updates block for the translation unit.
3477 Abv = new llvm::BitCodeAbbrev();
3478 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3479 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3480 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3481 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3482 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3483 WriteDeclContextVisibleUpdate(TU);
3484
3485 // If the translation unit has an anonymous namespace, and we don't already
3486 // have an update block for it, write it as an update block.
3487 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3488 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3489 if (Record.empty()) {
3490 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003491 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003492 }
3493 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003494
3495 // Make sure visible decls, added to DeclContexts previously loaded from
3496 // an AST file, are registered for serialization.
3497 for (SmallVector<const Decl *, 16>::iterator
3498 I = UpdatingVisibleDecls.begin(),
3499 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3500 GetDeclRef(*I);
3501 }
3502
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003503 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003504 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003505
Douglas Gregora119da02011-08-02 16:26:37 +00003506 // Form the record of special types.
3507 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003508 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003509 AddTypeRef(Context.getFILEType(), SpecialTypes);
3510 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3511 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3512 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3513 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003514 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003515 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003516
Douglas Gregor366809a2009-04-26 03:49:13 +00003517 // Keep writing types and declarations until all types and
3518 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003519 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003520 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003521 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3522 E = DeclsToRewrite.end();
3523 I != E; ++I)
3524 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003525 while (!DeclTypesToEmit.empty()) {
3526 DeclOrType DOT = DeclTypesToEmit.front();
3527 DeclTypesToEmit.pop();
3528 if (DOT.isType())
3529 WriteType(DOT.getType());
3530 else
3531 WriteDecl(Context, DOT.getDecl());
3532 }
3533 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003534
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003535 DoneWritingDeclsAndTypes = true;
3536
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003537 WriteFileDeclIDsMap();
3538 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003539 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003540
3541 if (Chain) {
3542 // Write the mapping information describing our module dependencies and how
3543 // each of those modules were mapped into our own offset/ID space, so that
3544 // the reader can build the appropriate mapping to its own offset/ID space.
3545 // The map consists solely of a blob with the following format:
3546 // *(module-name-len:i16 module-name:len*i8
3547 // source-location-offset:i32
3548 // identifier-id:i32
3549 // preprocessed-entity-id:i32
3550 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003551 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003552 // selector-id:i32
3553 // declaration-id:i32
3554 // c++-base-specifiers-id:i32
3555 // type-id:i32)
3556 //
3557 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3558 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3560 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003561 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003562 {
3563 llvm::raw_svector_ostream Out(Buffer);
3564 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003565 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003566 M != MEnd; ++M) {
3567 StringRef FileName = (*M)->FileName;
3568 io::Emit16(Out, FileName.size());
3569 Out.write(FileName.data(), FileName.size());
3570 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3571 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003572 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003573 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003574 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003575 io::Emit32(Out, (*M)->BaseSelectorID);
3576 io::Emit32(Out, (*M)->BaseDeclID);
3577 io::Emit32(Out, (*M)->BaseTypeIndex);
3578 }
3579 }
3580 Record.clear();
3581 Record.push_back(MODULE_OFFSET_MAP);
3582 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3583 Buffer.data(), Buffer.size());
3584 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003585 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003586 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003587 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003588 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003589 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003590 WriteFPPragmaOptions(SemaRef.getFPOptions());
3591 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003592
Sebastian Redl1476ed42010-07-16 16:36:56 +00003593 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003594 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003595
Anders Carlssonc8505782011-03-06 18:41:18 +00003596 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003597
Douglas Gregore209e502011-12-06 01:10:29 +00003598 // If we're emitting a module, write out the submodule information.
3599 if (WritingModule)
3600 WriteSubmodules(WritingModule);
3601
Douglas Gregora119da02011-08-02 16:26:37 +00003602 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3603
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003604 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003605 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003606 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003607
3608 // Write the record containing tentative definitions.
3609 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003610 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003611
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003612 // Write the record containing unused file scoped decls.
3613 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003614 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003615
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003616 // Write the record containing weak undeclared identifiers.
3617 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003618 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003619 WeakUndeclaredIdentifiers);
3620
Douglas Gregor14c22f22009-04-22 22:18:58 +00003621 // Write the record containing locally-scoped external definitions.
3622 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003623 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003624 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003625
3626 // Write the record containing ext_vector type names.
3627 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003628 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003629
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003630 // Write the record containing VTable uses information.
3631 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003632 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003633
3634 // Write the record containing dynamic classes declarations.
3635 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003636 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003637
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003638 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003639 if (!PendingInstantiations.empty())
3640 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003641
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003642 // Write the record containing declaration references of Sema.
3643 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003644 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003645
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003646 // Write the record containing CUDA-specific declaration references.
3647 if (!CUDASpecialDeclRefs.empty())
3648 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003649
3650 // Write the delegating constructors.
3651 if (!DelegatingCtorDecls.empty())
3652 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003653
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003654 // Write the known namespaces.
3655 if (!KnownNamespaces.empty())
3656 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3657
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003658 // Write the visible updates to DeclContexts.
3659 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3660 I = UpdatedDeclContexts.begin(),
3661 E = UpdatedDeclContexts.end();
3662 I != E; ++I)
3663 WriteDeclContextVisibleUpdate(*I);
3664
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003665 if (!WritingModule) {
3666 // Write the submodules that were imported, if any.
3667 RecordData ImportedModules;
3668 for (ASTContext::import_iterator I = Context.local_import_begin(),
3669 IEnd = Context.local_import_end();
3670 I != IEnd; ++I) {
3671 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3672 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3673 }
3674 if (!ImportedModules.empty()) {
3675 // Sort module IDs.
3676 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3677
3678 // Unique module IDs.
3679 ImportedModules.erase(std::unique(ImportedModules.begin(),
3680 ImportedModules.end()),
3681 ImportedModules.end());
3682
3683 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3684 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003685 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003686
3687 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003688 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003689 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003690 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003691 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003692 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003693
Douglas Gregor3e1af842009-04-17 22:13:46 +00003694 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003695 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003696 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003697 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003698 Record.push_back(NumLexicalDeclContexts);
3699 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003700 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003701 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003702}
3703
Douglas Gregora8235d62012-10-09 23:05:51 +00003704void ASTWriter::WriteMacroUpdates() {
3705 if (MacroUpdates.empty())
3706 return;
3707
3708 RecordData Record;
3709 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3710 E = MacroUpdates.end();
3711 I != E; ++I) {
3712 addMacroRef(I->first, Record);
3713 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003714 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003715 }
3716 Stream.EmitRecord(MACRO_UPDATES, Record);
3717}
3718
Douglas Gregor61c5e342011-09-17 00:05:03 +00003719/// \brief Go through the declaration update blocks and resolve declaration
3720/// pointers into declaration IDs.
3721void ASTWriter::ResolveDeclUpdatesBlocks() {
3722 for (DeclUpdateMap::iterator
3723 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3724 const Decl *D = I->first;
3725 UpdateRecord &URec = I->second;
3726
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003727 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003728 continue; // The decl will be written completely
3729
3730 unsigned Idx = 0, N = URec.size();
3731 while (Idx < N) {
3732 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003733 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3734 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3735 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3736 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3737 ++Idx;
3738 break;
3739
3740 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3741 ++Idx;
3742 break;
3743 }
3744 }
3745 }
3746}
3747
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003748void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003749 if (DeclUpdates.empty())
3750 return;
3751
3752 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003753 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003754 for (DeclUpdateMap::iterator
3755 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3756 const Decl *D = I->first;
3757 UpdateRecord &URec = I->second;
3758
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003759 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003760 continue; // The decl will be written completely,no need to store updates.
3761
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003762 uint64_t Offset = Stream.GetCurrentBitNo();
3763 Stream.EmitRecord(DECL_UPDATES, URec);
3764
3765 OffsetsRecord.push_back(GetDeclRef(D));
3766 OffsetsRecord.push_back(Offset);
3767 }
3768 Stream.ExitBlock();
3769 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3770}
3771
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003772void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003773 if (ReplacedDecls.empty())
3774 return;
3775
3776 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003777 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003778 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003779 Record.push_back(I->ID);
3780 Record.push_back(I->Offset);
3781 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003782 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003783 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003784}
3785
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003786void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003787 Record.push_back(Loc.getRawEncoding());
3788}
3789
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003790void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003791 AddSourceLocation(Range.getBegin(), Record);
3792 AddSourceLocation(Range.getEnd(), Record);
3793}
3794
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003795void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003796 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003797 const uint64_t *Words = Value.getRawData();
3798 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003799}
3800
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003801void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003802 Record.push_back(Value.isUnsigned());
3803 AddAPInt(Value, Record);
3804}
3805
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003806void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003807 AddAPInt(Value.bitcastToAPInt(), Record);
3808}
3809
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003810void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003811 Record.push_back(getIdentifierRef(II));
3812}
3813
Douglas Gregora8235d62012-10-09 23:05:51 +00003814void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3815 Record.push_back(getMacroRef(MI));
3816}
3817
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003818IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003819 if (II == 0)
3820 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003821
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003822 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003823 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003824 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003825 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003826}
3827
Douglas Gregora8235d62012-10-09 23:05:51 +00003828MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3829 // Don't emit builtin macros like __LINE__ to the AST file unless they
3830 // have been redefined by the header (in which case they are not
3831 // isBuiltinMacro).
3832 if (MI == 0 || MI->isBuiltinMacro())
3833 return 0;
3834
3835 MacroID &ID = MacroIDs[MI];
3836 if (ID == 0)
3837 ID = NextMacroID++;
3838 return ID;
3839}
3840
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003841void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003842 Record.push_back(getSelectorRef(SelRef));
3843}
3844
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003845SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003846 if (Sel.getAsOpaquePtr() == 0) {
3847 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003848 }
3849
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003850 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003851 if (SID == 0 && Chain) {
3852 // This might trigger a ReadSelector callback, which will set the ID for
3853 // this selector.
3854 Chain->LoadSelector(Sel);
3855 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003856 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003857 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003858 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003859 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003860}
3861
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003862void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003863 AddDeclRef(Temp->getDestructor(), Record);
3864}
3865
Douglas Gregor7c789c12010-10-29 22:39:52 +00003866void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3867 CXXBaseSpecifier const *BasesEnd,
3868 RecordDataImpl &Record) {
3869 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3870 CXXBaseSpecifiersToWrite.push_back(
3871 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3872 Bases, BasesEnd));
3873 Record.push_back(NextCXXBaseSpecifiersID++);
3874}
3875
Sebastian Redla4232eb2010-08-18 23:56:21 +00003876void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003877 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003878 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003879 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003880 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003881 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003882 break;
3883 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003884 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003885 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003886 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003887 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003888 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003889 break;
3890 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003891 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003892 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003893 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003894 break;
John McCall833ca992009-10-29 08:12:44 +00003895 case TemplateArgument::Null:
3896 case TemplateArgument::Integral:
3897 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003898 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003899 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003900 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003901 break;
3902 }
3903}
3904
Sebastian Redla4232eb2010-08-18 23:56:21 +00003905void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003906 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003907 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003908
3909 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3910 bool InfoHasSameExpr
3911 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3912 Record.push_back(InfoHasSameExpr);
3913 if (InfoHasSameExpr)
3914 return; // Avoid storing the same expr twice.
3915 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003916 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3917 Record);
3918}
3919
Douglas Gregordc355712011-02-25 00:36:19 +00003920void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3921 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003922 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003923 AddTypeRef(QualType(), Record);
3924 return;
3925 }
3926
Douglas Gregordc355712011-02-25 00:36:19 +00003927 AddTypeLoc(TInfo->getTypeLoc(), Record);
3928}
3929
3930void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3931 AddTypeRef(TL.getType(), Record);
3932
John McCalla1ee0c52009-10-16 21:56:05 +00003933 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003934 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003935 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003936}
3937
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003938void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003939 Record.push_back(GetOrCreateTypeID(T));
3940}
3941
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003942TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3943 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003944 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3945}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003946
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003947TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003948 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003949 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003950}
3951
3952TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3953 if (T.isNull())
3954 return TypeIdx();
3955 assert(!T.getLocalFastQualifiers());
3956
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003957 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003958 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003959 if (DoneWritingDeclsAndTypes) {
3960 assert(0 && "New type seen after serializing all the types to emit!");
3961 return TypeIdx();
3962 }
3963
Douglas Gregor366809a2009-04-26 03:49:13 +00003964 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003965 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003966 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003967 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003968 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003969 return Idx;
3970}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003971
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003972TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003973 if (T.isNull())
3974 return TypeIdx();
3975 assert(!T.getLocalFastQualifiers());
3976
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003977 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3978 assert(I != TypeIdxs.end() && "Type not emitted!");
3979 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003980}
3981
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003982void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003983 Record.push_back(GetDeclRef(D));
3984}
3985
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003986DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003987 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3988
Douglas Gregor2cf26342009-04-09 22:27:44 +00003989 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003990 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003991 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003992
3993 // If D comes from an AST file, its declaration ID is already known and
3994 // fixed.
3995 if (D->isFromASTFile())
3996 return D->getGlobalID();
3997
Douglas Gregor97475832010-10-05 18:37:06 +00003998 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003999 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004000 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004001 if (DoneWritingDeclsAndTypes) {
4002 assert(0 && "New decl seen after serializing all the decls to emit!");
4003 return 0;
4004 }
4005
Douglas Gregor2cf26342009-04-09 22:27:44 +00004006 // We haven't seen this declaration before. Give it a new ID and
4007 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004008 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004009 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004010 }
4011
Sebastian Redl681d7232010-07-27 00:17:23 +00004012 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004013}
4014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004015DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004016 if (D == 0)
4017 return 0;
4018
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004019 // If D comes from an AST file, its declaration ID is already known and
4020 // fixed.
4021 if (D->isFromASTFile())
4022 return D->getGlobalID();
4023
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004024 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4025 return DeclIDs[D];
4026}
4027
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004028static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4029 std::pair<unsigned, serialization::DeclID> R) {
4030 return L.first < R.first;
4031}
4032
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004033void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004034 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004035 assert(D);
4036
4037 SourceLocation Loc = D->getLocation();
4038 if (Loc.isInvalid())
4039 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004040
4041 // We only keep track of the file-level declarations of each file.
4042 if (!D->getLexicalDeclContext()->isFileContext())
4043 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004044 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4045 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004046 if (isa<ParmVarDecl>(D))
4047 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004048
4049 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004050 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004051 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004052 FileID FID;
4053 unsigned Offset;
4054 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004055 if (FID.isInvalid())
4056 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004057 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004058
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004059 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004060 if (!Info)
4061 Info = new DeclIDInFileInfo();
4062
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004063 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004064 LocDeclIDsTy &Decls = Info->DeclIDs;
4065
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004066 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004067 Decls.push_back(LocDecl);
4068 return;
4069 }
4070
4071 LocDeclIDsTy::iterator
4072 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4073
4074 Decls.insert(I, LocDecl);
4075}
4076
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004077void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004078 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004079 Record.push_back(Name.getNameKind());
4080 switch (Name.getNameKind()) {
4081 case DeclarationName::Identifier:
4082 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4083 break;
4084
4085 case DeclarationName::ObjCZeroArgSelector:
4086 case DeclarationName::ObjCOneArgSelector:
4087 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004088 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004089 break;
4090
4091 case DeclarationName::CXXConstructorName:
4092 case DeclarationName::CXXDestructorName:
4093 case DeclarationName::CXXConversionFunctionName:
4094 AddTypeRef(Name.getCXXNameType(), Record);
4095 break;
4096
4097 case DeclarationName::CXXOperatorName:
4098 Record.push_back(Name.getCXXOverloadedOperator());
4099 break;
4100
Sean Hunt3e518bd2009-11-29 07:34:05 +00004101 case DeclarationName::CXXLiteralOperatorName:
4102 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4103 break;
4104
Douglas Gregor2cf26342009-04-09 22:27:44 +00004105 case DeclarationName::CXXUsingDirective:
4106 // No extra data to emit
4107 break;
4108 }
4109}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004110
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004111void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004112 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004113 switch (Name.getNameKind()) {
4114 case DeclarationName::CXXConstructorName:
4115 case DeclarationName::CXXDestructorName:
4116 case DeclarationName::CXXConversionFunctionName:
4117 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4118 break;
4119
4120 case DeclarationName::CXXOperatorName:
4121 AddSourceLocation(
4122 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4123 Record);
4124 AddSourceLocation(
4125 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4126 Record);
4127 break;
4128
4129 case DeclarationName::CXXLiteralOperatorName:
4130 AddSourceLocation(
4131 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4132 Record);
4133 break;
4134
4135 case DeclarationName::Identifier:
4136 case DeclarationName::ObjCZeroArgSelector:
4137 case DeclarationName::ObjCOneArgSelector:
4138 case DeclarationName::ObjCMultiArgSelector:
4139 case DeclarationName::CXXUsingDirective:
4140 break;
4141 }
4142}
4143
4144void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004145 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004146 AddDeclarationName(NameInfo.getName(), Record);
4147 AddSourceLocation(NameInfo.getLoc(), Record);
4148 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4149}
4150
4151void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004152 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004153 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004154 Record.push_back(Info.NumTemplParamLists);
4155 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4156 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4157}
4158
Sebastian Redla4232eb2010-08-18 23:56:21 +00004159void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004160 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004161 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004162 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004163 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004164
4165 // Push each of the NNS's onto a stack for serialization in reverse order.
4166 while (NNS) {
4167 NestedNames.push_back(NNS);
4168 NNS = NNS->getPrefix();
4169 }
4170
4171 Record.push_back(NestedNames.size());
4172 while(!NestedNames.empty()) {
4173 NNS = NestedNames.pop_back_val();
4174 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4175 Record.push_back(Kind);
4176 switch (Kind) {
4177 case NestedNameSpecifier::Identifier:
4178 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4179 break;
4180
4181 case NestedNameSpecifier::Namespace:
4182 AddDeclRef(NNS->getAsNamespace(), Record);
4183 break;
4184
Douglas Gregor14aba762011-02-24 02:36:08 +00004185 case NestedNameSpecifier::NamespaceAlias:
4186 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4187 break;
4188
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004189 case NestedNameSpecifier::TypeSpec:
4190 case NestedNameSpecifier::TypeSpecWithTemplate:
4191 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4192 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4193 break;
4194
4195 case NestedNameSpecifier::Global:
4196 // Don't need to write an associated value.
4197 break;
4198 }
4199 }
4200}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004201
Douglas Gregordc355712011-02-25 00:36:19 +00004202void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4203 RecordDataImpl &Record) {
4204 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004205 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004206 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004207
4208 // Push each of the nested-name-specifiers's onto a stack for
4209 // serialization in reverse order.
4210 while (NNS) {
4211 NestedNames.push_back(NNS);
4212 NNS = NNS.getPrefix();
4213 }
4214
4215 Record.push_back(NestedNames.size());
4216 while(!NestedNames.empty()) {
4217 NNS = NestedNames.pop_back_val();
4218 NestedNameSpecifier::SpecifierKind Kind
4219 = NNS.getNestedNameSpecifier()->getKind();
4220 Record.push_back(Kind);
4221 switch (Kind) {
4222 case NestedNameSpecifier::Identifier:
4223 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4224 AddSourceRange(NNS.getLocalSourceRange(), Record);
4225 break;
4226
4227 case NestedNameSpecifier::Namespace:
4228 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4229 AddSourceRange(NNS.getLocalSourceRange(), Record);
4230 break;
4231
4232 case NestedNameSpecifier::NamespaceAlias:
4233 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4234 AddSourceRange(NNS.getLocalSourceRange(), Record);
4235 break;
4236
4237 case NestedNameSpecifier::TypeSpec:
4238 case NestedNameSpecifier::TypeSpecWithTemplate:
4239 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4240 AddTypeLoc(NNS.getTypeLoc(), Record);
4241 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4242 break;
4243
4244 case NestedNameSpecifier::Global:
4245 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4246 break;
4247 }
4248 }
4249}
4250
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004251void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004252 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004253 Record.push_back(Kind);
4254 switch (Kind) {
4255 case TemplateName::Template:
4256 AddDeclRef(Name.getAsTemplateDecl(), Record);
4257 break;
4258
4259 case TemplateName::OverloadedTemplate: {
4260 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4261 Record.push_back(OvT->size());
4262 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4263 I != E; ++I)
4264 AddDeclRef(*I, Record);
4265 break;
4266 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004267
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004268 case TemplateName::QualifiedTemplate: {
4269 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4270 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4271 Record.push_back(QualT->hasTemplateKeyword());
4272 AddDeclRef(QualT->getTemplateDecl(), Record);
4273 break;
4274 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004275
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004276 case TemplateName::DependentTemplate: {
4277 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4278 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4279 Record.push_back(DepT->isIdentifier());
4280 if (DepT->isIdentifier())
4281 AddIdentifierRef(DepT->getIdentifier(), Record);
4282 else
4283 Record.push_back(DepT->getOperator());
4284 break;
4285 }
John McCall14606042011-06-30 08:33:18 +00004286
4287 case TemplateName::SubstTemplateTemplateParm: {
4288 SubstTemplateTemplateParmStorage *subst
4289 = Name.getAsSubstTemplateTemplateParm();
4290 AddDeclRef(subst->getParameter(), Record);
4291 AddTemplateName(subst->getReplacement(), Record);
4292 break;
4293 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004294
4295 case TemplateName::SubstTemplateTemplateParmPack: {
4296 SubstTemplateTemplateParmPackStorage *SubstPack
4297 = Name.getAsSubstTemplateTemplateParmPack();
4298 AddDeclRef(SubstPack->getParameterPack(), Record);
4299 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4300 break;
4301 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004302 }
4303}
4304
Michael J. Spencer20249a12010-10-21 03:16:25 +00004305void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004306 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004307 Record.push_back(Arg.getKind());
4308 switch (Arg.getKind()) {
4309 case TemplateArgument::Null:
4310 break;
4311 case TemplateArgument::Type:
4312 AddTypeRef(Arg.getAsType(), Record);
4313 break;
4314 case TemplateArgument::Declaration:
4315 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004316 Record.push_back(Arg.isDeclForReferenceParam());
4317 break;
4318 case TemplateArgument::NullPtr:
4319 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004320 break;
4321 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004322 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004323 AddTypeRef(Arg.getIntegralType(), Record);
4324 break;
4325 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004326 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4327 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004328 case TemplateArgument::TemplateExpansion:
4329 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004330 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4331 Record.push_back(*NumExpansions + 1);
4332 else
4333 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004334 break;
4335 case TemplateArgument::Expression:
4336 AddStmt(Arg.getAsExpr());
4337 break;
4338 case TemplateArgument::Pack:
4339 Record.push_back(Arg.pack_size());
4340 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4341 I != E; ++I)
4342 AddTemplateArgument(*I, Record);
4343 break;
4344 }
4345}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004346
4347void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004348ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004349 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004350 assert(TemplateParams && "No TemplateParams!");
4351 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4352 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4353 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4354 Record.push_back(TemplateParams->size());
4355 for (TemplateParameterList::const_iterator
4356 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4357 P != PEnd; ++P)
4358 AddDeclRef(*P, Record);
4359}
4360
4361/// \brief Emit a template argument list.
4362void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004363ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004364 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004365 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004366 Record.push_back(TemplateArgs->size());
4367 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004368 AddTemplateArgument(TemplateArgs->get(i), Record);
4369}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004370
4371
4372void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004373ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004374 Record.push_back(Set.size());
4375 for (UnresolvedSetImpl::const_iterator
4376 I = Set.begin(), E = Set.end(); I != E; ++I) {
4377 AddDeclRef(I.getDecl(), Record);
4378 Record.push_back(I.getAccess());
4379 }
4380}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004381
Sebastian Redla4232eb2010-08-18 23:56:21 +00004382void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004383 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004384 Record.push_back(Base.isVirtual());
4385 Record.push_back(Base.isBaseOfClass());
4386 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004387 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004388 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004389 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004390 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4391 : SourceLocation(),
4392 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004393}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004394
Douglas Gregor7c789c12010-10-29 22:39:52 +00004395void ASTWriter::FlushCXXBaseSpecifiers() {
4396 RecordData Record;
4397 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4398 Record.clear();
4399
4400 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004401 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004402 if (Index == CXXBaseSpecifiersOffsets.size())
4403 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4404 else {
4405 if (Index > CXXBaseSpecifiersOffsets.size())
4406 CXXBaseSpecifiersOffsets.resize(Index + 1);
4407 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4408 }
4409
4410 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4411 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4412 Record.push_back(BEnd - B);
4413 for (; B != BEnd; ++B)
4414 AddCXXBaseSpecifier(*B, Record);
4415 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004416
4417 // Flush any expressions that were written as part of the base specifiers.
4418 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004419 }
4420
4421 CXXBaseSpecifiersToWrite.clear();
4422}
4423
Sean Huntcbb67482011-01-08 20:30:50 +00004424void ASTWriter::AddCXXCtorInitializers(
4425 const CXXCtorInitializer * const *CtorInitializers,
4426 unsigned NumCtorInitializers,
4427 RecordDataImpl &Record) {
4428 Record.push_back(NumCtorInitializers);
4429 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4430 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004431
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004432 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004433 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004434 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004435 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004436 } else if (Init->isDelegatingInitializer()) {
4437 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004438 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004439 } else if (Init->isMemberInitializer()){
4440 Record.push_back(CTOR_INITIALIZER_MEMBER);
4441 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004442 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004443 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4444 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004445 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004446
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004447 AddSourceLocation(Init->getMemberLocation(), Record);
4448 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004449 AddSourceLocation(Init->getLParenLoc(), Record);
4450 AddSourceLocation(Init->getRParenLoc(), Record);
4451 Record.push_back(Init->isWritten());
4452 if (Init->isWritten()) {
4453 Record.push_back(Init->getSourceOrder());
4454 } else {
4455 Record.push_back(Init->getNumArrayIndices());
4456 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4457 AddDeclRef(Init->getArrayIndex(i), Record);
4458 }
4459 }
4460}
4461
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004462void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4463 assert(D->DefinitionData);
4464 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004465 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004466 Record.push_back(Data.UserDeclaredConstructor);
4467 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004468 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004469 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004470 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004471 Record.push_back(Data.UserDeclaredDestructor);
4472 Record.push_back(Data.Aggregate);
4473 Record.push_back(Data.PlainOldData);
4474 Record.push_back(Data.Empty);
4475 Record.push_back(Data.Polymorphic);
4476 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004477 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004478 Record.push_back(Data.HasNoNonEmptyBases);
4479 Record.push_back(Data.HasPrivateFields);
4480 Record.push_back(Data.HasProtectedFields);
4481 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004482 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004483 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004484 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004485 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004486 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004487 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004488 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004489 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004490 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004491 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004492 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004493 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004494 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004495 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004496 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004497 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004498 Record.push_back(Data.DeclaredDefaultConstructor);
4499 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004500 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004501 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004502 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004503 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004504 Record.push_back(Data.FailedImplicitMoveConstructor);
4505 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004506 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004507
4508 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004509 if (Data.NumBases > 0)
4510 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4511 Record);
4512
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004513 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4514 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004515 if (Data.NumVBases > 0)
4516 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4517 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004518
4519 AddUnresolvedSet(Data.Conversions, Record);
4520 AddUnresolvedSet(Data.VisibleConversions, Record);
4521 // Data.Definition is the owning decl, no need to write it.
4522 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004523
4524 // Add lambda-specific data.
4525 if (Data.IsLambda) {
4526 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004527 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004528 Record.push_back(Lambda.NumCaptures);
4529 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004530 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004531 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004532 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004533 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4534 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4535 AddSourceLocation(Capture.getLocation(), Record);
4536 Record.push_back(Capture.isImplicit());
4537 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4538 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4539 AddDeclRef(Var, Record);
4540 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4541 : SourceLocation(),
4542 Record);
4543 }
4544 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004545}
4546
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004547void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004548 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004549 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004550 assert(FirstDeclID == NextDeclID &&
4551 FirstTypeID == NextTypeID &&
4552 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004553 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004554 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004555 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004556 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004557
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004558 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004559
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004560 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4561 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4562 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004563 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004564 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004565 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004566 NextDeclID = FirstDeclID;
4567 NextTypeID = FirstTypeID;
4568 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004569 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004570 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004571 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004572}
4573
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004574void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004575 IdentifierIDs[II] = ID;
4576}
4577
Douglas Gregora8235d62012-10-09 23:05:51 +00004578void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4579 MacroIDs[MI] = ID;
4580}
4581
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004582void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004583 // Always take the highest-numbered type index. This copes with an interesting
4584 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004585 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004586 // keep the higher-numbered entry so that we can properly write it out to
4587 // the AST file.
4588 TypeIdx &StoredIdx = TypeIdxs[T];
4589 if (Idx.getIndex() >= StoredIdx.getIndex())
4590 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004591}
4592
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004593void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004594 SelectorIDs[S] = ID;
4595}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004596
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004597void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004598 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004599 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004600 MacroDefinitions[MD] = ID;
4601}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004602
Douglas Gregora015cab2011-12-02 17:30:13 +00004603void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4604 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4605 SubmoduleIDs[Mod] = ID;
4606}
4607
Douglas Gregora8235d62012-10-09 23:05:51 +00004608void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4609 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4610}
4611
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004612void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004613 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004614 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004615 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4616 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004617 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004618 // A forward reference was mutated into a definition. Rewrite it.
4619 // FIXME: This happens during template instantiation, should we
4620 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004621 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004622 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004623 }
4624}
Douglas Gregora8235d62012-10-09 23:05:51 +00004625
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004626void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004627 assert(!WritingAST && "Already writing the AST!");
4628
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004629 // TU and namespaces are handled elsewhere.
4630 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4631 return;
4632
Douglas Gregor919814d2011-09-09 23:01:35 +00004633 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004634 return; // Not a source decl added to a DeclContext from PCH.
4635
4636 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004637 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004638}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004639
4640void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004641 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004642 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004643 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004644 return; // Not a source member added to a class from PCH.
4645 if (!isa<CXXMethodDecl>(D))
4646 return; // We are interested in lazily declared implicit methods.
4647
4648 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004649 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004650 UpdateRecord &Record = DeclUpdates[RD];
4651 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004652 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004653}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004654
4655void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4656 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004657 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004658 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004659 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004660 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004661 return; // Not a source specialization added to a template from PCH.
4662
4663 UpdateRecord &Record = DeclUpdates[TD];
4664 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004665 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004666}
Douglas Gregor89d99802010-11-30 06:16:57 +00004667
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004668void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4669 const FunctionDecl *D) {
4670 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004671 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004672 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004673 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004674 return; // Not a source specialization added to a template from PCH.
4675
4676 UpdateRecord &Record = DeclUpdates[TD];
4677 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004678 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004679}
4680
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004681void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004682 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004683 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004684 return; // Declaration not imported from PCH.
4685
4686 // Implicit decl from a PCH was defined.
4687 // FIXME: Should implicit definition be a separate FunctionDecl?
4688 RewriteDecl(D);
4689}
4690
Sebastian Redlf79a7192011-04-29 08:19:30 +00004691void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004692 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004693 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004694 return;
4695
4696 // Since the actual instantiation is delayed, this really means that we need
4697 // to update the instantiation location.
4698 UpdateRecord &Record = DeclUpdates[D];
4699 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4700 AddSourceLocation(
4701 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4702}
4703
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004704void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4705 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004706 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004707 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004708 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004709
4710 assert(IFD->getDefinition() && "Category on a class without a definition?");
4711 ObjCClassesWithCategories.insert(
4712 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004713}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004714
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004715
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004716void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4717 const ObjCPropertyDecl *OrigProp,
4718 const ObjCCategoryDecl *ClassExt) {
4719 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4720 if (!D)
4721 return;
4722
4723 assert(!WritingAST && "Already writing the AST!");
4724 if (!D->isFromASTFile())
4725 return; // Declaration not imported from PCH.
4726
4727 RewriteDecl(D);
4728}