blob: befb74dbf3d2813df920c76b80fbccc64d99e5e6 [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);
Douglas Gregora930dc92012-10-22 18:42:04 +0000778 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000779 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000780 RECORD(FILE_SYSTEM_OPTIONS);
781
Douglas Gregorc337fef2012-10-19 00:45:00 +0000782 BLOCK(INPUT_FILES_BLOCK);
783 RECORD(INPUT_FILE);
784
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000785 // AST Top-Level Block.
786 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000787 RECORD(TYPE_OFFSET);
788 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000789 RECORD(IDENTIFIER_OFFSET);
790 RECORD(IDENTIFIER_TABLE);
791 RECORD(EXTERNAL_DEFINITIONS);
792 RECORD(SPECIAL_TYPES);
793 RECORD(STATISTICS);
794 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000795 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000796 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
797 RECORD(SELECTOR_OFFSETS);
798 RECORD(METHOD_POOL);
799 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000800 RECORD(SOURCE_LOCATION_OFFSETS);
801 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000802 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000803 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000804 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000805 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000806 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000807 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000808 RECORD(SEMA_DECL_REFS);
809 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
810 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
811 RECORD(DECL_REPLACEMENTS);
812 RECORD(UPDATE_VISIBLE);
813 RECORD(DECL_UPDATE_OFFSETS);
814 RECORD(DECL_UPDATES);
815 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
816 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000817 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000818 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000819 RECORD(FP_PRAGMA_OPTIONS);
820 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000821 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000822 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000823 RECORD(MODULE_OFFSET_MAP);
824 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000825 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000826 RECORD(FILE_SORTED_DECLS);
827 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000828 RECORD(MERGED_DECLARATIONS);
829 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000830 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000831 RECORD(MACRO_OFFSET);
832 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000833
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000834 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000835 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000836 RECORD(SM_SLOC_FILE_ENTRY);
837 RECORD(SM_SLOC_BUFFER_ENTRY);
838 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000839 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000841 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000842 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000843 RECORD(PP_MACRO_OBJECT_LIKE);
844 RECORD(PP_MACRO_FUNCTION_LIKE);
845 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000846
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000847 // Decls and Types block.
848 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000849 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000850 RECORD(TYPE_COMPLEX);
851 RECORD(TYPE_POINTER);
852 RECORD(TYPE_BLOCK_POINTER);
853 RECORD(TYPE_LVALUE_REFERENCE);
854 RECORD(TYPE_RVALUE_REFERENCE);
855 RECORD(TYPE_MEMBER_POINTER);
856 RECORD(TYPE_CONSTANT_ARRAY);
857 RECORD(TYPE_INCOMPLETE_ARRAY);
858 RECORD(TYPE_VARIABLE_ARRAY);
859 RECORD(TYPE_VECTOR);
860 RECORD(TYPE_EXT_VECTOR);
861 RECORD(TYPE_FUNCTION_PROTO);
862 RECORD(TYPE_FUNCTION_NO_PROTO);
863 RECORD(TYPE_TYPEDEF);
864 RECORD(TYPE_TYPEOF_EXPR);
865 RECORD(TYPE_TYPEOF);
866 RECORD(TYPE_RECORD);
867 RECORD(TYPE_ENUM);
868 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000869 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000870 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000871 RECORD(TYPE_DECLTYPE);
872 RECORD(TYPE_ELABORATED);
873 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
874 RECORD(TYPE_UNRESOLVED_USING);
875 RECORD(TYPE_INJECTED_CLASS_NAME);
876 RECORD(TYPE_OBJC_OBJECT);
877 RECORD(TYPE_TEMPLATE_TYPE_PARM);
878 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
879 RECORD(TYPE_DEPENDENT_NAME);
880 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
881 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
882 RECORD(TYPE_PAREN);
883 RECORD(TYPE_PACK_EXPANSION);
884 RECORD(TYPE_ATTRIBUTED);
885 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000886 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000887 RECORD(DECL_TYPEDEF);
888 RECORD(DECL_ENUM);
889 RECORD(DECL_RECORD);
890 RECORD(DECL_ENUM_CONSTANT);
891 RECORD(DECL_FUNCTION);
892 RECORD(DECL_OBJC_METHOD);
893 RECORD(DECL_OBJC_INTERFACE);
894 RECORD(DECL_OBJC_PROTOCOL);
895 RECORD(DECL_OBJC_IVAR);
896 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000897 RECORD(DECL_OBJC_CATEGORY);
898 RECORD(DECL_OBJC_CATEGORY_IMPL);
899 RECORD(DECL_OBJC_IMPLEMENTATION);
900 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
901 RECORD(DECL_OBJC_PROPERTY);
902 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000903 RECORD(DECL_FIELD);
904 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000905 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000906 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000907 RECORD(DECL_FILE_SCOPE_ASM);
908 RECORD(DECL_BLOCK);
909 RECORD(DECL_CONTEXT_LEXICAL);
910 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000911 RECORD(DECL_NAMESPACE);
912 RECORD(DECL_NAMESPACE_ALIAS);
913 RECORD(DECL_USING);
914 RECORD(DECL_USING_SHADOW);
915 RECORD(DECL_USING_DIRECTIVE);
916 RECORD(DECL_UNRESOLVED_USING_VALUE);
917 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
918 RECORD(DECL_LINKAGE_SPEC);
919 RECORD(DECL_CXX_RECORD);
920 RECORD(DECL_CXX_METHOD);
921 RECORD(DECL_CXX_CONSTRUCTOR);
922 RECORD(DECL_CXX_DESTRUCTOR);
923 RECORD(DECL_CXX_CONVERSION);
924 RECORD(DECL_ACCESS_SPEC);
925 RECORD(DECL_FRIEND);
926 RECORD(DECL_FRIEND_TEMPLATE);
927 RECORD(DECL_CLASS_TEMPLATE);
928 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
929 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
930 RECORD(DECL_FUNCTION_TEMPLATE);
931 RECORD(DECL_TEMPLATE_TYPE_PARM);
932 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
933 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
934 RECORD(DECL_STATIC_ASSERT);
935 RECORD(DECL_CXX_BASE_SPECIFIERS);
936 RECORD(DECL_INDIRECTFIELD);
937 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
938
Douglas Gregora72d8c42011-06-03 02:27:19 +0000939 // Statements and Exprs can occur in the Decls and Types block.
940 AddStmtsExprs(Stream, Record);
941
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000942 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000943 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000944 RECORD(PPD_MACRO_DEFINITION);
945 RECORD(PPD_INCLUSION_DIRECTIVE);
946
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000947#undef RECORD
948#undef BLOCK
949 Stream.ExitBlock();
950}
951
Douglas Gregore650c8c2009-07-07 00:12:59 +0000952/// \brief Adjusts the given filename to only write out the portion of the
953/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000954///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955/// \param Filename the file name to adjust.
956///
957/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
958/// the returned filename will be adjusted by this system root.
959///
960/// \returns either the original filename (if it needs no adjustment) or the
961/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000962static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000963adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Douglas Gregor832d6202011-07-22 16:35:34 +0000966 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 // Verify that the filename and the system root have the same prefix.
970 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000971 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 if (Filename[Pos] != isysroot[Pos])
973 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 // We hit the end of the filename before we hit the end of the system root.
976 if (!Filename[Pos])
977 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 // If the file name has a '/' at the current position, skip over the '/'.
980 // We distinguish sysroot-based includes from absolute includes by the
981 // absence of '/' at the beginning of sysroot-based includes.
982 if (Filename[Pos] == '/')
983 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 return Filename + Pos;
986}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000987
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000988/// \brief Write the control block.
989void ASTWriter::WriteControlBlock(ASTContext &Context, StringRef isysroot,
990 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000991 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000992 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
993 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000994
Douglas Gregore650c8c2009-07-07 00:12:59 +0000995 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000996 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
997 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
998 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
999 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1000 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1001 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1002 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1003 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1005 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1006 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001007 Record.push_back(VERSION_MAJOR);
1008 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001009 Record.push_back(CLANG_VERSION_MAJOR);
1010 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001011 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001012 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001013 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1014 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001015
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001016 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001017 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001018 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1019 llvm::SmallVector<char, 128> ModulePaths;
1020 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001021
1022 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1023 M != MEnd; ++M) {
1024 // Skip modules that weren't directly imported.
1025 if (!(*M)->isDirectlyImported())
1026 continue;
1027
1028 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1029 // FIXME: Write import location, once it matters.
1030 // FIXME: This writes the absolute path for AST files we depend on.
1031 const std::string &FileName = (*M)->FileName;
1032 Record.push_back(FileName.size());
1033 Record.append(FileName.begin(), FileName.end());
1034 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001035 Stream.EmitRecord(IMPORTS, Record);
1036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001038 // Language options.
1039 Record.clear();
1040 const LangOptions &LangOpts = Context.getLangOpts();
1041#define LANGOPT(Name, Bits, Default, Description) \
1042 Record.push_back(LangOpts.Name);
1043#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1044 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1045#include "clang/Basic/LangOptions.def"
1046
1047 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1048 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1049
1050 Record.push_back(LangOpts.CurrentModule.size());
1051 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1052 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1053
Douglas Gregoree097c12012-10-18 17:58:09 +00001054 // Target options.
1055 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001056 const TargetInfo &Target = Context.getTargetInfo();
1057 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001058 AddString(TargetOpts.Triple, Record);
1059 AddString(TargetOpts.CPU, Record);
1060 AddString(TargetOpts.ABI, Record);
1061 AddString(TargetOpts.CXXABI, Record);
1062 AddString(TargetOpts.LinkerVersion, Record);
1063 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1064 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1065 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1066 }
1067 Record.push_back(TargetOpts.Features.size());
1068 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1069 AddString(TargetOpts.Features[I], Record);
1070 }
1071 Stream.EmitRecord(TARGET_OPTIONS, Record);
1072
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001073 // Diagnostic options.
1074 Record.clear();
1075 const DiagnosticOptions &DiagOpts
1076 = Context.getDiagnostics().getDiagnosticOptions();
1077#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1078#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1079 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1080#include "clang/Basic/DiagnosticOptions.def"
1081 Record.push_back(DiagOpts.Warnings.size());
1082 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1083 AddString(DiagOpts.Warnings[I], Record);
1084 // Note: we don't serialize the log or serialization file names, because they
1085 // are generally transient files and will almost always be overridden.
1086 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1087
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001088 // File system options.
1089 Record.clear();
1090 const FileSystemOptions &FSOpts
1091 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1092 AddString(FSOpts.WorkingDir, Record);
1093 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1094
Douglas Gregor31d375f2011-05-06 21:43:30 +00001095 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001096 SourceManager &SM = Context.getSourceManager();
1097 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1098 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001099 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1100 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001101 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1102 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1103
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001104 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001106 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001107
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001108 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001109 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001110 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001111 RecordData Record;
Douglas Gregor39c497b2012-10-18 18:36:53 +00001112 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001113 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001114 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
1115 Record.clear();
Douglas Gregorb64c1932009-05-12 01:31:05 +00001116 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001117
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001118 // Original PCH directory
1119 if (!OutputFile.empty() && OutputFile != "-") {
1120 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1121 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1123 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1124
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001125 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001126
1127 llvm::sys::fs::make_absolute(OutputPath);
1128 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1129
1130 RecordData Record;
1131 Record.push_back(ORIGINAL_PCH_DIR);
1132 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1133 }
1134
Douglas Gregor745e6f12012-10-19 00:38:02 +00001135 WriteInputFiles(Context.SourceMgr, isysroot);
1136 Stream.ExitBlock();
1137}
1138
1139void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1140 using namespace llvm;
1141 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1142 RecordData Record;
1143
1144 // Create input-file abbreviation.
1145 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1146 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001147 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001148 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1149 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001150 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001151 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1152 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1153
1154 // Write out all of the input files.
1155 std::vector<uint32_t> InputFileOffsets;
1156 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1157 // Get this source location entry.
1158 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001159 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001160
1161 // We only care about file entries that were not overridden.
1162 if (!SLoc->isFile())
1163 continue;
1164 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001165 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001166 continue;
1167
Douglas Gregora930dc92012-10-22 18:42:04 +00001168 // Record this entry's offset.
1169 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1170 InputFileIDs[Cache->OrigEntry] = InputFileOffsets.size();
1171
Douglas Gregor745e6f12012-10-19 00:38:02 +00001172 Record.clear();
1173 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001174 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001175
1176 // Emit size/modification time for this file.
1177 Record.push_back(Cache->OrigEntry->getSize());
1178 Record.push_back(Cache->OrigEntry->getModificationTime());
1179
Douglas Gregora930dc92012-10-22 18:42:04 +00001180 // Whether this file was overridden.
1181 Record.push_back(Cache->BufferOverridden);
1182
Douglas Gregor745e6f12012-10-19 00:38:02 +00001183 // Turn the file name into an absolute path, if it isn't already.
1184 const char *Filename = Cache->OrigEntry->getName();
1185 SmallString<128> FilePath(Filename);
1186
1187 // Ask the file manager to fixup the relative path for us. This will
1188 // honor the working directory.
1189 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1190
1191 // FIXME: This call to make_absolute shouldn't be necessary, the
1192 // call to FixupRelativePath should always return an absolute path.
1193 llvm::sys::fs::make_absolute(FilePath);
1194 Filename = FilePath.c_str();
1195
1196 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1197
1198 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1199 }
1200
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001201 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001202
1203 // Create input file offsets abbreviation.
1204 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1205 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1206 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1207 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1208 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1209
1210 // Write input file offsets.
1211 Record.clear();
1212 Record.push_back(INPUT_FILE_OFFSETS);
1213 Record.push_back(InputFileOffsets.size());
1214 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001215}
1216
Douglas Gregor14f79002009-04-10 03:52:48 +00001217//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001218// stat cache Serialization
1219//===----------------------------------------------------------------------===//
1220
1221namespace {
1222// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001223class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001224public:
1225 typedef const char * key_type;
1226 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Chris Lattner74e976b2010-11-23 19:28:12 +00001228 typedef struct stat data_type;
1229 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001230
1231 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001232 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234
1235 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001236 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001237 data_type_ref Data) {
1238 unsigned StrLen = strlen(path);
1239 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001240 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001241 clang::io::Emit8(Out, DataLen);
1242 return std::make_pair(StrLen + 1, DataLen);
1243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Chris Lattner5f9e2722011-07-23 10:55:15 +00001245 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001246 Out.write(path, KeyLen);
1247 }
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Chris Lattner5f9e2722011-07-23 10:55:15 +00001249 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001250 data_type_ref Data, unsigned DataLen) {
1251 using namespace clang::io;
1252 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Chris Lattner74e976b2010-11-23 19:28:12 +00001254 Emit32(Out, (uint32_t) Data.st_ino);
1255 Emit32(Out, (uint32_t) Data.st_dev);
1256 Emit16(Out, (uint16_t) Data.st_mode);
1257 Emit64(Out, (uint64_t) Data.st_mtime);
1258 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001259
1260 assert(Out.tell() - Start == DataLen && "Wrong data length");
1261 }
1262};
1263} // end anonymous namespace
1264
Sebastian Redl3397c552010-08-18 23:56:27 +00001265/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001266void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001267 // Build the on-disk hash table containing information about every
1268 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001269 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001270 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001271 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001272 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001273 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001274 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001275 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001276 }
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001278 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001279 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001280 uint32_t BucketOffset;
1281 {
1282 llvm::raw_svector_ostream Out(StatCacheData);
1283 // Make sure that no bucket is at offset 0
1284 clang::io::Emit32(Out, 0);
1285 BucketOffset = Generator.Emit(Out);
1286 }
1287
1288 // Create a blob abbreviation
1289 using namespace llvm;
1290 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001291 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1295 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1296
1297 // Write the stat cache
1298 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001299 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001300 Record.push_back(BucketOffset);
1301 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001302 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001303}
1304
1305//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001306// Source Manager Serialization
1307//===----------------------------------------------------------------------===//
1308
1309/// \brief Create an abbreviation for the SLocEntry that refers to a
1310/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001311static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001312 using namespace llvm;
1313 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001314 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001319 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001321 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001324 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001325}
1326
1327/// \brief Create an abbreviation for the SLocEntry that refers to a
1328/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001329static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001330 using namespace llvm;
1331 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001332 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1335 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001338 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001339}
1340
1341/// \brief Create an abbreviation for the SLocEntry that refers to a
1342/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001343static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001344 using namespace llvm;
1345 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001346 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001348 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001349}
1350
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001351/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1352/// expansion.
1353static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001354 using namespace llvm;
1355 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001356 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001361 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001362 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001363}
1364
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001365namespace {
1366 // Trait used for the on-disk hash table of header search information.
1367 class HeaderFileInfoTrait {
1368 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001369
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001370 // Keep track of the framework names we've used during serialization.
1371 SmallVector<char, 128> FrameworkStringData;
1372 llvm::StringMap<unsigned> FrameworkNameOffset;
1373
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001374 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001375 HeaderFileInfoTrait(ASTWriter &Writer)
1376 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001377
1378 typedef const char *key_type;
1379 typedef key_type key_type_ref;
1380
1381 typedef HeaderFileInfo data_type;
1382 typedef const data_type &data_type_ref;
1383
1384 static unsigned ComputeHash(const char *path) {
1385 // The hash is based only on the filename portion of the key, so that the
1386 // reader can match based on filenames when symlinking or excess path
1387 // elements ("foo/../", "../") change the form of the name. However,
1388 // complete path is still the key.
1389 return llvm::HashString(llvm::sys::path::filename(path));
1390 }
1391
1392 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001393 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001394 data_type_ref Data) {
1395 unsigned StrLen = strlen(path);
1396 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001397 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001398 clang::io::Emit8(Out, DataLen);
1399 return std::make_pair(StrLen + 1, DataLen);
1400 }
1401
Chris Lattner5f9e2722011-07-23 10:55:15 +00001402 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001403 Out.write(path, KeyLen);
1404 }
1405
Chris Lattner5f9e2722011-07-23 10:55:15 +00001406 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001407 data_type_ref Data, unsigned DataLen) {
1408 using namespace clang::io;
1409 uint64_t Start = Out.tell(); (void)Start;
1410
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001411 unsigned char Flags = (Data.isImport << 5)
1412 | (Data.isPragmaOnce << 4)
1413 | (Data.DirInfo << 2)
1414 | (Data.Resolved << 1)
1415 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001416 Emit8(Out, (uint8_t)Flags);
1417 Emit16(Out, (uint16_t) Data.NumIncludes);
1418
1419 if (!Data.ControllingMacro)
1420 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1421 else
1422 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001423
1424 unsigned Offset = 0;
1425 if (!Data.Framework.empty()) {
1426 // If this header refers into a framework, save the framework name.
1427 llvm::StringMap<unsigned>::iterator Pos
1428 = FrameworkNameOffset.find(Data.Framework);
1429 if (Pos == FrameworkNameOffset.end()) {
1430 Offset = FrameworkStringData.size() + 1;
1431 FrameworkStringData.append(Data.Framework.begin(),
1432 Data.Framework.end());
1433 FrameworkStringData.push_back(0);
1434
1435 FrameworkNameOffset[Data.Framework] = Offset;
1436 } else
1437 Offset = Pos->second;
1438 }
1439 Emit32(Out, Offset);
1440
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001441 assert(Out.tell() - Start == DataLen && "Wrong data length");
1442 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001443
1444 const char *strings_begin() const { return FrameworkStringData.begin(); }
1445 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001446 };
1447} // end anonymous namespace
1448
1449/// \brief Write the header search block for the list of files that
1450///
1451/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001452void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001453 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001454 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1455
1456 if (FilesByUID.size() > HS.header_file_size())
1457 FilesByUID.resize(HS.header_file_size());
1458
Benjamin Kramerfacde172012-06-06 17:32:50 +00001459 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001460 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001461 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001462 unsigned NumHeaderSearchEntries = 0;
1463 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1464 const FileEntry *File = FilesByUID[UID];
1465 if (!File)
1466 continue;
1467
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001468 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1469 // from the external source if it was not provided already.
1470 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001471 if (HFI.External && Chain)
1472 continue;
1473
1474 // Turn the file name into an absolute path, if it isn't already.
1475 const char *Filename = File->getName();
1476 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1477
1478 // If we performed any translation on the file name at all, we need to
1479 // save this string, since the generator will refer to it later.
1480 if (Filename != File->getName()) {
1481 Filename = strdup(Filename);
1482 SavedStrings.push_back(Filename);
1483 }
1484
1485 Generator.insert(Filename, HFI, GeneratorTrait);
1486 ++NumHeaderSearchEntries;
1487 }
1488
1489 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001490 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001491 uint32_t BucketOffset;
1492 {
1493 llvm::raw_svector_ostream Out(TableData);
1494 // Make sure that no bucket is at offset 0
1495 clang::io::Emit32(Out, 0);
1496 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1497 }
1498
1499 // Create a blob abbreviation
1500 using namespace llvm;
1501 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1502 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001505 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001506 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1507 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1508
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001509 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001510 RecordData Record;
1511 Record.push_back(HEADER_SEARCH_TABLE);
1512 Record.push_back(BucketOffset);
1513 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001514 Record.push_back(TableData.size());
1515 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001516 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1517
1518 // Free all of the strings we had to duplicate.
1519 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1520 free((void*)SavedStrings[I]);
1521}
1522
Douglas Gregor14f79002009-04-10 03:52:48 +00001523/// \brief Writes the block containing the serialized form of the
1524/// source manager.
1525///
1526/// TODO: We should probably use an on-disk hash table (stored in a
1527/// blob), indexed based on the file name, so that we only create
1528/// entries for files that we actually need. In the common case (no
1529/// errors), we probably won't have to create file entries for any of
1530/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001531void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001532 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001533 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001534 RecordData Record;
1535
Chris Lattnerf04ad692009-04-10 17:16:57 +00001536 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001537 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001538
1539 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001540 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1541 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1542 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001543 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001544
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001545 // Write out the source location entry table. We skip the first
1546 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001547 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001548 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001549 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1550 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001551 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001552 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001553 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001554 FileID FID = FileID::get(I);
1555 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001556
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001557 // Record the offset of this source-location entry.
1558 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1559
1560 // Figure out which record code to use.
1561 unsigned Code;
1562 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001563 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1564 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001565 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001566 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001567 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001568 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001569 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001570 Record.clear();
1571 Record.push_back(Code);
1572
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001573 // Starting offset of this entry within this module, so skip the dummy.
1574 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001575 if (SLoc->isFile()) {
1576 const SrcMgr::FileInfo &File = SLoc->getFile();
1577 Record.push_back(File.getIncludeLoc().getRawEncoding());
1578 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1579 Record.push_back(File.hasLineDirectives());
1580
1581 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001582 if (Content->OrigEntry) {
1583 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001584 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001585
Douglas Gregora930dc92012-10-22 18:42:04 +00001586 // The source location entry is a file. Emit input file ID.
1587 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1588 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001590 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001591
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001592 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001593 if (FDI != FileDeclIDs.end()) {
1594 Record.push_back(FDI->second->FirstDeclIndex);
1595 Record.push_back(FDI->second->DeclIDs.size());
1596 } else {
1597 Record.push_back(0);
1598 Record.push_back(0);
1599 }
Douglas Gregora081da52011-11-16 20:05:18 +00001600
Douglas Gregora930dc92012-10-22 18:42:04 +00001601 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001602
1603 if (Content->BufferOverridden) {
1604 Record.clear();
1605 Record.push_back(SM_SLOC_BUFFER_BLOB);
1606 const llvm::MemoryBuffer *Buffer
1607 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1608 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1609 StringRef(Buffer->getBufferStart(),
1610 Buffer->getBufferSize() + 1));
1611 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001612 } else {
1613 // The source location entry is a buffer. The blob associated
1614 // with this entry contains the contents of the buffer.
1615
1616 // We add one to the size so that we capture the trailing NULL
1617 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1618 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001619 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001620 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001621 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001622 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001623 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001624 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001625 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001626 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001627 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001628 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001629
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001630 if (strcmp(Name, "<built-in>") == 0) {
1631 PreloadSLocs.push_back(SLocEntryOffsets.size());
1632 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001633 }
1634 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001635 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001636 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001637 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1638 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001639 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1640 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001641
1642 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001643 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001644 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001645 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001646 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001647 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001648 }
1649 }
1650
Douglas Gregorc9490c02009-04-16 22:23:12 +00001651 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001652
1653 if (SLocEntryOffsets.empty())
1654 return;
1655
Sebastian Redl3397c552010-08-18 23:56:27 +00001656 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001657 // table is used for lazily loading source-location information.
1658 using namespace llvm;
1659 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001660 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001661 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001662 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001663 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1664 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001666 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001667 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001668 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001669 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001670 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001671
Sebastian Redl3397c552010-08-18 23:56:27 +00001672 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001673 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001674 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001675
1676 // Write the line table. It depends on remapping working, so it must come
1677 // after the source location offsets.
1678 if (SourceMgr.hasLineTable()) {
1679 LineTableInfo &LineTable = SourceMgr.getLineTable();
1680
1681 Record.clear();
1682 // Emit the file names
1683 Record.push_back(LineTable.getNumFilenames());
1684 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1685 // Emit the file name
1686 const char *Filename = LineTable.getFilename(I);
1687 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1688 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1689 Record.push_back(FilenameLen);
1690 if (FilenameLen)
1691 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1692 }
1693
1694 // Emit the line entries
1695 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1696 L != LEnd; ++L) {
1697 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001698 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001699 continue;
1700
1701 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001702 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001703
1704 // Emit the line entries
1705 Record.push_back(L->second.size());
1706 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1707 LEEnd = L->second.end();
1708 LE != LEEnd; ++LE) {
1709 Record.push_back(LE->FileOffset);
1710 Record.push_back(LE->LineNo);
1711 Record.push_back(LE->FilenameID);
1712 Record.push_back((unsigned)LE->FileKind);
1713 Record.push_back(LE->IncludeOffset);
1714 }
1715 }
1716 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1717 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001718}
1719
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001720//===----------------------------------------------------------------------===//
1721// Preprocessor Serialization
1722//===----------------------------------------------------------------------===//
1723
Douglas Gregor9c736102011-02-10 18:20:09 +00001724static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1725 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1726 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1727 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1728 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1729 return X.first->getName().compare(Y.first->getName());
1730}
1731
Chris Lattner0b1fb982009-04-10 17:15:23 +00001732/// \brief Writes the block containing the serialized form of the
1733/// preprocessor.
1734///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001735void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001736 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1737 if (PPRec)
1738 WritePreprocessorDetail(*PPRec);
1739
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001740 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001741
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001742 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1743 if (PP.getCounterValue() != 0) {
1744 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001745 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001746 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001747 }
1748
1749 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001750 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Sebastian Redl3397c552010-08-18 23:56:27 +00001752 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001753 // FIXME: use diagnostics subsystem for localization etc.
1754 if (PP.SawDateOrTime())
1755 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Douglas Gregorecdcb882010-10-20 22:00:55 +00001757
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001758 // Loop over all the macro definitions that are live at the end of the file,
1759 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001760
Douglas Gregor9c736102011-02-10 18:20:09 +00001761 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001762 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001763 MacrosToEmit;
1764 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001765 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001766 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001767 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001768 if (!IsModule || I->second->isPublic()) {
1769 MacroDefinitionsSeen.insert(I->first);
1770 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001771 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001772 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001773
Douglas Gregor9c736102011-02-10 18:20:09 +00001774 // Sort the set of macro definitions that need to be serialized by the
1775 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001776 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001777 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001778
Douglas Gregora8235d62012-10-09 23:05:51 +00001779 /// \brief Offsets of each of the macros into the bitstream, indexed by
1780 /// the local macro ID
1781 ///
1782 /// For each identifier that is associated with a macro, this map
1783 /// provides the offset into the bitstream where that macro is
1784 /// defined.
1785 std::vector<uint32_t> MacroOffsets;
1786
Douglas Gregor9c736102011-02-10 18:20:09 +00001787 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1788 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001789
Douglas Gregora8235d62012-10-09 23:05:51 +00001790 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1791 MI = MI->getPreviousDefinition()) {
1792 MacroID ID = getMacroRef(MI);
1793 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001794 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Douglas Gregora8235d62012-10-09 23:05:51 +00001796 // Skip macros from a AST file if we're chaining.
1797 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1798 continue;
1799
1800 if (ID < FirstMacroID) {
1801 // This will have been dealt with via an update record.
1802 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1803 continue;
1804 }
1805
1806 // Record the local offset of this macro.
1807 unsigned Index = ID - FirstMacroID;
1808 if (Index == MacroOffsets.size())
1809 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1810 else {
1811 if (Index > MacroOffsets.size())
1812 MacroOffsets.resize(Index + 1);
1813
1814 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1815 }
1816
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001817 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001818 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001819 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001820 AddSourceLocation(MI->getDefinitionLoc(), Record);
1821 AddSourceLocation(MI->getUndefLoc(), Record);
1822 Record.push_back(MI->isUsed());
1823 Record.push_back(MI->isPublic());
1824 AddSourceLocation(MI->getVisibilityLocation(), Record);
1825 unsigned Code;
1826 if (MI->isObjectLike()) {
1827 Code = PP_MACRO_OBJECT_LIKE;
1828 } else {
1829 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001830
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001831 Record.push_back(MI->isC99Varargs());
1832 Record.push_back(MI->isGNUVarargs());
1833 Record.push_back(MI->getNumArgs());
1834 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1835 I != E; ++I)
1836 AddIdentifierRef(*I, Record);
1837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001839 // If we have a detailed preprocessing record, record the macro definition
1840 // ID that corresponds to this macro.
1841 if (PPRec)
1842 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1843
1844 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001845 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001846
1847 // Emit the tokens array.
1848 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1849 // Note that we know that the preprocessor does not have any annotation
1850 // tokens in it because they are created by the parser, and thus can't
1851 // be in a macro definition.
1852 const Token &Tok = MI->getReplacementToken(TokNo);
1853
1854 Record.push_back(Tok.getLocation().getRawEncoding());
1855 Record.push_back(Tok.getLength());
1856
1857 // FIXME: When reading literal tokens, reconstruct the literal pointer
1858 // if it is needed.
1859 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1860 // FIXME: Should translate token kind to a stable encoding.
1861 Record.push_back(Tok.getKind());
1862 // FIXME: Should translate token flags to a stable encoding.
1863 Record.push_back(Tok.getFlags());
1864
1865 Stream.EmitRecord(PP_TOKEN, Record);
1866 Record.clear();
1867 }
1868 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001869 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001870 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001871 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001872
1873 // Write the offsets table for macro IDs.
1874 using namespace llvm;
1875 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1876 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1880
1881 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1882 Record.clear();
1883 Record.push_back(MACRO_OFFSET);
1884 Record.push_back(MacroOffsets.size());
1885 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1886 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1887 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001888}
1889
1890void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001891 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001892 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001893
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001894 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001895
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001896 // Enter the preprocessor block.
1897 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001898
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001899 // If the preprocessor has a preprocessing record, emit it.
1900 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001901 using namespace llvm;
1902
1903 // Set up the abbreviation for
1904 unsigned InclusionAbbrev = 0;
1905 {
1906 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1907 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1913 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1914 }
1915
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001916 unsigned FirstPreprocessorEntityID
1917 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1918 + NUM_PREDEF_PP_ENTITY_IDS;
1919 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001920 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001921 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1922 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001923 E != EEnd;
1924 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001925 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001926
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001927 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1928 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001929
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001930 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001931 // Record this macro definition's ID.
1932 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001933
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001934 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001935 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1936 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001937 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001938
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001939 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001940 Record.push_back(ME->isBuiltinMacro());
1941 if (ME->isBuiltinMacro())
1942 AddIdentifierRef(ME->getName(), Record);
1943 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001944 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001945 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001946 continue;
1947 }
1948
1949 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1950 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001951 Record.push_back(ID->getFileName().size());
1952 Record.push_back(ID->wasInQuotes());
1953 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001954 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001955 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001956 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001957 // Check that the FileEntry is not null because it was not resolved and
1958 // we create a PCH even with compiler errors.
1959 if (ID->getFile())
1960 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001961 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1962 continue;
1963 }
1964
1965 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1966 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001967 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001968
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001969 // Write the offsets table for the preprocessing record.
1970 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001971 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1972
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001973 // Write the offsets table for identifier IDs.
1974 using namespace llvm;
1975 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001976 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001979 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001980
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001981 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001982 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001983 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001984 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1985 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001986 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001987}
1988
Douglas Gregore209e502011-12-06 01:10:29 +00001989unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1990 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1991 if (Known != SubmoduleIDs.end())
1992 return Known->second;
1993
1994 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1995}
1996
Douglas Gregor26ced122011-12-01 00:59:36 +00001997/// \brief Compute the number of modules within the given tree (including the
1998/// given module).
1999static unsigned getNumberOfModules(Module *Mod) {
2000 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002001 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2002 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002003 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002004 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002005
2006 return ChildModules + 1;
2007}
2008
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002009void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002010 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002011 // FIXME: This feels like it belongs somewhere else, but there are no
2012 // other consumers of this information.
2013 SourceManager &SrcMgr = PP->getSourceManager();
2014 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2015 for (ASTContext::import_iterator I = Context->local_import_begin(),
2016 IEnd = Context->local_import_end();
2017 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002018 if (Module *ImportedFrom
2019 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2020 SrcMgr))) {
2021 ImportedFrom->Imports.push_back(I->getImportedModule());
2022 }
2023 }
2024
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002025 // Enter the submodule description block.
2026 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2027
2028 // Write the abbreviations needed for the submodules block.
2029 using namespace llvm;
2030 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2031 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2041 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2042
2043 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002044 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2046 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2047
2048 Abbrev = new BitCodeAbbrev();
2049 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2051 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002052
2053 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002054 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2056 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2057
2058 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002059 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2061 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2062
Douglas Gregor51f564f2011-12-31 04:05:44 +00002063 Abbrev = new BitCodeAbbrev();
2064 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2066 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2067
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002068 Abbrev = new BitCodeAbbrev();
2069 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2071 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2072
Douglas Gregor26ced122011-12-01 00:59:36 +00002073 // Write the submodule metadata block.
2074 RecordData Record;
2075 Record.push_back(getNumberOfModules(WritingModule));
2076 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2077 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2078
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002079 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002080 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002081 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002082 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002083 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002084 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002085 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002086
2087 // Emit the definition of the block.
2088 Record.clear();
2089 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002090 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002091 if (Mod->Parent) {
2092 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2093 Record.push_back(SubmoduleIDs[Mod->Parent]);
2094 } else {
2095 Record.push_back(0);
2096 }
2097 Record.push_back(Mod->IsFramework);
2098 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002099 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002100 Record.push_back(Mod->InferSubmodules);
2101 Record.push_back(Mod->InferExplicitSubmodules);
2102 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002103 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2104
Douglas Gregor51f564f2011-12-31 04:05:44 +00002105 // Emit the requirements.
2106 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2107 Record.clear();
2108 Record.push_back(SUBMODULE_REQUIRES);
2109 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2110 Mod->Requires[I].data(),
2111 Mod->Requires[I].size());
2112 }
2113
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002114 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002115 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002116 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002117 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002118 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002119 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002120 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2121 Record.clear();
2122 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2123 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2124 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002125 }
2126
2127 // Emit the headers.
2128 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2129 Record.clear();
2130 Record.push_back(SUBMODULE_HEADER);
2131 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2132 Mod->Headers[I]->getName());
2133 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002134 // Emit the excluded headers.
2135 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2136 Record.clear();
2137 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2138 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2139 Mod->ExcludedHeaders[I]->getName());
2140 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002141 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2142 Record.clear();
2143 Record.push_back(SUBMODULE_TOPHEADER);
2144 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2145 Mod->TopHeaders[I]->getName());
2146 }
Douglas Gregor55988682011-12-05 16:33:54 +00002147
2148 // Emit the imports.
2149 if (!Mod->Imports.empty()) {
2150 Record.clear();
2151 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002152 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002153 assert(ImportedID && "Unknown submodule!");
2154 Record.push_back(ImportedID);
2155 }
2156 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2157 }
2158
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002159 // Emit the exports.
2160 if (!Mod->Exports.empty()) {
2161 Record.clear();
2162 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002163 if (Module *Exported = Mod->Exports[I].getPointer()) {
2164 unsigned ExportedID = SubmoduleIDs[Exported];
2165 assert(ExportedID > 0 && "Unknown submodule ID?");
2166 Record.push_back(ExportedID);
2167 } else {
2168 Record.push_back(0);
2169 }
2170
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002171 Record.push_back(Mod->Exports[I].getInt());
2172 }
2173 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2174 }
2175
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002176 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002177 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2178 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002179 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002180 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002181 }
2182
2183 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002184
2185 assert((NextSubmoduleID - FirstSubmoduleID
2186 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002187}
2188
Douglas Gregor185dbd72011-12-01 02:07:58 +00002189serialization::SubmoduleID
2190ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002191 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002192 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002193
2194 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002195 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002196 Module *OwningMod
2197 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002198 if (!OwningMod)
2199 return 0;
2200
Douglas Gregore209e502011-12-06 01:10:29 +00002201 // Check whether this submodule is part of our own module.
2202 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002203 return 0;
2204
Douglas Gregore209e502011-12-06 01:10:29 +00002205 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002206}
2207
David Blaikied6471f72011-09-25 23:23:43 +00002208void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002209 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002210 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002211 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2212 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002213 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002214 if (point.Loc.isInvalid())
2215 continue;
2216
2217 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002218 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002219 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002220 if (I->second.isPragma()) {
2221 Record.push_back(I->first);
2222 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002223 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002224 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002225 Record.push_back(-1); // mark the end of the diag/map pairs for this
2226 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002227 }
2228
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002229 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002230 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002231}
2232
Anders Carlssonc8505782011-03-06 18:41:18 +00002233void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2234 if (CXXBaseSpecifiersOffsets.empty())
2235 return;
2236
2237 RecordData Record;
2238
2239 // Create a blob abbreviation for the C++ base specifiers offsets.
2240 using namespace llvm;
2241
2242 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2243 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2246 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2247
Douglas Gregore92b8a12011-08-04 00:01:48 +00002248 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002249 Record.clear();
2250 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2251 Record.push_back(CXXBaseSpecifiersOffsets.size());
2252 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002253 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002254}
2255
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002256//===----------------------------------------------------------------------===//
2257// Type Serialization
2258//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002259
Sebastian Redl3397c552010-08-18 23:56:27 +00002260/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002261void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002262 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002263 if (Idx.getIndex() == 0) // we haven't seen this type before.
2264 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Douglas Gregor97475832010-10-05 18:37:06 +00002266 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002267
Douglas Gregor2cf26342009-04-09 22:27:44 +00002268 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002269 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002270 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002271 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002272 else if (TypeOffsets.size() < Index) {
2273 TypeOffsets.resize(Index + 1);
2274 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002275 }
2276
2277 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Douglas Gregor2cf26342009-04-09 22:27:44 +00002279 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002280 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002281
Douglas Gregora4923eb2009-11-16 21:35:15 +00002282 if (T.hasLocalNonFastQualifiers()) {
2283 Qualifiers Qs = T.getLocalQualifiers();
2284 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002285 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002286 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002287 } else {
2288 switch (T->getTypeClass()) {
2289 // For all of the concrete, non-dependent types, call the
2290 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002291#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002292 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002293#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002294#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002295 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002296 }
2297
2298 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002299 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002300
2301 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002302 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303}
2304
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002305//===----------------------------------------------------------------------===//
2306// Declaration Serialization
2307//===----------------------------------------------------------------------===//
2308
Douglas Gregor2cf26342009-04-09 22:27:44 +00002309/// \brief Write the block containing all of the declaration IDs
2310/// lexically declared within the given DeclContext.
2311///
2312/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2313/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002314uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002315 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002316 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002317 return 0;
2318
Douglas Gregorc9490c02009-04-16 22:23:12 +00002319 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002320 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002321 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002322 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002323 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2324 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002325 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002326
Douglas Gregor25123082009-04-22 22:34:57 +00002327 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002328 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002329 return Offset;
2330}
2331
Sebastian Redla4232eb2010-08-18 23:56:21 +00002332void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002333 using namespace llvm;
2334 RecordData Record;
2335
2336 // Write the type offsets array
2337 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002338 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2342 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2343 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002344 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002345 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002346 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002347 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002348
2349 // Write the declaration offsets array
2350 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002351 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2355 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2356 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002357 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002358 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002359 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002360 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002361}
2362
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002363void ASTWriter::WriteFileDeclIDsMap() {
2364 using namespace llvm;
2365 RecordData Record;
2366
2367 // Join the vectors of DeclIDs from all files.
2368 SmallVector<DeclID, 256> FileSortedIDs;
2369 for (FileDeclIDsTy::iterator
2370 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2371 DeclIDInFileInfo &Info = *FI->second;
2372 Info.FirstDeclIndex = FileSortedIDs.size();
2373 for (LocDeclIDsTy::iterator
2374 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2375 FileSortedIDs.push_back(DI->second);
2376 }
2377
2378 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2379 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2382 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2383 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002384 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002385 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2386}
2387
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002388void ASTWriter::WriteComments() {
2389 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002390 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002391 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002392 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2393 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002394 I != E; ++I) {
2395 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002396 AddSourceRange((*I)->getSourceRange(), Record);
2397 Record.push_back((*I)->getKind());
2398 Record.push_back((*I)->isTrailingComment());
2399 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002400 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2401 }
2402 Stream.ExitBlock();
2403}
2404
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002405//===----------------------------------------------------------------------===//
2406// Global Method Pool and Selector Serialization
2407//===----------------------------------------------------------------------===//
2408
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002409namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002410// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002411class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002412 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002413
2414public:
2415 typedef Selector key_type;
2416 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Sebastian Redl5d050072010-08-04 17:20:04 +00002418 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002419 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002420 ObjCMethodList Instance, Factory;
2421 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002422 typedef const data_type& data_type_ref;
2423
Sebastian Redl3397c552010-08-18 23:56:27 +00002424 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002426 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002427 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002428 }
Mike Stump1eb44332009-09-09 15:08:12 +00002429
2430 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002431 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002432 data_type_ref Methods) {
2433 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2434 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002435 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2436 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002437 Method = Method->Next)
2438 if (Method->Method)
2439 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002440 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002441 Method = Method->Next)
2442 if (Method->Method)
2443 DataLen += 4;
2444 clang::io::Emit16(Out, DataLen);
2445 return std::make_pair(KeyLen, DataLen);
2446 }
Mike Stump1eb44332009-09-09 15:08:12 +00002447
Chris Lattner5f9e2722011-07-23 10:55:15 +00002448 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002449 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002450 assert((Start >> 32) == 0 && "Selector key offset too large");
2451 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002452 unsigned N = Sel.getNumArgs();
2453 clang::io::Emit16(Out, N);
2454 if (N == 0)
2455 N = 1;
2456 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002457 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002458 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2459 }
Mike Stump1eb44332009-09-09 15:08:12 +00002460
Chris Lattner5f9e2722011-07-23 10:55:15 +00002461 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002462 data_type_ref Methods, unsigned DataLen) {
2463 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002464 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002465 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002466 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002467 Method = Method->Next)
2468 if (Method->Method)
2469 ++NumInstanceMethods;
2470
2471 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002472 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002473 Method = Method->Next)
2474 if (Method->Method)
2475 ++NumFactoryMethods;
2476
2477 clang::io::Emit16(Out, NumInstanceMethods);
2478 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002479 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002480 Method = Method->Next)
2481 if (Method->Method)
2482 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002483 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002484 Method = Method->Next)
2485 if (Method->Method)
2486 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002487
2488 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002489 }
2490};
2491} // end anonymous namespace
2492
Sebastian Redl059612d2010-08-03 21:58:15 +00002493/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002494///
2495/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002496/// in an on-disk hash table indexed by the selector. The hash table also
2497/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002498void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002499 using namespace llvm;
2500
Sebastian Redl059612d2010-08-03 21:58:15 +00002501 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002502 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002503 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002504 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002505 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002506 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002507 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002508 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002509
Sebastian Redl059612d2010-08-03 21:58:15 +00002510 // Create the on-disk hash table representation. We walk through every
2511 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002512 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002513 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002514 I = SelectorIDs.begin(), E = SelectorIDs.end();
2515 I != E; ++I) {
2516 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002517 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002518 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002519 I->second,
2520 ObjCMethodList(),
2521 ObjCMethodList()
2522 };
2523 if (F != SemaRef.MethodPool.end()) {
2524 Data.Instance = F->second.first;
2525 Data.Factory = F->second.second;
2526 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002527 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002528 // changed.
2529 if (Chain && I->second < FirstSelectorID) {
2530 // Selector already exists. Did it change?
2531 bool changed = false;
2532 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2533 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002534 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002535 changed = true;
2536 }
2537 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2538 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002539 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002540 changed = true;
2541 }
2542 if (!changed)
2543 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002544 } else if (Data.Instance.Method || Data.Factory.Method) {
2545 // A new method pool entry.
2546 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002547 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002548 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002549 }
2550
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002551 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002552 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002553 uint32_t BucketOffset;
2554 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002555 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002556 llvm::raw_svector_ostream Out(MethodPool);
2557 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002558 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002559 BucketOffset = Generator.Emit(Out, Trait);
2560 }
2561
2562 // Create a blob abbreviation
2563 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002564 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2568 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2569
Douglas Gregor83941df2009-04-25 17:48:32 +00002570 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002571 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002572 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002573 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002574 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002575 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002576
2577 // Create a blob abbreviation for the selector table offsets.
2578 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002579 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2583 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2584
2585 // Write the selector offsets table.
2586 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002587 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002588 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002589 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002590 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002591 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002592 }
2593}
2594
Sebastian Redl3397c552010-08-18 23:56:27 +00002595/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002596void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002597 using namespace llvm;
2598 if (SemaRef.ReferencedSelectors.empty())
2599 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002600
Fariborz Jahanian32019832010-07-23 19:11:11 +00002601 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002602
Sebastian Redl3397c552010-08-18 23:56:27 +00002603 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002604 // very tricky to fix, and given that @selector shouldn't really appear in
2605 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002606 for (DenseMap<Selector, SourceLocation>::iterator S =
2607 SemaRef.ReferencedSelectors.begin(),
2608 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2609 Selector Sel = (*S).first;
2610 SourceLocation Loc = (*S).second;
2611 AddSelectorRef(Sel, Record);
2612 AddSourceLocation(Loc, Record);
2613 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002614 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002615}
2616
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002617//===----------------------------------------------------------------------===//
2618// Identifier Table Serialization
2619//===----------------------------------------------------------------------===//
2620
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002621namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002622class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002623 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002624 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002625 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002626 bool IsModule;
2627
Douglas Gregora92193e2009-04-28 21:18:29 +00002628 /// \brief Determines whether this is an "interesting" identifier
2629 /// that needs a full IdentifierInfo structure written into the hash
2630 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002631 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002632 if (II->isPoisoned() ||
2633 II->isExtensionToken() ||
2634 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002635 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002636 II->getFETokenInfo<void>())
2637 return true;
2638
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002639 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002640 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002641
2642 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2643 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002644 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002645
2646 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002647 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002648
2649 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002650 }
2651
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002652public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002653 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002654 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002655
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002656 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002657 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002658
Douglas Gregoreee242f2011-10-27 09:33:13 +00002659 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2660 IdentifierResolver &IdResolver, bool IsModule)
2661 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002662
2663 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002664 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002665 }
Mike Stump1eb44332009-09-09 15:08:12 +00002666
2667 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002668 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002669 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002670 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002671 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002672 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002673 DataLen += 2; // 2 bytes for builtin ID
2674 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002675 if (hadMacroDefinition(II, Macro)) {
2676 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2677 if (Writer.getMacroRef(M) != 0)
2678 DataLen += 4;
2679 }
2680
2681 DataLen += 4;
2682 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002683
Douglas Gregoreee242f2011-10-27 09:33:13 +00002684 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2685 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002686 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002687 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002688 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002689 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002690 // We emit the key length after the data length so that every
2691 // string is preceded by a 16-bit length. This matches the PTH
2692 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002693 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002694 return std::make_pair(KeyLen, DataLen);
2695 }
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Chris Lattner5f9e2722011-07-23 10:55:15 +00002697 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002698 unsigned KeyLen) {
2699 // Record the location of the key data. This is used when generating
2700 // the mapping from persistent IDs to strings.
2701 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002702 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002703 }
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Douglas Gregor7143aab2011-09-01 17:04:32 +00002705 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002706 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002707 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002708 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002709 clang::io::Emit32(Out, ID << 1);
2710 return;
2711 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002712
Douglas Gregora92193e2009-04-28 21:18:29 +00002713 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002714 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2715 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2716 clang::io::Emit16(Out, Bits);
2717 Bits = 0;
2718 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002719 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002720 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2721 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002722 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002723 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002724 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002725
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002726 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002727 // Write all of the macro IDs associated with this identifier.
2728 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2729 if (MacroID ID = Writer.getMacroRef(M))
2730 clang::io::Emit32(Out, ID);
2731 }
2732
2733 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002734 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002735
Douglas Gregor668c1a42009-04-21 22:25:48 +00002736 // Emit the declaration IDs in reverse order, because the
2737 // IdentifierResolver provides the declarations as they would be
2738 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002739 // "stat"), but the ASTReader adds declarations to the end of the list
2740 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002741 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002742 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2743 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002744 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002745 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002746 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002747 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002748 }
2749};
2750} // end anonymous namespace
2751
Sebastian Redl3397c552010-08-18 23:56:27 +00002752/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002753///
2754/// The identifier table consists of a blob containing string data
2755/// (the actual identifiers themselves) and a separate "offsets" index
2756/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002757void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2758 IdentifierResolver &IdResolver,
2759 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002760 using namespace llvm;
2761
2762 // Create and write out the blob that contains the identifier
2763 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002764 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002765 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002766 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Douglas Gregor92b059e2009-04-28 20:33:11 +00002768 // Look for any identifiers that were named while processing the
2769 // headers, but are otherwise not needed. We add these to the hash
2770 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002771 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002772 // file.
2773 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2774 IDEnd = PP.getIdentifierTable().end();
2775 ID != IDEnd; ++ID)
2776 getIdentifierRef(ID->second);
2777
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002778 // Create the on-disk hash table representation. We only store offsets
2779 // for identifiers that appear here for the first time.
2780 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002781 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002782 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2783 ID != IDEnd; ++ID) {
2784 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002785 if (!Chain || !ID->first->isFromAST() ||
2786 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002787 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2788 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002789 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002790
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002791 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002792 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002793 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002794 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002795 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002796 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002797 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002798 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002799 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002800 }
2801
2802 // Create a blob abbreviation
2803 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002804 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002807 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002808
2809 // Write the identifier table
2810 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002811 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002812 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002813 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002814 }
2815
2816 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002817 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002818 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002819 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002820 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002821 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2822 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2823
2824 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002825 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002826 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002827 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002828 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002829 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002830}
2831
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002832//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002833// DeclContext's Name Lookup Table Serialization
2834//===----------------------------------------------------------------------===//
2835
2836namespace {
2837// Trait used for the on-disk hash table used in the method pool.
2838class ASTDeclContextNameLookupTrait {
2839 ASTWriter &Writer;
2840
2841public:
2842 typedef DeclarationName key_type;
2843 typedef key_type key_type_ref;
2844
2845 typedef DeclContext::lookup_result data_type;
2846 typedef const data_type& data_type_ref;
2847
2848 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2849
2850 unsigned ComputeHash(DeclarationName Name) {
2851 llvm::FoldingSetNodeID ID;
2852 ID.AddInteger(Name.getNameKind());
2853
2854 switch (Name.getNameKind()) {
2855 case DeclarationName::Identifier:
2856 ID.AddString(Name.getAsIdentifierInfo()->getName());
2857 break;
2858 case DeclarationName::ObjCZeroArgSelector:
2859 case DeclarationName::ObjCOneArgSelector:
2860 case DeclarationName::ObjCMultiArgSelector:
2861 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2862 break;
2863 case DeclarationName::CXXConstructorName:
2864 case DeclarationName::CXXDestructorName:
2865 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002866 break;
2867 case DeclarationName::CXXOperatorName:
2868 ID.AddInteger(Name.getCXXOverloadedOperator());
2869 break;
2870 case DeclarationName::CXXLiteralOperatorName:
2871 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2872 case DeclarationName::CXXUsingDirective:
2873 break;
2874 }
2875
2876 return ID.ComputeHash();
2877 }
2878
2879 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002880 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002881 data_type_ref Lookup) {
2882 unsigned KeyLen = 1;
2883 switch (Name.getNameKind()) {
2884 case DeclarationName::Identifier:
2885 case DeclarationName::ObjCZeroArgSelector:
2886 case DeclarationName::ObjCOneArgSelector:
2887 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002888 case DeclarationName::CXXLiteralOperatorName:
2889 KeyLen += 4;
2890 break;
2891 case DeclarationName::CXXOperatorName:
2892 KeyLen += 1;
2893 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002894 case DeclarationName::CXXConstructorName:
2895 case DeclarationName::CXXDestructorName:
2896 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002897 case DeclarationName::CXXUsingDirective:
2898 break;
2899 }
2900 clang::io::Emit16(Out, KeyLen);
2901
2902 // 2 bytes for num of decls and 4 for each DeclID.
2903 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2904 clang::io::Emit16(Out, DataLen);
2905
2906 return std::make_pair(KeyLen, DataLen);
2907 }
2908
Chris Lattner5f9e2722011-07-23 10:55:15 +00002909 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002910 using namespace clang::io;
2911
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002912 Emit8(Out, Name.getNameKind());
2913 switch (Name.getNameKind()) {
2914 case DeclarationName::Identifier:
2915 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002916 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002917 case DeclarationName::ObjCZeroArgSelector:
2918 case DeclarationName::ObjCOneArgSelector:
2919 case DeclarationName::ObjCMultiArgSelector:
2920 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002921 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002922 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002923 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2924 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002925 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002926 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002927 case DeclarationName::CXXLiteralOperatorName:
2928 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002929 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002930 case DeclarationName::CXXConstructorName:
2931 case DeclarationName::CXXDestructorName:
2932 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002933 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002934 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002935 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002936
2937 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002938 }
2939
Chris Lattner5f9e2722011-07-23 10:55:15 +00002940 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002941 data_type Lookup, unsigned DataLen) {
2942 uint64_t Start = Out.tell(); (void)Start;
2943 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2944 for (; Lookup.first != Lookup.second; ++Lookup.first)
2945 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2946
2947 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2948 }
2949};
2950} // end anonymous namespace
2951
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002952/// \brief Write the block containing all of the declaration IDs
2953/// visible from the given DeclContext.
2954///
2955/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002956/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002957uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2958 DeclContext *DC) {
2959 if (DC->getPrimaryContext() != DC)
2960 return 0;
2961
2962 // Since there is no name lookup into functions or methods, don't bother to
2963 // build a visible-declarations table for these entities.
2964 if (DC->isFunctionOrMethod())
2965 return 0;
2966
2967 // If not in C++, we perform name lookup for the translation unit via the
2968 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2969 // FIXME: In C++ we need the visible declarations in order to "see" the
2970 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002971 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002972 return 0;
2973
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002974 // Serialize the contents of the mapping used for lookup. Note that,
2975 // although we have two very different code paths, the serialized
2976 // representation is the same for both cases: a declaration name,
2977 // followed by a size, followed by references to the visible
2978 // declarations that have that name.
2979 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002980 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002981 if (!Map || Map->empty())
2982 return 0;
2983
2984 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2985 ASTDeclContextNameLookupTrait Trait(*this);
2986
2987 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002988 DeclarationName ConversionName;
2989 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002990 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2991 D != DEnd; ++D) {
2992 DeclarationName Name = D->first;
2993 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002994 if (Result.first != Result.second) {
2995 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2996 // Hash all conversion function names to the same name. The actual
2997 // type information in conversion function name is not used in the
2998 // key (since such type information is not stable across different
2999 // modules), so the intended effect is to coalesce all of the conversion
3000 // functions under a single key.
3001 if (!ConversionName)
3002 ConversionName = Name;
3003 ConversionDecls.append(Result.first, Result.second);
3004 continue;
3005 }
3006
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003007 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003008 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003009 }
3010
Douglas Gregore5a54b62011-08-30 20:49:19 +00003011 // Add the conversion functions
3012 if (!ConversionDecls.empty()) {
3013 Generator.insert(ConversionName,
3014 DeclContext::lookup_result(ConversionDecls.begin(),
3015 ConversionDecls.end()),
3016 Trait);
3017 }
3018
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003019 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003020 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003021 uint32_t BucketOffset;
3022 {
3023 llvm::raw_svector_ostream Out(LookupTable);
3024 // Make sure that no bucket is at offset 0
3025 clang::io::Emit32(Out, 0);
3026 BucketOffset = Generator.Emit(Out, Trait);
3027 }
3028
3029 // Write the lookup table
3030 RecordData Record;
3031 Record.push_back(DECL_CONTEXT_VISIBLE);
3032 Record.push_back(BucketOffset);
3033 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3034 LookupTable.str());
3035
3036 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3037 ++NumVisibleDeclContexts;
3038 return Offset;
3039}
3040
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003041/// \brief Write an UPDATE_VISIBLE block for the given context.
3042///
3043/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3044/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003045/// (in C++), for namespaces, and for classes with forward-declared unscoped
3046/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003047void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003048 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3049 if (!Map || Map->empty())
3050 return;
3051
3052 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3053 ASTDeclContextNameLookupTrait Trait(*this);
3054
3055 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003056 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3057 D != DEnd; ++D) {
3058 DeclarationName Name = D->first;
3059 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003060 // For any name that appears in this table, the results are complete, i.e.
3061 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003062 if (Result.first != Result.second)
3063 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003064 }
3065
3066 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003067 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003068 uint32_t BucketOffset;
3069 {
3070 llvm::raw_svector_ostream Out(LookupTable);
3071 // Make sure that no bucket is at offset 0
3072 clang::io::Emit32(Out, 0);
3073 BucketOffset = Generator.Emit(Out, Trait);
3074 }
3075
3076 // Write the lookup table
3077 RecordData Record;
3078 Record.push_back(UPDATE_VISIBLE);
3079 Record.push_back(getDeclID(cast<Decl>(DC)));
3080 Record.push_back(BucketOffset);
3081 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3082}
3083
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003084/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3085void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3086 RecordData Record;
3087 Record.push_back(Opts.fp_contract);
3088 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3089}
3090
3091/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3092void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003093 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003094 return;
3095
3096 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3097 RecordData Record;
3098#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3099#include "clang/Basic/OpenCLExtensions.def"
3100 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3101}
3102
Douglas Gregor2171bf12012-01-15 16:58:34 +00003103void ASTWriter::WriteRedeclarations() {
3104 RecordData LocalRedeclChains;
3105 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3106
3107 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3108 Decl *First = Redeclarations[I];
3109 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3110
3111 Decl *MostRecent = First->getMostRecentDecl();
3112
3113 // If we only have a single declaration, there is no point in storing
3114 // a redeclaration chain.
3115 if (First == MostRecent)
3116 continue;
3117
3118 unsigned Offset = LocalRedeclChains.size();
3119 unsigned Size = 0;
3120 LocalRedeclChains.push_back(0); // Placeholder for the size.
3121
3122 // Collect the set of local redeclarations of this declaration.
3123 for (Decl *Prev = MostRecent; Prev != First;
3124 Prev = Prev->getPreviousDecl()) {
3125 if (!Prev->isFromASTFile()) {
3126 AddDeclRef(Prev, LocalRedeclChains);
3127 ++Size;
3128 }
3129 }
3130 LocalRedeclChains[Offset] = Size;
3131
3132 // Reverse the set of local redeclarations, so that we store them in
3133 // order (since we found them in reverse order).
3134 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3135
3136 // Add the mapping from the first ID to the set of local declarations.
3137 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3138 LocalRedeclsMap.push_back(Info);
3139
3140 assert(N == Redeclarations.size() &&
3141 "Deserialized a declaration we shouldn't have");
3142 }
3143
3144 if (LocalRedeclChains.empty())
3145 return;
3146
3147 // Sort the local redeclarations map by the first declaration ID,
3148 // since the reader will be performing binary searches on this information.
3149 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3150
3151 // Emit the local redeclarations map.
3152 using namespace llvm;
3153 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3154 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3157 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3158
3159 RecordData Record;
3160 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3161 Record.push_back(LocalRedeclsMap.size());
3162 Stream.EmitRecordWithBlob(AbbrevID, Record,
3163 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3164 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3165
3166 // Emit the redeclaration chains.
3167 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3168}
3169
Douglas Gregorcff9f262012-01-27 01:47:08 +00003170void ASTWriter::WriteObjCCategories() {
3171 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3172 RecordData Categories;
3173
3174 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3175 unsigned Size = 0;
3176 unsigned StartIndex = Categories.size();
3177
3178 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3179
3180 // Allocate space for the size.
3181 Categories.push_back(0);
3182
3183 // Add the categories.
3184 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3185 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3186 assert(getDeclID(Cat) != 0 && "Bogus category");
3187 AddDeclRef(Cat, Categories);
3188 }
3189
3190 // Update the size.
3191 Categories[StartIndex] = Size;
3192
3193 // Record this interface -> category map.
3194 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3195 CategoriesMap.push_back(CatInfo);
3196 }
3197
3198 // Sort the categories map by the definition ID, since the reader will be
3199 // performing binary searches on this information.
3200 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3201
3202 // Emit the categories map.
3203 using namespace llvm;
3204 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3205 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3208 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3209
3210 RecordData Record;
3211 Record.push_back(OBJC_CATEGORIES_MAP);
3212 Record.push_back(CategoriesMap.size());
3213 Stream.EmitRecordWithBlob(AbbrevID, Record,
3214 reinterpret_cast<char*>(CategoriesMap.data()),
3215 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3216
3217 // Emit the category lists.
3218 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3219}
3220
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003221void ASTWriter::WriteMergedDecls() {
3222 if (!Chain || Chain->MergedDecls.empty())
3223 return;
3224
3225 RecordData Record;
3226 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3227 IEnd = Chain->MergedDecls.end();
3228 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003229 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003230 : getDeclID(I->first);
3231 assert(CanonID && "Merged declaration not known?");
3232
3233 Record.push_back(CanonID);
3234 Record.push_back(I->second.size());
3235 Record.append(I->second.begin(), I->second.end());
3236 }
3237 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3238}
3239
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003240//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003241// General Serialization Routines
3242//===----------------------------------------------------------------------===//
3243
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003244/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003245void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3246 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003247 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003248 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3249 e = Attrs.end(); i != e; ++i){
3250 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003251 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003252 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003253
Sean Huntcf807c42010-08-18 23:23:40 +00003254#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003255
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003256 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003257}
3258
Chris Lattner5f9e2722011-07-23 10:55:15 +00003259void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003260 Record.push_back(Str.size());
3261 Record.insert(Record.end(), Str.begin(), Str.end());
3262}
3263
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003264void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3265 RecordDataImpl &Record) {
3266 Record.push_back(Version.getMajor());
3267 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3268 Record.push_back(*Minor + 1);
3269 else
3270 Record.push_back(0);
3271 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3272 Record.push_back(*Subminor + 1);
3273 else
3274 Record.push_back(0);
3275}
3276
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003277/// \brief Note that the identifier II occurs at the given offset
3278/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003279void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003280 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003281 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003282 // up earlier in the chain and thus don't need an offset.
3283 if (ID >= FirstIdentID)
3284 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003285}
3286
Douglas Gregor83941df2009-04-25 17:48:32 +00003287/// \brief Note that the selector Sel occurs at the given offset
3288/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003289void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003290 unsigned ID = SelectorIDs[Sel];
3291 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003292 // Don't record offsets for selectors that are also available in a different
3293 // file.
3294 if (ID < FirstSelectorID)
3295 return;
3296 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003297}
3298
Sebastian Redla4232eb2010-08-18 23:56:21 +00003299ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003300 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003301 WritingAST(false), DoneWritingDeclsAndTypes(false),
3302 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003303 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003304 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003305 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3306 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003307 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3308 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003309 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003310 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003311 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003312 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003313 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003314 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003315 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3316 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3317 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003318 DeclTypedefAbbrev(0),
3319 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3320 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003321{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003322}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003323
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003324ASTWriter::~ASTWriter() {
3325 for (FileDeclIDsTy::iterator
3326 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3327 delete I->second;
3328}
3329
Sebastian Redla4232eb2010-08-18 23:56:21 +00003330void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003331 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003332 Module *WritingModule, StringRef isysroot,
3333 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003334 WritingAST = true;
3335
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003336 ASTHasCompilerErrors = hasErrors;
3337
Douglas Gregor2cf26342009-04-09 22:27:44 +00003338 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003339 Stream.Emit((unsigned)'C', 8);
3340 Stream.Emit((unsigned)'P', 8);
3341 Stream.Emit((unsigned)'C', 8);
3342 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003343
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003344 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003345
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003346 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003347 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003348 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003349 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003350 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003351 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003352 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003353
3354 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003355}
3356
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003357template<typename Vector>
3358static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3359 ASTWriter::RecordData &Record) {
3360 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3361 I != E; ++I) {
3362 Writer.AddDeclRef(*I, Record);
3363 }
3364}
3365
Sebastian Redla4232eb2010-08-18 23:56:21 +00003366void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003367 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003368 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003369 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003370 using namespace llvm;
3371
Douglas Gregorecc2c092011-12-01 22:20:10 +00003372 // Make sure that the AST reader knows to finalize itself.
3373 if (Chain)
3374 Chain->finalizeForWriting();
3375
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003376 ASTContext &Context = SemaRef.Context;
3377 Preprocessor &PP = SemaRef.PP;
3378
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003379 // Set up predefined declaration IDs.
3380 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003381 if (Context.ObjCIdDecl)
3382 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003383 if (Context.ObjCSelDecl)
3384 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003385 if (Context.ObjCClassDecl)
3386 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003387 if (Context.ObjCProtocolClassDecl)
3388 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003389 if (Context.Int128Decl)
3390 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3391 if (Context.UInt128Decl)
3392 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003393 if (Context.ObjCInstanceTypeDecl)
3394 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003395 if (Context.BuiltinVaListDecl)
3396 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3397
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003398 if (!Chain) {
3399 // Make sure that we emit IdentifierInfos (and any attached
3400 // declarations) for builtins. We don't need to do this when we're
3401 // emitting chained PCH files, because all of the builtins will be
3402 // in the original PCH file.
3403 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003404 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003405 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003406 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003407 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003408 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3409 getIdentifierRef(&Table.get(BuiltinNames[I]));
3410 }
3411
Douglas Gregoreee242f2011-10-27 09:33:13 +00003412 // If there are any out-of-date identifiers, bring them up to date.
3413 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3414 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3415 IDEnd = PP.getIdentifierTable().end();
3416 ID != IDEnd; ++ID)
3417 if (ID->second->isOutOfDate())
3418 ExtSource->updateOutOfDateIdentifier(*ID->second);
3419 }
3420
Chris Lattner63d65f82009-09-08 18:19:27 +00003421 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003422 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003423 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003424 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003425 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003426
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003427 // Build a record containing all of the file scoped decls in this file.
3428 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003429 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3430 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003431
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003432 // Build a record containing all of the delegating constructors we still need
3433 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003434 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003435 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003436
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003437 // Write the set of weak, undeclared identifiers. We always write the
3438 // entire table, since later PCH files in a PCH chain are only interested in
3439 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003440 RecordData WeakUndeclaredIdentifiers;
3441 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003442 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003443 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3444 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3445 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3446 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3447 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3448 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3449 }
3450 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003451
Douglas Gregor14c22f22009-04-22 22:18:58 +00003452 // Build a record containing all of the locally-scoped external
3453 // declarations in this header file. Generally, this record will be
3454 // empty.
3455 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003456 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003457 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003458 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003459 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3460 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003461 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003462 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003463 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3464 }
3465
Douglas Gregorb81c1702009-04-27 20:06:05 +00003466 // Build a record containing all of the ext_vector declarations.
3467 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003468 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003469
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003470 // Build a record containing all of the VTable uses information.
3471 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003472 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003473 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3474 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3475 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3476 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3477 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003478 }
3479
3480 // Build a record containing all of dynamic classes declarations.
3481 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003482 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003483
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003484 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003485 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003486 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003487 I = SemaRef.PendingInstantiations.begin(),
3488 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3489 AddDeclRef(I->first, PendingInstantiations);
3490 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003491 }
3492 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3493 "There are local ones at end of translation unit!");
3494
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003495 // Build a record containing some declaration references.
3496 RecordData SemaDeclRefs;
3497 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3498 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3499 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3500 }
3501
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003502 RecordData CUDASpecialDeclRefs;
3503 if (Context.getcudaConfigureCallDecl()) {
3504 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3505 }
3506
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003507 // Build a record containing all of the known namespaces.
3508 RecordData KnownNamespaces;
3509 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3510 I = SemaRef.KnownNamespaces.begin(),
3511 IEnd = SemaRef.KnownNamespaces.end();
3512 I != IEnd; ++I) {
3513 if (!I->second)
3514 AddDeclRef(I->first, KnownNamespaces);
3515 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003516
3517 // Write the control block
3518 WriteControlBlock(Context, isysroot, OutputFile);
3519
Sebastian Redl3397c552010-08-18 23:56:27 +00003520 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003521 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003522 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor832d6202011-07-22 16:35:34 +00003523 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003524 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003525
3526 // Create a lexical update block containing all of the declarations in the
3527 // translation unit that do not come from other AST files.
3528 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3529 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3530 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3531 E = TU->noload_decls_end();
3532 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003533 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003534 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003535 }
3536
3537 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3538 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3539 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3540 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3541 Record.clear();
3542 Record.push_back(TU_UPDATE_LEXICAL);
3543 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3544 data(NewGlobalDecls));
3545
3546 // And a visible updates block for the translation unit.
3547 Abv = new llvm::BitCodeAbbrev();
3548 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3549 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3550 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3551 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3552 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3553 WriteDeclContextVisibleUpdate(TU);
3554
3555 // If the translation unit has an anonymous namespace, and we don't already
3556 // have an update block for it, write it as an update block.
3557 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3558 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3559 if (Record.empty()) {
3560 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003561 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003562 }
3563 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003564
3565 // Make sure visible decls, added to DeclContexts previously loaded from
3566 // an AST file, are registered for serialization.
3567 for (SmallVector<const Decl *, 16>::iterator
3568 I = UpdatingVisibleDecls.begin(),
3569 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3570 GetDeclRef(*I);
3571 }
3572
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003573 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003574 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003575
Douglas Gregora119da02011-08-02 16:26:37 +00003576 // Form the record of special types.
3577 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003578 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003579 AddTypeRef(Context.getFILEType(), SpecialTypes);
3580 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3581 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3582 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3583 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003584 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003585 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003586
Douglas Gregor366809a2009-04-26 03:49:13 +00003587 // Keep writing types and declarations until all types and
3588 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003589 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003590 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003591 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3592 E = DeclsToRewrite.end();
3593 I != E; ++I)
3594 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003595 while (!DeclTypesToEmit.empty()) {
3596 DeclOrType DOT = DeclTypesToEmit.front();
3597 DeclTypesToEmit.pop();
3598 if (DOT.isType())
3599 WriteType(DOT.getType());
3600 else
3601 WriteDecl(Context, DOT.getDecl());
3602 }
3603 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003604
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003605 DoneWritingDeclsAndTypes = true;
3606
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003607 WriteFileDeclIDsMap();
3608 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003609 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003610
3611 if (Chain) {
3612 // Write the mapping information describing our module dependencies and how
3613 // each of those modules were mapped into our own offset/ID space, so that
3614 // the reader can build the appropriate mapping to its own offset/ID space.
3615 // The map consists solely of a blob with the following format:
3616 // *(module-name-len:i16 module-name:len*i8
3617 // source-location-offset:i32
3618 // identifier-id:i32
3619 // preprocessed-entity-id:i32
3620 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003621 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003622 // selector-id:i32
3623 // declaration-id:i32
3624 // c++-base-specifiers-id:i32
3625 // type-id:i32)
3626 //
3627 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3628 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3630 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003631 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003632 {
3633 llvm::raw_svector_ostream Out(Buffer);
3634 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003635 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003636 M != MEnd; ++M) {
3637 StringRef FileName = (*M)->FileName;
3638 io::Emit16(Out, FileName.size());
3639 Out.write(FileName.data(), FileName.size());
3640 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3641 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003642 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003643 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003644 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003645 io::Emit32(Out, (*M)->BaseSelectorID);
3646 io::Emit32(Out, (*M)->BaseDeclID);
3647 io::Emit32(Out, (*M)->BaseTypeIndex);
3648 }
3649 }
3650 Record.clear();
3651 Record.push_back(MODULE_OFFSET_MAP);
3652 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3653 Buffer.data(), Buffer.size());
3654 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003655 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003656 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003657 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003658 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003659 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003660 WriteFPPragmaOptions(SemaRef.getFPOptions());
3661 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003662
Sebastian Redl1476ed42010-07-16 16:36:56 +00003663 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003664 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003665
Anders Carlssonc8505782011-03-06 18:41:18 +00003666 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003667
Douglas Gregore209e502011-12-06 01:10:29 +00003668 // If we're emitting a module, write out the submodule information.
3669 if (WritingModule)
3670 WriteSubmodules(WritingModule);
3671
Douglas Gregora119da02011-08-02 16:26:37 +00003672 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3673
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003674 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003675 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003676 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003677
3678 // Write the record containing tentative definitions.
3679 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003680 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003681
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003682 // Write the record containing unused file scoped decls.
3683 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003684 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003685
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003686 // Write the record containing weak undeclared identifiers.
3687 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003688 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003689 WeakUndeclaredIdentifiers);
3690
Douglas Gregor14c22f22009-04-22 22:18:58 +00003691 // Write the record containing locally-scoped external definitions.
3692 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003693 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003694 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003695
3696 // Write the record containing ext_vector type names.
3697 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003698 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003699
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003700 // Write the record containing VTable uses information.
3701 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003702 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003703
3704 // Write the record containing dynamic classes declarations.
3705 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003706 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003707
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003708 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003709 if (!PendingInstantiations.empty())
3710 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003711
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003712 // Write the record containing declaration references of Sema.
3713 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003714 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003715
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003716 // Write the record containing CUDA-specific declaration references.
3717 if (!CUDASpecialDeclRefs.empty())
3718 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003719
3720 // Write the delegating constructors.
3721 if (!DelegatingCtorDecls.empty())
3722 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003723
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003724 // Write the known namespaces.
3725 if (!KnownNamespaces.empty())
3726 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3727
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003728 // Write the visible updates to DeclContexts.
3729 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3730 I = UpdatedDeclContexts.begin(),
3731 E = UpdatedDeclContexts.end();
3732 I != E; ++I)
3733 WriteDeclContextVisibleUpdate(*I);
3734
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003735 if (!WritingModule) {
3736 // Write the submodules that were imported, if any.
3737 RecordData ImportedModules;
3738 for (ASTContext::import_iterator I = Context.local_import_begin(),
3739 IEnd = Context.local_import_end();
3740 I != IEnd; ++I) {
3741 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3742 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3743 }
3744 if (!ImportedModules.empty()) {
3745 // Sort module IDs.
3746 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3747
3748 // Unique module IDs.
3749 ImportedModules.erase(std::unique(ImportedModules.begin(),
3750 ImportedModules.end()),
3751 ImportedModules.end());
3752
3753 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3754 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003755 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003756
3757 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003758 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003759 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003760 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003761 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003762 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003763
Douglas Gregor3e1af842009-04-17 22:13:46 +00003764 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003765 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003766 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003767 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003768 Record.push_back(NumLexicalDeclContexts);
3769 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003770 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003771 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003772}
3773
Douglas Gregora8235d62012-10-09 23:05:51 +00003774void ASTWriter::WriteMacroUpdates() {
3775 if (MacroUpdates.empty())
3776 return;
3777
3778 RecordData Record;
3779 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3780 E = MacroUpdates.end();
3781 I != E; ++I) {
3782 addMacroRef(I->first, Record);
3783 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003784 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003785 }
3786 Stream.EmitRecord(MACRO_UPDATES, Record);
3787}
3788
Douglas Gregor61c5e342011-09-17 00:05:03 +00003789/// \brief Go through the declaration update blocks and resolve declaration
3790/// pointers into declaration IDs.
3791void ASTWriter::ResolveDeclUpdatesBlocks() {
3792 for (DeclUpdateMap::iterator
3793 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3794 const Decl *D = I->first;
3795 UpdateRecord &URec = I->second;
3796
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003797 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003798 continue; // The decl will be written completely
3799
3800 unsigned Idx = 0, N = URec.size();
3801 while (Idx < N) {
3802 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003803 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3804 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3805 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3806 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3807 ++Idx;
3808 break;
3809
3810 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3811 ++Idx;
3812 break;
3813 }
3814 }
3815 }
3816}
3817
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003818void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003819 if (DeclUpdates.empty())
3820 return;
3821
3822 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003823 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003824 for (DeclUpdateMap::iterator
3825 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3826 const Decl *D = I->first;
3827 UpdateRecord &URec = I->second;
3828
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003829 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003830 continue; // The decl will be written completely,no need to store updates.
3831
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003832 uint64_t Offset = Stream.GetCurrentBitNo();
3833 Stream.EmitRecord(DECL_UPDATES, URec);
3834
3835 OffsetsRecord.push_back(GetDeclRef(D));
3836 OffsetsRecord.push_back(Offset);
3837 }
3838 Stream.ExitBlock();
3839 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3840}
3841
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003842void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003843 if (ReplacedDecls.empty())
3844 return;
3845
3846 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003847 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003848 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003849 Record.push_back(I->ID);
3850 Record.push_back(I->Offset);
3851 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003852 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003853 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003854}
3855
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003856void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003857 Record.push_back(Loc.getRawEncoding());
3858}
3859
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003860void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003861 AddSourceLocation(Range.getBegin(), Record);
3862 AddSourceLocation(Range.getEnd(), Record);
3863}
3864
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003865void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003866 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003867 const uint64_t *Words = Value.getRawData();
3868 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003869}
3870
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003871void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003872 Record.push_back(Value.isUnsigned());
3873 AddAPInt(Value, Record);
3874}
3875
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003876void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003877 AddAPInt(Value.bitcastToAPInt(), Record);
3878}
3879
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003880void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003881 Record.push_back(getIdentifierRef(II));
3882}
3883
Douglas Gregora8235d62012-10-09 23:05:51 +00003884void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3885 Record.push_back(getMacroRef(MI));
3886}
3887
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003888IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003889 if (II == 0)
3890 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003891
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003892 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003893 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003894 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003895 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003896}
3897
Douglas Gregora8235d62012-10-09 23:05:51 +00003898MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3899 // Don't emit builtin macros like __LINE__ to the AST file unless they
3900 // have been redefined by the header (in which case they are not
3901 // isBuiltinMacro).
3902 if (MI == 0 || MI->isBuiltinMacro())
3903 return 0;
3904
3905 MacroID &ID = MacroIDs[MI];
3906 if (ID == 0)
3907 ID = NextMacroID++;
3908 return ID;
3909}
3910
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003911void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003912 Record.push_back(getSelectorRef(SelRef));
3913}
3914
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003915SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003916 if (Sel.getAsOpaquePtr() == 0) {
3917 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003918 }
3919
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003920 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003921 if (SID == 0 && Chain) {
3922 // This might trigger a ReadSelector callback, which will set the ID for
3923 // this selector.
3924 Chain->LoadSelector(Sel);
3925 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003926 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003927 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003928 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003929 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003930}
3931
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003932void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003933 AddDeclRef(Temp->getDestructor(), Record);
3934}
3935
Douglas Gregor7c789c12010-10-29 22:39:52 +00003936void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3937 CXXBaseSpecifier const *BasesEnd,
3938 RecordDataImpl &Record) {
3939 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3940 CXXBaseSpecifiersToWrite.push_back(
3941 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3942 Bases, BasesEnd));
3943 Record.push_back(NextCXXBaseSpecifiersID++);
3944}
3945
Sebastian Redla4232eb2010-08-18 23:56:21 +00003946void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003947 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003948 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003949 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003950 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003951 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003952 break;
3953 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003954 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003955 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003956 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003957 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003958 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003959 break;
3960 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003961 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003962 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003963 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003964 break;
John McCall833ca992009-10-29 08:12:44 +00003965 case TemplateArgument::Null:
3966 case TemplateArgument::Integral:
3967 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003968 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003969 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003970 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003971 break;
3972 }
3973}
3974
Sebastian Redla4232eb2010-08-18 23:56:21 +00003975void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003976 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003977 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003978
3979 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3980 bool InfoHasSameExpr
3981 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3982 Record.push_back(InfoHasSameExpr);
3983 if (InfoHasSameExpr)
3984 return; // Avoid storing the same expr twice.
3985 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003986 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3987 Record);
3988}
3989
Douglas Gregordc355712011-02-25 00:36:19 +00003990void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3991 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003992 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003993 AddTypeRef(QualType(), Record);
3994 return;
3995 }
3996
Douglas Gregordc355712011-02-25 00:36:19 +00003997 AddTypeLoc(TInfo->getTypeLoc(), Record);
3998}
3999
4000void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4001 AddTypeRef(TL.getType(), Record);
4002
John McCalla1ee0c52009-10-16 21:56:05 +00004003 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004004 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004005 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004006}
4007
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004008void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004009 Record.push_back(GetOrCreateTypeID(T));
4010}
4011
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004012TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4013 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004014 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4015}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004016
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004017TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004018 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004019 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004020}
4021
4022TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4023 if (T.isNull())
4024 return TypeIdx();
4025 assert(!T.getLocalFastQualifiers());
4026
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004027 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004028 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004029 if (DoneWritingDeclsAndTypes) {
4030 assert(0 && "New type seen after serializing all the types to emit!");
4031 return TypeIdx();
4032 }
4033
Douglas Gregor366809a2009-04-26 03:49:13 +00004034 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004035 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004036 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004037 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004038 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004039 return Idx;
4040}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004041
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004042TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004043 if (T.isNull())
4044 return TypeIdx();
4045 assert(!T.getLocalFastQualifiers());
4046
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004047 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4048 assert(I != TypeIdxs.end() && "Type not emitted!");
4049 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004050}
4051
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004052void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004053 Record.push_back(GetDeclRef(D));
4054}
4055
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004056DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004057 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4058
Douglas Gregor2cf26342009-04-09 22:27:44 +00004059 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004060 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004061 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004062
4063 // If D comes from an AST file, its declaration ID is already known and
4064 // fixed.
4065 if (D->isFromASTFile())
4066 return D->getGlobalID();
4067
Douglas Gregor97475832010-10-05 18:37:06 +00004068 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004069 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004070 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004071 if (DoneWritingDeclsAndTypes) {
4072 assert(0 && "New decl seen after serializing all the decls to emit!");
4073 return 0;
4074 }
4075
Douglas Gregor2cf26342009-04-09 22:27:44 +00004076 // We haven't seen this declaration before. Give it a new ID and
4077 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004078 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004079 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004080 }
4081
Sebastian Redl681d7232010-07-27 00:17:23 +00004082 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004083}
4084
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004085DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004086 if (D == 0)
4087 return 0;
4088
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004089 // If D comes from an AST file, its declaration ID is already known and
4090 // fixed.
4091 if (D->isFromASTFile())
4092 return D->getGlobalID();
4093
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004094 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4095 return DeclIDs[D];
4096}
4097
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004098static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4099 std::pair<unsigned, serialization::DeclID> R) {
4100 return L.first < R.first;
4101}
4102
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004103void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004104 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004105 assert(D);
4106
4107 SourceLocation Loc = D->getLocation();
4108 if (Loc.isInvalid())
4109 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004110
4111 // We only keep track of the file-level declarations of each file.
4112 if (!D->getLexicalDeclContext()->isFileContext())
4113 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004114 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4115 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004116 if (isa<ParmVarDecl>(D))
4117 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004118
4119 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004120 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004121 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004122 FileID FID;
4123 unsigned Offset;
4124 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004125 if (FID.isInvalid())
4126 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004127 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004128
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004129 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004130 if (!Info)
4131 Info = new DeclIDInFileInfo();
4132
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004133 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004134 LocDeclIDsTy &Decls = Info->DeclIDs;
4135
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004136 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004137 Decls.push_back(LocDecl);
4138 return;
4139 }
4140
4141 LocDeclIDsTy::iterator
4142 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4143
4144 Decls.insert(I, LocDecl);
4145}
4146
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004147void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004148 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004149 Record.push_back(Name.getNameKind());
4150 switch (Name.getNameKind()) {
4151 case DeclarationName::Identifier:
4152 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4153 break;
4154
4155 case DeclarationName::ObjCZeroArgSelector:
4156 case DeclarationName::ObjCOneArgSelector:
4157 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004158 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004159 break;
4160
4161 case DeclarationName::CXXConstructorName:
4162 case DeclarationName::CXXDestructorName:
4163 case DeclarationName::CXXConversionFunctionName:
4164 AddTypeRef(Name.getCXXNameType(), Record);
4165 break;
4166
4167 case DeclarationName::CXXOperatorName:
4168 Record.push_back(Name.getCXXOverloadedOperator());
4169 break;
4170
Sean Hunt3e518bd2009-11-29 07:34:05 +00004171 case DeclarationName::CXXLiteralOperatorName:
4172 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4173 break;
4174
Douglas Gregor2cf26342009-04-09 22:27:44 +00004175 case DeclarationName::CXXUsingDirective:
4176 // No extra data to emit
4177 break;
4178 }
4179}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004180
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004181void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004182 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004183 switch (Name.getNameKind()) {
4184 case DeclarationName::CXXConstructorName:
4185 case DeclarationName::CXXDestructorName:
4186 case DeclarationName::CXXConversionFunctionName:
4187 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4188 break;
4189
4190 case DeclarationName::CXXOperatorName:
4191 AddSourceLocation(
4192 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4193 Record);
4194 AddSourceLocation(
4195 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4196 Record);
4197 break;
4198
4199 case DeclarationName::CXXLiteralOperatorName:
4200 AddSourceLocation(
4201 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4202 Record);
4203 break;
4204
4205 case DeclarationName::Identifier:
4206 case DeclarationName::ObjCZeroArgSelector:
4207 case DeclarationName::ObjCOneArgSelector:
4208 case DeclarationName::ObjCMultiArgSelector:
4209 case DeclarationName::CXXUsingDirective:
4210 break;
4211 }
4212}
4213
4214void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004215 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004216 AddDeclarationName(NameInfo.getName(), Record);
4217 AddSourceLocation(NameInfo.getLoc(), Record);
4218 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4219}
4220
4221void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004222 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004223 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004224 Record.push_back(Info.NumTemplParamLists);
4225 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4226 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4227}
4228
Sebastian Redla4232eb2010-08-18 23:56:21 +00004229void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004230 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004231 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004232 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004233 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004234
4235 // Push each of the NNS's onto a stack for serialization in reverse order.
4236 while (NNS) {
4237 NestedNames.push_back(NNS);
4238 NNS = NNS->getPrefix();
4239 }
4240
4241 Record.push_back(NestedNames.size());
4242 while(!NestedNames.empty()) {
4243 NNS = NestedNames.pop_back_val();
4244 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4245 Record.push_back(Kind);
4246 switch (Kind) {
4247 case NestedNameSpecifier::Identifier:
4248 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4249 break;
4250
4251 case NestedNameSpecifier::Namespace:
4252 AddDeclRef(NNS->getAsNamespace(), Record);
4253 break;
4254
Douglas Gregor14aba762011-02-24 02:36:08 +00004255 case NestedNameSpecifier::NamespaceAlias:
4256 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4257 break;
4258
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004259 case NestedNameSpecifier::TypeSpec:
4260 case NestedNameSpecifier::TypeSpecWithTemplate:
4261 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4262 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4263 break;
4264
4265 case NestedNameSpecifier::Global:
4266 // Don't need to write an associated value.
4267 break;
4268 }
4269 }
4270}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004271
Douglas Gregordc355712011-02-25 00:36:19 +00004272void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4273 RecordDataImpl &Record) {
4274 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004275 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004276 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004277
4278 // Push each of the nested-name-specifiers's onto a stack for
4279 // serialization in reverse order.
4280 while (NNS) {
4281 NestedNames.push_back(NNS);
4282 NNS = NNS.getPrefix();
4283 }
4284
4285 Record.push_back(NestedNames.size());
4286 while(!NestedNames.empty()) {
4287 NNS = NestedNames.pop_back_val();
4288 NestedNameSpecifier::SpecifierKind Kind
4289 = NNS.getNestedNameSpecifier()->getKind();
4290 Record.push_back(Kind);
4291 switch (Kind) {
4292 case NestedNameSpecifier::Identifier:
4293 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4294 AddSourceRange(NNS.getLocalSourceRange(), Record);
4295 break;
4296
4297 case NestedNameSpecifier::Namespace:
4298 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4299 AddSourceRange(NNS.getLocalSourceRange(), Record);
4300 break;
4301
4302 case NestedNameSpecifier::NamespaceAlias:
4303 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4304 AddSourceRange(NNS.getLocalSourceRange(), Record);
4305 break;
4306
4307 case NestedNameSpecifier::TypeSpec:
4308 case NestedNameSpecifier::TypeSpecWithTemplate:
4309 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4310 AddTypeLoc(NNS.getTypeLoc(), Record);
4311 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4312 break;
4313
4314 case NestedNameSpecifier::Global:
4315 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4316 break;
4317 }
4318 }
4319}
4320
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004321void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004322 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004323 Record.push_back(Kind);
4324 switch (Kind) {
4325 case TemplateName::Template:
4326 AddDeclRef(Name.getAsTemplateDecl(), Record);
4327 break;
4328
4329 case TemplateName::OverloadedTemplate: {
4330 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4331 Record.push_back(OvT->size());
4332 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4333 I != E; ++I)
4334 AddDeclRef(*I, Record);
4335 break;
4336 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004337
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004338 case TemplateName::QualifiedTemplate: {
4339 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4340 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4341 Record.push_back(QualT->hasTemplateKeyword());
4342 AddDeclRef(QualT->getTemplateDecl(), Record);
4343 break;
4344 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004345
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004346 case TemplateName::DependentTemplate: {
4347 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4348 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4349 Record.push_back(DepT->isIdentifier());
4350 if (DepT->isIdentifier())
4351 AddIdentifierRef(DepT->getIdentifier(), Record);
4352 else
4353 Record.push_back(DepT->getOperator());
4354 break;
4355 }
John McCall14606042011-06-30 08:33:18 +00004356
4357 case TemplateName::SubstTemplateTemplateParm: {
4358 SubstTemplateTemplateParmStorage *subst
4359 = Name.getAsSubstTemplateTemplateParm();
4360 AddDeclRef(subst->getParameter(), Record);
4361 AddTemplateName(subst->getReplacement(), Record);
4362 break;
4363 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004364
4365 case TemplateName::SubstTemplateTemplateParmPack: {
4366 SubstTemplateTemplateParmPackStorage *SubstPack
4367 = Name.getAsSubstTemplateTemplateParmPack();
4368 AddDeclRef(SubstPack->getParameterPack(), Record);
4369 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4370 break;
4371 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004372 }
4373}
4374
Michael J. Spencer20249a12010-10-21 03:16:25 +00004375void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004376 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004377 Record.push_back(Arg.getKind());
4378 switch (Arg.getKind()) {
4379 case TemplateArgument::Null:
4380 break;
4381 case TemplateArgument::Type:
4382 AddTypeRef(Arg.getAsType(), Record);
4383 break;
4384 case TemplateArgument::Declaration:
4385 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004386 Record.push_back(Arg.isDeclForReferenceParam());
4387 break;
4388 case TemplateArgument::NullPtr:
4389 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004390 break;
4391 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004392 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004393 AddTypeRef(Arg.getIntegralType(), Record);
4394 break;
4395 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004396 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4397 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004398 case TemplateArgument::TemplateExpansion:
4399 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004400 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4401 Record.push_back(*NumExpansions + 1);
4402 else
4403 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004404 break;
4405 case TemplateArgument::Expression:
4406 AddStmt(Arg.getAsExpr());
4407 break;
4408 case TemplateArgument::Pack:
4409 Record.push_back(Arg.pack_size());
4410 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4411 I != E; ++I)
4412 AddTemplateArgument(*I, Record);
4413 break;
4414 }
4415}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004416
4417void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004418ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004419 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004420 assert(TemplateParams && "No TemplateParams!");
4421 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4422 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4423 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4424 Record.push_back(TemplateParams->size());
4425 for (TemplateParameterList::const_iterator
4426 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4427 P != PEnd; ++P)
4428 AddDeclRef(*P, Record);
4429}
4430
4431/// \brief Emit a template argument list.
4432void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004433ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004434 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004435 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004436 Record.push_back(TemplateArgs->size());
4437 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004438 AddTemplateArgument(TemplateArgs->get(i), Record);
4439}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004440
4441
4442void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004443ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004444 Record.push_back(Set.size());
4445 for (UnresolvedSetImpl::const_iterator
4446 I = Set.begin(), E = Set.end(); I != E; ++I) {
4447 AddDeclRef(I.getDecl(), Record);
4448 Record.push_back(I.getAccess());
4449 }
4450}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004451
Sebastian Redla4232eb2010-08-18 23:56:21 +00004452void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004453 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004454 Record.push_back(Base.isVirtual());
4455 Record.push_back(Base.isBaseOfClass());
4456 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004457 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004458 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004459 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004460 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4461 : SourceLocation(),
4462 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004463}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004464
Douglas Gregor7c789c12010-10-29 22:39:52 +00004465void ASTWriter::FlushCXXBaseSpecifiers() {
4466 RecordData Record;
4467 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4468 Record.clear();
4469
4470 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004471 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004472 if (Index == CXXBaseSpecifiersOffsets.size())
4473 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4474 else {
4475 if (Index > CXXBaseSpecifiersOffsets.size())
4476 CXXBaseSpecifiersOffsets.resize(Index + 1);
4477 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4478 }
4479
4480 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4481 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4482 Record.push_back(BEnd - B);
4483 for (; B != BEnd; ++B)
4484 AddCXXBaseSpecifier(*B, Record);
4485 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004486
4487 // Flush any expressions that were written as part of the base specifiers.
4488 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004489 }
4490
4491 CXXBaseSpecifiersToWrite.clear();
4492}
4493
Sean Huntcbb67482011-01-08 20:30:50 +00004494void ASTWriter::AddCXXCtorInitializers(
4495 const CXXCtorInitializer * const *CtorInitializers,
4496 unsigned NumCtorInitializers,
4497 RecordDataImpl &Record) {
4498 Record.push_back(NumCtorInitializers);
4499 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4500 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004501
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004502 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004503 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004504 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004505 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004506 } else if (Init->isDelegatingInitializer()) {
4507 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004508 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004509 } else if (Init->isMemberInitializer()){
4510 Record.push_back(CTOR_INITIALIZER_MEMBER);
4511 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004512 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004513 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4514 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004515 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004516
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004517 AddSourceLocation(Init->getMemberLocation(), Record);
4518 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004519 AddSourceLocation(Init->getLParenLoc(), Record);
4520 AddSourceLocation(Init->getRParenLoc(), Record);
4521 Record.push_back(Init->isWritten());
4522 if (Init->isWritten()) {
4523 Record.push_back(Init->getSourceOrder());
4524 } else {
4525 Record.push_back(Init->getNumArrayIndices());
4526 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4527 AddDeclRef(Init->getArrayIndex(i), Record);
4528 }
4529 }
4530}
4531
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004532void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4533 assert(D->DefinitionData);
4534 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004535 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004536 Record.push_back(Data.UserDeclaredConstructor);
4537 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004538 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004539 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004540 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004541 Record.push_back(Data.UserDeclaredDestructor);
4542 Record.push_back(Data.Aggregate);
4543 Record.push_back(Data.PlainOldData);
4544 Record.push_back(Data.Empty);
4545 Record.push_back(Data.Polymorphic);
4546 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004547 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004548 Record.push_back(Data.HasNoNonEmptyBases);
4549 Record.push_back(Data.HasPrivateFields);
4550 Record.push_back(Data.HasProtectedFields);
4551 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004552 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004553 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004554 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004555 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004556 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004557 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004558 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004559 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004560 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004561 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004562 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004563 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004564 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004565 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004566 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004567 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004568 Record.push_back(Data.DeclaredDefaultConstructor);
4569 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004570 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004571 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004572 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004573 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004574 Record.push_back(Data.FailedImplicitMoveConstructor);
4575 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004576 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004577
4578 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004579 if (Data.NumBases > 0)
4580 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4581 Record);
4582
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004583 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4584 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004585 if (Data.NumVBases > 0)
4586 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4587 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004588
4589 AddUnresolvedSet(Data.Conversions, Record);
4590 AddUnresolvedSet(Data.VisibleConversions, Record);
4591 // Data.Definition is the owning decl, no need to write it.
4592 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004593
4594 // Add lambda-specific data.
4595 if (Data.IsLambda) {
4596 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004597 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004598 Record.push_back(Lambda.NumCaptures);
4599 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004600 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004601 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004602 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004603 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4604 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4605 AddSourceLocation(Capture.getLocation(), Record);
4606 Record.push_back(Capture.isImplicit());
4607 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4608 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4609 AddDeclRef(Var, Record);
4610 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4611 : SourceLocation(),
4612 Record);
4613 }
4614 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004615}
4616
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004617void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004618 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004619 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004620 assert(FirstDeclID == NextDeclID &&
4621 FirstTypeID == NextTypeID &&
4622 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004623 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004624 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004625 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004626 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004627
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004628 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004629
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004630 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4631 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4632 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004633 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004634 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004635 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004636 NextDeclID = FirstDeclID;
4637 NextTypeID = FirstTypeID;
4638 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004639 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004640 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004641 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004642}
4643
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004644void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004645 IdentifierIDs[II] = ID;
4646}
4647
Douglas Gregora8235d62012-10-09 23:05:51 +00004648void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4649 MacroIDs[MI] = ID;
4650}
4651
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004652void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004653 // Always take the highest-numbered type index. This copes with an interesting
4654 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004655 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004656 // keep the higher-numbered entry so that we can properly write it out to
4657 // the AST file.
4658 TypeIdx &StoredIdx = TypeIdxs[T];
4659 if (Idx.getIndex() >= StoredIdx.getIndex())
4660 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004661}
4662
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004663void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004664 SelectorIDs[S] = ID;
4665}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004666
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004667void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004668 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004669 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004670 MacroDefinitions[MD] = ID;
4671}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004672
Douglas Gregora015cab2011-12-02 17:30:13 +00004673void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4674 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4675 SubmoduleIDs[Mod] = ID;
4676}
4677
Douglas Gregora8235d62012-10-09 23:05:51 +00004678void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4679 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4680}
4681
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004682void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004683 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004684 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004685 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4686 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004687 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004688 // A forward reference was mutated into a definition. Rewrite it.
4689 // FIXME: This happens during template instantiation, should we
4690 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004691 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004692 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004693 }
4694}
Douglas Gregora8235d62012-10-09 23:05:51 +00004695
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004696void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004697 assert(!WritingAST && "Already writing the AST!");
4698
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004699 // TU and namespaces are handled elsewhere.
4700 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4701 return;
4702
Douglas Gregor919814d2011-09-09 23:01:35 +00004703 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004704 return; // Not a source decl added to a DeclContext from PCH.
4705
4706 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004707 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004708}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004709
4710void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004711 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004712 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004713 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004714 return; // Not a source member added to a class from PCH.
4715 if (!isa<CXXMethodDecl>(D))
4716 return; // We are interested in lazily declared implicit methods.
4717
4718 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004719 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004720 UpdateRecord &Record = DeclUpdates[RD];
4721 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004722 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004723}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004724
4725void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4726 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004727 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004728 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004729 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004730 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004731 return; // Not a source specialization added to a template from PCH.
4732
4733 UpdateRecord &Record = DeclUpdates[TD];
4734 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004735 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004736}
Douglas Gregor89d99802010-11-30 06:16:57 +00004737
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004738void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4739 const FunctionDecl *D) {
4740 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004741 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004742 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004743 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004744 return; // Not a source specialization added to a template from PCH.
4745
4746 UpdateRecord &Record = DeclUpdates[TD];
4747 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004748 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004749}
4750
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004751void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004752 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004753 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004754 return; // Declaration not imported from PCH.
4755
4756 // Implicit decl from a PCH was defined.
4757 // FIXME: Should implicit definition be a separate FunctionDecl?
4758 RewriteDecl(D);
4759}
4760
Sebastian Redlf79a7192011-04-29 08:19:30 +00004761void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004762 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004763 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004764 return;
4765
4766 // Since the actual instantiation is delayed, this really means that we need
4767 // to update the instantiation location.
4768 UpdateRecord &Record = DeclUpdates[D];
4769 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4770 AddSourceLocation(
4771 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4772}
4773
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004774void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4775 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004776 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004777 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004778 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004779
4780 assert(IFD->getDefinition() && "Category on a class without a definition?");
4781 ObjCClassesWithCategories.insert(
4782 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004783}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004784
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004785
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004786void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4787 const ObjCPropertyDecl *OrigProp,
4788 const ObjCCategoryDecl *ClassExt) {
4789 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4790 if (!D)
4791 return;
4792
4793 assert(!WritingAST && "Already writing the AST!");
4794 if (!D->isFromASTFile())
4795 return; // Declaration not imported from PCH.
4796
4797 RewriteDecl(D);
4798}