blob: 838ec35173f900e115435903f5faf4e605d9e125 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Sebastian Redlade50002010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000059}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
66
Douglas Gregor2cf26342009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000070
Douglas Gregor2cf26342009-04-09 22:27:44 +000071namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl3397c552010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000116}
117
Sebastian Redl3397c552010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159}
160
Sebastian Redl3397c552010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175}
176
Sebastian Redl3397c552010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
188 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000189 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000190 Record.push_back(T->getExceptionSpecType());
191 if (T->getExceptionSpecType() == EST_Dynamic) {
192 Record.push_back(T->getNumExceptions());
193 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
194 Writer.AddTypeRef(T->getExceptionType(I), Record);
195 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
196 Writer.AddStmt(T->getNoexceptExpr());
197 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000198 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199}
200
Sebastian Redl3397c552010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000202 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000203 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000204}
John McCalled976492009-12-04 22:46:56 +0000205
Sebastian Redl3397c552010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000207 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000208 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
209 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000210 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000211}
212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000214 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000215 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000216}
217
Sebastian Redl3397c552010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000226}
227
Sean Huntca63c202011-05-24 22:41:36 +0000228void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
229 Writer.AddTypeRef(T->getBaseType(), Record);
230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
231 Record.push_back(T->getUTTKind());
232 Code = TYPE_UNARY_TRANSFORM;
233}
234
Richard Smith34b41d92011-02-20 03:19:35 +0000235void ASTTypeWriter::VisitAutoType(const AutoType *T) {
236 Writer.AddTypeRef(T->getDeducedType(), Record);
237 Code = TYPE_AUTO;
238}
239
Sebastian Redl3397c552010-08-18 23:56:27 +0000240void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000241 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000242 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000243 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000244 "Cannot serialize in the middle of a type definition");
245}
246
Sebastian Redl3397c552010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000249 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000250}
251
Sebastian Redl3397c552010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000254 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255}
256
John McCall9d156a72011-01-06 01:58:22 +0000257void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
258 Writer.AddTypeRef(T->getModifiedType(), Record);
259 Writer.AddTypeRef(T->getEquivalentType(), Record);
260 Record.push_back(T->getAttrKind());
261 Code = TYPE_ATTRIBUTED;
262}
263
Mike Stump1eb44332009-09-09 15:08:12 +0000264void
Sebastian Redl3397c552010-08-18 23:56:27 +0000265ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000266 const SubstTemplateTypeParmType *T) {
267 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
268 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000269 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000270}
271
272void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
274 const SubstTemplateTypeParmPackType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
278}
279
280void
Sebastian Redl3397c552010-08-18 23:56:27 +0000281ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000282 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000283 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000284 Writer.AddTemplateName(T->getTemplateName(), Record);
285 Record.push_back(T->getNumArgs());
286 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
287 ArgI != ArgE; ++ArgI)
288 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000289 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
290 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000291 : T->getCanonicalTypeInternal(),
292 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000293 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000294}
295
296void
Sebastian Redl3397c552010-08-18 23:56:27 +0000297ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000298 VisitArrayType(T);
299 Writer.AddStmt(T->getSizeExpr());
300 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl3397c552010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000306 const DependentSizedExtVectorType *T) {
307 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000308 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl3397c552010-08-18 23:56:27 +0000312ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313 Record.push_back(T->getDepth());
314 Record.push_back(T->getIndex());
315 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000316 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000317 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000318}
319
320void
Sebastian Redl3397c552010-08-18 23:56:27 +0000321ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000322 Record.push_back(T->getKeyword());
323 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
324 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000325 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
326 : T->getCanonicalTypeInternal(),
327 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000333 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000334 Record.push_back(T->getKeyword());
335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddIdentifierRef(T->getIdentifier(), Record);
337 Record.push_back(T->getNumArgs());
338 for (DependentTemplateSpecializationType::iterator
339 I = T->begin(), E = T->end(); I != E; ++I)
340 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000341 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000342}
343
Douglas Gregor7536dd52010-12-20 02:24:11 +0000344void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
345 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000346 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
347 Record.push_back(*NumExpansions + 1);
348 else
349 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000350 Code = TYPE_PACK_EXPANSION;
351}
352
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000353void ASTTypeWriter::VisitParenType(const ParenType *T) {
354 Writer.AddTypeRef(T->getInnerType(), Record);
355 Code = TYPE_PAREN;
356}
357
Sebastian Redl3397c552010-08-18 23:56:27 +0000358void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000359 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000360 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
361 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000362 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000363}
364
Sebastian Redl3397c552010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000366 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000367 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000368 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000369}
370
Sebastian Redl3397c552010-08-18 23:56:27 +0000371void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000372 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000374}
375
Sebastian Redl3397c552010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000377 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000378 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000379 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000380 E = T->qual_end(); I != E; ++I)
381 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000382 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000383}
384
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000385void
Sebastian Redl3397c552010-08-18 23:56:27 +0000386ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000387 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000388 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000389}
390
Eli Friedmanb001de72011-10-06 23:00:33 +0000391void
392ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
393 Writer.AddTypeRef(T->getValueType(), Record);
394 Code = TYPE_ATOMIC;
395}
396
John McCalla1ee0c52009-10-16 21:56:05 +0000397namespace {
398
399class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000400 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000401 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000402
403public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000404 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000405 : Writer(Writer), Record(Record) { }
406
John McCall51bd8032009-10-18 01:05:36 +0000407#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000408#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000409 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000410#include "clang/AST/TypeLocNodes.def"
411
John McCall51bd8032009-10-18 01:05:36 +0000412 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
413 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000414};
415
416}
417
John McCall51bd8032009-10-18 01:05:36 +0000418void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
419 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000420}
John McCall51bd8032009-10-18 01:05:36 +0000421void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000422 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
423 if (TL.needsExtraLocalData()) {
424 Record.push_back(TL.getWrittenTypeSpec());
425 Record.push_back(TL.getWrittenSignSpec());
426 Record.push_back(TL.getWrittenWidthSpec());
427 Record.push_back(TL.hasModeAttr());
428 }
John McCalla1ee0c52009-10-16 21:56:05 +0000429}
John McCall51bd8032009-10-18 01:05:36 +0000430void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000432}
John McCall51bd8032009-10-18 01:05:36 +0000433void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000435}
John McCall51bd8032009-10-18 01:05:36 +0000436void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000438}
John McCall51bd8032009-10-18 01:05:36 +0000439void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000441}
John McCall51bd8032009-10-18 01:05:36 +0000442void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000444}
John McCall51bd8032009-10-18 01:05:36 +0000445void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000447 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
451 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
452 Record.push_back(TL.getSizeExpr() ? 1 : 0);
453 if (TL.getSizeExpr())
454 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
457 VisitArrayTypeLoc(TL);
458}
459void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
460 VisitArrayTypeLoc(TL);
461}
462void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
463 VisitArrayTypeLoc(TL);
464}
465void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
466 DependentSizedArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
470 DependentSizedExtVectorTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
473void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
475}
476void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
479void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000480 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
481 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000482 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000483 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
484 Writer.AddDeclRef(TL.getArg(i), Record);
485}
486void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
487 VisitFunctionTypeLoc(TL);
488}
489void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
490 VisitFunctionTypeLoc(TL);
491}
John McCalled976492009-12-04 22:46:56 +0000492void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
John McCall51bd8032009-10-18 01:05:36 +0000495void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getNameLoc(), Record);
497}
498void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
500 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
501 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000502}
503void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000504 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
505 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
506 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
507 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000508}
509void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
Sean Huntca63c202011-05-24 22:41:36 +0000512void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
513 Writer.AddSourceLocation(TL.getKWLoc(), Record);
514 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
515 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
516 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
517}
Richard Smith34b41d92011-02-20 03:19:35 +0000518void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
John McCall51bd8032009-10-18 01:05:36 +0000521void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall9d156a72011-01-06 01:58:22 +0000527void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
529 if (TL.hasAttrOperand()) {
530 SourceRange range = TL.getAttrOperandParensRange();
531 Writer.AddSourceLocation(range.getBegin(), Record);
532 Writer.AddSourceLocation(range.getEnd(), Record);
533 }
534 if (TL.hasAttrExprOperand()) {
535 Expr *operand = TL.getAttrExprOperand();
536 Record.push_back(operand ? 1 : 0);
537 if (operand) Writer.AddStmt(operand);
538 } else if (TL.hasAttrEnumOperand()) {
539 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
540 }
541}
John McCall51bd8032009-10-18 01:05:36 +0000542void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCall49a832b2009-10-18 09:09:24 +0000545void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
546 SubstTemplateTypeParmTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getNameLoc(), Record);
548}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000549void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
550 SubstTemplateTypeParmPackTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall51bd8032009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
554 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000555 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000559 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
560 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000561}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000562void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
564 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
565}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000566void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000567 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000568 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000569}
John McCall3cb0ebd2010-03-10 03:28:59 +0000570void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
Douglas Gregor4714c122010-03-31 17:34:00 +0000573void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000574 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000575 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000576 Writer.AddSourceLocation(TL.getNameLoc(), Record);
577}
John McCall33500952010-06-11 00:33:02 +0000578void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
579 DependentTemplateSpecializationTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
584 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
585 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000586 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
587 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000588}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000589void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
590 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
591}
John McCall51bd8032009-10-18 01:05:36 +0000592void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
593 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000594}
595void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
596 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
600 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000601}
John McCall54e14c42009-10-22 22:37:11 +0000602void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000604}
Eli Friedmanb001de72011-10-06 23:00:33 +0000605void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getKWLoc(), Record);
607 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
608 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
609}
John McCalla1ee0c52009-10-16 21:56:05 +0000610
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000611//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000612// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000613//===----------------------------------------------------------------------===//
614
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000615static void EmitBlockID(unsigned ID, const char *Name,
616 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000617 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000618 Record.clear();
619 Record.push_back(ID);
620 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
621
622 // Emit the block name if present.
623 if (Name == 0 || Name[0] == 0) return;
624 Record.clear();
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
628}
629
630static void EmitRecordID(unsigned ID, const char *Name,
631 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000632 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 Record.clear();
634 Record.push_back(ID);
635 while (*Name)
636 Record.push_back(*Name++);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000638}
639
640static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000641 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000642#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000643 RECORD(STMT_STOP);
644 RECORD(STMT_NULL_PTR);
645 RECORD(STMT_NULL);
646 RECORD(STMT_COMPOUND);
647 RECORD(STMT_CASE);
648 RECORD(STMT_DEFAULT);
649 RECORD(STMT_LABEL);
650 RECORD(STMT_IF);
651 RECORD(STMT_SWITCH);
652 RECORD(STMT_WHILE);
653 RECORD(STMT_DO);
654 RECORD(STMT_FOR);
655 RECORD(STMT_GOTO);
656 RECORD(STMT_INDIRECT_GOTO);
657 RECORD(STMT_CONTINUE);
658 RECORD(STMT_BREAK);
659 RECORD(STMT_RETURN);
660 RECORD(STMT_DECL);
661 RECORD(STMT_ASM);
662 RECORD(EXPR_PREDEFINED);
663 RECORD(EXPR_DECL_REF);
664 RECORD(EXPR_INTEGER_LITERAL);
665 RECORD(EXPR_FLOATING_LITERAL);
666 RECORD(EXPR_IMAGINARY_LITERAL);
667 RECORD(EXPR_STRING_LITERAL);
668 RECORD(EXPR_CHARACTER_LITERAL);
669 RECORD(EXPR_PAREN);
670 RECORD(EXPR_UNARY_OPERATOR);
671 RECORD(EXPR_SIZEOF_ALIGN_OF);
672 RECORD(EXPR_ARRAY_SUBSCRIPT);
673 RECORD(EXPR_CALL);
674 RECORD(EXPR_MEMBER);
675 RECORD(EXPR_BINARY_OPERATOR);
676 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
677 RECORD(EXPR_CONDITIONAL_OPERATOR);
678 RECORD(EXPR_IMPLICIT_CAST);
679 RECORD(EXPR_CSTYLE_CAST);
680 RECORD(EXPR_COMPOUND_LITERAL);
681 RECORD(EXPR_EXT_VECTOR_ELEMENT);
682 RECORD(EXPR_INIT_LIST);
683 RECORD(EXPR_DESIGNATED_INIT);
684 RECORD(EXPR_IMPLICIT_VALUE_INIT);
685 RECORD(EXPR_VA_ARG);
686 RECORD(EXPR_ADDR_LABEL);
687 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000688 RECORD(EXPR_CHOOSE);
689 RECORD(EXPR_GNU_NULL);
690 RECORD(EXPR_SHUFFLE_VECTOR);
691 RECORD(EXPR_BLOCK);
692 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000693 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000694 RECORD(EXPR_OBJC_STRING_LITERAL);
695 RECORD(EXPR_OBJC_ENCODE);
696 RECORD(EXPR_OBJC_SELECTOR_EXPR);
697 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
698 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
699 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
700 RECORD(EXPR_OBJC_KVC_REF_EXPR);
701 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000702 RECORD(STMT_OBJC_FOR_COLLECTION);
703 RECORD(STMT_OBJC_CATCH);
704 RECORD(STMT_OBJC_FINALLY);
705 RECORD(STMT_OBJC_AT_TRY);
706 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
707 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000708 RECORD(EXPR_CXX_OPERATOR_CALL);
709 RECORD(EXPR_CXX_CONSTRUCT);
710 RECORD(EXPR_CXX_STATIC_CAST);
711 RECORD(EXPR_CXX_DYNAMIC_CAST);
712 RECORD(EXPR_CXX_REINTERPRET_CAST);
713 RECORD(EXPR_CXX_CONST_CAST);
714 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
715 RECORD(EXPR_CXX_BOOL_LITERAL);
716 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000717 RECORD(EXPR_CXX_TYPEID_EXPR);
718 RECORD(EXPR_CXX_TYPEID_TYPE);
719 RECORD(EXPR_CXX_UUIDOF_EXPR);
720 RECORD(EXPR_CXX_UUIDOF_TYPE);
721 RECORD(EXPR_CXX_THIS);
722 RECORD(EXPR_CXX_THROW);
723 RECORD(EXPR_CXX_DEFAULT_ARG);
724 RECORD(EXPR_CXX_BIND_TEMPORARY);
725 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
726 RECORD(EXPR_CXX_NEW);
727 RECORD(EXPR_CXX_DELETE);
728 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
729 RECORD(EXPR_EXPR_WITH_CLEANUPS);
730 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
731 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
732 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
733 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
734 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
735 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
736 RECORD(EXPR_CXX_NOEXCEPT);
737 RECORD(EXPR_OPAQUE_VALUE);
738 RECORD(EXPR_BINARY_TYPE_TRAIT);
739 RECORD(EXPR_PACK_EXPANSION);
740 RECORD(EXPR_SIZEOF_PACK);
741 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000742 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000743#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000744}
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Sebastian Redla4232eb2010-08-18 23:56:21 +0000746void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000747 RecordData Record;
748 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000750#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
751#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Sebastian Redl3397c552010-08-18 23:56:27 +0000753 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000754 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000755 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000756 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000757 RECORD(TYPE_OFFSET);
758 RECORD(DECL_OFFSET);
759 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000760 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761 RECORD(IDENTIFIER_OFFSET);
762 RECORD(IDENTIFIER_TABLE);
763 RECORD(EXTERNAL_DEFINITIONS);
764 RECORD(SPECIAL_TYPES);
765 RECORD(STATISTICS);
766 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000767 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000768 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
769 RECORD(SELECTOR_OFFSETS);
770 RECORD(METHOD_POOL);
771 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000772 RECORD(SOURCE_LOCATION_OFFSETS);
773 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000774 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000775 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000776 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000777 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000778 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000779 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000780 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000781 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000782 RECORD(SEMA_DECL_REFS);
783 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
784 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
785 RECORD(DECL_REPLACEMENTS);
786 RECORD(UPDATE_VISIBLE);
787 RECORD(DECL_UPDATE_OFFSETS);
788 RECORD(DECL_UPDATES);
789 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
790 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000791 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000792 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000793 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000794 RECORD(FP_PRAGMA_OPTIONS);
795 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000796 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000797 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
798 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000799 RECORD(MODULE_OFFSET_MAP);
800 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000801 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000802 RECORD(FILE_SORTED_DECLS);
803 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000804 RECORD(MERGED_DECLARATIONS);
805 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000806 RECORD(OBJC_CATEGORIES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000807
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000808 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000809 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000810 RECORD(SM_SLOC_FILE_ENTRY);
811 RECORD(SM_SLOC_BUFFER_ENTRY);
812 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000813 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000815 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000816 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000817 RECORD(PP_MACRO_OBJECT_LIKE);
818 RECORD(PP_MACRO_FUNCTION_LIKE);
819 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000820
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000821 // Decls and Types block.
822 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000823 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000824 RECORD(TYPE_COMPLEX);
825 RECORD(TYPE_POINTER);
826 RECORD(TYPE_BLOCK_POINTER);
827 RECORD(TYPE_LVALUE_REFERENCE);
828 RECORD(TYPE_RVALUE_REFERENCE);
829 RECORD(TYPE_MEMBER_POINTER);
830 RECORD(TYPE_CONSTANT_ARRAY);
831 RECORD(TYPE_INCOMPLETE_ARRAY);
832 RECORD(TYPE_VARIABLE_ARRAY);
833 RECORD(TYPE_VECTOR);
834 RECORD(TYPE_EXT_VECTOR);
835 RECORD(TYPE_FUNCTION_PROTO);
836 RECORD(TYPE_FUNCTION_NO_PROTO);
837 RECORD(TYPE_TYPEDEF);
838 RECORD(TYPE_TYPEOF_EXPR);
839 RECORD(TYPE_TYPEOF);
840 RECORD(TYPE_RECORD);
841 RECORD(TYPE_ENUM);
842 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000843 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000844 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000845 RECORD(TYPE_DECLTYPE);
846 RECORD(TYPE_ELABORATED);
847 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
848 RECORD(TYPE_UNRESOLVED_USING);
849 RECORD(TYPE_INJECTED_CLASS_NAME);
850 RECORD(TYPE_OBJC_OBJECT);
851 RECORD(TYPE_TEMPLATE_TYPE_PARM);
852 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
853 RECORD(TYPE_DEPENDENT_NAME);
854 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
855 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
856 RECORD(TYPE_PAREN);
857 RECORD(TYPE_PACK_EXPANSION);
858 RECORD(TYPE_ATTRIBUTED);
859 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000860 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000861 RECORD(DECL_TYPEDEF);
862 RECORD(DECL_ENUM);
863 RECORD(DECL_RECORD);
864 RECORD(DECL_ENUM_CONSTANT);
865 RECORD(DECL_FUNCTION);
866 RECORD(DECL_OBJC_METHOD);
867 RECORD(DECL_OBJC_INTERFACE);
868 RECORD(DECL_OBJC_PROTOCOL);
869 RECORD(DECL_OBJC_IVAR);
870 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000871 RECORD(DECL_OBJC_CATEGORY);
872 RECORD(DECL_OBJC_CATEGORY_IMPL);
873 RECORD(DECL_OBJC_IMPLEMENTATION);
874 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
875 RECORD(DECL_OBJC_PROPERTY);
876 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000877 RECORD(DECL_FIELD);
878 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000879 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000880 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000881 RECORD(DECL_FILE_SCOPE_ASM);
882 RECORD(DECL_BLOCK);
883 RECORD(DECL_CONTEXT_LEXICAL);
884 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000885 RECORD(DECL_NAMESPACE);
886 RECORD(DECL_NAMESPACE_ALIAS);
887 RECORD(DECL_USING);
888 RECORD(DECL_USING_SHADOW);
889 RECORD(DECL_USING_DIRECTIVE);
890 RECORD(DECL_UNRESOLVED_USING_VALUE);
891 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
892 RECORD(DECL_LINKAGE_SPEC);
893 RECORD(DECL_CXX_RECORD);
894 RECORD(DECL_CXX_METHOD);
895 RECORD(DECL_CXX_CONSTRUCTOR);
896 RECORD(DECL_CXX_DESTRUCTOR);
897 RECORD(DECL_CXX_CONVERSION);
898 RECORD(DECL_ACCESS_SPEC);
899 RECORD(DECL_FRIEND);
900 RECORD(DECL_FRIEND_TEMPLATE);
901 RECORD(DECL_CLASS_TEMPLATE);
902 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
903 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
904 RECORD(DECL_FUNCTION_TEMPLATE);
905 RECORD(DECL_TEMPLATE_TYPE_PARM);
906 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
907 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
908 RECORD(DECL_STATIC_ASSERT);
909 RECORD(DECL_CXX_BASE_SPECIFIERS);
910 RECORD(DECL_INDIRECTFIELD);
911 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
912
Douglas Gregora72d8c42011-06-03 02:27:19 +0000913 // Statements and Exprs can occur in the Decls and Types block.
914 AddStmtsExprs(Stream, Record);
915
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000916 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000917 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000918 RECORD(PPD_MACRO_DEFINITION);
919 RECORD(PPD_INCLUSION_DIRECTIVE);
920
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000921#undef RECORD
922#undef BLOCK
923 Stream.ExitBlock();
924}
925
Douglas Gregore650c8c2009-07-07 00:12:59 +0000926/// \brief Adjusts the given filename to only write out the portion of the
927/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000928///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000929/// \param Filename the file name to adjust.
930///
931/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
932/// the returned filename will be adjusted by this system root.
933///
934/// \returns either the original filename (if it needs no adjustment) or the
935/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000936static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000937adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000938 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Douglas Gregor832d6202011-07-22 16:35:34 +0000940 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000941 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943 // Verify that the filename and the system root have the same prefix.
944 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000945 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000946 if (Filename[Pos] != isysroot[Pos])
947 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Douglas Gregore650c8c2009-07-07 00:12:59 +0000949 // We hit the end of the filename before we hit the end of the system root.
950 if (!Filename[Pos])
951 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Douglas Gregore650c8c2009-07-07 00:12:59 +0000953 // If the file name has a '/' at the current position, skip over the '/'.
954 // We distinguish sysroot-based includes from absolute includes by the
955 // absence of '/' at the beginning of sysroot-based includes.
956 if (Filename[Pos] == '/')
957 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Douglas Gregore650c8c2009-07-07 00:12:59 +0000959 return Filename + Pos;
960}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000961
Sebastian Redl3397c552010-08-18 23:56:27 +0000962/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000963void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000964 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000965 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000966
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000968 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000970 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000971 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
972 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
974 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
975 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000976 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000980 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000981 Record.push_back(VERSION_MAJOR);
982 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 Record.push_back(CLANG_VERSION_MAJOR);
984 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000985 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000986 const std::string &Triple = Target.getTriple().getTriple();
987 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
988
989 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000990 serialization::ModuleManager &Mgr = Chain->getModuleManager();
991 llvm::SmallVector<char, 128> ModulePaths;
992 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000993
994 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
995 M != MEnd; ++M) {
996 // Skip modules that weren't directly imported.
997 if (!(*M)->isDirectlyImported())
998 continue;
999
1000 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1001 // FIXME: Write import location, once it matters.
1002 // FIXME: This writes the absolute path for AST files we depend on.
1003 const std::string &FileName = (*M)->FileName;
1004 Record.push_back(FileName.size());
1005 Record.append(FileName.begin(), FileName.end());
1006 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001007 Stream.EmitRecord(IMPORTS, Record);
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregor31d375f2011-05-06 21:43:30 +00001010 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001011 SourceManager &SM = Context.getSourceManager();
1012 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1013 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001014 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001015 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1016 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1017
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001018 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001020 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001021
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001022 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001023 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001024 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001025 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001026 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001027 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001028
1029 Record.clear();
1030 Record.push_back(SM.getMainFileID().getOpaqueValue());
1031 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001032 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001033
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001034 // Original PCH directory
1035 if (!OutputFile.empty() && OutputFile != "-") {
1036 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1037 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1039 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1040
1041 llvm::SmallString<128> OutputPath(OutputFile);
1042
1043 llvm::sys::fs::make_absolute(OutputPath);
1044 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1045
1046 RecordData Record;
1047 Record.push_back(ORIGINAL_PCH_DIR);
1048 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1049 }
1050
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001051 // Repository branch/version information.
1052 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001053 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001054 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1055 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001056 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001057 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001058 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1059 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001060}
1061
1062/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001063void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001064 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001065#define LANGOPT(Name, Bits, Default, Description) \
1066 Record.push_back(LangOpts.Name);
1067#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1068 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1069#include "clang/Basic/LangOptions.def"
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001070
1071 Record.push_back(LangOpts.CurrentModule.size());
1072 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001073 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001074}
1075
Douglas Gregor14f79002009-04-10 03:52:48 +00001076//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001077// stat cache Serialization
1078//===----------------------------------------------------------------------===//
1079
1080namespace {
1081// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001082class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001083public:
1084 typedef const char * key_type;
1085 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattner74e976b2010-11-23 19:28:12 +00001087 typedef struct stat data_type;
1088 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001089
1090 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001091 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
1094 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001095 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096 data_type_ref Data) {
1097 unsigned StrLen = strlen(path);
1098 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001099 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001100 clang::io::Emit8(Out, DataLen);
1101 return std::make_pair(StrLen + 1, DataLen);
1102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Chris Lattner5f9e2722011-07-23 10:55:15 +00001104 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001105 Out.write(path, KeyLen);
1106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Chris Lattner5f9e2722011-07-23 10:55:15 +00001108 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001109 data_type_ref Data, unsigned DataLen) {
1110 using namespace clang::io;
1111 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Chris Lattner74e976b2010-11-23 19:28:12 +00001113 Emit32(Out, (uint32_t) Data.st_ino);
1114 Emit32(Out, (uint32_t) Data.st_dev);
1115 Emit16(Out, (uint16_t) Data.st_mode);
1116 Emit64(Out, (uint64_t) Data.st_mtime);
1117 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001118
1119 assert(Out.tell() - Start == DataLen && "Wrong data length");
1120 }
1121};
1122} // end anonymous namespace
1123
Sebastian Redl3397c552010-08-18 23:56:27 +00001124/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001125void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001126 // Build the on-disk hash table containing information about every
1127 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001128 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001129 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001130 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001131 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001132 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001133 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001134 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001137 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001138 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001139 uint32_t BucketOffset;
1140 {
1141 llvm::raw_svector_ostream Out(StatCacheData);
1142 // Make sure that no bucket is at offset 0
1143 clang::io::Emit32(Out, 0);
1144 BucketOffset = Generator.Emit(Out);
1145 }
1146
1147 // Create a blob abbreviation
1148 using namespace llvm;
1149 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001150 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1154 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1155
1156 // Write the stat cache
1157 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001158 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001159 Record.push_back(BucketOffset);
1160 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001161 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001162}
1163
1164//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001165// Source Manager Serialization
1166//===----------------------------------------------------------------------===//
1167
1168/// \brief Create an abbreviation for the SLocEntry that refers to a
1169/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001170static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001171 using namespace llvm;
1172 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001173 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001178 // FileEntry fields.
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001186 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001187}
1188
1189/// \brief Create an abbreviation for the SLocEntry that refers to a
1190/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001191static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001192 using namespace llvm;
1193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001194 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001200 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001201}
1202
1203/// \brief Create an abbreviation for the SLocEntry that refers to a
1204/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001205static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001206 using namespace llvm;
1207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001208 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001210 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001211}
1212
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001213/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1214/// expansion.
1215static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001216 using namespace llvm;
1217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001218 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001224 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001225}
1226
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001227namespace {
1228 // Trait used for the on-disk hash table of header search information.
1229 class HeaderFileInfoTrait {
1230 ASTWriter &Writer;
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001231 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001232
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001233 // Keep track of the framework names we've used during serialization.
1234 SmallVector<char, 128> FrameworkStringData;
1235 llvm::StringMap<unsigned> FrameworkNameOffset;
1236
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001237 public:
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001238 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001239 : Writer(Writer), HS(HS) { }
1240
1241 typedef const char *key_type;
1242 typedef key_type key_type_ref;
1243
1244 typedef HeaderFileInfo data_type;
1245 typedef const data_type &data_type_ref;
1246
1247 static unsigned ComputeHash(const char *path) {
1248 // The hash is based only on the filename portion of the key, so that the
1249 // reader can match based on filenames when symlinking or excess path
1250 // elements ("foo/../", "../") change the form of the name. However,
1251 // complete path is still the key.
1252 return llvm::HashString(llvm::sys::path::filename(path));
1253 }
1254
1255 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001256 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001257 data_type_ref Data) {
1258 unsigned StrLen = strlen(path);
1259 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001260 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001261 clang::io::Emit8(Out, DataLen);
1262 return std::make_pair(StrLen + 1, DataLen);
1263 }
1264
Chris Lattner5f9e2722011-07-23 10:55:15 +00001265 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001266 Out.write(path, KeyLen);
1267 }
1268
Chris Lattner5f9e2722011-07-23 10:55:15 +00001269 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001270 data_type_ref Data, unsigned DataLen) {
1271 using namespace clang::io;
1272 uint64_t Start = Out.tell(); (void)Start;
1273
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001274 unsigned char Flags = (Data.isImport << 5)
1275 | (Data.isPragmaOnce << 4)
1276 | (Data.DirInfo << 2)
1277 | (Data.Resolved << 1)
1278 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001279 Emit8(Out, (uint8_t)Flags);
1280 Emit16(Out, (uint16_t) Data.NumIncludes);
1281
1282 if (!Data.ControllingMacro)
1283 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1284 else
1285 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001286
1287 unsigned Offset = 0;
1288 if (!Data.Framework.empty()) {
1289 // If this header refers into a framework, save the framework name.
1290 llvm::StringMap<unsigned>::iterator Pos
1291 = FrameworkNameOffset.find(Data.Framework);
1292 if (Pos == FrameworkNameOffset.end()) {
1293 Offset = FrameworkStringData.size() + 1;
1294 FrameworkStringData.append(Data.Framework.begin(),
1295 Data.Framework.end());
1296 FrameworkStringData.push_back(0);
1297
1298 FrameworkNameOffset[Data.Framework] = Offset;
1299 } else
1300 Offset = Pos->second;
1301 }
1302 Emit32(Out, Offset);
1303
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001304 assert(Out.tell() - Start == DataLen && "Wrong data length");
1305 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001306
1307 const char *strings_begin() const { return FrameworkStringData.begin(); }
1308 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001309 };
1310} // end anonymous namespace
1311
1312/// \brief Write the header search block for the list of files that
1313///
1314/// \param HS The header search structure to save.
1315///
1316/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001317void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001318 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001319 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1320
1321 if (FilesByUID.size() > HS.header_file_size())
1322 FilesByUID.resize(HS.header_file_size());
1323
1324 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1325 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001326 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001327 unsigned NumHeaderSearchEntries = 0;
1328 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1329 const FileEntry *File = FilesByUID[UID];
1330 if (!File)
1331 continue;
1332
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001333 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1334 // from the external source if it was not provided already.
1335 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001336 if (HFI.External && Chain)
1337 continue;
1338
1339 // Turn the file name into an absolute path, if it isn't already.
1340 const char *Filename = File->getName();
1341 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1342
1343 // If we performed any translation on the file name at all, we need to
1344 // save this string, since the generator will refer to it later.
1345 if (Filename != File->getName()) {
1346 Filename = strdup(Filename);
1347 SavedStrings.push_back(Filename);
1348 }
1349
1350 Generator.insert(Filename, HFI, GeneratorTrait);
1351 ++NumHeaderSearchEntries;
1352 }
1353
1354 // Create the on-disk hash table in a buffer.
1355 llvm::SmallString<4096> TableData;
1356 uint32_t BucketOffset;
1357 {
1358 llvm::raw_svector_ostream Out(TableData);
1359 // Make sure that no bucket is at offset 0
1360 clang::io::Emit32(Out, 0);
1361 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1362 }
1363
1364 // Create a blob abbreviation
1365 using namespace llvm;
1366 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1367 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1372 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1373
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001374 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001375 RecordData Record;
1376 Record.push_back(HEADER_SEARCH_TABLE);
1377 Record.push_back(BucketOffset);
1378 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001379 Record.push_back(TableData.size());
1380 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001381 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1382
1383 // Free all of the strings we had to duplicate.
1384 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1385 free((void*)SavedStrings[I]);
1386}
1387
Douglas Gregor14f79002009-04-10 03:52:48 +00001388/// \brief Writes the block containing the serialized form of the
1389/// source manager.
1390///
1391/// TODO: We should probably use an on-disk hash table (stored in a
1392/// blob), indexed based on the file name, so that we only create
1393/// entries for files that we actually need. In the common case (no
1394/// errors), we probably won't have to create file entries for any of
1395/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001396void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001397 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001398 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001399 RecordData Record;
1400
Chris Lattnerf04ad692009-04-10 17:16:57 +00001401 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001402 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001403
1404 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001405 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1406 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1407 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001408 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001409
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001410 // Write out the source location entry table. We skip the first
1411 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001412 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001413 // Write out the offsets of only source location file entries.
1414 // We will go through them in ASTReader::validateFileEntries().
1415 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001416 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001417 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1418 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001419 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001420 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001421 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001422
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001423 // Record the offset of this source-location entry.
1424 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1425
1426 // Figure out which record code to use.
1427 unsigned Code;
1428 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001429 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1430 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001431 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001432 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1433 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001434 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001435 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001436 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001437 Record.clear();
1438 Record.push_back(Code);
1439
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001440 // Starting offset of this entry within this module, so skip the dummy.
1441 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001442 if (SLoc->isFile()) {
1443 const SrcMgr::FileInfo &File = SLoc->getFile();
1444 Record.push_back(File.getIncludeLoc().getRawEncoding());
1445 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1446 Record.push_back(File.hasLineDirectives());
1447
1448 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001449 if (Content->OrigEntry) {
1450 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001451 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001452
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001453 // The source location entry is a file. The blob associated
1454 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregor2d52be52010-03-21 22:49:54 +00001456 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001457 Record.push_back(Content->OrigEntry->getSize());
1458 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001459 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001460 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001461
1462 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1463 if (FDI != FileDeclIDs.end()) {
1464 Record.push_back(FDI->second->FirstDeclIndex);
1465 Record.push_back(FDI->second->DeclIDs.size());
1466 } else {
1467 Record.push_back(0);
1468 Record.push_back(0);
1469 }
Douglas Gregora081da52011-11-16 20:05:18 +00001470
Douglas Gregore650c8c2009-07-07 00:12:59 +00001471 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001472 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001473 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001474
1475 // Ask the file manager to fixup the relative path for us. This will
1476 // honor the working directory.
1477 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1478
1479 // FIXME: This call to make_absolute shouldn't be necessary, the
1480 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001481 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001482 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregore650c8c2009-07-07 00:12:59 +00001484 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001485 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001486
1487 if (Content->BufferOverridden) {
1488 Record.clear();
1489 Record.push_back(SM_SLOC_BUFFER_BLOB);
1490 const llvm::MemoryBuffer *Buffer
1491 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1492 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1493 StringRef(Buffer->getBufferStart(),
1494 Buffer->getBufferSize() + 1));
1495 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001496 } else {
1497 // The source location entry is a buffer. The blob associated
1498 // with this entry contains the contents of the buffer.
1499
1500 // We add one to the size so that we capture the trailing NULL
1501 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1502 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001503 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001504 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001505 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001506 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001507 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001508 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001509 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001510 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001511 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001512 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001513
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001514 if (strcmp(Name, "<built-in>") == 0) {
1515 PreloadSLocs.push_back(SLocEntryOffsets.size());
1516 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001517 }
1518 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001519 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001520 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001521 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1522 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001523 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1524 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001525
1526 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001527 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001528 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001529 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001530 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001531 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 }
1533 }
1534
Douglas Gregorc9490c02009-04-16 22:23:12 +00001535 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001536
1537 if (SLocEntryOffsets.empty())
1538 return;
1539
Sebastian Redl3397c552010-08-18 23:56:27 +00001540 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001541 // table is used for lazily loading source-location information.
1542 using namespace llvm;
1543 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001544 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1548 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001550 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001551 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001553 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001554 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001555
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001556 Abbrev = new BitCodeAbbrev();
1557 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1560 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1561
1562 Record.clear();
1563 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1564 Record.push_back(SLocFileEntryOffsets.size());
1565 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1566 data(SLocFileEntryOffsets));
1567
Sebastian Redl3397c552010-08-18 23:56:27 +00001568 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001569 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001570 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001571
1572 // Write the line table. It depends on remapping working, so it must come
1573 // after the source location offsets.
1574 if (SourceMgr.hasLineTable()) {
1575 LineTableInfo &LineTable = SourceMgr.getLineTable();
1576
1577 Record.clear();
1578 // Emit the file names
1579 Record.push_back(LineTable.getNumFilenames());
1580 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1581 // Emit the file name
1582 const char *Filename = LineTable.getFilename(I);
1583 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1584 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1585 Record.push_back(FilenameLen);
1586 if (FilenameLen)
1587 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1588 }
1589
1590 // Emit the line entries
1591 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1592 L != LEnd; ++L) {
1593 // Only emit entries for local files.
1594 if (L->first < 0)
1595 continue;
1596
1597 // Emit the file ID
1598 Record.push_back(L->first);
1599
1600 // Emit the line entries
1601 Record.push_back(L->second.size());
1602 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1603 LEEnd = L->second.end();
1604 LE != LEEnd; ++LE) {
1605 Record.push_back(LE->FileOffset);
1606 Record.push_back(LE->LineNo);
1607 Record.push_back(LE->FilenameID);
1608 Record.push_back((unsigned)LE->FileKind);
1609 Record.push_back(LE->IncludeOffset);
1610 }
1611 }
1612 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1613 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001614}
1615
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001616//===----------------------------------------------------------------------===//
1617// Preprocessor Serialization
1618//===----------------------------------------------------------------------===//
1619
Douglas Gregor9c736102011-02-10 18:20:09 +00001620static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1621 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1622 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1623 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1624 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1625 return X.first->getName().compare(Y.first->getName());
1626}
1627
Chris Lattner0b1fb982009-04-10 17:15:23 +00001628/// \brief Writes the block containing the serialized form of the
1629/// preprocessor.
1630///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001631void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001632 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1633 if (PPRec)
1634 WritePreprocessorDetail(*PPRec);
1635
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001636 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001637
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001638 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1639 if (PP.getCounterValue() != 0) {
1640 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001641 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001642 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001643 }
1644
1645 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001646 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Sebastian Redl3397c552010-08-18 23:56:27 +00001648 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001649 // FIXME: use diagnostics subsystem for localization etc.
1650 if (PP.SawDateOrTime())
1651 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Douglas Gregorecdcb882010-10-20 22:00:55 +00001653
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001654 // Loop over all the macro definitions that are live at the end of the file,
1655 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001656
Douglas Gregor9c736102011-02-10 18:20:09 +00001657 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001658 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001659 MacrosToEmit;
1660 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001661 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1662 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001663 I != E; ++I) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001664 const IdentifierInfo *Name = I->first;
Douglas Gregoraa93a872011-10-17 15:32:29 +00001665 if (!IsModule || I->second->isPublic()) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001666 MacroDefinitionsSeen.insert(Name);
Douglas Gregor7143aab2011-09-01 17:04:32 +00001667 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1668 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001669 }
1670
1671 // Sort the set of macro definitions that need to be serialized by the
1672 // name of the macro, to provide a stable ordering.
1673 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1674 &compareMacroDefinitions);
1675
Douglas Gregor040a8042011-02-11 00:26:14 +00001676 // Resolve any identifiers that defined macros at the time they were
1677 // deserialized, adding them to the list of macros to emit (if appropriate).
1678 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1679 IdentifierInfo *Name
1680 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1681 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1682 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1683 }
1684
Douglas Gregor9c736102011-02-10 18:20:09 +00001685 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1686 const IdentifierInfo *Name = MacrosToEmit[I].first;
1687 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001688 if (!MI)
1689 continue;
1690
Sebastian Redl3397c552010-08-18 23:56:27 +00001691 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001692 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001693 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001694
1695 // FIXME: There is a (probably minor) optimization we could do here, if
1696 // the macro comes from the original PCH but the identifier comes from a
1697 // chained PCH, by storing the offset into the original PCH rather than
1698 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001699 if (MI->isBuiltinMacro() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00001700 (Chain &&
1701 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1702 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001703 continue;
1704
Douglas Gregor9c736102011-02-10 18:20:09 +00001705 AddIdentifierRef(Name, Record);
1706 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001707 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1708 Record.push_back(MI->isUsed());
Douglas Gregoraa93a872011-10-17 15:32:29 +00001709 Record.push_back(MI->isPublic());
1710 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001711 unsigned Code;
1712 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001713 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001714 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001715 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001717 Record.push_back(MI->isC99Varargs());
1718 Record.push_back(MI->isGNUVarargs());
1719 Record.push_back(MI->getNumArgs());
1720 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1721 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001722 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001723 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001724
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001725 // If we have a detailed preprocessing record, record the macro definition
1726 // ID that corresponds to this macro.
1727 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001728 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001729
Douglas Gregorc9490c02009-04-16 22:23:12 +00001730 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001731 Record.clear();
1732
Chris Lattnerdf961c22009-04-10 18:08:30 +00001733 // Emit the tokens array.
1734 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1735 // Note that we know that the preprocessor does not have any annotation
1736 // tokens in it because they are created by the parser, and thus can't be
1737 // in a macro definition.
1738 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Chris Lattnerdf961c22009-04-10 18:08:30 +00001740 Record.push_back(Tok.getLocation().getRawEncoding());
1741 Record.push_back(Tok.getLength());
1742
Chris Lattnerdf961c22009-04-10 18:08:30 +00001743 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1744 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001745 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001746 // FIXME: Should translate token kind to a stable encoding.
1747 Record.push_back(Tok.getKind());
1748 // FIXME: Should translate token flags to a stable encoding.
1749 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001751 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001752 Record.clear();
1753 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001754 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001755 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001756 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001757}
1758
1759void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001760 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001761 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001762
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001763 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001764
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001765 // Enter the preprocessor block.
1766 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001767
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001768 // If the preprocessor has a preprocessing record, emit it.
1769 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001770 using namespace llvm;
1771
1772 // Set up the abbreviation for
1773 unsigned InclusionAbbrev = 0;
1774 {
1775 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1776 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1781 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1782 }
1783
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001784 unsigned FirstPreprocessorEntityID
1785 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1786 + NUM_PREDEF_PP_ENTITY_IDS;
1787 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001788 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001789 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1790 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001791 E != EEnd;
1792 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001793 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001794
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001795 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1796 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001797
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001798 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001799 // Record this macro definition's ID.
1800 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001801
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001802 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001803 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1804 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001805 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001806
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001807 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001808 Record.push_back(ME->isBuiltinMacro());
1809 if (ME->isBuiltinMacro())
1810 AddIdentifierRef(ME->getName(), Record);
1811 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001812 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001813 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001814 continue;
1815 }
1816
1817 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1818 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001819 Record.push_back(ID->getFileName().size());
1820 Record.push_back(ID->wasInQuotes());
1821 Record.push_back(static_cast<unsigned>(ID->getKind()));
1822 llvm::SmallString<64> Buffer;
1823 Buffer += ID->getFileName();
1824 Buffer += ID->getFile()->getName();
1825 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1826 continue;
1827 }
1828
1829 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1830 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001831 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001832
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001833 // Write the offsets table for the preprocessing record.
1834 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001835 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1836
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001837 // Write the offsets table for identifier IDs.
1838 using namespace llvm;
1839 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001840 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001843 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001844
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001845 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001846 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001847 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001848 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1849 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001850 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001851}
1852
Douglas Gregore209e502011-12-06 01:10:29 +00001853unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1854 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1855 if (Known != SubmoduleIDs.end())
1856 return Known->second;
1857
1858 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1859}
1860
Douglas Gregor26ced122011-12-01 00:59:36 +00001861/// \brief Compute the number of modules within the given tree (including the
1862/// given module).
1863static unsigned getNumberOfModules(Module *Mod) {
1864 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001865 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1866 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001867 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001868 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001869
1870 return ChildModules + 1;
1871}
1872
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001873void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001874 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001875 // FIXME: This feels like it belongs somewhere else, but there are no
1876 // other consumers of this information.
1877 SourceManager &SrcMgr = PP->getSourceManager();
1878 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1879 for (ASTContext::import_iterator I = Context->local_import_begin(),
1880 IEnd = Context->local_import_end();
1881 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001882 if (Module *ImportedFrom
1883 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1884 SrcMgr))) {
1885 ImportedFrom->Imports.push_back(I->getImportedModule());
1886 }
1887 }
1888
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001889 // Enter the submodule description block.
1890 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1891
1892 // Write the abbreviations needed for the submodules block.
1893 using namespace llvm;
1894 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1895 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregor1e123682011-12-05 22:27:44 +00001900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
1901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
1902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1904 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1905
1906 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001907 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1909 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1910
1911 Abbrev = new BitCodeAbbrev();
1912 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1914 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001915
1916 Abbrev = new BitCodeAbbrev();
1917 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1919 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1920
Douglas Gregor51f564f2011-12-31 04:05:44 +00001921 Abbrev = new BitCodeAbbrev();
1922 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1924 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1925
Douglas Gregor26ced122011-12-01 00:59:36 +00001926 // Write the submodule metadata block.
1927 RecordData Record;
1928 Record.push_back(getNumberOfModules(WritingModule));
1929 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1930 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1931
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001932 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001933 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001934 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001935 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001936 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001937 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001938 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001939
1940 // Emit the definition of the block.
1941 Record.clear();
1942 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001943 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001944 if (Mod->Parent) {
1945 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1946 Record.push_back(SubmoduleIDs[Mod->Parent]);
1947 } else {
1948 Record.push_back(0);
1949 }
1950 Record.push_back(Mod->IsFramework);
1951 Record.push_back(Mod->IsExplicit);
Douglas Gregor1e123682011-12-05 22:27:44 +00001952 Record.push_back(Mod->InferSubmodules);
1953 Record.push_back(Mod->InferExplicitSubmodules);
1954 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001955 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1956
Douglas Gregor51f564f2011-12-31 04:05:44 +00001957 // Emit the requirements.
1958 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1959 Record.clear();
1960 Record.push_back(SUBMODULE_REQUIRES);
1961 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1962 Mod->Requires[I].data(),
1963 Mod->Requires[I].size());
1964 }
1965
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001966 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00001967 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001968 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001969 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001970 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00001971 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00001972 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1973 Record.clear();
1974 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1975 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1976 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001977 }
1978
1979 // Emit the headers.
1980 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1981 Record.clear();
1982 Record.push_back(SUBMODULE_HEADER);
1983 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1984 Mod->Headers[I]->getName());
1985 }
Douglas Gregor55988682011-12-05 16:33:54 +00001986
1987 // Emit the imports.
1988 if (!Mod->Imports.empty()) {
1989 Record.clear();
1990 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00001991 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00001992 assert(ImportedID && "Unknown submodule!");
1993 Record.push_back(ImportedID);
1994 }
1995 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
1996 }
1997
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00001998 // Emit the exports.
1999 if (!Mod->Exports.empty()) {
2000 Record.clear();
2001 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002002 if (Module *Exported = Mod->Exports[I].getPointer()) {
2003 unsigned ExportedID = SubmoduleIDs[Exported];
2004 assert(ExportedID > 0 && "Unknown submodule ID?");
2005 Record.push_back(ExportedID);
2006 } else {
2007 Record.push_back(0);
2008 }
2009
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002010 Record.push_back(Mod->Exports[I].getInt());
2011 }
2012 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2013 }
2014
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002015 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002016 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2017 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002018 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002019 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002020 }
2021
2022 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002023
2024 assert((NextSubmoduleID - FirstSubmoduleID
2025 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002026}
2027
Douglas Gregor185dbd72011-12-01 02:07:58 +00002028serialization::SubmoduleID
2029ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002030 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002031 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002032
2033 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002034 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002035 Module *OwningMod
2036 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002037 if (!OwningMod)
2038 return 0;
2039
Douglas Gregore209e502011-12-06 01:10:29 +00002040 // Check whether this submodule is part of our own module.
2041 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002042 return 0;
2043
Douglas Gregore209e502011-12-06 01:10:29 +00002044 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002045}
2046
David Blaikied6471f72011-09-25 23:23:43 +00002047void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002048 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002049 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002050 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2051 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002052 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002053 if (point.Loc.isInvalid())
2054 continue;
2055
2056 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002057 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002058 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002059 if (I->second.isPragma()) {
2060 Record.push_back(I->first);
2061 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002062 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002063 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002064 Record.push_back(-1); // mark the end of the diag/map pairs for this
2065 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002066 }
2067
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002068 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002069 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002070}
2071
Anders Carlssonc8505782011-03-06 18:41:18 +00002072void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2073 if (CXXBaseSpecifiersOffsets.empty())
2074 return;
2075
2076 RecordData Record;
2077
2078 // Create a blob abbreviation for the C++ base specifiers offsets.
2079 using namespace llvm;
2080
2081 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2082 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2085 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2086
Douglas Gregore92b8a12011-08-04 00:01:48 +00002087 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002088 Record.clear();
2089 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2090 Record.push_back(CXXBaseSpecifiersOffsets.size());
2091 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002092 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002093}
2094
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002095//===----------------------------------------------------------------------===//
2096// Type Serialization
2097//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002098
Sebastian Redl3397c552010-08-18 23:56:27 +00002099/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002100void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002101 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002102 if (Idx.getIndex() == 0) // we haven't seen this type before.
2103 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Douglas Gregor97475832010-10-05 18:37:06 +00002105 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002106
Douglas Gregor2cf26342009-04-09 22:27:44 +00002107 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002108 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002109 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002110 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002111 else if (TypeOffsets.size() < Index) {
2112 TypeOffsets.resize(Index + 1);
2113 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002114 }
2115
2116 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Douglas Gregor2cf26342009-04-09 22:27:44 +00002118 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002119 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002120
Douglas Gregora4923eb2009-11-16 21:35:15 +00002121 if (T.hasLocalNonFastQualifiers()) {
2122 Qualifiers Qs = T.getLocalQualifiers();
2123 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002124 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002125 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002126 } else {
2127 switch (T->getTypeClass()) {
2128 // For all of the concrete, non-dependent types, call the
2129 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002130#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002131 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002132#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002133#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002134 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002135 }
2136
2137 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002138 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002139
2140 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002141 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002142}
2143
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002144//===----------------------------------------------------------------------===//
2145// Declaration Serialization
2146//===----------------------------------------------------------------------===//
2147
Douglas Gregor2cf26342009-04-09 22:27:44 +00002148/// \brief Write the block containing all of the declaration IDs
2149/// lexically declared within the given DeclContext.
2150///
2151/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2152/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002153uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002154 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002155 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002156 return 0;
2157
Douglas Gregorc9490c02009-04-16 22:23:12 +00002158 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002159 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002160 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002161 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002162 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2163 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002164 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002165
Douglas Gregor25123082009-04-22 22:34:57 +00002166 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002167 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002168 return Offset;
2169}
2170
Sebastian Redla4232eb2010-08-18 23:56:21 +00002171void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002172 using namespace llvm;
2173 RecordData Record;
2174
2175 // Write the type offsets array
2176 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002177 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2181 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2182 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002183 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002184 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002185 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002186 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002187
2188 // Write the declaration offsets array
2189 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002190 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2194 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2195 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002196 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002197 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002198 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002199 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002200}
2201
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002202void ASTWriter::WriteFileDeclIDsMap() {
2203 using namespace llvm;
2204 RecordData Record;
2205
2206 // Join the vectors of DeclIDs from all files.
2207 SmallVector<DeclID, 256> FileSortedIDs;
2208 for (FileDeclIDsTy::iterator
2209 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2210 DeclIDInFileInfo &Info = *FI->second;
2211 Info.FirstDeclIndex = FileSortedIDs.size();
2212 for (LocDeclIDsTy::iterator
2213 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2214 FileSortedIDs.push_back(DI->second);
2215 }
2216
2217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2218 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2220 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2221 Record.push_back(FILE_SORTED_DECLS);
2222 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2223}
2224
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002225//===----------------------------------------------------------------------===//
2226// Global Method Pool and Selector Serialization
2227//===----------------------------------------------------------------------===//
2228
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002229namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002230// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002231class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002232 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002233
2234public:
2235 typedef Selector key_type;
2236 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Sebastian Redl5d050072010-08-04 17:20:04 +00002238 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002239 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002240 ObjCMethodList Instance, Factory;
2241 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002242 typedef const data_type& data_type_ref;
2243
Sebastian Redl3397c552010-08-18 23:56:27 +00002244 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002246 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002247 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002248 }
Mike Stump1eb44332009-09-09 15:08:12 +00002249
2250 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002251 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002252 data_type_ref Methods) {
2253 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2254 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002255 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2256 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002257 Method = Method->Next)
2258 if (Method->Method)
2259 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002260 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002261 Method = Method->Next)
2262 if (Method->Method)
2263 DataLen += 4;
2264 clang::io::Emit16(Out, DataLen);
2265 return std::make_pair(KeyLen, DataLen);
2266 }
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Chris Lattner5f9e2722011-07-23 10:55:15 +00002268 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002269 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002270 assert((Start >> 32) == 0 && "Selector key offset too large");
2271 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002272 unsigned N = Sel.getNumArgs();
2273 clang::io::Emit16(Out, N);
2274 if (N == 0)
2275 N = 1;
2276 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002277 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002278 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2279 }
Mike Stump1eb44332009-09-09 15:08:12 +00002280
Chris Lattner5f9e2722011-07-23 10:55:15 +00002281 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002282 data_type_ref Methods, unsigned DataLen) {
2283 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002284 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002285 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002286 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002287 Method = Method->Next)
2288 if (Method->Method)
2289 ++NumInstanceMethods;
2290
2291 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002292 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002293 Method = Method->Next)
2294 if (Method->Method)
2295 ++NumFactoryMethods;
2296
2297 clang::io::Emit16(Out, NumInstanceMethods);
2298 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002299 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002300 Method = Method->Next)
2301 if (Method->Method)
2302 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002303 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002304 Method = Method->Next)
2305 if (Method->Method)
2306 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002307
2308 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002309 }
2310};
2311} // end anonymous namespace
2312
Sebastian Redl059612d2010-08-03 21:58:15 +00002313/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002314///
2315/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002316/// in an on-disk hash table indexed by the selector. The hash table also
2317/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002318void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002319 using namespace llvm;
2320
Sebastian Redl059612d2010-08-03 21:58:15 +00002321 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002322 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002323 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002324 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002325 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002326 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002327 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002328 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Sebastian Redl059612d2010-08-03 21:58:15 +00002330 // Create the on-disk hash table representation. We walk through every
2331 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002332 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002333 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002334 I = SelectorIDs.begin(), E = SelectorIDs.end();
2335 I != E; ++I) {
2336 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002337 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002338 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002339 I->second,
2340 ObjCMethodList(),
2341 ObjCMethodList()
2342 };
2343 if (F != SemaRef.MethodPool.end()) {
2344 Data.Instance = F->second.first;
2345 Data.Factory = F->second.second;
2346 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002347 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002348 // changed.
2349 if (Chain && I->second < FirstSelectorID) {
2350 // Selector already exists. Did it change?
2351 bool changed = false;
2352 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2353 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002354 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002355 changed = true;
2356 }
2357 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2358 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002359 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002360 changed = true;
2361 }
2362 if (!changed)
2363 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002364 } else if (Data.Instance.Method || Data.Factory.Method) {
2365 // A new method pool entry.
2366 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002367 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002368 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002369 }
2370
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002371 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002372 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002373 uint32_t BucketOffset;
2374 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002375 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002376 llvm::raw_svector_ostream Out(MethodPool);
2377 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002378 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002379 BucketOffset = Generator.Emit(Out, Trait);
2380 }
2381
2382 // Create a blob abbreviation
2383 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002384 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2388 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2389
Douglas Gregor83941df2009-04-25 17:48:32 +00002390 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002391 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002392 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002393 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002394 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002395 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002396
2397 // Create a blob abbreviation for the selector table offsets.
2398 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002399 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2403 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2404
2405 // Write the selector offsets table.
2406 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002407 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002408 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002409 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002410 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002411 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002412 }
2413}
2414
Sebastian Redl3397c552010-08-18 23:56:27 +00002415/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002416void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002417 using namespace llvm;
2418 if (SemaRef.ReferencedSelectors.empty())
2419 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002420
Fariborz Jahanian32019832010-07-23 19:11:11 +00002421 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002422
Sebastian Redl3397c552010-08-18 23:56:27 +00002423 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002424 // very tricky to fix, and given that @selector shouldn't really appear in
2425 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002426 for (DenseMap<Selector, SourceLocation>::iterator S =
2427 SemaRef.ReferencedSelectors.begin(),
2428 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2429 Selector Sel = (*S).first;
2430 SourceLocation Loc = (*S).second;
2431 AddSelectorRef(Sel, Record);
2432 AddSourceLocation(Loc, Record);
2433 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002434 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002435}
2436
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002437//===----------------------------------------------------------------------===//
2438// Identifier Table Serialization
2439//===----------------------------------------------------------------------===//
2440
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002441namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002442class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002443 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002444 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002445 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002446 bool IsModule;
2447
Douglas Gregora92193e2009-04-28 21:18:29 +00002448 /// \brief Determines whether this is an "interesting" identifier
2449 /// that needs a full IdentifierInfo structure written into the hash
2450 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002451 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002452 if (II->isPoisoned() ||
2453 II->isExtensionToken() ||
2454 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002455 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002456 II->getFETokenInfo<void>())
2457 return true;
2458
Douglas Gregorce835df2011-09-14 22:14:14 +00002459 return hasMacroDefinition(II, Macro);
2460 }
2461
2462 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002463 if (!II->hasMacroDefinition())
2464 return false;
2465
Douglas Gregorce835df2011-09-14 22:14:14 +00002466 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002467 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002468
Douglas Gregorce835df2011-09-14 22:14:14 +00002469 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002470 }
2471
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002472public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002473 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002474 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002475
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002476 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002477 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002478
Douglas Gregoreee242f2011-10-27 09:33:13 +00002479 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2480 IdentifierResolver &IdResolver, bool IsModule)
2481 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002482
2483 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002484 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002485 }
Mike Stump1eb44332009-09-09 15:08:12 +00002486
2487 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002488 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002489 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002490 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002491 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002492 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002493 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002494 if (hasMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002495 DataLen += 8;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002496
2497 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2498 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002499 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002500 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002501 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002502 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002503 // We emit the key length after the data length so that every
2504 // string is preceded by a 16-bit length. This matches the PTH
2505 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002506 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002507 return std::make_pair(KeyLen, DataLen);
2508 }
Mike Stump1eb44332009-09-09 15:08:12 +00002509
Chris Lattner5f9e2722011-07-23 10:55:15 +00002510 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002511 unsigned KeyLen) {
2512 // Record the location of the key data. This is used when generating
2513 // the mapping from persistent IDs to strings.
2514 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002515 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002516 }
Mike Stump1eb44332009-09-09 15:08:12 +00002517
Douglas Gregor7143aab2011-09-01 17:04:32 +00002518 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002519 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002520 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002521 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002522 clang::io::Emit32(Out, ID << 1);
2523 return;
2524 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002525
Douglas Gregora92193e2009-04-28 21:18:29 +00002526 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002527 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002528 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002529 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topper925be542011-12-19 05:04:33 +00002530 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregorce835df2011-09-14 22:14:14 +00002531 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002532 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2533 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002534 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002535 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002536 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002537
Douglas Gregor13292642011-12-02 15:45:10 +00002538 if (HasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002539 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor13292642011-12-02 15:45:10 +00002540 clang::io::Emit32(Out,
2541 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2542 }
2543
Douglas Gregor668c1a42009-04-21 22:25:48 +00002544 // Emit the declaration IDs in reverse order, because the
2545 // IdentifierResolver provides the declarations as they would be
2546 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002547 // "stat"), but the ASTReader adds declarations to the end of the list
2548 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002549 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002550 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2551 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002552 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002553 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002554 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002555 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002556 }
2557};
2558} // end anonymous namespace
2559
Sebastian Redl3397c552010-08-18 23:56:27 +00002560/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002561///
2562/// The identifier table consists of a blob containing string data
2563/// (the actual identifiers themselves) and a separate "offsets" index
2564/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002565void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2566 IdentifierResolver &IdResolver,
2567 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002568 using namespace llvm;
2569
2570 // Create and write out the blob that contains the identifier
2571 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002572 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002573 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002574 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002575
Douglas Gregor92b059e2009-04-28 20:33:11 +00002576 // Look for any identifiers that were named while processing the
2577 // headers, but are otherwise not needed. We add these to the hash
2578 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002579 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002580 // file.
2581 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2582 IDEnd = PP.getIdentifierTable().end();
2583 ID != IDEnd; ++ID)
2584 getIdentifierRef(ID->second);
2585
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002586 // Create the on-disk hash table representation. We only store offsets
2587 // for identifiers that appear here for the first time.
2588 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002589 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002590 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2591 ID != IDEnd; ++ID) {
2592 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002593 if (!Chain || !ID->first->isFromAST() ||
2594 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002595 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2596 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002597 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002598
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002599 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002600 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002601 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002602 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002603 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002604 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002605 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002606 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002607 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002608 }
2609
2610 // Create a blob abbreviation
2611 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002615 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002616
2617 // Write the identifier table
2618 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002619 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002620 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002621 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002622 }
2623
2624 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002625 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002626 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2630 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2631
2632 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002633 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002634 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002635 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002636 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002637 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002638}
2639
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002640//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002641// DeclContext's Name Lookup Table Serialization
2642//===----------------------------------------------------------------------===//
2643
2644namespace {
2645// Trait used for the on-disk hash table used in the method pool.
2646class ASTDeclContextNameLookupTrait {
2647 ASTWriter &Writer;
2648
2649public:
2650 typedef DeclarationName key_type;
2651 typedef key_type key_type_ref;
2652
2653 typedef DeclContext::lookup_result data_type;
2654 typedef const data_type& data_type_ref;
2655
2656 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2657
2658 unsigned ComputeHash(DeclarationName Name) {
2659 llvm::FoldingSetNodeID ID;
2660 ID.AddInteger(Name.getNameKind());
2661
2662 switch (Name.getNameKind()) {
2663 case DeclarationName::Identifier:
2664 ID.AddString(Name.getAsIdentifierInfo()->getName());
2665 break;
2666 case DeclarationName::ObjCZeroArgSelector:
2667 case DeclarationName::ObjCOneArgSelector:
2668 case DeclarationName::ObjCMultiArgSelector:
2669 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2670 break;
2671 case DeclarationName::CXXConstructorName:
2672 case DeclarationName::CXXDestructorName:
2673 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002674 break;
2675 case DeclarationName::CXXOperatorName:
2676 ID.AddInteger(Name.getCXXOverloadedOperator());
2677 break;
2678 case DeclarationName::CXXLiteralOperatorName:
2679 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2680 case DeclarationName::CXXUsingDirective:
2681 break;
2682 }
2683
2684 return ID.ComputeHash();
2685 }
2686
2687 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002688 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002689 data_type_ref Lookup) {
2690 unsigned KeyLen = 1;
2691 switch (Name.getNameKind()) {
2692 case DeclarationName::Identifier:
2693 case DeclarationName::ObjCZeroArgSelector:
2694 case DeclarationName::ObjCOneArgSelector:
2695 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002696 case DeclarationName::CXXLiteralOperatorName:
2697 KeyLen += 4;
2698 break;
2699 case DeclarationName::CXXOperatorName:
2700 KeyLen += 1;
2701 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002702 case DeclarationName::CXXConstructorName:
2703 case DeclarationName::CXXDestructorName:
2704 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002705 case DeclarationName::CXXUsingDirective:
2706 break;
2707 }
2708 clang::io::Emit16(Out, KeyLen);
2709
2710 // 2 bytes for num of decls and 4 for each DeclID.
2711 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2712 clang::io::Emit16(Out, DataLen);
2713
2714 return std::make_pair(KeyLen, DataLen);
2715 }
2716
Chris Lattner5f9e2722011-07-23 10:55:15 +00002717 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002718 using namespace clang::io;
2719
2720 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2721 Emit8(Out, Name.getNameKind());
2722 switch (Name.getNameKind()) {
2723 case DeclarationName::Identifier:
2724 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2725 break;
2726 case DeclarationName::ObjCZeroArgSelector:
2727 case DeclarationName::ObjCOneArgSelector:
2728 case DeclarationName::ObjCMultiArgSelector:
2729 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2730 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002731 case DeclarationName::CXXOperatorName:
2732 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2733 Emit8(Out, Name.getCXXOverloadedOperator());
2734 break;
2735 case DeclarationName::CXXLiteralOperatorName:
2736 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2737 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002738 case DeclarationName::CXXConstructorName:
2739 case DeclarationName::CXXDestructorName:
2740 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002741 case DeclarationName::CXXUsingDirective:
2742 break;
2743 }
2744 }
2745
Chris Lattner5f9e2722011-07-23 10:55:15 +00002746 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002747 data_type Lookup, unsigned DataLen) {
2748 uint64_t Start = Out.tell(); (void)Start;
2749 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2750 for (; Lookup.first != Lookup.second; ++Lookup.first)
2751 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2752
2753 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2754 }
2755};
2756} // end anonymous namespace
2757
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002758/// \brief Write the block containing all of the declaration IDs
2759/// visible from the given DeclContext.
2760///
2761/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002762/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002763uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2764 DeclContext *DC) {
2765 if (DC->getPrimaryContext() != DC)
2766 return 0;
2767
2768 // Since there is no name lookup into functions or methods, don't bother to
2769 // build a visible-declarations table for these entities.
2770 if (DC->isFunctionOrMethod())
2771 return 0;
2772
2773 // If not in C++, we perform name lookup for the translation unit via the
2774 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2775 // FIXME: In C++ we need the visible declarations in order to "see" the
2776 // friend declarations, is there a way to do this without writing the table ?
2777 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2778 return 0;
2779
2780 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002781 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002782 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002783
2784 // Serialize the contents of the mapping used for lookup. Note that,
2785 // although we have two very different code paths, the serialized
2786 // representation is the same for both cases: a declaration name,
2787 // followed by a size, followed by references to the visible
2788 // declarations that have that name.
2789 uint64_t Offset = Stream.GetCurrentBitNo();
2790 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2791 if (!Map || Map->empty())
2792 return 0;
2793
2794 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2795 ASTDeclContextNameLookupTrait Trait(*this);
2796
2797 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002798 DeclarationName ConversionName;
2799 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002800 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2801 D != DEnd; ++D) {
2802 DeclarationName Name = D->first;
2803 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002804 if (Result.first != Result.second) {
2805 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2806 // Hash all conversion function names to the same name. The actual
2807 // type information in conversion function name is not used in the
2808 // key (since such type information is not stable across different
2809 // modules), so the intended effect is to coalesce all of the conversion
2810 // functions under a single key.
2811 if (!ConversionName)
2812 ConversionName = Name;
2813 ConversionDecls.append(Result.first, Result.second);
2814 continue;
2815 }
2816
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002817 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002818 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002819 }
2820
Douglas Gregore5a54b62011-08-30 20:49:19 +00002821 // Add the conversion functions
2822 if (!ConversionDecls.empty()) {
2823 Generator.insert(ConversionName,
2824 DeclContext::lookup_result(ConversionDecls.begin(),
2825 ConversionDecls.end()),
2826 Trait);
2827 }
2828
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002829 // Create the on-disk hash table in a buffer.
2830 llvm::SmallString<4096> LookupTable;
2831 uint32_t BucketOffset;
2832 {
2833 llvm::raw_svector_ostream Out(LookupTable);
2834 // Make sure that no bucket is at offset 0
2835 clang::io::Emit32(Out, 0);
2836 BucketOffset = Generator.Emit(Out, Trait);
2837 }
2838
2839 // Write the lookup table
2840 RecordData Record;
2841 Record.push_back(DECL_CONTEXT_VISIBLE);
2842 Record.push_back(BucketOffset);
2843 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2844 LookupTable.str());
2845
2846 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2847 ++NumVisibleDeclContexts;
2848 return Offset;
2849}
2850
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002851/// \brief Write an UPDATE_VISIBLE block for the given context.
2852///
2853/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2854/// DeclContext in a dependent AST file. As such, they only exist for the TU
2855/// (in C++) and for namespaces.
2856void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002857 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2858 if (!Map || Map->empty())
2859 return;
2860
2861 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2862 ASTDeclContextNameLookupTrait Trait(*this);
2863
2864 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002865 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2866 D != DEnd; ++D) {
2867 DeclarationName Name = D->first;
2868 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002869 // For any name that appears in this table, the results are complete, i.e.
2870 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002871 if (Result.first != Result.second)
2872 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002873 }
2874
2875 // Create the on-disk hash table in a buffer.
2876 llvm::SmallString<4096> LookupTable;
2877 uint32_t BucketOffset;
2878 {
2879 llvm::raw_svector_ostream Out(LookupTable);
2880 // Make sure that no bucket is at offset 0
2881 clang::io::Emit32(Out, 0);
2882 BucketOffset = Generator.Emit(Out, Trait);
2883 }
2884
2885 // Write the lookup table
2886 RecordData Record;
2887 Record.push_back(UPDATE_VISIBLE);
2888 Record.push_back(getDeclID(cast<Decl>(DC)));
2889 Record.push_back(BucketOffset);
2890 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2891}
2892
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002893/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2894void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2895 RecordData Record;
2896 Record.push_back(Opts.fp_contract);
2897 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2898}
2899
2900/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2901void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2902 if (!SemaRef.Context.getLangOptions().OpenCL)
2903 return;
2904
2905 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2906 RecordData Record;
2907#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2908#include "clang/Basic/OpenCLExtensions.def"
2909 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2910}
2911
Douglas Gregor2171bf12012-01-15 16:58:34 +00002912void ASTWriter::WriteRedeclarations() {
2913 RecordData LocalRedeclChains;
2914 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2915
2916 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2917 Decl *First = Redeclarations[I];
2918 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2919
2920 Decl *MostRecent = First->getMostRecentDecl();
2921
2922 // If we only have a single declaration, there is no point in storing
2923 // a redeclaration chain.
2924 if (First == MostRecent)
2925 continue;
2926
2927 unsigned Offset = LocalRedeclChains.size();
2928 unsigned Size = 0;
2929 LocalRedeclChains.push_back(0); // Placeholder for the size.
2930
2931 // Collect the set of local redeclarations of this declaration.
2932 for (Decl *Prev = MostRecent; Prev != First;
2933 Prev = Prev->getPreviousDecl()) {
2934 if (!Prev->isFromASTFile()) {
2935 AddDeclRef(Prev, LocalRedeclChains);
2936 ++Size;
2937 }
2938 }
2939 LocalRedeclChains[Offset] = Size;
2940
2941 // Reverse the set of local redeclarations, so that we store them in
2942 // order (since we found them in reverse order).
2943 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2944
2945 // Add the mapping from the first ID to the set of local declarations.
2946 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2947 LocalRedeclsMap.push_back(Info);
2948
2949 assert(N == Redeclarations.size() &&
2950 "Deserialized a declaration we shouldn't have");
2951 }
2952
2953 if (LocalRedeclChains.empty())
2954 return;
2955
2956 // Sort the local redeclarations map by the first declaration ID,
2957 // since the reader will be performing binary searches on this information.
2958 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2959
2960 // Emit the local redeclarations map.
2961 using namespace llvm;
2962 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2963 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2966 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
2967
2968 RecordData Record;
2969 Record.push_back(LOCAL_REDECLARATIONS_MAP);
2970 Record.push_back(LocalRedeclsMap.size());
2971 Stream.EmitRecordWithBlob(AbbrevID, Record,
2972 reinterpret_cast<char*>(LocalRedeclsMap.data()),
2973 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
2974
2975 // Emit the redeclaration chains.
2976 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
2977}
2978
Douglas Gregorcff9f262012-01-27 01:47:08 +00002979void ASTWriter::WriteObjCCategories() {
2980 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
2981 RecordData Categories;
2982
2983 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
2984 unsigned Size = 0;
2985 unsigned StartIndex = Categories.size();
2986
2987 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
2988
2989 // Allocate space for the size.
2990 Categories.push_back(0);
2991
2992 // Add the categories.
2993 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
2994 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
2995 assert(getDeclID(Cat) != 0 && "Bogus category");
2996 AddDeclRef(Cat, Categories);
2997 }
2998
2999 // Update the size.
3000 Categories[StartIndex] = Size;
3001
3002 // Record this interface -> category map.
3003 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3004 CategoriesMap.push_back(CatInfo);
3005 }
3006
3007 // Sort the categories map by the definition ID, since the reader will be
3008 // performing binary searches on this information.
3009 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3010
3011 // Emit the categories map.
3012 using namespace llvm;
3013 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3014 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3017 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3018
3019 RecordData Record;
3020 Record.push_back(OBJC_CATEGORIES_MAP);
3021 Record.push_back(CategoriesMap.size());
3022 Stream.EmitRecordWithBlob(AbbrevID, Record,
3023 reinterpret_cast<char*>(CategoriesMap.data()),
3024 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3025
3026 // Emit the category lists.
3027 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3028}
3029
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003030void ASTWriter::WriteMergedDecls() {
3031 if (!Chain || Chain->MergedDecls.empty())
3032 return;
3033
3034 RecordData Record;
3035 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3036 IEnd = Chain->MergedDecls.end();
3037 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003038 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003039 : getDeclID(I->first);
3040 assert(CanonID && "Merged declaration not known?");
3041
3042 Record.push_back(CanonID);
3043 Record.push_back(I->second.size());
3044 Record.append(I->second.begin(), I->second.end());
3045 }
3046 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3047}
3048
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003049//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003050// General Serialization Routines
3051//===----------------------------------------------------------------------===//
3052
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003053/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003054void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003055 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00003056 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
3057 const Attr * A = *i;
3058 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003059 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003060
Sean Huntcf807c42010-08-18 23:23:40 +00003061#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003062
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003063 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003064}
3065
Chris Lattner5f9e2722011-07-23 10:55:15 +00003066void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003067 Record.push_back(Str.size());
3068 Record.insert(Record.end(), Str.begin(), Str.end());
3069}
3070
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003071void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3072 RecordDataImpl &Record) {
3073 Record.push_back(Version.getMajor());
3074 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3075 Record.push_back(*Minor + 1);
3076 else
3077 Record.push_back(0);
3078 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3079 Record.push_back(*Subminor + 1);
3080 else
3081 Record.push_back(0);
3082}
3083
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003084/// \brief Note that the identifier II occurs at the given offset
3085/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003086void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003087 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003088 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003089 // up earlier in the chain and thus don't need an offset.
3090 if (ID >= FirstIdentID)
3091 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003092}
3093
Douglas Gregor83941df2009-04-25 17:48:32 +00003094/// \brief Note that the selector Sel occurs at the given offset
3095/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003096void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003097 unsigned ID = SelectorIDs[Sel];
3098 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003099 // Don't record offsets for selectors that are also available in a different
3100 // file.
3101 if (ID < FirstSelectorID)
3102 return;
3103 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003104}
3105
Sebastian Redla4232eb2010-08-18 23:56:21 +00003106ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003107 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
3108 WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003109 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003110 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003111 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003112 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3113 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003114 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003115 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003116 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003117 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003118 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003119 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003120 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3121 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3122 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003123 DeclTypedefAbbrev(0),
3124 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3125 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003126{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003127}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003128
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003129ASTWriter::~ASTWriter() {
3130 for (FileDeclIDsTy::iterator
3131 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3132 delete I->second;
3133}
3134
Sebastian Redla4232eb2010-08-18 23:56:21 +00003135void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003136 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003137 Module *WritingModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003138 WritingAST = true;
3139
Douglas Gregor2cf26342009-04-09 22:27:44 +00003140 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003141 Stream.Emit((unsigned)'C', 8);
3142 Stream.Emit((unsigned)'P', 8);
3143 Stream.Emit((unsigned)'C', 8);
3144 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003146 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003147
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003148 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003149 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003150 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003151 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003152 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003153 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003154 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003155
3156 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003157}
3158
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003159template<typename Vector>
3160static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3161 ASTWriter::RecordData &Record) {
3162 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3163 I != E; ++I) {
3164 Writer.AddDeclRef(*I, Record);
3165 }
3166}
3167
Sebastian Redla4232eb2010-08-18 23:56:21 +00003168void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003169 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003170 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003171 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003172 using namespace llvm;
3173
Douglas Gregorecc2c092011-12-01 22:20:10 +00003174 // Make sure that the AST reader knows to finalize itself.
3175 if (Chain)
3176 Chain->finalizeForWriting();
3177
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003178 ASTContext &Context = SemaRef.Context;
3179 Preprocessor &PP = SemaRef.PP;
3180
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003181 // Set up predefined declaration IDs.
3182 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003183 if (Context.ObjCIdDecl)
3184 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003185 if (Context.ObjCSelDecl)
3186 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003187 if (Context.ObjCClassDecl)
3188 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003189 if (Context.ObjCProtocolClassDecl)
3190 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003191 if (Context.Int128Decl)
3192 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3193 if (Context.UInt128Decl)
3194 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003195 if (Context.ObjCInstanceTypeDecl)
3196 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003197
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003198 if (!Chain) {
3199 // Make sure that we emit IdentifierInfos (and any attached
3200 // declarations) for builtins. We don't need to do this when we're
3201 // emitting chained PCH files, because all of the builtins will be
3202 // in the original PCH file.
3203 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003204 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003205 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003206 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
3207 Context.getLangOptions().NoBuiltin);
3208 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3209 getIdentifierRef(&Table.get(BuiltinNames[I]));
3210 }
3211
Douglas Gregoreee242f2011-10-27 09:33:13 +00003212 // If there are any out-of-date identifiers, bring them up to date.
3213 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3214 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3215 IDEnd = PP.getIdentifierTable().end();
3216 ID != IDEnd; ++ID)
3217 if (ID->second->isOutOfDate())
3218 ExtSource->updateOutOfDateIdentifier(*ID->second);
3219 }
3220
Chris Lattner63d65f82009-09-08 18:19:27 +00003221 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003222 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003223 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003224 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003225 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003226
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003227 // Build a record containing all of the file scoped decls in this file.
3228 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003229 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3230 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003231
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003232 // Build a record containing all of the delegating constructors we still need
3233 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003234 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003235 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003236
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003237 // Write the set of weak, undeclared identifiers. We always write the
3238 // entire table, since later PCH files in a PCH chain are only interested in
3239 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003240 RecordData WeakUndeclaredIdentifiers;
3241 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003242 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003243 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3244 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3245 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3246 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3247 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3248 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3249 }
3250 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003251
Douglas Gregor14c22f22009-04-22 22:18:58 +00003252 // Build a record containing all of the locally-scoped external
3253 // declarations in this header file. Generally, this record will be
3254 // empty.
3255 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003256 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003257 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003258 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003259 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3260 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003261 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003262 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003263 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3264 }
3265
Douglas Gregorb81c1702009-04-27 20:06:05 +00003266 // Build a record containing all of the ext_vector declarations.
3267 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003268 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003269
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003270 // Build a record containing all of the VTable uses information.
3271 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003272 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003273 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3274 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3275 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3276 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3277 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003278 }
3279
3280 // Build a record containing all of dynamic classes declarations.
3281 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003282 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003283
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003284 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003285 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003286 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003287 I = SemaRef.PendingInstantiations.begin(),
3288 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3289 AddDeclRef(I->first, PendingInstantiations);
3290 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003291 }
3292 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3293 "There are local ones at end of translation unit!");
3294
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003295 // Build a record containing some declaration references.
3296 RecordData SemaDeclRefs;
3297 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3298 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3299 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3300 }
3301
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003302 RecordData CUDASpecialDeclRefs;
3303 if (Context.getcudaConfigureCallDecl()) {
3304 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3305 }
3306
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003307 // Build a record containing all of the known namespaces.
3308 RecordData KnownNamespaces;
3309 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3310 I = SemaRef.KnownNamespaces.begin(),
3311 IEnd = SemaRef.KnownNamespaces.end();
3312 I != IEnd; ++I) {
3313 if (!I->second)
3314 AddDeclRef(I->first, KnownNamespaces);
3315 }
3316
Sebastian Redl3397c552010-08-18 23:56:27 +00003317 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003318 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003319 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003320 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003321 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00003322 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003323 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003324
3325 // Create a lexical update block containing all of the declarations in the
3326 // translation unit that do not come from other AST files.
3327 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3328 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3329 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3330 E = TU->noload_decls_end();
3331 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003332 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003333 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003334 }
3335
3336 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3337 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3338 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3339 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3340 Record.clear();
3341 Record.push_back(TU_UPDATE_LEXICAL);
3342 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3343 data(NewGlobalDecls));
3344
3345 // And a visible updates block for the translation unit.
3346 Abv = new llvm::BitCodeAbbrev();
3347 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3348 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3349 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3350 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3351 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3352 WriteDeclContextVisibleUpdate(TU);
3353
3354 // If the translation unit has an anonymous namespace, and we don't already
3355 // have an update block for it, write it as an update block.
3356 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3357 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3358 if (Record.empty()) {
3359 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003360 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003361 }
3362 }
3363
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003364 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003365 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003366
Douglas Gregora119da02011-08-02 16:26:37 +00003367 // Form the record of special types.
3368 RecordData SpecialTypes;
3369 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003370 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003371 AddTypeRef(Context.getFILEType(), SpecialTypes);
3372 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3373 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3374 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3375 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003376 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003377 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003378
Douglas Gregor366809a2009-04-26 03:49:13 +00003379 // Keep writing types and declarations until all types and
3380 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003381 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003382 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003383 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3384 E = DeclsToRewrite.end();
3385 I != E; ++I)
3386 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003387 while (!DeclTypesToEmit.empty()) {
3388 DeclOrType DOT = DeclTypesToEmit.front();
3389 DeclTypesToEmit.pop();
3390 if (DOT.isType())
3391 WriteType(DOT.getType());
3392 else
3393 WriteDecl(Context, DOT.getDecl());
3394 }
3395 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003396
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003397 WriteFileDeclIDsMap();
3398 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3399
3400 if (Chain) {
3401 // Write the mapping information describing our module dependencies and how
3402 // each of those modules were mapped into our own offset/ID space, so that
3403 // the reader can build the appropriate mapping to its own offset/ID space.
3404 // The map consists solely of a blob with the following format:
3405 // *(module-name-len:i16 module-name:len*i8
3406 // source-location-offset:i32
3407 // identifier-id:i32
3408 // preprocessed-entity-id:i32
3409 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003410 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003411 // selector-id:i32
3412 // declaration-id:i32
3413 // c++-base-specifiers-id:i32
3414 // type-id:i32)
3415 //
3416 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3417 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3419 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
3420 llvm::SmallString<2048> Buffer;
3421 {
3422 llvm::raw_svector_ostream Out(Buffer);
3423 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003424 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003425 M != MEnd; ++M) {
3426 StringRef FileName = (*M)->FileName;
3427 io::Emit16(Out, FileName.size());
3428 Out.write(FileName.data(), FileName.size());
3429 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3430 io::Emit32(Out, (*M)->BaseIdentifierID);
3431 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003432 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003433 io::Emit32(Out, (*M)->BaseSelectorID);
3434 io::Emit32(Out, (*M)->BaseDeclID);
3435 io::Emit32(Out, (*M)->BaseTypeIndex);
3436 }
3437 }
3438 Record.clear();
3439 Record.push_back(MODULE_OFFSET_MAP);
3440 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3441 Buffer.data(), Buffer.size());
3442 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003443 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003444 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003445 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003446 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003447 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003448 WriteFPPragmaOptions(SemaRef.getFPOptions());
3449 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003450
Sebastian Redl1476ed42010-07-16 16:36:56 +00003451 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003452 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003453
Anders Carlssonc8505782011-03-06 18:41:18 +00003454 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003455
Douglas Gregore209e502011-12-06 01:10:29 +00003456 // If we're emitting a module, write out the submodule information.
3457 if (WritingModule)
3458 WriteSubmodules(WritingModule);
3459
Douglas Gregora119da02011-08-02 16:26:37 +00003460 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3461
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003462 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003463 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003464 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003465
3466 // Write the record containing tentative definitions.
3467 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003468 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003469
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003470 // Write the record containing unused file scoped decls.
3471 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003472 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003473
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003474 // Write the record containing weak undeclared identifiers.
3475 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003476 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003477 WeakUndeclaredIdentifiers);
3478
Douglas Gregor14c22f22009-04-22 22:18:58 +00003479 // Write the record containing locally-scoped external definitions.
3480 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003481 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003482 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003483
3484 // Write the record containing ext_vector type names.
3485 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003486 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003487
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003488 // Write the record containing VTable uses information.
3489 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003490 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003491
3492 // Write the record containing dynamic classes declarations.
3493 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003494 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003495
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003496 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003497 if (!PendingInstantiations.empty())
3498 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003499
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003500 // Write the record containing declaration references of Sema.
3501 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003502 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003503
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003504 // Write the record containing CUDA-specific declaration references.
3505 if (!CUDASpecialDeclRefs.empty())
3506 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003507
3508 // Write the delegating constructors.
3509 if (!DelegatingCtorDecls.empty())
3510 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003511
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003512 // Write the known namespaces.
3513 if (!KnownNamespaces.empty())
3514 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3515
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003516 // Write the visible updates to DeclContexts.
3517 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3518 I = UpdatedDeclContexts.begin(),
3519 E = UpdatedDeclContexts.end();
3520 I != E; ++I)
3521 WriteDeclContextVisibleUpdate(*I);
3522
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003523 if (!WritingModule) {
3524 // Write the submodules that were imported, if any.
3525 RecordData ImportedModules;
3526 for (ASTContext::import_iterator I = Context.local_import_begin(),
3527 IEnd = Context.local_import_end();
3528 I != IEnd; ++I) {
3529 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3530 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3531 }
3532 if (!ImportedModules.empty()) {
3533 // Sort module IDs.
3534 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3535
3536 // Unique module IDs.
3537 ImportedModules.erase(std::unique(ImportedModules.begin(),
3538 ImportedModules.end()),
3539 ImportedModules.end());
3540
3541 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3542 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003543 }
3544
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003545 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003546 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003547 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003548 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003549 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003550
Douglas Gregor3e1af842009-04-17 22:13:46 +00003551 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003552 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003553 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003554 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003555 Record.push_back(NumLexicalDeclContexts);
3556 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003557 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003558 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003559}
3560
Douglas Gregor61c5e342011-09-17 00:05:03 +00003561/// \brief Go through the declaration update blocks and resolve declaration
3562/// pointers into declaration IDs.
3563void ASTWriter::ResolveDeclUpdatesBlocks() {
3564 for (DeclUpdateMap::iterator
3565 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3566 const Decl *D = I->first;
3567 UpdateRecord &URec = I->second;
3568
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003569 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003570 continue; // The decl will be written completely
3571
3572 unsigned Idx = 0, N = URec.size();
3573 while (Idx < N) {
3574 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003575 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3576 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3577 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3578 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3579 ++Idx;
3580 break;
3581
3582 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3583 ++Idx;
3584 break;
3585 }
3586 }
3587 }
3588}
3589
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003590void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003591 if (DeclUpdates.empty())
3592 return;
3593
3594 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003595 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003596 for (DeclUpdateMap::iterator
3597 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3598 const Decl *D = I->first;
3599 UpdateRecord &URec = I->second;
3600
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003601 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003602 continue; // The decl will be written completely,no need to store updates.
3603
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003604 uint64_t Offset = Stream.GetCurrentBitNo();
3605 Stream.EmitRecord(DECL_UPDATES, URec);
3606
3607 OffsetsRecord.push_back(GetDeclRef(D));
3608 OffsetsRecord.push_back(Offset);
3609 }
3610 Stream.ExitBlock();
3611 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3612}
3613
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003614void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003615 if (ReplacedDecls.empty())
3616 return;
3617
3618 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003619 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003620 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003621 Record.push_back(I->ID);
3622 Record.push_back(I->Offset);
3623 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003624 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003625 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003626}
3627
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003628void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003629 Record.push_back(Loc.getRawEncoding());
3630}
3631
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003632void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003633 AddSourceLocation(Range.getBegin(), Record);
3634 AddSourceLocation(Range.getEnd(), Record);
3635}
3636
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003637void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003638 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003639 const uint64_t *Words = Value.getRawData();
3640 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003641}
3642
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003643void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003644 Record.push_back(Value.isUnsigned());
3645 AddAPInt(Value, Record);
3646}
3647
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003648void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003649 AddAPInt(Value.bitcastToAPInt(), Record);
3650}
3651
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003652void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003653 Record.push_back(getIdentifierRef(II));
3654}
3655
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003656IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003657 if (II == 0)
3658 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003659
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003660 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003661 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003662 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003663 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003664}
3665
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003666void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003667 Record.push_back(getSelectorRef(SelRef));
3668}
3669
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003670SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003671 if (Sel.getAsOpaquePtr() == 0) {
3672 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003673 }
3674
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003675 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003676 if (SID == 0 && Chain) {
3677 // This might trigger a ReadSelector callback, which will set the ID for
3678 // this selector.
3679 Chain->LoadSelector(Sel);
3680 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003681 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003682 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003683 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003684 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003685}
3686
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003687void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003688 AddDeclRef(Temp->getDestructor(), Record);
3689}
3690
Douglas Gregor7c789c12010-10-29 22:39:52 +00003691void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3692 CXXBaseSpecifier const *BasesEnd,
3693 RecordDataImpl &Record) {
3694 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3695 CXXBaseSpecifiersToWrite.push_back(
3696 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3697 Bases, BasesEnd));
3698 Record.push_back(NextCXXBaseSpecifiersID++);
3699}
3700
Sebastian Redla4232eb2010-08-18 23:56:21 +00003701void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003702 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003703 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003704 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003705 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003706 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003707 break;
3708 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003709 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003710 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003711 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003712 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003713 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003714 break;
3715 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003716 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003717 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003718 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003719 break;
John McCall833ca992009-10-29 08:12:44 +00003720 case TemplateArgument::Null:
3721 case TemplateArgument::Integral:
3722 case TemplateArgument::Declaration:
3723 case TemplateArgument::Pack:
3724 break;
3725 }
3726}
3727
Sebastian Redla4232eb2010-08-18 23:56:21 +00003728void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003729 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003730 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003731
3732 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3733 bool InfoHasSameExpr
3734 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3735 Record.push_back(InfoHasSameExpr);
3736 if (InfoHasSameExpr)
3737 return; // Avoid storing the same expr twice.
3738 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003739 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3740 Record);
3741}
3742
Douglas Gregordc355712011-02-25 00:36:19 +00003743void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3744 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003745 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003746 AddTypeRef(QualType(), Record);
3747 return;
3748 }
3749
Douglas Gregordc355712011-02-25 00:36:19 +00003750 AddTypeLoc(TInfo->getTypeLoc(), Record);
3751}
3752
3753void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3754 AddTypeRef(TL.getType(), Record);
3755
John McCalla1ee0c52009-10-16 21:56:05 +00003756 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003757 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003758 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003759}
3760
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003761void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003762 Record.push_back(GetOrCreateTypeID(T));
3763}
3764
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003765TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3766 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003767 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3768}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003769
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003770TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003771 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003772 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003773}
3774
3775TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3776 if (T.isNull())
3777 return TypeIdx();
3778 assert(!T.getLocalFastQualifiers());
3779
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003780 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003781 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003782 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003783 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003784 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003785 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003786 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003787 return Idx;
3788}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003789
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003790TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003791 if (T.isNull())
3792 return TypeIdx();
3793 assert(!T.getLocalFastQualifiers());
3794
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003795 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3796 assert(I != TypeIdxs.end() && "Type not emitted!");
3797 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003798}
3799
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003800void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003801 Record.push_back(GetDeclRef(D));
3802}
3803
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003804DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003805 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3806
Douglas Gregor2cf26342009-04-09 22:27:44 +00003807 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003808 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003809 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003810
3811 // If D comes from an AST file, its declaration ID is already known and
3812 // fixed.
3813 if (D->isFromASTFile())
3814 return D->getGlobalID();
3815
Douglas Gregor97475832010-10-05 18:37:06 +00003816 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003817 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003818 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003819 // We haven't seen this declaration before. Give it a new ID and
3820 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003821 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003822 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003823 }
3824
Sebastian Redl681d7232010-07-27 00:17:23 +00003825 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003826}
3827
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003828DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003829 if (D == 0)
3830 return 0;
3831
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003832 // If D comes from an AST file, its declaration ID is already known and
3833 // fixed.
3834 if (D->isFromASTFile())
3835 return D->getGlobalID();
3836
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003837 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3838 return DeclIDs[D];
3839}
3840
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003841static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3842 std::pair<unsigned, serialization::DeclID> R) {
3843 return L.first < R.first;
3844}
3845
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003846void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003847 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003848 assert(D);
3849
3850 SourceLocation Loc = D->getLocation();
3851 if (Loc.isInvalid())
3852 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003853
3854 // We only keep track of the file-level declarations of each file.
3855 if (!D->getLexicalDeclContext()->isFileContext())
3856 return;
3857
3858 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003859 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003860 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003861 FileID FID;
3862 unsigned Offset;
3863 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003864 if (FID.isInvalid())
3865 return;
3866 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3867 assert(Entry->isFile());
3868
3869 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3870 if (!Info)
3871 Info = new DeclIDInFileInfo();
3872
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003873 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003874 LocDeclIDsTy &Decls = Info->DeclIDs;
3875
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003876 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003877 Decls.push_back(LocDecl);
3878 return;
3879 }
3880
3881 LocDeclIDsTy::iterator
3882 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3883
3884 Decls.insert(I, LocDecl);
3885}
3886
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003887void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003888 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003889 Record.push_back(Name.getNameKind());
3890 switch (Name.getNameKind()) {
3891 case DeclarationName::Identifier:
3892 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3893 break;
3894
3895 case DeclarationName::ObjCZeroArgSelector:
3896 case DeclarationName::ObjCOneArgSelector:
3897 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003898 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003899 break;
3900
3901 case DeclarationName::CXXConstructorName:
3902 case DeclarationName::CXXDestructorName:
3903 case DeclarationName::CXXConversionFunctionName:
3904 AddTypeRef(Name.getCXXNameType(), Record);
3905 break;
3906
3907 case DeclarationName::CXXOperatorName:
3908 Record.push_back(Name.getCXXOverloadedOperator());
3909 break;
3910
Sean Hunt3e518bd2009-11-29 07:34:05 +00003911 case DeclarationName::CXXLiteralOperatorName:
3912 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3913 break;
3914
Douglas Gregor2cf26342009-04-09 22:27:44 +00003915 case DeclarationName::CXXUsingDirective:
3916 // No extra data to emit
3917 break;
3918 }
3919}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003920
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003921void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003922 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003923 switch (Name.getNameKind()) {
3924 case DeclarationName::CXXConstructorName:
3925 case DeclarationName::CXXDestructorName:
3926 case DeclarationName::CXXConversionFunctionName:
3927 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3928 break;
3929
3930 case DeclarationName::CXXOperatorName:
3931 AddSourceLocation(
3932 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3933 Record);
3934 AddSourceLocation(
3935 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3936 Record);
3937 break;
3938
3939 case DeclarationName::CXXLiteralOperatorName:
3940 AddSourceLocation(
3941 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3942 Record);
3943 break;
3944
3945 case DeclarationName::Identifier:
3946 case DeclarationName::ObjCZeroArgSelector:
3947 case DeclarationName::ObjCOneArgSelector:
3948 case DeclarationName::ObjCMultiArgSelector:
3949 case DeclarationName::CXXUsingDirective:
3950 break;
3951 }
3952}
3953
3954void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003955 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003956 AddDeclarationName(NameInfo.getName(), Record);
3957 AddSourceLocation(NameInfo.getLoc(), Record);
3958 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3959}
3960
3961void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003962 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003963 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003964 Record.push_back(Info.NumTemplParamLists);
3965 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3966 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3967}
3968
Sebastian Redla4232eb2010-08-18 23:56:21 +00003969void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003970 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003971 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003972 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003973 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003974
3975 // Push each of the NNS's onto a stack for serialization in reverse order.
3976 while (NNS) {
3977 NestedNames.push_back(NNS);
3978 NNS = NNS->getPrefix();
3979 }
3980
3981 Record.push_back(NestedNames.size());
3982 while(!NestedNames.empty()) {
3983 NNS = NestedNames.pop_back_val();
3984 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3985 Record.push_back(Kind);
3986 switch (Kind) {
3987 case NestedNameSpecifier::Identifier:
3988 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3989 break;
3990
3991 case NestedNameSpecifier::Namespace:
3992 AddDeclRef(NNS->getAsNamespace(), Record);
3993 break;
3994
Douglas Gregor14aba762011-02-24 02:36:08 +00003995 case NestedNameSpecifier::NamespaceAlias:
3996 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3997 break;
3998
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003999 case NestedNameSpecifier::TypeSpec:
4000 case NestedNameSpecifier::TypeSpecWithTemplate:
4001 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4002 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4003 break;
4004
4005 case NestedNameSpecifier::Global:
4006 // Don't need to write an associated value.
4007 break;
4008 }
4009 }
4010}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004011
Douglas Gregordc355712011-02-25 00:36:19 +00004012void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4013 RecordDataImpl &Record) {
4014 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004015 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004016 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004017
4018 // Push each of the nested-name-specifiers's onto a stack for
4019 // serialization in reverse order.
4020 while (NNS) {
4021 NestedNames.push_back(NNS);
4022 NNS = NNS.getPrefix();
4023 }
4024
4025 Record.push_back(NestedNames.size());
4026 while(!NestedNames.empty()) {
4027 NNS = NestedNames.pop_back_val();
4028 NestedNameSpecifier::SpecifierKind Kind
4029 = NNS.getNestedNameSpecifier()->getKind();
4030 Record.push_back(Kind);
4031 switch (Kind) {
4032 case NestedNameSpecifier::Identifier:
4033 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4034 AddSourceRange(NNS.getLocalSourceRange(), Record);
4035 break;
4036
4037 case NestedNameSpecifier::Namespace:
4038 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4039 AddSourceRange(NNS.getLocalSourceRange(), Record);
4040 break;
4041
4042 case NestedNameSpecifier::NamespaceAlias:
4043 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4044 AddSourceRange(NNS.getLocalSourceRange(), Record);
4045 break;
4046
4047 case NestedNameSpecifier::TypeSpec:
4048 case NestedNameSpecifier::TypeSpecWithTemplate:
4049 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4050 AddTypeLoc(NNS.getTypeLoc(), Record);
4051 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4052 break;
4053
4054 case NestedNameSpecifier::Global:
4055 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4056 break;
4057 }
4058 }
4059}
4060
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004061void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004062 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004063 Record.push_back(Kind);
4064 switch (Kind) {
4065 case TemplateName::Template:
4066 AddDeclRef(Name.getAsTemplateDecl(), Record);
4067 break;
4068
4069 case TemplateName::OverloadedTemplate: {
4070 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4071 Record.push_back(OvT->size());
4072 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4073 I != E; ++I)
4074 AddDeclRef(*I, Record);
4075 break;
4076 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004077
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004078 case TemplateName::QualifiedTemplate: {
4079 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4080 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4081 Record.push_back(QualT->hasTemplateKeyword());
4082 AddDeclRef(QualT->getTemplateDecl(), Record);
4083 break;
4084 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004085
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004086 case TemplateName::DependentTemplate: {
4087 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4088 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4089 Record.push_back(DepT->isIdentifier());
4090 if (DepT->isIdentifier())
4091 AddIdentifierRef(DepT->getIdentifier(), Record);
4092 else
4093 Record.push_back(DepT->getOperator());
4094 break;
4095 }
John McCall14606042011-06-30 08:33:18 +00004096
4097 case TemplateName::SubstTemplateTemplateParm: {
4098 SubstTemplateTemplateParmStorage *subst
4099 = Name.getAsSubstTemplateTemplateParm();
4100 AddDeclRef(subst->getParameter(), Record);
4101 AddTemplateName(subst->getReplacement(), Record);
4102 break;
4103 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004104
4105 case TemplateName::SubstTemplateTemplateParmPack: {
4106 SubstTemplateTemplateParmPackStorage *SubstPack
4107 = Name.getAsSubstTemplateTemplateParmPack();
4108 AddDeclRef(SubstPack->getParameterPack(), Record);
4109 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4110 break;
4111 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004112 }
4113}
4114
Michael J. Spencer20249a12010-10-21 03:16:25 +00004115void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004116 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004117 Record.push_back(Arg.getKind());
4118 switch (Arg.getKind()) {
4119 case TemplateArgument::Null:
4120 break;
4121 case TemplateArgument::Type:
4122 AddTypeRef(Arg.getAsType(), Record);
4123 break;
4124 case TemplateArgument::Declaration:
4125 AddDeclRef(Arg.getAsDecl(), Record);
4126 break;
4127 case TemplateArgument::Integral:
4128 AddAPSInt(*Arg.getAsIntegral(), Record);
4129 AddTypeRef(Arg.getIntegralType(), Record);
4130 break;
4131 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004132 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4133 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004134 case TemplateArgument::TemplateExpansion:
4135 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004136 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4137 Record.push_back(*NumExpansions + 1);
4138 else
4139 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004140 break;
4141 case TemplateArgument::Expression:
4142 AddStmt(Arg.getAsExpr());
4143 break;
4144 case TemplateArgument::Pack:
4145 Record.push_back(Arg.pack_size());
4146 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4147 I != E; ++I)
4148 AddTemplateArgument(*I, Record);
4149 break;
4150 }
4151}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004152
4153void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004154ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004155 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004156 assert(TemplateParams && "No TemplateParams!");
4157 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4158 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4159 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4160 Record.push_back(TemplateParams->size());
4161 for (TemplateParameterList::const_iterator
4162 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4163 P != PEnd; ++P)
4164 AddDeclRef(*P, Record);
4165}
4166
4167/// \brief Emit a template argument list.
4168void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004169ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004170 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004171 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004172 Record.push_back(TemplateArgs->size());
4173 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004174 AddTemplateArgument(TemplateArgs->get(i), Record);
4175}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004176
4177
4178void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004179ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004180 Record.push_back(Set.size());
4181 for (UnresolvedSetImpl::const_iterator
4182 I = Set.begin(), E = Set.end(); I != E; ++I) {
4183 AddDeclRef(I.getDecl(), Record);
4184 Record.push_back(I.getAccess());
4185 }
4186}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004187
Sebastian Redla4232eb2010-08-18 23:56:21 +00004188void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004189 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004190 Record.push_back(Base.isVirtual());
4191 Record.push_back(Base.isBaseOfClass());
4192 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004193 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004194 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004195 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004196 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4197 : SourceLocation(),
4198 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004199}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004200
Douglas Gregor7c789c12010-10-29 22:39:52 +00004201void ASTWriter::FlushCXXBaseSpecifiers() {
4202 RecordData Record;
4203 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4204 Record.clear();
4205
4206 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004207 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004208 if (Index == CXXBaseSpecifiersOffsets.size())
4209 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4210 else {
4211 if (Index > CXXBaseSpecifiersOffsets.size())
4212 CXXBaseSpecifiersOffsets.resize(Index + 1);
4213 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4214 }
4215
4216 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4217 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4218 Record.push_back(BEnd - B);
4219 for (; B != BEnd; ++B)
4220 AddCXXBaseSpecifier(*B, Record);
4221 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004222
4223 // Flush any expressions that were written as part of the base specifiers.
4224 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004225 }
4226
4227 CXXBaseSpecifiersToWrite.clear();
4228}
4229
Sean Huntcbb67482011-01-08 20:30:50 +00004230void ASTWriter::AddCXXCtorInitializers(
4231 const CXXCtorInitializer * const *CtorInitializers,
4232 unsigned NumCtorInitializers,
4233 RecordDataImpl &Record) {
4234 Record.push_back(NumCtorInitializers);
4235 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4236 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004237
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004238 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004239 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004240 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004241 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004242 } else if (Init->isDelegatingInitializer()) {
4243 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004244 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004245 } else if (Init->isMemberInitializer()){
4246 Record.push_back(CTOR_INITIALIZER_MEMBER);
4247 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004248 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004249 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4250 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004251 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004252
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004253 AddSourceLocation(Init->getMemberLocation(), Record);
4254 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004255 AddSourceLocation(Init->getLParenLoc(), Record);
4256 AddSourceLocation(Init->getRParenLoc(), Record);
4257 Record.push_back(Init->isWritten());
4258 if (Init->isWritten()) {
4259 Record.push_back(Init->getSourceOrder());
4260 } else {
4261 Record.push_back(Init->getNumArrayIndices());
4262 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4263 AddDeclRef(Init->getArrayIndex(i), Record);
4264 }
4265 }
4266}
4267
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004268void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4269 assert(D->DefinitionData);
4270 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
4271 Record.push_back(Data.UserDeclaredConstructor);
4272 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004273 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004274 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004275 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004276 Record.push_back(Data.UserDeclaredDestructor);
4277 Record.push_back(Data.Aggregate);
4278 Record.push_back(Data.PlainOldData);
4279 Record.push_back(Data.Empty);
4280 Record.push_back(Data.Polymorphic);
4281 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004282 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004283 Record.push_back(Data.HasNoNonEmptyBases);
4284 Record.push_back(Data.HasPrivateFields);
4285 Record.push_back(Data.HasProtectedFields);
4286 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004287 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00004288 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004289 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004290 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004291 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004292 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004293 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004294 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004295 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004296 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004297 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004298 Record.push_back(Data.DeclaredDefaultConstructor);
4299 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004300 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004301 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004302 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004303 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004304 Record.push_back(Data.FailedImplicitMoveConstructor);
4305 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004306
4307 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004308 if (Data.NumBases > 0)
4309 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4310 Record);
4311
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004312 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4313 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004314 if (Data.NumVBases > 0)
4315 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4316 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004317
4318 AddUnresolvedSet(Data.Conversions, Record);
4319 AddUnresolvedSet(Data.VisibleConversions, Record);
4320 // Data.Definition is the owning decl, no need to write it.
4321 AddDeclRef(Data.FirstFriend, Record);
4322}
4323
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004324void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004325 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004326 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004327 assert(FirstDeclID == NextDeclID &&
4328 FirstTypeID == NextTypeID &&
4329 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004330 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004331 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004332 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004333
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004334 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004335
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004336 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4337 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4338 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004339 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004340 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004341 NextDeclID = FirstDeclID;
4342 NextTypeID = FirstTypeID;
4343 NextIdentID = FirstIdentID;
4344 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004345 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004346}
4347
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004348void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004349 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004350 if (II->hasMacroDefinition())
4351 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004352}
4353
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004354void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004355 // Always take the highest-numbered type index. This copes with an interesting
4356 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004357 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004358 // keep the higher-numbered entry so that we can properly write it out to
4359 // the AST file.
4360 TypeIdx &StoredIdx = TypeIdxs[T];
4361 if (Idx.getIndex() >= StoredIdx.getIndex())
4362 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004363}
4364
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004365void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004366 SelectorIDs[S] = ID;
4367}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004368
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004369void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004370 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004371 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004372 MacroDefinitions[MD] = ID;
4373}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004374
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004375void ASTWriter::MacroVisible(IdentifierInfo *II) {
4376 DeserializedMacroNames.push_back(II);
4377}
4378
Douglas Gregora015cab2011-12-02 17:30:13 +00004379void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4380 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4381 SubmoduleIDs[Mod] = ID;
4382}
4383
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004384void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004385 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004386 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004387 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4388 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004389 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004390 // A forward reference was mutated into a definition. Rewrite it.
4391 // FIXME: This happens during template instantiation, should we
4392 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004393 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004394 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004395 }
4396}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004397void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004398 assert(!WritingAST && "Already writing the AST!");
4399
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004400 // TU and namespaces are handled elsewhere.
4401 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4402 return;
4403
Douglas Gregor919814d2011-09-09 23:01:35 +00004404 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004405 return; // Not a source decl added to a DeclContext from PCH.
4406
4407 AddUpdatedDeclContext(DC);
4408}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004409
4410void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004411 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004412 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004413 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004414 return; // Not a source member added to a class from PCH.
4415 if (!isa<CXXMethodDecl>(D))
4416 return; // We are interested in lazily declared implicit methods.
4417
4418 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004419 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004420 UpdateRecord &Record = DeclUpdates[RD];
4421 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004422 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004423}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004424
4425void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4426 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004427 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004428 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004429 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004430 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004431 return; // Not a source specialization added to a template from PCH.
4432
4433 UpdateRecord &Record = DeclUpdates[TD];
4434 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004435 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004436}
Douglas Gregor89d99802010-11-30 06:16:57 +00004437
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004438void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4439 const FunctionDecl *D) {
4440 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004441 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004442 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004443 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004444 return; // Not a source specialization added to a template from PCH.
4445
4446 UpdateRecord &Record = DeclUpdates[TD];
4447 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004448 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004449}
4450
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004451void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004452 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004453 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004454 return; // Declaration not imported from PCH.
4455
4456 // Implicit decl from a PCH was defined.
4457 // FIXME: Should implicit definition be a separate FunctionDecl?
4458 RewriteDecl(D);
4459}
4460
Sebastian Redlf79a7192011-04-29 08:19:30 +00004461void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004462 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004463 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004464 return;
4465
4466 // Since the actual instantiation is delayed, this really means that we need
4467 // to update the instantiation location.
4468 UpdateRecord &Record = DeclUpdates[D];
4469 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4470 AddSourceLocation(
4471 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4472}
4473
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004474void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4475 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004476 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004477 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004478 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004479
4480 assert(IFD->getDefinition() && "Category on a class without a definition?");
4481 ObjCClassesWithCategories.insert(
4482 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004483}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004484
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004485
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004486void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4487 const ObjCPropertyDecl *OrigProp,
4488 const ObjCCategoryDecl *ClassExt) {
4489 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4490 if (!D)
4491 return;
4492
4493 assert(!WritingAST && "Already writing the AST!");
4494 if (!D->isFromASTFile())
4495 return; // Declaration not imported from PCH.
4496
4497 RewriteDecl(D);
4498}
4499