blob: c5f9b2dc88a719ac1f79c9fe62285ec6a37c7b6b [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) {
John McCall833ca992009-10-29 08:12:44 +0000555 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000559 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
560 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000561}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000562void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
564 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
565}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000566void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000567 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000568 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000569}
John McCall3cb0ebd2010-03-10 03:28:59 +0000570void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
Douglas Gregor4714c122010-03-31 17:34:00 +0000573void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000574 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000575 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000576 Writer.AddSourceLocation(TL.getNameLoc(), Record);
577}
John McCall33500952010-06-11 00:33:02 +0000578void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
579 DependentTemplateSpecializationTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
584 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
585 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000586 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
587 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000588}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000589void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
590 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
591}
John McCall51bd8032009-10-18 01:05:36 +0000592void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
593 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000594}
595void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
596 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
600 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000601}
John McCall54e14c42009-10-22 22:37:11 +0000602void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000604}
Eli Friedmanb001de72011-10-06 23:00:33 +0000605void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getKWLoc(), Record);
607 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
608 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
609}
John McCalla1ee0c52009-10-16 21:56:05 +0000610
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000611//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000612// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000613//===----------------------------------------------------------------------===//
614
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000615static void EmitBlockID(unsigned ID, const char *Name,
616 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000617 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000618 Record.clear();
619 Record.push_back(ID);
620 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
621
622 // Emit the block name if present.
623 if (Name == 0 || Name[0] == 0) return;
624 Record.clear();
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
628}
629
630static void EmitRecordID(unsigned ID, const char *Name,
631 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000632 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 Record.clear();
634 Record.push_back(ID);
635 while (*Name)
636 Record.push_back(*Name++);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000638}
639
640static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000641 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000642#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000643 RECORD(STMT_STOP);
644 RECORD(STMT_NULL_PTR);
645 RECORD(STMT_NULL);
646 RECORD(STMT_COMPOUND);
647 RECORD(STMT_CASE);
648 RECORD(STMT_DEFAULT);
649 RECORD(STMT_LABEL);
650 RECORD(STMT_IF);
651 RECORD(STMT_SWITCH);
652 RECORD(STMT_WHILE);
653 RECORD(STMT_DO);
654 RECORD(STMT_FOR);
655 RECORD(STMT_GOTO);
656 RECORD(STMT_INDIRECT_GOTO);
657 RECORD(STMT_CONTINUE);
658 RECORD(STMT_BREAK);
659 RECORD(STMT_RETURN);
660 RECORD(STMT_DECL);
661 RECORD(STMT_ASM);
662 RECORD(EXPR_PREDEFINED);
663 RECORD(EXPR_DECL_REF);
664 RECORD(EXPR_INTEGER_LITERAL);
665 RECORD(EXPR_FLOATING_LITERAL);
666 RECORD(EXPR_IMAGINARY_LITERAL);
667 RECORD(EXPR_STRING_LITERAL);
668 RECORD(EXPR_CHARACTER_LITERAL);
669 RECORD(EXPR_PAREN);
670 RECORD(EXPR_UNARY_OPERATOR);
671 RECORD(EXPR_SIZEOF_ALIGN_OF);
672 RECORD(EXPR_ARRAY_SUBSCRIPT);
673 RECORD(EXPR_CALL);
674 RECORD(EXPR_MEMBER);
675 RECORD(EXPR_BINARY_OPERATOR);
676 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
677 RECORD(EXPR_CONDITIONAL_OPERATOR);
678 RECORD(EXPR_IMPLICIT_CAST);
679 RECORD(EXPR_CSTYLE_CAST);
680 RECORD(EXPR_COMPOUND_LITERAL);
681 RECORD(EXPR_EXT_VECTOR_ELEMENT);
682 RECORD(EXPR_INIT_LIST);
683 RECORD(EXPR_DESIGNATED_INIT);
684 RECORD(EXPR_IMPLICIT_VALUE_INIT);
685 RECORD(EXPR_VA_ARG);
686 RECORD(EXPR_ADDR_LABEL);
687 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000688 RECORD(EXPR_CHOOSE);
689 RECORD(EXPR_GNU_NULL);
690 RECORD(EXPR_SHUFFLE_VECTOR);
691 RECORD(EXPR_BLOCK);
692 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000693 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000694 RECORD(EXPR_OBJC_STRING_LITERAL);
695 RECORD(EXPR_OBJC_ENCODE);
696 RECORD(EXPR_OBJC_SELECTOR_EXPR);
697 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
698 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
699 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
700 RECORD(EXPR_OBJC_KVC_REF_EXPR);
701 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000702 RECORD(STMT_OBJC_FOR_COLLECTION);
703 RECORD(STMT_OBJC_CATCH);
704 RECORD(STMT_OBJC_FINALLY);
705 RECORD(STMT_OBJC_AT_TRY);
706 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
707 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000708 RECORD(EXPR_CXX_OPERATOR_CALL);
709 RECORD(EXPR_CXX_CONSTRUCT);
710 RECORD(EXPR_CXX_STATIC_CAST);
711 RECORD(EXPR_CXX_DYNAMIC_CAST);
712 RECORD(EXPR_CXX_REINTERPRET_CAST);
713 RECORD(EXPR_CXX_CONST_CAST);
714 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
715 RECORD(EXPR_CXX_BOOL_LITERAL);
716 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000717 RECORD(EXPR_CXX_TYPEID_EXPR);
718 RECORD(EXPR_CXX_TYPEID_TYPE);
719 RECORD(EXPR_CXX_UUIDOF_EXPR);
720 RECORD(EXPR_CXX_UUIDOF_TYPE);
721 RECORD(EXPR_CXX_THIS);
722 RECORD(EXPR_CXX_THROW);
723 RECORD(EXPR_CXX_DEFAULT_ARG);
724 RECORD(EXPR_CXX_BIND_TEMPORARY);
725 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
726 RECORD(EXPR_CXX_NEW);
727 RECORD(EXPR_CXX_DELETE);
728 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
729 RECORD(EXPR_EXPR_WITH_CLEANUPS);
730 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
731 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
732 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
733 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
734 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
735 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
736 RECORD(EXPR_CXX_NOEXCEPT);
737 RECORD(EXPR_OPAQUE_VALUE);
738 RECORD(EXPR_BINARY_TYPE_TRAIT);
739 RECORD(EXPR_PACK_EXPANSION);
740 RECORD(EXPR_SIZEOF_PACK);
741 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000742 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000743#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000744}
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Sebastian Redla4232eb2010-08-18 23:56:21 +0000746void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000747 RecordData Record;
748 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000750#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
751#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Sebastian Redl3397c552010-08-18 23:56:27 +0000753 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000754 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000755 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000756 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000757 RECORD(TYPE_OFFSET);
758 RECORD(DECL_OFFSET);
759 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000760 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761 RECORD(IDENTIFIER_OFFSET);
762 RECORD(IDENTIFIER_TABLE);
763 RECORD(EXTERNAL_DEFINITIONS);
764 RECORD(SPECIAL_TYPES);
765 RECORD(STATISTICS);
766 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000767 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000768 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
769 RECORD(SELECTOR_OFFSETS);
770 RECORD(METHOD_POOL);
771 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000772 RECORD(SOURCE_LOCATION_OFFSETS);
773 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000774 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000775 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000776 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000777 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000778 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000779 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000780 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000781 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000782 RECORD(SEMA_DECL_REFS);
783 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
784 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
785 RECORD(DECL_REPLACEMENTS);
786 RECORD(UPDATE_VISIBLE);
787 RECORD(DECL_UPDATE_OFFSETS);
788 RECORD(DECL_UPDATES);
789 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
790 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000791 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000792 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000793 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000794 RECORD(FP_PRAGMA_OPTIONS);
795 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000796 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000797 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
798 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000799 RECORD(MODULE_OFFSET_MAP);
800 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregora1266512011-12-19 21:09:25 +0000801 RECORD(OBJC_CHAINED_CATEGORIES);
802 RECORD(FILE_SORTED_DECLS);
803 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000804 RECORD(MERGED_DECLARATIONS);
805 RECORD(LOCAL_REDECLARATIONS);
806
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000807 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000808 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000809 RECORD(SM_SLOC_FILE_ENTRY);
810 RECORD(SM_SLOC_BUFFER_ENTRY);
811 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000812 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000814 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000815 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000816 RECORD(PP_MACRO_OBJECT_LIKE);
817 RECORD(PP_MACRO_FUNCTION_LIKE);
818 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000819
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000820 // Decls and Types block.
821 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000822 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000823 RECORD(TYPE_COMPLEX);
824 RECORD(TYPE_POINTER);
825 RECORD(TYPE_BLOCK_POINTER);
826 RECORD(TYPE_LVALUE_REFERENCE);
827 RECORD(TYPE_RVALUE_REFERENCE);
828 RECORD(TYPE_MEMBER_POINTER);
829 RECORD(TYPE_CONSTANT_ARRAY);
830 RECORD(TYPE_INCOMPLETE_ARRAY);
831 RECORD(TYPE_VARIABLE_ARRAY);
832 RECORD(TYPE_VECTOR);
833 RECORD(TYPE_EXT_VECTOR);
834 RECORD(TYPE_FUNCTION_PROTO);
835 RECORD(TYPE_FUNCTION_NO_PROTO);
836 RECORD(TYPE_TYPEDEF);
837 RECORD(TYPE_TYPEOF_EXPR);
838 RECORD(TYPE_TYPEOF);
839 RECORD(TYPE_RECORD);
840 RECORD(TYPE_ENUM);
841 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000842 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000843 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000844 RECORD(TYPE_DECLTYPE);
845 RECORD(TYPE_ELABORATED);
846 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
847 RECORD(TYPE_UNRESOLVED_USING);
848 RECORD(TYPE_INJECTED_CLASS_NAME);
849 RECORD(TYPE_OBJC_OBJECT);
850 RECORD(TYPE_TEMPLATE_TYPE_PARM);
851 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
852 RECORD(TYPE_DEPENDENT_NAME);
853 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
854 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
855 RECORD(TYPE_PAREN);
856 RECORD(TYPE_PACK_EXPANSION);
857 RECORD(TYPE_ATTRIBUTED);
858 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000859 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000860 RECORD(DECL_TYPEDEF);
861 RECORD(DECL_ENUM);
862 RECORD(DECL_RECORD);
863 RECORD(DECL_ENUM_CONSTANT);
864 RECORD(DECL_FUNCTION);
865 RECORD(DECL_OBJC_METHOD);
866 RECORD(DECL_OBJC_INTERFACE);
867 RECORD(DECL_OBJC_PROTOCOL);
868 RECORD(DECL_OBJC_IVAR);
869 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000870 RECORD(DECL_OBJC_CATEGORY);
871 RECORD(DECL_OBJC_CATEGORY_IMPL);
872 RECORD(DECL_OBJC_IMPLEMENTATION);
873 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
874 RECORD(DECL_OBJC_PROPERTY);
875 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000876 RECORD(DECL_FIELD);
877 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000878 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000879 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000880 RECORD(DECL_FILE_SCOPE_ASM);
881 RECORD(DECL_BLOCK);
882 RECORD(DECL_CONTEXT_LEXICAL);
883 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000884 RECORD(DECL_NAMESPACE);
885 RECORD(DECL_NAMESPACE_ALIAS);
886 RECORD(DECL_USING);
887 RECORD(DECL_USING_SHADOW);
888 RECORD(DECL_USING_DIRECTIVE);
889 RECORD(DECL_UNRESOLVED_USING_VALUE);
890 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
891 RECORD(DECL_LINKAGE_SPEC);
892 RECORD(DECL_CXX_RECORD);
893 RECORD(DECL_CXX_METHOD);
894 RECORD(DECL_CXX_CONSTRUCTOR);
895 RECORD(DECL_CXX_DESTRUCTOR);
896 RECORD(DECL_CXX_CONVERSION);
897 RECORD(DECL_ACCESS_SPEC);
898 RECORD(DECL_FRIEND);
899 RECORD(DECL_FRIEND_TEMPLATE);
900 RECORD(DECL_CLASS_TEMPLATE);
901 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
902 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
903 RECORD(DECL_FUNCTION_TEMPLATE);
904 RECORD(DECL_TEMPLATE_TYPE_PARM);
905 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
906 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
907 RECORD(DECL_STATIC_ASSERT);
908 RECORD(DECL_CXX_BASE_SPECIFIERS);
909 RECORD(DECL_INDIRECTFIELD);
910 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
911
Douglas Gregora72d8c42011-06-03 02:27:19 +0000912 // Statements and Exprs can occur in the Decls and Types block.
913 AddStmtsExprs(Stream, Record);
914
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000915 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000916 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000917 RECORD(PPD_MACRO_DEFINITION);
918 RECORD(PPD_INCLUSION_DIRECTIVE);
919
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000920#undef RECORD
921#undef BLOCK
922 Stream.ExitBlock();
923}
924
Douglas Gregore650c8c2009-07-07 00:12:59 +0000925/// \brief Adjusts the given filename to only write out the portion of the
926/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000927///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000928/// \param Filename the file name to adjust.
929///
930/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
931/// the returned filename will be adjusted by this system root.
932///
933/// \returns either the original filename (if it needs no adjustment) or the
934/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000935static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000936adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000937 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Douglas Gregor832d6202011-07-22 16:35:34 +0000939 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000940 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregore650c8c2009-07-07 00:12:59 +0000942 // Verify that the filename and the system root have the same prefix.
943 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000944 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945 if (Filename[Pos] != isysroot[Pos])
946 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Douglas Gregore650c8c2009-07-07 00:12:59 +0000948 // We hit the end of the filename before we hit the end of the system root.
949 if (!Filename[Pos])
950 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Douglas Gregore650c8c2009-07-07 00:12:59 +0000952 // If the file name has a '/' at the current position, skip over the '/'.
953 // We distinguish sysroot-based includes from absolute includes by the
954 // absence of '/' at the beginning of sysroot-based includes.
955 if (Filename[Pos] == '/')
956 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregore650c8c2009-07-07 00:12:59 +0000958 return Filename + Pos;
959}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000960
Sebastian Redl3397c552010-08-18 23:56:27 +0000961/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000962void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000963 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000964 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000965
Douglas Gregore650c8c2009-07-07 00:12:59 +0000966 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000967 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000969 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000970 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
971 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
973 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
974 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000975 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000976 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000979 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000980 Record.push_back(VERSION_MAJOR);
981 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000982 Record.push_back(CLANG_VERSION_MAJOR);
983 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000984 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000985 const std::string &Triple = Target.getTriple().getTriple();
986 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
987
988 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000989 serialization::ModuleManager &Mgr = Chain->getModuleManager();
990 llvm::SmallVector<char, 128> ModulePaths;
991 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000992
993 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
994 M != MEnd; ++M) {
995 // Skip modules that weren't directly imported.
996 if (!(*M)->isDirectlyImported())
997 continue;
998
999 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1000 // FIXME: Write import location, once it matters.
1001 // FIXME: This writes the absolute path for AST files we depend on.
1002 const std::string &FileName = (*M)->FileName;
1003 Record.push_back(FileName.size());
1004 Record.append(FileName.begin(), FileName.end());
1005 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001006 Stream.EmitRecord(IMPORTS, Record);
1007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Douglas Gregor31d375f2011-05-06 21:43:30 +00001009 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001010 SourceManager &SM = Context.getSourceManager();
1011 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1012 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001013 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001014 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1015 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1016
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001017 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001019 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001020
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001021 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001022 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001023 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001024 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001025 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001026 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001027
1028 Record.clear();
1029 Record.push_back(SM.getMainFileID().getOpaqueValue());
1030 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001031 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001032
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001033 // Original PCH directory
1034 if (!OutputFile.empty() && OutputFile != "-") {
1035 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1036 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1038 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1039
1040 llvm::SmallString<128> OutputPath(OutputFile);
1041
1042 llvm::sys::fs::make_absolute(OutputPath);
1043 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1044
1045 RecordData Record;
1046 Record.push_back(ORIGINAL_PCH_DIR);
1047 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1048 }
1049
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001050 // Repository branch/version information.
1051 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001052 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001053 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1054 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001055 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001056 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001057 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1058 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001059}
1060
1061/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001062void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001063 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001064#define LANGOPT(Name, Bits, Default, Description) \
1065 Record.push_back(LangOpts.Name);
1066#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1067 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1068#include "clang/Basic/LangOptions.def"
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001069
1070 Record.push_back(LangOpts.CurrentModule.size());
1071 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001072 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001073}
1074
Douglas Gregor14f79002009-04-10 03:52:48 +00001075//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001076// stat cache Serialization
1077//===----------------------------------------------------------------------===//
1078
1079namespace {
1080// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001081class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001082public:
1083 typedef const char * key_type;
1084 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Chris Lattner74e976b2010-11-23 19:28:12 +00001086 typedef struct stat data_type;
1087 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001088
1089 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001090 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001091 }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
1093 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001094 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001095 data_type_ref Data) {
1096 unsigned StrLen = strlen(path);
1097 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001098 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099 clang::io::Emit8(Out, DataLen);
1100 return std::make_pair(StrLen + 1, DataLen);
1101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner5f9e2722011-07-23 10:55:15 +00001103 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001104 Out.write(path, KeyLen);
1105 }
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Chris Lattner5f9e2722011-07-23 10:55:15 +00001107 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001108 data_type_ref Data, unsigned DataLen) {
1109 using namespace clang::io;
1110 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner74e976b2010-11-23 19:28:12 +00001112 Emit32(Out, (uint32_t) Data.st_ino);
1113 Emit32(Out, (uint32_t) Data.st_dev);
1114 Emit16(Out, (uint16_t) Data.st_mode);
1115 Emit64(Out, (uint64_t) Data.st_mtime);
1116 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001117
1118 assert(Out.tell() - Start == DataLen && "Wrong data length");
1119 }
1120};
1121} // end anonymous namespace
1122
Sebastian Redl3397c552010-08-18 23:56:27 +00001123/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001124void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001125 // Build the on-disk hash table containing information about every
1126 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001127 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001129 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001130 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001131 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001132 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001133 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001136 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001137 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001138 uint32_t BucketOffset;
1139 {
1140 llvm::raw_svector_ostream Out(StatCacheData);
1141 // Make sure that no bucket is at offset 0
1142 clang::io::Emit32(Out, 0);
1143 BucketOffset = Generator.Emit(Out);
1144 }
1145
1146 // Create a blob abbreviation
1147 using namespace llvm;
1148 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001149 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1153 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1154
1155 // Write the stat cache
1156 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001157 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001158 Record.push_back(BucketOffset);
1159 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001160 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001161}
1162
1163//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001164// Source Manager Serialization
1165//===----------------------------------------------------------------------===//
1166
1167/// \brief Create an abbreviation for the SLocEntry that refers to a
1168/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001169static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001170 using namespace llvm;
1171 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001172 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001177 // FileEntry fields.
1178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001185 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001186}
1187
1188/// \brief Create an abbreviation for the SLocEntry that refers to a
1189/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001190static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001191 using namespace llvm;
1192 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001193 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001199 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001200}
1201
1202/// \brief Create an abbreviation for the SLocEntry that refers to a
1203/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001204static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001205 using namespace llvm;
1206 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001207 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001209 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001210}
1211
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001212/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1213/// expansion.
1214static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001215 using namespace llvm;
1216 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001217 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001223 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001224}
1225
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001226namespace {
1227 // Trait used for the on-disk hash table of header search information.
1228 class HeaderFileInfoTrait {
1229 ASTWriter &Writer;
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001230 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001231
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001232 // Keep track of the framework names we've used during serialization.
1233 SmallVector<char, 128> FrameworkStringData;
1234 llvm::StringMap<unsigned> FrameworkNameOffset;
1235
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001236 public:
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001237 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001238 : Writer(Writer), HS(HS) { }
1239
1240 typedef const char *key_type;
1241 typedef key_type key_type_ref;
1242
1243 typedef HeaderFileInfo data_type;
1244 typedef const data_type &data_type_ref;
1245
1246 static unsigned ComputeHash(const char *path) {
1247 // The hash is based only on the filename portion of the key, so that the
1248 // reader can match based on filenames when symlinking or excess path
1249 // elements ("foo/../", "../") change the form of the name. However,
1250 // complete path is still the key.
1251 return llvm::HashString(llvm::sys::path::filename(path));
1252 }
1253
1254 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001255 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001256 data_type_ref Data) {
1257 unsigned StrLen = strlen(path);
1258 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001259 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001260 clang::io::Emit8(Out, DataLen);
1261 return std::make_pair(StrLen + 1, DataLen);
1262 }
1263
Chris Lattner5f9e2722011-07-23 10:55:15 +00001264 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001265 Out.write(path, KeyLen);
1266 }
1267
Chris Lattner5f9e2722011-07-23 10:55:15 +00001268 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001269 data_type_ref Data, unsigned DataLen) {
1270 using namespace clang::io;
1271 uint64_t Start = Out.tell(); (void)Start;
1272
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001273 unsigned char Flags = (Data.isImport << 5)
1274 | (Data.isPragmaOnce << 4)
1275 | (Data.DirInfo << 2)
1276 | (Data.Resolved << 1)
1277 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001278 Emit8(Out, (uint8_t)Flags);
1279 Emit16(Out, (uint16_t) Data.NumIncludes);
1280
1281 if (!Data.ControllingMacro)
1282 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1283 else
1284 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001285
1286 unsigned Offset = 0;
1287 if (!Data.Framework.empty()) {
1288 // If this header refers into a framework, save the framework name.
1289 llvm::StringMap<unsigned>::iterator Pos
1290 = FrameworkNameOffset.find(Data.Framework);
1291 if (Pos == FrameworkNameOffset.end()) {
1292 Offset = FrameworkStringData.size() + 1;
1293 FrameworkStringData.append(Data.Framework.begin(),
1294 Data.Framework.end());
1295 FrameworkStringData.push_back(0);
1296
1297 FrameworkNameOffset[Data.Framework] = Offset;
1298 } else
1299 Offset = Pos->second;
1300 }
1301 Emit32(Out, Offset);
1302
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001303 assert(Out.tell() - Start == DataLen && "Wrong data length");
1304 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001305
1306 const char *strings_begin() const { return FrameworkStringData.begin(); }
1307 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001308 };
1309} // end anonymous namespace
1310
1311/// \brief Write the header search block for the list of files that
1312///
1313/// \param HS The header search structure to save.
1314///
1315/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001316void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001317 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001318 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1319
1320 if (FilesByUID.size() > HS.header_file_size())
1321 FilesByUID.resize(HS.header_file_size());
1322
1323 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1324 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001325 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001326 unsigned NumHeaderSearchEntries = 0;
1327 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1328 const FileEntry *File = FilesByUID[UID];
1329 if (!File)
1330 continue;
1331
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001332 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1333 // from the external source if it was not provided already.
1334 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001335 if (HFI.External && Chain)
1336 continue;
1337
1338 // Turn the file name into an absolute path, if it isn't already.
1339 const char *Filename = File->getName();
1340 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1341
1342 // If we performed any translation on the file name at all, we need to
1343 // save this string, since the generator will refer to it later.
1344 if (Filename != File->getName()) {
1345 Filename = strdup(Filename);
1346 SavedStrings.push_back(Filename);
1347 }
1348
1349 Generator.insert(Filename, HFI, GeneratorTrait);
1350 ++NumHeaderSearchEntries;
1351 }
1352
1353 // Create the on-disk hash table in a buffer.
1354 llvm::SmallString<4096> TableData;
1355 uint32_t BucketOffset;
1356 {
1357 llvm::raw_svector_ostream Out(TableData);
1358 // Make sure that no bucket is at offset 0
1359 clang::io::Emit32(Out, 0);
1360 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1361 }
1362
1363 // Create a blob abbreviation
1364 using namespace llvm;
1365 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1366 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1371 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1372
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001373 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001374 RecordData Record;
1375 Record.push_back(HEADER_SEARCH_TABLE);
1376 Record.push_back(BucketOffset);
1377 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001378 Record.push_back(TableData.size());
1379 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001380 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1381
1382 // Free all of the strings we had to duplicate.
1383 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1384 free((void*)SavedStrings[I]);
1385}
1386
Douglas Gregor14f79002009-04-10 03:52:48 +00001387/// \brief Writes the block containing the serialized form of the
1388/// source manager.
1389///
1390/// TODO: We should probably use an on-disk hash table (stored in a
1391/// blob), indexed based on the file name, so that we only create
1392/// entries for files that we actually need. In the common case (no
1393/// errors), we probably won't have to create file entries for any of
1394/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001395void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001396 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001397 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001398 RecordData Record;
1399
Chris Lattnerf04ad692009-04-10 17:16:57 +00001400 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001401 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001402
1403 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001404 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1405 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1406 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001407 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001408
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001409 // Write out the source location entry table. We skip the first
1410 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001411 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001412 // Write out the offsets of only source location file entries.
1413 // We will go through them in ASTReader::validateFileEntries().
1414 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001415 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001416 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1417 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001418 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001419 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001420 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001421
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001422 // Record the offset of this source-location entry.
1423 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1424
1425 // Figure out which record code to use.
1426 unsigned Code;
1427 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001428 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1429 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001430 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001431 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1432 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001433 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001434 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001435 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001436 Record.clear();
1437 Record.push_back(Code);
1438
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001439 // Starting offset of this entry within this module, so skip the dummy.
1440 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001441 if (SLoc->isFile()) {
1442 const SrcMgr::FileInfo &File = SLoc->getFile();
1443 Record.push_back(File.getIncludeLoc().getRawEncoding());
1444 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1445 Record.push_back(File.hasLineDirectives());
1446
1447 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001448 if (Content->OrigEntry) {
1449 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001450 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001451
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001452 // The source location entry is a file. The blob associated
1453 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Douglas Gregor2d52be52010-03-21 22:49:54 +00001455 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001456 Record.push_back(Content->OrigEntry->getSize());
1457 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001458 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001459 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001460
1461 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1462 if (FDI != FileDeclIDs.end()) {
1463 Record.push_back(FDI->second->FirstDeclIndex);
1464 Record.push_back(FDI->second->DeclIDs.size());
1465 } else {
1466 Record.push_back(0);
1467 Record.push_back(0);
1468 }
Douglas Gregora081da52011-11-16 20:05:18 +00001469
Douglas Gregore650c8c2009-07-07 00:12:59 +00001470 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001471 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001472 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001473
1474 // Ask the file manager to fixup the relative path for us. This will
1475 // honor the working directory.
1476 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1477
1478 // FIXME: This call to make_absolute shouldn't be necessary, the
1479 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001480 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001481 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Douglas Gregore650c8c2009-07-07 00:12:59 +00001483 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001484 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001485
1486 if (Content->BufferOverridden) {
1487 Record.clear();
1488 Record.push_back(SM_SLOC_BUFFER_BLOB);
1489 const llvm::MemoryBuffer *Buffer
1490 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1491 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1492 StringRef(Buffer->getBufferStart(),
1493 Buffer->getBufferSize() + 1));
1494 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001495 } else {
1496 // The source location entry is a buffer. The blob associated
1497 // with this entry contains the contents of the buffer.
1498
1499 // We add one to the size so that we capture the trailing NULL
1500 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1501 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001502 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001503 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001504 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001505 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001506 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001507 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001508 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001509 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001510 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001511 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001512
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001513 if (strcmp(Name, "<built-in>") == 0) {
1514 PreloadSLocs.push_back(SLocEntryOffsets.size());
1515 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001516 }
1517 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001518 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001519 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001520 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1521 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001522 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1523 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001524
1525 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001526 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001527 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001528 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001529 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001530 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001531 }
1532 }
1533
Douglas Gregorc9490c02009-04-16 22:23:12 +00001534 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001535
1536 if (SLocEntryOffsets.empty())
1537 return;
1538
Sebastian Redl3397c552010-08-18 23:56:27 +00001539 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001540 // table is used for lazily loading source-location information.
1541 using namespace llvm;
1542 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001543 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1547 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001549 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001550 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001551 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001552 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001553 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001554
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001555 Abbrev = new BitCodeAbbrev();
1556 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1559 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1560
1561 Record.clear();
1562 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1563 Record.push_back(SLocFileEntryOffsets.size());
1564 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1565 data(SLocFileEntryOffsets));
1566
Sebastian Redl3397c552010-08-18 23:56:27 +00001567 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001568 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001569 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001570
1571 // Write the line table. It depends on remapping working, so it must come
1572 // after the source location offsets.
1573 if (SourceMgr.hasLineTable()) {
1574 LineTableInfo &LineTable = SourceMgr.getLineTable();
1575
1576 Record.clear();
1577 // Emit the file names
1578 Record.push_back(LineTable.getNumFilenames());
1579 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1580 // Emit the file name
1581 const char *Filename = LineTable.getFilename(I);
1582 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1583 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1584 Record.push_back(FilenameLen);
1585 if (FilenameLen)
1586 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1587 }
1588
1589 // Emit the line entries
1590 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1591 L != LEnd; ++L) {
1592 // Only emit entries for local files.
1593 if (L->first < 0)
1594 continue;
1595
1596 // Emit the file ID
1597 Record.push_back(L->first);
1598
1599 // Emit the line entries
1600 Record.push_back(L->second.size());
1601 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1602 LEEnd = L->second.end();
1603 LE != LEEnd; ++LE) {
1604 Record.push_back(LE->FileOffset);
1605 Record.push_back(LE->LineNo);
1606 Record.push_back(LE->FilenameID);
1607 Record.push_back((unsigned)LE->FileKind);
1608 Record.push_back(LE->IncludeOffset);
1609 }
1610 }
1611 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1612 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001613}
1614
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001615//===----------------------------------------------------------------------===//
1616// Preprocessor Serialization
1617//===----------------------------------------------------------------------===//
1618
Douglas Gregor9c736102011-02-10 18:20:09 +00001619static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1620 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1621 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1622 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1623 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1624 return X.first->getName().compare(Y.first->getName());
1625}
1626
Chris Lattner0b1fb982009-04-10 17:15:23 +00001627/// \brief Writes the block containing the serialized form of the
1628/// preprocessor.
1629///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001630void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001631 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1632 if (PPRec)
1633 WritePreprocessorDetail(*PPRec);
1634
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001635 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001636
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001637 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1638 if (PP.getCounterValue() != 0) {
1639 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001640 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001641 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001642 }
1643
1644 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001645 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Sebastian Redl3397c552010-08-18 23:56:27 +00001647 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001648 // FIXME: use diagnostics subsystem for localization etc.
1649 if (PP.SawDateOrTime())
1650 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Douglas Gregorecdcb882010-10-20 22:00:55 +00001652
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001653 // Loop over all the macro definitions that are live at the end of the file,
1654 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001655
Douglas Gregor9c736102011-02-10 18:20:09 +00001656 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001657 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001658 MacrosToEmit;
1659 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001660 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1661 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001662 I != E; ++I) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001663 const IdentifierInfo *Name = I->first;
Douglas Gregoraa93a872011-10-17 15:32:29 +00001664 if (!IsModule || I->second->isPublic()) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001665 MacroDefinitionsSeen.insert(Name);
Douglas Gregor7143aab2011-09-01 17:04:32 +00001666 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1667 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001668 }
1669
1670 // Sort the set of macro definitions that need to be serialized by the
1671 // name of the macro, to provide a stable ordering.
1672 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1673 &compareMacroDefinitions);
1674
Douglas Gregor040a8042011-02-11 00:26:14 +00001675 // Resolve any identifiers that defined macros at the time they were
1676 // deserialized, adding them to the list of macros to emit (if appropriate).
1677 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1678 IdentifierInfo *Name
1679 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1680 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1681 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1682 }
1683
Douglas Gregor9c736102011-02-10 18:20:09 +00001684 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1685 const IdentifierInfo *Name = MacrosToEmit[I].first;
1686 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001687 if (!MI)
1688 continue;
1689
Sebastian Redl3397c552010-08-18 23:56:27 +00001690 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001691 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001692 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001693
1694 // FIXME: There is a (probably minor) optimization we could do here, if
1695 // the macro comes from the original PCH but the identifier comes from a
1696 // chained PCH, by storing the offset into the original PCH rather than
1697 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001698 if (MI->isBuiltinMacro() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00001699 (Chain &&
1700 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1701 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001702 continue;
1703
Douglas Gregor9c736102011-02-10 18:20:09 +00001704 AddIdentifierRef(Name, Record);
1705 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001706 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1707 Record.push_back(MI->isUsed());
Douglas Gregoraa93a872011-10-17 15:32:29 +00001708 Record.push_back(MI->isPublic());
1709 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001710 unsigned Code;
1711 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001712 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001713 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001714 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001716 Record.push_back(MI->isC99Varargs());
1717 Record.push_back(MI->isGNUVarargs());
1718 Record.push_back(MI->getNumArgs());
1719 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1720 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001721 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001722 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001723
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001724 // If we have a detailed preprocessing record, record the macro definition
1725 // ID that corresponds to this macro.
1726 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001727 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001728
Douglas Gregorc9490c02009-04-16 22:23:12 +00001729 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001730 Record.clear();
1731
Chris Lattnerdf961c22009-04-10 18:08:30 +00001732 // Emit the tokens array.
1733 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1734 // Note that we know that the preprocessor does not have any annotation
1735 // tokens in it because they are created by the parser, and thus can't be
1736 // in a macro definition.
1737 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Chris Lattnerdf961c22009-04-10 18:08:30 +00001739 Record.push_back(Tok.getLocation().getRawEncoding());
1740 Record.push_back(Tok.getLength());
1741
Chris Lattnerdf961c22009-04-10 18:08:30 +00001742 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1743 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001744 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001745 // FIXME: Should translate token kind to a stable encoding.
1746 Record.push_back(Tok.getKind());
1747 // FIXME: Should translate token flags to a stable encoding.
1748 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001750 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001751 Record.clear();
1752 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001753 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001754 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001755 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001756}
1757
1758void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001759 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001760 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001761
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001762 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001763
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001764 // Enter the preprocessor block.
1765 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001766
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001767 // If the preprocessor has a preprocessing record, emit it.
1768 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001769 using namespace llvm;
1770
1771 // Set up the abbreviation for
1772 unsigned InclusionAbbrev = 0;
1773 {
1774 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1775 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1780 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1781 }
1782
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001783 unsigned FirstPreprocessorEntityID
1784 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1785 + NUM_PREDEF_PP_ENTITY_IDS;
1786 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001787 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001788 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1789 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001790 E != EEnd;
1791 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001792 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001793
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001794 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1795 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001796
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001797 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001798 // Record this macro definition's ID.
1799 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001800
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001801 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001802 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1803 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001804 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001805
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001806 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001807 Record.push_back(ME->isBuiltinMacro());
1808 if (ME->isBuiltinMacro())
1809 AddIdentifierRef(ME->getName(), Record);
1810 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001811 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001812 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001813 continue;
1814 }
1815
1816 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1817 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001818 Record.push_back(ID->getFileName().size());
1819 Record.push_back(ID->wasInQuotes());
1820 Record.push_back(static_cast<unsigned>(ID->getKind()));
1821 llvm::SmallString<64> Buffer;
1822 Buffer += ID->getFileName();
1823 Buffer += ID->getFile()->getName();
1824 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1825 continue;
1826 }
1827
1828 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1829 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001830 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001831
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001832 // Write the offsets table for the preprocessing record.
1833 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001834 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1835
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001836 // Write the offsets table for identifier IDs.
1837 using namespace llvm;
1838 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001839 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001842 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001843
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001844 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001845 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001846 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001847 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1848 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001849 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001850}
1851
Douglas Gregore209e502011-12-06 01:10:29 +00001852unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1853 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1854 if (Known != SubmoduleIDs.end())
1855 return Known->second;
1856
1857 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1858}
1859
Douglas Gregor26ced122011-12-01 00:59:36 +00001860/// \brief Compute the number of modules within the given tree (including the
1861/// given module).
1862static unsigned getNumberOfModules(Module *Mod) {
1863 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001864 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1865 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001866 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001867 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001868
1869 return ChildModules + 1;
1870}
1871
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001872void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001873 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001874 // FIXME: This feels like it belongs somewhere else, but there are no
1875 // other consumers of this information.
1876 SourceManager &SrcMgr = PP->getSourceManager();
1877 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1878 for (ASTContext::import_iterator I = Context->local_import_begin(),
1879 IEnd = Context->local_import_end();
1880 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001881 if (Module *ImportedFrom
1882 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1883 SrcMgr))) {
1884 ImportedFrom->Imports.push_back(I->getImportedModule());
1885 }
1886 }
1887
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001888 // Enter the submodule description block.
1889 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1890
1891 // Write the abbreviations needed for the submodules block.
1892 using namespace llvm;
1893 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1894 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregor1e123682011-12-05 22:27:44 +00001899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
1900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
1901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1903 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1904
1905 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001906 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1908 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1909
1910 Abbrev = new BitCodeAbbrev();
1911 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1913 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001914
1915 Abbrev = new BitCodeAbbrev();
1916 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1918 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1919
Douglas Gregor51f564f2011-12-31 04:05:44 +00001920 Abbrev = new BitCodeAbbrev();
1921 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1923 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1924
Douglas Gregor26ced122011-12-01 00:59:36 +00001925 // Write the submodule metadata block.
1926 RecordData Record;
1927 Record.push_back(getNumberOfModules(WritingModule));
1928 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1929 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1930
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001931 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001932 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001933 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001934 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001935 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001936 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001937 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001938
1939 // Emit the definition of the block.
1940 Record.clear();
1941 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001942 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001943 if (Mod->Parent) {
1944 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1945 Record.push_back(SubmoduleIDs[Mod->Parent]);
1946 } else {
1947 Record.push_back(0);
1948 }
1949 Record.push_back(Mod->IsFramework);
1950 Record.push_back(Mod->IsExplicit);
Douglas Gregor1e123682011-12-05 22:27:44 +00001951 Record.push_back(Mod->InferSubmodules);
1952 Record.push_back(Mod->InferExplicitSubmodules);
1953 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001954 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1955
Douglas Gregor51f564f2011-12-31 04:05:44 +00001956 // Emit the requirements.
1957 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1958 Record.clear();
1959 Record.push_back(SUBMODULE_REQUIRES);
1960 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1961 Mod->Requires[I].data(),
1962 Mod->Requires[I].size());
1963 }
1964
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001965 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00001966 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001967 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001968 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001969 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00001970 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00001971 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1972 Record.clear();
1973 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1974 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1975 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001976 }
1977
1978 // Emit the headers.
1979 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1980 Record.clear();
1981 Record.push_back(SUBMODULE_HEADER);
1982 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1983 Mod->Headers[I]->getName());
1984 }
Douglas Gregor55988682011-12-05 16:33:54 +00001985
1986 // Emit the imports.
1987 if (!Mod->Imports.empty()) {
1988 Record.clear();
1989 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00001990 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00001991 assert(ImportedID && "Unknown submodule!");
1992 Record.push_back(ImportedID);
1993 }
1994 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
1995 }
1996
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00001997 // Emit the exports.
1998 if (!Mod->Exports.empty()) {
1999 Record.clear();
2000 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002001 if (Module *Exported = Mod->Exports[I].getPointer()) {
2002 unsigned ExportedID = SubmoduleIDs[Exported];
2003 assert(ExportedID > 0 && "Unknown submodule ID?");
2004 Record.push_back(ExportedID);
2005 } else {
2006 Record.push_back(0);
2007 }
2008
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002009 Record.push_back(Mod->Exports[I].getInt());
2010 }
2011 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2012 }
2013
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002014 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002015 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2016 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002017 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002018 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002019 }
2020
2021 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002022
2023 assert((NextSubmoduleID - FirstSubmoduleID
2024 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002025}
2026
Douglas Gregor185dbd72011-12-01 02:07:58 +00002027serialization::SubmoduleID
2028ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002029 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002030 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002031
2032 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002033 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002034 Module *OwningMod
2035 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002036 if (!OwningMod)
2037 return 0;
2038
Douglas Gregore209e502011-12-06 01:10:29 +00002039 // Check whether this submodule is part of our own module.
2040 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002041 return 0;
2042
Douglas Gregore209e502011-12-06 01:10:29 +00002043 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002044}
2045
David Blaikied6471f72011-09-25 23:23:43 +00002046void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002047 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002048 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002049 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2050 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002051 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002052 if (point.Loc.isInvalid())
2053 continue;
2054
2055 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002056 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002057 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002058 if (I->second.isPragma()) {
2059 Record.push_back(I->first);
2060 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002061 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002062 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002063 Record.push_back(-1); // mark the end of the diag/map pairs for this
2064 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002065 }
2066
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002067 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002068 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002069}
2070
Anders Carlssonc8505782011-03-06 18:41:18 +00002071void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2072 if (CXXBaseSpecifiersOffsets.empty())
2073 return;
2074
2075 RecordData Record;
2076
2077 // Create a blob abbreviation for the C++ base specifiers offsets.
2078 using namespace llvm;
2079
2080 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2081 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2084 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2085
Douglas Gregore92b8a12011-08-04 00:01:48 +00002086 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002087 Record.clear();
2088 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2089 Record.push_back(CXXBaseSpecifiersOffsets.size());
2090 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002091 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002092}
2093
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002094//===----------------------------------------------------------------------===//
2095// Type Serialization
2096//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002097
Sebastian Redl3397c552010-08-18 23:56:27 +00002098/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002099void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002100 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002101 if (Idx.getIndex() == 0) // we haven't seen this type before.
2102 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Douglas Gregor97475832010-10-05 18:37:06 +00002104 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002105
Douglas Gregor2cf26342009-04-09 22:27:44 +00002106 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002107 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002108 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002109 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002110 else if (TypeOffsets.size() < Index) {
2111 TypeOffsets.resize(Index + 1);
2112 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002113 }
2114
2115 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002116
Douglas Gregor2cf26342009-04-09 22:27:44 +00002117 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002118 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002119
Douglas Gregora4923eb2009-11-16 21:35:15 +00002120 if (T.hasLocalNonFastQualifiers()) {
2121 Qualifiers Qs = T.getLocalQualifiers();
2122 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002123 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002124 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002125 } else {
2126 switch (T->getTypeClass()) {
2127 // For all of the concrete, non-dependent types, call the
2128 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002129#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002130 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002131#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002132#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002133 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002134 }
2135
2136 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002137 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002138
2139 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002140 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002141}
2142
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002143//===----------------------------------------------------------------------===//
2144// Declaration Serialization
2145//===----------------------------------------------------------------------===//
2146
Douglas Gregor2cf26342009-04-09 22:27:44 +00002147/// \brief Write the block containing all of the declaration IDs
2148/// lexically declared within the given DeclContext.
2149///
2150/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2151/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002152uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002153 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002154 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002155 return 0;
2156
Douglas Gregorc9490c02009-04-16 22:23:12 +00002157 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002158 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002159 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002160 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002161 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2162 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002163 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002164
Douglas Gregor25123082009-04-22 22:34:57 +00002165 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002166 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002167 return Offset;
2168}
2169
Sebastian Redla4232eb2010-08-18 23:56:21 +00002170void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002171 using namespace llvm;
2172 RecordData Record;
2173
2174 // Write the type offsets array
2175 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002176 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2180 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2181 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002182 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002183 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002184 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002185 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002186
2187 // Write the declaration offsets array
2188 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002189 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2193 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2194 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002195 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002196 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002197 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002198 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002199}
2200
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002201void ASTWriter::WriteFileDeclIDsMap() {
2202 using namespace llvm;
2203 RecordData Record;
2204
2205 // Join the vectors of DeclIDs from all files.
2206 SmallVector<DeclID, 256> FileSortedIDs;
2207 for (FileDeclIDsTy::iterator
2208 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2209 DeclIDInFileInfo &Info = *FI->second;
2210 Info.FirstDeclIndex = FileSortedIDs.size();
2211 for (LocDeclIDsTy::iterator
2212 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2213 FileSortedIDs.push_back(DI->second);
2214 }
2215
2216 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2217 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2219 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2220 Record.push_back(FILE_SORTED_DECLS);
2221 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2222}
2223
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002224//===----------------------------------------------------------------------===//
2225// Global Method Pool and Selector Serialization
2226//===----------------------------------------------------------------------===//
2227
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002228namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002229// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002230class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002231 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002232
2233public:
2234 typedef Selector key_type;
2235 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002236
Sebastian Redl5d050072010-08-04 17:20:04 +00002237 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002238 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002239 ObjCMethodList Instance, Factory;
2240 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002241 typedef const data_type& data_type_ref;
2242
Sebastian Redl3397c552010-08-18 23:56:27 +00002243 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002245 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002246 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002247 }
Mike Stump1eb44332009-09-09 15:08:12 +00002248
2249 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002250 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002251 data_type_ref Methods) {
2252 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2253 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002254 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2255 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002256 Method = Method->Next)
2257 if (Method->Method)
2258 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002259 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002260 Method = Method->Next)
2261 if (Method->Method)
2262 DataLen += 4;
2263 clang::io::Emit16(Out, DataLen);
2264 return std::make_pair(KeyLen, DataLen);
2265 }
Mike Stump1eb44332009-09-09 15:08:12 +00002266
Chris Lattner5f9e2722011-07-23 10:55:15 +00002267 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002268 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002269 assert((Start >> 32) == 0 && "Selector key offset too large");
2270 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002271 unsigned N = Sel.getNumArgs();
2272 clang::io::Emit16(Out, N);
2273 if (N == 0)
2274 N = 1;
2275 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002276 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002277 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2278 }
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Chris Lattner5f9e2722011-07-23 10:55:15 +00002280 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002281 data_type_ref Methods, unsigned DataLen) {
2282 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002283 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002284 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002285 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002286 Method = Method->Next)
2287 if (Method->Method)
2288 ++NumInstanceMethods;
2289
2290 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002291 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002292 Method = Method->Next)
2293 if (Method->Method)
2294 ++NumFactoryMethods;
2295
2296 clang::io::Emit16(Out, NumInstanceMethods);
2297 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002298 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002299 Method = Method->Next)
2300 if (Method->Method)
2301 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002302 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002303 Method = Method->Next)
2304 if (Method->Method)
2305 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002306
2307 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002308 }
2309};
2310} // end anonymous namespace
2311
Sebastian Redl059612d2010-08-03 21:58:15 +00002312/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002313///
2314/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002315/// in an on-disk hash table indexed by the selector. The hash table also
2316/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002317void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002318 using namespace llvm;
2319
Sebastian Redl059612d2010-08-03 21:58:15 +00002320 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002321 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002322 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002323 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002324 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002325 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002326 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002327 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002328
Sebastian Redl059612d2010-08-03 21:58:15 +00002329 // Create the on-disk hash table representation. We walk through every
2330 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002331 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002332 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002333 I = SelectorIDs.begin(), E = SelectorIDs.end();
2334 I != E; ++I) {
2335 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002336 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002337 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002338 I->second,
2339 ObjCMethodList(),
2340 ObjCMethodList()
2341 };
2342 if (F != SemaRef.MethodPool.end()) {
2343 Data.Instance = F->second.first;
2344 Data.Factory = F->second.second;
2345 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002346 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002347 // changed.
2348 if (Chain && I->second < FirstSelectorID) {
2349 // Selector already exists. Did it change?
2350 bool changed = false;
2351 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2352 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002353 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002354 changed = true;
2355 }
2356 for (ObjCMethodList *M = &Data.Factory; !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 if (!changed)
2362 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002363 } else if (Data.Instance.Method || Data.Factory.Method) {
2364 // A new method pool entry.
2365 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002366 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002367 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002368 }
2369
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002370 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002371 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002372 uint32_t BucketOffset;
2373 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002374 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002375 llvm::raw_svector_ostream Out(MethodPool);
2376 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002377 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002378 BucketOffset = Generator.Emit(Out, Trait);
2379 }
2380
2381 // Create a blob abbreviation
2382 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002383 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2387 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2388
Douglas Gregor83941df2009-04-25 17:48:32 +00002389 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002390 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002391 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002392 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002393 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002394 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002395
2396 // Create a blob abbreviation for the selector table offsets.
2397 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002398 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2402 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2403
2404 // Write the selector offsets table.
2405 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002406 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002407 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002408 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002409 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002410 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002411 }
2412}
2413
Sebastian Redl3397c552010-08-18 23:56:27 +00002414/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002415void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002416 using namespace llvm;
2417 if (SemaRef.ReferencedSelectors.empty())
2418 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002419
Fariborz Jahanian32019832010-07-23 19:11:11 +00002420 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002421
Sebastian Redl3397c552010-08-18 23:56:27 +00002422 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002423 // very tricky to fix, and given that @selector shouldn't really appear in
2424 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002425 for (DenseMap<Selector, SourceLocation>::iterator S =
2426 SemaRef.ReferencedSelectors.begin(),
2427 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2428 Selector Sel = (*S).first;
2429 SourceLocation Loc = (*S).second;
2430 AddSelectorRef(Sel, Record);
2431 AddSourceLocation(Loc, Record);
2432 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002433 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002434}
2435
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002436//===----------------------------------------------------------------------===//
2437// Identifier Table Serialization
2438//===----------------------------------------------------------------------===//
2439
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002440namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002441class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002442 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002443 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002444 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002445 bool IsModule;
2446
Douglas Gregora92193e2009-04-28 21:18:29 +00002447 /// \brief Determines whether this is an "interesting" identifier
2448 /// that needs a full IdentifierInfo structure written into the hash
2449 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002450 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002451 if (II->isPoisoned() ||
2452 II->isExtensionToken() ||
2453 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002454 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002455 II->getFETokenInfo<void>())
2456 return true;
2457
Douglas Gregorce835df2011-09-14 22:14:14 +00002458 return hasMacroDefinition(II, Macro);
2459 }
2460
2461 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002462 if (!II->hasMacroDefinition())
2463 return false;
2464
Douglas Gregorce835df2011-09-14 22:14:14 +00002465 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002466 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002467
Douglas Gregorce835df2011-09-14 22:14:14 +00002468 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002469 }
2470
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002471public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002472 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002473 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002475 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002476 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002477
Douglas Gregoreee242f2011-10-27 09:33:13 +00002478 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2479 IdentifierResolver &IdResolver, bool IsModule)
2480 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002481
2482 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002483 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002484 }
Mike Stump1eb44332009-09-09 15:08:12 +00002485
2486 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002487 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002488 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002489 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002490 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002491 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002492 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002493 if (hasMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002494 DataLen += 8;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002495
2496 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2497 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002498 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002499 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002500 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002501 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002502 // We emit the key length after the data length so that every
2503 // string is preceded by a 16-bit length. This matches the PTH
2504 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002505 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002506 return std::make_pair(KeyLen, DataLen);
2507 }
Mike Stump1eb44332009-09-09 15:08:12 +00002508
Chris Lattner5f9e2722011-07-23 10:55:15 +00002509 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002510 unsigned KeyLen) {
2511 // Record the location of the key data. This is used when generating
2512 // the mapping from persistent IDs to strings.
2513 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002514 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002515 }
Mike Stump1eb44332009-09-09 15:08:12 +00002516
Douglas Gregor7143aab2011-09-01 17:04:32 +00002517 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002518 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002519 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002520 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002521 clang::io::Emit32(Out, ID << 1);
2522 return;
2523 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002524
Douglas Gregora92193e2009-04-28 21:18:29 +00002525 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002526 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002527 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002528 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topper925be542011-12-19 05:04:33 +00002529 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregorce835df2011-09-14 22:14:14 +00002530 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002531 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2532 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002533 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002534 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002535 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002536
Douglas Gregor13292642011-12-02 15:45:10 +00002537 if (HasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002538 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor13292642011-12-02 15:45:10 +00002539 clang::io::Emit32(Out,
2540 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2541 }
2542
Douglas Gregor668c1a42009-04-21 22:25:48 +00002543 // Emit the declaration IDs in reverse order, because the
2544 // IdentifierResolver provides the declarations as they would be
2545 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002546 // "stat"), but the ASTReader adds declarations to the end of the list
2547 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002548 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002549 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2550 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002551 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002552 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002553 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002554 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002555 }
2556};
2557} // end anonymous namespace
2558
Sebastian Redl3397c552010-08-18 23:56:27 +00002559/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002560///
2561/// The identifier table consists of a blob containing string data
2562/// (the actual identifiers themselves) and a separate "offsets" index
2563/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002564void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2565 IdentifierResolver &IdResolver,
2566 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002567 using namespace llvm;
2568
2569 // Create and write out the blob that contains the identifier
2570 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002571 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002572 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002573 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002574
Douglas Gregor92b059e2009-04-28 20:33:11 +00002575 // Look for any identifiers that were named while processing the
2576 // headers, but are otherwise not needed. We add these to the hash
2577 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002578 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002579 // file.
2580 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2581 IDEnd = PP.getIdentifierTable().end();
2582 ID != IDEnd; ++ID)
2583 getIdentifierRef(ID->second);
2584
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002585 // Create the on-disk hash table representation. We only store offsets
2586 // for identifiers that appear here for the first time.
2587 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002588 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002589 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2590 ID != IDEnd; ++ID) {
2591 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002592 if (!Chain || !ID->first->isFromAST() ||
2593 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002594 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2595 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002596 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002597
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002598 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002599 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002600 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002601 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002602 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002603 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002604 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002605 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002606 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002607 }
2608
2609 // Create a blob abbreviation
2610 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002611 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002614 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002615
2616 // Write the identifier table
2617 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002618 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002619 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002620 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002621 }
2622
2623 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002624 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002625 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2629 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2630
2631 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002632 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002633 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002634 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002635 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002636 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002637}
2638
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002639//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002640// DeclContext's Name Lookup Table Serialization
2641//===----------------------------------------------------------------------===//
2642
2643namespace {
2644// Trait used for the on-disk hash table used in the method pool.
2645class ASTDeclContextNameLookupTrait {
2646 ASTWriter &Writer;
2647
2648public:
2649 typedef DeclarationName key_type;
2650 typedef key_type key_type_ref;
2651
2652 typedef DeclContext::lookup_result data_type;
2653 typedef const data_type& data_type_ref;
2654
2655 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2656
2657 unsigned ComputeHash(DeclarationName Name) {
2658 llvm::FoldingSetNodeID ID;
2659 ID.AddInteger(Name.getNameKind());
2660
2661 switch (Name.getNameKind()) {
2662 case DeclarationName::Identifier:
2663 ID.AddString(Name.getAsIdentifierInfo()->getName());
2664 break;
2665 case DeclarationName::ObjCZeroArgSelector:
2666 case DeclarationName::ObjCOneArgSelector:
2667 case DeclarationName::ObjCMultiArgSelector:
2668 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2669 break;
2670 case DeclarationName::CXXConstructorName:
2671 case DeclarationName::CXXDestructorName:
2672 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002673 break;
2674 case DeclarationName::CXXOperatorName:
2675 ID.AddInteger(Name.getCXXOverloadedOperator());
2676 break;
2677 case DeclarationName::CXXLiteralOperatorName:
2678 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2679 case DeclarationName::CXXUsingDirective:
2680 break;
2681 }
2682
2683 return ID.ComputeHash();
2684 }
2685
2686 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002687 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002688 data_type_ref Lookup) {
2689 unsigned KeyLen = 1;
2690 switch (Name.getNameKind()) {
2691 case DeclarationName::Identifier:
2692 case DeclarationName::ObjCZeroArgSelector:
2693 case DeclarationName::ObjCOneArgSelector:
2694 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002695 case DeclarationName::CXXLiteralOperatorName:
2696 KeyLen += 4;
2697 break;
2698 case DeclarationName::CXXOperatorName:
2699 KeyLen += 1;
2700 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002701 case DeclarationName::CXXConstructorName:
2702 case DeclarationName::CXXDestructorName:
2703 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002704 case DeclarationName::CXXUsingDirective:
2705 break;
2706 }
2707 clang::io::Emit16(Out, KeyLen);
2708
2709 // 2 bytes for num of decls and 4 for each DeclID.
2710 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2711 clang::io::Emit16(Out, DataLen);
2712
2713 return std::make_pair(KeyLen, DataLen);
2714 }
2715
Chris Lattner5f9e2722011-07-23 10:55:15 +00002716 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002717 using namespace clang::io;
2718
2719 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2720 Emit8(Out, Name.getNameKind());
2721 switch (Name.getNameKind()) {
2722 case DeclarationName::Identifier:
2723 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2724 break;
2725 case DeclarationName::ObjCZeroArgSelector:
2726 case DeclarationName::ObjCOneArgSelector:
2727 case DeclarationName::ObjCMultiArgSelector:
2728 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2729 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002730 case DeclarationName::CXXOperatorName:
2731 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2732 Emit8(Out, Name.getCXXOverloadedOperator());
2733 break;
2734 case DeclarationName::CXXLiteralOperatorName:
2735 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2736 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002737 case DeclarationName::CXXConstructorName:
2738 case DeclarationName::CXXDestructorName:
2739 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002740 case DeclarationName::CXXUsingDirective:
2741 break;
2742 }
2743 }
2744
Chris Lattner5f9e2722011-07-23 10:55:15 +00002745 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002746 data_type Lookup, unsigned DataLen) {
2747 uint64_t Start = Out.tell(); (void)Start;
2748 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2749 for (; Lookup.first != Lookup.second; ++Lookup.first)
2750 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2751
2752 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2753 }
2754};
2755} // end anonymous namespace
2756
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002757/// \brief Write the block containing all of the declaration IDs
2758/// visible from the given DeclContext.
2759///
2760/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002761/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002762uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2763 DeclContext *DC) {
2764 if (DC->getPrimaryContext() != DC)
2765 return 0;
2766
2767 // Since there is no name lookup into functions or methods, don't bother to
2768 // build a visible-declarations table for these entities.
2769 if (DC->isFunctionOrMethod())
2770 return 0;
2771
2772 // If not in C++, we perform name lookup for the translation unit via the
2773 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2774 // FIXME: In C++ we need the visible declarations in order to "see" the
2775 // friend declarations, is there a way to do this without writing the table ?
2776 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2777 return 0;
2778
2779 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002780 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002781 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002782
2783 // Serialize the contents of the mapping used for lookup. Note that,
2784 // although we have two very different code paths, the serialized
2785 // representation is the same for both cases: a declaration name,
2786 // followed by a size, followed by references to the visible
2787 // declarations that have that name.
2788 uint64_t Offset = Stream.GetCurrentBitNo();
2789 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2790 if (!Map || Map->empty())
2791 return 0;
2792
2793 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2794 ASTDeclContextNameLookupTrait Trait(*this);
2795
2796 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002797 DeclarationName ConversionName;
2798 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002799 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2800 D != DEnd; ++D) {
2801 DeclarationName Name = D->first;
2802 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002803 if (Result.first != Result.second) {
2804 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2805 // Hash all conversion function names to the same name. The actual
2806 // type information in conversion function name is not used in the
2807 // key (since such type information is not stable across different
2808 // modules), so the intended effect is to coalesce all of the conversion
2809 // functions under a single key.
2810 if (!ConversionName)
2811 ConversionName = Name;
2812 ConversionDecls.append(Result.first, Result.second);
2813 continue;
2814 }
2815
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002816 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002817 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002818 }
2819
Douglas Gregore5a54b62011-08-30 20:49:19 +00002820 // Add the conversion functions
2821 if (!ConversionDecls.empty()) {
2822 Generator.insert(ConversionName,
2823 DeclContext::lookup_result(ConversionDecls.begin(),
2824 ConversionDecls.end()),
2825 Trait);
2826 }
2827
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002828 // Create the on-disk hash table in a buffer.
2829 llvm::SmallString<4096> LookupTable;
2830 uint32_t BucketOffset;
2831 {
2832 llvm::raw_svector_ostream Out(LookupTable);
2833 // Make sure that no bucket is at offset 0
2834 clang::io::Emit32(Out, 0);
2835 BucketOffset = Generator.Emit(Out, Trait);
2836 }
2837
2838 // Write the lookup table
2839 RecordData Record;
2840 Record.push_back(DECL_CONTEXT_VISIBLE);
2841 Record.push_back(BucketOffset);
2842 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2843 LookupTable.str());
2844
2845 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2846 ++NumVisibleDeclContexts;
2847 return Offset;
2848}
2849
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002850/// \brief Write an UPDATE_VISIBLE block for the given context.
2851///
2852/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2853/// DeclContext in a dependent AST file. As such, they only exist for the TU
2854/// (in C++) and for namespaces.
2855void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002856 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2857 if (!Map || Map->empty())
2858 return;
2859
2860 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2861 ASTDeclContextNameLookupTrait Trait(*this);
2862
2863 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002864 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2865 D != DEnd; ++D) {
2866 DeclarationName Name = D->first;
2867 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002868 // For any name that appears in this table, the results are complete, i.e.
2869 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002870 if (Result.first != Result.second)
2871 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002872 }
2873
2874 // Create the on-disk hash table in a buffer.
2875 llvm::SmallString<4096> LookupTable;
2876 uint32_t BucketOffset;
2877 {
2878 llvm::raw_svector_ostream Out(LookupTable);
2879 // Make sure that no bucket is at offset 0
2880 clang::io::Emit32(Out, 0);
2881 BucketOffset = Generator.Emit(Out, Trait);
2882 }
2883
2884 // Write the lookup table
2885 RecordData Record;
2886 Record.push_back(UPDATE_VISIBLE);
2887 Record.push_back(getDeclID(cast<Decl>(DC)));
2888 Record.push_back(BucketOffset);
2889 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2890}
2891
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002892/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2893void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2894 RecordData Record;
2895 Record.push_back(Opts.fp_contract);
2896 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2897}
2898
2899/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2900void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2901 if (!SemaRef.Context.getLangOptions().OpenCL)
2902 return;
2903
2904 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2905 RecordData Record;
2906#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2907#include "clang/Basic/OpenCLExtensions.def"
2908 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2909}
2910
Douglas Gregor2171bf12012-01-15 16:58:34 +00002911void ASTWriter::WriteRedeclarations() {
2912 RecordData LocalRedeclChains;
2913 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2914
2915 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2916 Decl *First = Redeclarations[I];
2917 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2918
2919 Decl *MostRecent = First->getMostRecentDecl();
2920
2921 // If we only have a single declaration, there is no point in storing
2922 // a redeclaration chain.
2923 if (First == MostRecent)
2924 continue;
2925
2926 unsigned Offset = LocalRedeclChains.size();
2927 unsigned Size = 0;
2928 LocalRedeclChains.push_back(0); // Placeholder for the size.
2929
2930 // Collect the set of local redeclarations of this declaration.
2931 for (Decl *Prev = MostRecent; Prev != First;
2932 Prev = Prev->getPreviousDecl()) {
2933 if (!Prev->isFromASTFile()) {
2934 AddDeclRef(Prev, LocalRedeclChains);
2935 ++Size;
2936 }
2937 }
2938 LocalRedeclChains[Offset] = Size;
2939
2940 // Reverse the set of local redeclarations, so that we store them in
2941 // order (since we found them in reverse order).
2942 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2943
2944 // Add the mapping from the first ID to the set of local declarations.
2945 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2946 LocalRedeclsMap.push_back(Info);
2947
2948 assert(N == Redeclarations.size() &&
2949 "Deserialized a declaration we shouldn't have");
2950 }
2951
2952 if (LocalRedeclChains.empty())
2953 return;
2954
2955 // Sort the local redeclarations map by the first declaration ID,
2956 // since the reader will be performing binary searches on this information.
2957 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2958
2959 // Emit the local redeclarations map.
2960 using namespace llvm;
2961 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2962 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2965 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
2966
2967 RecordData Record;
2968 Record.push_back(LOCAL_REDECLARATIONS_MAP);
2969 Record.push_back(LocalRedeclsMap.size());
2970 Stream.EmitRecordWithBlob(AbbrevID, Record,
2971 reinterpret_cast<char*>(LocalRedeclsMap.data()),
2972 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
2973
2974 // Emit the redeclaration chains.
2975 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
2976}
2977
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002978void ASTWriter::WriteMergedDecls() {
2979 if (!Chain || Chain->MergedDecls.empty())
2980 return;
2981
2982 RecordData Record;
2983 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
2984 IEnd = Chain->MergedDecls.end();
2985 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00002986 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002987 : getDeclID(I->first);
2988 assert(CanonID && "Merged declaration not known?");
2989
2990 Record.push_back(CanonID);
2991 Record.push_back(I->second.size());
2992 Record.append(I->second.begin(), I->second.end());
2993 }
2994 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
2995}
2996
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002997//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002998// General Serialization Routines
2999//===----------------------------------------------------------------------===//
3000
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003001/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003002void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003003 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00003004 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
3005 const Attr * A = *i;
3006 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003007 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003008
Sean Huntcf807c42010-08-18 23:23:40 +00003009#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003010
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003011 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003012}
3013
Chris Lattner5f9e2722011-07-23 10:55:15 +00003014void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003015 Record.push_back(Str.size());
3016 Record.insert(Record.end(), Str.begin(), Str.end());
3017}
3018
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003019void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3020 RecordDataImpl &Record) {
3021 Record.push_back(Version.getMajor());
3022 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3023 Record.push_back(*Minor + 1);
3024 else
3025 Record.push_back(0);
3026 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3027 Record.push_back(*Subminor + 1);
3028 else
3029 Record.push_back(0);
3030}
3031
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003032/// \brief Note that the identifier II occurs at the given offset
3033/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003034void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003035 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003036 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003037 // up earlier in the chain and thus don't need an offset.
3038 if (ID >= FirstIdentID)
3039 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003040}
3041
Douglas Gregor83941df2009-04-25 17:48:32 +00003042/// \brief Note that the selector Sel occurs at the given offset
3043/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003044void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003045 unsigned ID = SelectorIDs[Sel];
3046 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003047 // Don't record offsets for selectors that are also available in a different
3048 // file.
3049 if (ID < FirstSelectorID)
3050 return;
3051 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003052}
3053
Sebastian Redla4232eb2010-08-18 23:56:21 +00003054ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003055 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
3056 WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003057 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003058 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003059 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003060 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3061 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003062 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003063 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003064 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003065 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003066 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003067 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003068 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3069 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3070 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003071 DeclTypedefAbbrev(0),
3072 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3073 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003074{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003075}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003076
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003077ASTWriter::~ASTWriter() {
3078 for (FileDeclIDsTy::iterator
3079 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3080 delete I->second;
3081}
3082
Sebastian Redla4232eb2010-08-18 23:56:21 +00003083void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003084 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003085 Module *WritingModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003086 WritingAST = true;
3087
Douglas Gregor2cf26342009-04-09 22:27:44 +00003088 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003089 Stream.Emit((unsigned)'C', 8);
3090 Stream.Emit((unsigned)'P', 8);
3091 Stream.Emit((unsigned)'C', 8);
3092 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003093
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003094 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003095
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003096 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003097 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003098 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003099 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003100 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003101 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003102 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003103
3104 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003105}
3106
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003107template<typename Vector>
3108static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3109 ASTWriter::RecordData &Record) {
3110 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3111 I != E; ++I) {
3112 Writer.AddDeclRef(*I, Record);
3113 }
3114}
3115
Sebastian Redla4232eb2010-08-18 23:56:21 +00003116void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003117 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003118 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003119 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003120 using namespace llvm;
3121
Douglas Gregorecc2c092011-12-01 22:20:10 +00003122 // Make sure that the AST reader knows to finalize itself.
3123 if (Chain)
3124 Chain->finalizeForWriting();
3125
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003126 ASTContext &Context = SemaRef.Context;
3127 Preprocessor &PP = SemaRef.PP;
3128
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003129 // Set up predefined declaration IDs.
3130 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003131 if (Context.ObjCIdDecl)
3132 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003133 if (Context.ObjCSelDecl)
3134 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003135 if (Context.ObjCClassDecl)
3136 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003137 if (Context.ObjCProtocolClassDecl)
3138 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003139 if (Context.Int128Decl)
3140 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3141 if (Context.UInt128Decl)
3142 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003143 if (Context.ObjCInstanceTypeDecl)
3144 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003145
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003146 if (!Chain) {
3147 // Make sure that we emit IdentifierInfos (and any attached
3148 // declarations) for builtins. We don't need to do this when we're
3149 // emitting chained PCH files, because all of the builtins will be
3150 // in the original PCH file.
3151 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003152 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003153 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003154 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
3155 Context.getLangOptions().NoBuiltin);
3156 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3157 getIdentifierRef(&Table.get(BuiltinNames[I]));
3158 }
3159
Douglas Gregoreee242f2011-10-27 09:33:13 +00003160 // If there are any out-of-date identifiers, bring them up to date.
3161 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3162 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3163 IDEnd = PP.getIdentifierTable().end();
3164 ID != IDEnd; ++ID)
3165 if (ID->second->isOutOfDate())
3166 ExtSource->updateOutOfDateIdentifier(*ID->second);
3167 }
3168
Chris Lattner63d65f82009-09-08 18:19:27 +00003169 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003170 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003171 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003172 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003173 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003174
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003175 // Build a record containing all of the file scoped decls in this file.
3176 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003177 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3178 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003179
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003180 // Build a record containing all of the delegating constructors we still need
3181 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003182 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003183 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003184
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003185 // Write the set of weak, undeclared identifiers. We always write the
3186 // entire table, since later PCH files in a PCH chain are only interested in
3187 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003188 RecordData WeakUndeclaredIdentifiers;
3189 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003190 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003191 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3192 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3193 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3194 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3195 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3196 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3197 }
3198 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003199
Douglas Gregor14c22f22009-04-22 22:18:58 +00003200 // Build a record containing all of the locally-scoped external
3201 // declarations in this header file. Generally, this record will be
3202 // empty.
3203 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003204 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003205 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003206 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003207 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3208 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003209 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003210 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003211 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3212 }
3213
Douglas Gregorb81c1702009-04-27 20:06:05 +00003214 // Build a record containing all of the ext_vector declarations.
3215 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003216 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003217
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003218 // Build a record containing all of the VTable uses information.
3219 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003220 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003221 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3222 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3223 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3224 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3225 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003226 }
3227
3228 // Build a record containing all of dynamic classes declarations.
3229 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003230 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003231
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003232 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003233 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003234 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003235 I = SemaRef.PendingInstantiations.begin(),
3236 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3237 AddDeclRef(I->first, PendingInstantiations);
3238 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003239 }
3240 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3241 "There are local ones at end of translation unit!");
3242
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003243 // Build a record containing some declaration references.
3244 RecordData SemaDeclRefs;
3245 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3246 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3247 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3248 }
3249
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003250 RecordData CUDASpecialDeclRefs;
3251 if (Context.getcudaConfigureCallDecl()) {
3252 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3253 }
3254
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003255 // Build a record containing all of the known namespaces.
3256 RecordData KnownNamespaces;
3257 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3258 I = SemaRef.KnownNamespaces.begin(),
3259 IEnd = SemaRef.KnownNamespaces.end();
3260 I != IEnd; ++I) {
3261 if (!I->second)
3262 AddDeclRef(I->first, KnownNamespaces);
3263 }
3264
Sebastian Redl3397c552010-08-18 23:56:27 +00003265 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003266 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003267 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003268 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003269 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00003270 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003271 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003272
3273 // Create a lexical update block containing all of the declarations in the
3274 // translation unit that do not come from other AST files.
3275 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3276 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3277 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3278 E = TU->noload_decls_end();
3279 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003280 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003281 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003282 }
3283
3284 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3285 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3286 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3287 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3288 Record.clear();
3289 Record.push_back(TU_UPDATE_LEXICAL);
3290 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3291 data(NewGlobalDecls));
3292
3293 // And a visible updates block for the translation unit.
3294 Abv = new llvm::BitCodeAbbrev();
3295 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3296 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3297 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3298 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3299 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3300 WriteDeclContextVisibleUpdate(TU);
3301
3302 // If the translation unit has an anonymous namespace, and we don't already
3303 // have an update block for it, write it as an update block.
3304 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3305 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3306 if (Record.empty()) {
3307 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003308 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003309 }
3310 }
3311
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003312 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003313 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003314
Douglas Gregora119da02011-08-02 16:26:37 +00003315 // Form the record of special types.
3316 RecordData SpecialTypes;
3317 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003318 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003319 AddTypeRef(Context.getFILEType(), SpecialTypes);
3320 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3321 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3322 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3323 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003324 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003325 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003326
Douglas Gregor366809a2009-04-26 03:49:13 +00003327 // Keep writing types and declarations until all types and
3328 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003329 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003330 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003331 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3332 E = DeclsToRewrite.end();
3333 I != E; ++I)
3334 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003335 while (!DeclTypesToEmit.empty()) {
3336 DeclOrType DOT = DeclTypesToEmit.front();
3337 DeclTypesToEmit.pop();
3338 if (DOT.isType())
3339 WriteType(DOT.getType());
3340 else
3341 WriteDecl(Context, DOT.getDecl());
3342 }
3343 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003344
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003345 WriteFileDeclIDsMap();
3346 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3347
3348 if (Chain) {
3349 // Write the mapping information describing our module dependencies and how
3350 // each of those modules were mapped into our own offset/ID space, so that
3351 // the reader can build the appropriate mapping to its own offset/ID space.
3352 // The map consists solely of a blob with the following format:
3353 // *(module-name-len:i16 module-name:len*i8
3354 // source-location-offset:i32
3355 // identifier-id:i32
3356 // preprocessed-entity-id:i32
3357 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003358 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003359 // selector-id:i32
3360 // declaration-id:i32
3361 // c++-base-specifiers-id:i32
3362 // type-id:i32)
3363 //
3364 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3365 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3367 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
3368 llvm::SmallString<2048> Buffer;
3369 {
3370 llvm::raw_svector_ostream Out(Buffer);
3371 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003372 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003373 M != MEnd; ++M) {
3374 StringRef FileName = (*M)->FileName;
3375 io::Emit16(Out, FileName.size());
3376 Out.write(FileName.data(), FileName.size());
3377 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3378 io::Emit32(Out, (*M)->BaseIdentifierID);
3379 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003380 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003381 io::Emit32(Out, (*M)->BaseSelectorID);
3382 io::Emit32(Out, (*M)->BaseDeclID);
3383 io::Emit32(Out, (*M)->BaseTypeIndex);
3384 }
3385 }
3386 Record.clear();
3387 Record.push_back(MODULE_OFFSET_MAP);
3388 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3389 Buffer.data(), Buffer.size());
3390 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003391 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003392 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003393 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003394 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003395 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003396 WriteFPPragmaOptions(SemaRef.getFPOptions());
3397 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003398
Sebastian Redl1476ed42010-07-16 16:36:56 +00003399 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003400 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003401
Anders Carlssonc8505782011-03-06 18:41:18 +00003402 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003403
Douglas Gregore209e502011-12-06 01:10:29 +00003404 // If we're emitting a module, write out the submodule information.
3405 if (WritingModule)
3406 WriteSubmodules(WritingModule);
3407
Douglas Gregora119da02011-08-02 16:26:37 +00003408 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3409
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003410 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003411 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003412 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003413
3414 // Write the record containing tentative definitions.
3415 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003416 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003417
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003418 // Write the record containing unused file scoped decls.
3419 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003420 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003421
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003422 // Write the record containing weak undeclared identifiers.
3423 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003424 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003425 WeakUndeclaredIdentifiers);
3426
Douglas Gregor14c22f22009-04-22 22:18:58 +00003427 // Write the record containing locally-scoped external definitions.
3428 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003429 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003430 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003431
3432 // Write the record containing ext_vector type names.
3433 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003434 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003435
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003436 // Write the record containing VTable uses information.
3437 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003438 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003439
3440 // Write the record containing dynamic classes declarations.
3441 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003442 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003443
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003444 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003445 if (!PendingInstantiations.empty())
3446 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003447
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003448 // Write the record containing declaration references of Sema.
3449 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003450 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003451
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003452 // Write the record containing CUDA-specific declaration references.
3453 if (!CUDASpecialDeclRefs.empty())
3454 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003455
3456 // Write the delegating constructors.
3457 if (!DelegatingCtorDecls.empty())
3458 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003459
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003460 // Write the known namespaces.
3461 if (!KnownNamespaces.empty())
3462 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3463
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003464 // Write the visible updates to DeclContexts.
3465 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3466 I = UpdatedDeclContexts.begin(),
3467 E = UpdatedDeclContexts.end();
3468 I != E; ++I)
3469 WriteDeclContextVisibleUpdate(*I);
3470
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003471 if (!WritingModule) {
3472 // Write the submodules that were imported, if any.
3473 RecordData ImportedModules;
3474 for (ASTContext::import_iterator I = Context.local_import_begin(),
3475 IEnd = Context.local_import_end();
3476 I != IEnd; ++I) {
3477 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3478 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3479 }
3480 if (!ImportedModules.empty()) {
3481 // Sort module IDs.
3482 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3483
3484 // Unique module IDs.
3485 ImportedModules.erase(std::unique(ImportedModules.begin(),
3486 ImportedModules.end()),
3487 ImportedModules.end());
3488
3489 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3490 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003491 }
3492
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003493 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003494 WriteDeclReplacementsBlock();
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003495 WriteChainedObjCCategories();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003496 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003497 WriteRedeclarations();
Douglas Gregora1be2782011-12-17 23:38:30 +00003498
Douglas Gregor3e1af842009-04-17 22:13:46 +00003499 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003500 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003501 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003502 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003503 Record.push_back(NumLexicalDeclContexts);
3504 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003505 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003506 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003507}
3508
Douglas Gregor61c5e342011-09-17 00:05:03 +00003509/// \brief Go through the declaration update blocks and resolve declaration
3510/// pointers into declaration IDs.
3511void ASTWriter::ResolveDeclUpdatesBlocks() {
3512 for (DeclUpdateMap::iterator
3513 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3514 const Decl *D = I->first;
3515 UpdateRecord &URec = I->second;
3516
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003517 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003518 continue; // The decl will be written completely
3519
3520 unsigned Idx = 0, N = URec.size();
3521 while (Idx < N) {
3522 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003523 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3524 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3525 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3526 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3527 ++Idx;
3528 break;
3529
3530 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3531 ++Idx;
3532 break;
3533 }
3534 }
3535 }
3536}
3537
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003538void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003539 if (DeclUpdates.empty())
3540 return;
3541
3542 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003543 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003544 for (DeclUpdateMap::iterator
3545 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3546 const Decl *D = I->first;
3547 UpdateRecord &URec = I->second;
3548
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003549 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003550 continue; // The decl will be written completely,no need to store updates.
3551
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003552 uint64_t Offset = Stream.GetCurrentBitNo();
3553 Stream.EmitRecord(DECL_UPDATES, URec);
3554
3555 OffsetsRecord.push_back(GetDeclRef(D));
3556 OffsetsRecord.push_back(Offset);
3557 }
3558 Stream.ExitBlock();
3559 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3560}
3561
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003562void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003563 if (ReplacedDecls.empty())
3564 return;
3565
3566 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003567 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003568 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003569 Record.push_back(I->ID);
3570 Record.push_back(I->Offset);
3571 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003572 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003573 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003574}
3575
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003576void ASTWriter::WriteChainedObjCCategories() {
3577 if (LocalChainedObjCCategories.empty())
3578 return;
3579
3580 RecordData Record;
3581 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3582 I = LocalChainedObjCCategories.begin(),
3583 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3584 ChainedObjCCategoriesData &Data = *I;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003585 if (isRewritten(Data.Interface))
3586 continue;
3587
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003588 assert(Data.Interface->getCategoryList());
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003589 serialization::DeclID
3590 HeadCatID = getDeclID(Data.Interface->getCategoryList());
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003591
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003592 Record.push_back(getDeclID(Data.Interface));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003593 Record.push_back(HeadCatID);
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003594 Record.push_back(getDeclID(Data.TailCategory));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003595 }
3596 Stream.EmitRecord(OBJC_CHAINED_CATEGORIES, Record);
3597}
3598
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003599void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003600 Record.push_back(Loc.getRawEncoding());
3601}
3602
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003603void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003604 AddSourceLocation(Range.getBegin(), Record);
3605 AddSourceLocation(Range.getEnd(), Record);
3606}
3607
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003608void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003609 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003610 const uint64_t *Words = Value.getRawData();
3611 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003612}
3613
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003614void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003615 Record.push_back(Value.isUnsigned());
3616 AddAPInt(Value, Record);
3617}
3618
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003619void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003620 AddAPInt(Value.bitcastToAPInt(), Record);
3621}
3622
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003623void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003624 Record.push_back(getIdentifierRef(II));
3625}
3626
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003627IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003628 if (II == 0)
3629 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003630
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003631 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003632 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003633 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003634 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003635}
3636
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003637void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003638 Record.push_back(getSelectorRef(SelRef));
3639}
3640
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003641SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003642 if (Sel.getAsOpaquePtr() == 0) {
3643 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003644 }
3645
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003646 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003647 if (SID == 0 && Chain) {
3648 // This might trigger a ReadSelector callback, which will set the ID for
3649 // this selector.
3650 Chain->LoadSelector(Sel);
3651 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003652 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003653 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003654 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003655 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003656}
3657
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003658void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003659 AddDeclRef(Temp->getDestructor(), Record);
3660}
3661
Douglas Gregor7c789c12010-10-29 22:39:52 +00003662void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3663 CXXBaseSpecifier const *BasesEnd,
3664 RecordDataImpl &Record) {
3665 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3666 CXXBaseSpecifiersToWrite.push_back(
3667 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3668 Bases, BasesEnd));
3669 Record.push_back(NextCXXBaseSpecifiersID++);
3670}
3671
Sebastian Redla4232eb2010-08-18 23:56:21 +00003672void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003673 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003674 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003675 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003676 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003677 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003678 break;
3679 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003680 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003681 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003682 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003683 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003684 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003685 break;
3686 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003687 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003688 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003689 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003690 break;
John McCall833ca992009-10-29 08:12:44 +00003691 case TemplateArgument::Null:
3692 case TemplateArgument::Integral:
3693 case TemplateArgument::Declaration:
3694 case TemplateArgument::Pack:
3695 break;
3696 }
3697}
3698
Sebastian Redla4232eb2010-08-18 23:56:21 +00003699void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003700 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003701 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003702
3703 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3704 bool InfoHasSameExpr
3705 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3706 Record.push_back(InfoHasSameExpr);
3707 if (InfoHasSameExpr)
3708 return; // Avoid storing the same expr twice.
3709 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003710 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3711 Record);
3712}
3713
Douglas Gregordc355712011-02-25 00:36:19 +00003714void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3715 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003716 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003717 AddTypeRef(QualType(), Record);
3718 return;
3719 }
3720
Douglas Gregordc355712011-02-25 00:36:19 +00003721 AddTypeLoc(TInfo->getTypeLoc(), Record);
3722}
3723
3724void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3725 AddTypeRef(TL.getType(), Record);
3726
John McCalla1ee0c52009-10-16 21:56:05 +00003727 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003728 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003729 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003730}
3731
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003732void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003733 Record.push_back(GetOrCreateTypeID(T));
3734}
3735
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003736TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3737 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003738 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3739}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003740
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003741TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003742 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003743 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003744}
3745
3746TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3747 if (T.isNull())
3748 return TypeIdx();
3749 assert(!T.getLocalFastQualifiers());
3750
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003751 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003752 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003753 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003754 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003755 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003756 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003757 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003758 return Idx;
3759}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003760
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003761TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003762 if (T.isNull())
3763 return TypeIdx();
3764 assert(!T.getLocalFastQualifiers());
3765
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003766 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3767 assert(I != TypeIdxs.end() && "Type not emitted!");
3768 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003769}
3770
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003771void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003772 Record.push_back(GetDeclRef(D));
3773}
3774
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003775DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003776 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3777
Douglas Gregor2cf26342009-04-09 22:27:44 +00003778 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003779 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003780 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003781
3782 // If D comes from an AST file, its declaration ID is already known and
3783 // fixed.
3784 if (D->isFromASTFile())
3785 return D->getGlobalID();
3786
Douglas Gregor97475832010-10-05 18:37:06 +00003787 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003788 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003789 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003790 // We haven't seen this declaration before. Give it a new ID and
3791 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003792 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003793 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003794 }
3795
Sebastian Redl681d7232010-07-27 00:17:23 +00003796 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003797}
3798
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003799DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003800 if (D == 0)
3801 return 0;
3802
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003803 // If D comes from an AST file, its declaration ID is already known and
3804 // fixed.
3805 if (D->isFromASTFile())
3806 return D->getGlobalID();
3807
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003808 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3809 return DeclIDs[D];
3810}
3811
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003812static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3813 std::pair<unsigned, serialization::DeclID> R) {
3814 return L.first < R.first;
3815}
3816
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003817void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003818 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003819 assert(D);
3820
3821 SourceLocation Loc = D->getLocation();
3822 if (Loc.isInvalid())
3823 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003824
3825 // We only keep track of the file-level declarations of each file.
3826 if (!D->getLexicalDeclContext()->isFileContext())
3827 return;
3828
3829 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003830 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003831 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003832 FileID FID;
3833 unsigned Offset;
3834 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003835 if (FID.isInvalid())
3836 return;
3837 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3838 assert(Entry->isFile());
3839
3840 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3841 if (!Info)
3842 Info = new DeclIDInFileInfo();
3843
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003844 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003845 LocDeclIDsTy &Decls = Info->DeclIDs;
3846
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003847 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003848 Decls.push_back(LocDecl);
3849 return;
3850 }
3851
3852 LocDeclIDsTy::iterator
3853 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3854
3855 Decls.insert(I, LocDecl);
3856}
3857
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003858void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003859 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003860 Record.push_back(Name.getNameKind());
3861 switch (Name.getNameKind()) {
3862 case DeclarationName::Identifier:
3863 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3864 break;
3865
3866 case DeclarationName::ObjCZeroArgSelector:
3867 case DeclarationName::ObjCOneArgSelector:
3868 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003869 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003870 break;
3871
3872 case DeclarationName::CXXConstructorName:
3873 case DeclarationName::CXXDestructorName:
3874 case DeclarationName::CXXConversionFunctionName:
3875 AddTypeRef(Name.getCXXNameType(), Record);
3876 break;
3877
3878 case DeclarationName::CXXOperatorName:
3879 Record.push_back(Name.getCXXOverloadedOperator());
3880 break;
3881
Sean Hunt3e518bd2009-11-29 07:34:05 +00003882 case DeclarationName::CXXLiteralOperatorName:
3883 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3884 break;
3885
Douglas Gregor2cf26342009-04-09 22:27:44 +00003886 case DeclarationName::CXXUsingDirective:
3887 // No extra data to emit
3888 break;
3889 }
3890}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003891
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003892void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003893 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003894 switch (Name.getNameKind()) {
3895 case DeclarationName::CXXConstructorName:
3896 case DeclarationName::CXXDestructorName:
3897 case DeclarationName::CXXConversionFunctionName:
3898 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3899 break;
3900
3901 case DeclarationName::CXXOperatorName:
3902 AddSourceLocation(
3903 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3904 Record);
3905 AddSourceLocation(
3906 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3907 Record);
3908 break;
3909
3910 case DeclarationName::CXXLiteralOperatorName:
3911 AddSourceLocation(
3912 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3913 Record);
3914 break;
3915
3916 case DeclarationName::Identifier:
3917 case DeclarationName::ObjCZeroArgSelector:
3918 case DeclarationName::ObjCOneArgSelector:
3919 case DeclarationName::ObjCMultiArgSelector:
3920 case DeclarationName::CXXUsingDirective:
3921 break;
3922 }
3923}
3924
3925void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003926 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003927 AddDeclarationName(NameInfo.getName(), Record);
3928 AddSourceLocation(NameInfo.getLoc(), Record);
3929 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3930}
3931
3932void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003933 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003934 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003935 Record.push_back(Info.NumTemplParamLists);
3936 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3937 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3938}
3939
Sebastian Redla4232eb2010-08-18 23:56:21 +00003940void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003941 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003942 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003943 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003944 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003945
3946 // Push each of the NNS's onto a stack for serialization in reverse order.
3947 while (NNS) {
3948 NestedNames.push_back(NNS);
3949 NNS = NNS->getPrefix();
3950 }
3951
3952 Record.push_back(NestedNames.size());
3953 while(!NestedNames.empty()) {
3954 NNS = NestedNames.pop_back_val();
3955 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3956 Record.push_back(Kind);
3957 switch (Kind) {
3958 case NestedNameSpecifier::Identifier:
3959 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3960 break;
3961
3962 case NestedNameSpecifier::Namespace:
3963 AddDeclRef(NNS->getAsNamespace(), Record);
3964 break;
3965
Douglas Gregor14aba762011-02-24 02:36:08 +00003966 case NestedNameSpecifier::NamespaceAlias:
3967 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3968 break;
3969
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003970 case NestedNameSpecifier::TypeSpec:
3971 case NestedNameSpecifier::TypeSpecWithTemplate:
3972 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3973 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3974 break;
3975
3976 case NestedNameSpecifier::Global:
3977 // Don't need to write an associated value.
3978 break;
3979 }
3980 }
3981}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003982
Douglas Gregordc355712011-02-25 00:36:19 +00003983void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3984 RecordDataImpl &Record) {
3985 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003986 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003987 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00003988
3989 // Push each of the nested-name-specifiers's onto a stack for
3990 // serialization in reverse order.
3991 while (NNS) {
3992 NestedNames.push_back(NNS);
3993 NNS = NNS.getPrefix();
3994 }
3995
3996 Record.push_back(NestedNames.size());
3997 while(!NestedNames.empty()) {
3998 NNS = NestedNames.pop_back_val();
3999 NestedNameSpecifier::SpecifierKind Kind
4000 = NNS.getNestedNameSpecifier()->getKind();
4001 Record.push_back(Kind);
4002 switch (Kind) {
4003 case NestedNameSpecifier::Identifier:
4004 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4005 AddSourceRange(NNS.getLocalSourceRange(), Record);
4006 break;
4007
4008 case NestedNameSpecifier::Namespace:
4009 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4010 AddSourceRange(NNS.getLocalSourceRange(), Record);
4011 break;
4012
4013 case NestedNameSpecifier::NamespaceAlias:
4014 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4015 AddSourceRange(NNS.getLocalSourceRange(), Record);
4016 break;
4017
4018 case NestedNameSpecifier::TypeSpec:
4019 case NestedNameSpecifier::TypeSpecWithTemplate:
4020 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4021 AddTypeLoc(NNS.getTypeLoc(), Record);
4022 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4023 break;
4024
4025 case NestedNameSpecifier::Global:
4026 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4027 break;
4028 }
4029 }
4030}
4031
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004032void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004033 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004034 Record.push_back(Kind);
4035 switch (Kind) {
4036 case TemplateName::Template:
4037 AddDeclRef(Name.getAsTemplateDecl(), Record);
4038 break;
4039
4040 case TemplateName::OverloadedTemplate: {
4041 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4042 Record.push_back(OvT->size());
4043 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4044 I != E; ++I)
4045 AddDeclRef(*I, Record);
4046 break;
4047 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004048
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004049 case TemplateName::QualifiedTemplate: {
4050 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4051 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4052 Record.push_back(QualT->hasTemplateKeyword());
4053 AddDeclRef(QualT->getTemplateDecl(), Record);
4054 break;
4055 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004056
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004057 case TemplateName::DependentTemplate: {
4058 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4059 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4060 Record.push_back(DepT->isIdentifier());
4061 if (DepT->isIdentifier())
4062 AddIdentifierRef(DepT->getIdentifier(), Record);
4063 else
4064 Record.push_back(DepT->getOperator());
4065 break;
4066 }
John McCall14606042011-06-30 08:33:18 +00004067
4068 case TemplateName::SubstTemplateTemplateParm: {
4069 SubstTemplateTemplateParmStorage *subst
4070 = Name.getAsSubstTemplateTemplateParm();
4071 AddDeclRef(subst->getParameter(), Record);
4072 AddTemplateName(subst->getReplacement(), Record);
4073 break;
4074 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004075
4076 case TemplateName::SubstTemplateTemplateParmPack: {
4077 SubstTemplateTemplateParmPackStorage *SubstPack
4078 = Name.getAsSubstTemplateTemplateParmPack();
4079 AddDeclRef(SubstPack->getParameterPack(), Record);
4080 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4081 break;
4082 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004083 }
4084}
4085
Michael J. Spencer20249a12010-10-21 03:16:25 +00004086void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004087 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004088 Record.push_back(Arg.getKind());
4089 switch (Arg.getKind()) {
4090 case TemplateArgument::Null:
4091 break;
4092 case TemplateArgument::Type:
4093 AddTypeRef(Arg.getAsType(), Record);
4094 break;
4095 case TemplateArgument::Declaration:
4096 AddDeclRef(Arg.getAsDecl(), Record);
4097 break;
4098 case TemplateArgument::Integral:
4099 AddAPSInt(*Arg.getAsIntegral(), Record);
4100 AddTypeRef(Arg.getIntegralType(), Record);
4101 break;
4102 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004103 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4104 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004105 case TemplateArgument::TemplateExpansion:
4106 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004107 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4108 Record.push_back(*NumExpansions + 1);
4109 else
4110 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004111 break;
4112 case TemplateArgument::Expression:
4113 AddStmt(Arg.getAsExpr());
4114 break;
4115 case TemplateArgument::Pack:
4116 Record.push_back(Arg.pack_size());
4117 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4118 I != E; ++I)
4119 AddTemplateArgument(*I, Record);
4120 break;
4121 }
4122}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004123
4124void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004125ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004126 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004127 assert(TemplateParams && "No TemplateParams!");
4128 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4129 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4130 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4131 Record.push_back(TemplateParams->size());
4132 for (TemplateParameterList::const_iterator
4133 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4134 P != PEnd; ++P)
4135 AddDeclRef(*P, Record);
4136}
4137
4138/// \brief Emit a template argument list.
4139void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004140ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004141 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004142 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004143 Record.push_back(TemplateArgs->size());
4144 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004145 AddTemplateArgument(TemplateArgs->get(i), Record);
4146}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004147
4148
4149void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004150ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004151 Record.push_back(Set.size());
4152 for (UnresolvedSetImpl::const_iterator
4153 I = Set.begin(), E = Set.end(); I != E; ++I) {
4154 AddDeclRef(I.getDecl(), Record);
4155 Record.push_back(I.getAccess());
4156 }
4157}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004158
Sebastian Redla4232eb2010-08-18 23:56:21 +00004159void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004160 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004161 Record.push_back(Base.isVirtual());
4162 Record.push_back(Base.isBaseOfClass());
4163 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004164 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004165 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004166 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004167 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4168 : SourceLocation(),
4169 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004170}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004171
Douglas Gregor7c789c12010-10-29 22:39:52 +00004172void ASTWriter::FlushCXXBaseSpecifiers() {
4173 RecordData Record;
4174 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4175 Record.clear();
4176
4177 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004178 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004179 if (Index == CXXBaseSpecifiersOffsets.size())
4180 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4181 else {
4182 if (Index > CXXBaseSpecifiersOffsets.size())
4183 CXXBaseSpecifiersOffsets.resize(Index + 1);
4184 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4185 }
4186
4187 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4188 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4189 Record.push_back(BEnd - B);
4190 for (; B != BEnd; ++B)
4191 AddCXXBaseSpecifier(*B, Record);
4192 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004193
4194 // Flush any expressions that were written as part of the base specifiers.
4195 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004196 }
4197
4198 CXXBaseSpecifiersToWrite.clear();
4199}
4200
Sean Huntcbb67482011-01-08 20:30:50 +00004201void ASTWriter::AddCXXCtorInitializers(
4202 const CXXCtorInitializer * const *CtorInitializers,
4203 unsigned NumCtorInitializers,
4204 RecordDataImpl &Record) {
4205 Record.push_back(NumCtorInitializers);
4206 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4207 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004208
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004209 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004210 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004211 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004212 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004213 } else if (Init->isDelegatingInitializer()) {
4214 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004215 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004216 } else if (Init->isMemberInitializer()){
4217 Record.push_back(CTOR_INITIALIZER_MEMBER);
4218 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004219 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004220 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4221 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004222 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004223
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004224 AddSourceLocation(Init->getMemberLocation(), Record);
4225 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004226 AddSourceLocation(Init->getLParenLoc(), Record);
4227 AddSourceLocation(Init->getRParenLoc(), Record);
4228 Record.push_back(Init->isWritten());
4229 if (Init->isWritten()) {
4230 Record.push_back(Init->getSourceOrder());
4231 } else {
4232 Record.push_back(Init->getNumArrayIndices());
4233 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4234 AddDeclRef(Init->getArrayIndex(i), Record);
4235 }
4236 }
4237}
4238
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004239void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4240 assert(D->DefinitionData);
4241 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
4242 Record.push_back(Data.UserDeclaredConstructor);
4243 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004244 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004245 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004246 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004247 Record.push_back(Data.UserDeclaredDestructor);
4248 Record.push_back(Data.Aggregate);
4249 Record.push_back(Data.PlainOldData);
4250 Record.push_back(Data.Empty);
4251 Record.push_back(Data.Polymorphic);
4252 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004253 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004254 Record.push_back(Data.HasNoNonEmptyBases);
4255 Record.push_back(Data.HasPrivateFields);
4256 Record.push_back(Data.HasProtectedFields);
4257 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004258 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00004259 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004260 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004261 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004262 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004263 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004264 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004265 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004266 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004267 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004268 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004269 Record.push_back(Data.DeclaredDefaultConstructor);
4270 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004271 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004272 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004273 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004274 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004275 Record.push_back(Data.FailedImplicitMoveConstructor);
4276 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004277
4278 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004279 if (Data.NumBases > 0)
4280 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4281 Record);
4282
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004283 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4284 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004285 if (Data.NumVBases > 0)
4286 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4287 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004288
4289 AddUnresolvedSet(Data.Conversions, Record);
4290 AddUnresolvedSet(Data.VisibleConversions, Record);
4291 // Data.Definition is the owning decl, no need to write it.
4292 AddDeclRef(Data.FirstFriend, Record);
4293}
4294
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004295void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004296 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004297 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004298 assert(FirstDeclID == NextDeclID &&
4299 FirstTypeID == NextTypeID &&
4300 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004301 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004302 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004303 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004304
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004305 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004306
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004307 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4308 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4309 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004310 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004311 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004312 NextDeclID = FirstDeclID;
4313 NextTypeID = FirstTypeID;
4314 NextIdentID = FirstIdentID;
4315 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004316 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004317}
4318
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004319void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004320 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004321 if (II->hasMacroDefinition())
4322 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004323}
4324
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004325void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004326 // Always take the highest-numbered type index. This copes with an interesting
4327 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004328 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004329 // keep the higher-numbered entry so that we can properly write it out to
4330 // the AST file.
4331 TypeIdx &StoredIdx = TypeIdxs[T];
4332 if (Idx.getIndex() >= StoredIdx.getIndex())
4333 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004334}
4335
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004336void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004337 SelectorIDs[S] = ID;
4338}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004339
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004340void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004341 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004342 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004343 MacroDefinitions[MD] = ID;
4344}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004345
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004346void ASTWriter::MacroVisible(IdentifierInfo *II) {
4347 DeserializedMacroNames.push_back(II);
4348}
4349
Douglas Gregora015cab2011-12-02 17:30:13 +00004350void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4351 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4352 SubmoduleIDs[Mod] = ID;
4353}
4354
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004355void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004356 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004357 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004358 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4359 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004360 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004361 // A forward reference was mutated into a definition. Rewrite it.
4362 // FIXME: This happens during template instantiation, should we
4363 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004364 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004365 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004366 }
4367}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004368void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004369 assert(!WritingAST && "Already writing the AST!");
4370
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004371 // TU and namespaces are handled elsewhere.
4372 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4373 return;
4374
Douglas Gregor919814d2011-09-09 23:01:35 +00004375 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004376 return; // Not a source decl added to a DeclContext from PCH.
4377
4378 AddUpdatedDeclContext(DC);
4379}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004380
4381void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004382 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004383 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004384 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004385 return; // Not a source member added to a class from PCH.
4386 if (!isa<CXXMethodDecl>(D))
4387 return; // We are interested in lazily declared implicit methods.
4388
4389 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004390 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004391 UpdateRecord &Record = DeclUpdates[RD];
4392 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004393 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004394}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004395
4396void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4397 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004398 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004399 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004400 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004401 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004402 return; // Not a source specialization added to a template from PCH.
4403
4404 UpdateRecord &Record = DeclUpdates[TD];
4405 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004406 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004407}
Douglas Gregor89d99802010-11-30 06:16:57 +00004408
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004409void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4410 const FunctionDecl *D) {
4411 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004412 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004413 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004414 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004415 return; // Not a source specialization added to a template from PCH.
4416
4417 UpdateRecord &Record = DeclUpdates[TD];
4418 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004419 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004420}
4421
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004422void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004423 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004424 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004425 return; // Declaration not imported from PCH.
4426
4427 // Implicit decl from a PCH was defined.
4428 // FIXME: Should implicit definition be a separate FunctionDecl?
4429 RewriteDecl(D);
4430}
4431
Sebastian Redlf79a7192011-04-29 08:19:30 +00004432void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004433 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004434 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004435 return;
4436
4437 // Since the actual instantiation is delayed, this really means that we need
4438 // to update the instantiation location.
4439 UpdateRecord &Record = DeclUpdates[D];
4440 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4441 AddSourceLocation(
4442 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4443}
4444
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004445void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4446 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004447 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004448 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004449 return; // Declaration not imported from PCH.
4450 if (CatD->getNextClassCategory() &&
Douglas Gregor919814d2011-09-09 23:01:35 +00004451 !CatD->getNextClassCategory()->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004452 return; // We already recorded that the tail of a category chain should be
4453 // attached to an interface.
4454
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004455 ChainedObjCCategoriesData Data = { IFD, CatD };
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004456 LocalChainedObjCCategories.push_back(Data);
4457}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004458
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004459
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004460void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4461 const ObjCPropertyDecl *OrigProp,
4462 const ObjCCategoryDecl *ClassExt) {
4463 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4464 if (!D)
4465 return;
4466
4467 assert(!WritingAST && "Already writing the AST!");
4468 if (!D->isFromASTFile())
4469 return; // Declaration not imported from PCH.
4470
4471 RewriteDecl(D);
4472}
4473