blob: 9ed2a6c6d95671f86ccd268b938d62c30da8e5e5 [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 Gregor2cf26342009-04-09 22:27:44 +0000242 Writer.AddDeclRef(T->getDecl(), 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 Gregordeacbdc2010-08-11 12:19:30 +0000372 Writer.AddDeclRef(T->getDecl(), 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 Gregora1266512011-12-19 21:09:25 +0000781 RECORD(LOCAL_REDECLARATIONS);
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);
804
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000805 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000806 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000807 RECORD(SM_SLOC_FILE_ENTRY);
808 RECORD(SM_SLOC_BUFFER_ENTRY);
809 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000810 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000812 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000813 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000814 RECORD(PP_MACRO_OBJECT_LIKE);
815 RECORD(PP_MACRO_FUNCTION_LIKE);
816 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000817
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000818 // Decls and Types block.
819 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000820 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000821 RECORD(TYPE_COMPLEX);
822 RECORD(TYPE_POINTER);
823 RECORD(TYPE_BLOCK_POINTER);
824 RECORD(TYPE_LVALUE_REFERENCE);
825 RECORD(TYPE_RVALUE_REFERENCE);
826 RECORD(TYPE_MEMBER_POINTER);
827 RECORD(TYPE_CONSTANT_ARRAY);
828 RECORD(TYPE_INCOMPLETE_ARRAY);
829 RECORD(TYPE_VARIABLE_ARRAY);
830 RECORD(TYPE_VECTOR);
831 RECORD(TYPE_EXT_VECTOR);
832 RECORD(TYPE_FUNCTION_PROTO);
833 RECORD(TYPE_FUNCTION_NO_PROTO);
834 RECORD(TYPE_TYPEDEF);
835 RECORD(TYPE_TYPEOF_EXPR);
836 RECORD(TYPE_TYPEOF);
837 RECORD(TYPE_RECORD);
838 RECORD(TYPE_ENUM);
839 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000840 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000841 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000842 RECORD(TYPE_DECLTYPE);
843 RECORD(TYPE_ELABORATED);
844 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
845 RECORD(TYPE_UNRESOLVED_USING);
846 RECORD(TYPE_INJECTED_CLASS_NAME);
847 RECORD(TYPE_OBJC_OBJECT);
848 RECORD(TYPE_TEMPLATE_TYPE_PARM);
849 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
850 RECORD(TYPE_DEPENDENT_NAME);
851 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
852 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
853 RECORD(TYPE_PAREN);
854 RECORD(TYPE_PACK_EXPANSION);
855 RECORD(TYPE_ATTRIBUTED);
856 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000857 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000858 RECORD(DECL_TYPEDEF);
859 RECORD(DECL_ENUM);
860 RECORD(DECL_RECORD);
861 RECORD(DECL_ENUM_CONSTANT);
862 RECORD(DECL_FUNCTION);
863 RECORD(DECL_OBJC_METHOD);
864 RECORD(DECL_OBJC_INTERFACE);
865 RECORD(DECL_OBJC_PROTOCOL);
866 RECORD(DECL_OBJC_IVAR);
867 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000868 RECORD(DECL_OBJC_CATEGORY);
869 RECORD(DECL_OBJC_CATEGORY_IMPL);
870 RECORD(DECL_OBJC_IMPLEMENTATION);
871 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
872 RECORD(DECL_OBJC_PROPERTY);
873 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000874 RECORD(DECL_FIELD);
875 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000876 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000877 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000878 RECORD(DECL_FILE_SCOPE_ASM);
879 RECORD(DECL_BLOCK);
880 RECORD(DECL_CONTEXT_LEXICAL);
881 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000882 RECORD(DECL_NAMESPACE);
883 RECORD(DECL_NAMESPACE_ALIAS);
884 RECORD(DECL_USING);
885 RECORD(DECL_USING_SHADOW);
886 RECORD(DECL_USING_DIRECTIVE);
887 RECORD(DECL_UNRESOLVED_USING_VALUE);
888 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
889 RECORD(DECL_LINKAGE_SPEC);
890 RECORD(DECL_CXX_RECORD);
891 RECORD(DECL_CXX_METHOD);
892 RECORD(DECL_CXX_CONSTRUCTOR);
893 RECORD(DECL_CXX_DESTRUCTOR);
894 RECORD(DECL_CXX_CONVERSION);
895 RECORD(DECL_ACCESS_SPEC);
896 RECORD(DECL_FRIEND);
897 RECORD(DECL_FRIEND_TEMPLATE);
898 RECORD(DECL_CLASS_TEMPLATE);
899 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
900 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
901 RECORD(DECL_FUNCTION_TEMPLATE);
902 RECORD(DECL_TEMPLATE_TYPE_PARM);
903 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
904 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
905 RECORD(DECL_STATIC_ASSERT);
906 RECORD(DECL_CXX_BASE_SPECIFIERS);
907 RECORD(DECL_INDIRECTFIELD);
908 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
909
Douglas Gregora72d8c42011-06-03 02:27:19 +0000910 // Statements and Exprs can occur in the Decls and Types block.
911 AddStmtsExprs(Stream, Record);
912
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000913 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000914 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000915 RECORD(PPD_MACRO_DEFINITION);
916 RECORD(PPD_INCLUSION_DIRECTIVE);
917
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000918#undef RECORD
919#undef BLOCK
920 Stream.ExitBlock();
921}
922
Douglas Gregore650c8c2009-07-07 00:12:59 +0000923/// \brief Adjusts the given filename to only write out the portion of the
924/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000925///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000926/// \param Filename the file name to adjust.
927///
928/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
929/// the returned filename will be adjusted by this system root.
930///
931/// \returns either the original filename (if it needs no adjustment) or the
932/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000933static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000934adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000935 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Douglas Gregor832d6202011-07-22 16:35:34 +0000937 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000938 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Douglas Gregore650c8c2009-07-07 00:12:59 +0000940 // Verify that the filename and the system root have the same prefix.
941 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000942 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943 if (Filename[Pos] != isysroot[Pos])
944 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Douglas Gregore650c8c2009-07-07 00:12:59 +0000946 // We hit the end of the filename before we hit the end of the system root.
947 if (!Filename[Pos])
948 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Douglas Gregore650c8c2009-07-07 00:12:59 +0000950 // If the file name has a '/' at the current position, skip over the '/'.
951 // We distinguish sysroot-based includes from absolute includes by the
952 // absence of '/' at the beginning of sysroot-based includes.
953 if (Filename[Pos] == '/')
954 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Douglas Gregore650c8c2009-07-07 00:12:59 +0000956 return Filename + Pos;
957}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000958
Sebastian Redl3397c552010-08-18 23:56:27 +0000959/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000960void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000961 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000962 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000963
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000965 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000966 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000967 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000968 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
969 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
971 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
972 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000973 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregore650c8c2009-07-07 00:12:59 +0000976 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000977 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000978 Record.push_back(VERSION_MAJOR);
979 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000980 Record.push_back(CLANG_VERSION_MAJOR);
981 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000982 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000983 const std::string &Triple = Target.getTriple().getTriple();
984 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
985
986 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000987 serialization::ModuleManager &Mgr = Chain->getModuleManager();
988 llvm::SmallVector<char, 128> ModulePaths;
989 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000990
991 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
992 M != MEnd; ++M) {
993 // Skip modules that weren't directly imported.
994 if (!(*M)->isDirectlyImported())
995 continue;
996
997 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
998 // FIXME: Write import location, once it matters.
999 // FIXME: This writes the absolute path for AST files we depend on.
1000 const std::string &FileName = (*M)->FileName;
1001 Record.push_back(FileName.size());
1002 Record.append(FileName.begin(), FileName.end());
1003 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001004 Stream.EmitRecord(IMPORTS, Record);
1005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Douglas Gregor31d375f2011-05-06 21:43:30 +00001007 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001008 SourceManager &SM = Context.getSourceManager();
1009 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1010 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001011 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001012 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1013 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1014
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001015 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001017 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001018
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001019 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001020 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001021 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001022 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001023 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001024 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001025
1026 Record.clear();
1027 Record.push_back(SM.getMainFileID().getOpaqueValue());
1028 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001029 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001030
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001031 // Original PCH directory
1032 if (!OutputFile.empty() && OutputFile != "-") {
1033 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1034 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1036 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1037
1038 llvm::SmallString<128> OutputPath(OutputFile);
1039
1040 llvm::sys::fs::make_absolute(OutputPath);
1041 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1042
1043 RecordData Record;
1044 Record.push_back(ORIGINAL_PCH_DIR);
1045 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1046 }
1047
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001048 // Repository branch/version information.
1049 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001050 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001051 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1052 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001053 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001054 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001055 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1056 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001057}
1058
1059/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001060void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001061 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001062#define LANGOPT(Name, Bits, Default, Description) \
1063 Record.push_back(LangOpts.Name);
1064#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1065 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1066#include "clang/Basic/LangOptions.def"
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001067
1068 Record.push_back(LangOpts.CurrentModule.size());
1069 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001070 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001071}
1072
Douglas Gregor14f79002009-04-10 03:52:48 +00001073//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001074// stat cache Serialization
1075//===----------------------------------------------------------------------===//
1076
1077namespace {
1078// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001079class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001080public:
1081 typedef const char * key_type;
1082 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Chris Lattner74e976b2010-11-23 19:28:12 +00001084 typedef struct stat data_type;
1085 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001086
1087 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001088 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001089 }
Mike Stump1eb44332009-09-09 15:08:12 +00001090
1091 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001092 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001093 data_type_ref Data) {
1094 unsigned StrLen = strlen(path);
1095 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001096 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001097 clang::io::Emit8(Out, DataLen);
1098 return std::make_pair(StrLen + 1, DataLen);
1099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Chris Lattner5f9e2722011-07-23 10:55:15 +00001101 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102 Out.write(path, KeyLen);
1103 }
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattner5f9e2722011-07-23 10:55:15 +00001105 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001106 data_type_ref Data, unsigned DataLen) {
1107 using namespace clang::io;
1108 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Chris Lattner74e976b2010-11-23 19:28:12 +00001110 Emit32(Out, (uint32_t) Data.st_ino);
1111 Emit32(Out, (uint32_t) Data.st_dev);
1112 Emit16(Out, (uint16_t) Data.st_mode);
1113 Emit64(Out, (uint64_t) Data.st_mtime);
1114 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001115
1116 assert(Out.tell() - Start == DataLen && "Wrong data length");
1117 }
1118};
1119} // end anonymous namespace
1120
Sebastian Redl3397c552010-08-18 23:56:27 +00001121/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001122void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001123 // Build the on-disk hash table containing information about every
1124 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001125 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001126 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001127 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001129 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001130 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001131 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001132 }
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001134 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001135 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001136 uint32_t BucketOffset;
1137 {
1138 llvm::raw_svector_ostream Out(StatCacheData);
1139 // Make sure that no bucket is at offset 0
1140 clang::io::Emit32(Out, 0);
1141 BucketOffset = Generator.Emit(Out);
1142 }
1143
1144 // Create a blob abbreviation
1145 using namespace llvm;
1146 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001147 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1151 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1152
1153 // Write the stat cache
1154 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001155 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001156 Record.push_back(BucketOffset);
1157 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001158 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001159}
1160
1161//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001162// Source Manager Serialization
1163//===----------------------------------------------------------------------===//
1164
1165/// \brief Create an abbreviation for the SLocEntry that refers to a
1166/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001167static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001168 using namespace llvm;
1169 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001170 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001175 // FileEntry fields.
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001183 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001184}
1185
1186/// \brief Create an abbreviation for the SLocEntry that refers to a
1187/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001188static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001189 using namespace llvm;
1190 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001191 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001197 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001198}
1199
1200/// \brief Create an abbreviation for the SLocEntry that refers to a
1201/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001202static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001203 using namespace llvm;
1204 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001205 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001207 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001208}
1209
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001210/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1211/// expansion.
1212static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001213 using namespace llvm;
1214 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001215 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001221 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001222}
1223
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001224namespace {
1225 // Trait used for the on-disk hash table of header search information.
1226 class HeaderFileInfoTrait {
1227 ASTWriter &Writer;
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001228 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001229
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001230 // Keep track of the framework names we've used during serialization.
1231 SmallVector<char, 128> FrameworkStringData;
1232 llvm::StringMap<unsigned> FrameworkNameOffset;
1233
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001234 public:
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001235 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001236 : Writer(Writer), HS(HS) { }
1237
1238 typedef const char *key_type;
1239 typedef key_type key_type_ref;
1240
1241 typedef HeaderFileInfo data_type;
1242 typedef const data_type &data_type_ref;
1243
1244 static unsigned ComputeHash(const char *path) {
1245 // The hash is based only on the filename portion of the key, so that the
1246 // reader can match based on filenames when symlinking or excess path
1247 // elements ("foo/../", "../") change the form of the name. However,
1248 // complete path is still the key.
1249 return llvm::HashString(llvm::sys::path::filename(path));
1250 }
1251
1252 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001253 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001254 data_type_ref Data) {
1255 unsigned StrLen = strlen(path);
1256 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001257 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001258 clang::io::Emit8(Out, DataLen);
1259 return std::make_pair(StrLen + 1, DataLen);
1260 }
1261
Chris Lattner5f9e2722011-07-23 10:55:15 +00001262 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001263 Out.write(path, KeyLen);
1264 }
1265
Chris Lattner5f9e2722011-07-23 10:55:15 +00001266 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001267 data_type_ref Data, unsigned DataLen) {
1268 using namespace clang::io;
1269 uint64_t Start = Out.tell(); (void)Start;
1270
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001271 unsigned char Flags = (Data.isImport << 5)
1272 | (Data.isPragmaOnce << 4)
1273 | (Data.DirInfo << 2)
1274 | (Data.Resolved << 1)
1275 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001276 Emit8(Out, (uint8_t)Flags);
1277 Emit16(Out, (uint16_t) Data.NumIncludes);
1278
1279 if (!Data.ControllingMacro)
1280 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1281 else
1282 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001283
1284 unsigned Offset = 0;
1285 if (!Data.Framework.empty()) {
1286 // If this header refers into a framework, save the framework name.
1287 llvm::StringMap<unsigned>::iterator Pos
1288 = FrameworkNameOffset.find(Data.Framework);
1289 if (Pos == FrameworkNameOffset.end()) {
1290 Offset = FrameworkStringData.size() + 1;
1291 FrameworkStringData.append(Data.Framework.begin(),
1292 Data.Framework.end());
1293 FrameworkStringData.push_back(0);
1294
1295 FrameworkNameOffset[Data.Framework] = Offset;
1296 } else
1297 Offset = Pos->second;
1298 }
1299 Emit32(Out, Offset);
1300
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001301 assert(Out.tell() - Start == DataLen && "Wrong data length");
1302 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001303
1304 const char *strings_begin() const { return FrameworkStringData.begin(); }
1305 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001306 };
1307} // end anonymous namespace
1308
1309/// \brief Write the header search block for the list of files that
1310///
1311/// \param HS The header search structure to save.
1312///
1313/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001314void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001315 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001316 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1317
1318 if (FilesByUID.size() > HS.header_file_size())
1319 FilesByUID.resize(HS.header_file_size());
1320
1321 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1322 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001323 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001324 unsigned NumHeaderSearchEntries = 0;
1325 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1326 const FileEntry *File = FilesByUID[UID];
1327 if (!File)
1328 continue;
1329
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001330 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1331 // from the external source if it was not provided already.
1332 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001333 if (HFI.External && Chain)
1334 continue;
1335
1336 // Turn the file name into an absolute path, if it isn't already.
1337 const char *Filename = File->getName();
1338 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1339
1340 // If we performed any translation on the file name at all, we need to
1341 // save this string, since the generator will refer to it later.
1342 if (Filename != File->getName()) {
1343 Filename = strdup(Filename);
1344 SavedStrings.push_back(Filename);
1345 }
1346
1347 Generator.insert(Filename, HFI, GeneratorTrait);
1348 ++NumHeaderSearchEntries;
1349 }
1350
1351 // Create the on-disk hash table in a buffer.
1352 llvm::SmallString<4096> TableData;
1353 uint32_t BucketOffset;
1354 {
1355 llvm::raw_svector_ostream Out(TableData);
1356 // Make sure that no bucket is at offset 0
1357 clang::io::Emit32(Out, 0);
1358 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1359 }
1360
1361 // Create a blob abbreviation
1362 using namespace llvm;
1363 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1364 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1369 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1370
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001371 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001372 RecordData Record;
1373 Record.push_back(HEADER_SEARCH_TABLE);
1374 Record.push_back(BucketOffset);
1375 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001376 Record.push_back(TableData.size());
1377 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001378 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1379
1380 // Free all of the strings we had to duplicate.
1381 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1382 free((void*)SavedStrings[I]);
1383}
1384
Douglas Gregor14f79002009-04-10 03:52:48 +00001385/// \brief Writes the block containing the serialized form of the
1386/// source manager.
1387///
1388/// TODO: We should probably use an on-disk hash table (stored in a
1389/// blob), indexed based on the file name, so that we only create
1390/// entries for files that we actually need. In the common case (no
1391/// errors), we probably won't have to create file entries for any of
1392/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001393void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001394 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001395 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001396 RecordData Record;
1397
Chris Lattnerf04ad692009-04-10 17:16:57 +00001398 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001399 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001400
1401 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001402 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1403 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1404 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001405 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001406
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001407 // Write out the source location entry table. We skip the first
1408 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001409 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001410 // Write out the offsets of only source location file entries.
1411 // We will go through them in ASTReader::validateFileEntries().
1412 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001413 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001414 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1415 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001416 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001417 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001418 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001419
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001420 // Record the offset of this source-location entry.
1421 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1422
1423 // Figure out which record code to use.
1424 unsigned Code;
1425 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001426 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1427 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001428 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001429 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1430 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001431 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001432 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001433 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001434 Record.clear();
1435 Record.push_back(Code);
1436
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001437 // Starting offset of this entry within this module, so skip the dummy.
1438 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001439 if (SLoc->isFile()) {
1440 const SrcMgr::FileInfo &File = SLoc->getFile();
1441 Record.push_back(File.getIncludeLoc().getRawEncoding());
1442 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1443 Record.push_back(File.hasLineDirectives());
1444
1445 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001446 if (Content->OrigEntry) {
1447 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001448 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001449
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001450 // The source location entry is a file. The blob associated
1451 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Douglas Gregor2d52be52010-03-21 22:49:54 +00001453 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001454 Record.push_back(Content->OrigEntry->getSize());
1455 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001456 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001457 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001458
1459 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1460 if (FDI != FileDeclIDs.end()) {
1461 Record.push_back(FDI->second->FirstDeclIndex);
1462 Record.push_back(FDI->second->DeclIDs.size());
1463 } else {
1464 Record.push_back(0);
1465 Record.push_back(0);
1466 }
Douglas Gregora081da52011-11-16 20:05:18 +00001467
Douglas Gregore650c8c2009-07-07 00:12:59 +00001468 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001469 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001470 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001471
1472 // Ask the file manager to fixup the relative path for us. This will
1473 // honor the working directory.
1474 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1475
1476 // FIXME: This call to make_absolute shouldn't be necessary, the
1477 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001478 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001479 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Douglas Gregore650c8c2009-07-07 00:12:59 +00001481 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001482 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001483
1484 if (Content->BufferOverridden) {
1485 Record.clear();
1486 Record.push_back(SM_SLOC_BUFFER_BLOB);
1487 const llvm::MemoryBuffer *Buffer
1488 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1489 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1490 StringRef(Buffer->getBufferStart(),
1491 Buffer->getBufferSize() + 1));
1492 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001493 } else {
1494 // The source location entry is a buffer. The blob associated
1495 // with this entry contains the contents of the buffer.
1496
1497 // We add one to the size so that we capture the trailing NULL
1498 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1499 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001500 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001501 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001502 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001503 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001504 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001505 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001506 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001507 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001508 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001509 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001510
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001511 if (strcmp(Name, "<built-in>") == 0) {
1512 PreloadSLocs.push_back(SLocEntryOffsets.size());
1513 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001514 }
1515 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001516 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001517 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001518 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1519 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001520 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1521 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001522
1523 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001524 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001525 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001526 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001527 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001528 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001529 }
1530 }
1531
Douglas Gregorc9490c02009-04-16 22:23:12 +00001532 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001533
1534 if (SLocEntryOffsets.empty())
1535 return;
1536
Sebastian Redl3397c552010-08-18 23:56:27 +00001537 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001538 // table is used for lazily loading source-location information.
1539 using namespace llvm;
1540 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001541 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1545 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001548 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001549 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001550 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001551 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001553 Abbrev = new BitCodeAbbrev();
1554 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1557 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1558
1559 Record.clear();
1560 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1561 Record.push_back(SLocFileEntryOffsets.size());
1562 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1563 data(SLocFileEntryOffsets));
1564
Sebastian Redl3397c552010-08-18 23:56:27 +00001565 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001566 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001567 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001568
1569 // Write the line table. It depends on remapping working, so it must come
1570 // after the source location offsets.
1571 if (SourceMgr.hasLineTable()) {
1572 LineTableInfo &LineTable = SourceMgr.getLineTable();
1573
1574 Record.clear();
1575 // Emit the file names
1576 Record.push_back(LineTable.getNumFilenames());
1577 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1578 // Emit the file name
1579 const char *Filename = LineTable.getFilename(I);
1580 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1581 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1582 Record.push_back(FilenameLen);
1583 if (FilenameLen)
1584 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1585 }
1586
1587 // Emit the line entries
1588 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1589 L != LEnd; ++L) {
1590 // Only emit entries for local files.
1591 if (L->first < 0)
1592 continue;
1593
1594 // Emit the file ID
1595 Record.push_back(L->first);
1596
1597 // Emit the line entries
1598 Record.push_back(L->second.size());
1599 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1600 LEEnd = L->second.end();
1601 LE != LEEnd; ++LE) {
1602 Record.push_back(LE->FileOffset);
1603 Record.push_back(LE->LineNo);
1604 Record.push_back(LE->FilenameID);
1605 Record.push_back((unsigned)LE->FileKind);
1606 Record.push_back(LE->IncludeOffset);
1607 }
1608 }
1609 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1610 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001611}
1612
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001613//===----------------------------------------------------------------------===//
1614// Preprocessor Serialization
1615//===----------------------------------------------------------------------===//
1616
Douglas Gregor9c736102011-02-10 18:20:09 +00001617static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1618 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1619 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1620 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1621 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1622 return X.first->getName().compare(Y.first->getName());
1623}
1624
Chris Lattner0b1fb982009-04-10 17:15:23 +00001625/// \brief Writes the block containing the serialized form of the
1626/// preprocessor.
1627///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001628void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001629 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1630 if (PPRec)
1631 WritePreprocessorDetail(*PPRec);
1632
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001633 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001634
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001635 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1636 if (PP.getCounterValue() != 0) {
1637 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001638 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001639 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001640 }
1641
1642 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001643 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Sebastian Redl3397c552010-08-18 23:56:27 +00001645 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001646 // FIXME: use diagnostics subsystem for localization etc.
1647 if (PP.SawDateOrTime())
1648 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Douglas Gregorecdcb882010-10-20 22:00:55 +00001650
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001651 // Loop over all the macro definitions that are live at the end of the file,
1652 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001653
Douglas Gregor9c736102011-02-10 18:20:09 +00001654 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001655 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001656 MacrosToEmit;
1657 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001658 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1659 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001660 I != E; ++I) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001661 const IdentifierInfo *Name = I->first;
Douglas Gregoraa93a872011-10-17 15:32:29 +00001662 if (!IsModule || I->second->isPublic()) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001663 MacroDefinitionsSeen.insert(Name);
Douglas Gregor7143aab2011-09-01 17:04:32 +00001664 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1665 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001666 }
1667
1668 // Sort the set of macro definitions that need to be serialized by the
1669 // name of the macro, to provide a stable ordering.
1670 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1671 &compareMacroDefinitions);
1672
Douglas Gregor040a8042011-02-11 00:26:14 +00001673 // Resolve any identifiers that defined macros at the time they were
1674 // deserialized, adding them to the list of macros to emit (if appropriate).
1675 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1676 IdentifierInfo *Name
1677 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1678 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1679 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1680 }
1681
Douglas Gregor9c736102011-02-10 18:20:09 +00001682 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1683 const IdentifierInfo *Name = MacrosToEmit[I].first;
1684 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001685 if (!MI)
1686 continue;
1687
Sebastian Redl3397c552010-08-18 23:56:27 +00001688 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001689 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001690 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001691
1692 // FIXME: There is a (probably minor) optimization we could do here, if
1693 // the macro comes from the original PCH but the identifier comes from a
1694 // chained PCH, by storing the offset into the original PCH rather than
1695 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001696 if (MI->isBuiltinMacro() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00001697 (Chain &&
1698 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1699 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001700 continue;
1701
Douglas Gregor9c736102011-02-10 18:20:09 +00001702 AddIdentifierRef(Name, Record);
1703 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001704 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1705 Record.push_back(MI->isUsed());
Douglas Gregoraa93a872011-10-17 15:32:29 +00001706 Record.push_back(MI->isPublic());
1707 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001708 unsigned Code;
1709 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001710 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001711 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001712 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001714 Record.push_back(MI->isC99Varargs());
1715 Record.push_back(MI->isGNUVarargs());
1716 Record.push_back(MI->getNumArgs());
1717 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1718 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001719 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001720 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001721
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001722 // If we have a detailed preprocessing record, record the macro definition
1723 // ID that corresponds to this macro.
1724 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001725 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001726
Douglas Gregorc9490c02009-04-16 22:23:12 +00001727 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001728 Record.clear();
1729
Chris Lattnerdf961c22009-04-10 18:08:30 +00001730 // Emit the tokens array.
1731 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1732 // Note that we know that the preprocessor does not have any annotation
1733 // tokens in it because they are created by the parser, and thus can't be
1734 // in a macro definition.
1735 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Chris Lattnerdf961c22009-04-10 18:08:30 +00001737 Record.push_back(Tok.getLocation().getRawEncoding());
1738 Record.push_back(Tok.getLength());
1739
Chris Lattnerdf961c22009-04-10 18:08:30 +00001740 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1741 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001742 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001743 // FIXME: Should translate token kind to a stable encoding.
1744 Record.push_back(Tok.getKind());
1745 // FIXME: Should translate token flags to a stable encoding.
1746 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001748 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001749 Record.clear();
1750 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001751 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001752 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001753 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001754}
1755
1756void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001757 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001758 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001759
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001760 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001761
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001762 // Enter the preprocessor block.
1763 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001764
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001765 // If the preprocessor has a preprocessing record, emit it.
1766 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001767 using namespace llvm;
1768
1769 // Set up the abbreviation for
1770 unsigned InclusionAbbrev = 0;
1771 {
1772 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1773 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1778 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1779 }
1780
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001781 unsigned FirstPreprocessorEntityID
1782 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1783 + NUM_PREDEF_PP_ENTITY_IDS;
1784 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001785 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001786 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1787 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001788 E != EEnd;
1789 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001790 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001791
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001792 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1793 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001794
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001795 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001796 // Record this macro definition's ID.
1797 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001798
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001799 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001800 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1801 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001802 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001803
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001804 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001805 Record.push_back(ME->isBuiltinMacro());
1806 if (ME->isBuiltinMacro())
1807 AddIdentifierRef(ME->getName(), Record);
1808 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001809 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001810 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001811 continue;
1812 }
1813
1814 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1815 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001816 Record.push_back(ID->getFileName().size());
1817 Record.push_back(ID->wasInQuotes());
1818 Record.push_back(static_cast<unsigned>(ID->getKind()));
1819 llvm::SmallString<64> Buffer;
1820 Buffer += ID->getFileName();
1821 Buffer += ID->getFile()->getName();
1822 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1823 continue;
1824 }
1825
1826 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1827 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001828 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001829
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001830 // Write the offsets table for the preprocessing record.
1831 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001832 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1833
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001834 // Write the offsets table for identifier IDs.
1835 using namespace llvm;
1836 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001837 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001840 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001841
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001842 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001843 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001844 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001845 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1846 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001847 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001848}
1849
Douglas Gregore209e502011-12-06 01:10:29 +00001850unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1851 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1852 if (Known != SubmoduleIDs.end())
1853 return Known->second;
1854
1855 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1856}
1857
Douglas Gregor26ced122011-12-01 00:59:36 +00001858/// \brief Compute the number of modules within the given tree (including the
1859/// given module).
1860static unsigned getNumberOfModules(Module *Mod) {
1861 unsigned ChildModules = 0;
1862 for (llvm::StringMap<Module *>::iterator Sub = Mod->SubModules.begin(),
1863 SubEnd = Mod->SubModules.end();
1864 Sub != SubEnd; ++Sub)
1865 ChildModules += getNumberOfModules(Sub->getValue());
1866
1867 return ChildModules + 1;
1868}
1869
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001870void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001871 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001872 // FIXME: This feels like it belongs somewhere else, but there are no
1873 // other consumers of this information.
1874 SourceManager &SrcMgr = PP->getSourceManager();
1875 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1876 for (ASTContext::import_iterator I = Context->local_import_begin(),
1877 IEnd = Context->local_import_end();
1878 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001879 if (Module *ImportedFrom
1880 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1881 SrcMgr))) {
1882 ImportedFrom->Imports.push_back(I->getImportedModule());
1883 }
1884 }
1885
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001886 // Enter the submodule description block.
1887 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1888
1889 // Write the abbreviations needed for the submodules block.
1890 using namespace llvm;
1891 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1892 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001893 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregor1e123682011-12-05 22:27:44 +00001897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
1898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
1899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1901 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1902
1903 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001904 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1906 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1907
1908 Abbrev = new BitCodeAbbrev();
1909 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1911 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001912
1913 Abbrev = new BitCodeAbbrev();
1914 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1916 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1917
Douglas Gregor51f564f2011-12-31 04:05:44 +00001918 Abbrev = new BitCodeAbbrev();
1919 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1921 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1922
Douglas Gregor26ced122011-12-01 00:59:36 +00001923 // Write the submodule metadata block.
1924 RecordData Record;
1925 Record.push_back(getNumberOfModules(WritingModule));
1926 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1927 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1928
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001929 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001930 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001931 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001932 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001933 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001934 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001935 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001936
1937 // Emit the definition of the block.
1938 Record.clear();
1939 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001940 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001941 if (Mod->Parent) {
1942 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1943 Record.push_back(SubmoduleIDs[Mod->Parent]);
1944 } else {
1945 Record.push_back(0);
1946 }
1947 Record.push_back(Mod->IsFramework);
1948 Record.push_back(Mod->IsExplicit);
Douglas Gregor1e123682011-12-05 22:27:44 +00001949 Record.push_back(Mod->InferSubmodules);
1950 Record.push_back(Mod->InferExplicitSubmodules);
1951 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001952 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1953
Douglas Gregor51f564f2011-12-31 04:05:44 +00001954 // Emit the requirements.
1955 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1956 Record.clear();
1957 Record.push_back(SUBMODULE_REQUIRES);
1958 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1959 Mod->Requires[I].data(),
1960 Mod->Requires[I].size());
1961 }
1962
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001963 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00001964 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001965 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001966 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001967 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00001968 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00001969 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1970 Record.clear();
1971 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1972 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1973 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001974 }
1975
1976 // Emit the headers.
1977 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1978 Record.clear();
1979 Record.push_back(SUBMODULE_HEADER);
1980 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1981 Mod->Headers[I]->getName());
1982 }
Douglas Gregor55988682011-12-05 16:33:54 +00001983
1984 // Emit the imports.
1985 if (!Mod->Imports.empty()) {
1986 Record.clear();
1987 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00001988 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00001989 assert(ImportedID && "Unknown submodule!");
1990 Record.push_back(ImportedID);
1991 }
1992 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
1993 }
1994
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00001995 // Emit the exports.
1996 if (!Mod->Exports.empty()) {
1997 Record.clear();
1998 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00001999 if (Module *Exported = Mod->Exports[I].getPointer()) {
2000 unsigned ExportedID = SubmoduleIDs[Exported];
2001 assert(ExportedID > 0 && "Unknown submodule ID?");
2002 Record.push_back(ExportedID);
2003 } else {
2004 Record.push_back(0);
2005 }
2006
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002007 Record.push_back(Mod->Exports[I].getInt());
2008 }
2009 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2010 }
2011
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002012 // Queue up the submodules of this module.
2013 llvm::SmallVector<StringRef, 2> SubModules;
2014
2015 // Sort the submodules first, so we get a predictable ordering in the AST
2016 // file.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002017 for (llvm::StringMap<Module *>::iterator
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002018 Sub = Mod->SubModules.begin(),
2019 SubEnd = Mod->SubModules.end();
2020 Sub != SubEnd; ++Sub)
2021 SubModules.push_back(Sub->getKey());
2022 llvm::array_pod_sort(SubModules.begin(), SubModules.end());
2023
2024 for (unsigned I = 0, N = SubModules.size(); I != N; ++I)
2025 Q.push(Mod->SubModules[SubModules[I]]);
2026 }
2027
2028 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002029
2030 assert((NextSubmoduleID - FirstSubmoduleID
2031 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002032}
2033
Douglas Gregor185dbd72011-12-01 02:07:58 +00002034serialization::SubmoduleID
2035ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002036 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002037 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002038
2039 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002040 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002041 Module *OwningMod
2042 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002043 if (!OwningMod)
2044 return 0;
2045
Douglas Gregore209e502011-12-06 01:10:29 +00002046 // Check whether this submodule is part of our own module.
2047 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002048 return 0;
2049
Douglas Gregore209e502011-12-06 01:10:29 +00002050 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002051}
2052
David Blaikied6471f72011-09-25 23:23:43 +00002053void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002054 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002055 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002056 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2057 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002058 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002059 if (point.Loc.isInvalid())
2060 continue;
2061
2062 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002063 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002064 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002065 if (I->second.isPragma()) {
2066 Record.push_back(I->first);
2067 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002068 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002069 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002070 Record.push_back(-1); // mark the end of the diag/map pairs for this
2071 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002072 }
2073
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002074 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002075 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002076}
2077
Anders Carlssonc8505782011-03-06 18:41:18 +00002078void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2079 if (CXXBaseSpecifiersOffsets.empty())
2080 return;
2081
2082 RecordData Record;
2083
2084 // Create a blob abbreviation for the C++ base specifiers offsets.
2085 using namespace llvm;
2086
2087 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2088 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2091 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2092
Douglas Gregore92b8a12011-08-04 00:01:48 +00002093 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002094 Record.clear();
2095 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2096 Record.push_back(CXXBaseSpecifiersOffsets.size());
2097 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002098 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002099}
2100
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002101//===----------------------------------------------------------------------===//
2102// Type Serialization
2103//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002104
Sebastian Redl3397c552010-08-18 23:56:27 +00002105/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002106void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002107 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002108 if (Idx.getIndex() == 0) // we haven't seen this type before.
2109 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Douglas Gregor97475832010-10-05 18:37:06 +00002111 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002112
Douglas Gregor2cf26342009-04-09 22:27:44 +00002113 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002114 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002115 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002116 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002117 else if (TypeOffsets.size() < Index) {
2118 TypeOffsets.resize(Index + 1);
2119 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002120 }
2121
2122 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002123
Douglas Gregor2cf26342009-04-09 22:27:44 +00002124 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002125 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002126
Douglas Gregora4923eb2009-11-16 21:35:15 +00002127 if (T.hasLocalNonFastQualifiers()) {
2128 Qualifiers Qs = T.getLocalQualifiers();
2129 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002130 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002131 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002132 } else {
2133 switch (T->getTypeClass()) {
2134 // For all of the concrete, non-dependent types, call the
2135 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002136#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002137 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002138#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002139#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002140 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002141 }
2142
2143 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002144 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002145
2146 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002147 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002148}
2149
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002150//===----------------------------------------------------------------------===//
2151// Declaration Serialization
2152//===----------------------------------------------------------------------===//
2153
Douglas Gregor2cf26342009-04-09 22:27:44 +00002154/// \brief Write the block containing all of the declaration IDs
2155/// lexically declared within the given DeclContext.
2156///
2157/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2158/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002159uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002160 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002161 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002162 return 0;
2163
Douglas Gregorc9490c02009-04-16 22:23:12 +00002164 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002165 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002166 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002167 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002168 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2169 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002170 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002171
Douglas Gregor25123082009-04-22 22:34:57 +00002172 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002173 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002174 return Offset;
2175}
2176
Sebastian Redla4232eb2010-08-18 23:56:21 +00002177void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002178 using namespace llvm;
2179 RecordData Record;
2180
2181 // Write the type offsets array
2182 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002183 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2187 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2188 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002189 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002190 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002191 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002192 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002193
2194 // Write the declaration offsets array
2195 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002196 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2200 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2201 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002202 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002203 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002204 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002205 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002206}
2207
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002208void ASTWriter::WriteFileDeclIDsMap() {
2209 using namespace llvm;
2210 RecordData Record;
2211
2212 // Join the vectors of DeclIDs from all files.
2213 SmallVector<DeclID, 256> FileSortedIDs;
2214 for (FileDeclIDsTy::iterator
2215 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2216 DeclIDInFileInfo &Info = *FI->second;
2217 Info.FirstDeclIndex = FileSortedIDs.size();
2218 for (LocDeclIDsTy::iterator
2219 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2220 FileSortedIDs.push_back(DI->second);
2221 }
2222
2223 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2224 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2226 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2227 Record.push_back(FILE_SORTED_DECLS);
2228 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2229}
2230
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002231//===----------------------------------------------------------------------===//
2232// Global Method Pool and Selector Serialization
2233//===----------------------------------------------------------------------===//
2234
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002235namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002236// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002237class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002238 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002239
2240public:
2241 typedef Selector key_type;
2242 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Sebastian Redl5d050072010-08-04 17:20:04 +00002244 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002245 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002246 ObjCMethodList Instance, Factory;
2247 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002248 typedef const data_type& data_type_ref;
2249
Sebastian Redl3397c552010-08-18 23:56:27 +00002250 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002252 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002253 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002254 }
Mike Stump1eb44332009-09-09 15:08:12 +00002255
2256 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002257 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002258 data_type_ref Methods) {
2259 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2260 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002261 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2262 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002263 Method = Method->Next)
2264 if (Method->Method)
2265 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002266 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002267 Method = Method->Next)
2268 if (Method->Method)
2269 DataLen += 4;
2270 clang::io::Emit16(Out, DataLen);
2271 return std::make_pair(KeyLen, DataLen);
2272 }
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Chris Lattner5f9e2722011-07-23 10:55:15 +00002274 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002275 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002276 assert((Start >> 32) == 0 && "Selector key offset too large");
2277 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002278 unsigned N = Sel.getNumArgs();
2279 clang::io::Emit16(Out, N);
2280 if (N == 0)
2281 N = 1;
2282 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002283 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002284 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2285 }
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Chris Lattner5f9e2722011-07-23 10:55:15 +00002287 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002288 data_type_ref Methods, unsigned DataLen) {
2289 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002290 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002291 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002292 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002293 Method = Method->Next)
2294 if (Method->Method)
2295 ++NumInstanceMethods;
2296
2297 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002298 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002299 Method = Method->Next)
2300 if (Method->Method)
2301 ++NumFactoryMethods;
2302
2303 clang::io::Emit16(Out, NumInstanceMethods);
2304 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002305 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002306 Method = Method->Next)
2307 if (Method->Method)
2308 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002309 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002310 Method = Method->Next)
2311 if (Method->Method)
2312 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002313
2314 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002315 }
2316};
2317} // end anonymous namespace
2318
Sebastian Redl059612d2010-08-03 21:58:15 +00002319/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002320///
2321/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002322/// in an on-disk hash table indexed by the selector. The hash table also
2323/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002324void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002325 using namespace llvm;
2326
Sebastian Redl059612d2010-08-03 21:58:15 +00002327 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002328 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002329 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002330 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002331 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002332 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002333 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002334 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Sebastian Redl059612d2010-08-03 21:58:15 +00002336 // Create the on-disk hash table representation. We walk through every
2337 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002338 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002339 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002340 I = SelectorIDs.begin(), E = SelectorIDs.end();
2341 I != E; ++I) {
2342 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002343 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002344 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002345 I->second,
2346 ObjCMethodList(),
2347 ObjCMethodList()
2348 };
2349 if (F != SemaRef.MethodPool.end()) {
2350 Data.Instance = F->second.first;
2351 Data.Factory = F->second.second;
2352 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002353 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002354 // changed.
2355 if (Chain && I->second < FirstSelectorID) {
2356 // Selector already exists. Did it change?
2357 bool changed = false;
2358 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2359 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002360 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002361 changed = true;
2362 }
2363 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2364 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002365 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002366 changed = true;
2367 }
2368 if (!changed)
2369 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002370 } else if (Data.Instance.Method || Data.Factory.Method) {
2371 // A new method pool entry.
2372 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002373 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002374 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002375 }
2376
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002377 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002378 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002379 uint32_t BucketOffset;
2380 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002381 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002382 llvm::raw_svector_ostream Out(MethodPool);
2383 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002384 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002385 BucketOffset = Generator.Emit(Out, Trait);
2386 }
2387
2388 // Create a blob abbreviation
2389 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002390 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2394 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2395
Douglas Gregor83941df2009-04-25 17:48:32 +00002396 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002397 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002398 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002399 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002400 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002401 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002402
2403 // Create a blob abbreviation for the selector table offsets.
2404 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002405 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2409 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2410
2411 // Write the selector offsets table.
2412 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002413 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002414 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002415 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002416 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002417 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002418 }
2419}
2420
Sebastian Redl3397c552010-08-18 23:56:27 +00002421/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002422void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002423 using namespace llvm;
2424 if (SemaRef.ReferencedSelectors.empty())
2425 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002426
Fariborz Jahanian32019832010-07-23 19:11:11 +00002427 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002428
Sebastian Redl3397c552010-08-18 23:56:27 +00002429 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002430 // very tricky to fix, and given that @selector shouldn't really appear in
2431 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002432 for (DenseMap<Selector, SourceLocation>::iterator S =
2433 SemaRef.ReferencedSelectors.begin(),
2434 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2435 Selector Sel = (*S).first;
2436 SourceLocation Loc = (*S).second;
2437 AddSelectorRef(Sel, Record);
2438 AddSourceLocation(Loc, Record);
2439 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002440 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002441}
2442
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002443//===----------------------------------------------------------------------===//
2444// Identifier Table Serialization
2445//===----------------------------------------------------------------------===//
2446
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002447namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002448class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002449 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002450 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002451 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002452 bool IsModule;
2453
Douglas Gregora92193e2009-04-28 21:18:29 +00002454 /// \brief Determines whether this is an "interesting" identifier
2455 /// that needs a full IdentifierInfo structure written into the hash
2456 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002457 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002458 if (II->isPoisoned() ||
2459 II->isExtensionToken() ||
2460 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002461 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002462 II->getFETokenInfo<void>())
2463 return true;
2464
Douglas Gregorce835df2011-09-14 22:14:14 +00002465 return hasMacroDefinition(II, Macro);
2466 }
2467
2468 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002469 if (!II->hasMacroDefinition())
2470 return false;
2471
Douglas Gregorce835df2011-09-14 22:14:14 +00002472 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002473 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002474
Douglas Gregorce835df2011-09-14 22:14:14 +00002475 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002476 }
2477
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002478public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002479 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002480 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002482 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002483 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Douglas Gregoreee242f2011-10-27 09:33:13 +00002485 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2486 IdentifierResolver &IdResolver, bool IsModule)
2487 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002488
2489 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002490 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002491 }
Mike Stump1eb44332009-09-09 15:08:12 +00002492
2493 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002494 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002495 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002496 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002497 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002498 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002499 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002500 if (hasMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002501 DataLen += 8;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002502
2503 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2504 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002505 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002506 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002507 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002508 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002509 // We emit the key length after the data length so that every
2510 // string is preceded by a 16-bit length. This matches the PTH
2511 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002512 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002513 return std::make_pair(KeyLen, DataLen);
2514 }
Mike Stump1eb44332009-09-09 15:08:12 +00002515
Chris Lattner5f9e2722011-07-23 10:55:15 +00002516 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002517 unsigned KeyLen) {
2518 // Record the location of the key data. This is used when generating
2519 // the mapping from persistent IDs to strings.
2520 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002521 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002522 }
Mike Stump1eb44332009-09-09 15:08:12 +00002523
Douglas Gregor7143aab2011-09-01 17:04:32 +00002524 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002525 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002526 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002527 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002528 clang::io::Emit32(Out, ID << 1);
2529 return;
2530 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002531
Douglas Gregora92193e2009-04-28 21:18:29 +00002532 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002533 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002534 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002535 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topper925be542011-12-19 05:04:33 +00002536 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregorce835df2011-09-14 22:14:14 +00002537 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002538 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2539 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002540 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002541 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002542 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002543
Douglas Gregor13292642011-12-02 15:45:10 +00002544 if (HasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002545 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor13292642011-12-02 15:45:10 +00002546 clang::io::Emit32(Out,
2547 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2548 }
2549
Douglas Gregor668c1a42009-04-21 22:25:48 +00002550 // Emit the declaration IDs in reverse order, because the
2551 // IdentifierResolver provides the declarations as they would be
2552 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002553 // "stat"), but the ASTReader adds declarations to the end of the list
2554 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002555 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002556 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2557 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002558 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002559 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002560 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002561 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002562 }
2563};
2564} // end anonymous namespace
2565
Sebastian Redl3397c552010-08-18 23:56:27 +00002566/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002567///
2568/// The identifier table consists of a blob containing string data
2569/// (the actual identifiers themselves) and a separate "offsets" index
2570/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002571void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2572 IdentifierResolver &IdResolver,
2573 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002574 using namespace llvm;
2575
2576 // Create and write out the blob that contains the identifier
2577 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002578 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002579 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002580 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Douglas Gregor92b059e2009-04-28 20:33:11 +00002582 // Look for any identifiers that were named while processing the
2583 // headers, but are otherwise not needed. We add these to the hash
2584 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002585 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002586 // file.
2587 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2588 IDEnd = PP.getIdentifierTable().end();
2589 ID != IDEnd; ++ID)
2590 getIdentifierRef(ID->second);
2591
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002592 // Create the on-disk hash table representation. We only store offsets
2593 // for identifiers that appear here for the first time.
2594 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002595 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002596 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2597 ID != IDEnd; ++ID) {
2598 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002599 if (!Chain || !ID->first->isFromAST() ||
2600 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002601 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2602 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002603 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002604
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002605 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002606 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002607 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002608 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002609 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002610 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002611 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002612 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002613 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002614 }
2615
2616 // Create a blob abbreviation
2617 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002618 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002621 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002622
2623 // Write the identifier table
2624 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002625 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002626 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002627 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002628 }
2629
2630 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002631 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002632 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2636 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2637
2638 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002639 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002640 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002641 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002642 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002643 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002644}
2645
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002646//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002647// DeclContext's Name Lookup Table Serialization
2648//===----------------------------------------------------------------------===//
2649
2650namespace {
2651// Trait used for the on-disk hash table used in the method pool.
2652class ASTDeclContextNameLookupTrait {
2653 ASTWriter &Writer;
2654
2655public:
2656 typedef DeclarationName key_type;
2657 typedef key_type key_type_ref;
2658
2659 typedef DeclContext::lookup_result data_type;
2660 typedef const data_type& data_type_ref;
2661
2662 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2663
2664 unsigned ComputeHash(DeclarationName Name) {
2665 llvm::FoldingSetNodeID ID;
2666 ID.AddInteger(Name.getNameKind());
2667
2668 switch (Name.getNameKind()) {
2669 case DeclarationName::Identifier:
2670 ID.AddString(Name.getAsIdentifierInfo()->getName());
2671 break;
2672 case DeclarationName::ObjCZeroArgSelector:
2673 case DeclarationName::ObjCOneArgSelector:
2674 case DeclarationName::ObjCMultiArgSelector:
2675 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2676 break;
2677 case DeclarationName::CXXConstructorName:
2678 case DeclarationName::CXXDestructorName:
2679 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002680 break;
2681 case DeclarationName::CXXOperatorName:
2682 ID.AddInteger(Name.getCXXOverloadedOperator());
2683 break;
2684 case DeclarationName::CXXLiteralOperatorName:
2685 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2686 case DeclarationName::CXXUsingDirective:
2687 break;
2688 }
2689
2690 return ID.ComputeHash();
2691 }
2692
2693 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002694 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002695 data_type_ref Lookup) {
2696 unsigned KeyLen = 1;
2697 switch (Name.getNameKind()) {
2698 case DeclarationName::Identifier:
2699 case DeclarationName::ObjCZeroArgSelector:
2700 case DeclarationName::ObjCOneArgSelector:
2701 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002702 case DeclarationName::CXXLiteralOperatorName:
2703 KeyLen += 4;
2704 break;
2705 case DeclarationName::CXXOperatorName:
2706 KeyLen += 1;
2707 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002708 case DeclarationName::CXXConstructorName:
2709 case DeclarationName::CXXDestructorName:
2710 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002711 case DeclarationName::CXXUsingDirective:
2712 break;
2713 }
2714 clang::io::Emit16(Out, KeyLen);
2715
2716 // 2 bytes for num of decls and 4 for each DeclID.
2717 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2718 clang::io::Emit16(Out, DataLen);
2719
2720 return std::make_pair(KeyLen, DataLen);
2721 }
2722
Chris Lattner5f9e2722011-07-23 10:55:15 +00002723 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002724 using namespace clang::io;
2725
2726 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2727 Emit8(Out, Name.getNameKind());
2728 switch (Name.getNameKind()) {
2729 case DeclarationName::Identifier:
2730 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2731 break;
2732 case DeclarationName::ObjCZeroArgSelector:
2733 case DeclarationName::ObjCOneArgSelector:
2734 case DeclarationName::ObjCMultiArgSelector:
2735 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2736 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002737 case DeclarationName::CXXOperatorName:
2738 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2739 Emit8(Out, Name.getCXXOverloadedOperator());
2740 break;
2741 case DeclarationName::CXXLiteralOperatorName:
2742 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2743 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002744 case DeclarationName::CXXConstructorName:
2745 case DeclarationName::CXXDestructorName:
2746 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002747 case DeclarationName::CXXUsingDirective:
2748 break;
2749 }
2750 }
2751
Chris Lattner5f9e2722011-07-23 10:55:15 +00002752 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002753 data_type Lookup, unsigned DataLen) {
2754 uint64_t Start = Out.tell(); (void)Start;
2755 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2756 for (; Lookup.first != Lookup.second; ++Lookup.first)
2757 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2758
2759 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2760 }
2761};
2762} // end anonymous namespace
2763
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002764/// \brief Write the block containing all of the declaration IDs
2765/// visible from the given DeclContext.
2766///
2767/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002768/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002769uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2770 DeclContext *DC) {
2771 if (DC->getPrimaryContext() != DC)
2772 return 0;
2773
2774 // Since there is no name lookup into functions or methods, don't bother to
2775 // build a visible-declarations table for these entities.
2776 if (DC->isFunctionOrMethod())
2777 return 0;
2778
2779 // If not in C++, we perform name lookup for the translation unit via the
2780 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2781 // FIXME: In C++ we need the visible declarations in order to "see" the
2782 // friend declarations, is there a way to do this without writing the table ?
2783 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2784 return 0;
2785
2786 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002787 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002788 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002789
2790 // Serialize the contents of the mapping used for lookup. Note that,
2791 // although we have two very different code paths, the serialized
2792 // representation is the same for both cases: a declaration name,
2793 // followed by a size, followed by references to the visible
2794 // declarations that have that name.
2795 uint64_t Offset = Stream.GetCurrentBitNo();
2796 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2797 if (!Map || Map->empty())
2798 return 0;
2799
2800 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2801 ASTDeclContextNameLookupTrait Trait(*this);
2802
2803 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002804 DeclarationName ConversionName;
2805 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002806 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2807 D != DEnd; ++D) {
2808 DeclarationName Name = D->first;
2809 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002810 if (Result.first != Result.second) {
2811 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2812 // Hash all conversion function names to the same name. The actual
2813 // type information in conversion function name is not used in the
2814 // key (since such type information is not stable across different
2815 // modules), so the intended effect is to coalesce all of the conversion
2816 // functions under a single key.
2817 if (!ConversionName)
2818 ConversionName = Name;
2819 ConversionDecls.append(Result.first, Result.second);
2820 continue;
2821 }
2822
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002823 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002824 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002825 }
2826
Douglas Gregore5a54b62011-08-30 20:49:19 +00002827 // Add the conversion functions
2828 if (!ConversionDecls.empty()) {
2829 Generator.insert(ConversionName,
2830 DeclContext::lookup_result(ConversionDecls.begin(),
2831 ConversionDecls.end()),
2832 Trait);
2833 }
2834
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002835 // Create the on-disk hash table in a buffer.
2836 llvm::SmallString<4096> LookupTable;
2837 uint32_t BucketOffset;
2838 {
2839 llvm::raw_svector_ostream Out(LookupTable);
2840 // Make sure that no bucket is at offset 0
2841 clang::io::Emit32(Out, 0);
2842 BucketOffset = Generator.Emit(Out, Trait);
2843 }
2844
2845 // Write the lookup table
2846 RecordData Record;
2847 Record.push_back(DECL_CONTEXT_VISIBLE);
2848 Record.push_back(BucketOffset);
2849 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2850 LookupTable.str());
2851
2852 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2853 ++NumVisibleDeclContexts;
2854 return Offset;
2855}
2856
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002857/// \brief Write an UPDATE_VISIBLE block for the given context.
2858///
2859/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2860/// DeclContext in a dependent AST file. As such, they only exist for the TU
2861/// (in C++) and for namespaces.
2862void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002863 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2864 if (!Map || Map->empty())
2865 return;
2866
2867 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2868 ASTDeclContextNameLookupTrait Trait(*this);
2869
2870 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002871 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2872 D != DEnd; ++D) {
2873 DeclarationName Name = D->first;
2874 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002875 // For any name that appears in this table, the results are complete, i.e.
2876 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002877 if (Result.first != Result.second)
2878 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002879 }
2880
2881 // Create the on-disk hash table in a buffer.
2882 llvm::SmallString<4096> LookupTable;
2883 uint32_t BucketOffset;
2884 {
2885 llvm::raw_svector_ostream Out(LookupTable);
2886 // Make sure that no bucket is at offset 0
2887 clang::io::Emit32(Out, 0);
2888 BucketOffset = Generator.Emit(Out, Trait);
2889 }
2890
2891 // Write the lookup table
2892 RecordData Record;
2893 Record.push_back(UPDATE_VISIBLE);
2894 Record.push_back(getDeclID(cast<Decl>(DC)));
2895 Record.push_back(BucketOffset);
2896 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2897}
2898
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002899/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2900void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2901 RecordData Record;
2902 Record.push_back(Opts.fp_contract);
2903 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2904}
2905
2906/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2907void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2908 if (!SemaRef.Context.getLangOptions().OpenCL)
2909 return;
2910
2911 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2912 RecordData Record;
2913#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2914#include "clang/Basic/OpenCLExtensions.def"
2915 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2916}
2917
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002918void ASTWriter::WriteMergedDecls() {
2919 if (!Chain || Chain->MergedDecls.empty())
2920 return;
2921
2922 RecordData Record;
2923 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
2924 IEnd = Chain->MergedDecls.end();
2925 I != IEnd; ++I) {
2926 DeclID CanonID = I->first->isFromASTFile()? Chain->DeclToID[I->first]
2927 : getDeclID(I->first);
2928 assert(CanonID && "Merged declaration not known?");
2929
2930 Record.push_back(CanonID);
2931 Record.push_back(I->second.size());
2932 Record.append(I->second.begin(), I->second.end());
2933 }
2934 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
2935}
2936
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002937//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002938// General Serialization Routines
2939//===----------------------------------------------------------------------===//
2940
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002941/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002942void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002943 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002944 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2945 const Attr * A = *i;
2946 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002947 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002948
Sean Huntcf807c42010-08-18 23:23:40 +00002949#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002950
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002951 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002952}
2953
Chris Lattner5f9e2722011-07-23 10:55:15 +00002954void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002955 Record.push_back(Str.size());
2956 Record.insert(Record.end(), Str.begin(), Str.end());
2957}
2958
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002959void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2960 RecordDataImpl &Record) {
2961 Record.push_back(Version.getMajor());
2962 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2963 Record.push_back(*Minor + 1);
2964 else
2965 Record.push_back(0);
2966 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2967 Record.push_back(*Subminor + 1);
2968 else
2969 Record.push_back(0);
2970}
2971
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002972/// \brief Note that the identifier II occurs at the given offset
2973/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002974void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002975 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002976 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002977 // up earlier in the chain and thus don't need an offset.
2978 if (ID >= FirstIdentID)
2979 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002980}
2981
Douglas Gregor83941df2009-04-25 17:48:32 +00002982/// \brief Note that the selector Sel occurs at the given offset
2983/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002984void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002985 unsigned ID = SelectorIDs[Sel];
2986 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002987 // Don't record offsets for selectors that are also available in a different
2988 // file.
2989 if (ID < FirstSelectorID)
2990 return;
2991 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002992}
2993
Sebastian Redla4232eb2010-08-18 23:56:21 +00002994ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00002995 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
2996 WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002997 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002998 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002999 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003000 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3001 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003002 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003003 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003004 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003005 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003006 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003007 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003008 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3009 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3010 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003011 DeclTypedefAbbrev(0),
3012 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3013 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003014{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003015}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003016
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003017ASTWriter::~ASTWriter() {
3018 for (FileDeclIDsTy::iterator
3019 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3020 delete I->second;
3021}
3022
Sebastian Redla4232eb2010-08-18 23:56:21 +00003023void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003024 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003025 Module *WritingModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003026 WritingAST = true;
3027
Douglas Gregor2cf26342009-04-09 22:27:44 +00003028 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003029 Stream.Emit((unsigned)'C', 8);
3030 Stream.Emit((unsigned)'P', 8);
3031 Stream.Emit((unsigned)'C', 8);
3032 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003034 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003035
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003036 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003037 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003038 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003039 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003040 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003041 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003042 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003043
3044 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003045}
3046
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003047template<typename Vector>
3048static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3049 ASTWriter::RecordData &Record) {
3050 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3051 I != E; ++I) {
3052 Writer.AddDeclRef(*I, Record);
3053 }
3054}
3055
Sebastian Redla4232eb2010-08-18 23:56:21 +00003056void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003057 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003058 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003059 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003060 using namespace llvm;
3061
Douglas Gregorecc2c092011-12-01 22:20:10 +00003062 // Make sure that the AST reader knows to finalize itself.
3063 if (Chain)
3064 Chain->finalizeForWriting();
3065
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003066 ASTContext &Context = SemaRef.Context;
3067 Preprocessor &PP = SemaRef.PP;
3068
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003069 // Set up predefined declaration IDs.
3070 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003071 if (Context.ObjCIdDecl)
3072 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003073 if (Context.ObjCSelDecl)
3074 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003075 if (Context.ObjCClassDecl)
3076 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003077 if (Context.Int128Decl)
3078 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3079 if (Context.UInt128Decl)
3080 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003081 if (Context.ObjCInstanceTypeDecl)
3082 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003083
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003084 if (!Chain) {
3085 // Make sure that we emit IdentifierInfos (and any attached
3086 // declarations) for builtins. We don't need to do this when we're
3087 // emitting chained PCH files, because all of the builtins will be
3088 // in the original PCH file.
3089 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003090 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003091 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003092 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
3093 Context.getLangOptions().NoBuiltin);
3094 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3095 getIdentifierRef(&Table.get(BuiltinNames[I]));
3096 }
3097
Douglas Gregoreee242f2011-10-27 09:33:13 +00003098 // If there are any out-of-date identifiers, bring them up to date.
3099 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3100 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3101 IDEnd = PP.getIdentifierTable().end();
3102 ID != IDEnd; ++ID)
3103 if (ID->second->isOutOfDate())
3104 ExtSource->updateOutOfDateIdentifier(*ID->second);
3105 }
3106
Chris Lattner63d65f82009-09-08 18:19:27 +00003107 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003108 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003109 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003110 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003111 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003112
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003113 // Build a record containing all of the file scoped decls in this file.
3114 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003115 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3116 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003117
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003118 // Build a record containing all of the delegating constructors we still need
3119 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003120 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003121 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003122
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003123 // Write the set of weak, undeclared identifiers. We always write the
3124 // entire table, since later PCH files in a PCH chain are only interested in
3125 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003126 RecordData WeakUndeclaredIdentifiers;
3127 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003128 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003129 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3130 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3131 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3132 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3133 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3134 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3135 }
3136 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003137
Douglas Gregor14c22f22009-04-22 22:18:58 +00003138 // Build a record containing all of the locally-scoped external
3139 // declarations in this header file. Generally, this record will be
3140 // empty.
3141 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003142 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003143 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003144 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003145 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3146 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003147 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003148 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003149 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3150 }
3151
Douglas Gregorb81c1702009-04-27 20:06:05 +00003152 // Build a record containing all of the ext_vector declarations.
3153 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003154 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003155
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003156 // Build a record containing all of the VTable uses information.
3157 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003158 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003159 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3160 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3161 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3162 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3163 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003164 }
3165
3166 // Build a record containing all of dynamic classes declarations.
3167 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003168 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003169
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003170 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003171 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003172 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003173 I = SemaRef.PendingInstantiations.begin(),
3174 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3175 AddDeclRef(I->first, PendingInstantiations);
3176 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003177 }
3178 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3179 "There are local ones at end of translation unit!");
3180
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003181 // Build a record containing some declaration references.
3182 RecordData SemaDeclRefs;
3183 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3184 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3185 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3186 }
3187
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003188 RecordData CUDASpecialDeclRefs;
3189 if (Context.getcudaConfigureCallDecl()) {
3190 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3191 }
3192
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003193 // Build a record containing all of the known namespaces.
3194 RecordData KnownNamespaces;
3195 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3196 I = SemaRef.KnownNamespaces.begin(),
3197 IEnd = SemaRef.KnownNamespaces.end();
3198 I != IEnd; ++I) {
3199 if (!I->second)
3200 AddDeclRef(I->first, KnownNamespaces);
3201 }
3202
Sebastian Redl3397c552010-08-18 23:56:27 +00003203 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003204 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003205 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003206 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003207 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00003208 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003209 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003210
3211 // Create a lexical update block containing all of the declarations in the
3212 // translation unit that do not come from other AST files.
3213 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3214 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3215 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3216 E = TU->noload_decls_end();
3217 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003218 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003219 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003220 }
3221
3222 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3223 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3224 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3225 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3226 Record.clear();
3227 Record.push_back(TU_UPDATE_LEXICAL);
3228 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3229 data(NewGlobalDecls));
3230
3231 // And a visible updates block for the translation unit.
3232 Abv = new llvm::BitCodeAbbrev();
3233 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3234 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3235 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3236 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3237 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3238 WriteDeclContextVisibleUpdate(TU);
3239
3240 // If the translation unit has an anonymous namespace, and we don't already
3241 // have an update block for it, write it as an update block.
3242 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3243 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3244 if (Record.empty()) {
3245 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003246 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003247 }
3248 }
3249
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003250 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003251 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003252
Douglas Gregora119da02011-08-02 16:26:37 +00003253 // Form the record of special types.
3254 RecordData SpecialTypes;
3255 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor30403a62011-08-11 22:04:35 +00003256 AddTypeRef(Context.ObjCProtoType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003257 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003258 AddTypeRef(Context.getFILEType(), SpecialTypes);
3259 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3260 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3261 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3262 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003263 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003264 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003265
Douglas Gregor366809a2009-04-26 03:49:13 +00003266 // Keep writing types and declarations until all types and
3267 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003268 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003269 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003270 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3271 E = DeclsToRewrite.end();
3272 I != E; ++I)
3273 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003274 while (!DeclTypesToEmit.empty()) {
3275 DeclOrType DOT = DeclTypesToEmit.front();
3276 DeclTypesToEmit.pop();
3277 if (DOT.isType())
3278 WriteType(DOT.getType());
3279 else
3280 WriteDecl(Context, DOT.getDecl());
3281 }
3282 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003283
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003284 WriteFileDeclIDsMap();
3285 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3286
3287 if (Chain) {
3288 // Write the mapping information describing our module dependencies and how
3289 // each of those modules were mapped into our own offset/ID space, so that
3290 // the reader can build the appropriate mapping to its own offset/ID space.
3291 // The map consists solely of a blob with the following format:
3292 // *(module-name-len:i16 module-name:len*i8
3293 // source-location-offset:i32
3294 // identifier-id:i32
3295 // preprocessed-entity-id:i32
3296 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003297 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003298 // selector-id:i32
3299 // declaration-id:i32
3300 // c++-base-specifiers-id:i32
3301 // type-id:i32)
3302 //
3303 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3304 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3306 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
3307 llvm::SmallString<2048> Buffer;
3308 {
3309 llvm::raw_svector_ostream Out(Buffer);
3310 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003311 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003312 M != MEnd; ++M) {
3313 StringRef FileName = (*M)->FileName;
3314 io::Emit16(Out, FileName.size());
3315 Out.write(FileName.data(), FileName.size());
3316 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3317 io::Emit32(Out, (*M)->BaseIdentifierID);
3318 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003319 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003320 io::Emit32(Out, (*M)->BaseSelectorID);
3321 io::Emit32(Out, (*M)->BaseDeclID);
3322 io::Emit32(Out, (*M)->BaseTypeIndex);
3323 }
3324 }
3325 Record.clear();
3326 Record.push_back(MODULE_OFFSET_MAP);
3327 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3328 Buffer.data(), Buffer.size());
3329 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003330 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003331 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003332 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003333 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003334 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003335 WriteFPPragmaOptions(SemaRef.getFPOptions());
3336 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003337
Sebastian Redl1476ed42010-07-16 16:36:56 +00003338 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003339 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003340
Anders Carlssonc8505782011-03-06 18:41:18 +00003341 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003342
Douglas Gregore209e502011-12-06 01:10:29 +00003343 // If we're emitting a module, write out the submodule information.
3344 if (WritingModule)
3345 WriteSubmodules(WritingModule);
3346
Douglas Gregora119da02011-08-02 16:26:37 +00003347 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3348
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003349 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003350 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003351 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003352
3353 // Write the record containing tentative definitions.
3354 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003355 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003356
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003357 // Write the record containing unused file scoped decls.
3358 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003359 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003360
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003361 // Write the record containing weak undeclared identifiers.
3362 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003363 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003364 WeakUndeclaredIdentifiers);
3365
Douglas Gregor14c22f22009-04-22 22:18:58 +00003366 // Write the record containing locally-scoped external definitions.
3367 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003368 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003369 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003370
3371 // Write the record containing ext_vector type names.
3372 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003373 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003374
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003375 // Write the record containing VTable uses information.
3376 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003377 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003378
3379 // Write the record containing dynamic classes declarations.
3380 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003381 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003382
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003383 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003384 if (!PendingInstantiations.empty())
3385 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003386
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003387 // Write the record containing declaration references of Sema.
3388 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003389 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003390
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003391 // Write the record containing CUDA-specific declaration references.
3392 if (!CUDASpecialDeclRefs.empty())
3393 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003394
3395 // Write the delegating constructors.
3396 if (!DelegatingCtorDecls.empty())
3397 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003398
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003399 // Write the known namespaces.
3400 if (!KnownNamespaces.empty())
3401 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3402
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003403 // Write the visible updates to DeclContexts.
3404 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3405 I = UpdatedDeclContexts.begin(),
3406 E = UpdatedDeclContexts.end();
3407 I != E; ++I)
3408 WriteDeclContextVisibleUpdate(*I);
3409
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003410 if (!WritingModule) {
3411 // Write the submodules that were imported, if any.
3412 RecordData ImportedModules;
3413 for (ASTContext::import_iterator I = Context.local_import_begin(),
3414 IEnd = Context.local_import_end();
3415 I != IEnd; ++I) {
3416 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3417 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3418 }
3419 if (!ImportedModules.empty()) {
3420 // Sort module IDs.
3421 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3422
3423 // Unique module IDs.
3424 ImportedModules.erase(std::unique(ImportedModules.begin(),
3425 ImportedModules.end()),
3426 ImportedModules.end());
3427
3428 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3429 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003430 }
3431
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003432 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003433 WriteDeclReplacementsBlock();
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003434 WriteChainedObjCCategories();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003435 WriteMergedDecls();
3436
Douglas Gregora1be2782011-12-17 23:38:30 +00003437 if (!LocalRedeclarations.empty()) {
3438 // Sort the local redeclarations info by the first declaration ID,
3439 // since the reader will be perforing binary searches on this information.
3440 llvm::array_pod_sort(LocalRedeclarations.begin(),LocalRedeclarations.end());
3441
3442 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3443 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS));
3444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3446 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3447
3448 Record.clear();
3449 Record.push_back(LOCAL_REDECLARATIONS);
3450 Record.push_back(LocalRedeclarations.size());
3451 Stream.EmitRecordWithBlob(AbbrevID, Record,
3452 reinterpret_cast<char*>(LocalRedeclarations.data()),
3453 LocalRedeclarations.size() * sizeof(LocalRedeclarationsInfo));
3454 }
3455
Douglas Gregor3e1af842009-04-17 22:13:46 +00003456 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003457 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003458 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003459 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003460 Record.push_back(NumLexicalDeclContexts);
3461 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003462 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003463 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003464}
3465
Douglas Gregor61c5e342011-09-17 00:05:03 +00003466/// \brief Go through the declaration update blocks and resolve declaration
3467/// pointers into declaration IDs.
3468void ASTWriter::ResolveDeclUpdatesBlocks() {
3469 for (DeclUpdateMap::iterator
3470 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3471 const Decl *D = I->first;
3472 UpdateRecord &URec = I->second;
3473
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003474 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003475 continue; // The decl will be written completely
3476
3477 unsigned Idx = 0, N = URec.size();
3478 while (Idx < N) {
3479 switch ((DeclUpdateKind)URec[Idx++]) {
3480 case UPD_CXX_SET_DEFINITIONDATA:
3481 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3482 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3483 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Douglas Gregor53df7a12011-12-15 18:03:09 +00003484 case UPD_OBJC_SET_CLASS_DEFINITIONDATA:
Douglas Gregor1d784b22012-01-01 19:51:50 +00003485 case UPD_OBJC_SET_PROTOCOL_DEFINITIONDATA:
Douglas Gregor61c5e342011-09-17 00:05:03 +00003486 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3487 ++Idx;
3488 break;
3489
3490 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3491 ++Idx;
3492 break;
3493 }
3494 }
3495 }
3496}
3497
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003498void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003499 if (DeclUpdates.empty())
3500 return;
3501
3502 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003503 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003504 for (DeclUpdateMap::iterator
3505 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3506 const Decl *D = I->first;
3507 UpdateRecord &URec = I->second;
3508
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003509 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003510 continue; // The decl will be written completely,no need to store updates.
3511
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003512 uint64_t Offset = Stream.GetCurrentBitNo();
3513 Stream.EmitRecord(DECL_UPDATES, URec);
3514
3515 OffsetsRecord.push_back(GetDeclRef(D));
3516 OffsetsRecord.push_back(Offset);
3517 }
3518 Stream.ExitBlock();
3519 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3520}
3521
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003522void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003523 if (ReplacedDecls.empty())
3524 return;
3525
3526 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003527 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003528 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003529 Record.push_back(I->ID);
3530 Record.push_back(I->Offset);
3531 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003532 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003533 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003534}
3535
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003536void ASTWriter::WriteChainedObjCCategories() {
3537 if (LocalChainedObjCCategories.empty())
3538 return;
3539
3540 RecordData Record;
3541 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3542 I = LocalChainedObjCCategories.begin(),
3543 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3544 ChainedObjCCategoriesData &Data = *I;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003545 if (isRewritten(Data.Interface))
3546 continue;
3547
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003548 assert(Data.Interface->getCategoryList());
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003549 serialization::DeclID
3550 HeadCatID = getDeclID(Data.Interface->getCategoryList());
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003551
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003552 Record.push_back(getDeclID(Data.Interface));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003553 Record.push_back(HeadCatID);
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003554 Record.push_back(getDeclID(Data.TailCategory));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003555 }
3556 Stream.EmitRecord(OBJC_CHAINED_CATEGORIES, Record);
3557}
3558
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003559void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003560 Record.push_back(Loc.getRawEncoding());
3561}
3562
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003563void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003564 AddSourceLocation(Range.getBegin(), Record);
3565 AddSourceLocation(Range.getEnd(), Record);
3566}
3567
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003568void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003569 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003570 const uint64_t *Words = Value.getRawData();
3571 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003572}
3573
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003574void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003575 Record.push_back(Value.isUnsigned());
3576 AddAPInt(Value, Record);
3577}
3578
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003579void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003580 AddAPInt(Value.bitcastToAPInt(), Record);
3581}
3582
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003583void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003584 Record.push_back(getIdentifierRef(II));
3585}
3586
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003587IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003588 if (II == 0)
3589 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003590
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003591 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003592 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003593 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003594 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003595}
3596
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003597void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003598 Record.push_back(getSelectorRef(SelRef));
3599}
3600
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003601SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003602 if (Sel.getAsOpaquePtr() == 0) {
3603 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003604 }
3605
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003606 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003607 if (SID == 0 && Chain) {
3608 // This might trigger a ReadSelector callback, which will set the ID for
3609 // this selector.
3610 Chain->LoadSelector(Sel);
3611 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003612 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003613 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003614 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003615 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003616}
3617
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003618void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003619 AddDeclRef(Temp->getDestructor(), Record);
3620}
3621
Douglas Gregor7c789c12010-10-29 22:39:52 +00003622void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3623 CXXBaseSpecifier const *BasesEnd,
3624 RecordDataImpl &Record) {
3625 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3626 CXXBaseSpecifiersToWrite.push_back(
3627 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3628 Bases, BasesEnd));
3629 Record.push_back(NextCXXBaseSpecifiersID++);
3630}
3631
Sebastian Redla4232eb2010-08-18 23:56:21 +00003632void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003633 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003634 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003635 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003636 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003637 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003638 break;
3639 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003640 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003641 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003642 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003643 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003644 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003645 break;
3646 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003647 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003648 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003649 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003650 break;
John McCall833ca992009-10-29 08:12:44 +00003651 case TemplateArgument::Null:
3652 case TemplateArgument::Integral:
3653 case TemplateArgument::Declaration:
3654 case TemplateArgument::Pack:
3655 break;
3656 }
3657}
3658
Sebastian Redla4232eb2010-08-18 23:56:21 +00003659void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003660 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003661 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003662
3663 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3664 bool InfoHasSameExpr
3665 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3666 Record.push_back(InfoHasSameExpr);
3667 if (InfoHasSameExpr)
3668 return; // Avoid storing the same expr twice.
3669 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003670 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3671 Record);
3672}
3673
Douglas Gregordc355712011-02-25 00:36:19 +00003674void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3675 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003676 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003677 AddTypeRef(QualType(), Record);
3678 return;
3679 }
3680
Douglas Gregordc355712011-02-25 00:36:19 +00003681 AddTypeLoc(TInfo->getTypeLoc(), Record);
3682}
3683
3684void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3685 AddTypeRef(TL.getType(), Record);
3686
John McCalla1ee0c52009-10-16 21:56:05 +00003687 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003688 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003689 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003690}
3691
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003692void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003693 Record.push_back(GetOrCreateTypeID(T));
3694}
3695
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003696TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3697 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003698 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3699}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003700
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003701TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003702 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003703 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003704}
3705
3706TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3707 if (T.isNull())
3708 return TypeIdx();
3709 assert(!T.getLocalFastQualifiers());
3710
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003711 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003712 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003713 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003714 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003715 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003716 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003717 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003718 return Idx;
3719}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003720
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003721TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003722 if (T.isNull())
3723 return TypeIdx();
3724 assert(!T.getLocalFastQualifiers());
3725
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003726 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3727 assert(I != TypeIdxs.end() && "Type not emitted!");
3728 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003729}
3730
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003731void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003732 Record.push_back(GetDeclRef(D));
3733}
3734
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003735DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003736 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3737
Douglas Gregor2cf26342009-04-09 22:27:44 +00003738 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003739 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003740 }
Douglas Gregor97475832010-10-05 18:37:06 +00003741 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003742 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003743 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003744 // We haven't seen this declaration before. Give it a new ID and
3745 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003746 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003747 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003748 }
3749
Sebastian Redl681d7232010-07-27 00:17:23 +00003750 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003751}
3752
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003753DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003754 if (D == 0)
3755 return 0;
3756
3757 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3758 return DeclIDs[D];
3759}
3760
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003761static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3762 std::pair<unsigned, serialization::DeclID> R) {
3763 return L.first < R.first;
3764}
3765
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003766void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003767 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003768 assert(D);
3769
3770 SourceLocation Loc = D->getLocation();
3771 if (Loc.isInvalid())
3772 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003773
3774 // We only keep track of the file-level declarations of each file.
3775 if (!D->getLexicalDeclContext()->isFileContext())
3776 return;
3777
3778 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003779 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003780 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003781 FileID FID;
3782 unsigned Offset;
3783 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003784 if (FID.isInvalid())
3785 return;
3786 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3787 assert(Entry->isFile());
3788
3789 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3790 if (!Info)
3791 Info = new DeclIDInFileInfo();
3792
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003793 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003794 LocDeclIDsTy &Decls = Info->DeclIDs;
3795
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003796 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003797 Decls.push_back(LocDecl);
3798 return;
3799 }
3800
3801 LocDeclIDsTy::iterator
3802 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3803
3804 Decls.insert(I, LocDecl);
3805}
3806
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003807void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003808 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003809 Record.push_back(Name.getNameKind());
3810 switch (Name.getNameKind()) {
3811 case DeclarationName::Identifier:
3812 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3813 break;
3814
3815 case DeclarationName::ObjCZeroArgSelector:
3816 case DeclarationName::ObjCOneArgSelector:
3817 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003818 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003819 break;
3820
3821 case DeclarationName::CXXConstructorName:
3822 case DeclarationName::CXXDestructorName:
3823 case DeclarationName::CXXConversionFunctionName:
3824 AddTypeRef(Name.getCXXNameType(), Record);
3825 break;
3826
3827 case DeclarationName::CXXOperatorName:
3828 Record.push_back(Name.getCXXOverloadedOperator());
3829 break;
3830
Sean Hunt3e518bd2009-11-29 07:34:05 +00003831 case DeclarationName::CXXLiteralOperatorName:
3832 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3833 break;
3834
Douglas Gregor2cf26342009-04-09 22:27:44 +00003835 case DeclarationName::CXXUsingDirective:
3836 // No extra data to emit
3837 break;
3838 }
3839}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003840
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003841void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003842 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003843 switch (Name.getNameKind()) {
3844 case DeclarationName::CXXConstructorName:
3845 case DeclarationName::CXXDestructorName:
3846 case DeclarationName::CXXConversionFunctionName:
3847 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3848 break;
3849
3850 case DeclarationName::CXXOperatorName:
3851 AddSourceLocation(
3852 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3853 Record);
3854 AddSourceLocation(
3855 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3856 Record);
3857 break;
3858
3859 case DeclarationName::CXXLiteralOperatorName:
3860 AddSourceLocation(
3861 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3862 Record);
3863 break;
3864
3865 case DeclarationName::Identifier:
3866 case DeclarationName::ObjCZeroArgSelector:
3867 case DeclarationName::ObjCOneArgSelector:
3868 case DeclarationName::ObjCMultiArgSelector:
3869 case DeclarationName::CXXUsingDirective:
3870 break;
3871 }
3872}
3873
3874void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003875 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003876 AddDeclarationName(NameInfo.getName(), Record);
3877 AddSourceLocation(NameInfo.getLoc(), Record);
3878 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3879}
3880
3881void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003882 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003883 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003884 Record.push_back(Info.NumTemplParamLists);
3885 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3886 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3887}
3888
Sebastian Redla4232eb2010-08-18 23:56:21 +00003889void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003890 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003891 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003892 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003893 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003894
3895 // Push each of the NNS's onto a stack for serialization in reverse order.
3896 while (NNS) {
3897 NestedNames.push_back(NNS);
3898 NNS = NNS->getPrefix();
3899 }
3900
3901 Record.push_back(NestedNames.size());
3902 while(!NestedNames.empty()) {
3903 NNS = NestedNames.pop_back_val();
3904 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3905 Record.push_back(Kind);
3906 switch (Kind) {
3907 case NestedNameSpecifier::Identifier:
3908 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3909 break;
3910
3911 case NestedNameSpecifier::Namespace:
3912 AddDeclRef(NNS->getAsNamespace(), Record);
3913 break;
3914
Douglas Gregor14aba762011-02-24 02:36:08 +00003915 case NestedNameSpecifier::NamespaceAlias:
3916 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3917 break;
3918
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003919 case NestedNameSpecifier::TypeSpec:
3920 case NestedNameSpecifier::TypeSpecWithTemplate:
3921 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3922 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3923 break;
3924
3925 case NestedNameSpecifier::Global:
3926 // Don't need to write an associated value.
3927 break;
3928 }
3929 }
3930}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003931
Douglas Gregordc355712011-02-25 00:36:19 +00003932void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3933 RecordDataImpl &Record) {
3934 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003935 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003936 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00003937
3938 // Push each of the nested-name-specifiers's onto a stack for
3939 // serialization in reverse order.
3940 while (NNS) {
3941 NestedNames.push_back(NNS);
3942 NNS = NNS.getPrefix();
3943 }
3944
3945 Record.push_back(NestedNames.size());
3946 while(!NestedNames.empty()) {
3947 NNS = NestedNames.pop_back_val();
3948 NestedNameSpecifier::SpecifierKind Kind
3949 = NNS.getNestedNameSpecifier()->getKind();
3950 Record.push_back(Kind);
3951 switch (Kind) {
3952 case NestedNameSpecifier::Identifier:
3953 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3954 AddSourceRange(NNS.getLocalSourceRange(), Record);
3955 break;
3956
3957 case NestedNameSpecifier::Namespace:
3958 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3959 AddSourceRange(NNS.getLocalSourceRange(), Record);
3960 break;
3961
3962 case NestedNameSpecifier::NamespaceAlias:
3963 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3964 AddSourceRange(NNS.getLocalSourceRange(), Record);
3965 break;
3966
3967 case NestedNameSpecifier::TypeSpec:
3968 case NestedNameSpecifier::TypeSpecWithTemplate:
3969 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3970 AddTypeLoc(NNS.getTypeLoc(), Record);
3971 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3972 break;
3973
3974 case NestedNameSpecifier::Global:
3975 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3976 break;
3977 }
3978 }
3979}
3980
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003981void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003982 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003983 Record.push_back(Kind);
3984 switch (Kind) {
3985 case TemplateName::Template:
3986 AddDeclRef(Name.getAsTemplateDecl(), Record);
3987 break;
3988
3989 case TemplateName::OverloadedTemplate: {
3990 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3991 Record.push_back(OvT->size());
3992 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3993 I != E; ++I)
3994 AddDeclRef(*I, Record);
3995 break;
3996 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003997
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003998 case TemplateName::QualifiedTemplate: {
3999 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4000 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4001 Record.push_back(QualT->hasTemplateKeyword());
4002 AddDeclRef(QualT->getTemplateDecl(), Record);
4003 break;
4004 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004005
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004006 case TemplateName::DependentTemplate: {
4007 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4008 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4009 Record.push_back(DepT->isIdentifier());
4010 if (DepT->isIdentifier())
4011 AddIdentifierRef(DepT->getIdentifier(), Record);
4012 else
4013 Record.push_back(DepT->getOperator());
4014 break;
4015 }
John McCall14606042011-06-30 08:33:18 +00004016
4017 case TemplateName::SubstTemplateTemplateParm: {
4018 SubstTemplateTemplateParmStorage *subst
4019 = Name.getAsSubstTemplateTemplateParm();
4020 AddDeclRef(subst->getParameter(), Record);
4021 AddTemplateName(subst->getReplacement(), Record);
4022 break;
4023 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004024
4025 case TemplateName::SubstTemplateTemplateParmPack: {
4026 SubstTemplateTemplateParmPackStorage *SubstPack
4027 = Name.getAsSubstTemplateTemplateParmPack();
4028 AddDeclRef(SubstPack->getParameterPack(), Record);
4029 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4030 break;
4031 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004032 }
4033}
4034
Michael J. Spencer20249a12010-10-21 03:16:25 +00004035void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004036 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004037 Record.push_back(Arg.getKind());
4038 switch (Arg.getKind()) {
4039 case TemplateArgument::Null:
4040 break;
4041 case TemplateArgument::Type:
4042 AddTypeRef(Arg.getAsType(), Record);
4043 break;
4044 case TemplateArgument::Declaration:
4045 AddDeclRef(Arg.getAsDecl(), Record);
4046 break;
4047 case TemplateArgument::Integral:
4048 AddAPSInt(*Arg.getAsIntegral(), Record);
4049 AddTypeRef(Arg.getIntegralType(), Record);
4050 break;
4051 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004052 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4053 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004054 case TemplateArgument::TemplateExpansion:
4055 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004056 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4057 Record.push_back(*NumExpansions + 1);
4058 else
4059 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004060 break;
4061 case TemplateArgument::Expression:
4062 AddStmt(Arg.getAsExpr());
4063 break;
4064 case TemplateArgument::Pack:
4065 Record.push_back(Arg.pack_size());
4066 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4067 I != E; ++I)
4068 AddTemplateArgument(*I, Record);
4069 break;
4070 }
4071}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004072
4073void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004074ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004075 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004076 assert(TemplateParams && "No TemplateParams!");
4077 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4078 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4079 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4080 Record.push_back(TemplateParams->size());
4081 for (TemplateParameterList::const_iterator
4082 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4083 P != PEnd; ++P)
4084 AddDeclRef(*P, Record);
4085}
4086
4087/// \brief Emit a template argument list.
4088void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004089ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004090 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004091 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004092 Record.push_back(TemplateArgs->size());
4093 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004094 AddTemplateArgument(TemplateArgs->get(i), Record);
4095}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004096
4097
4098void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004099ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004100 Record.push_back(Set.size());
4101 for (UnresolvedSetImpl::const_iterator
4102 I = Set.begin(), E = Set.end(); I != E; ++I) {
4103 AddDeclRef(I.getDecl(), Record);
4104 Record.push_back(I.getAccess());
4105 }
4106}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004107
Sebastian Redla4232eb2010-08-18 23:56:21 +00004108void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004109 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004110 Record.push_back(Base.isVirtual());
4111 Record.push_back(Base.isBaseOfClass());
4112 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004113 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004114 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004115 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004116 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4117 : SourceLocation(),
4118 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004119}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004120
Douglas Gregor7c789c12010-10-29 22:39:52 +00004121void ASTWriter::FlushCXXBaseSpecifiers() {
4122 RecordData Record;
4123 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4124 Record.clear();
4125
4126 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004127 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004128 if (Index == CXXBaseSpecifiersOffsets.size())
4129 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4130 else {
4131 if (Index > CXXBaseSpecifiersOffsets.size())
4132 CXXBaseSpecifiersOffsets.resize(Index + 1);
4133 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4134 }
4135
4136 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4137 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4138 Record.push_back(BEnd - B);
4139 for (; B != BEnd; ++B)
4140 AddCXXBaseSpecifier(*B, Record);
4141 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004142
4143 // Flush any expressions that were written as part of the base specifiers.
4144 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004145 }
4146
4147 CXXBaseSpecifiersToWrite.clear();
4148}
4149
Sean Huntcbb67482011-01-08 20:30:50 +00004150void ASTWriter::AddCXXCtorInitializers(
4151 const CXXCtorInitializer * const *CtorInitializers,
4152 unsigned NumCtorInitializers,
4153 RecordDataImpl &Record) {
4154 Record.push_back(NumCtorInitializers);
4155 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4156 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004157
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004158 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004159 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004160 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004161 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004162 } else if (Init->isDelegatingInitializer()) {
4163 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004164 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004165 } else if (Init->isMemberInitializer()){
4166 Record.push_back(CTOR_INITIALIZER_MEMBER);
4167 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004168 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004169 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4170 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004171 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004172
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004173 AddSourceLocation(Init->getMemberLocation(), Record);
4174 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004175 AddSourceLocation(Init->getLParenLoc(), Record);
4176 AddSourceLocation(Init->getRParenLoc(), Record);
4177 Record.push_back(Init->isWritten());
4178 if (Init->isWritten()) {
4179 Record.push_back(Init->getSourceOrder());
4180 } else {
4181 Record.push_back(Init->getNumArrayIndices());
4182 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4183 AddDeclRef(Init->getArrayIndex(i), Record);
4184 }
4185 }
4186}
4187
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004188void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4189 assert(D->DefinitionData);
4190 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
4191 Record.push_back(Data.UserDeclaredConstructor);
4192 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004193 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004194 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004195 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004196 Record.push_back(Data.UserDeclaredDestructor);
4197 Record.push_back(Data.Aggregate);
4198 Record.push_back(Data.PlainOldData);
4199 Record.push_back(Data.Empty);
4200 Record.push_back(Data.Polymorphic);
4201 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004202 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004203 Record.push_back(Data.HasNoNonEmptyBases);
4204 Record.push_back(Data.HasPrivateFields);
4205 Record.push_back(Data.HasProtectedFields);
4206 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004207 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00004208 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004209 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004210 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004211 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004212 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004213 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004214 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004215 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004216 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004217 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004218 Record.push_back(Data.DeclaredDefaultConstructor);
4219 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004220 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004221 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004222 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004223 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004224 Record.push_back(Data.FailedImplicitMoveConstructor);
4225 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004226
4227 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004228 if (Data.NumBases > 0)
4229 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4230 Record);
4231
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004232 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4233 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004234 if (Data.NumVBases > 0)
4235 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4236 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004237
4238 AddUnresolvedSet(Data.Conversions, Record);
4239 AddUnresolvedSet(Data.VisibleConversions, Record);
4240 // Data.Definition is the owning decl, no need to write it.
4241 AddDeclRef(Data.FirstFriend, Record);
4242}
4243
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004244void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004245 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004246 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004247 assert(FirstDeclID == NextDeclID &&
4248 FirstTypeID == NextTypeID &&
4249 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004250 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004251 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004252 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004253
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004254 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004255
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004256 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4257 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4258 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004259 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004260 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004261 NextDeclID = FirstDeclID;
4262 NextTypeID = FirstTypeID;
4263 NextIdentID = FirstIdentID;
4264 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004265 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004266}
4267
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004268void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004269 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004270 if (II->hasMacroDefinition())
4271 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004272}
4273
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004274void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004275 // Always take the highest-numbered type index. This copes with an interesting
4276 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004277 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004278 // keep the higher-numbered entry so that we can properly write it out to
4279 // the AST file.
4280 TypeIdx &StoredIdx = TypeIdxs[T];
4281 if (Idx.getIndex() >= StoredIdx.getIndex())
4282 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004283}
4284
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004285void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00004286 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004287}
Sebastian Redl5d050072010-08-04 17:20:04 +00004288
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004289void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004290 SelectorIDs[S] = ID;
4291}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004292
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004293void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004294 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004295 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004296 MacroDefinitions[MD] = ID;
4297}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004298
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004299void ASTWriter::MacroVisible(IdentifierInfo *II) {
4300 DeserializedMacroNames.push_back(II);
4301}
4302
Douglas Gregora015cab2011-12-02 17:30:13 +00004303void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4304 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4305 SubmoduleIDs[Mod] = ID;
4306}
4307
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004308void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004309 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004310 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004311 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4312 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004313 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004314 // A forward reference was mutated into a definition. Rewrite it.
4315 // FIXME: This happens during template instantiation, should we
4316 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004317 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004318 }
4319
4320 for (CXXRecordDecl::redecl_iterator
4321 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
4322 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
4323 if (Redecl == RD)
4324 continue;
4325
4326 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004327 if (Redecl->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004328 UpdateRecord &Record = DeclUpdates[Redecl];
4329 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
4330 assert(Redecl->DefinitionData);
4331 assert(Redecl->DefinitionData->Definition == D);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004332 Record.push_back(reinterpret_cast<uint64_t>(D)); // the DefinitionDecl
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004333 }
4334 }
4335 }
4336}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004337void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004338 assert(!WritingAST && "Already writing the AST!");
4339
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004340 // TU and namespaces are handled elsewhere.
4341 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4342 return;
4343
Douglas Gregor919814d2011-09-09 23:01:35 +00004344 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004345 return; // Not a source decl added to a DeclContext from PCH.
4346
4347 AddUpdatedDeclContext(DC);
4348}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004349
4350void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004351 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004352 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004353 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004354 return; // Not a source member added to a class from PCH.
4355 if (!isa<CXXMethodDecl>(D))
4356 return; // We are interested in lazily declared implicit methods.
4357
4358 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004359 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004360 UpdateRecord &Record = DeclUpdates[RD];
4361 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004362 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004363}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004364
4365void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4366 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004367 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004368 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004369 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004370 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004371 return; // Not a source specialization added to a template from PCH.
4372
4373 UpdateRecord &Record = DeclUpdates[TD];
4374 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004375 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004376}
Douglas Gregor89d99802010-11-30 06:16:57 +00004377
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004378void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4379 const FunctionDecl *D) {
4380 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004381 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004382 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004383 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004384 return; // Not a source specialization added to a template from PCH.
4385
4386 UpdateRecord &Record = DeclUpdates[TD];
4387 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004388 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004389}
4390
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004391void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004392 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004393 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004394 return; // Declaration not imported from PCH.
4395
4396 // Implicit decl from a PCH was defined.
4397 // FIXME: Should implicit definition be a separate FunctionDecl?
4398 RewriteDecl(D);
4399}
4400
Sebastian Redlf79a7192011-04-29 08:19:30 +00004401void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004402 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004403 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004404 return;
4405
4406 // Since the actual instantiation is delayed, this really means that we need
4407 // to update the instantiation location.
4408 UpdateRecord &Record = DeclUpdates[D];
4409 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4410 AddSourceLocation(
4411 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4412}
4413
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004414void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4415 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004416 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004417 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004418 return; // Declaration not imported from PCH.
4419 if (CatD->getNextClassCategory() &&
Douglas Gregor919814d2011-09-09 23:01:35 +00004420 !CatD->getNextClassCategory()->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004421 return; // We already recorded that the tail of a category chain should be
4422 // attached to an interface.
4423
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004424 ChainedObjCCategoriesData Data = { IFD, CatD };
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004425 LocalChainedObjCCategories.push_back(Data);
4426}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004427
4428void ASTWriter::CompletedObjCForwardRef(const ObjCContainerDecl *D) {
4429 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004430
Douglas Gregor53df7a12011-12-15 18:03:09 +00004431 if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4432 for (ObjCInterfaceDecl::redecl_iterator I = ID->redecls_begin(),
4433 E = ID->redecls_end();
4434 I != E; ++I) {
4435 if (*I == ID)
4436 continue;
4437
4438 // We are interested when a PCH decl is modified.
4439 if (I->isFromASTFile()) {
4440 UpdateRecord &Record = DeclUpdates[*I];
4441 Record.push_back(UPD_OBJC_SET_CLASS_DEFINITIONDATA);
4442 assert((*I)->hasDefinition());
4443 assert((*I)->getDefinition() == D);
4444 Record.push_back(reinterpret_cast<uint64_t>(D)); // the DefinitionDecl
4445 }
4446 }
4447 }
Douglas Gregor1d784b22012-01-01 19:51:50 +00004448
4449 if (const ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4450 for (ObjCProtocolDecl::redecl_iterator I = PD->redecls_begin(),
4451 E = PD->redecls_end();
4452 I != E; ++I) {
4453 if (*I == PD)
4454 continue;
4455
4456 // We are interested when a PCH decl is modified.
4457 if (I->isFromASTFile()) {
4458 UpdateRecord &Record = DeclUpdates[*I];
4459 Record.push_back(UPD_OBJC_SET_PROTOCOL_DEFINITIONDATA);
4460 assert((*I)->hasDefinition());
4461 assert((*I)->getDefinition() == D);
4462 Record.push_back(reinterpret_cast<uint64_t>(D)); // the DefinitionDecl
4463 }
4464 }
4465 }
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004466}
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004467
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004468void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4469 const ObjCPropertyDecl *OrigProp,
4470 const ObjCCategoryDecl *ClassExt) {
4471 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4472 if (!D)
4473 return;
4474
4475 assert(!WritingAST && "Already writing the AST!");
4476 if (!D->isFromASTFile())
4477 return; // Declaration not imported from PCH.
4478
4479 RewriteDecl(D);
4480}
4481