blob: 827fbed177a24cee62ee7b1ce1e1d8177d9070e9 [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 Gregorab41e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Sebastian Redlade50002010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000059}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
66
Douglas Gregor2cf26342009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000070
Douglas Gregor2cf26342009-04-09 22:27:44 +000071namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl3397c552010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000116}
117
Sebastian Redl3397c552010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159}
160
Sebastian Redl3397c552010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175}
176
Sebastian Redl3397c552010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
188 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000189 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000190 Record.push_back(T->getExceptionSpecType());
191 if (T->getExceptionSpecType() == EST_Dynamic) {
192 Record.push_back(T->getNumExceptions());
193 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
194 Writer.AddTypeRef(T->getExceptionType(I), Record);
195 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
196 Writer.AddStmt(T->getNoexceptExpr());
197 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000198 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199}
200
Sebastian Redl3397c552010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000202 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000203 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000204}
John McCalled976492009-12-04 22:46:56 +0000205
Sebastian Redl3397c552010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000207 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000208 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
209 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000210 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000211}
212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000214 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000215 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000216}
217
Sebastian Redl3397c552010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000226}
227
Sean Huntca63c202011-05-24 22:41:36 +0000228void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
229 Writer.AddTypeRef(T->getBaseType(), Record);
230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
231 Record.push_back(T->getUTTKind());
232 Code = TYPE_UNARY_TRANSFORM;
233}
234
Richard Smith34b41d92011-02-20 03:19:35 +0000235void ASTTypeWriter::VisitAutoType(const AutoType *T) {
236 Writer.AddTypeRef(T->getDeducedType(), Record);
237 Code = TYPE_AUTO;
238}
239
Sebastian Redl3397c552010-08-18 23:56:27 +0000240void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000241 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000242 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000243 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000244 "Cannot serialize in the middle of a type definition");
245}
246
Sebastian Redl3397c552010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000249 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000250}
251
Sebastian Redl3397c552010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000254 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255}
256
John McCall9d156a72011-01-06 01:58:22 +0000257void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
258 Writer.AddTypeRef(T->getModifiedType(), Record);
259 Writer.AddTypeRef(T->getEquivalentType(), Record);
260 Record.push_back(T->getAttrKind());
261 Code = TYPE_ATTRIBUTED;
262}
263
Mike Stump1eb44332009-09-09 15:08:12 +0000264void
Sebastian Redl3397c552010-08-18 23:56:27 +0000265ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000266 const SubstTemplateTypeParmType *T) {
267 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
268 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000269 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000270}
271
272void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
274 const SubstTemplateTypeParmPackType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
278}
279
280void
Sebastian Redl3397c552010-08-18 23:56:27 +0000281ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000282 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000283 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000284 Writer.AddTemplateName(T->getTemplateName(), Record);
285 Record.push_back(T->getNumArgs());
286 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
287 ArgI != ArgE; ++ArgI)
288 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000289 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
290 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000291 : T->getCanonicalTypeInternal(),
292 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000293 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000294}
295
296void
Sebastian Redl3397c552010-08-18 23:56:27 +0000297ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000298 VisitArrayType(T);
299 Writer.AddStmt(T->getSizeExpr());
300 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl3397c552010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000306 const DependentSizedExtVectorType *T) {
307 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000308 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl3397c552010-08-18 23:56:27 +0000312ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313 Record.push_back(T->getDepth());
314 Record.push_back(T->getIndex());
315 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000316 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000317 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000318}
319
320void
Sebastian Redl3397c552010-08-18 23:56:27 +0000321ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000322 Record.push_back(T->getKeyword());
323 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
324 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000325 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
326 : T->getCanonicalTypeInternal(),
327 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000333 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000334 Record.push_back(T->getKeyword());
335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddIdentifierRef(T->getIdentifier(), Record);
337 Record.push_back(T->getNumArgs());
338 for (DependentTemplateSpecializationType::iterator
339 I = T->begin(), E = T->end(); I != E; ++I)
340 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000341 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000342}
343
Douglas Gregor7536dd52010-12-20 02:24:11 +0000344void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
345 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000346 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
347 Record.push_back(*NumExpansions + 1);
348 else
349 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000350 Code = TYPE_PACK_EXPANSION;
351}
352
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000353void ASTTypeWriter::VisitParenType(const ParenType *T) {
354 Writer.AddTypeRef(T->getInnerType(), Record);
355 Code = TYPE_PAREN;
356}
357
Sebastian Redl3397c552010-08-18 23:56:27 +0000358void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000359 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000360 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
361 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000362 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000363}
364
Sebastian Redl3397c552010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000366 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000367 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000368 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000369}
370
Sebastian Redl3397c552010-08-18 23:56:27 +0000371void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000372 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000374}
375
Sebastian Redl3397c552010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000377 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000378 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000379 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000380 E = T->qual_end(); I != E; ++I)
381 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000382 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000383}
384
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000385void
Sebastian Redl3397c552010-08-18 23:56:27 +0000386ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000387 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000388 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000389}
390
Eli Friedmanb001de72011-10-06 23:00:33 +0000391void
392ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
393 Writer.AddTypeRef(T->getValueType(), Record);
394 Code = TYPE_ATOMIC;
395}
396
John McCalla1ee0c52009-10-16 21:56:05 +0000397namespace {
398
399class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000400 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000401 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000402
403public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000404 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000405 : Writer(Writer), Record(Record) { }
406
John McCall51bd8032009-10-18 01:05:36 +0000407#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000408#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000409 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000410#include "clang/AST/TypeLocNodes.def"
411
John McCall51bd8032009-10-18 01:05:36 +0000412 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
413 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000414};
415
416}
417
John McCall51bd8032009-10-18 01:05:36 +0000418void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
419 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000420}
John McCall51bd8032009-10-18 01:05:36 +0000421void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000422 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
423 if (TL.needsExtraLocalData()) {
424 Record.push_back(TL.getWrittenTypeSpec());
425 Record.push_back(TL.getWrittenSignSpec());
426 Record.push_back(TL.getWrittenWidthSpec());
427 Record.push_back(TL.hasModeAttr());
428 }
John McCalla1ee0c52009-10-16 21:56:05 +0000429}
John McCall51bd8032009-10-18 01:05:36 +0000430void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000432}
John McCall51bd8032009-10-18 01:05:36 +0000433void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000435}
John McCall51bd8032009-10-18 01:05:36 +0000436void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000438}
John McCall51bd8032009-10-18 01:05:36 +0000439void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000441}
John McCall51bd8032009-10-18 01:05:36 +0000442void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000444}
John McCall51bd8032009-10-18 01:05:36 +0000445void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000447 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
451 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
452 Record.push_back(TL.getSizeExpr() ? 1 : 0);
453 if (TL.getSizeExpr())
454 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
457 VisitArrayTypeLoc(TL);
458}
459void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
460 VisitArrayTypeLoc(TL);
461}
462void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
463 VisitArrayTypeLoc(TL);
464}
465void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
466 DependentSizedArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
470 DependentSizedExtVectorTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
473void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
475}
476void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
479void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000480 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
481 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000482 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000483 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
484 Writer.AddDeclRef(TL.getArg(i), Record);
485}
486void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
487 VisitFunctionTypeLoc(TL);
488}
489void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
490 VisitFunctionTypeLoc(TL);
491}
John McCalled976492009-12-04 22:46:56 +0000492void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
John McCall51bd8032009-10-18 01:05:36 +0000495void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getNameLoc(), Record);
497}
498void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
500 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
501 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000502}
503void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000504 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
505 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
506 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
507 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000508}
509void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
Sean Huntca63c202011-05-24 22:41:36 +0000512void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
513 Writer.AddSourceLocation(TL.getKWLoc(), Record);
514 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
515 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
516 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
517}
Richard Smith34b41d92011-02-20 03:19:35 +0000518void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
John McCall51bd8032009-10-18 01:05:36 +0000521void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall9d156a72011-01-06 01:58:22 +0000527void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
529 if (TL.hasAttrOperand()) {
530 SourceRange range = TL.getAttrOperandParensRange();
531 Writer.AddSourceLocation(range.getBegin(), Record);
532 Writer.AddSourceLocation(range.getEnd(), Record);
533 }
534 if (TL.hasAttrExprOperand()) {
535 Expr *operand = TL.getAttrExprOperand();
536 Record.push_back(operand ? 1 : 0);
537 if (operand) Writer.AddStmt(operand);
538 } else if (TL.hasAttrEnumOperand()) {
539 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
540 }
541}
John McCall51bd8032009-10-18 01:05:36 +0000542void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCall49a832b2009-10-18 09:09:24 +0000545void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
546 SubstTemplateTypeParmTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getNameLoc(), Record);
548}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000549void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
550 SubstTemplateTypeParmPackTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall51bd8032009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
554 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000555 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000556 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
557 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
558 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
559 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000560 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
561 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000562}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000563void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
564 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
565 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
566}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000567void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000568 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000569 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000570}
John McCall3cb0ebd2010-03-10 03:28:59 +0000571void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
572 Writer.AddSourceLocation(TL.getNameLoc(), Record);
573}
Douglas Gregor4714c122010-03-31 17:34:00 +0000574void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000575 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000576 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578}
John McCall33500952010-06-11 00:33:02 +0000579void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
580 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000581 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000582 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000583 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000584 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000585 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
586 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
587 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000588 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
589 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000590}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000591void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
592 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
593}
John McCall51bd8032009-10-18 01:05:36 +0000594void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000596}
597void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
598 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000599 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
600 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
601 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
602 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000603}
John McCall54e14c42009-10-22 22:37:11 +0000604void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
605 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000606}
Eli Friedmanb001de72011-10-06 23:00:33 +0000607void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
608 Writer.AddSourceLocation(TL.getKWLoc(), Record);
609 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
610 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
611}
John McCalla1ee0c52009-10-16 21:56:05 +0000612
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000613//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000614// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000615//===----------------------------------------------------------------------===//
616
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000617static void EmitBlockID(unsigned ID, const char *Name,
618 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000619 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000620 Record.clear();
621 Record.push_back(ID);
622 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
623
624 // Emit the block name if present.
625 if (Name == 0 || Name[0] == 0) return;
626 Record.clear();
627 while (*Name)
628 Record.push_back(*Name++);
629 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
630}
631
632static void EmitRecordID(unsigned ID, const char *Name,
633 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000634 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000635 Record.clear();
636 Record.push_back(ID);
637 while (*Name)
638 Record.push_back(*Name++);
639 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000640}
641
642static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000643 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000644#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000645 RECORD(STMT_STOP);
646 RECORD(STMT_NULL_PTR);
647 RECORD(STMT_NULL);
648 RECORD(STMT_COMPOUND);
649 RECORD(STMT_CASE);
650 RECORD(STMT_DEFAULT);
651 RECORD(STMT_LABEL);
652 RECORD(STMT_IF);
653 RECORD(STMT_SWITCH);
654 RECORD(STMT_WHILE);
655 RECORD(STMT_DO);
656 RECORD(STMT_FOR);
657 RECORD(STMT_GOTO);
658 RECORD(STMT_INDIRECT_GOTO);
659 RECORD(STMT_CONTINUE);
660 RECORD(STMT_BREAK);
661 RECORD(STMT_RETURN);
662 RECORD(STMT_DECL);
663 RECORD(STMT_ASM);
664 RECORD(EXPR_PREDEFINED);
665 RECORD(EXPR_DECL_REF);
666 RECORD(EXPR_INTEGER_LITERAL);
667 RECORD(EXPR_FLOATING_LITERAL);
668 RECORD(EXPR_IMAGINARY_LITERAL);
669 RECORD(EXPR_STRING_LITERAL);
670 RECORD(EXPR_CHARACTER_LITERAL);
671 RECORD(EXPR_PAREN);
672 RECORD(EXPR_UNARY_OPERATOR);
673 RECORD(EXPR_SIZEOF_ALIGN_OF);
674 RECORD(EXPR_ARRAY_SUBSCRIPT);
675 RECORD(EXPR_CALL);
676 RECORD(EXPR_MEMBER);
677 RECORD(EXPR_BINARY_OPERATOR);
678 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
679 RECORD(EXPR_CONDITIONAL_OPERATOR);
680 RECORD(EXPR_IMPLICIT_CAST);
681 RECORD(EXPR_CSTYLE_CAST);
682 RECORD(EXPR_COMPOUND_LITERAL);
683 RECORD(EXPR_EXT_VECTOR_ELEMENT);
684 RECORD(EXPR_INIT_LIST);
685 RECORD(EXPR_DESIGNATED_INIT);
686 RECORD(EXPR_IMPLICIT_VALUE_INIT);
687 RECORD(EXPR_VA_ARG);
688 RECORD(EXPR_ADDR_LABEL);
689 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000690 RECORD(EXPR_CHOOSE);
691 RECORD(EXPR_GNU_NULL);
692 RECORD(EXPR_SHUFFLE_VECTOR);
693 RECORD(EXPR_BLOCK);
694 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000695 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000696 RECORD(EXPR_OBJC_STRING_LITERAL);
697 RECORD(EXPR_OBJC_ENCODE);
698 RECORD(EXPR_OBJC_SELECTOR_EXPR);
699 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
700 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
701 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
702 RECORD(EXPR_OBJC_KVC_REF_EXPR);
703 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000704 RECORD(STMT_OBJC_FOR_COLLECTION);
705 RECORD(STMT_OBJC_CATCH);
706 RECORD(STMT_OBJC_FINALLY);
707 RECORD(STMT_OBJC_AT_TRY);
708 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
709 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000710 RECORD(EXPR_CXX_OPERATOR_CALL);
711 RECORD(EXPR_CXX_CONSTRUCT);
712 RECORD(EXPR_CXX_STATIC_CAST);
713 RECORD(EXPR_CXX_DYNAMIC_CAST);
714 RECORD(EXPR_CXX_REINTERPRET_CAST);
715 RECORD(EXPR_CXX_CONST_CAST);
716 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
717 RECORD(EXPR_CXX_BOOL_LITERAL);
718 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000719 RECORD(EXPR_CXX_TYPEID_EXPR);
720 RECORD(EXPR_CXX_TYPEID_TYPE);
721 RECORD(EXPR_CXX_UUIDOF_EXPR);
722 RECORD(EXPR_CXX_UUIDOF_TYPE);
723 RECORD(EXPR_CXX_THIS);
724 RECORD(EXPR_CXX_THROW);
725 RECORD(EXPR_CXX_DEFAULT_ARG);
726 RECORD(EXPR_CXX_BIND_TEMPORARY);
727 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
728 RECORD(EXPR_CXX_NEW);
729 RECORD(EXPR_CXX_DELETE);
730 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
731 RECORD(EXPR_EXPR_WITH_CLEANUPS);
732 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
733 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
734 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
735 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
736 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
737 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
738 RECORD(EXPR_CXX_NOEXCEPT);
739 RECORD(EXPR_OPAQUE_VALUE);
740 RECORD(EXPR_BINARY_TYPE_TRAIT);
741 RECORD(EXPR_PACK_EXPANSION);
742 RECORD(EXPR_SIZEOF_PACK);
743 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000744 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000745#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000746}
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Sebastian Redla4232eb2010-08-18 23:56:21 +0000748void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000749 RecordData Record;
750 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000752#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
753#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Sebastian Redl3397c552010-08-18 23:56:27 +0000755 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000756 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000757 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000758 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000759 RECORD(TYPE_OFFSET);
760 RECORD(DECL_OFFSET);
761 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000762 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000763 RECORD(IDENTIFIER_OFFSET);
764 RECORD(IDENTIFIER_TABLE);
765 RECORD(EXTERNAL_DEFINITIONS);
766 RECORD(SPECIAL_TYPES);
767 RECORD(STATISTICS);
768 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000769 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000770 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
771 RECORD(SELECTOR_OFFSETS);
772 RECORD(METHOD_POOL);
773 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000774 RECORD(SOURCE_LOCATION_OFFSETS);
775 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000776 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000777 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000778 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000779 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000780 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000781 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000782 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000783 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000784 RECORD(SEMA_DECL_REFS);
785 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
786 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
787 RECORD(DECL_REPLACEMENTS);
788 RECORD(UPDATE_VISIBLE);
789 RECORD(DECL_UPDATE_OFFSETS);
790 RECORD(DECL_UPDATES);
791 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
792 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000793 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000794 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000795 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000796 RECORD(FP_PRAGMA_OPTIONS);
797 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000798 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000799 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
800 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000801 RECORD(MODULE_OFFSET_MAP);
802 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000803 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000804 RECORD(FILE_SORTED_DECLS);
805 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000806 RECORD(MERGED_DECLARATIONS);
807 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000808 RECORD(OBJC_CATEGORIES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000809
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000810 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000811 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000812 RECORD(SM_SLOC_FILE_ENTRY);
813 RECORD(SM_SLOC_BUFFER_ENTRY);
814 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000815 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000817 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000818 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000819 RECORD(PP_MACRO_OBJECT_LIKE);
820 RECORD(PP_MACRO_FUNCTION_LIKE);
821 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000822
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000823 // Decls and Types block.
824 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000825 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000826 RECORD(TYPE_COMPLEX);
827 RECORD(TYPE_POINTER);
828 RECORD(TYPE_BLOCK_POINTER);
829 RECORD(TYPE_LVALUE_REFERENCE);
830 RECORD(TYPE_RVALUE_REFERENCE);
831 RECORD(TYPE_MEMBER_POINTER);
832 RECORD(TYPE_CONSTANT_ARRAY);
833 RECORD(TYPE_INCOMPLETE_ARRAY);
834 RECORD(TYPE_VARIABLE_ARRAY);
835 RECORD(TYPE_VECTOR);
836 RECORD(TYPE_EXT_VECTOR);
837 RECORD(TYPE_FUNCTION_PROTO);
838 RECORD(TYPE_FUNCTION_NO_PROTO);
839 RECORD(TYPE_TYPEDEF);
840 RECORD(TYPE_TYPEOF_EXPR);
841 RECORD(TYPE_TYPEOF);
842 RECORD(TYPE_RECORD);
843 RECORD(TYPE_ENUM);
844 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000845 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000846 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000847 RECORD(TYPE_DECLTYPE);
848 RECORD(TYPE_ELABORATED);
849 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
850 RECORD(TYPE_UNRESOLVED_USING);
851 RECORD(TYPE_INJECTED_CLASS_NAME);
852 RECORD(TYPE_OBJC_OBJECT);
853 RECORD(TYPE_TEMPLATE_TYPE_PARM);
854 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
855 RECORD(TYPE_DEPENDENT_NAME);
856 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
857 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
858 RECORD(TYPE_PAREN);
859 RECORD(TYPE_PACK_EXPANSION);
860 RECORD(TYPE_ATTRIBUTED);
861 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000862 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000863 RECORD(DECL_TYPEDEF);
864 RECORD(DECL_ENUM);
865 RECORD(DECL_RECORD);
866 RECORD(DECL_ENUM_CONSTANT);
867 RECORD(DECL_FUNCTION);
868 RECORD(DECL_OBJC_METHOD);
869 RECORD(DECL_OBJC_INTERFACE);
870 RECORD(DECL_OBJC_PROTOCOL);
871 RECORD(DECL_OBJC_IVAR);
872 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000873 RECORD(DECL_OBJC_CATEGORY);
874 RECORD(DECL_OBJC_CATEGORY_IMPL);
875 RECORD(DECL_OBJC_IMPLEMENTATION);
876 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
877 RECORD(DECL_OBJC_PROPERTY);
878 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000879 RECORD(DECL_FIELD);
880 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000881 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000882 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000883 RECORD(DECL_FILE_SCOPE_ASM);
884 RECORD(DECL_BLOCK);
885 RECORD(DECL_CONTEXT_LEXICAL);
886 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000887 RECORD(DECL_NAMESPACE);
888 RECORD(DECL_NAMESPACE_ALIAS);
889 RECORD(DECL_USING);
890 RECORD(DECL_USING_SHADOW);
891 RECORD(DECL_USING_DIRECTIVE);
892 RECORD(DECL_UNRESOLVED_USING_VALUE);
893 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
894 RECORD(DECL_LINKAGE_SPEC);
895 RECORD(DECL_CXX_RECORD);
896 RECORD(DECL_CXX_METHOD);
897 RECORD(DECL_CXX_CONSTRUCTOR);
898 RECORD(DECL_CXX_DESTRUCTOR);
899 RECORD(DECL_CXX_CONVERSION);
900 RECORD(DECL_ACCESS_SPEC);
901 RECORD(DECL_FRIEND);
902 RECORD(DECL_FRIEND_TEMPLATE);
903 RECORD(DECL_CLASS_TEMPLATE);
904 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
905 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
906 RECORD(DECL_FUNCTION_TEMPLATE);
907 RECORD(DECL_TEMPLATE_TYPE_PARM);
908 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
909 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
910 RECORD(DECL_STATIC_ASSERT);
911 RECORD(DECL_CXX_BASE_SPECIFIERS);
912 RECORD(DECL_INDIRECTFIELD);
913 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
914
Douglas Gregora72d8c42011-06-03 02:27:19 +0000915 // Statements and Exprs can occur in the Decls and Types block.
916 AddStmtsExprs(Stream, Record);
917
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000918 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000919 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000920 RECORD(PPD_MACRO_DEFINITION);
921 RECORD(PPD_INCLUSION_DIRECTIVE);
922
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000923#undef RECORD
924#undef BLOCK
925 Stream.ExitBlock();
926}
927
Douglas Gregore650c8c2009-07-07 00:12:59 +0000928/// \brief Adjusts the given filename to only write out the portion of the
929/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000930///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000931/// \param Filename the file name to adjust.
932///
933/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
934/// the returned filename will be adjusted by this system root.
935///
936/// \returns either the original filename (if it needs no adjustment) or the
937/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000938static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000939adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000940 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregor832d6202011-07-22 16:35:34 +0000942 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945 // Verify that the filename and the system root have the same prefix.
946 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000947 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000948 if (Filename[Pos] != isysroot[Pos])
949 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Douglas Gregore650c8c2009-07-07 00:12:59 +0000951 // We hit the end of the filename before we hit the end of the system root.
952 if (!Filename[Pos])
953 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955 // If the file name has a '/' at the current position, skip over the '/'.
956 // We distinguish sysroot-based includes from absolute includes by the
957 // absence of '/' at the beginning of sysroot-based includes.
958 if (Filename[Pos] == '/')
959 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961 return Filename + Pos;
962}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000963
Sebastian Redl3397c552010-08-18 23:56:27 +0000964/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000965void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000966 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000967 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000968
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000970 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000972 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000973 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
974 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
976 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
977 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000978 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000982 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000983 Record.push_back(VERSION_MAJOR);
984 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 Record.push_back(CLANG_VERSION_MAJOR);
986 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000987 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000988 const std::string &Triple = Target.getTriple().getTriple();
989 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
990
991 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000992 serialization::ModuleManager &Mgr = Chain->getModuleManager();
993 llvm::SmallVector<char, 128> ModulePaths;
994 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000995
996 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
997 M != MEnd; ++M) {
998 // Skip modules that weren't directly imported.
999 if (!(*M)->isDirectlyImported())
1000 continue;
1001
1002 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1003 // FIXME: Write import location, once it matters.
1004 // FIXME: This writes the absolute path for AST files we depend on.
1005 const std::string &FileName = (*M)->FileName;
1006 Record.push_back(FileName.size());
1007 Record.append(FileName.begin(), FileName.end());
1008 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001009 Stream.EmitRecord(IMPORTS, Record);
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Douglas Gregor31d375f2011-05-06 21:43:30 +00001012 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001013 SourceManager &SM = Context.getSourceManager();
1014 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1015 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001016 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001017 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1018 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1019
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001020 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001022 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001023
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001024 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001025 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001026 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001027 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001028 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001029 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001030
1031 Record.clear();
1032 Record.push_back(SM.getMainFileID().getOpaqueValue());
1033 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001034 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001035
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001036 // Original PCH directory
1037 if (!OutputFile.empty() && OutputFile != "-") {
1038 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1039 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1041 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1042
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001043 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001044
1045 llvm::sys::fs::make_absolute(OutputPath);
1046 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1047
1048 RecordData Record;
1049 Record.push_back(ORIGINAL_PCH_DIR);
1050 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1051 }
1052
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001053 // Repository branch/version information.
1054 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001055 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001056 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1057 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001058 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001059 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001060 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1061 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001062}
1063
1064/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001065void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001066 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001067#define LANGOPT(Name, Bits, Default, Description) \
1068 Record.push_back(LangOpts.Name);
1069#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1070 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1071#include "clang/Basic/LangOptions.def"
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001072
1073 Record.push_back(LangOpts.CurrentModule.size());
1074 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001075 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001076}
1077
Douglas Gregor14f79002009-04-10 03:52:48 +00001078//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001079// stat cache Serialization
1080//===----------------------------------------------------------------------===//
1081
1082namespace {
1083// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001084class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001085public:
1086 typedef const char * key_type;
1087 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner74e976b2010-11-23 19:28:12 +00001089 typedef struct stat data_type;
1090 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001091
1092 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001093 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
1096 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001097 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001098 data_type_ref Data) {
1099 unsigned StrLen = strlen(path);
1100 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001101 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102 clang::io::Emit8(Out, DataLen);
1103 return std::make_pair(StrLen + 1, DataLen);
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner5f9e2722011-07-23 10:55:15 +00001106 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001107 Out.write(path, KeyLen);
1108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Chris Lattner5f9e2722011-07-23 10:55:15 +00001110 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001111 data_type_ref Data, unsigned DataLen) {
1112 using namespace clang::io;
1113 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Chris Lattner74e976b2010-11-23 19:28:12 +00001115 Emit32(Out, (uint32_t) Data.st_ino);
1116 Emit32(Out, (uint32_t) Data.st_dev);
1117 Emit16(Out, (uint16_t) Data.st_mode);
1118 Emit64(Out, (uint64_t) Data.st_mtime);
1119 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001120
1121 assert(Out.tell() - Start == DataLen && "Wrong data length");
1122 }
1123};
1124} // end anonymous namespace
1125
Sebastian Redl3397c552010-08-18 23:56:27 +00001126/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001127void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128 // Build the on-disk hash table containing information about every
1129 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001130 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001131 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001132 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001133 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001134 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001135 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001136 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001139 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001140 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001141 uint32_t BucketOffset;
1142 {
1143 llvm::raw_svector_ostream Out(StatCacheData);
1144 // Make sure that no bucket is at offset 0
1145 clang::io::Emit32(Out, 0);
1146 BucketOffset = Generator.Emit(Out);
1147 }
1148
1149 // Create a blob abbreviation
1150 using namespace llvm;
1151 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001152 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1156 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1157
1158 // Write the stat cache
1159 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001160 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001161 Record.push_back(BucketOffset);
1162 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001163 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001164}
1165
1166//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001167// Source Manager Serialization
1168//===----------------------------------------------------------------------===//
1169
1170/// \brief Create an abbreviation for the SLocEntry that refers to a
1171/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001172static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001173 using namespace llvm;
1174 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001175 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001180 // FileEntry fields.
1181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001188 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001189}
1190
1191/// \brief Create an abbreviation for the SLocEntry that refers to a
1192/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001193static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001194 using namespace llvm;
1195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001196 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001202 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001203}
1204
1205/// \brief Create an abbreviation for the SLocEntry that refers to a
1206/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001207static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001208 using namespace llvm;
1209 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001210 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001212 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001213}
1214
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001215/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1216/// expansion.
1217static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001218 using namespace llvm;
1219 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001220 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001226 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001227}
1228
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001229namespace {
1230 // Trait used for the on-disk hash table of header search information.
1231 class HeaderFileInfoTrait {
1232 ASTWriter &Writer;
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001233 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001234
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001235 // Keep track of the framework names we've used during serialization.
1236 SmallVector<char, 128> FrameworkStringData;
1237 llvm::StringMap<unsigned> FrameworkNameOffset;
1238
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001239 public:
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001240 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001241 : Writer(Writer), HS(HS) { }
1242
1243 typedef const char *key_type;
1244 typedef key_type key_type_ref;
1245
1246 typedef HeaderFileInfo data_type;
1247 typedef const data_type &data_type_ref;
1248
1249 static unsigned ComputeHash(const char *path) {
1250 // The hash is based only on the filename portion of the key, so that the
1251 // reader can match based on filenames when symlinking or excess path
1252 // elements ("foo/../", "../") change the form of the name. However,
1253 // complete path is still the key.
1254 return llvm::HashString(llvm::sys::path::filename(path));
1255 }
1256
1257 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001258 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001259 data_type_ref Data) {
1260 unsigned StrLen = strlen(path);
1261 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001262 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001263 clang::io::Emit8(Out, DataLen);
1264 return std::make_pair(StrLen + 1, DataLen);
1265 }
1266
Chris Lattner5f9e2722011-07-23 10:55:15 +00001267 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001268 Out.write(path, KeyLen);
1269 }
1270
Chris Lattner5f9e2722011-07-23 10:55:15 +00001271 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001272 data_type_ref Data, unsigned DataLen) {
1273 using namespace clang::io;
1274 uint64_t Start = Out.tell(); (void)Start;
1275
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001276 unsigned char Flags = (Data.isImport << 5)
1277 | (Data.isPragmaOnce << 4)
1278 | (Data.DirInfo << 2)
1279 | (Data.Resolved << 1)
1280 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001281 Emit8(Out, (uint8_t)Flags);
1282 Emit16(Out, (uint16_t) Data.NumIncludes);
1283
1284 if (!Data.ControllingMacro)
1285 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1286 else
1287 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001288
1289 unsigned Offset = 0;
1290 if (!Data.Framework.empty()) {
1291 // If this header refers into a framework, save the framework name.
1292 llvm::StringMap<unsigned>::iterator Pos
1293 = FrameworkNameOffset.find(Data.Framework);
1294 if (Pos == FrameworkNameOffset.end()) {
1295 Offset = FrameworkStringData.size() + 1;
1296 FrameworkStringData.append(Data.Framework.begin(),
1297 Data.Framework.end());
1298 FrameworkStringData.push_back(0);
1299
1300 FrameworkNameOffset[Data.Framework] = Offset;
1301 } else
1302 Offset = Pos->second;
1303 }
1304 Emit32(Out, Offset);
1305
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001306 assert(Out.tell() - Start == DataLen && "Wrong data length");
1307 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001308
1309 const char *strings_begin() const { return FrameworkStringData.begin(); }
1310 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001311 };
1312} // end anonymous namespace
1313
1314/// \brief Write the header search block for the list of files that
1315///
1316/// \param HS The header search structure to save.
1317///
1318/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001319void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001320 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001321 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1322
1323 if (FilesByUID.size() > HS.header_file_size())
1324 FilesByUID.resize(HS.header_file_size());
1325
1326 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1327 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001328 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001329 unsigned NumHeaderSearchEntries = 0;
1330 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1331 const FileEntry *File = FilesByUID[UID];
1332 if (!File)
1333 continue;
1334
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001335 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1336 // from the external source if it was not provided already.
1337 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001338 if (HFI.External && Chain)
1339 continue;
1340
1341 // Turn the file name into an absolute path, if it isn't already.
1342 const char *Filename = File->getName();
1343 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1344
1345 // If we performed any translation on the file name at all, we need to
1346 // save this string, since the generator will refer to it later.
1347 if (Filename != File->getName()) {
1348 Filename = strdup(Filename);
1349 SavedStrings.push_back(Filename);
1350 }
1351
1352 Generator.insert(Filename, HFI, GeneratorTrait);
1353 ++NumHeaderSearchEntries;
1354 }
1355
1356 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001357 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001358 uint32_t BucketOffset;
1359 {
1360 llvm::raw_svector_ostream Out(TableData);
1361 // Make sure that no bucket is at offset 0
1362 clang::io::Emit32(Out, 0);
1363 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1364 }
1365
1366 // Create a blob abbreviation
1367 using namespace llvm;
1368 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1369 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1374 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1375
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001376 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001377 RecordData Record;
1378 Record.push_back(HEADER_SEARCH_TABLE);
1379 Record.push_back(BucketOffset);
1380 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001381 Record.push_back(TableData.size());
1382 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001383 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1384
1385 // Free all of the strings we had to duplicate.
1386 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1387 free((void*)SavedStrings[I]);
1388}
1389
Douglas Gregor14f79002009-04-10 03:52:48 +00001390/// \brief Writes the block containing the serialized form of the
1391/// source manager.
1392///
1393/// TODO: We should probably use an on-disk hash table (stored in a
1394/// blob), indexed based on the file name, so that we only create
1395/// entries for files that we actually need. In the common case (no
1396/// errors), we probably won't have to create file entries for any of
1397/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001398void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001399 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001400 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001401 RecordData Record;
1402
Chris Lattnerf04ad692009-04-10 17:16:57 +00001403 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001404 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001405
1406 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001407 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1408 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1409 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001410 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001411
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001412 // Write out the source location entry table. We skip the first
1413 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001414 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001415 // Write out the offsets of only source location file entries.
1416 // We will go through them in ASTReader::validateFileEntries().
1417 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001418 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001419 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1420 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001421 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001422 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001423 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001424
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001425 // Record the offset of this source-location entry.
1426 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1427
1428 // Figure out which record code to use.
1429 unsigned Code;
1430 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001431 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1432 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001433 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001434 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1435 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001436 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001437 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001438 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001439 Record.clear();
1440 Record.push_back(Code);
1441
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001442 // Starting offset of this entry within this module, so skip the dummy.
1443 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001444 if (SLoc->isFile()) {
1445 const SrcMgr::FileInfo &File = SLoc->getFile();
1446 Record.push_back(File.getIncludeLoc().getRawEncoding());
1447 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1448 Record.push_back(File.hasLineDirectives());
1449
1450 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001451 if (Content->OrigEntry) {
1452 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001453 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001454
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001455 // The source location entry is a file. The blob associated
1456 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Douglas Gregor2d52be52010-03-21 22:49:54 +00001458 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001459 Record.push_back(Content->OrigEntry->getSize());
1460 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001461 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001462 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001463
1464 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1465 if (FDI != FileDeclIDs.end()) {
1466 Record.push_back(FDI->second->FirstDeclIndex);
1467 Record.push_back(FDI->second->DeclIDs.size());
1468 } else {
1469 Record.push_back(0);
1470 Record.push_back(0);
1471 }
Douglas Gregora081da52011-11-16 20:05:18 +00001472
Douglas Gregore650c8c2009-07-07 00:12:59 +00001473 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001474 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001475 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001476
1477 // Ask the file manager to fixup the relative path for us. This will
1478 // honor the working directory.
1479 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1480
1481 // FIXME: This call to make_absolute shouldn't be necessary, the
1482 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001483 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001484 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Douglas Gregore650c8c2009-07-07 00:12:59 +00001486 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001487 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001488
1489 if (Content->BufferOverridden) {
1490 Record.clear();
1491 Record.push_back(SM_SLOC_BUFFER_BLOB);
1492 const llvm::MemoryBuffer *Buffer
1493 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1494 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1495 StringRef(Buffer->getBufferStart(),
1496 Buffer->getBufferSize() + 1));
1497 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001498 } else {
1499 // The source location entry is a buffer. The blob associated
1500 // with this entry contains the contents of the buffer.
1501
1502 // We add one to the size so that we capture the trailing NULL
1503 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1504 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001505 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001506 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001507 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001508 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001509 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001510 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001511 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001512 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001513 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001514 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001515
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001516 if (strcmp(Name, "<built-in>") == 0) {
1517 PreloadSLocs.push_back(SLocEntryOffsets.size());
1518 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001519 }
1520 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001521 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001522 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001523 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1524 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001525 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1526 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001527
1528 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001529 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001530 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001531 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001533 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001534 }
1535 }
1536
Douglas Gregorc9490c02009-04-16 22:23:12 +00001537 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001538
1539 if (SLocEntryOffsets.empty())
1540 return;
1541
Sebastian Redl3397c552010-08-18 23:56:27 +00001542 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001543 // table is used for lazily loading source-location information.
1544 using namespace llvm;
1545 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001546 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1550 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001553 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001554 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001555 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001556 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001557
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001558 Abbrev = new BitCodeAbbrev();
1559 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1562 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1563
1564 Record.clear();
1565 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1566 Record.push_back(SLocFileEntryOffsets.size());
1567 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1568 data(SLocFileEntryOffsets));
1569
Sebastian Redl3397c552010-08-18 23:56:27 +00001570 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001571 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001572 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001573
1574 // Write the line table. It depends on remapping working, so it must come
1575 // after the source location offsets.
1576 if (SourceMgr.hasLineTable()) {
1577 LineTableInfo &LineTable = SourceMgr.getLineTable();
1578
1579 Record.clear();
1580 // Emit the file names
1581 Record.push_back(LineTable.getNumFilenames());
1582 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1583 // Emit the file name
1584 const char *Filename = LineTable.getFilename(I);
1585 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1586 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1587 Record.push_back(FilenameLen);
1588 if (FilenameLen)
1589 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1590 }
1591
1592 // Emit the line entries
1593 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1594 L != LEnd; ++L) {
1595 // Only emit entries for local files.
1596 if (L->first < 0)
1597 continue;
1598
1599 // Emit the file ID
1600 Record.push_back(L->first);
1601
1602 // Emit the line entries
1603 Record.push_back(L->second.size());
1604 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1605 LEEnd = L->second.end();
1606 LE != LEEnd; ++LE) {
1607 Record.push_back(LE->FileOffset);
1608 Record.push_back(LE->LineNo);
1609 Record.push_back(LE->FilenameID);
1610 Record.push_back((unsigned)LE->FileKind);
1611 Record.push_back(LE->IncludeOffset);
1612 }
1613 }
1614 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1615 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001616}
1617
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001618//===----------------------------------------------------------------------===//
1619// Preprocessor Serialization
1620//===----------------------------------------------------------------------===//
1621
Douglas Gregor9c736102011-02-10 18:20:09 +00001622static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1623 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1624 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1625 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1626 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1627 return X.first->getName().compare(Y.first->getName());
1628}
1629
Chris Lattner0b1fb982009-04-10 17:15:23 +00001630/// \brief Writes the block containing the serialized form of the
1631/// preprocessor.
1632///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001633void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001634 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1635 if (PPRec)
1636 WritePreprocessorDetail(*PPRec);
1637
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001638 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001639
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001640 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1641 if (PP.getCounterValue() != 0) {
1642 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001643 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001644 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001645 }
1646
1647 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001648 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Sebastian Redl3397c552010-08-18 23:56:27 +00001650 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001651 // FIXME: use diagnostics subsystem for localization etc.
1652 if (PP.SawDateOrTime())
1653 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Douglas Gregorecdcb882010-10-20 22:00:55 +00001655
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001656 // Loop over all the macro definitions that are live at the end of the file,
1657 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001658
Douglas Gregor9c736102011-02-10 18:20:09 +00001659 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001660 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001661 MacrosToEmit;
1662 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001663 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1664 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001665 I != E; ++I) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001666 const IdentifierInfo *Name = I->first;
Douglas Gregoraa93a872011-10-17 15:32:29 +00001667 if (!IsModule || I->second->isPublic()) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001668 MacroDefinitionsSeen.insert(Name);
Douglas Gregor7143aab2011-09-01 17:04:32 +00001669 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1670 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001671 }
1672
1673 // Sort the set of macro definitions that need to be serialized by the
1674 // name of the macro, to provide a stable ordering.
1675 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1676 &compareMacroDefinitions);
1677
Douglas Gregor040a8042011-02-11 00:26:14 +00001678 // Resolve any identifiers that defined macros at the time they were
1679 // deserialized, adding them to the list of macros to emit (if appropriate).
1680 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1681 IdentifierInfo *Name
1682 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1683 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1684 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1685 }
1686
Douglas Gregor9c736102011-02-10 18:20:09 +00001687 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1688 const IdentifierInfo *Name = MacrosToEmit[I].first;
1689 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001690 if (!MI)
1691 continue;
1692
Sebastian Redl3397c552010-08-18 23:56:27 +00001693 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001694 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001695 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001696
1697 // FIXME: There is a (probably minor) optimization we could do here, if
1698 // the macro comes from the original PCH but the identifier comes from a
1699 // chained PCH, by storing the offset into the original PCH rather than
1700 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001701 if (MI->isBuiltinMacro() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00001702 (Chain &&
1703 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1704 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001705 continue;
1706
Douglas Gregor9c736102011-02-10 18:20:09 +00001707 AddIdentifierRef(Name, Record);
1708 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001709 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1710 Record.push_back(MI->isUsed());
Douglas Gregoraa93a872011-10-17 15:32:29 +00001711 Record.push_back(MI->isPublic());
1712 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001713 unsigned Code;
1714 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001715 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001716 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001717 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001719 Record.push_back(MI->isC99Varargs());
1720 Record.push_back(MI->isGNUVarargs());
1721 Record.push_back(MI->getNumArgs());
1722 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1723 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001724 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001725 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001726
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001727 // If we have a detailed preprocessing record, record the macro definition
1728 // ID that corresponds to this macro.
1729 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001730 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001731
Douglas Gregorc9490c02009-04-16 22:23:12 +00001732 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001733 Record.clear();
1734
Chris Lattnerdf961c22009-04-10 18:08:30 +00001735 // Emit the tokens array.
1736 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1737 // Note that we know that the preprocessor does not have any annotation
1738 // tokens in it because they are created by the parser, and thus can't be
1739 // in a macro definition.
1740 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Chris Lattnerdf961c22009-04-10 18:08:30 +00001742 Record.push_back(Tok.getLocation().getRawEncoding());
1743 Record.push_back(Tok.getLength());
1744
Chris Lattnerdf961c22009-04-10 18:08:30 +00001745 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1746 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001747 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001748 // FIXME: Should translate token kind to a stable encoding.
1749 Record.push_back(Tok.getKind());
1750 // FIXME: Should translate token flags to a stable encoding.
1751 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001753 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001754 Record.clear();
1755 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001756 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001757 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001758 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001759}
1760
1761void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001762 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001763 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001764
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001765 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001766
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001767 // Enter the preprocessor block.
1768 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001769
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001770 // If the preprocessor has a preprocessing record, emit it.
1771 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001772 using namespace llvm;
1773
1774 // Set up the abbreviation for
1775 unsigned InclusionAbbrev = 0;
1776 {
1777 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1778 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1783 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1784 }
1785
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001786 unsigned FirstPreprocessorEntityID
1787 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1788 + NUM_PREDEF_PP_ENTITY_IDS;
1789 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001790 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001791 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1792 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001793 E != EEnd;
1794 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001795 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001796
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001797 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1798 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001799
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001800 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001801 // Record this macro definition's ID.
1802 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001803
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001804 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001805 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1806 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001807 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001808
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001809 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001810 Record.push_back(ME->isBuiltinMacro());
1811 if (ME->isBuiltinMacro())
1812 AddIdentifierRef(ME->getName(), Record);
1813 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001814 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001815 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001816 continue;
1817 }
1818
1819 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1820 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001821 Record.push_back(ID->getFileName().size());
1822 Record.push_back(ID->wasInQuotes());
1823 Record.push_back(static_cast<unsigned>(ID->getKind()));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001824 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001825 Buffer += ID->getFileName();
1826 Buffer += ID->getFile()->getName();
1827 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1828 continue;
1829 }
1830
1831 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1832 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001833 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001834
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001835 // Write the offsets table for the preprocessing record.
1836 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001837 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1838
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001839 // Write the offsets table for identifier IDs.
1840 using namespace llvm;
1841 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001842 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001845 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001846
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001847 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001848 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001849 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001850 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1851 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001852 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001853}
1854
Douglas Gregore209e502011-12-06 01:10:29 +00001855unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1856 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1857 if (Known != SubmoduleIDs.end())
1858 return Known->second;
1859
1860 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1861}
1862
Douglas Gregor26ced122011-12-01 00:59:36 +00001863/// \brief Compute the number of modules within the given tree (including the
1864/// given module).
1865static unsigned getNumberOfModules(Module *Mod) {
1866 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001867 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1868 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001869 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001870 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001871
1872 return ChildModules + 1;
1873}
1874
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001875void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001876 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001877 // FIXME: This feels like it belongs somewhere else, but there are no
1878 // other consumers of this information.
1879 SourceManager &SrcMgr = PP->getSourceManager();
1880 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1881 for (ASTContext::import_iterator I = Context->local_import_begin(),
1882 IEnd = Context->local_import_end();
1883 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001884 if (Module *ImportedFrom
1885 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1886 SrcMgr))) {
1887 ImportedFrom->Imports.push_back(I->getImportedModule());
1888 }
1889 }
1890
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001891 // Enter the submodule description block.
1892 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1893
1894 // Write the abbreviations needed for the submodules block.
1895 using namespace llvm;
1896 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1897 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1907 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1908
1909 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001910 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1912 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1913
1914 Abbrev = new BitCodeAbbrev();
1915 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1917 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001918
1919 Abbrev = new BitCodeAbbrev();
1920 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1922 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1923
Douglas Gregor51f564f2011-12-31 04:05:44 +00001924 Abbrev = new BitCodeAbbrev();
1925 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1927 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1928
Douglas Gregor26ced122011-12-01 00:59:36 +00001929 // Write the submodule metadata block.
1930 RecordData Record;
1931 Record.push_back(getNumberOfModules(WritingModule));
1932 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1933 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1934
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001935 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001936 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001937 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001938 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001939 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001940 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001941 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001942
1943 // Emit the definition of the block.
1944 Record.clear();
1945 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001946 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001947 if (Mod->Parent) {
1948 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1949 Record.push_back(SubmoduleIDs[Mod->Parent]);
1950 } else {
1951 Record.push_back(0);
1952 }
1953 Record.push_back(Mod->IsFramework);
1954 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001955 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00001956 Record.push_back(Mod->InferSubmodules);
1957 Record.push_back(Mod->InferExplicitSubmodules);
1958 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001959 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1960
Douglas Gregor51f564f2011-12-31 04:05:44 +00001961 // Emit the requirements.
1962 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1963 Record.clear();
1964 Record.push_back(SUBMODULE_REQUIRES);
1965 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1966 Mod->Requires[I].data(),
1967 Mod->Requires[I].size());
1968 }
1969
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001970 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00001971 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001972 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001973 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001974 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00001975 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00001976 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1977 Record.clear();
1978 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1979 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1980 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001981 }
1982
1983 // Emit the headers.
1984 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1985 Record.clear();
1986 Record.push_back(SUBMODULE_HEADER);
1987 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1988 Mod->Headers[I]->getName());
1989 }
Douglas Gregor55988682011-12-05 16:33:54 +00001990
1991 // Emit the imports.
1992 if (!Mod->Imports.empty()) {
1993 Record.clear();
1994 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00001995 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00001996 assert(ImportedID && "Unknown submodule!");
1997 Record.push_back(ImportedID);
1998 }
1999 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2000 }
2001
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002002 // Emit the exports.
2003 if (!Mod->Exports.empty()) {
2004 Record.clear();
2005 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002006 if (Module *Exported = Mod->Exports[I].getPointer()) {
2007 unsigned ExportedID = SubmoduleIDs[Exported];
2008 assert(ExportedID > 0 && "Unknown submodule ID?");
2009 Record.push_back(ExportedID);
2010 } else {
2011 Record.push_back(0);
2012 }
2013
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002014 Record.push_back(Mod->Exports[I].getInt());
2015 }
2016 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2017 }
2018
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002019 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002020 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2021 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002022 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002023 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002024 }
2025
2026 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002027
2028 assert((NextSubmoduleID - FirstSubmoduleID
2029 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002030}
2031
Douglas Gregor185dbd72011-12-01 02:07:58 +00002032serialization::SubmoduleID
2033ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002034 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002035 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002036
2037 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002038 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002039 Module *OwningMod
2040 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002041 if (!OwningMod)
2042 return 0;
2043
Douglas Gregore209e502011-12-06 01:10:29 +00002044 // Check whether this submodule is part of our own module.
2045 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002046 return 0;
2047
Douglas Gregore209e502011-12-06 01:10:29 +00002048 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002049}
2050
David Blaikied6471f72011-09-25 23:23:43 +00002051void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002052 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002053 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002054 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2055 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002056 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002057 if (point.Loc.isInvalid())
2058 continue;
2059
2060 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002061 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002062 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002063 if (I->second.isPragma()) {
2064 Record.push_back(I->first);
2065 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002066 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002067 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002068 Record.push_back(-1); // mark the end of the diag/map pairs for this
2069 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002070 }
2071
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002072 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002073 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002074}
2075
Anders Carlssonc8505782011-03-06 18:41:18 +00002076void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2077 if (CXXBaseSpecifiersOffsets.empty())
2078 return;
2079
2080 RecordData Record;
2081
2082 // Create a blob abbreviation for the C++ base specifiers offsets.
2083 using namespace llvm;
2084
2085 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2086 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2089 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2090
Douglas Gregore92b8a12011-08-04 00:01:48 +00002091 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002092 Record.clear();
2093 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2094 Record.push_back(CXXBaseSpecifiersOffsets.size());
2095 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002096 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002097}
2098
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002099//===----------------------------------------------------------------------===//
2100// Type Serialization
2101//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002102
Sebastian Redl3397c552010-08-18 23:56:27 +00002103/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002104void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002105 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002106 if (Idx.getIndex() == 0) // we haven't seen this type before.
2107 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002108
Douglas Gregor97475832010-10-05 18:37:06 +00002109 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002110
Douglas Gregor2cf26342009-04-09 22:27:44 +00002111 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002112 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002113 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002114 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002115 else if (TypeOffsets.size() < Index) {
2116 TypeOffsets.resize(Index + 1);
2117 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002118 }
2119
2120 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002121
Douglas Gregor2cf26342009-04-09 22:27:44 +00002122 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002123 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002124
Douglas Gregora4923eb2009-11-16 21:35:15 +00002125 if (T.hasLocalNonFastQualifiers()) {
2126 Qualifiers Qs = T.getLocalQualifiers();
2127 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002128 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002129 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002130 } else {
2131 switch (T->getTypeClass()) {
2132 // For all of the concrete, non-dependent types, call the
2133 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002134#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002135 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002136#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002137#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002138 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002139 }
2140
2141 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002142 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002143
2144 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002145 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002146}
2147
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002148//===----------------------------------------------------------------------===//
2149// Declaration Serialization
2150//===----------------------------------------------------------------------===//
2151
Douglas Gregor2cf26342009-04-09 22:27:44 +00002152/// \brief Write the block containing all of the declaration IDs
2153/// lexically declared within the given DeclContext.
2154///
2155/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2156/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002157uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002158 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002159 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002160 return 0;
2161
Douglas Gregorc9490c02009-04-16 22:23:12 +00002162 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002163 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002164 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002165 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002166 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2167 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002168 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002169
Douglas Gregor25123082009-04-22 22:34:57 +00002170 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002171 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002172 return Offset;
2173}
2174
Sebastian Redla4232eb2010-08-18 23:56:21 +00002175void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002176 using namespace llvm;
2177 RecordData Record;
2178
2179 // Write the type offsets array
2180 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002181 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2185 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2186 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002187 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002188 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002189 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002190 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002191
2192 // Write the declaration offsets array
2193 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002194 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2198 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2199 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002200 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002201 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002202 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002203 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002204}
2205
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002206void ASTWriter::WriteFileDeclIDsMap() {
2207 using namespace llvm;
2208 RecordData Record;
2209
2210 // Join the vectors of DeclIDs from all files.
2211 SmallVector<DeclID, 256> FileSortedIDs;
2212 for (FileDeclIDsTy::iterator
2213 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2214 DeclIDInFileInfo &Info = *FI->second;
2215 Info.FirstDeclIndex = FileSortedIDs.size();
2216 for (LocDeclIDsTy::iterator
2217 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2218 FileSortedIDs.push_back(DI->second);
2219 }
2220
2221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2222 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2224 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2225 Record.push_back(FILE_SORTED_DECLS);
2226 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2227}
2228
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002229//===----------------------------------------------------------------------===//
2230// Global Method Pool and Selector Serialization
2231//===----------------------------------------------------------------------===//
2232
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002233namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002234// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002235class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002236 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002237
2238public:
2239 typedef Selector key_type;
2240 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Sebastian Redl5d050072010-08-04 17:20:04 +00002242 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002243 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002244 ObjCMethodList Instance, Factory;
2245 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002246 typedef const data_type& data_type_ref;
2247
Sebastian Redl3397c552010-08-18 23:56:27 +00002248 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002249
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002250 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002251 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002252 }
Mike Stump1eb44332009-09-09 15:08:12 +00002253
2254 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002255 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002256 data_type_ref Methods) {
2257 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2258 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002259 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2260 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002261 Method = Method->Next)
2262 if (Method->Method)
2263 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002264 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002265 Method = Method->Next)
2266 if (Method->Method)
2267 DataLen += 4;
2268 clang::io::Emit16(Out, DataLen);
2269 return std::make_pair(KeyLen, DataLen);
2270 }
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Chris Lattner5f9e2722011-07-23 10:55:15 +00002272 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002273 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002274 assert((Start >> 32) == 0 && "Selector key offset too large");
2275 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002276 unsigned N = Sel.getNumArgs();
2277 clang::io::Emit16(Out, N);
2278 if (N == 0)
2279 N = 1;
2280 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002281 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002282 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2283 }
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Chris Lattner5f9e2722011-07-23 10:55:15 +00002285 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002286 data_type_ref Methods, unsigned DataLen) {
2287 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002288 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002289 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002290 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002291 Method = Method->Next)
2292 if (Method->Method)
2293 ++NumInstanceMethods;
2294
2295 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002296 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002297 Method = Method->Next)
2298 if (Method->Method)
2299 ++NumFactoryMethods;
2300
2301 clang::io::Emit16(Out, NumInstanceMethods);
2302 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002303 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002304 Method = Method->Next)
2305 if (Method->Method)
2306 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002307 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002308 Method = Method->Next)
2309 if (Method->Method)
2310 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002311
2312 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002313 }
2314};
2315} // end anonymous namespace
2316
Sebastian Redl059612d2010-08-03 21:58:15 +00002317/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002318///
2319/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002320/// in an on-disk hash table indexed by the selector. The hash table also
2321/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002322void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002323 using namespace llvm;
2324
Sebastian Redl059612d2010-08-03 21:58:15 +00002325 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002326 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002327 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002328 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002329 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002330 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002331 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002332 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Sebastian Redl059612d2010-08-03 21:58:15 +00002334 // Create the on-disk hash table representation. We walk through every
2335 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002336 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002337 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002338 I = SelectorIDs.begin(), E = SelectorIDs.end();
2339 I != E; ++I) {
2340 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002341 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002342 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002343 I->second,
2344 ObjCMethodList(),
2345 ObjCMethodList()
2346 };
2347 if (F != SemaRef.MethodPool.end()) {
2348 Data.Instance = F->second.first;
2349 Data.Factory = F->second.second;
2350 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002351 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002352 // changed.
2353 if (Chain && I->second < FirstSelectorID) {
2354 // Selector already exists. Did it change?
2355 bool changed = false;
2356 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2357 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002358 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002359 changed = true;
2360 }
2361 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2362 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002363 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002364 changed = true;
2365 }
2366 if (!changed)
2367 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002368 } else if (Data.Instance.Method || Data.Factory.Method) {
2369 // A new method pool entry.
2370 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002371 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002372 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002373 }
2374
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002375 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002376 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002377 uint32_t BucketOffset;
2378 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002379 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002380 llvm::raw_svector_ostream Out(MethodPool);
2381 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002382 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002383 BucketOffset = Generator.Emit(Out, Trait);
2384 }
2385
2386 // Create a blob abbreviation
2387 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002388 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2392 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2393
Douglas Gregor83941df2009-04-25 17:48:32 +00002394 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002395 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002396 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002397 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002398 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002399 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002400
2401 // Create a blob abbreviation for the selector table offsets.
2402 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002403 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2407 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2408
2409 // Write the selector offsets table.
2410 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002411 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002412 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002413 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002414 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002415 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002416 }
2417}
2418
Sebastian Redl3397c552010-08-18 23:56:27 +00002419/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002420void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002421 using namespace llvm;
2422 if (SemaRef.ReferencedSelectors.empty())
2423 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002424
Fariborz Jahanian32019832010-07-23 19:11:11 +00002425 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002426
Sebastian Redl3397c552010-08-18 23:56:27 +00002427 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002428 // very tricky to fix, and given that @selector shouldn't really appear in
2429 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002430 for (DenseMap<Selector, SourceLocation>::iterator S =
2431 SemaRef.ReferencedSelectors.begin(),
2432 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2433 Selector Sel = (*S).first;
2434 SourceLocation Loc = (*S).second;
2435 AddSelectorRef(Sel, Record);
2436 AddSourceLocation(Loc, Record);
2437 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002438 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002439}
2440
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002441//===----------------------------------------------------------------------===//
2442// Identifier Table Serialization
2443//===----------------------------------------------------------------------===//
2444
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002445namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002446class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002447 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002448 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002449 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002450 bool IsModule;
2451
Douglas Gregora92193e2009-04-28 21:18:29 +00002452 /// \brief Determines whether this is an "interesting" identifier
2453 /// that needs a full IdentifierInfo structure written into the hash
2454 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002455 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002456 if (II->isPoisoned() ||
2457 II->isExtensionToken() ||
2458 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002459 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002460 II->getFETokenInfo<void>())
2461 return true;
2462
Douglas Gregorce835df2011-09-14 22:14:14 +00002463 return hasMacroDefinition(II, Macro);
2464 }
2465
2466 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002467 if (!II->hasMacroDefinition())
2468 return false;
2469
Douglas Gregorce835df2011-09-14 22:14:14 +00002470 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002471 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002472
Douglas Gregorce835df2011-09-14 22:14:14 +00002473 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002474 }
2475
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002476public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002477 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002478 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002479
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002480 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002481 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Douglas Gregoreee242f2011-10-27 09:33:13 +00002483 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2484 IdentifierResolver &IdResolver, bool IsModule)
2485 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002486
2487 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002488 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002489 }
Mike Stump1eb44332009-09-09 15:08:12 +00002490
2491 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002492 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002493 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002494 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002495 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002496 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002497 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002498 if (hasMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002499 DataLen += 8;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002500
2501 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2502 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002503 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002504 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002505 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002506 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002507 // We emit the key length after the data length so that every
2508 // string is preceded by a 16-bit length. This matches the PTH
2509 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002510 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002511 return std::make_pair(KeyLen, DataLen);
2512 }
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Chris Lattner5f9e2722011-07-23 10:55:15 +00002514 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002515 unsigned KeyLen) {
2516 // Record the location of the key data. This is used when generating
2517 // the mapping from persistent IDs to strings.
2518 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002519 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002520 }
Mike Stump1eb44332009-09-09 15:08:12 +00002521
Douglas Gregor7143aab2011-09-01 17:04:32 +00002522 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002523 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002524 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002525 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002526 clang::io::Emit32(Out, ID << 1);
2527 return;
2528 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002529
Douglas Gregora92193e2009-04-28 21:18:29 +00002530 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002531 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002532 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002533 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topper925be542011-12-19 05:04:33 +00002534 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregorce835df2011-09-14 22:14:14 +00002535 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002536 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2537 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002538 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002539 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002540 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002541
Douglas Gregor13292642011-12-02 15:45:10 +00002542 if (HasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002543 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor13292642011-12-02 15:45:10 +00002544 clang::io::Emit32(Out,
2545 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2546 }
2547
Douglas Gregor668c1a42009-04-21 22:25:48 +00002548 // Emit the declaration IDs in reverse order, because the
2549 // IdentifierResolver provides the declarations as they would be
2550 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002551 // "stat"), but the ASTReader adds declarations to the end of the list
2552 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002553 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002554 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2555 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002556 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002557 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002558 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002559 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002560 }
2561};
2562} // end anonymous namespace
2563
Sebastian Redl3397c552010-08-18 23:56:27 +00002564/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002565///
2566/// The identifier table consists of a blob containing string data
2567/// (the actual identifiers themselves) and a separate "offsets" index
2568/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002569void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2570 IdentifierResolver &IdResolver,
2571 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002572 using namespace llvm;
2573
2574 // Create and write out the blob that contains the identifier
2575 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002576 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002577 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002578 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002579
Douglas Gregor92b059e2009-04-28 20:33:11 +00002580 // Look for any identifiers that were named while processing the
2581 // headers, but are otherwise not needed. We add these to the hash
2582 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002583 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002584 // file.
2585 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2586 IDEnd = PP.getIdentifierTable().end();
2587 ID != IDEnd; ++ID)
2588 getIdentifierRef(ID->second);
2589
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002590 // Create the on-disk hash table representation. We only store offsets
2591 // for identifiers that appear here for the first time.
2592 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002593 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002594 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2595 ID != IDEnd; ++ID) {
2596 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002597 if (!Chain || !ID->first->isFromAST() ||
2598 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002599 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2600 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002601 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002602
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002603 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002604 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002605 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002606 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002607 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002608 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002609 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002610 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002611 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002612 }
2613
2614 // Create a blob abbreviation
2615 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002616 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002619 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002620
2621 // Write the identifier table
2622 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002623 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002624 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002625 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002626 }
2627
2628 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002629 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002630 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2634 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2635
2636 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002637 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002638 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002639 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002640 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002641 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002642}
2643
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002644//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002645// DeclContext's Name Lookup Table Serialization
2646//===----------------------------------------------------------------------===//
2647
2648namespace {
2649// Trait used for the on-disk hash table used in the method pool.
2650class ASTDeclContextNameLookupTrait {
2651 ASTWriter &Writer;
2652
2653public:
2654 typedef DeclarationName key_type;
2655 typedef key_type key_type_ref;
2656
2657 typedef DeclContext::lookup_result data_type;
2658 typedef const data_type& data_type_ref;
2659
2660 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2661
2662 unsigned ComputeHash(DeclarationName Name) {
2663 llvm::FoldingSetNodeID ID;
2664 ID.AddInteger(Name.getNameKind());
2665
2666 switch (Name.getNameKind()) {
2667 case DeclarationName::Identifier:
2668 ID.AddString(Name.getAsIdentifierInfo()->getName());
2669 break;
2670 case DeclarationName::ObjCZeroArgSelector:
2671 case DeclarationName::ObjCOneArgSelector:
2672 case DeclarationName::ObjCMultiArgSelector:
2673 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2674 break;
2675 case DeclarationName::CXXConstructorName:
2676 case DeclarationName::CXXDestructorName:
2677 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002678 break;
2679 case DeclarationName::CXXOperatorName:
2680 ID.AddInteger(Name.getCXXOverloadedOperator());
2681 break;
2682 case DeclarationName::CXXLiteralOperatorName:
2683 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2684 case DeclarationName::CXXUsingDirective:
2685 break;
2686 }
2687
2688 return ID.ComputeHash();
2689 }
2690
2691 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002692 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002693 data_type_ref Lookup) {
2694 unsigned KeyLen = 1;
2695 switch (Name.getNameKind()) {
2696 case DeclarationName::Identifier:
2697 case DeclarationName::ObjCZeroArgSelector:
2698 case DeclarationName::ObjCOneArgSelector:
2699 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002700 case DeclarationName::CXXLiteralOperatorName:
2701 KeyLen += 4;
2702 break;
2703 case DeclarationName::CXXOperatorName:
2704 KeyLen += 1;
2705 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002706 case DeclarationName::CXXConstructorName:
2707 case DeclarationName::CXXDestructorName:
2708 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002709 case DeclarationName::CXXUsingDirective:
2710 break;
2711 }
2712 clang::io::Emit16(Out, KeyLen);
2713
2714 // 2 bytes for num of decls and 4 for each DeclID.
2715 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2716 clang::io::Emit16(Out, DataLen);
2717
2718 return std::make_pair(KeyLen, DataLen);
2719 }
2720
Chris Lattner5f9e2722011-07-23 10:55:15 +00002721 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002722 using namespace clang::io;
2723
2724 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2725 Emit8(Out, Name.getNameKind());
2726 switch (Name.getNameKind()) {
2727 case DeclarationName::Identifier:
2728 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2729 break;
2730 case DeclarationName::ObjCZeroArgSelector:
2731 case DeclarationName::ObjCOneArgSelector:
2732 case DeclarationName::ObjCMultiArgSelector:
2733 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2734 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002735 case DeclarationName::CXXOperatorName:
2736 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2737 Emit8(Out, Name.getCXXOverloadedOperator());
2738 break;
2739 case DeclarationName::CXXLiteralOperatorName:
2740 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2741 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002742 case DeclarationName::CXXConstructorName:
2743 case DeclarationName::CXXDestructorName:
2744 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002745 case DeclarationName::CXXUsingDirective:
2746 break;
2747 }
2748 }
2749
Chris Lattner5f9e2722011-07-23 10:55:15 +00002750 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002751 data_type Lookup, unsigned DataLen) {
2752 uint64_t Start = Out.tell(); (void)Start;
2753 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2754 for (; Lookup.first != Lookup.second; ++Lookup.first)
2755 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2756
2757 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2758 }
2759};
2760} // end anonymous namespace
2761
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002762/// \brief Write the block containing all of the declaration IDs
2763/// visible from the given DeclContext.
2764///
2765/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002766/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002767uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2768 DeclContext *DC) {
2769 if (DC->getPrimaryContext() != DC)
2770 return 0;
2771
2772 // Since there is no name lookup into functions or methods, don't bother to
2773 // build a visible-declarations table for these entities.
2774 if (DC->isFunctionOrMethod())
2775 return 0;
2776
2777 // If not in C++, we perform name lookup for the translation unit via the
2778 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2779 // FIXME: In C++ we need the visible declarations in order to "see" the
2780 // friend declarations, is there a way to do this without writing the table ?
2781 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2782 return 0;
2783
2784 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002785 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002786 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002787
2788 // Serialize the contents of the mapping used for lookup. Note that,
2789 // although we have two very different code paths, the serialized
2790 // representation is the same for both cases: a declaration name,
2791 // followed by a size, followed by references to the visible
2792 // declarations that have that name.
2793 uint64_t Offset = Stream.GetCurrentBitNo();
2794 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2795 if (!Map || Map->empty())
2796 return 0;
2797
2798 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2799 ASTDeclContextNameLookupTrait Trait(*this);
2800
2801 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002802 DeclarationName ConversionName;
2803 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002804 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2805 D != DEnd; ++D) {
2806 DeclarationName Name = D->first;
2807 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002808 if (Result.first != Result.second) {
2809 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2810 // Hash all conversion function names to the same name. The actual
2811 // type information in conversion function name is not used in the
2812 // key (since such type information is not stable across different
2813 // modules), so the intended effect is to coalesce all of the conversion
2814 // functions under a single key.
2815 if (!ConversionName)
2816 ConversionName = Name;
2817 ConversionDecls.append(Result.first, Result.second);
2818 continue;
2819 }
2820
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002821 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002822 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002823 }
2824
Douglas Gregore5a54b62011-08-30 20:49:19 +00002825 // Add the conversion functions
2826 if (!ConversionDecls.empty()) {
2827 Generator.insert(ConversionName,
2828 DeclContext::lookup_result(ConversionDecls.begin(),
2829 ConversionDecls.end()),
2830 Trait);
2831 }
2832
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002833 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002834 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002835 uint32_t BucketOffset;
2836 {
2837 llvm::raw_svector_ostream Out(LookupTable);
2838 // Make sure that no bucket is at offset 0
2839 clang::io::Emit32(Out, 0);
2840 BucketOffset = Generator.Emit(Out, Trait);
2841 }
2842
2843 // Write the lookup table
2844 RecordData Record;
2845 Record.push_back(DECL_CONTEXT_VISIBLE);
2846 Record.push_back(BucketOffset);
2847 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2848 LookupTable.str());
2849
2850 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2851 ++NumVisibleDeclContexts;
2852 return Offset;
2853}
2854
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002855/// \brief Write an UPDATE_VISIBLE block for the given context.
2856///
2857/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2858/// DeclContext in a dependent AST file. As such, they only exist for the TU
2859/// (in C++) and for namespaces.
2860void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002861 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2862 if (!Map || Map->empty())
2863 return;
2864
2865 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2866 ASTDeclContextNameLookupTrait Trait(*this);
2867
2868 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002869 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2870 D != DEnd; ++D) {
2871 DeclarationName Name = D->first;
2872 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002873 // For any name that appears in this table, the results are complete, i.e.
2874 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002875 if (Result.first != Result.second)
2876 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002877 }
2878
2879 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002880 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002881 uint32_t BucketOffset;
2882 {
2883 llvm::raw_svector_ostream Out(LookupTable);
2884 // Make sure that no bucket is at offset 0
2885 clang::io::Emit32(Out, 0);
2886 BucketOffset = Generator.Emit(Out, Trait);
2887 }
2888
2889 // Write the lookup table
2890 RecordData Record;
2891 Record.push_back(UPDATE_VISIBLE);
2892 Record.push_back(getDeclID(cast<Decl>(DC)));
2893 Record.push_back(BucketOffset);
2894 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2895}
2896
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002897/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2898void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2899 RecordData Record;
2900 Record.push_back(Opts.fp_contract);
2901 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2902}
2903
2904/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2905void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2906 if (!SemaRef.Context.getLangOptions().OpenCL)
2907 return;
2908
2909 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2910 RecordData Record;
2911#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2912#include "clang/Basic/OpenCLExtensions.def"
2913 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2914}
2915
Douglas Gregor2171bf12012-01-15 16:58:34 +00002916void ASTWriter::WriteRedeclarations() {
2917 RecordData LocalRedeclChains;
2918 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2919
2920 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2921 Decl *First = Redeclarations[I];
2922 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2923
2924 Decl *MostRecent = First->getMostRecentDecl();
2925
2926 // If we only have a single declaration, there is no point in storing
2927 // a redeclaration chain.
2928 if (First == MostRecent)
2929 continue;
2930
2931 unsigned Offset = LocalRedeclChains.size();
2932 unsigned Size = 0;
2933 LocalRedeclChains.push_back(0); // Placeholder for the size.
2934
2935 // Collect the set of local redeclarations of this declaration.
2936 for (Decl *Prev = MostRecent; Prev != First;
2937 Prev = Prev->getPreviousDecl()) {
2938 if (!Prev->isFromASTFile()) {
2939 AddDeclRef(Prev, LocalRedeclChains);
2940 ++Size;
2941 }
2942 }
2943 LocalRedeclChains[Offset] = Size;
2944
2945 // Reverse the set of local redeclarations, so that we store them in
2946 // order (since we found them in reverse order).
2947 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2948
2949 // Add the mapping from the first ID to the set of local declarations.
2950 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2951 LocalRedeclsMap.push_back(Info);
2952
2953 assert(N == Redeclarations.size() &&
2954 "Deserialized a declaration we shouldn't have");
2955 }
2956
2957 if (LocalRedeclChains.empty())
2958 return;
2959
2960 // Sort the local redeclarations map by the first declaration ID,
2961 // since the reader will be performing binary searches on this information.
2962 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2963
2964 // Emit the local redeclarations map.
2965 using namespace llvm;
2966 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2967 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2970 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
2971
2972 RecordData Record;
2973 Record.push_back(LOCAL_REDECLARATIONS_MAP);
2974 Record.push_back(LocalRedeclsMap.size());
2975 Stream.EmitRecordWithBlob(AbbrevID, Record,
2976 reinterpret_cast<char*>(LocalRedeclsMap.data()),
2977 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
2978
2979 // Emit the redeclaration chains.
2980 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
2981}
2982
Douglas Gregorcff9f262012-01-27 01:47:08 +00002983void ASTWriter::WriteObjCCategories() {
2984 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
2985 RecordData Categories;
2986
2987 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
2988 unsigned Size = 0;
2989 unsigned StartIndex = Categories.size();
2990
2991 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
2992
2993 // Allocate space for the size.
2994 Categories.push_back(0);
2995
2996 // Add the categories.
2997 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
2998 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
2999 assert(getDeclID(Cat) != 0 && "Bogus category");
3000 AddDeclRef(Cat, Categories);
3001 }
3002
3003 // Update the size.
3004 Categories[StartIndex] = Size;
3005
3006 // Record this interface -> category map.
3007 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3008 CategoriesMap.push_back(CatInfo);
3009 }
3010
3011 // Sort the categories map by the definition ID, since the reader will be
3012 // performing binary searches on this information.
3013 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3014
3015 // Emit the categories map.
3016 using namespace llvm;
3017 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3018 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3021 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3022
3023 RecordData Record;
3024 Record.push_back(OBJC_CATEGORIES_MAP);
3025 Record.push_back(CategoriesMap.size());
3026 Stream.EmitRecordWithBlob(AbbrevID, Record,
3027 reinterpret_cast<char*>(CategoriesMap.data()),
3028 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3029
3030 // Emit the category lists.
3031 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3032}
3033
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003034void ASTWriter::WriteMergedDecls() {
3035 if (!Chain || Chain->MergedDecls.empty())
3036 return;
3037
3038 RecordData Record;
3039 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3040 IEnd = Chain->MergedDecls.end();
3041 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003042 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003043 : getDeclID(I->first);
3044 assert(CanonID && "Merged declaration not known?");
3045
3046 Record.push_back(CanonID);
3047 Record.push_back(I->second.size());
3048 Record.append(I->second.begin(), I->second.end());
3049 }
3050 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3051}
3052
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003053//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003054// General Serialization Routines
3055//===----------------------------------------------------------------------===//
3056
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003057/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003058void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003059 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00003060 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
3061 const Attr * A = *i;
3062 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003063 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003064
Sean Huntcf807c42010-08-18 23:23:40 +00003065#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003066
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003067 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003068}
3069
Chris Lattner5f9e2722011-07-23 10:55:15 +00003070void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003071 Record.push_back(Str.size());
3072 Record.insert(Record.end(), Str.begin(), Str.end());
3073}
3074
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003075void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3076 RecordDataImpl &Record) {
3077 Record.push_back(Version.getMajor());
3078 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3079 Record.push_back(*Minor + 1);
3080 else
3081 Record.push_back(0);
3082 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3083 Record.push_back(*Subminor + 1);
3084 else
3085 Record.push_back(0);
3086}
3087
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003088/// \brief Note that the identifier II occurs at the given offset
3089/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003090void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003091 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003092 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003093 // up earlier in the chain and thus don't need an offset.
3094 if (ID >= FirstIdentID)
3095 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003096}
3097
Douglas Gregor83941df2009-04-25 17:48:32 +00003098/// \brief Note that the selector Sel occurs at the given offset
3099/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003100void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003101 unsigned ID = SelectorIDs[Sel];
3102 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003103 // Don't record offsets for selectors that are also available in a different
3104 // file.
3105 if (ID < FirstSelectorID)
3106 return;
3107 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003108}
3109
Sebastian Redla4232eb2010-08-18 23:56:21 +00003110ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003111 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
3112 WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003113 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003114 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003115 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003116 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3117 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003118 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003119 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003120 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003121 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003122 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003123 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003124 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3125 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3126 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003127 DeclTypedefAbbrev(0),
3128 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3129 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003130{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003131}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003132
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003133ASTWriter::~ASTWriter() {
3134 for (FileDeclIDsTy::iterator
3135 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3136 delete I->second;
3137}
3138
Sebastian Redla4232eb2010-08-18 23:56:21 +00003139void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003140 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003141 Module *WritingModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003142 WritingAST = true;
3143
Douglas Gregor2cf26342009-04-09 22:27:44 +00003144 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003145 Stream.Emit((unsigned)'C', 8);
3146 Stream.Emit((unsigned)'P', 8);
3147 Stream.Emit((unsigned)'C', 8);
3148 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003149
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003150 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003151
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003152 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003153 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003154 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003155 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003156 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003157 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003158 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003159
3160 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003161}
3162
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003163template<typename Vector>
3164static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3165 ASTWriter::RecordData &Record) {
3166 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3167 I != E; ++I) {
3168 Writer.AddDeclRef(*I, Record);
3169 }
3170}
3171
Sebastian Redla4232eb2010-08-18 23:56:21 +00003172void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003173 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003174 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003175 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003176 using namespace llvm;
3177
Douglas Gregorecc2c092011-12-01 22:20:10 +00003178 // Make sure that the AST reader knows to finalize itself.
3179 if (Chain)
3180 Chain->finalizeForWriting();
3181
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003182 ASTContext &Context = SemaRef.Context;
3183 Preprocessor &PP = SemaRef.PP;
3184
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003185 // Set up predefined declaration IDs.
3186 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003187 if (Context.ObjCIdDecl)
3188 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003189 if (Context.ObjCSelDecl)
3190 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003191 if (Context.ObjCClassDecl)
3192 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003193 if (Context.ObjCProtocolClassDecl)
3194 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003195 if (Context.Int128Decl)
3196 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3197 if (Context.UInt128Decl)
3198 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003199 if (Context.ObjCInstanceTypeDecl)
3200 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003201
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003202 if (!Chain) {
3203 // Make sure that we emit IdentifierInfos (and any attached
3204 // declarations) for builtins. We don't need to do this when we're
3205 // emitting chained PCH files, because all of the builtins will be
3206 // in the original PCH file.
3207 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003208 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003209 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003210 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
3211 Context.getLangOptions().NoBuiltin);
3212 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3213 getIdentifierRef(&Table.get(BuiltinNames[I]));
3214 }
3215
Douglas Gregoreee242f2011-10-27 09:33:13 +00003216 // If there are any out-of-date identifiers, bring them up to date.
3217 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3218 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3219 IDEnd = PP.getIdentifierTable().end();
3220 ID != IDEnd; ++ID)
3221 if (ID->second->isOutOfDate())
3222 ExtSource->updateOutOfDateIdentifier(*ID->second);
3223 }
3224
Chris Lattner63d65f82009-09-08 18:19:27 +00003225 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003226 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003227 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003228 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003229 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003230
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003231 // Build a record containing all of the file scoped decls in this file.
3232 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003233 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3234 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003235
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003236 // Build a record containing all of the delegating constructors we still need
3237 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003238 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003239 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003240
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003241 // Write the set of weak, undeclared identifiers. We always write the
3242 // entire table, since later PCH files in a PCH chain are only interested in
3243 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003244 RecordData WeakUndeclaredIdentifiers;
3245 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003246 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003247 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3248 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3249 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3250 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3251 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3252 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3253 }
3254 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003255
Douglas Gregor14c22f22009-04-22 22:18:58 +00003256 // Build a record containing all of the locally-scoped external
3257 // declarations in this header file. Generally, this record will be
3258 // empty.
3259 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003260 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003261 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003262 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003263 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3264 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003265 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003266 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003267 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3268 }
3269
Douglas Gregorb81c1702009-04-27 20:06:05 +00003270 // Build a record containing all of the ext_vector declarations.
3271 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003272 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003273
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003274 // Build a record containing all of the VTable uses information.
3275 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003276 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003277 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3278 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3279 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3280 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3281 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003282 }
3283
3284 // Build a record containing all of dynamic classes declarations.
3285 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003286 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003287
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003288 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003289 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003290 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003291 I = SemaRef.PendingInstantiations.begin(),
3292 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3293 AddDeclRef(I->first, PendingInstantiations);
3294 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003295 }
3296 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3297 "There are local ones at end of translation unit!");
3298
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003299 // Build a record containing some declaration references.
3300 RecordData SemaDeclRefs;
3301 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3302 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3303 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3304 }
3305
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003306 RecordData CUDASpecialDeclRefs;
3307 if (Context.getcudaConfigureCallDecl()) {
3308 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3309 }
3310
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003311 // Build a record containing all of the known namespaces.
3312 RecordData KnownNamespaces;
3313 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3314 I = SemaRef.KnownNamespaces.begin(),
3315 IEnd = SemaRef.KnownNamespaces.end();
3316 I != IEnd; ++I) {
3317 if (!I->second)
3318 AddDeclRef(I->first, KnownNamespaces);
3319 }
3320
Sebastian Redl3397c552010-08-18 23:56:27 +00003321 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003322 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003323 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003324 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003325 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00003326 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003327 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003328
3329 // Create a lexical update block containing all of the declarations in the
3330 // translation unit that do not come from other AST files.
3331 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3332 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3333 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3334 E = TU->noload_decls_end();
3335 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003336 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003337 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003338 }
3339
3340 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3341 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3342 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3343 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3344 Record.clear();
3345 Record.push_back(TU_UPDATE_LEXICAL);
3346 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3347 data(NewGlobalDecls));
3348
3349 // And a visible updates block for the translation unit.
3350 Abv = new llvm::BitCodeAbbrev();
3351 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3352 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3353 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3354 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3355 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3356 WriteDeclContextVisibleUpdate(TU);
3357
3358 // If the translation unit has an anonymous namespace, and we don't already
3359 // have an update block for it, write it as an update block.
3360 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3361 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3362 if (Record.empty()) {
3363 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003364 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003365 }
3366 }
3367
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003368 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003369 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003370
Douglas Gregora119da02011-08-02 16:26:37 +00003371 // Form the record of special types.
3372 RecordData SpecialTypes;
3373 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003374 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003375 AddTypeRef(Context.getFILEType(), SpecialTypes);
3376 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3377 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3378 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3379 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003380 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003381 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003382
Douglas Gregor366809a2009-04-26 03:49:13 +00003383 // Keep writing types and declarations until all types and
3384 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003385 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003386 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003387 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3388 E = DeclsToRewrite.end();
3389 I != E; ++I)
3390 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003391 while (!DeclTypesToEmit.empty()) {
3392 DeclOrType DOT = DeclTypesToEmit.front();
3393 DeclTypesToEmit.pop();
3394 if (DOT.isType())
3395 WriteType(DOT.getType());
3396 else
3397 WriteDecl(Context, DOT.getDecl());
3398 }
3399 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003400
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003401 WriteFileDeclIDsMap();
3402 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3403
3404 if (Chain) {
3405 // Write the mapping information describing our module dependencies and how
3406 // each of those modules were mapped into our own offset/ID space, so that
3407 // the reader can build the appropriate mapping to its own offset/ID space.
3408 // The map consists solely of a blob with the following format:
3409 // *(module-name-len:i16 module-name:len*i8
3410 // source-location-offset:i32
3411 // identifier-id:i32
3412 // preprocessed-entity-id:i32
3413 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003414 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003415 // selector-id:i32
3416 // declaration-id:i32
3417 // c++-base-specifiers-id:i32
3418 // type-id:i32)
3419 //
3420 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3421 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3423 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003424 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003425 {
3426 llvm::raw_svector_ostream Out(Buffer);
3427 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003428 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003429 M != MEnd; ++M) {
3430 StringRef FileName = (*M)->FileName;
3431 io::Emit16(Out, FileName.size());
3432 Out.write(FileName.data(), FileName.size());
3433 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3434 io::Emit32(Out, (*M)->BaseIdentifierID);
3435 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003436 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003437 io::Emit32(Out, (*M)->BaseSelectorID);
3438 io::Emit32(Out, (*M)->BaseDeclID);
3439 io::Emit32(Out, (*M)->BaseTypeIndex);
3440 }
3441 }
3442 Record.clear();
3443 Record.push_back(MODULE_OFFSET_MAP);
3444 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3445 Buffer.data(), Buffer.size());
3446 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003447 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003448 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003449 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003450 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003451 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003452 WriteFPPragmaOptions(SemaRef.getFPOptions());
3453 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003454
Sebastian Redl1476ed42010-07-16 16:36:56 +00003455 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003456 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003457
Anders Carlssonc8505782011-03-06 18:41:18 +00003458 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003459
Douglas Gregore209e502011-12-06 01:10:29 +00003460 // If we're emitting a module, write out the submodule information.
3461 if (WritingModule)
3462 WriteSubmodules(WritingModule);
3463
Douglas Gregora119da02011-08-02 16:26:37 +00003464 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3465
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003466 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003467 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003468 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003469
3470 // Write the record containing tentative definitions.
3471 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003472 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003473
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003474 // Write the record containing unused file scoped decls.
3475 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003476 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003477
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003478 // Write the record containing weak undeclared identifiers.
3479 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003480 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003481 WeakUndeclaredIdentifiers);
3482
Douglas Gregor14c22f22009-04-22 22:18:58 +00003483 // Write the record containing locally-scoped external definitions.
3484 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003485 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003486 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003487
3488 // Write the record containing ext_vector type names.
3489 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003490 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003491
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003492 // Write the record containing VTable uses information.
3493 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003494 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003495
3496 // Write the record containing dynamic classes declarations.
3497 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003498 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003499
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003500 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003501 if (!PendingInstantiations.empty())
3502 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003503
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003504 // Write the record containing declaration references of Sema.
3505 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003506 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003507
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003508 // Write the record containing CUDA-specific declaration references.
3509 if (!CUDASpecialDeclRefs.empty())
3510 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003511
3512 // Write the delegating constructors.
3513 if (!DelegatingCtorDecls.empty())
3514 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003515
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003516 // Write the known namespaces.
3517 if (!KnownNamespaces.empty())
3518 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3519
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003520 // Write the visible updates to DeclContexts.
3521 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3522 I = UpdatedDeclContexts.begin(),
3523 E = UpdatedDeclContexts.end();
3524 I != E; ++I)
3525 WriteDeclContextVisibleUpdate(*I);
3526
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003527 if (!WritingModule) {
3528 // Write the submodules that were imported, if any.
3529 RecordData ImportedModules;
3530 for (ASTContext::import_iterator I = Context.local_import_begin(),
3531 IEnd = Context.local_import_end();
3532 I != IEnd; ++I) {
3533 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3534 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3535 }
3536 if (!ImportedModules.empty()) {
3537 // Sort module IDs.
3538 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3539
3540 // Unique module IDs.
3541 ImportedModules.erase(std::unique(ImportedModules.begin(),
3542 ImportedModules.end()),
3543 ImportedModules.end());
3544
3545 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3546 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003547 }
3548
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003549 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003550 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003551 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003552 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003553 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003554
Douglas Gregor3e1af842009-04-17 22:13:46 +00003555 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003556 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003557 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003558 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003559 Record.push_back(NumLexicalDeclContexts);
3560 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003561 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003562 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003563}
3564
Douglas Gregor61c5e342011-09-17 00:05:03 +00003565/// \brief Go through the declaration update blocks and resolve declaration
3566/// pointers into declaration IDs.
3567void ASTWriter::ResolveDeclUpdatesBlocks() {
3568 for (DeclUpdateMap::iterator
3569 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3570 const Decl *D = I->first;
3571 UpdateRecord &URec = I->second;
3572
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003573 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003574 continue; // The decl will be written completely
3575
3576 unsigned Idx = 0, N = URec.size();
3577 while (Idx < N) {
3578 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003579 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3580 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3581 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3582 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3583 ++Idx;
3584 break;
3585
3586 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3587 ++Idx;
3588 break;
3589 }
3590 }
3591 }
3592}
3593
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003594void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003595 if (DeclUpdates.empty())
3596 return;
3597
3598 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003599 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003600 for (DeclUpdateMap::iterator
3601 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3602 const Decl *D = I->first;
3603 UpdateRecord &URec = I->second;
3604
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003605 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003606 continue; // The decl will be written completely,no need to store updates.
3607
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003608 uint64_t Offset = Stream.GetCurrentBitNo();
3609 Stream.EmitRecord(DECL_UPDATES, URec);
3610
3611 OffsetsRecord.push_back(GetDeclRef(D));
3612 OffsetsRecord.push_back(Offset);
3613 }
3614 Stream.ExitBlock();
3615 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3616}
3617
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003618void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003619 if (ReplacedDecls.empty())
3620 return;
3621
3622 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003623 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003624 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003625 Record.push_back(I->ID);
3626 Record.push_back(I->Offset);
3627 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003628 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003629 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003630}
3631
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003632void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003633 Record.push_back(Loc.getRawEncoding());
3634}
3635
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003636void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003637 AddSourceLocation(Range.getBegin(), Record);
3638 AddSourceLocation(Range.getEnd(), Record);
3639}
3640
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003641void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003642 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003643 const uint64_t *Words = Value.getRawData();
3644 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003645}
3646
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003647void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003648 Record.push_back(Value.isUnsigned());
3649 AddAPInt(Value, Record);
3650}
3651
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003652void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003653 AddAPInt(Value.bitcastToAPInt(), Record);
3654}
3655
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003656void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003657 Record.push_back(getIdentifierRef(II));
3658}
3659
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003660IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003661 if (II == 0)
3662 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003663
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003664 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003665 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003666 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003667 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003668}
3669
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003670void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003671 Record.push_back(getSelectorRef(SelRef));
3672}
3673
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003674SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003675 if (Sel.getAsOpaquePtr() == 0) {
3676 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003677 }
3678
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003679 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003680 if (SID == 0 && Chain) {
3681 // This might trigger a ReadSelector callback, which will set the ID for
3682 // this selector.
3683 Chain->LoadSelector(Sel);
3684 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003685 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003686 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003687 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003688 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003689}
3690
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003691void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003692 AddDeclRef(Temp->getDestructor(), Record);
3693}
3694
Douglas Gregor7c789c12010-10-29 22:39:52 +00003695void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3696 CXXBaseSpecifier const *BasesEnd,
3697 RecordDataImpl &Record) {
3698 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3699 CXXBaseSpecifiersToWrite.push_back(
3700 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3701 Bases, BasesEnd));
3702 Record.push_back(NextCXXBaseSpecifiersID++);
3703}
3704
Sebastian Redla4232eb2010-08-18 23:56:21 +00003705void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003706 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003707 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003708 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003709 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003710 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003711 break;
3712 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003713 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003714 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003715 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003716 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003717 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003718 break;
3719 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003720 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003721 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003722 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003723 break;
John McCall833ca992009-10-29 08:12:44 +00003724 case TemplateArgument::Null:
3725 case TemplateArgument::Integral:
3726 case TemplateArgument::Declaration:
3727 case TemplateArgument::Pack:
3728 break;
3729 }
3730}
3731
Sebastian Redla4232eb2010-08-18 23:56:21 +00003732void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003733 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003734 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003735
3736 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3737 bool InfoHasSameExpr
3738 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3739 Record.push_back(InfoHasSameExpr);
3740 if (InfoHasSameExpr)
3741 return; // Avoid storing the same expr twice.
3742 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003743 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3744 Record);
3745}
3746
Douglas Gregordc355712011-02-25 00:36:19 +00003747void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3748 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003749 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003750 AddTypeRef(QualType(), Record);
3751 return;
3752 }
3753
Douglas Gregordc355712011-02-25 00:36:19 +00003754 AddTypeLoc(TInfo->getTypeLoc(), Record);
3755}
3756
3757void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3758 AddTypeRef(TL.getType(), Record);
3759
John McCalla1ee0c52009-10-16 21:56:05 +00003760 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003761 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003762 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003763}
3764
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003765void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003766 Record.push_back(GetOrCreateTypeID(T));
3767}
3768
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003769TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3770 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003771 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3772}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003773
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003774TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003775 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003776 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003777}
3778
3779TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3780 if (T.isNull())
3781 return TypeIdx();
3782 assert(!T.getLocalFastQualifiers());
3783
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003784 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003785 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003786 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003787 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003788 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003789 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003790 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003791 return Idx;
3792}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003793
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003794TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003795 if (T.isNull())
3796 return TypeIdx();
3797 assert(!T.getLocalFastQualifiers());
3798
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003799 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3800 assert(I != TypeIdxs.end() && "Type not emitted!");
3801 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003802}
3803
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003804void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003805 Record.push_back(GetDeclRef(D));
3806}
3807
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003808DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003809 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3810
Douglas Gregor2cf26342009-04-09 22:27:44 +00003811 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003812 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003813 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003814
3815 // If D comes from an AST file, its declaration ID is already known and
3816 // fixed.
3817 if (D->isFromASTFile())
3818 return D->getGlobalID();
3819
Douglas Gregor97475832010-10-05 18:37:06 +00003820 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003821 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003822 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003823 // We haven't seen this declaration before. Give it a new ID and
3824 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003825 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003826 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003827 }
3828
Sebastian Redl681d7232010-07-27 00:17:23 +00003829 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003830}
3831
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003832DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003833 if (D == 0)
3834 return 0;
3835
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003836 // If D comes from an AST file, its declaration ID is already known and
3837 // fixed.
3838 if (D->isFromASTFile())
3839 return D->getGlobalID();
3840
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003841 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3842 return DeclIDs[D];
3843}
3844
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003845static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3846 std::pair<unsigned, serialization::DeclID> R) {
3847 return L.first < R.first;
3848}
3849
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003850void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003851 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003852 assert(D);
3853
3854 SourceLocation Loc = D->getLocation();
3855 if (Loc.isInvalid())
3856 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003857
3858 // We only keep track of the file-level declarations of each file.
3859 if (!D->getLexicalDeclContext()->isFileContext())
3860 return;
3861
3862 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003863 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003864 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003865 FileID FID;
3866 unsigned Offset;
3867 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003868 if (FID.isInvalid())
3869 return;
3870 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3871 assert(Entry->isFile());
3872
3873 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3874 if (!Info)
3875 Info = new DeclIDInFileInfo();
3876
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003877 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003878 LocDeclIDsTy &Decls = Info->DeclIDs;
3879
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003880 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003881 Decls.push_back(LocDecl);
3882 return;
3883 }
3884
3885 LocDeclIDsTy::iterator
3886 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3887
3888 Decls.insert(I, LocDecl);
3889}
3890
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003891void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003892 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003893 Record.push_back(Name.getNameKind());
3894 switch (Name.getNameKind()) {
3895 case DeclarationName::Identifier:
3896 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3897 break;
3898
3899 case DeclarationName::ObjCZeroArgSelector:
3900 case DeclarationName::ObjCOneArgSelector:
3901 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003902 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003903 break;
3904
3905 case DeclarationName::CXXConstructorName:
3906 case DeclarationName::CXXDestructorName:
3907 case DeclarationName::CXXConversionFunctionName:
3908 AddTypeRef(Name.getCXXNameType(), Record);
3909 break;
3910
3911 case DeclarationName::CXXOperatorName:
3912 Record.push_back(Name.getCXXOverloadedOperator());
3913 break;
3914
Sean Hunt3e518bd2009-11-29 07:34:05 +00003915 case DeclarationName::CXXLiteralOperatorName:
3916 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3917 break;
3918
Douglas Gregor2cf26342009-04-09 22:27:44 +00003919 case DeclarationName::CXXUsingDirective:
3920 // No extra data to emit
3921 break;
3922 }
3923}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003924
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003925void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003926 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003927 switch (Name.getNameKind()) {
3928 case DeclarationName::CXXConstructorName:
3929 case DeclarationName::CXXDestructorName:
3930 case DeclarationName::CXXConversionFunctionName:
3931 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3932 break;
3933
3934 case DeclarationName::CXXOperatorName:
3935 AddSourceLocation(
3936 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3937 Record);
3938 AddSourceLocation(
3939 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3940 Record);
3941 break;
3942
3943 case DeclarationName::CXXLiteralOperatorName:
3944 AddSourceLocation(
3945 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3946 Record);
3947 break;
3948
3949 case DeclarationName::Identifier:
3950 case DeclarationName::ObjCZeroArgSelector:
3951 case DeclarationName::ObjCOneArgSelector:
3952 case DeclarationName::ObjCMultiArgSelector:
3953 case DeclarationName::CXXUsingDirective:
3954 break;
3955 }
3956}
3957
3958void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003959 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003960 AddDeclarationName(NameInfo.getName(), Record);
3961 AddSourceLocation(NameInfo.getLoc(), Record);
3962 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3963}
3964
3965void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003966 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003967 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003968 Record.push_back(Info.NumTemplParamLists);
3969 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3970 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3971}
3972
Sebastian Redla4232eb2010-08-18 23:56:21 +00003973void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003974 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003975 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003976 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003977 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003978
3979 // Push each of the NNS's onto a stack for serialization in reverse order.
3980 while (NNS) {
3981 NestedNames.push_back(NNS);
3982 NNS = NNS->getPrefix();
3983 }
3984
3985 Record.push_back(NestedNames.size());
3986 while(!NestedNames.empty()) {
3987 NNS = NestedNames.pop_back_val();
3988 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3989 Record.push_back(Kind);
3990 switch (Kind) {
3991 case NestedNameSpecifier::Identifier:
3992 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3993 break;
3994
3995 case NestedNameSpecifier::Namespace:
3996 AddDeclRef(NNS->getAsNamespace(), Record);
3997 break;
3998
Douglas Gregor14aba762011-02-24 02:36:08 +00003999 case NestedNameSpecifier::NamespaceAlias:
4000 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4001 break;
4002
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004003 case NestedNameSpecifier::TypeSpec:
4004 case NestedNameSpecifier::TypeSpecWithTemplate:
4005 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4006 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4007 break;
4008
4009 case NestedNameSpecifier::Global:
4010 // Don't need to write an associated value.
4011 break;
4012 }
4013 }
4014}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004015
Douglas Gregordc355712011-02-25 00:36:19 +00004016void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4017 RecordDataImpl &Record) {
4018 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004019 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004020 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004021
4022 // Push each of the nested-name-specifiers's onto a stack for
4023 // serialization in reverse order.
4024 while (NNS) {
4025 NestedNames.push_back(NNS);
4026 NNS = NNS.getPrefix();
4027 }
4028
4029 Record.push_back(NestedNames.size());
4030 while(!NestedNames.empty()) {
4031 NNS = NestedNames.pop_back_val();
4032 NestedNameSpecifier::SpecifierKind Kind
4033 = NNS.getNestedNameSpecifier()->getKind();
4034 Record.push_back(Kind);
4035 switch (Kind) {
4036 case NestedNameSpecifier::Identifier:
4037 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4038 AddSourceRange(NNS.getLocalSourceRange(), Record);
4039 break;
4040
4041 case NestedNameSpecifier::Namespace:
4042 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4043 AddSourceRange(NNS.getLocalSourceRange(), Record);
4044 break;
4045
4046 case NestedNameSpecifier::NamespaceAlias:
4047 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4048 AddSourceRange(NNS.getLocalSourceRange(), Record);
4049 break;
4050
4051 case NestedNameSpecifier::TypeSpec:
4052 case NestedNameSpecifier::TypeSpecWithTemplate:
4053 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4054 AddTypeLoc(NNS.getTypeLoc(), Record);
4055 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4056 break;
4057
4058 case NestedNameSpecifier::Global:
4059 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4060 break;
4061 }
4062 }
4063}
4064
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004065void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004066 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004067 Record.push_back(Kind);
4068 switch (Kind) {
4069 case TemplateName::Template:
4070 AddDeclRef(Name.getAsTemplateDecl(), Record);
4071 break;
4072
4073 case TemplateName::OverloadedTemplate: {
4074 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4075 Record.push_back(OvT->size());
4076 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4077 I != E; ++I)
4078 AddDeclRef(*I, Record);
4079 break;
4080 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004081
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004082 case TemplateName::QualifiedTemplate: {
4083 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4084 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4085 Record.push_back(QualT->hasTemplateKeyword());
4086 AddDeclRef(QualT->getTemplateDecl(), Record);
4087 break;
4088 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004089
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004090 case TemplateName::DependentTemplate: {
4091 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4092 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4093 Record.push_back(DepT->isIdentifier());
4094 if (DepT->isIdentifier())
4095 AddIdentifierRef(DepT->getIdentifier(), Record);
4096 else
4097 Record.push_back(DepT->getOperator());
4098 break;
4099 }
John McCall14606042011-06-30 08:33:18 +00004100
4101 case TemplateName::SubstTemplateTemplateParm: {
4102 SubstTemplateTemplateParmStorage *subst
4103 = Name.getAsSubstTemplateTemplateParm();
4104 AddDeclRef(subst->getParameter(), Record);
4105 AddTemplateName(subst->getReplacement(), Record);
4106 break;
4107 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004108
4109 case TemplateName::SubstTemplateTemplateParmPack: {
4110 SubstTemplateTemplateParmPackStorage *SubstPack
4111 = Name.getAsSubstTemplateTemplateParmPack();
4112 AddDeclRef(SubstPack->getParameterPack(), Record);
4113 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4114 break;
4115 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004116 }
4117}
4118
Michael J. Spencer20249a12010-10-21 03:16:25 +00004119void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004120 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004121 Record.push_back(Arg.getKind());
4122 switch (Arg.getKind()) {
4123 case TemplateArgument::Null:
4124 break;
4125 case TemplateArgument::Type:
4126 AddTypeRef(Arg.getAsType(), Record);
4127 break;
4128 case TemplateArgument::Declaration:
4129 AddDeclRef(Arg.getAsDecl(), Record);
4130 break;
4131 case TemplateArgument::Integral:
4132 AddAPSInt(*Arg.getAsIntegral(), Record);
4133 AddTypeRef(Arg.getIntegralType(), Record);
4134 break;
4135 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004136 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4137 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004138 case TemplateArgument::TemplateExpansion:
4139 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004140 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4141 Record.push_back(*NumExpansions + 1);
4142 else
4143 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004144 break;
4145 case TemplateArgument::Expression:
4146 AddStmt(Arg.getAsExpr());
4147 break;
4148 case TemplateArgument::Pack:
4149 Record.push_back(Arg.pack_size());
4150 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4151 I != E; ++I)
4152 AddTemplateArgument(*I, Record);
4153 break;
4154 }
4155}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004156
4157void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004158ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004159 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004160 assert(TemplateParams && "No TemplateParams!");
4161 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4162 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4163 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4164 Record.push_back(TemplateParams->size());
4165 for (TemplateParameterList::const_iterator
4166 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4167 P != PEnd; ++P)
4168 AddDeclRef(*P, Record);
4169}
4170
4171/// \brief Emit a template argument list.
4172void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004173ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004174 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004175 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004176 Record.push_back(TemplateArgs->size());
4177 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004178 AddTemplateArgument(TemplateArgs->get(i), Record);
4179}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004180
4181
4182void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004183ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004184 Record.push_back(Set.size());
4185 for (UnresolvedSetImpl::const_iterator
4186 I = Set.begin(), E = Set.end(); I != E; ++I) {
4187 AddDeclRef(I.getDecl(), Record);
4188 Record.push_back(I.getAccess());
4189 }
4190}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004191
Sebastian Redla4232eb2010-08-18 23:56:21 +00004192void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004193 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004194 Record.push_back(Base.isVirtual());
4195 Record.push_back(Base.isBaseOfClass());
4196 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004197 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004198 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004199 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004200 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4201 : SourceLocation(),
4202 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004203}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004204
Douglas Gregor7c789c12010-10-29 22:39:52 +00004205void ASTWriter::FlushCXXBaseSpecifiers() {
4206 RecordData Record;
4207 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4208 Record.clear();
4209
4210 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004211 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004212 if (Index == CXXBaseSpecifiersOffsets.size())
4213 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4214 else {
4215 if (Index > CXXBaseSpecifiersOffsets.size())
4216 CXXBaseSpecifiersOffsets.resize(Index + 1);
4217 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4218 }
4219
4220 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4221 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4222 Record.push_back(BEnd - B);
4223 for (; B != BEnd; ++B)
4224 AddCXXBaseSpecifier(*B, Record);
4225 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004226
4227 // Flush any expressions that were written as part of the base specifiers.
4228 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004229 }
4230
4231 CXXBaseSpecifiersToWrite.clear();
4232}
4233
Sean Huntcbb67482011-01-08 20:30:50 +00004234void ASTWriter::AddCXXCtorInitializers(
4235 const CXXCtorInitializer * const *CtorInitializers,
4236 unsigned NumCtorInitializers,
4237 RecordDataImpl &Record) {
4238 Record.push_back(NumCtorInitializers);
4239 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4240 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004241
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004242 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004243 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004244 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004245 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004246 } else if (Init->isDelegatingInitializer()) {
4247 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004248 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004249 } else if (Init->isMemberInitializer()){
4250 Record.push_back(CTOR_INITIALIZER_MEMBER);
4251 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004252 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004253 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4254 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004255 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004256
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004257 AddSourceLocation(Init->getMemberLocation(), Record);
4258 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004259 AddSourceLocation(Init->getLParenLoc(), Record);
4260 AddSourceLocation(Init->getRParenLoc(), Record);
4261 Record.push_back(Init->isWritten());
4262 if (Init->isWritten()) {
4263 Record.push_back(Init->getSourceOrder());
4264 } else {
4265 Record.push_back(Init->getNumArrayIndices());
4266 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4267 AddDeclRef(Init->getArrayIndex(i), Record);
4268 }
4269 }
4270}
4271
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004272void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4273 assert(D->DefinitionData);
4274 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
4275 Record.push_back(Data.UserDeclaredConstructor);
4276 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004277 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004278 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004279 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004280 Record.push_back(Data.UserDeclaredDestructor);
4281 Record.push_back(Data.Aggregate);
4282 Record.push_back(Data.PlainOldData);
4283 Record.push_back(Data.Empty);
4284 Record.push_back(Data.Polymorphic);
4285 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004286 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004287 Record.push_back(Data.HasNoNonEmptyBases);
4288 Record.push_back(Data.HasPrivateFields);
4289 Record.push_back(Data.HasProtectedFields);
4290 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004291 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00004292 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004293 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004294 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004295 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004296 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004297 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004298 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004299 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004300 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004301 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004302 Record.push_back(Data.DeclaredDefaultConstructor);
4303 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004304 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004305 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004306 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004307 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004308 Record.push_back(Data.FailedImplicitMoveConstructor);
4309 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004310
4311 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004312 if (Data.NumBases > 0)
4313 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4314 Record);
4315
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004316 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4317 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004318 if (Data.NumVBases > 0)
4319 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4320 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004321
4322 AddUnresolvedSet(Data.Conversions, Record);
4323 AddUnresolvedSet(Data.VisibleConversions, Record);
4324 // Data.Definition is the owning decl, no need to write it.
4325 AddDeclRef(Data.FirstFriend, Record);
4326}
4327
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004328void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004329 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004330 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004331 assert(FirstDeclID == NextDeclID &&
4332 FirstTypeID == NextTypeID &&
4333 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004334 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004335 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004336 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004337
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004338 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004339
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004340 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4341 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4342 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004343 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004344 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004345 NextDeclID = FirstDeclID;
4346 NextTypeID = FirstTypeID;
4347 NextIdentID = FirstIdentID;
4348 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004349 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004350}
4351
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004352void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004353 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004354 if (II->hasMacroDefinition())
4355 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004356}
4357
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004358void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004359 // Always take the highest-numbered type index. This copes with an interesting
4360 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004361 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004362 // keep the higher-numbered entry so that we can properly write it out to
4363 // the AST file.
4364 TypeIdx &StoredIdx = TypeIdxs[T];
4365 if (Idx.getIndex() >= StoredIdx.getIndex())
4366 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004367}
4368
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004369void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004370 SelectorIDs[S] = ID;
4371}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004372
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004373void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004374 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004375 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004376 MacroDefinitions[MD] = ID;
4377}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004378
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004379void ASTWriter::MacroVisible(IdentifierInfo *II) {
4380 DeserializedMacroNames.push_back(II);
4381}
4382
Douglas Gregora015cab2011-12-02 17:30:13 +00004383void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4384 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4385 SubmoduleIDs[Mod] = ID;
4386}
4387
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004388void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004389 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004390 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004391 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4392 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004393 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004394 // A forward reference was mutated into a definition. Rewrite it.
4395 // FIXME: This happens during template instantiation, should we
4396 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004397 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004398 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004399 }
4400}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004401void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004402 assert(!WritingAST && "Already writing the AST!");
4403
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004404 // TU and namespaces are handled elsewhere.
4405 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4406 return;
4407
Douglas Gregor919814d2011-09-09 23:01:35 +00004408 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004409 return; // Not a source decl added to a DeclContext from PCH.
4410
4411 AddUpdatedDeclContext(DC);
4412}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004413
4414void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004415 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004416 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004417 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004418 return; // Not a source member added to a class from PCH.
4419 if (!isa<CXXMethodDecl>(D))
4420 return; // We are interested in lazily declared implicit methods.
4421
4422 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004423 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004424 UpdateRecord &Record = DeclUpdates[RD];
4425 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004426 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004427}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004428
4429void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4430 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004431 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004432 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004433 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004434 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004435 return; // Not a source specialization added to a template from PCH.
4436
4437 UpdateRecord &Record = DeclUpdates[TD];
4438 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004439 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004440}
Douglas Gregor89d99802010-11-30 06:16:57 +00004441
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004442void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4443 const FunctionDecl *D) {
4444 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004445 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004446 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004447 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004448 return; // Not a source specialization added to a template from PCH.
4449
4450 UpdateRecord &Record = DeclUpdates[TD];
4451 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004452 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004453}
4454
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004455void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004456 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004457 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004458 return; // Declaration not imported from PCH.
4459
4460 // Implicit decl from a PCH was defined.
4461 // FIXME: Should implicit definition be a separate FunctionDecl?
4462 RewriteDecl(D);
4463}
4464
Sebastian Redlf79a7192011-04-29 08:19:30 +00004465void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004466 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004467 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004468 return;
4469
4470 // Since the actual instantiation is delayed, this really means that we need
4471 // to update the instantiation location.
4472 UpdateRecord &Record = DeclUpdates[D];
4473 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4474 AddSourceLocation(
4475 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4476}
4477
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004478void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4479 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004480 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004481 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004482 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004483
4484 assert(IFD->getDefinition() && "Category on a class without a definition?");
4485 ObjCClassesWithCategories.insert(
4486 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004487}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004488
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004489
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004490void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4491 const ObjCPropertyDecl *OrigProp,
4492 const ObjCCategoryDecl *ClassExt) {
4493 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4494 if (!D)
4495 return;
4496
4497 assert(!WritingAST && "Already writing the AST!");
4498 if (!D->isFromASTFile())
4499 return; // Declaration not imported from PCH.
4500
4501 RewriteDecl(D);
4502}
4503