blob: 801615280f3be0f8c940767eb824539c5ca5c8c7 [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 Gregor7ae467f2012-10-18 18:27:37 +0000779
Douglas Gregorc337fef2012-10-19 00:45:00 +0000780 BLOCK(INPUT_FILES_BLOCK);
781 RECORD(INPUT_FILE);
782
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000783 // AST Top-Level Block.
784 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000785 RECORD(TYPE_OFFSET);
786 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000787 RECORD(IDENTIFIER_OFFSET);
788 RECORD(IDENTIFIER_TABLE);
789 RECORD(EXTERNAL_DEFINITIONS);
790 RECORD(SPECIAL_TYPES);
791 RECORD(STATISTICS);
792 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000793 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000794 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
795 RECORD(SELECTOR_OFFSETS);
796 RECORD(METHOD_POOL);
797 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000798 RECORD(SOURCE_LOCATION_OFFSETS);
799 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000800 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000801 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000802 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000803 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000804 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000805 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000806 RECORD(SEMA_DECL_REFS);
807 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
808 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
809 RECORD(DECL_REPLACEMENTS);
810 RECORD(UPDATE_VISIBLE);
811 RECORD(DECL_UPDATE_OFFSETS);
812 RECORD(DECL_UPDATES);
813 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
814 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000815 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000816 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000817 RECORD(FP_PRAGMA_OPTIONS);
818 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000819 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000820 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000821 RECORD(MODULE_OFFSET_MAP);
822 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000823 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000824 RECORD(FILE_SORTED_DECLS);
825 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000826 RECORD(MERGED_DECLARATIONS);
827 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000828 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000829 RECORD(MACRO_OFFSET);
830 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000831
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000832 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000833 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000834 RECORD(SM_SLOC_FILE_ENTRY);
835 RECORD(SM_SLOC_BUFFER_ENTRY);
836 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000837 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000839 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000840 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000841 RECORD(PP_MACRO_OBJECT_LIKE);
842 RECORD(PP_MACRO_FUNCTION_LIKE);
843 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000844
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000845 // Decls and Types block.
846 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000847 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000848 RECORD(TYPE_COMPLEX);
849 RECORD(TYPE_POINTER);
850 RECORD(TYPE_BLOCK_POINTER);
851 RECORD(TYPE_LVALUE_REFERENCE);
852 RECORD(TYPE_RVALUE_REFERENCE);
853 RECORD(TYPE_MEMBER_POINTER);
854 RECORD(TYPE_CONSTANT_ARRAY);
855 RECORD(TYPE_INCOMPLETE_ARRAY);
856 RECORD(TYPE_VARIABLE_ARRAY);
857 RECORD(TYPE_VECTOR);
858 RECORD(TYPE_EXT_VECTOR);
859 RECORD(TYPE_FUNCTION_PROTO);
860 RECORD(TYPE_FUNCTION_NO_PROTO);
861 RECORD(TYPE_TYPEDEF);
862 RECORD(TYPE_TYPEOF_EXPR);
863 RECORD(TYPE_TYPEOF);
864 RECORD(TYPE_RECORD);
865 RECORD(TYPE_ENUM);
866 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000867 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000868 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000869 RECORD(TYPE_DECLTYPE);
870 RECORD(TYPE_ELABORATED);
871 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
872 RECORD(TYPE_UNRESOLVED_USING);
873 RECORD(TYPE_INJECTED_CLASS_NAME);
874 RECORD(TYPE_OBJC_OBJECT);
875 RECORD(TYPE_TEMPLATE_TYPE_PARM);
876 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
877 RECORD(TYPE_DEPENDENT_NAME);
878 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
879 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
880 RECORD(TYPE_PAREN);
881 RECORD(TYPE_PACK_EXPANSION);
882 RECORD(TYPE_ATTRIBUTED);
883 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000884 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000885 RECORD(DECL_TYPEDEF);
886 RECORD(DECL_ENUM);
887 RECORD(DECL_RECORD);
888 RECORD(DECL_ENUM_CONSTANT);
889 RECORD(DECL_FUNCTION);
890 RECORD(DECL_OBJC_METHOD);
891 RECORD(DECL_OBJC_INTERFACE);
892 RECORD(DECL_OBJC_PROTOCOL);
893 RECORD(DECL_OBJC_IVAR);
894 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000895 RECORD(DECL_OBJC_CATEGORY);
896 RECORD(DECL_OBJC_CATEGORY_IMPL);
897 RECORD(DECL_OBJC_IMPLEMENTATION);
898 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
899 RECORD(DECL_OBJC_PROPERTY);
900 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000901 RECORD(DECL_FIELD);
902 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000903 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000904 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000905 RECORD(DECL_FILE_SCOPE_ASM);
906 RECORD(DECL_BLOCK);
907 RECORD(DECL_CONTEXT_LEXICAL);
908 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000909 RECORD(DECL_NAMESPACE);
910 RECORD(DECL_NAMESPACE_ALIAS);
911 RECORD(DECL_USING);
912 RECORD(DECL_USING_SHADOW);
913 RECORD(DECL_USING_DIRECTIVE);
914 RECORD(DECL_UNRESOLVED_USING_VALUE);
915 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
916 RECORD(DECL_LINKAGE_SPEC);
917 RECORD(DECL_CXX_RECORD);
918 RECORD(DECL_CXX_METHOD);
919 RECORD(DECL_CXX_CONSTRUCTOR);
920 RECORD(DECL_CXX_DESTRUCTOR);
921 RECORD(DECL_CXX_CONVERSION);
922 RECORD(DECL_ACCESS_SPEC);
923 RECORD(DECL_FRIEND);
924 RECORD(DECL_FRIEND_TEMPLATE);
925 RECORD(DECL_CLASS_TEMPLATE);
926 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
927 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
928 RECORD(DECL_FUNCTION_TEMPLATE);
929 RECORD(DECL_TEMPLATE_TYPE_PARM);
930 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
931 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
932 RECORD(DECL_STATIC_ASSERT);
933 RECORD(DECL_CXX_BASE_SPECIFIERS);
934 RECORD(DECL_INDIRECTFIELD);
935 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
936
Douglas Gregora72d8c42011-06-03 02:27:19 +0000937 // Statements and Exprs can occur in the Decls and Types block.
938 AddStmtsExprs(Stream, Record);
939
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000940 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000941 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000942 RECORD(PPD_MACRO_DEFINITION);
943 RECORD(PPD_INCLUSION_DIRECTIVE);
944
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000945#undef RECORD
946#undef BLOCK
947 Stream.ExitBlock();
948}
949
Douglas Gregore650c8c2009-07-07 00:12:59 +0000950/// \brief Adjusts the given filename to only write out the portion of the
951/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000952///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000953/// \param Filename the file name to adjust.
954///
955/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
956/// the returned filename will be adjusted by this system root.
957///
958/// \returns either the original filename (if it needs no adjustment) or the
959/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000960static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000961adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Douglas Gregor832d6202011-07-22 16:35:34 +0000964 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000965 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 // Verify that the filename and the system root have the same prefix.
968 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000969 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 if (Filename[Pos] != isysroot[Pos])
971 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 // We hit the end of the filename before we hit the end of the system root.
974 if (!Filename[Pos])
975 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 // If the file name has a '/' at the current position, skip over the '/'.
978 // We distinguish sysroot-based includes from absolute includes by the
979 // absence of '/' at the beginning of sysroot-based includes.
980 if (Filename[Pos] == '/')
981 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 return Filename + Pos;
984}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000985
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000986/// \brief Write the control block.
987void ASTWriter::WriteControlBlock(ASTContext &Context, StringRef isysroot,
988 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000989 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000990 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
991 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000992
Douglas Gregore650c8c2009-07-07 00:12:59 +0000993 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000994 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
995 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
996 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
997 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
998 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
999 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1000 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1001 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1002 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1003 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1004 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001005 Record.push_back(VERSION_MAJOR);
1006 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001007 Record.push_back(CLANG_VERSION_MAJOR);
1008 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001009 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001010 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001011 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1012 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001013
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001014 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001015 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001016 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1017 llvm::SmallVector<char, 128> ModulePaths;
1018 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001019
1020 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1021 M != MEnd; ++M) {
1022 // Skip modules that weren't directly imported.
1023 if (!(*M)->isDirectlyImported())
1024 continue;
1025
1026 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1027 // FIXME: Write import location, once it matters.
1028 // FIXME: This writes the absolute path for AST files we depend on.
1029 const std::string &FileName = (*M)->FileName;
1030 Record.push_back(FileName.size());
1031 Record.append(FileName.begin(), FileName.end());
1032 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001033 Stream.EmitRecord(IMPORTS, Record);
1034 }
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001036 // Language options.
1037 Record.clear();
1038 const LangOptions &LangOpts = Context.getLangOpts();
1039#define LANGOPT(Name, Bits, Default, Description) \
1040 Record.push_back(LangOpts.Name);
1041#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1042 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1043#include "clang/Basic/LangOptions.def"
1044
1045 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1046 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1047
1048 Record.push_back(LangOpts.CurrentModule.size());
1049 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1050 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1051
Douglas Gregoree097c12012-10-18 17:58:09 +00001052 // Target options.
1053 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001054 const TargetInfo &Target = Context.getTargetInfo();
1055 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001056 AddString(TargetOpts.Triple, Record);
1057 AddString(TargetOpts.CPU, Record);
1058 AddString(TargetOpts.ABI, Record);
1059 AddString(TargetOpts.CXXABI, Record);
1060 AddString(TargetOpts.LinkerVersion, Record);
1061 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1062 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1063 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1064 }
1065 Record.push_back(TargetOpts.Features.size());
1066 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1067 AddString(TargetOpts.Features[I], Record);
1068 }
1069 Stream.EmitRecord(TARGET_OPTIONS, Record);
1070
Douglas Gregor31d375f2011-05-06 21:43:30 +00001071 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001072 SourceManager &SM = Context.getSourceManager();
1073 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1074 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001075 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1076 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001077 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1078 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1079
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001080 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001082 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001083
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001084 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001085 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001086 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001087 RecordData Record;
Douglas Gregor39c497b2012-10-18 18:36:53 +00001088 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001089 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001090 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
1091 Record.clear();
Douglas Gregorb64c1932009-05-12 01:31:05 +00001092 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001093
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001094 // Original PCH directory
1095 if (!OutputFile.empty() && OutputFile != "-") {
1096 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1097 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1099 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1100
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001101 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001102
1103 llvm::sys::fs::make_absolute(OutputPath);
1104 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1105
1106 RecordData Record;
1107 Record.push_back(ORIGINAL_PCH_DIR);
1108 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1109 }
1110
Douglas Gregor745e6f12012-10-19 00:38:02 +00001111 WriteInputFiles(Context.SourceMgr, isysroot);
1112 Stream.ExitBlock();
1113}
1114
1115void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1116 using namespace llvm;
1117 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1118 RecordData Record;
1119
1120 // Create input-file abbreviation.
1121 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1122 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001123 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001124 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1125 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001126 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001127 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1128 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1129
1130 // Write out all of the input files.
1131 std::vector<uint32_t> InputFileOffsets;
1132 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1133 // Get this source location entry.
1134 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001135 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001136
1137 // We only care about file entries that were not overridden.
1138 if (!SLoc->isFile())
1139 continue;
1140 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001141 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001142 continue;
1143
Douglas Gregora930dc92012-10-22 18:42:04 +00001144 // Record this entry's offset.
1145 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1146 InputFileIDs[Cache->OrigEntry] = InputFileOffsets.size();
1147
Douglas Gregor745e6f12012-10-19 00:38:02 +00001148 Record.clear();
1149 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001150 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001151
1152 // Emit size/modification time for this file.
1153 Record.push_back(Cache->OrigEntry->getSize());
1154 Record.push_back(Cache->OrigEntry->getModificationTime());
1155
Douglas Gregora930dc92012-10-22 18:42:04 +00001156 // Whether this file was overridden.
1157 Record.push_back(Cache->BufferOverridden);
1158
Douglas Gregor745e6f12012-10-19 00:38:02 +00001159 // Turn the file name into an absolute path, if it isn't already.
1160 const char *Filename = Cache->OrigEntry->getName();
1161 SmallString<128> FilePath(Filename);
1162
1163 // Ask the file manager to fixup the relative path for us. This will
1164 // honor the working directory.
1165 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1166
1167 // FIXME: This call to make_absolute shouldn't be necessary, the
1168 // call to FixupRelativePath should always return an absolute path.
1169 llvm::sys::fs::make_absolute(FilePath);
1170 Filename = FilePath.c_str();
1171
1172 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1173
1174 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1175 }
1176
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001177 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001178
1179 // Create input file offsets abbreviation.
1180 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1181 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1182 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1183 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1184 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1185
1186 // Write input file offsets.
1187 Record.clear();
1188 Record.push_back(INPUT_FILE_OFFSETS);
1189 Record.push_back(InputFileOffsets.size());
1190 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001191}
1192
Douglas Gregor14f79002009-04-10 03:52:48 +00001193//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001194// stat cache Serialization
1195//===----------------------------------------------------------------------===//
1196
1197namespace {
1198// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001199class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001200public:
1201 typedef const char * key_type;
1202 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Chris Lattner74e976b2010-11-23 19:28:12 +00001204 typedef struct stat data_type;
1205 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001206
1207 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001208 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
1211 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001212 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001213 data_type_ref Data) {
1214 unsigned StrLen = strlen(path);
1215 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001216 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001217 clang::io::Emit8(Out, DataLen);
1218 return std::make_pair(StrLen + 1, DataLen);
1219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Chris Lattner5f9e2722011-07-23 10:55:15 +00001221 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001222 Out.write(path, KeyLen);
1223 }
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Chris Lattner5f9e2722011-07-23 10:55:15 +00001225 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001226 data_type_ref Data, unsigned DataLen) {
1227 using namespace clang::io;
1228 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Chris Lattner74e976b2010-11-23 19:28:12 +00001230 Emit32(Out, (uint32_t) Data.st_ino);
1231 Emit32(Out, (uint32_t) Data.st_dev);
1232 Emit16(Out, (uint16_t) Data.st_mode);
1233 Emit64(Out, (uint64_t) Data.st_mtime);
1234 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001235
1236 assert(Out.tell() - Start == DataLen && "Wrong data length");
1237 }
1238};
1239} // end anonymous namespace
1240
Sebastian Redl3397c552010-08-18 23:56:27 +00001241/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001242void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001243 // Build the on-disk hash table containing information about every
1244 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001245 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001246 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001247 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001248 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001249 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001250 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001251 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001252 }
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001254 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001255 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001256 uint32_t BucketOffset;
1257 {
1258 llvm::raw_svector_ostream Out(StatCacheData);
1259 // Make sure that no bucket is at offset 0
1260 clang::io::Emit32(Out, 0);
1261 BucketOffset = Generator.Emit(Out);
1262 }
1263
1264 // Create a blob abbreviation
1265 using namespace llvm;
1266 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001267 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1271 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1272
1273 // Write the stat cache
1274 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001275 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001276 Record.push_back(BucketOffset);
1277 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001278 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001279}
1280
1281//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001282// Source Manager Serialization
1283//===----------------------------------------------------------------------===//
1284
1285/// \brief Create an abbreviation for the SLocEntry that refers to a
1286/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001287static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001288 using namespace llvm;
1289 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001290 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001295 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001300 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001301}
1302
1303/// \brief Create an abbreviation for the SLocEntry that refers to a
1304/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001305static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001306 using namespace llvm;
1307 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001308 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001309 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001314 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001315}
1316
1317/// \brief Create an abbreviation for the SLocEntry that refers to a
1318/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001319static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001320 using namespace llvm;
1321 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001322 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001324 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001325}
1326
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001327/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1328/// expansion.
1329static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001330 using namespace llvm;
1331 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001332 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1335 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001338 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001339}
1340
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001341namespace {
1342 // Trait used for the on-disk hash table of header search information.
1343 class HeaderFileInfoTrait {
1344 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001345
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001346 // Keep track of the framework names we've used during serialization.
1347 SmallVector<char, 128> FrameworkStringData;
1348 llvm::StringMap<unsigned> FrameworkNameOffset;
1349
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001350 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001351 HeaderFileInfoTrait(ASTWriter &Writer)
1352 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001353
1354 typedef const char *key_type;
1355 typedef key_type key_type_ref;
1356
1357 typedef HeaderFileInfo data_type;
1358 typedef const data_type &data_type_ref;
1359
1360 static unsigned ComputeHash(const char *path) {
1361 // The hash is based only on the filename portion of the key, so that the
1362 // reader can match based on filenames when symlinking or excess path
1363 // elements ("foo/../", "../") change the form of the name. However,
1364 // complete path is still the key.
1365 return llvm::HashString(llvm::sys::path::filename(path));
1366 }
1367
1368 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001369 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001370 data_type_ref Data) {
1371 unsigned StrLen = strlen(path);
1372 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001373 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001374 clang::io::Emit8(Out, DataLen);
1375 return std::make_pair(StrLen + 1, DataLen);
1376 }
1377
Chris Lattner5f9e2722011-07-23 10:55:15 +00001378 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001379 Out.write(path, KeyLen);
1380 }
1381
Chris Lattner5f9e2722011-07-23 10:55:15 +00001382 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001383 data_type_ref Data, unsigned DataLen) {
1384 using namespace clang::io;
1385 uint64_t Start = Out.tell(); (void)Start;
1386
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001387 unsigned char Flags = (Data.isImport << 5)
1388 | (Data.isPragmaOnce << 4)
1389 | (Data.DirInfo << 2)
1390 | (Data.Resolved << 1)
1391 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001392 Emit8(Out, (uint8_t)Flags);
1393 Emit16(Out, (uint16_t) Data.NumIncludes);
1394
1395 if (!Data.ControllingMacro)
1396 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1397 else
1398 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001399
1400 unsigned Offset = 0;
1401 if (!Data.Framework.empty()) {
1402 // If this header refers into a framework, save the framework name.
1403 llvm::StringMap<unsigned>::iterator Pos
1404 = FrameworkNameOffset.find(Data.Framework);
1405 if (Pos == FrameworkNameOffset.end()) {
1406 Offset = FrameworkStringData.size() + 1;
1407 FrameworkStringData.append(Data.Framework.begin(),
1408 Data.Framework.end());
1409 FrameworkStringData.push_back(0);
1410
1411 FrameworkNameOffset[Data.Framework] = Offset;
1412 } else
1413 Offset = Pos->second;
1414 }
1415 Emit32(Out, Offset);
1416
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001417 assert(Out.tell() - Start == DataLen && "Wrong data length");
1418 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001419
1420 const char *strings_begin() const { return FrameworkStringData.begin(); }
1421 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001422 };
1423} // end anonymous namespace
1424
1425/// \brief Write the header search block for the list of files that
1426///
1427/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001428void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001429 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001430 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1431
1432 if (FilesByUID.size() > HS.header_file_size())
1433 FilesByUID.resize(HS.header_file_size());
1434
Benjamin Kramerfacde172012-06-06 17:32:50 +00001435 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001436 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001437 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001438 unsigned NumHeaderSearchEntries = 0;
1439 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1440 const FileEntry *File = FilesByUID[UID];
1441 if (!File)
1442 continue;
1443
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001444 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1445 // from the external source if it was not provided already.
1446 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001447 if (HFI.External && Chain)
1448 continue;
1449
1450 // Turn the file name into an absolute path, if it isn't already.
1451 const char *Filename = File->getName();
1452 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1453
1454 // If we performed any translation on the file name at all, we need to
1455 // save this string, since the generator will refer to it later.
1456 if (Filename != File->getName()) {
1457 Filename = strdup(Filename);
1458 SavedStrings.push_back(Filename);
1459 }
1460
1461 Generator.insert(Filename, HFI, GeneratorTrait);
1462 ++NumHeaderSearchEntries;
1463 }
1464
1465 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001466 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001467 uint32_t BucketOffset;
1468 {
1469 llvm::raw_svector_ostream Out(TableData);
1470 // Make sure that no bucket is at offset 0
1471 clang::io::Emit32(Out, 0);
1472 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1473 }
1474
1475 // Create a blob abbreviation
1476 using namespace llvm;
1477 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1478 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001481 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001482 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1483 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1484
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001485 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001486 RecordData Record;
1487 Record.push_back(HEADER_SEARCH_TABLE);
1488 Record.push_back(BucketOffset);
1489 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001490 Record.push_back(TableData.size());
1491 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001492 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1493
1494 // Free all of the strings we had to duplicate.
1495 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1496 free((void*)SavedStrings[I]);
1497}
1498
Douglas Gregor14f79002009-04-10 03:52:48 +00001499/// \brief Writes the block containing the serialized form of the
1500/// source manager.
1501///
1502/// TODO: We should probably use an on-disk hash table (stored in a
1503/// blob), indexed based on the file name, so that we only create
1504/// entries for files that we actually need. In the common case (no
1505/// errors), we probably won't have to create file entries for any of
1506/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001507void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001508 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001509 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001510 RecordData Record;
1511
Chris Lattnerf04ad692009-04-10 17:16:57 +00001512 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001513 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001514
1515 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001516 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1517 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1518 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001519 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001520
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001521 // Write out the source location entry table. We skip the first
1522 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001523 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001524 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001525 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1526 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001527 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001528 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001529 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001530 FileID FID = FileID::get(I);
1531 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001532
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001533 // Record the offset of this source-location entry.
1534 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1535
1536 // Figure out which record code to use.
1537 unsigned Code;
1538 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001539 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1540 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001541 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001542 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001543 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001544 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001545 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001546 Record.clear();
1547 Record.push_back(Code);
1548
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001549 // Starting offset of this entry within this module, so skip the dummy.
1550 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001551 if (SLoc->isFile()) {
1552 const SrcMgr::FileInfo &File = SLoc->getFile();
1553 Record.push_back(File.getIncludeLoc().getRawEncoding());
1554 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1555 Record.push_back(File.hasLineDirectives());
1556
1557 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001558 if (Content->OrigEntry) {
1559 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001560 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001561
Douglas Gregora930dc92012-10-22 18:42:04 +00001562 // The source location entry is a file. Emit input file ID.
1563 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1564 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001566 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001567
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001568 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001569 if (FDI != FileDeclIDs.end()) {
1570 Record.push_back(FDI->second->FirstDeclIndex);
1571 Record.push_back(FDI->second->DeclIDs.size());
1572 } else {
1573 Record.push_back(0);
1574 Record.push_back(0);
1575 }
Douglas Gregora081da52011-11-16 20:05:18 +00001576
Douglas Gregora930dc92012-10-22 18:42:04 +00001577 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001578
1579 if (Content->BufferOverridden) {
1580 Record.clear();
1581 Record.push_back(SM_SLOC_BUFFER_BLOB);
1582 const llvm::MemoryBuffer *Buffer
1583 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1584 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1585 StringRef(Buffer->getBufferStart(),
1586 Buffer->getBufferSize() + 1));
1587 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001588 } else {
1589 // The source location entry is a buffer. The blob associated
1590 // with this entry contains the contents of the buffer.
1591
1592 // We add one to the size so that we capture the trailing NULL
1593 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1594 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001595 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001596 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001597 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001598 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001599 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001600 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001601 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001602 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001603 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001604 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001605
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001606 if (strcmp(Name, "<built-in>") == 0) {
1607 PreloadSLocs.push_back(SLocEntryOffsets.size());
1608 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001609 }
1610 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001611 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001612 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001613 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1614 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001615 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1616 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001617
1618 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001619 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001620 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001621 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001622 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001623 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001624 }
1625 }
1626
Douglas Gregorc9490c02009-04-16 22:23:12 +00001627 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001628
1629 if (SLocEntryOffsets.empty())
1630 return;
1631
Sebastian Redl3397c552010-08-18 23:56:27 +00001632 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001633 // table is used for lazily loading source-location information.
1634 using namespace llvm;
1635 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001636 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1640 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001642 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001643 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001644 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001645 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001646 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001647
Sebastian Redl3397c552010-08-18 23:56:27 +00001648 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001649 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001650 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001651
1652 // Write the line table. It depends on remapping working, so it must come
1653 // after the source location offsets.
1654 if (SourceMgr.hasLineTable()) {
1655 LineTableInfo &LineTable = SourceMgr.getLineTable();
1656
1657 Record.clear();
1658 // Emit the file names
1659 Record.push_back(LineTable.getNumFilenames());
1660 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1661 // Emit the file name
1662 const char *Filename = LineTable.getFilename(I);
1663 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1664 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1665 Record.push_back(FilenameLen);
1666 if (FilenameLen)
1667 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1668 }
1669
1670 // Emit the line entries
1671 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1672 L != LEnd; ++L) {
1673 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001674 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001675 continue;
1676
1677 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001678 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001679
1680 // Emit the line entries
1681 Record.push_back(L->second.size());
1682 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1683 LEEnd = L->second.end();
1684 LE != LEEnd; ++LE) {
1685 Record.push_back(LE->FileOffset);
1686 Record.push_back(LE->LineNo);
1687 Record.push_back(LE->FilenameID);
1688 Record.push_back((unsigned)LE->FileKind);
1689 Record.push_back(LE->IncludeOffset);
1690 }
1691 }
1692 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1693 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001694}
1695
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001696//===----------------------------------------------------------------------===//
1697// Preprocessor Serialization
1698//===----------------------------------------------------------------------===//
1699
Douglas Gregor9c736102011-02-10 18:20:09 +00001700static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1701 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1702 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1703 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1704 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1705 return X.first->getName().compare(Y.first->getName());
1706}
1707
Chris Lattner0b1fb982009-04-10 17:15:23 +00001708/// \brief Writes the block containing the serialized form of the
1709/// preprocessor.
1710///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001711void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001712 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1713 if (PPRec)
1714 WritePreprocessorDetail(*PPRec);
1715
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001716 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001717
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001718 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1719 if (PP.getCounterValue() != 0) {
1720 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001721 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001722 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001723 }
1724
1725 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001726 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Sebastian Redl3397c552010-08-18 23:56:27 +00001728 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001729 // FIXME: use diagnostics subsystem for localization etc.
1730 if (PP.SawDateOrTime())
1731 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Douglas Gregorecdcb882010-10-20 22:00:55 +00001733
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001734 // Loop over all the macro definitions that are live at the end of the file,
1735 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001736
Douglas Gregor9c736102011-02-10 18:20:09 +00001737 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001738 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001739 MacrosToEmit;
1740 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001741 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001742 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001743 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001744 if (!IsModule || I->second->isPublic()) {
1745 MacroDefinitionsSeen.insert(I->first);
1746 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001747 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001748 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001749
Douglas Gregor9c736102011-02-10 18:20:09 +00001750 // Sort the set of macro definitions that need to be serialized by the
1751 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001752 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001753 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001754
Douglas Gregora8235d62012-10-09 23:05:51 +00001755 /// \brief Offsets of each of the macros into the bitstream, indexed by
1756 /// the local macro ID
1757 ///
1758 /// For each identifier that is associated with a macro, this map
1759 /// provides the offset into the bitstream where that macro is
1760 /// defined.
1761 std::vector<uint32_t> MacroOffsets;
1762
Douglas Gregor9c736102011-02-10 18:20:09 +00001763 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1764 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001765
Douglas Gregora8235d62012-10-09 23:05:51 +00001766 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1767 MI = MI->getPreviousDefinition()) {
1768 MacroID ID = getMacroRef(MI);
1769 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001770 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Douglas Gregora8235d62012-10-09 23:05:51 +00001772 // Skip macros from a AST file if we're chaining.
1773 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1774 continue;
1775
1776 if (ID < FirstMacroID) {
1777 // This will have been dealt with via an update record.
1778 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1779 continue;
1780 }
1781
1782 // Record the local offset of this macro.
1783 unsigned Index = ID - FirstMacroID;
1784 if (Index == MacroOffsets.size())
1785 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1786 else {
1787 if (Index > MacroOffsets.size())
1788 MacroOffsets.resize(Index + 1);
1789
1790 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1791 }
1792
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001793 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001794 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001795 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001796 AddSourceLocation(MI->getDefinitionLoc(), Record);
1797 AddSourceLocation(MI->getUndefLoc(), Record);
1798 Record.push_back(MI->isUsed());
1799 Record.push_back(MI->isPublic());
1800 AddSourceLocation(MI->getVisibilityLocation(), Record);
1801 unsigned Code;
1802 if (MI->isObjectLike()) {
1803 Code = PP_MACRO_OBJECT_LIKE;
1804 } else {
1805 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001806
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001807 Record.push_back(MI->isC99Varargs());
1808 Record.push_back(MI->isGNUVarargs());
1809 Record.push_back(MI->getNumArgs());
1810 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1811 I != E; ++I)
1812 AddIdentifierRef(*I, Record);
1813 }
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001815 // If we have a detailed preprocessing record, record the macro definition
1816 // ID that corresponds to this macro.
1817 if (PPRec)
1818 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1819
1820 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001821 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001822
1823 // Emit the tokens array.
1824 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1825 // Note that we know that the preprocessor does not have any annotation
1826 // tokens in it because they are created by the parser, and thus can't
1827 // be in a macro definition.
1828 const Token &Tok = MI->getReplacementToken(TokNo);
1829
1830 Record.push_back(Tok.getLocation().getRawEncoding());
1831 Record.push_back(Tok.getLength());
1832
1833 // FIXME: When reading literal tokens, reconstruct the literal pointer
1834 // if it is needed.
1835 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1836 // FIXME: Should translate token kind to a stable encoding.
1837 Record.push_back(Tok.getKind());
1838 // FIXME: Should translate token flags to a stable encoding.
1839 Record.push_back(Tok.getFlags());
1840
1841 Stream.EmitRecord(PP_TOKEN, Record);
1842 Record.clear();
1843 }
1844 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001845 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001846 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001847 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001848
1849 // Write the offsets table for macro IDs.
1850 using namespace llvm;
1851 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1852 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1853 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1854 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1855 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1856
1857 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1858 Record.clear();
1859 Record.push_back(MACRO_OFFSET);
1860 Record.push_back(MacroOffsets.size());
1861 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1862 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1863 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001864}
1865
1866void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001867 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001868 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001869
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001870 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001871
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001872 // Enter the preprocessor block.
1873 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001874
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001875 // If the preprocessor has a preprocessing record, emit it.
1876 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001877 using namespace llvm;
1878
1879 // Set up the abbreviation for
1880 unsigned InclusionAbbrev = 0;
1881 {
1882 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1883 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1889 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1890 }
1891
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001892 unsigned FirstPreprocessorEntityID
1893 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1894 + NUM_PREDEF_PP_ENTITY_IDS;
1895 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001896 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001897 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1898 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001899 E != EEnd;
1900 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001901 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001902
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001903 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1904 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001905
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001906 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001907 // Record this macro definition's ID.
1908 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001909
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001910 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001911 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1912 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001913 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001914
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001915 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001916 Record.push_back(ME->isBuiltinMacro());
1917 if (ME->isBuiltinMacro())
1918 AddIdentifierRef(ME->getName(), Record);
1919 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001920 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001921 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001922 continue;
1923 }
1924
1925 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1926 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001927 Record.push_back(ID->getFileName().size());
1928 Record.push_back(ID->wasInQuotes());
1929 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001930 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001931 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001932 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001933 // Check that the FileEntry is not null because it was not resolved and
1934 // we create a PCH even with compiler errors.
1935 if (ID->getFile())
1936 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001937 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1938 continue;
1939 }
1940
1941 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1942 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001943 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001944
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001945 // Write the offsets table for the preprocessing record.
1946 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001947 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1948
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001949 // Write the offsets table for identifier IDs.
1950 using namespace llvm;
1951 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001952 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001955 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001956
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001957 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001958 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001959 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001960 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1961 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001962 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001963}
1964
Douglas Gregore209e502011-12-06 01:10:29 +00001965unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1966 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1967 if (Known != SubmoduleIDs.end())
1968 return Known->second;
1969
1970 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1971}
1972
Douglas Gregor26ced122011-12-01 00:59:36 +00001973/// \brief Compute the number of modules within the given tree (including the
1974/// given module).
1975static unsigned getNumberOfModules(Module *Mod) {
1976 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001977 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1978 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001979 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001980 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001981
1982 return ChildModules + 1;
1983}
1984
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001985void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001986 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001987 // FIXME: This feels like it belongs somewhere else, but there are no
1988 // other consumers of this information.
1989 SourceManager &SrcMgr = PP->getSourceManager();
1990 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1991 for (ASTContext::import_iterator I = Context->local_import_begin(),
1992 IEnd = Context->local_import_end();
1993 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001994 if (Module *ImportedFrom
1995 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1996 SrcMgr))) {
1997 ImportedFrom->Imports.push_back(I->getImportedModule());
1998 }
1999 }
2000
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002001 // Enter the submodule description block.
2002 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2003
2004 // Write the abbreviations needed for the submodules block.
2005 using namespace llvm;
2006 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2007 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2017 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2018
2019 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002020 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2022 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2023
2024 Abbrev = new BitCodeAbbrev();
2025 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2027 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002028
2029 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002030 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2032 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2033
2034 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002035 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2037 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2038
Douglas Gregor51f564f2011-12-31 04:05:44 +00002039 Abbrev = new BitCodeAbbrev();
2040 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2042 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2043
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002044 Abbrev = new BitCodeAbbrev();
2045 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2047 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2048
Douglas Gregor26ced122011-12-01 00:59:36 +00002049 // Write the submodule metadata block.
2050 RecordData Record;
2051 Record.push_back(getNumberOfModules(WritingModule));
2052 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2053 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2054
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002055 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002056 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002057 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002058 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002059 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002060 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002061 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002062
2063 // Emit the definition of the block.
2064 Record.clear();
2065 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002066 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002067 if (Mod->Parent) {
2068 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2069 Record.push_back(SubmoduleIDs[Mod->Parent]);
2070 } else {
2071 Record.push_back(0);
2072 }
2073 Record.push_back(Mod->IsFramework);
2074 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002075 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002076 Record.push_back(Mod->InferSubmodules);
2077 Record.push_back(Mod->InferExplicitSubmodules);
2078 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002079 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2080
Douglas Gregor51f564f2011-12-31 04:05:44 +00002081 // Emit the requirements.
2082 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2083 Record.clear();
2084 Record.push_back(SUBMODULE_REQUIRES);
2085 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2086 Mod->Requires[I].data(),
2087 Mod->Requires[I].size());
2088 }
2089
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002090 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002091 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002092 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002093 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002094 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002095 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002096 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2097 Record.clear();
2098 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2099 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2100 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002101 }
2102
2103 // Emit the headers.
2104 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2105 Record.clear();
2106 Record.push_back(SUBMODULE_HEADER);
2107 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2108 Mod->Headers[I]->getName());
2109 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002110 // Emit the excluded headers.
2111 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2112 Record.clear();
2113 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2114 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2115 Mod->ExcludedHeaders[I]->getName());
2116 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002117 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2118 Record.clear();
2119 Record.push_back(SUBMODULE_TOPHEADER);
2120 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2121 Mod->TopHeaders[I]->getName());
2122 }
Douglas Gregor55988682011-12-05 16:33:54 +00002123
2124 // Emit the imports.
2125 if (!Mod->Imports.empty()) {
2126 Record.clear();
2127 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002128 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002129 assert(ImportedID && "Unknown submodule!");
2130 Record.push_back(ImportedID);
2131 }
2132 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2133 }
2134
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002135 // Emit the exports.
2136 if (!Mod->Exports.empty()) {
2137 Record.clear();
2138 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002139 if (Module *Exported = Mod->Exports[I].getPointer()) {
2140 unsigned ExportedID = SubmoduleIDs[Exported];
2141 assert(ExportedID > 0 && "Unknown submodule ID?");
2142 Record.push_back(ExportedID);
2143 } else {
2144 Record.push_back(0);
2145 }
2146
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002147 Record.push_back(Mod->Exports[I].getInt());
2148 }
2149 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2150 }
2151
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002152 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002153 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2154 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002155 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002156 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002157 }
2158
2159 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002160
2161 assert((NextSubmoduleID - FirstSubmoduleID
2162 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002163}
2164
Douglas Gregor185dbd72011-12-01 02:07:58 +00002165serialization::SubmoduleID
2166ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002167 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002168 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002169
2170 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002171 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002172 Module *OwningMod
2173 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002174 if (!OwningMod)
2175 return 0;
2176
Douglas Gregore209e502011-12-06 01:10:29 +00002177 // Check whether this submodule is part of our own module.
2178 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002179 return 0;
2180
Douglas Gregore209e502011-12-06 01:10:29 +00002181 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002182}
2183
David Blaikied6471f72011-09-25 23:23:43 +00002184void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002185 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002186 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002187 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2188 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002189 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002190 if (point.Loc.isInvalid())
2191 continue;
2192
2193 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002194 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002195 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002196 if (I->second.isPragma()) {
2197 Record.push_back(I->first);
2198 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002199 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002200 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002201 Record.push_back(-1); // mark the end of the diag/map pairs for this
2202 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002203 }
2204
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002205 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002206 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002207}
2208
Anders Carlssonc8505782011-03-06 18:41:18 +00002209void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2210 if (CXXBaseSpecifiersOffsets.empty())
2211 return;
2212
2213 RecordData Record;
2214
2215 // Create a blob abbreviation for the C++ base specifiers offsets.
2216 using namespace llvm;
2217
2218 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2219 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2222 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2223
Douglas Gregore92b8a12011-08-04 00:01:48 +00002224 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002225 Record.clear();
2226 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2227 Record.push_back(CXXBaseSpecifiersOffsets.size());
2228 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002229 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002230}
2231
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002232//===----------------------------------------------------------------------===//
2233// Type Serialization
2234//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002235
Sebastian Redl3397c552010-08-18 23:56:27 +00002236/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002237void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002238 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002239 if (Idx.getIndex() == 0) // we haven't seen this type before.
2240 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Douglas Gregor97475832010-10-05 18:37:06 +00002242 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002243
Douglas Gregor2cf26342009-04-09 22:27:44 +00002244 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002245 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002246 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002247 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002248 else if (TypeOffsets.size() < Index) {
2249 TypeOffsets.resize(Index + 1);
2250 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002251 }
2252
2253 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Douglas Gregor2cf26342009-04-09 22:27:44 +00002255 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002256 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002257
Douglas Gregora4923eb2009-11-16 21:35:15 +00002258 if (T.hasLocalNonFastQualifiers()) {
2259 Qualifiers Qs = T.getLocalQualifiers();
2260 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002261 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002262 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002263 } else {
2264 switch (T->getTypeClass()) {
2265 // For all of the concrete, non-dependent types, call the
2266 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002267#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002268 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002269#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002270#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002271 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002272 }
2273
2274 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002275 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002276
2277 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002278 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002279}
2280
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002281//===----------------------------------------------------------------------===//
2282// Declaration Serialization
2283//===----------------------------------------------------------------------===//
2284
Douglas Gregor2cf26342009-04-09 22:27:44 +00002285/// \brief Write the block containing all of the declaration IDs
2286/// lexically declared within the given DeclContext.
2287///
2288/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2289/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002290uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002291 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002292 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002293 return 0;
2294
Douglas Gregorc9490c02009-04-16 22:23:12 +00002295 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002296 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002297 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002298 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002299 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2300 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002301 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002302
Douglas Gregor25123082009-04-22 22:34:57 +00002303 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002304 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002305 return Offset;
2306}
2307
Sebastian Redla4232eb2010-08-18 23:56:21 +00002308void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002309 using namespace llvm;
2310 RecordData Record;
2311
2312 // Write the type offsets array
2313 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002314 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2318 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2319 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002320 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002321 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002322 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002323 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002324
2325 // Write the declaration offsets array
2326 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002327 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2331 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2332 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002333 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002334 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002335 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002336 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002337}
2338
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002339void ASTWriter::WriteFileDeclIDsMap() {
2340 using namespace llvm;
2341 RecordData Record;
2342
2343 // Join the vectors of DeclIDs from all files.
2344 SmallVector<DeclID, 256> FileSortedIDs;
2345 for (FileDeclIDsTy::iterator
2346 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2347 DeclIDInFileInfo &Info = *FI->second;
2348 Info.FirstDeclIndex = FileSortedIDs.size();
2349 for (LocDeclIDsTy::iterator
2350 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2351 FileSortedIDs.push_back(DI->second);
2352 }
2353
2354 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2355 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2358 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2359 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002360 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002361 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2362}
2363
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002364void ASTWriter::WriteComments() {
2365 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002366 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002367 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002368 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2369 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002370 I != E; ++I) {
2371 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002372 AddSourceRange((*I)->getSourceRange(), Record);
2373 Record.push_back((*I)->getKind());
2374 Record.push_back((*I)->isTrailingComment());
2375 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002376 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2377 }
2378 Stream.ExitBlock();
2379}
2380
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002381//===----------------------------------------------------------------------===//
2382// Global Method Pool and Selector Serialization
2383//===----------------------------------------------------------------------===//
2384
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002385namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002386// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002387class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002388 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002389
2390public:
2391 typedef Selector key_type;
2392 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002393
Sebastian Redl5d050072010-08-04 17:20:04 +00002394 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002395 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002396 ObjCMethodList Instance, Factory;
2397 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002398 typedef const data_type& data_type_ref;
2399
Sebastian Redl3397c552010-08-18 23:56:27 +00002400 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002401
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002402 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002403 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002404 }
Mike Stump1eb44332009-09-09 15:08:12 +00002405
2406 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002407 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002408 data_type_ref Methods) {
2409 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2410 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002411 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2412 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002413 Method = Method->Next)
2414 if (Method->Method)
2415 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002416 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002417 Method = Method->Next)
2418 if (Method->Method)
2419 DataLen += 4;
2420 clang::io::Emit16(Out, DataLen);
2421 return std::make_pair(KeyLen, DataLen);
2422 }
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Chris Lattner5f9e2722011-07-23 10:55:15 +00002424 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002425 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002426 assert((Start >> 32) == 0 && "Selector key offset too large");
2427 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002428 unsigned N = Sel.getNumArgs();
2429 clang::io::Emit16(Out, N);
2430 if (N == 0)
2431 N = 1;
2432 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002433 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002434 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2435 }
Mike Stump1eb44332009-09-09 15:08:12 +00002436
Chris Lattner5f9e2722011-07-23 10:55:15 +00002437 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002438 data_type_ref Methods, unsigned DataLen) {
2439 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002440 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002441 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002442 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002443 Method = Method->Next)
2444 if (Method->Method)
2445 ++NumInstanceMethods;
2446
2447 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002448 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002449 Method = Method->Next)
2450 if (Method->Method)
2451 ++NumFactoryMethods;
2452
2453 clang::io::Emit16(Out, NumInstanceMethods);
2454 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002455 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002456 Method = Method->Next)
2457 if (Method->Method)
2458 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002459 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002460 Method = Method->Next)
2461 if (Method->Method)
2462 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002463
2464 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002465 }
2466};
2467} // end anonymous namespace
2468
Sebastian Redl059612d2010-08-03 21:58:15 +00002469/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002470///
2471/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002472/// in an on-disk hash table indexed by the selector. The hash table also
2473/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002474void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002475 using namespace llvm;
2476
Sebastian Redl059612d2010-08-03 21:58:15 +00002477 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002478 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002479 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002480 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002481 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002482 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002483 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002484 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002485
Sebastian Redl059612d2010-08-03 21:58:15 +00002486 // Create the on-disk hash table representation. We walk through every
2487 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002488 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002489 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002490 I = SelectorIDs.begin(), E = SelectorIDs.end();
2491 I != E; ++I) {
2492 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002493 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002494 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002495 I->second,
2496 ObjCMethodList(),
2497 ObjCMethodList()
2498 };
2499 if (F != SemaRef.MethodPool.end()) {
2500 Data.Instance = F->second.first;
2501 Data.Factory = F->second.second;
2502 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002503 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002504 // changed.
2505 if (Chain && I->second < FirstSelectorID) {
2506 // Selector already exists. Did it change?
2507 bool changed = false;
2508 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2509 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002510 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002511 changed = true;
2512 }
2513 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2514 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002515 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002516 changed = true;
2517 }
2518 if (!changed)
2519 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002520 } else if (Data.Instance.Method || Data.Factory.Method) {
2521 // A new method pool entry.
2522 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002523 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002524 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002525 }
2526
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002527 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002528 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002529 uint32_t BucketOffset;
2530 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002531 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002532 llvm::raw_svector_ostream Out(MethodPool);
2533 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002534 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002535 BucketOffset = Generator.Emit(Out, Trait);
2536 }
2537
2538 // Create a blob abbreviation
2539 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002540 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2544 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2545
Douglas Gregor83941df2009-04-25 17:48:32 +00002546 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002547 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002548 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002549 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002550 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002551 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002552
2553 // Create a blob abbreviation for the selector table offsets.
2554 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002555 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2559 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2560
2561 // Write the selector offsets table.
2562 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002563 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002564 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002565 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002566 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002567 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002568 }
2569}
2570
Sebastian Redl3397c552010-08-18 23:56:27 +00002571/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002572void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002573 using namespace llvm;
2574 if (SemaRef.ReferencedSelectors.empty())
2575 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002576
Fariborz Jahanian32019832010-07-23 19:11:11 +00002577 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002578
Sebastian Redl3397c552010-08-18 23:56:27 +00002579 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002580 // very tricky to fix, and given that @selector shouldn't really appear in
2581 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002582 for (DenseMap<Selector, SourceLocation>::iterator S =
2583 SemaRef.ReferencedSelectors.begin(),
2584 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2585 Selector Sel = (*S).first;
2586 SourceLocation Loc = (*S).second;
2587 AddSelectorRef(Sel, Record);
2588 AddSourceLocation(Loc, Record);
2589 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002590 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002591}
2592
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002593//===----------------------------------------------------------------------===//
2594// Identifier Table Serialization
2595//===----------------------------------------------------------------------===//
2596
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002597namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002598class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002599 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002600 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002601 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002602 bool IsModule;
2603
Douglas Gregora92193e2009-04-28 21:18:29 +00002604 /// \brief Determines whether this is an "interesting" identifier
2605 /// that needs a full IdentifierInfo structure written into the hash
2606 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002607 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002608 if (II->isPoisoned() ||
2609 II->isExtensionToken() ||
2610 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002611 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002612 II->getFETokenInfo<void>())
2613 return true;
2614
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002615 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002616 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002617
2618 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2619 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002620 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002621
2622 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002623 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002624
2625 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002626 }
2627
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002628public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002629 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002630 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002631
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002632 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002633 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002634
Douglas Gregoreee242f2011-10-27 09:33:13 +00002635 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2636 IdentifierResolver &IdResolver, bool IsModule)
2637 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002638
2639 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002640 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002641 }
Mike Stump1eb44332009-09-09 15:08:12 +00002642
2643 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002644 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002645 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002646 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002647 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002648 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002649 DataLen += 2; // 2 bytes for builtin ID
2650 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002651 if (hadMacroDefinition(II, Macro)) {
2652 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2653 if (Writer.getMacroRef(M) != 0)
2654 DataLen += 4;
2655 }
2656
2657 DataLen += 4;
2658 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002659
Douglas Gregoreee242f2011-10-27 09:33:13 +00002660 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2661 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002662 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002663 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002664 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002665 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002666 // We emit the key length after the data length so that every
2667 // string is preceded by a 16-bit length. This matches the PTH
2668 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002669 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002670 return std::make_pair(KeyLen, DataLen);
2671 }
Mike Stump1eb44332009-09-09 15:08:12 +00002672
Chris Lattner5f9e2722011-07-23 10:55:15 +00002673 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002674 unsigned KeyLen) {
2675 // Record the location of the key data. This is used when generating
2676 // the mapping from persistent IDs to strings.
2677 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002678 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002679 }
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Douglas Gregor7143aab2011-09-01 17:04:32 +00002681 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002682 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002683 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002684 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002685 clang::io::Emit32(Out, ID << 1);
2686 return;
2687 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002688
Douglas Gregora92193e2009-04-28 21:18:29 +00002689 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002690 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2691 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2692 clang::io::Emit16(Out, Bits);
2693 Bits = 0;
2694 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002695 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002696 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2697 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002698 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002699 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002700 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002701
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002702 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002703 // Write all of the macro IDs associated with this identifier.
2704 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2705 if (MacroID ID = Writer.getMacroRef(M))
2706 clang::io::Emit32(Out, ID);
2707 }
2708
2709 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002710 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002711
Douglas Gregor668c1a42009-04-21 22:25:48 +00002712 // Emit the declaration IDs in reverse order, because the
2713 // IdentifierResolver provides the declarations as they would be
2714 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002715 // "stat"), but the ASTReader adds declarations to the end of the list
2716 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002717 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002718 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2719 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002720 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002721 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002722 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002723 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002724 }
2725};
2726} // end anonymous namespace
2727
Sebastian Redl3397c552010-08-18 23:56:27 +00002728/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002729///
2730/// The identifier table consists of a blob containing string data
2731/// (the actual identifiers themselves) and a separate "offsets" index
2732/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002733void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2734 IdentifierResolver &IdResolver,
2735 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002736 using namespace llvm;
2737
2738 // Create and write out the blob that contains the identifier
2739 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002740 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002741 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002742 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002743
Douglas Gregor92b059e2009-04-28 20:33:11 +00002744 // Look for any identifiers that were named while processing the
2745 // headers, but are otherwise not needed. We add these to the hash
2746 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002747 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002748 // file.
2749 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2750 IDEnd = PP.getIdentifierTable().end();
2751 ID != IDEnd; ++ID)
2752 getIdentifierRef(ID->second);
2753
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002754 // Create the on-disk hash table representation. We only store offsets
2755 // for identifiers that appear here for the first time.
2756 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002757 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002758 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2759 ID != IDEnd; ++ID) {
2760 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002761 if (!Chain || !ID->first->isFromAST() ||
2762 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002763 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2764 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002765 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002766
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002767 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002768 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002769 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002770 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002771 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002772 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002773 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002774 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002775 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002776 }
2777
2778 // Create a blob abbreviation
2779 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002780 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002783 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002784
2785 // Write the identifier table
2786 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002787 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002788 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002789 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002790 }
2791
2792 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002793 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002794 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2798 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2799
2800 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002801 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002802 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002803 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002804 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002805 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002806}
2807
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002808//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002809// DeclContext's Name Lookup Table Serialization
2810//===----------------------------------------------------------------------===//
2811
2812namespace {
2813// Trait used for the on-disk hash table used in the method pool.
2814class ASTDeclContextNameLookupTrait {
2815 ASTWriter &Writer;
2816
2817public:
2818 typedef DeclarationName key_type;
2819 typedef key_type key_type_ref;
2820
2821 typedef DeclContext::lookup_result data_type;
2822 typedef const data_type& data_type_ref;
2823
2824 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2825
2826 unsigned ComputeHash(DeclarationName Name) {
2827 llvm::FoldingSetNodeID ID;
2828 ID.AddInteger(Name.getNameKind());
2829
2830 switch (Name.getNameKind()) {
2831 case DeclarationName::Identifier:
2832 ID.AddString(Name.getAsIdentifierInfo()->getName());
2833 break;
2834 case DeclarationName::ObjCZeroArgSelector:
2835 case DeclarationName::ObjCOneArgSelector:
2836 case DeclarationName::ObjCMultiArgSelector:
2837 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2838 break;
2839 case DeclarationName::CXXConstructorName:
2840 case DeclarationName::CXXDestructorName:
2841 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002842 break;
2843 case DeclarationName::CXXOperatorName:
2844 ID.AddInteger(Name.getCXXOverloadedOperator());
2845 break;
2846 case DeclarationName::CXXLiteralOperatorName:
2847 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2848 case DeclarationName::CXXUsingDirective:
2849 break;
2850 }
2851
2852 return ID.ComputeHash();
2853 }
2854
2855 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002856 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002857 data_type_ref Lookup) {
2858 unsigned KeyLen = 1;
2859 switch (Name.getNameKind()) {
2860 case DeclarationName::Identifier:
2861 case DeclarationName::ObjCZeroArgSelector:
2862 case DeclarationName::ObjCOneArgSelector:
2863 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002864 case DeclarationName::CXXLiteralOperatorName:
2865 KeyLen += 4;
2866 break;
2867 case DeclarationName::CXXOperatorName:
2868 KeyLen += 1;
2869 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002870 case DeclarationName::CXXConstructorName:
2871 case DeclarationName::CXXDestructorName:
2872 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002873 case DeclarationName::CXXUsingDirective:
2874 break;
2875 }
2876 clang::io::Emit16(Out, KeyLen);
2877
2878 // 2 bytes for num of decls and 4 for each DeclID.
2879 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2880 clang::io::Emit16(Out, DataLen);
2881
2882 return std::make_pair(KeyLen, DataLen);
2883 }
2884
Chris Lattner5f9e2722011-07-23 10:55:15 +00002885 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002886 using namespace clang::io;
2887
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002888 Emit8(Out, Name.getNameKind());
2889 switch (Name.getNameKind()) {
2890 case DeclarationName::Identifier:
2891 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002892 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002893 case DeclarationName::ObjCZeroArgSelector:
2894 case DeclarationName::ObjCOneArgSelector:
2895 case DeclarationName::ObjCMultiArgSelector:
2896 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002897 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002898 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002899 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2900 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002901 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002902 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002903 case DeclarationName::CXXLiteralOperatorName:
2904 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002905 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002906 case DeclarationName::CXXConstructorName:
2907 case DeclarationName::CXXDestructorName:
2908 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002909 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002910 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002911 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002912
2913 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002914 }
2915
Chris Lattner5f9e2722011-07-23 10:55:15 +00002916 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002917 data_type Lookup, unsigned DataLen) {
2918 uint64_t Start = Out.tell(); (void)Start;
2919 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2920 for (; Lookup.first != Lookup.second; ++Lookup.first)
2921 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2922
2923 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2924 }
2925};
2926} // end anonymous namespace
2927
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002928/// \brief Write the block containing all of the declaration IDs
2929/// visible from the given DeclContext.
2930///
2931/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002932/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002933uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2934 DeclContext *DC) {
2935 if (DC->getPrimaryContext() != DC)
2936 return 0;
2937
2938 // Since there is no name lookup into functions or methods, don't bother to
2939 // build a visible-declarations table for these entities.
2940 if (DC->isFunctionOrMethod())
2941 return 0;
2942
2943 // If not in C++, we perform name lookup for the translation unit via the
2944 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2945 // FIXME: In C++ we need the visible declarations in order to "see" the
2946 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002947 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002948 return 0;
2949
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002950 // Serialize the contents of the mapping used for lookup. Note that,
2951 // although we have two very different code paths, the serialized
2952 // representation is the same for both cases: a declaration name,
2953 // followed by a size, followed by references to the visible
2954 // declarations that have that name.
2955 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002956 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002957 if (!Map || Map->empty())
2958 return 0;
2959
2960 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2961 ASTDeclContextNameLookupTrait Trait(*this);
2962
2963 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002964 DeclarationName ConversionName;
2965 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002966 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2967 D != DEnd; ++D) {
2968 DeclarationName Name = D->first;
2969 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002970 if (Result.first != Result.second) {
2971 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2972 // Hash all conversion function names to the same name. The actual
2973 // type information in conversion function name is not used in the
2974 // key (since such type information is not stable across different
2975 // modules), so the intended effect is to coalesce all of the conversion
2976 // functions under a single key.
2977 if (!ConversionName)
2978 ConversionName = Name;
2979 ConversionDecls.append(Result.first, Result.second);
2980 continue;
2981 }
2982
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002983 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002984 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002985 }
2986
Douglas Gregore5a54b62011-08-30 20:49:19 +00002987 // Add the conversion functions
2988 if (!ConversionDecls.empty()) {
2989 Generator.insert(ConversionName,
2990 DeclContext::lookup_result(ConversionDecls.begin(),
2991 ConversionDecls.end()),
2992 Trait);
2993 }
2994
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002995 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002996 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002997 uint32_t BucketOffset;
2998 {
2999 llvm::raw_svector_ostream Out(LookupTable);
3000 // Make sure that no bucket is at offset 0
3001 clang::io::Emit32(Out, 0);
3002 BucketOffset = Generator.Emit(Out, Trait);
3003 }
3004
3005 // Write the lookup table
3006 RecordData Record;
3007 Record.push_back(DECL_CONTEXT_VISIBLE);
3008 Record.push_back(BucketOffset);
3009 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3010 LookupTable.str());
3011
3012 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3013 ++NumVisibleDeclContexts;
3014 return Offset;
3015}
3016
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003017/// \brief Write an UPDATE_VISIBLE block for the given context.
3018///
3019/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3020/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003021/// (in C++), for namespaces, and for classes with forward-declared unscoped
3022/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003023void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003024 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3025 if (!Map || Map->empty())
3026 return;
3027
3028 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3029 ASTDeclContextNameLookupTrait Trait(*this);
3030
3031 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003032 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3033 D != DEnd; ++D) {
3034 DeclarationName Name = D->first;
3035 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003036 // For any name that appears in this table, the results are complete, i.e.
3037 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003038 if (Result.first != Result.second)
3039 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003040 }
3041
3042 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003043 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003044 uint32_t BucketOffset;
3045 {
3046 llvm::raw_svector_ostream Out(LookupTable);
3047 // Make sure that no bucket is at offset 0
3048 clang::io::Emit32(Out, 0);
3049 BucketOffset = Generator.Emit(Out, Trait);
3050 }
3051
3052 // Write the lookup table
3053 RecordData Record;
3054 Record.push_back(UPDATE_VISIBLE);
3055 Record.push_back(getDeclID(cast<Decl>(DC)));
3056 Record.push_back(BucketOffset);
3057 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3058}
3059
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003060/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3061void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3062 RecordData Record;
3063 Record.push_back(Opts.fp_contract);
3064 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3065}
3066
3067/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3068void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003069 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003070 return;
3071
3072 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3073 RecordData Record;
3074#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3075#include "clang/Basic/OpenCLExtensions.def"
3076 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3077}
3078
Douglas Gregor2171bf12012-01-15 16:58:34 +00003079void ASTWriter::WriteRedeclarations() {
3080 RecordData LocalRedeclChains;
3081 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3082
3083 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3084 Decl *First = Redeclarations[I];
3085 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3086
3087 Decl *MostRecent = First->getMostRecentDecl();
3088
3089 // If we only have a single declaration, there is no point in storing
3090 // a redeclaration chain.
3091 if (First == MostRecent)
3092 continue;
3093
3094 unsigned Offset = LocalRedeclChains.size();
3095 unsigned Size = 0;
3096 LocalRedeclChains.push_back(0); // Placeholder for the size.
3097
3098 // Collect the set of local redeclarations of this declaration.
3099 for (Decl *Prev = MostRecent; Prev != First;
3100 Prev = Prev->getPreviousDecl()) {
3101 if (!Prev->isFromASTFile()) {
3102 AddDeclRef(Prev, LocalRedeclChains);
3103 ++Size;
3104 }
3105 }
3106 LocalRedeclChains[Offset] = Size;
3107
3108 // Reverse the set of local redeclarations, so that we store them in
3109 // order (since we found them in reverse order).
3110 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3111
3112 // Add the mapping from the first ID to the set of local declarations.
3113 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3114 LocalRedeclsMap.push_back(Info);
3115
3116 assert(N == Redeclarations.size() &&
3117 "Deserialized a declaration we shouldn't have");
3118 }
3119
3120 if (LocalRedeclChains.empty())
3121 return;
3122
3123 // Sort the local redeclarations map by the first declaration ID,
3124 // since the reader will be performing binary searches on this information.
3125 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3126
3127 // Emit the local redeclarations map.
3128 using namespace llvm;
3129 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3130 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3133 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3134
3135 RecordData Record;
3136 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3137 Record.push_back(LocalRedeclsMap.size());
3138 Stream.EmitRecordWithBlob(AbbrevID, Record,
3139 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3140 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3141
3142 // Emit the redeclaration chains.
3143 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3144}
3145
Douglas Gregorcff9f262012-01-27 01:47:08 +00003146void ASTWriter::WriteObjCCategories() {
3147 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3148 RecordData Categories;
3149
3150 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3151 unsigned Size = 0;
3152 unsigned StartIndex = Categories.size();
3153
3154 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3155
3156 // Allocate space for the size.
3157 Categories.push_back(0);
3158
3159 // Add the categories.
3160 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3161 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3162 assert(getDeclID(Cat) != 0 && "Bogus category");
3163 AddDeclRef(Cat, Categories);
3164 }
3165
3166 // Update the size.
3167 Categories[StartIndex] = Size;
3168
3169 // Record this interface -> category map.
3170 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3171 CategoriesMap.push_back(CatInfo);
3172 }
3173
3174 // Sort the categories map by the definition ID, since the reader will be
3175 // performing binary searches on this information.
3176 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3177
3178 // Emit the categories map.
3179 using namespace llvm;
3180 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3181 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3184 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3185
3186 RecordData Record;
3187 Record.push_back(OBJC_CATEGORIES_MAP);
3188 Record.push_back(CategoriesMap.size());
3189 Stream.EmitRecordWithBlob(AbbrevID, Record,
3190 reinterpret_cast<char*>(CategoriesMap.data()),
3191 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3192
3193 // Emit the category lists.
3194 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3195}
3196
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003197void ASTWriter::WriteMergedDecls() {
3198 if (!Chain || Chain->MergedDecls.empty())
3199 return;
3200
3201 RecordData Record;
3202 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3203 IEnd = Chain->MergedDecls.end();
3204 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003205 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003206 : getDeclID(I->first);
3207 assert(CanonID && "Merged declaration not known?");
3208
3209 Record.push_back(CanonID);
3210 Record.push_back(I->second.size());
3211 Record.append(I->second.begin(), I->second.end());
3212 }
3213 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3214}
3215
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003216//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003217// General Serialization Routines
3218//===----------------------------------------------------------------------===//
3219
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003220/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003221void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3222 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003223 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003224 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3225 e = Attrs.end(); i != e; ++i){
3226 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003227 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003228 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003229
Sean Huntcf807c42010-08-18 23:23:40 +00003230#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003231
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003232 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003233}
3234
Chris Lattner5f9e2722011-07-23 10:55:15 +00003235void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003236 Record.push_back(Str.size());
3237 Record.insert(Record.end(), Str.begin(), Str.end());
3238}
3239
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003240void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3241 RecordDataImpl &Record) {
3242 Record.push_back(Version.getMajor());
3243 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3244 Record.push_back(*Minor + 1);
3245 else
3246 Record.push_back(0);
3247 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3248 Record.push_back(*Subminor + 1);
3249 else
3250 Record.push_back(0);
3251}
3252
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003253/// \brief Note that the identifier II occurs at the given offset
3254/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003255void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003256 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003257 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003258 // up earlier in the chain and thus don't need an offset.
3259 if (ID >= FirstIdentID)
3260 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003261}
3262
Douglas Gregor83941df2009-04-25 17:48:32 +00003263/// \brief Note that the selector Sel occurs at the given offset
3264/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003265void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003266 unsigned ID = SelectorIDs[Sel];
3267 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003268 // Don't record offsets for selectors that are also available in a different
3269 // file.
3270 if (ID < FirstSelectorID)
3271 return;
3272 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003273}
3274
Sebastian Redla4232eb2010-08-18 23:56:21 +00003275ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003276 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003277 WritingAST(false), DoneWritingDeclsAndTypes(false),
3278 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003279 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003280 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003281 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3282 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003283 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3284 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003285 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003286 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003287 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003288 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003289 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003290 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003291 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3292 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3293 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003294 DeclTypedefAbbrev(0),
3295 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3296 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003297{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003298}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003299
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003300ASTWriter::~ASTWriter() {
3301 for (FileDeclIDsTy::iterator
3302 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3303 delete I->second;
3304}
3305
Sebastian Redla4232eb2010-08-18 23:56:21 +00003306void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003307 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003308 Module *WritingModule, StringRef isysroot,
3309 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003310 WritingAST = true;
3311
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003312 ASTHasCompilerErrors = hasErrors;
3313
Douglas Gregor2cf26342009-04-09 22:27:44 +00003314 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003315 Stream.Emit((unsigned)'C', 8);
3316 Stream.Emit((unsigned)'P', 8);
3317 Stream.Emit((unsigned)'C', 8);
3318 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003319
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003320 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003321
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003322 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003323 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003324 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003325 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003326 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003327 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003328 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003329
3330 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003331}
3332
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003333template<typename Vector>
3334static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3335 ASTWriter::RecordData &Record) {
3336 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3337 I != E; ++I) {
3338 Writer.AddDeclRef(*I, Record);
3339 }
3340}
3341
Sebastian Redla4232eb2010-08-18 23:56:21 +00003342void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003343 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003344 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003345 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003346 using namespace llvm;
3347
Douglas Gregorecc2c092011-12-01 22:20:10 +00003348 // Make sure that the AST reader knows to finalize itself.
3349 if (Chain)
3350 Chain->finalizeForWriting();
3351
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003352 ASTContext &Context = SemaRef.Context;
3353 Preprocessor &PP = SemaRef.PP;
3354
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003355 // Set up predefined declaration IDs.
3356 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003357 if (Context.ObjCIdDecl)
3358 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003359 if (Context.ObjCSelDecl)
3360 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003361 if (Context.ObjCClassDecl)
3362 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003363 if (Context.ObjCProtocolClassDecl)
3364 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003365 if (Context.Int128Decl)
3366 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3367 if (Context.UInt128Decl)
3368 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003369 if (Context.ObjCInstanceTypeDecl)
3370 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003371 if (Context.BuiltinVaListDecl)
3372 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3373
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003374 if (!Chain) {
3375 // Make sure that we emit IdentifierInfos (and any attached
3376 // declarations) for builtins. We don't need to do this when we're
3377 // emitting chained PCH files, because all of the builtins will be
3378 // in the original PCH file.
3379 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003380 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003381 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003382 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003383 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003384 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3385 getIdentifierRef(&Table.get(BuiltinNames[I]));
3386 }
3387
Douglas Gregoreee242f2011-10-27 09:33:13 +00003388 // If there are any out-of-date identifiers, bring them up to date.
3389 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3390 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3391 IDEnd = PP.getIdentifierTable().end();
3392 ID != IDEnd; ++ID)
3393 if (ID->second->isOutOfDate())
3394 ExtSource->updateOutOfDateIdentifier(*ID->second);
3395 }
3396
Chris Lattner63d65f82009-09-08 18:19:27 +00003397 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003398 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003399 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003400 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003401 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003402
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003403 // Build a record containing all of the file scoped decls in this file.
3404 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003405 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3406 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003407
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003408 // Build a record containing all of the delegating constructors we still need
3409 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003410 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003411 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003412
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003413 // Write the set of weak, undeclared identifiers. We always write the
3414 // entire table, since later PCH files in a PCH chain are only interested in
3415 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003416 RecordData WeakUndeclaredIdentifiers;
3417 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003418 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003419 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3420 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3421 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3422 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3423 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3424 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3425 }
3426 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003427
Douglas Gregor14c22f22009-04-22 22:18:58 +00003428 // Build a record containing all of the locally-scoped external
3429 // declarations in this header file. Generally, this record will be
3430 // empty.
3431 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003432 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003433 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003434 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003435 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3436 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003437 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003438 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003439 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3440 }
3441
Douglas Gregorb81c1702009-04-27 20:06:05 +00003442 // Build a record containing all of the ext_vector declarations.
3443 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003444 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003445
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003446 // Build a record containing all of the VTable uses information.
3447 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003448 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003449 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3450 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3451 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3452 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3453 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003454 }
3455
3456 // Build a record containing all of dynamic classes declarations.
3457 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003458 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003459
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003460 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003461 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003462 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003463 I = SemaRef.PendingInstantiations.begin(),
3464 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3465 AddDeclRef(I->first, PendingInstantiations);
3466 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003467 }
3468 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3469 "There are local ones at end of translation unit!");
3470
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003471 // Build a record containing some declaration references.
3472 RecordData SemaDeclRefs;
3473 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3474 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3475 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3476 }
3477
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003478 RecordData CUDASpecialDeclRefs;
3479 if (Context.getcudaConfigureCallDecl()) {
3480 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3481 }
3482
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003483 // Build a record containing all of the known namespaces.
3484 RecordData KnownNamespaces;
3485 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3486 I = SemaRef.KnownNamespaces.begin(),
3487 IEnd = SemaRef.KnownNamespaces.end();
3488 I != IEnd; ++I) {
3489 if (!I->second)
3490 AddDeclRef(I->first, KnownNamespaces);
3491 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003492
3493 // Write the control block
3494 WriteControlBlock(Context, isysroot, OutputFile);
3495
Sebastian Redl3397c552010-08-18 23:56:27 +00003496 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003497 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003498 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor832d6202011-07-22 16:35:34 +00003499 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003500 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003501
3502 // Create a lexical update block containing all of the declarations in the
3503 // translation unit that do not come from other AST files.
3504 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3505 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3506 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3507 E = TU->noload_decls_end();
3508 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003509 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003510 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003511 }
3512
3513 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3514 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3515 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3516 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3517 Record.clear();
3518 Record.push_back(TU_UPDATE_LEXICAL);
3519 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3520 data(NewGlobalDecls));
3521
3522 // And a visible updates block for the translation unit.
3523 Abv = new llvm::BitCodeAbbrev();
3524 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3525 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3526 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3527 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3528 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3529 WriteDeclContextVisibleUpdate(TU);
3530
3531 // If the translation unit has an anonymous namespace, and we don't already
3532 // have an update block for it, write it as an update block.
3533 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3534 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3535 if (Record.empty()) {
3536 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003537 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003538 }
3539 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003540
3541 // Make sure visible decls, added to DeclContexts previously loaded from
3542 // an AST file, are registered for serialization.
3543 for (SmallVector<const Decl *, 16>::iterator
3544 I = UpdatingVisibleDecls.begin(),
3545 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3546 GetDeclRef(*I);
3547 }
3548
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003549 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003550 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003551
Douglas Gregora119da02011-08-02 16:26:37 +00003552 // Form the record of special types.
3553 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003554 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003555 AddTypeRef(Context.getFILEType(), SpecialTypes);
3556 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3557 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3558 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3559 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003560 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003561 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003562
Douglas Gregor366809a2009-04-26 03:49:13 +00003563 // Keep writing types and declarations until all types and
3564 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003565 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003566 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003567 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3568 E = DeclsToRewrite.end();
3569 I != E; ++I)
3570 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003571 while (!DeclTypesToEmit.empty()) {
3572 DeclOrType DOT = DeclTypesToEmit.front();
3573 DeclTypesToEmit.pop();
3574 if (DOT.isType())
3575 WriteType(DOT.getType());
3576 else
3577 WriteDecl(Context, DOT.getDecl());
3578 }
3579 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003580
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003581 DoneWritingDeclsAndTypes = true;
3582
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003583 WriteFileDeclIDsMap();
3584 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003585 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003586
3587 if (Chain) {
3588 // Write the mapping information describing our module dependencies and how
3589 // each of those modules were mapped into our own offset/ID space, so that
3590 // the reader can build the appropriate mapping to its own offset/ID space.
3591 // The map consists solely of a blob with the following format:
3592 // *(module-name-len:i16 module-name:len*i8
3593 // source-location-offset:i32
3594 // identifier-id:i32
3595 // preprocessed-entity-id:i32
3596 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003597 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003598 // selector-id:i32
3599 // declaration-id:i32
3600 // c++-base-specifiers-id:i32
3601 // type-id:i32)
3602 //
3603 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3604 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3606 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003607 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003608 {
3609 llvm::raw_svector_ostream Out(Buffer);
3610 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003611 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003612 M != MEnd; ++M) {
3613 StringRef FileName = (*M)->FileName;
3614 io::Emit16(Out, FileName.size());
3615 Out.write(FileName.data(), FileName.size());
3616 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3617 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003618 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003619 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003620 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003621 io::Emit32(Out, (*M)->BaseSelectorID);
3622 io::Emit32(Out, (*M)->BaseDeclID);
3623 io::Emit32(Out, (*M)->BaseTypeIndex);
3624 }
3625 }
3626 Record.clear();
3627 Record.push_back(MODULE_OFFSET_MAP);
3628 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3629 Buffer.data(), Buffer.size());
3630 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003631 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003632 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003633 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003634 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003635 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003636 WriteFPPragmaOptions(SemaRef.getFPOptions());
3637 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003638
Sebastian Redl1476ed42010-07-16 16:36:56 +00003639 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003640 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003641
Anders Carlssonc8505782011-03-06 18:41:18 +00003642 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003643
Douglas Gregore209e502011-12-06 01:10:29 +00003644 // If we're emitting a module, write out the submodule information.
3645 if (WritingModule)
3646 WriteSubmodules(WritingModule);
3647
Douglas Gregora119da02011-08-02 16:26:37 +00003648 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3649
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003650 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003651 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003652 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003653
3654 // Write the record containing tentative definitions.
3655 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003656 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003657
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003658 // Write the record containing unused file scoped decls.
3659 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003660 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003661
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003662 // Write the record containing weak undeclared identifiers.
3663 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003664 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003665 WeakUndeclaredIdentifiers);
3666
Douglas Gregor14c22f22009-04-22 22:18:58 +00003667 // Write the record containing locally-scoped external definitions.
3668 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003669 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003670 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003671
3672 // Write the record containing ext_vector type names.
3673 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003674 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003675
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003676 // Write the record containing VTable uses information.
3677 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003678 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003679
3680 // Write the record containing dynamic classes declarations.
3681 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003682 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003683
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003684 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003685 if (!PendingInstantiations.empty())
3686 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003687
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003688 // Write the record containing declaration references of Sema.
3689 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003690 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003691
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003692 // Write the record containing CUDA-specific declaration references.
3693 if (!CUDASpecialDeclRefs.empty())
3694 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003695
3696 // Write the delegating constructors.
3697 if (!DelegatingCtorDecls.empty())
3698 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003699
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003700 // Write the known namespaces.
3701 if (!KnownNamespaces.empty())
3702 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3703
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003704 // Write the visible updates to DeclContexts.
3705 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3706 I = UpdatedDeclContexts.begin(),
3707 E = UpdatedDeclContexts.end();
3708 I != E; ++I)
3709 WriteDeclContextVisibleUpdate(*I);
3710
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003711 if (!WritingModule) {
3712 // Write the submodules that were imported, if any.
3713 RecordData ImportedModules;
3714 for (ASTContext::import_iterator I = Context.local_import_begin(),
3715 IEnd = Context.local_import_end();
3716 I != IEnd; ++I) {
3717 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3718 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3719 }
3720 if (!ImportedModules.empty()) {
3721 // Sort module IDs.
3722 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3723
3724 // Unique module IDs.
3725 ImportedModules.erase(std::unique(ImportedModules.begin(),
3726 ImportedModules.end()),
3727 ImportedModules.end());
3728
3729 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3730 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003731 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003732
3733 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003734 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003735 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003736 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003737 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003738 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003739
Douglas Gregor3e1af842009-04-17 22:13:46 +00003740 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003741 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003742 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003743 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003744 Record.push_back(NumLexicalDeclContexts);
3745 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003746 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003747 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003748}
3749
Douglas Gregora8235d62012-10-09 23:05:51 +00003750void ASTWriter::WriteMacroUpdates() {
3751 if (MacroUpdates.empty())
3752 return;
3753
3754 RecordData Record;
3755 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3756 E = MacroUpdates.end();
3757 I != E; ++I) {
3758 addMacroRef(I->first, Record);
3759 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003760 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003761 }
3762 Stream.EmitRecord(MACRO_UPDATES, Record);
3763}
3764
Douglas Gregor61c5e342011-09-17 00:05:03 +00003765/// \brief Go through the declaration update blocks and resolve declaration
3766/// pointers into declaration IDs.
3767void ASTWriter::ResolveDeclUpdatesBlocks() {
3768 for (DeclUpdateMap::iterator
3769 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3770 const Decl *D = I->first;
3771 UpdateRecord &URec = I->second;
3772
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003773 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003774 continue; // The decl will be written completely
3775
3776 unsigned Idx = 0, N = URec.size();
3777 while (Idx < N) {
3778 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003779 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3780 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3781 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3782 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3783 ++Idx;
3784 break;
3785
3786 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3787 ++Idx;
3788 break;
3789 }
3790 }
3791 }
3792}
3793
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003794void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003795 if (DeclUpdates.empty())
3796 return;
3797
3798 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003799 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003800 for (DeclUpdateMap::iterator
3801 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3802 const Decl *D = I->first;
3803 UpdateRecord &URec = I->second;
3804
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003805 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003806 continue; // The decl will be written completely,no need to store updates.
3807
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003808 uint64_t Offset = Stream.GetCurrentBitNo();
3809 Stream.EmitRecord(DECL_UPDATES, URec);
3810
3811 OffsetsRecord.push_back(GetDeclRef(D));
3812 OffsetsRecord.push_back(Offset);
3813 }
3814 Stream.ExitBlock();
3815 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3816}
3817
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003818void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003819 if (ReplacedDecls.empty())
3820 return;
3821
3822 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003823 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003824 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003825 Record.push_back(I->ID);
3826 Record.push_back(I->Offset);
3827 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003828 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003829 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003830}
3831
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003832void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003833 Record.push_back(Loc.getRawEncoding());
3834}
3835
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003836void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003837 AddSourceLocation(Range.getBegin(), Record);
3838 AddSourceLocation(Range.getEnd(), Record);
3839}
3840
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003841void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003842 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003843 const uint64_t *Words = Value.getRawData();
3844 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003845}
3846
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003847void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003848 Record.push_back(Value.isUnsigned());
3849 AddAPInt(Value, Record);
3850}
3851
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003852void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003853 AddAPInt(Value.bitcastToAPInt(), Record);
3854}
3855
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003856void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003857 Record.push_back(getIdentifierRef(II));
3858}
3859
Douglas Gregora8235d62012-10-09 23:05:51 +00003860void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3861 Record.push_back(getMacroRef(MI));
3862}
3863
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003864IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003865 if (II == 0)
3866 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003867
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003868 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003869 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003870 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003871 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003872}
3873
Douglas Gregora8235d62012-10-09 23:05:51 +00003874MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3875 // Don't emit builtin macros like __LINE__ to the AST file unless they
3876 // have been redefined by the header (in which case they are not
3877 // isBuiltinMacro).
3878 if (MI == 0 || MI->isBuiltinMacro())
3879 return 0;
3880
3881 MacroID &ID = MacroIDs[MI];
3882 if (ID == 0)
3883 ID = NextMacroID++;
3884 return ID;
3885}
3886
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003887void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003888 Record.push_back(getSelectorRef(SelRef));
3889}
3890
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003891SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003892 if (Sel.getAsOpaquePtr() == 0) {
3893 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003894 }
3895
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003896 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003897 if (SID == 0 && Chain) {
3898 // This might trigger a ReadSelector callback, which will set the ID for
3899 // this selector.
3900 Chain->LoadSelector(Sel);
3901 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003902 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003903 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003904 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003905 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003906}
3907
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003908void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003909 AddDeclRef(Temp->getDestructor(), Record);
3910}
3911
Douglas Gregor7c789c12010-10-29 22:39:52 +00003912void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3913 CXXBaseSpecifier const *BasesEnd,
3914 RecordDataImpl &Record) {
3915 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3916 CXXBaseSpecifiersToWrite.push_back(
3917 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3918 Bases, BasesEnd));
3919 Record.push_back(NextCXXBaseSpecifiersID++);
3920}
3921
Sebastian Redla4232eb2010-08-18 23:56:21 +00003922void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003923 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003924 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003925 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003926 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003927 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003928 break;
3929 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003930 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003931 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003932 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003933 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003934 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003935 break;
3936 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003937 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003938 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003939 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003940 break;
John McCall833ca992009-10-29 08:12:44 +00003941 case TemplateArgument::Null:
3942 case TemplateArgument::Integral:
3943 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003944 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003945 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003946 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003947 break;
3948 }
3949}
3950
Sebastian Redla4232eb2010-08-18 23:56:21 +00003951void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003952 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003953 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003954
3955 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3956 bool InfoHasSameExpr
3957 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3958 Record.push_back(InfoHasSameExpr);
3959 if (InfoHasSameExpr)
3960 return; // Avoid storing the same expr twice.
3961 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003962 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3963 Record);
3964}
3965
Douglas Gregordc355712011-02-25 00:36:19 +00003966void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3967 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003968 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003969 AddTypeRef(QualType(), Record);
3970 return;
3971 }
3972
Douglas Gregordc355712011-02-25 00:36:19 +00003973 AddTypeLoc(TInfo->getTypeLoc(), Record);
3974}
3975
3976void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3977 AddTypeRef(TL.getType(), Record);
3978
John McCalla1ee0c52009-10-16 21:56:05 +00003979 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003980 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003981 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003982}
3983
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003984void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003985 Record.push_back(GetOrCreateTypeID(T));
3986}
3987
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003988TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3989 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003990 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3991}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003992
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003993TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003994 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003995 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003996}
3997
3998TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3999 if (T.isNull())
4000 return TypeIdx();
4001 assert(!T.getLocalFastQualifiers());
4002
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004003 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004004 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004005 if (DoneWritingDeclsAndTypes) {
4006 assert(0 && "New type seen after serializing all the types to emit!");
4007 return TypeIdx();
4008 }
4009
Douglas Gregor366809a2009-04-26 03:49:13 +00004010 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004011 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004012 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004013 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004014 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004015 return Idx;
4016}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004017
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004018TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004019 if (T.isNull())
4020 return TypeIdx();
4021 assert(!T.getLocalFastQualifiers());
4022
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004023 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4024 assert(I != TypeIdxs.end() && "Type not emitted!");
4025 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004026}
4027
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004028void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004029 Record.push_back(GetDeclRef(D));
4030}
4031
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004032DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004033 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4034
Douglas Gregor2cf26342009-04-09 22:27:44 +00004035 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004036 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004037 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004038
4039 // If D comes from an AST file, its declaration ID is already known and
4040 // fixed.
4041 if (D->isFromASTFile())
4042 return D->getGlobalID();
4043
Douglas Gregor97475832010-10-05 18:37:06 +00004044 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004045 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004046 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004047 if (DoneWritingDeclsAndTypes) {
4048 assert(0 && "New decl seen after serializing all the decls to emit!");
4049 return 0;
4050 }
4051
Douglas Gregor2cf26342009-04-09 22:27:44 +00004052 // We haven't seen this declaration before. Give it a new ID and
4053 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004054 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004055 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004056 }
4057
Sebastian Redl681d7232010-07-27 00:17:23 +00004058 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004059}
4060
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004061DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004062 if (D == 0)
4063 return 0;
4064
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004065 // If D comes from an AST file, its declaration ID is already known and
4066 // fixed.
4067 if (D->isFromASTFile())
4068 return D->getGlobalID();
4069
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004070 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4071 return DeclIDs[D];
4072}
4073
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004074static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4075 std::pair<unsigned, serialization::DeclID> R) {
4076 return L.first < R.first;
4077}
4078
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004079void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004080 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004081 assert(D);
4082
4083 SourceLocation Loc = D->getLocation();
4084 if (Loc.isInvalid())
4085 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004086
4087 // We only keep track of the file-level declarations of each file.
4088 if (!D->getLexicalDeclContext()->isFileContext())
4089 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004090 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4091 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004092 if (isa<ParmVarDecl>(D))
4093 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004094
4095 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004096 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004097 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004098 FileID FID;
4099 unsigned Offset;
4100 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004101 if (FID.isInvalid())
4102 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004103 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004104
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004105 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004106 if (!Info)
4107 Info = new DeclIDInFileInfo();
4108
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004109 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004110 LocDeclIDsTy &Decls = Info->DeclIDs;
4111
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004112 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004113 Decls.push_back(LocDecl);
4114 return;
4115 }
4116
4117 LocDeclIDsTy::iterator
4118 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4119
4120 Decls.insert(I, LocDecl);
4121}
4122
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004123void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004124 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004125 Record.push_back(Name.getNameKind());
4126 switch (Name.getNameKind()) {
4127 case DeclarationName::Identifier:
4128 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4129 break;
4130
4131 case DeclarationName::ObjCZeroArgSelector:
4132 case DeclarationName::ObjCOneArgSelector:
4133 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004134 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004135 break;
4136
4137 case DeclarationName::CXXConstructorName:
4138 case DeclarationName::CXXDestructorName:
4139 case DeclarationName::CXXConversionFunctionName:
4140 AddTypeRef(Name.getCXXNameType(), Record);
4141 break;
4142
4143 case DeclarationName::CXXOperatorName:
4144 Record.push_back(Name.getCXXOverloadedOperator());
4145 break;
4146
Sean Hunt3e518bd2009-11-29 07:34:05 +00004147 case DeclarationName::CXXLiteralOperatorName:
4148 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4149 break;
4150
Douglas Gregor2cf26342009-04-09 22:27:44 +00004151 case DeclarationName::CXXUsingDirective:
4152 // No extra data to emit
4153 break;
4154 }
4155}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004156
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004157void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004158 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004159 switch (Name.getNameKind()) {
4160 case DeclarationName::CXXConstructorName:
4161 case DeclarationName::CXXDestructorName:
4162 case DeclarationName::CXXConversionFunctionName:
4163 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4164 break;
4165
4166 case DeclarationName::CXXOperatorName:
4167 AddSourceLocation(
4168 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4169 Record);
4170 AddSourceLocation(
4171 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4172 Record);
4173 break;
4174
4175 case DeclarationName::CXXLiteralOperatorName:
4176 AddSourceLocation(
4177 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4178 Record);
4179 break;
4180
4181 case DeclarationName::Identifier:
4182 case DeclarationName::ObjCZeroArgSelector:
4183 case DeclarationName::ObjCOneArgSelector:
4184 case DeclarationName::ObjCMultiArgSelector:
4185 case DeclarationName::CXXUsingDirective:
4186 break;
4187 }
4188}
4189
4190void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004191 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004192 AddDeclarationName(NameInfo.getName(), Record);
4193 AddSourceLocation(NameInfo.getLoc(), Record);
4194 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4195}
4196
4197void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004198 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004199 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004200 Record.push_back(Info.NumTemplParamLists);
4201 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4202 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4203}
4204
Sebastian Redla4232eb2010-08-18 23:56:21 +00004205void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004206 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004207 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004208 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004209 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004210
4211 // Push each of the NNS's onto a stack for serialization in reverse order.
4212 while (NNS) {
4213 NestedNames.push_back(NNS);
4214 NNS = NNS->getPrefix();
4215 }
4216
4217 Record.push_back(NestedNames.size());
4218 while(!NestedNames.empty()) {
4219 NNS = NestedNames.pop_back_val();
4220 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4221 Record.push_back(Kind);
4222 switch (Kind) {
4223 case NestedNameSpecifier::Identifier:
4224 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4225 break;
4226
4227 case NestedNameSpecifier::Namespace:
4228 AddDeclRef(NNS->getAsNamespace(), Record);
4229 break;
4230
Douglas Gregor14aba762011-02-24 02:36:08 +00004231 case NestedNameSpecifier::NamespaceAlias:
4232 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4233 break;
4234
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004235 case NestedNameSpecifier::TypeSpec:
4236 case NestedNameSpecifier::TypeSpecWithTemplate:
4237 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4238 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4239 break;
4240
4241 case NestedNameSpecifier::Global:
4242 // Don't need to write an associated value.
4243 break;
4244 }
4245 }
4246}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004247
Douglas Gregordc355712011-02-25 00:36:19 +00004248void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4249 RecordDataImpl &Record) {
4250 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004251 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004252 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004253
4254 // Push each of the nested-name-specifiers's onto a stack for
4255 // serialization in reverse order.
4256 while (NNS) {
4257 NestedNames.push_back(NNS);
4258 NNS = NNS.getPrefix();
4259 }
4260
4261 Record.push_back(NestedNames.size());
4262 while(!NestedNames.empty()) {
4263 NNS = NestedNames.pop_back_val();
4264 NestedNameSpecifier::SpecifierKind Kind
4265 = NNS.getNestedNameSpecifier()->getKind();
4266 Record.push_back(Kind);
4267 switch (Kind) {
4268 case NestedNameSpecifier::Identifier:
4269 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4270 AddSourceRange(NNS.getLocalSourceRange(), Record);
4271 break;
4272
4273 case NestedNameSpecifier::Namespace:
4274 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4275 AddSourceRange(NNS.getLocalSourceRange(), Record);
4276 break;
4277
4278 case NestedNameSpecifier::NamespaceAlias:
4279 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4280 AddSourceRange(NNS.getLocalSourceRange(), Record);
4281 break;
4282
4283 case NestedNameSpecifier::TypeSpec:
4284 case NestedNameSpecifier::TypeSpecWithTemplate:
4285 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4286 AddTypeLoc(NNS.getTypeLoc(), Record);
4287 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4288 break;
4289
4290 case NestedNameSpecifier::Global:
4291 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4292 break;
4293 }
4294 }
4295}
4296
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004297void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004298 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004299 Record.push_back(Kind);
4300 switch (Kind) {
4301 case TemplateName::Template:
4302 AddDeclRef(Name.getAsTemplateDecl(), Record);
4303 break;
4304
4305 case TemplateName::OverloadedTemplate: {
4306 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4307 Record.push_back(OvT->size());
4308 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4309 I != E; ++I)
4310 AddDeclRef(*I, Record);
4311 break;
4312 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004313
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004314 case TemplateName::QualifiedTemplate: {
4315 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4316 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4317 Record.push_back(QualT->hasTemplateKeyword());
4318 AddDeclRef(QualT->getTemplateDecl(), Record);
4319 break;
4320 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004321
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004322 case TemplateName::DependentTemplate: {
4323 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4324 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4325 Record.push_back(DepT->isIdentifier());
4326 if (DepT->isIdentifier())
4327 AddIdentifierRef(DepT->getIdentifier(), Record);
4328 else
4329 Record.push_back(DepT->getOperator());
4330 break;
4331 }
John McCall14606042011-06-30 08:33:18 +00004332
4333 case TemplateName::SubstTemplateTemplateParm: {
4334 SubstTemplateTemplateParmStorage *subst
4335 = Name.getAsSubstTemplateTemplateParm();
4336 AddDeclRef(subst->getParameter(), Record);
4337 AddTemplateName(subst->getReplacement(), Record);
4338 break;
4339 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004340
4341 case TemplateName::SubstTemplateTemplateParmPack: {
4342 SubstTemplateTemplateParmPackStorage *SubstPack
4343 = Name.getAsSubstTemplateTemplateParmPack();
4344 AddDeclRef(SubstPack->getParameterPack(), Record);
4345 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4346 break;
4347 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004348 }
4349}
4350
Michael J. Spencer20249a12010-10-21 03:16:25 +00004351void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004352 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004353 Record.push_back(Arg.getKind());
4354 switch (Arg.getKind()) {
4355 case TemplateArgument::Null:
4356 break;
4357 case TemplateArgument::Type:
4358 AddTypeRef(Arg.getAsType(), Record);
4359 break;
4360 case TemplateArgument::Declaration:
4361 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004362 Record.push_back(Arg.isDeclForReferenceParam());
4363 break;
4364 case TemplateArgument::NullPtr:
4365 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004366 break;
4367 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004368 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004369 AddTypeRef(Arg.getIntegralType(), Record);
4370 break;
4371 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004372 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4373 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004374 case TemplateArgument::TemplateExpansion:
4375 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004376 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4377 Record.push_back(*NumExpansions + 1);
4378 else
4379 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004380 break;
4381 case TemplateArgument::Expression:
4382 AddStmt(Arg.getAsExpr());
4383 break;
4384 case TemplateArgument::Pack:
4385 Record.push_back(Arg.pack_size());
4386 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4387 I != E; ++I)
4388 AddTemplateArgument(*I, Record);
4389 break;
4390 }
4391}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004392
4393void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004394ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004395 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004396 assert(TemplateParams && "No TemplateParams!");
4397 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4398 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4399 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4400 Record.push_back(TemplateParams->size());
4401 for (TemplateParameterList::const_iterator
4402 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4403 P != PEnd; ++P)
4404 AddDeclRef(*P, Record);
4405}
4406
4407/// \brief Emit a template argument list.
4408void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004409ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004410 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004411 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004412 Record.push_back(TemplateArgs->size());
4413 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004414 AddTemplateArgument(TemplateArgs->get(i), Record);
4415}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004416
4417
4418void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004419ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004420 Record.push_back(Set.size());
4421 for (UnresolvedSetImpl::const_iterator
4422 I = Set.begin(), E = Set.end(); I != E; ++I) {
4423 AddDeclRef(I.getDecl(), Record);
4424 Record.push_back(I.getAccess());
4425 }
4426}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004427
Sebastian Redla4232eb2010-08-18 23:56:21 +00004428void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004429 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004430 Record.push_back(Base.isVirtual());
4431 Record.push_back(Base.isBaseOfClass());
4432 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004433 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004434 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004435 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004436 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4437 : SourceLocation(),
4438 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004439}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004440
Douglas Gregor7c789c12010-10-29 22:39:52 +00004441void ASTWriter::FlushCXXBaseSpecifiers() {
4442 RecordData Record;
4443 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4444 Record.clear();
4445
4446 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004447 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004448 if (Index == CXXBaseSpecifiersOffsets.size())
4449 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4450 else {
4451 if (Index > CXXBaseSpecifiersOffsets.size())
4452 CXXBaseSpecifiersOffsets.resize(Index + 1);
4453 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4454 }
4455
4456 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4457 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4458 Record.push_back(BEnd - B);
4459 for (; B != BEnd; ++B)
4460 AddCXXBaseSpecifier(*B, Record);
4461 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004462
4463 // Flush any expressions that were written as part of the base specifiers.
4464 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004465 }
4466
4467 CXXBaseSpecifiersToWrite.clear();
4468}
4469
Sean Huntcbb67482011-01-08 20:30:50 +00004470void ASTWriter::AddCXXCtorInitializers(
4471 const CXXCtorInitializer * const *CtorInitializers,
4472 unsigned NumCtorInitializers,
4473 RecordDataImpl &Record) {
4474 Record.push_back(NumCtorInitializers);
4475 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4476 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004477
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004478 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004479 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004480 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004481 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004482 } else if (Init->isDelegatingInitializer()) {
4483 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004484 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004485 } else if (Init->isMemberInitializer()){
4486 Record.push_back(CTOR_INITIALIZER_MEMBER);
4487 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004488 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004489 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4490 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004491 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004492
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004493 AddSourceLocation(Init->getMemberLocation(), Record);
4494 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004495 AddSourceLocation(Init->getLParenLoc(), Record);
4496 AddSourceLocation(Init->getRParenLoc(), Record);
4497 Record.push_back(Init->isWritten());
4498 if (Init->isWritten()) {
4499 Record.push_back(Init->getSourceOrder());
4500 } else {
4501 Record.push_back(Init->getNumArrayIndices());
4502 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4503 AddDeclRef(Init->getArrayIndex(i), Record);
4504 }
4505 }
4506}
4507
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004508void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4509 assert(D->DefinitionData);
4510 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004511 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004512 Record.push_back(Data.UserDeclaredConstructor);
4513 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004514 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004515 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004516 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004517 Record.push_back(Data.UserDeclaredDestructor);
4518 Record.push_back(Data.Aggregate);
4519 Record.push_back(Data.PlainOldData);
4520 Record.push_back(Data.Empty);
4521 Record.push_back(Data.Polymorphic);
4522 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004523 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004524 Record.push_back(Data.HasNoNonEmptyBases);
4525 Record.push_back(Data.HasPrivateFields);
4526 Record.push_back(Data.HasProtectedFields);
4527 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004528 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004529 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004530 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004531 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004532 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004533 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004534 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004535 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004536 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004537 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004538 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004539 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004540 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004541 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004542 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004543 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004544 Record.push_back(Data.DeclaredDefaultConstructor);
4545 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004546 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004547 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004548 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004549 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004550 Record.push_back(Data.FailedImplicitMoveConstructor);
4551 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004552 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004553
4554 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004555 if (Data.NumBases > 0)
4556 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4557 Record);
4558
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004559 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4560 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004561 if (Data.NumVBases > 0)
4562 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4563 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004564
4565 AddUnresolvedSet(Data.Conversions, Record);
4566 AddUnresolvedSet(Data.VisibleConversions, Record);
4567 // Data.Definition is the owning decl, no need to write it.
4568 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004569
4570 // Add lambda-specific data.
4571 if (Data.IsLambda) {
4572 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004573 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004574 Record.push_back(Lambda.NumCaptures);
4575 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004576 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004577 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004578 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004579 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4580 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4581 AddSourceLocation(Capture.getLocation(), Record);
4582 Record.push_back(Capture.isImplicit());
4583 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4584 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4585 AddDeclRef(Var, Record);
4586 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4587 : SourceLocation(),
4588 Record);
4589 }
4590 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004591}
4592
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004593void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004594 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004595 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004596 assert(FirstDeclID == NextDeclID &&
4597 FirstTypeID == NextTypeID &&
4598 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004599 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004600 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004601 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004602 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004603
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004604 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004605
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004606 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4607 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4608 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004609 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004610 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004611 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004612 NextDeclID = FirstDeclID;
4613 NextTypeID = FirstTypeID;
4614 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004615 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004616 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004617 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004618}
4619
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004620void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004621 IdentifierIDs[II] = ID;
4622}
4623
Douglas Gregora8235d62012-10-09 23:05:51 +00004624void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4625 MacroIDs[MI] = ID;
4626}
4627
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004628void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004629 // Always take the highest-numbered type index. This copes with an interesting
4630 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004631 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004632 // keep the higher-numbered entry so that we can properly write it out to
4633 // the AST file.
4634 TypeIdx &StoredIdx = TypeIdxs[T];
4635 if (Idx.getIndex() >= StoredIdx.getIndex())
4636 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004637}
4638
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004639void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004640 SelectorIDs[S] = ID;
4641}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004642
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004643void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004644 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004645 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004646 MacroDefinitions[MD] = ID;
4647}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004648
Douglas Gregora015cab2011-12-02 17:30:13 +00004649void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4650 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4651 SubmoduleIDs[Mod] = ID;
4652}
4653
Douglas Gregora8235d62012-10-09 23:05:51 +00004654void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4655 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4656}
4657
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004658void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004659 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004660 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004661 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4662 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004663 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004664 // A forward reference was mutated into a definition. Rewrite it.
4665 // FIXME: This happens during template instantiation, should we
4666 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004667 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004668 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004669 }
4670}
Douglas Gregora8235d62012-10-09 23:05:51 +00004671
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004672void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004673 assert(!WritingAST && "Already writing the AST!");
4674
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004675 // TU and namespaces are handled elsewhere.
4676 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4677 return;
4678
Douglas Gregor919814d2011-09-09 23:01:35 +00004679 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004680 return; // Not a source decl added to a DeclContext from PCH.
4681
4682 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004683 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004684}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004685
4686void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004687 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004688 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004689 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004690 return; // Not a source member added to a class from PCH.
4691 if (!isa<CXXMethodDecl>(D))
4692 return; // We are interested in lazily declared implicit methods.
4693
4694 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004695 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004696 UpdateRecord &Record = DeclUpdates[RD];
4697 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004698 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004699}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004700
4701void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4702 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004703 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004704 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004705 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004706 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004707 return; // Not a source specialization added to a template from PCH.
4708
4709 UpdateRecord &Record = DeclUpdates[TD];
4710 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004711 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004712}
Douglas Gregor89d99802010-11-30 06:16:57 +00004713
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004714void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4715 const FunctionDecl *D) {
4716 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004717 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004718 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004719 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004720 return; // Not a source specialization added to a template from PCH.
4721
4722 UpdateRecord &Record = DeclUpdates[TD];
4723 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004724 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004725}
4726
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004727void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004728 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004729 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004730 return; // Declaration not imported from PCH.
4731
4732 // Implicit decl from a PCH was defined.
4733 // FIXME: Should implicit definition be a separate FunctionDecl?
4734 RewriteDecl(D);
4735}
4736
Sebastian Redlf79a7192011-04-29 08:19:30 +00004737void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004738 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004739 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004740 return;
4741
4742 // Since the actual instantiation is delayed, this really means that we need
4743 // to update the instantiation location.
4744 UpdateRecord &Record = DeclUpdates[D];
4745 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4746 AddSourceLocation(
4747 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4748}
4749
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004750void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4751 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004752 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004753 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004754 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004755
4756 assert(IFD->getDefinition() && "Category on a class without a definition?");
4757 ObjCClassesWithCategories.insert(
4758 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004759}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004760
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004761
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004762void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4763 const ObjCPropertyDecl *OrigProp,
4764 const ObjCCategoryDecl *ClassExt) {
4765 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4766 if (!D)
4767 return;
4768
4769 assert(!WritingAST && "Already writing the AST!");
4770 if (!D->isFromASTFile())
4771 return; // Declaration not imported from PCH.
4772
4773 RewriteDecl(D);
4774}