blob: a995a70f24cf242ce6aac6a0171c35023a0503c8 [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"
Douglas Gregor89d99802010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
Sebastian Redlade50002010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000056static StringRef data(const std::vector<T, Allocator> &v) {
57 if (v.empty()) return StringRef();
58 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000059 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000060}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000061
62template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000063static StringRef data(const SmallVectorImpl<T> &v) {
64 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000066}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000071
Douglas Gregor2cf26342009-04-09 22:27:44 +000072namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000080
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
84 void VisitArrayType(const ArrayType *T);
85 void VisitFunctionType(const FunctionType *T);
86 void VisitTagType(const TagType *T);
87
88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
89#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl3397c552010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000095 assert(false && "Built-in types are never serialized");
96}
97
Sebastian Redl3397c552010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101}
102
Sebastian Redl3397c552010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106}
107
Sebastian Redl3397c552010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111}
112
Sebastian Redl3397c552010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117}
118
Sebastian Redl3397c552010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000122}
123
Sebastian Redl3397c552010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128}
129
Sebastian Redl3397c552010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134}
135
Sebastian Redl3397c552010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140}
141
Sebastian Redl3397c552010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145}
146
Sebastian Redl3397c552010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153}
154
Sebastian Redl3397c552010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160}
161
Sebastian Redl3397c552010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176}
177
Sebastian Redl3397c552010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181}
182
Sebastian Redl3397c552010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184 VisitFunctionType(T);
185 Record.push_back(T->getNumArgs());
186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
187 Writer.AddTypeRef(T->getArgType(I), Record);
188 Record.push_back(T->isVariadic());
189 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
198 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000199 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200}
201
Sebastian Redl3397c552010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000203 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000204 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000205}
John McCalled976492009-12-04 22:46:56 +0000206
Sebastian Redl3397c552010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000211 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000212}
213
Sebastian Redl3397c552010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000215 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000216 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217}
218
Sebastian Redl3397c552010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222}
223
Sebastian Redl3397c552010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000225 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000227}
228
Sean Huntca63c202011-05-24 22:41:36 +0000229void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
230 Writer.AddTypeRef(T->getBaseType(), Record);
231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
232 Record.push_back(T->getUTTKind());
233 Code = TYPE_UNARY_TRANSFORM;
234}
235
Richard Smith34b41d92011-02-20 03:19:35 +0000236void ASTTypeWriter::VisitAutoType(const AutoType *T) {
237 Writer.AddTypeRef(T->getDeducedType(), Record);
238 Code = TYPE_AUTO;
239}
240
Sebastian Redl3397c552010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000242 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000243 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000244 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000245 "Cannot serialize in the middle of a type definition");
246}
247
Sebastian Redl3397c552010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000250 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251}
252
Sebastian Redl3397c552010-08-18 23:56:27 +0000253void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000255 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256}
257
John McCall9d156a72011-01-06 01:58:22 +0000258void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
259 Writer.AddTypeRef(T->getModifiedType(), Record);
260 Writer.AddTypeRef(T->getEquivalentType(), Record);
261 Record.push_back(T->getAttrKind());
262 Code = TYPE_ATTRIBUTED;
263}
264
Mike Stump1eb44332009-09-09 15:08:12 +0000265void
Sebastian Redl3397c552010-08-18 23:56:27 +0000266ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000267 const SubstTemplateTypeParmType *T) {
268 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
269 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000270 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000271}
272
273void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000274ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
275 const SubstTemplateTypeParmPackType *T) {
276 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
277 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
278 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
279}
280
281void
Sebastian Redl3397c552010-08-18 23:56:27 +0000282ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000283 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000284 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000285 Writer.AddTemplateName(T->getTemplateName(), Record);
286 Record.push_back(T->getNumArgs());
287 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
288 ArgI != ArgE; ++ArgI)
289 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000290 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
291 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000292 : T->getCanonicalTypeInternal(),
293 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000294 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000295}
296
297void
Sebastian Redl3397c552010-08-18 23:56:27 +0000298ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000299 VisitArrayType(T);
300 Writer.AddStmt(T->getSizeExpr());
301 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl3397c552010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000307 const DependentSizedExtVectorType *T) {
308 // FIXME: Serialize this type (C++ only)
309 assert(false && "Cannot serialize dependent sized extended vector types");
310}
311
312void
Sebastian Redl3397c552010-08-18 23:56:27 +0000313ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314 Record.push_back(T->getDepth());
315 Record.push_back(T->getIndex());
316 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000317 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000318 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl3397c552010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000323 Record.push_back(T->getKeyword());
324 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
325 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000326 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
327 : T->getCanonicalTypeInternal(),
328 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000329 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000330}
331
332void
Sebastian Redl3397c552010-08-18 23:56:27 +0000333ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000334 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000335 Record.push_back(T->getKeyword());
336 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
337 Writer.AddIdentifierRef(T->getIdentifier(), Record);
338 Record.push_back(T->getNumArgs());
339 for (DependentTemplateSpecializationType::iterator
340 I = T->begin(), E = T->end(); I != E; ++I)
341 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000342 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000343}
344
Douglas Gregor7536dd52010-12-20 02:24:11 +0000345void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
346 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000347 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
348 Record.push_back(*NumExpansions + 1);
349 else
350 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000351 Code = TYPE_PACK_EXPANSION;
352}
353
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000354void ASTTypeWriter::VisitParenType(const ParenType *T) {
355 Writer.AddTypeRef(T->getInnerType(), Record);
356 Code = TYPE_PAREN;
357}
358
Sebastian Redl3397c552010-08-18 23:56:27 +0000359void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000360 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000361 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
362 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000363 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000364}
365
Sebastian Redl3397c552010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000367 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000368 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000369 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000370}
371
Sebastian Redl3397c552010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000373 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000374 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000375}
376
Sebastian Redl3397c552010-08-18 23:56:27 +0000377void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000378 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000379 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000380 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000381 E = T->qual_end(); I != E; ++I)
382 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000384}
385
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000386void
Sebastian Redl3397c552010-08-18 23:56:27 +0000387ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000390}
391
John McCalla1ee0c52009-10-16 21:56:05 +0000392namespace {
393
394class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000395 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000396 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000397
398public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000399 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000400 : Writer(Writer), Record(Record) { }
401
John McCall51bd8032009-10-18 01:05:36 +0000402#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000403#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000404 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000405#include "clang/AST/TypeLocNodes.def"
406
John McCall51bd8032009-10-18 01:05:36 +0000407 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
408 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000409};
410
411}
412
John McCall51bd8032009-10-18 01:05:36 +0000413void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
414 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000415}
John McCall51bd8032009-10-18 01:05:36 +0000416void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000417 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
418 if (TL.needsExtraLocalData()) {
419 Record.push_back(TL.getWrittenTypeSpec());
420 Record.push_back(TL.getWrittenSignSpec());
421 Record.push_back(TL.getWrittenWidthSpec());
422 Record.push_back(TL.hasModeAttr());
423 }
John McCalla1ee0c52009-10-16 21:56:05 +0000424}
John McCall51bd8032009-10-18 01:05:36 +0000425void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000430}
John McCall51bd8032009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000433}
John McCall51bd8032009-10-18 01:05:36 +0000434void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000436}
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000442 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
446 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
447 Record.push_back(TL.getSizeExpr() ? 1 : 0);
448 if (TL.getSizeExpr())
449 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000450}
John McCall51bd8032009-10-18 01:05:36 +0000451void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
452 VisitArrayTypeLoc(TL);
453}
454void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
455 VisitArrayTypeLoc(TL);
456}
457void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
458 VisitArrayTypeLoc(TL);
459}
460void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
461 DependentSizedArrayTypeLoc TL) {
462 VisitArrayTypeLoc(TL);
463}
464void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
465 DependentSizedExtVectorTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
468void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
474void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000475 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
476 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000477 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000478 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
479 Writer.AddDeclRef(TL.getArg(i), Record);
480}
481void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
482 VisitFunctionTypeLoc(TL);
483}
484void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
485 VisitFunctionTypeLoc(TL);
486}
John McCalled976492009-12-04 22:46:56 +0000487void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
John McCall51bd8032009-10-18 01:05:36 +0000490void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getNameLoc(), Record);
492}
493void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000494 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
495 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
496 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000497}
498void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc 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);
502 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000503}
504void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
Sean Huntca63c202011-05-24 22:41:36 +0000507void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getKWLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
511 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
512}
Richard Smith34b41d92011-02-20 03:19:35 +0000513void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
John McCall51bd8032009-10-18 01:05:36 +0000516void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
519void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getNameLoc(), Record);
521}
John McCall9d156a72011-01-06 01:58:22 +0000522void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
523 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
524 if (TL.hasAttrOperand()) {
525 SourceRange range = TL.getAttrOperandParensRange();
526 Writer.AddSourceLocation(range.getBegin(), Record);
527 Writer.AddSourceLocation(range.getEnd(), Record);
528 }
529 if (TL.hasAttrExprOperand()) {
530 Expr *operand = TL.getAttrExprOperand();
531 Record.push_back(operand ? 1 : 0);
532 if (operand) Writer.AddStmt(operand);
533 } else if (TL.hasAttrEnumOperand()) {
534 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
535 }
536}
John McCall51bd8032009-10-18 01:05:36 +0000537void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
John McCall49a832b2009-10-18 09:09:24 +0000540void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
541 SubstTemplateTypeParmTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000544void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
545 SubstTemplateTypeParmPackTypeLoc TL) {
546 Writer.AddSourceLocation(TL.getNameLoc(), Record);
547}
John McCall51bd8032009-10-18 01:05:36 +0000548void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
549 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000550 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
551 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
552 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
553 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000554 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
555 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000556}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000557void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
559 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
560}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000561void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000562 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000563 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000564}
John McCall3cb0ebd2010-03-10 03:28:59 +0000565void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
567}
Douglas Gregor4714c122010-03-31 17:34:00 +0000568void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000569 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000570 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCall33500952010-06-11 00:33:02 +0000573void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
574 DependentTemplateSpecializationTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000576 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
579 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
580 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000581 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
582 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000583}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000584void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
586}
John McCall51bd8032009-10-18 01:05:36 +0000587void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000589}
590void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
591 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000592 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
593 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
594 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
595 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000596}
John McCall54e14c42009-10-22 22:37:11 +0000597void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000599}
John McCalla1ee0c52009-10-16 21:56:05 +0000600
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000601//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000602// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000603//===----------------------------------------------------------------------===//
604
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000605static void EmitBlockID(unsigned ID, const char *Name,
606 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000607 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000608 Record.clear();
609 Record.push_back(ID);
610 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
611
612 // Emit the block name if present.
613 if (Name == 0 || Name[0] == 0) return;
614 Record.clear();
615 while (*Name)
616 Record.push_back(*Name++);
617 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
618}
619
620static void EmitRecordID(unsigned ID, const char *Name,
621 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000622 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000623 Record.clear();
624 Record.push_back(ID);
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000628}
629
630static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000632#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000633 RECORD(STMT_STOP);
634 RECORD(STMT_NULL_PTR);
635 RECORD(STMT_NULL);
636 RECORD(STMT_COMPOUND);
637 RECORD(STMT_CASE);
638 RECORD(STMT_DEFAULT);
639 RECORD(STMT_LABEL);
640 RECORD(STMT_IF);
641 RECORD(STMT_SWITCH);
642 RECORD(STMT_WHILE);
643 RECORD(STMT_DO);
644 RECORD(STMT_FOR);
645 RECORD(STMT_GOTO);
646 RECORD(STMT_INDIRECT_GOTO);
647 RECORD(STMT_CONTINUE);
648 RECORD(STMT_BREAK);
649 RECORD(STMT_RETURN);
650 RECORD(STMT_DECL);
651 RECORD(STMT_ASM);
652 RECORD(EXPR_PREDEFINED);
653 RECORD(EXPR_DECL_REF);
654 RECORD(EXPR_INTEGER_LITERAL);
655 RECORD(EXPR_FLOATING_LITERAL);
656 RECORD(EXPR_IMAGINARY_LITERAL);
657 RECORD(EXPR_STRING_LITERAL);
658 RECORD(EXPR_CHARACTER_LITERAL);
659 RECORD(EXPR_PAREN);
660 RECORD(EXPR_UNARY_OPERATOR);
661 RECORD(EXPR_SIZEOF_ALIGN_OF);
662 RECORD(EXPR_ARRAY_SUBSCRIPT);
663 RECORD(EXPR_CALL);
664 RECORD(EXPR_MEMBER);
665 RECORD(EXPR_BINARY_OPERATOR);
666 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
667 RECORD(EXPR_CONDITIONAL_OPERATOR);
668 RECORD(EXPR_IMPLICIT_CAST);
669 RECORD(EXPR_CSTYLE_CAST);
670 RECORD(EXPR_COMPOUND_LITERAL);
671 RECORD(EXPR_EXT_VECTOR_ELEMENT);
672 RECORD(EXPR_INIT_LIST);
673 RECORD(EXPR_DESIGNATED_INIT);
674 RECORD(EXPR_IMPLICIT_VALUE_INIT);
675 RECORD(EXPR_VA_ARG);
676 RECORD(EXPR_ADDR_LABEL);
677 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000678 RECORD(EXPR_CHOOSE);
679 RECORD(EXPR_GNU_NULL);
680 RECORD(EXPR_SHUFFLE_VECTOR);
681 RECORD(EXPR_BLOCK);
682 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000683 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000684 RECORD(EXPR_OBJC_STRING_LITERAL);
685 RECORD(EXPR_OBJC_ENCODE);
686 RECORD(EXPR_OBJC_SELECTOR_EXPR);
687 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
688 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
689 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
690 RECORD(EXPR_OBJC_KVC_REF_EXPR);
691 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000692 RECORD(STMT_OBJC_FOR_COLLECTION);
693 RECORD(STMT_OBJC_CATCH);
694 RECORD(STMT_OBJC_FINALLY);
695 RECORD(STMT_OBJC_AT_TRY);
696 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
697 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000698 RECORD(EXPR_CXX_OPERATOR_CALL);
699 RECORD(EXPR_CXX_CONSTRUCT);
700 RECORD(EXPR_CXX_STATIC_CAST);
701 RECORD(EXPR_CXX_DYNAMIC_CAST);
702 RECORD(EXPR_CXX_REINTERPRET_CAST);
703 RECORD(EXPR_CXX_CONST_CAST);
704 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
705 RECORD(EXPR_CXX_BOOL_LITERAL);
706 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000707 RECORD(EXPR_CXX_TYPEID_EXPR);
708 RECORD(EXPR_CXX_TYPEID_TYPE);
709 RECORD(EXPR_CXX_UUIDOF_EXPR);
710 RECORD(EXPR_CXX_UUIDOF_TYPE);
711 RECORD(EXPR_CXX_THIS);
712 RECORD(EXPR_CXX_THROW);
713 RECORD(EXPR_CXX_DEFAULT_ARG);
714 RECORD(EXPR_CXX_BIND_TEMPORARY);
715 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
716 RECORD(EXPR_CXX_NEW);
717 RECORD(EXPR_CXX_DELETE);
718 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
719 RECORD(EXPR_EXPR_WITH_CLEANUPS);
720 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
721 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
722 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
723 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
724 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
725 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
726 RECORD(EXPR_CXX_NOEXCEPT);
727 RECORD(EXPR_OPAQUE_VALUE);
728 RECORD(EXPR_BINARY_TYPE_TRAIT);
729 RECORD(EXPR_PACK_EXPANSION);
730 RECORD(EXPR_SIZEOF_PACK);
731 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000732 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000733#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000734}
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Sebastian Redla4232eb2010-08-18 23:56:21 +0000736void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000737 RecordData Record;
738 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000740#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
741#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Sebastian Redl3397c552010-08-18 23:56:27 +0000743 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000744 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000745 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000746 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000747 RECORD(TYPE_OFFSET);
748 RECORD(DECL_OFFSET);
749 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000750 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000751 RECORD(IDENTIFIER_OFFSET);
752 RECORD(IDENTIFIER_TABLE);
753 RECORD(EXTERNAL_DEFINITIONS);
754 RECORD(SPECIAL_TYPES);
755 RECORD(STATISTICS);
756 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000757 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000758 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
759 RECORD(SELECTOR_OFFSETS);
760 RECORD(METHOD_POOL);
761 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000762 RECORD(SOURCE_LOCATION_OFFSETS);
763 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000764 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000765 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000766 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000767 RECORD(MACRO_DEFINITION_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000768 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000769 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000770 RECORD(TU_UPDATE_LEXICAL);
771 RECORD(REDECLS_UPDATE_LATEST);
772 RECORD(SEMA_DECL_REFS);
773 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
774 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
775 RECORD(DECL_REPLACEMENTS);
776 RECORD(UPDATE_VISIBLE);
777 RECORD(DECL_UPDATE_OFFSETS);
778 RECORD(DECL_UPDATES);
779 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
780 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000781 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000782 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000783 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000784 RECORD(FP_PRAGMA_OPTIONS);
785 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000786 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000787 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
788 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000789 RECORD(MODULE_OFFSET_MAP);
790 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000791
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000792 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000793 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000794 RECORD(SM_SLOC_FILE_ENTRY);
795 RECORD(SM_SLOC_BUFFER_ENTRY);
796 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000797 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000799 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000800 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000801 RECORD(PP_MACRO_OBJECT_LIKE);
802 RECORD(PP_MACRO_FUNCTION_LIKE);
803 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000804
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000805 // Decls and Types block.
806 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000807 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000808 RECORD(TYPE_COMPLEX);
809 RECORD(TYPE_POINTER);
810 RECORD(TYPE_BLOCK_POINTER);
811 RECORD(TYPE_LVALUE_REFERENCE);
812 RECORD(TYPE_RVALUE_REFERENCE);
813 RECORD(TYPE_MEMBER_POINTER);
814 RECORD(TYPE_CONSTANT_ARRAY);
815 RECORD(TYPE_INCOMPLETE_ARRAY);
816 RECORD(TYPE_VARIABLE_ARRAY);
817 RECORD(TYPE_VECTOR);
818 RECORD(TYPE_EXT_VECTOR);
819 RECORD(TYPE_FUNCTION_PROTO);
820 RECORD(TYPE_FUNCTION_NO_PROTO);
821 RECORD(TYPE_TYPEDEF);
822 RECORD(TYPE_TYPEOF_EXPR);
823 RECORD(TYPE_TYPEOF);
824 RECORD(TYPE_RECORD);
825 RECORD(TYPE_ENUM);
826 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000827 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000828 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000829 RECORD(TYPE_DECLTYPE);
830 RECORD(TYPE_ELABORATED);
831 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
832 RECORD(TYPE_UNRESOLVED_USING);
833 RECORD(TYPE_INJECTED_CLASS_NAME);
834 RECORD(TYPE_OBJC_OBJECT);
835 RECORD(TYPE_TEMPLATE_TYPE_PARM);
836 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
837 RECORD(TYPE_DEPENDENT_NAME);
838 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
839 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
840 RECORD(TYPE_PAREN);
841 RECORD(TYPE_PACK_EXPANSION);
842 RECORD(TYPE_ATTRIBUTED);
843 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000844 RECORD(DECL_TYPEDEF);
845 RECORD(DECL_ENUM);
846 RECORD(DECL_RECORD);
847 RECORD(DECL_ENUM_CONSTANT);
848 RECORD(DECL_FUNCTION);
849 RECORD(DECL_OBJC_METHOD);
850 RECORD(DECL_OBJC_INTERFACE);
851 RECORD(DECL_OBJC_PROTOCOL);
852 RECORD(DECL_OBJC_IVAR);
853 RECORD(DECL_OBJC_AT_DEFS_FIELD);
854 RECORD(DECL_OBJC_CLASS);
855 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
856 RECORD(DECL_OBJC_CATEGORY);
857 RECORD(DECL_OBJC_CATEGORY_IMPL);
858 RECORD(DECL_OBJC_IMPLEMENTATION);
859 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
860 RECORD(DECL_OBJC_PROPERTY);
861 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000862 RECORD(DECL_FIELD);
863 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000864 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000865 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000866 RECORD(DECL_FILE_SCOPE_ASM);
867 RECORD(DECL_BLOCK);
868 RECORD(DECL_CONTEXT_LEXICAL);
869 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000870 RECORD(DECL_NAMESPACE);
871 RECORD(DECL_NAMESPACE_ALIAS);
872 RECORD(DECL_USING);
873 RECORD(DECL_USING_SHADOW);
874 RECORD(DECL_USING_DIRECTIVE);
875 RECORD(DECL_UNRESOLVED_USING_VALUE);
876 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
877 RECORD(DECL_LINKAGE_SPEC);
878 RECORD(DECL_CXX_RECORD);
879 RECORD(DECL_CXX_METHOD);
880 RECORD(DECL_CXX_CONSTRUCTOR);
881 RECORD(DECL_CXX_DESTRUCTOR);
882 RECORD(DECL_CXX_CONVERSION);
883 RECORD(DECL_ACCESS_SPEC);
884 RECORD(DECL_FRIEND);
885 RECORD(DECL_FRIEND_TEMPLATE);
886 RECORD(DECL_CLASS_TEMPLATE);
887 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
888 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
889 RECORD(DECL_FUNCTION_TEMPLATE);
890 RECORD(DECL_TEMPLATE_TYPE_PARM);
891 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
892 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
893 RECORD(DECL_STATIC_ASSERT);
894 RECORD(DECL_CXX_BASE_SPECIFIERS);
895 RECORD(DECL_INDIRECTFIELD);
896 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
897
Douglas Gregora72d8c42011-06-03 02:27:19 +0000898 // Statements and Exprs can occur in the Decls and Types block.
899 AddStmtsExprs(Stream, Record);
900
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000901 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000902 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000903 RECORD(PPD_MACRO_DEFINITION);
904 RECORD(PPD_INCLUSION_DIRECTIVE);
905
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000906#undef RECORD
907#undef BLOCK
908 Stream.ExitBlock();
909}
910
Douglas Gregore650c8c2009-07-07 00:12:59 +0000911/// \brief Adjusts the given filename to only write out the portion of the
912/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000913///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000914/// \param Filename the file name to adjust.
915///
916/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
917/// the returned filename will be adjusted by this system root.
918///
919/// \returns either the original filename (if it needs no adjustment) or the
920/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000921static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000922adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000923 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Douglas Gregor832d6202011-07-22 16:35:34 +0000925 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000926 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregore650c8c2009-07-07 00:12:59 +0000928 // Verify that the filename and the system root have the same prefix.
929 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000930 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000931 if (Filename[Pos] != isysroot[Pos])
932 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Douglas Gregore650c8c2009-07-07 00:12:59 +0000934 // We hit the end of the filename before we hit the end of the system root.
935 if (!Filename[Pos])
936 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Douglas Gregore650c8c2009-07-07 00:12:59 +0000938 // If the file name has a '/' at the current position, skip over the '/'.
939 // We distinguish sysroot-based includes from absolute includes by the
940 // absence of '/' at the beginning of sysroot-based includes.
941 if (Filename[Pos] == '/')
942 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Douglas Gregore650c8c2009-07-07 00:12:59 +0000944 return Filename + Pos;
945}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000946
Sebastian Redl3397c552010-08-18 23:56:27 +0000947/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000948void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000949 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000950 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000951
Douglas Gregore650c8c2009-07-07 00:12:59 +0000952 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000953 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000954 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000955 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000956 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
957 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
959 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
960 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000961 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000965 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000966 Record.push_back(VERSION_MAJOR);
967 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 Record.push_back(CLANG_VERSION_MAJOR);
969 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000970 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000971 const std::string &Triple = Target.getTriple().getTriple();
972 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
973
974 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000975 serialization::ModuleManager &Mgr = Chain->getModuleManager();
976 llvm::SmallVector<char, 128> ModulePaths;
977 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000978
979 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
980 M != MEnd; ++M) {
981 // Skip modules that weren't directly imported.
982 if (!(*M)->isDirectlyImported())
983 continue;
984
985 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
986 // FIXME: Write import location, once it matters.
987 // FIXME: This writes the absolute path for AST files we depend on.
988 const std::string &FileName = (*M)->FileName;
989 Record.push_back(FileName.size());
990 Record.append(FileName.begin(), FileName.end());
991 }
Douglas Gregore95b9192011-08-17 21:07:30 +0000992 Stream.EmitRecord(IMPORTS, Record);
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Douglas Gregor31d375f2011-05-06 21:43:30 +0000995 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +0000996 SourceManager &SM = Context.getSourceManager();
997 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
998 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000999 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001000 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1001 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1002
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001003 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001005 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001006
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001007 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001008 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001009 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001010 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001011 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001012 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001013
1014 Record.clear();
1015 Record.push_back(SM.getMainFileID().getOpaqueValue());
1016 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001017 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001018
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001019 // Original PCH directory
1020 if (!OutputFile.empty() && OutputFile != "-") {
1021 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1022 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1024 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1025
1026 llvm::SmallString<128> OutputPath(OutputFile);
1027
1028 llvm::sys::fs::make_absolute(OutputPath);
1029 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1030
1031 RecordData Record;
1032 Record.push_back(ORIGINAL_PCH_DIR);
1033 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1034 }
1035
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001036 // Repository branch/version information.
1037 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001038 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001039 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1040 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001041 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001042 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001043 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1044 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001045}
1046
1047/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001048void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001049 RecordData Record;
1050 Record.push_back(LangOpts.Trigraphs);
1051 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1052 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1053 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1054 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001055 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001056 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1057 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1058 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1059 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00001060 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001061 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001062 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1063 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001064 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1065 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001066 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001068 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1069 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001070 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001071 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001072 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001073 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001074 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001075 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1076 // properties enabled.
Douglas Gregor74da19f2011-06-14 23:20:43 +00001077 Record.push_back(LangOpts.ObjCInferRelatedResultType);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001078 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001080 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001081 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1082 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001083 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001084 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001085 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001086 Record.push_back(LangOpts.CXXExceptions);
1087 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001088
Douglas Gregor6f755502011-02-01 15:15:22 +00001089 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001090 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1091 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1092 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1093
Chris Lattnerea5ce472009-04-27 07:35:58 +00001094 // Whether static initializers are protected by locks.
1095 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001096 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001097 Record.push_back(LangOpts.Blocks); // block extension to C
1098 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1099 // they are unused.
1100 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1101 // (modulo the platform support).
1102
Chris Lattnera4d71452010-06-26 21:25:03 +00001103 Record.push_back(LangOpts.getSignedOverflowBehavior());
1104 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001105
1106 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001107 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001108 // defined.
1109 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1110 // opposed to __DYNAMIC__).
1111 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1112
1113 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1114 // used (instead of C99 semantics).
1115 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00001116 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001117 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1118 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001119 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1120 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001121 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001122 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1123 // to the smallest integer type with
1124 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001125 Record.push_back(LangOpts.getGCMode());
1126 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001127 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001128 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001129 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001130 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001131 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001132 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001133 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001134 Record.push_back(LangOpts.SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00001135 Record.push_back(LangOpts.MRTD);
John McCallf85e1932011-06-15 23:02:42 +00001136 Record.push_back(LangOpts.ObjCAutoRefCount);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001137 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001138}
1139
Douglas Gregor14f79002009-04-10 03:52:48 +00001140//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001141// stat cache Serialization
1142//===----------------------------------------------------------------------===//
1143
1144namespace {
1145// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001146class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001147public:
1148 typedef const char * key_type;
1149 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Chris Lattner74e976b2010-11-23 19:28:12 +00001151 typedef struct stat data_type;
1152 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001153
1154 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001155 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
1158 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001159 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001160 data_type_ref Data) {
1161 unsigned StrLen = strlen(path);
1162 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001163 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001164 clang::io::Emit8(Out, DataLen);
1165 return std::make_pair(StrLen + 1, DataLen);
1166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Chris Lattner5f9e2722011-07-23 10:55:15 +00001168 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001169 Out.write(path, KeyLen);
1170 }
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Chris Lattner5f9e2722011-07-23 10:55:15 +00001172 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001173 data_type_ref Data, unsigned DataLen) {
1174 using namespace clang::io;
1175 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Chris Lattner74e976b2010-11-23 19:28:12 +00001177 Emit32(Out, (uint32_t) Data.st_ino);
1178 Emit32(Out, (uint32_t) Data.st_dev);
1179 Emit16(Out, (uint16_t) Data.st_mode);
1180 Emit64(Out, (uint64_t) Data.st_mtime);
1181 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001182
1183 assert(Out.tell() - Start == DataLen && "Wrong data length");
1184 }
1185};
1186} // end anonymous namespace
1187
Sebastian Redl3397c552010-08-18 23:56:27 +00001188/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001189void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001190 // Build the on-disk hash table containing information about every
1191 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001192 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001193 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001194 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001195 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001196 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001197 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001198 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001201 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001202 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001203 uint32_t BucketOffset;
1204 {
1205 llvm::raw_svector_ostream Out(StatCacheData);
1206 // Make sure that no bucket is at offset 0
1207 clang::io::Emit32(Out, 0);
1208 BucketOffset = Generator.Emit(Out);
1209 }
1210
1211 // Create a blob abbreviation
1212 using namespace llvm;
1213 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001214 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1218 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1219
1220 // Write the stat cache
1221 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001222 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001223 Record.push_back(BucketOffset);
1224 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001225 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001226}
1227
1228//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001229// Source Manager Serialization
1230//===----------------------------------------------------------------------===//
1231
1232/// \brief Create an abbreviation for the SLocEntry that refers to a
1233/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001234static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001235 using namespace llvm;
1236 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001237 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001242 // FileEntry fields.
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Douglas Gregor14f79002009-04-10 03:52:48 +00001246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001247 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001248}
1249
1250/// \brief Create an abbreviation for the SLocEntry that refers to a
1251/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001252static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001253 using namespace llvm;
1254 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001255 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001261 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001262}
1263
1264/// \brief Create an abbreviation for the SLocEntry that refers to a
1265/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001266static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001267 using namespace llvm;
1268 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001269 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001271 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001272}
1273
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001274/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1275/// expansion.
1276static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001277 using namespace llvm;
1278 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001279 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001285 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001286}
1287
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001288namespace {
1289 // Trait used for the on-disk hash table of header search information.
1290 class HeaderFileInfoTrait {
1291 ASTWriter &Writer;
1292 HeaderSearch &HS;
1293
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001294 // Keep track of the framework names we've used during serialization.
1295 SmallVector<char, 128> FrameworkStringData;
1296 llvm::StringMap<unsigned> FrameworkNameOffset;
1297
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001298 public:
1299 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1300 : Writer(Writer), HS(HS) { }
1301
1302 typedef const char *key_type;
1303 typedef key_type key_type_ref;
1304
1305 typedef HeaderFileInfo data_type;
1306 typedef const data_type &data_type_ref;
1307
1308 static unsigned ComputeHash(const char *path) {
1309 // The hash is based only on the filename portion of the key, so that the
1310 // reader can match based on filenames when symlinking or excess path
1311 // elements ("foo/../", "../") change the form of the name. However,
1312 // complete path is still the key.
1313 return llvm::HashString(llvm::sys::path::filename(path));
1314 }
1315
1316 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001317 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001318 data_type_ref Data) {
1319 unsigned StrLen = strlen(path);
1320 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001321 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001322 clang::io::Emit8(Out, DataLen);
1323 return std::make_pair(StrLen + 1, DataLen);
1324 }
1325
Chris Lattner5f9e2722011-07-23 10:55:15 +00001326 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001327 Out.write(path, KeyLen);
1328 }
1329
Chris Lattner5f9e2722011-07-23 10:55:15 +00001330 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001331 data_type_ref Data, unsigned DataLen) {
1332 using namespace clang::io;
1333 uint64_t Start = Out.tell(); (void)Start;
1334
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001335 unsigned char Flags = (Data.isImport << 5)
1336 | (Data.isPragmaOnce << 4)
1337 | (Data.DirInfo << 2)
1338 | (Data.Resolved << 1)
1339 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001340 Emit8(Out, (uint8_t)Flags);
1341 Emit16(Out, (uint16_t) Data.NumIncludes);
1342
1343 if (!Data.ControllingMacro)
1344 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1345 else
1346 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001347
1348 unsigned Offset = 0;
1349 if (!Data.Framework.empty()) {
1350 // If this header refers into a framework, save the framework name.
1351 llvm::StringMap<unsigned>::iterator Pos
1352 = FrameworkNameOffset.find(Data.Framework);
1353 if (Pos == FrameworkNameOffset.end()) {
1354 Offset = FrameworkStringData.size() + 1;
1355 FrameworkStringData.append(Data.Framework.begin(),
1356 Data.Framework.end());
1357 FrameworkStringData.push_back(0);
1358
1359 FrameworkNameOffset[Data.Framework] = Offset;
1360 } else
1361 Offset = Pos->second;
1362 }
1363 Emit32(Out, Offset);
1364
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001365 assert(Out.tell() - Start == DataLen && "Wrong data length");
1366 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001367
1368 const char *strings_begin() const { return FrameworkStringData.begin(); }
1369 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001370 };
1371} // end anonymous namespace
1372
1373/// \brief Write the header search block for the list of files that
1374///
1375/// \param HS The header search structure to save.
1376///
1377/// \param Chain Whether we're creating a chained AST file.
Douglas Gregor832d6202011-07-22 16:35:34 +00001378void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001379 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001380 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1381
1382 if (FilesByUID.size() > HS.header_file_size())
1383 FilesByUID.resize(HS.header_file_size());
1384
1385 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1386 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001387 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001388 unsigned NumHeaderSearchEntries = 0;
1389 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1390 const FileEntry *File = FilesByUID[UID];
1391 if (!File)
1392 continue;
1393
1394 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1395 if (HFI.External && Chain)
1396 continue;
1397
1398 // Turn the file name into an absolute path, if it isn't already.
1399 const char *Filename = File->getName();
1400 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1401
1402 // If we performed any translation on the file name at all, we need to
1403 // save this string, since the generator will refer to it later.
1404 if (Filename != File->getName()) {
1405 Filename = strdup(Filename);
1406 SavedStrings.push_back(Filename);
1407 }
1408
1409 Generator.insert(Filename, HFI, GeneratorTrait);
1410 ++NumHeaderSearchEntries;
1411 }
1412
1413 // Create the on-disk hash table in a buffer.
1414 llvm::SmallString<4096> TableData;
1415 uint32_t BucketOffset;
1416 {
1417 llvm::raw_svector_ostream Out(TableData);
1418 // Make sure that no bucket is at offset 0
1419 clang::io::Emit32(Out, 0);
1420 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1421 }
1422
1423 // Create a blob abbreviation
1424 using namespace llvm;
1425 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1426 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1431 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1432
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001433 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001434 RecordData Record;
1435 Record.push_back(HEADER_SEARCH_TABLE);
1436 Record.push_back(BucketOffset);
1437 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001438 Record.push_back(TableData.size());
1439 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001440 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1441
1442 // Free all of the strings we had to duplicate.
1443 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1444 free((void*)SavedStrings[I]);
1445}
1446
Douglas Gregor14f79002009-04-10 03:52:48 +00001447/// \brief Writes the block containing the serialized form of the
1448/// source manager.
1449///
1450/// TODO: We should probably use an on-disk hash table (stored in a
1451/// blob), indexed based on the file name, so that we only create
1452/// entries for files that we actually need. In the common case (no
1453/// errors), we probably won't have to create file entries for any of
1454/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001455void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001456 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001457 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001458 RecordData Record;
1459
Chris Lattnerf04ad692009-04-10 17:16:57 +00001460 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001461 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001462
1463 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001464 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1465 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1466 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001467 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001468
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001469 // Write out the source location entry table. We skip the first
1470 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001471 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001472 // Write out the offsets of only source location file entries.
1473 // We will go through them in ASTReader::validateFileEntries().
1474 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001475 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001476 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1477 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001478 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001479 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001480 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001481
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001482 // Record the offset of this source-location entry.
1483 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1484
1485 // Figure out which record code to use.
1486 unsigned Code;
1487 if (SLoc->isFile()) {
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001488 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001489 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001490 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1491 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001492 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001493 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001494 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001495 Record.clear();
1496 Record.push_back(Code);
1497
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001498 // Starting offset of this entry within this module, so skip the dummy.
1499 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001500 if (SLoc->isFile()) {
1501 const SrcMgr::FileInfo &File = SLoc->getFile();
1502 Record.push_back(File.getIncludeLoc().getRawEncoding());
1503 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1504 Record.push_back(File.hasLineDirectives());
1505
1506 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001507 if (Content->OrigEntry) {
1508 assert(Content->OrigEntry == Content->ContentsEntry &&
1509 "Writing to AST an overriden file is not supported");
1510
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001511 // The source location entry is a file. The blob associated
1512 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregor2d52be52010-03-21 22:49:54 +00001514 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001515 Record.push_back(Content->OrigEntry->getSize());
1516 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001517
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001518 Record.push_back(File.NumCreatedFIDs);
1519
Douglas Gregore650c8c2009-07-07 00:12:59 +00001520 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001521 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001522 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001523
1524 // Ask the file manager to fixup the relative path for us. This will
1525 // honor the working directory.
1526 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1527
1528 // FIXME: This call to make_absolute shouldn't be necessary, the
1529 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001530 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001531 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Douglas Gregore650c8c2009-07-07 00:12:59 +00001533 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001534 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001535 } else {
1536 // The source location entry is a buffer. The blob associated
1537 // with this entry contains the contents of the buffer.
1538
1539 // We add one to the size so that we capture the trailing NULL
1540 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1541 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001542 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001543 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001544 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001545 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001546 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001548 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001549 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001550 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001551 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001553 if (strcmp(Name, "<built-in>") == 0) {
1554 PreloadSLocs.push_back(SLocEntryOffsets.size());
1555 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001556 }
1557 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001558 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001559 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001560 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1561 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001562 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1563 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001564
1565 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001566 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001567 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001568 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001569 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001570 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001571 }
1572 }
1573
Douglas Gregorc9490c02009-04-16 22:23:12 +00001574 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001575
1576 if (SLocEntryOffsets.empty())
1577 return;
1578
Sebastian Redl3397c552010-08-18 23:56:27 +00001579 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001580 // table is used for lazily loading source-location information.
1581 using namespace llvm;
1582 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001583 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1587 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001589 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001590 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001591 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001592 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001593 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001594
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001595 Abbrev = new BitCodeAbbrev();
1596 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1597 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1599 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1600
1601 Record.clear();
1602 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1603 Record.push_back(SLocFileEntryOffsets.size());
1604 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1605 data(SLocFileEntryOffsets));
1606
Sebastian Redl3397c552010-08-18 23:56:27 +00001607 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001608 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001609 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001610
1611 // Write the line table. It depends on remapping working, so it must come
1612 // after the source location offsets.
1613 if (SourceMgr.hasLineTable()) {
1614 LineTableInfo &LineTable = SourceMgr.getLineTable();
1615
1616 Record.clear();
1617 // Emit the file names
1618 Record.push_back(LineTable.getNumFilenames());
1619 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1620 // Emit the file name
1621 const char *Filename = LineTable.getFilename(I);
1622 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1623 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1624 Record.push_back(FilenameLen);
1625 if (FilenameLen)
1626 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1627 }
1628
1629 // Emit the line entries
1630 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1631 L != LEnd; ++L) {
1632 // Only emit entries for local files.
1633 if (L->first < 0)
1634 continue;
1635
1636 // Emit the file ID
1637 Record.push_back(L->first);
1638
1639 // Emit the line entries
1640 Record.push_back(L->second.size());
1641 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1642 LEEnd = L->second.end();
1643 LE != LEEnd; ++LE) {
1644 Record.push_back(LE->FileOffset);
1645 Record.push_back(LE->LineNo);
1646 Record.push_back(LE->FilenameID);
1647 Record.push_back((unsigned)LE->FileKind);
1648 Record.push_back(LE->IncludeOffset);
1649 }
1650 }
1651 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1652 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001653}
1654
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001655//===----------------------------------------------------------------------===//
1656// Preprocessor Serialization
1657//===----------------------------------------------------------------------===//
1658
Douglas Gregor9c736102011-02-10 18:20:09 +00001659static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1660 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1661 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1662 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1663 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1664 return X.first->getName().compare(Y.first->getName());
1665}
1666
Chris Lattner0b1fb982009-04-10 17:15:23 +00001667/// \brief Writes the block containing the serialized form of the
1668/// preprocessor.
1669///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001670void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001671 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001672
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001673 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1674 if (PP.getCounterValue() != 0) {
1675 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001676 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001677 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001678 }
1679
1680 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001681 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Sebastian Redl3397c552010-08-18 23:56:27 +00001683 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001684 // FIXME: use diagnostics subsystem for localization etc.
1685 if (PP.SawDateOrTime())
1686 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Douglas Gregorecdcb882010-10-20 22:00:55 +00001688
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001689 // Loop over all the macro definitions that are live at the end of the file,
1690 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001691 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001692
Douglas Gregor9c736102011-02-10 18:20:09 +00001693 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001694 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001695 MacrosToEmit;
1696 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001697 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1698 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001699 I != E; ++I) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00001700 if (!IsModule || I->second->isExported()) {
1701 MacroDefinitionsSeen.insert(I->first);
1702 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1703 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001704 }
1705
1706 // Sort the set of macro definitions that need to be serialized by the
1707 // name of the macro, to provide a stable ordering.
1708 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1709 &compareMacroDefinitions);
1710
Douglas Gregor040a8042011-02-11 00:26:14 +00001711 // Resolve any identifiers that defined macros at the time they were
1712 // deserialized, adding them to the list of macros to emit (if appropriate).
1713 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1714 IdentifierInfo *Name
1715 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1716 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1717 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1718 }
1719
Douglas Gregor9c736102011-02-10 18:20:09 +00001720 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1721 const IdentifierInfo *Name = MacrosToEmit[I].first;
1722 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001723 if (!MI)
1724 continue;
1725
Sebastian Redl3397c552010-08-18 23:56:27 +00001726 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001727 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001728 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001729
1730 // FIXME: There is a (probably minor) optimization we could do here, if
1731 // the macro comes from the original PCH but the identifier comes from a
1732 // chained PCH, by storing the offset into the original PCH rather than
1733 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001734 if (MI->isBuiltinMacro() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00001735 (Chain && Name->isFromAST() && MI->isFromAST() &&
1736 !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001737 continue;
1738
Douglas Gregor9c736102011-02-10 18:20:09 +00001739 AddIdentifierRef(Name, Record);
1740 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001741 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1742 Record.push_back(MI->isUsed());
Douglas Gregor7143aab2011-09-01 17:04:32 +00001743 AddSourceLocation(MI->getExportLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001744 unsigned Code;
1745 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001746 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001747 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001748 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001750 Record.push_back(MI->isC99Varargs());
1751 Record.push_back(MI->isGNUVarargs());
1752 Record.push_back(MI->getNumArgs());
1753 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1754 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001755 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001756 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001757
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001758 // If we have a detailed preprocessing record, record the macro definition
1759 // ID that corresponds to this macro.
1760 if (PPRec)
1761 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001762
Douglas Gregorc9490c02009-04-16 22:23:12 +00001763 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001764 Record.clear();
1765
Chris Lattnerdf961c22009-04-10 18:08:30 +00001766 // Emit the tokens array.
1767 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1768 // Note that we know that the preprocessor does not have any annotation
1769 // tokens in it because they are created by the parser, and thus can't be
1770 // in a macro definition.
1771 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Chris Lattnerdf961c22009-04-10 18:08:30 +00001773 Record.push_back(Tok.getLocation().getRawEncoding());
1774 Record.push_back(Tok.getLength());
1775
Chris Lattnerdf961c22009-04-10 18:08:30 +00001776 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1777 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001778 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001779 // FIXME: Should translate token kind to a stable encoding.
1780 Record.push_back(Tok.getKind());
1781 // FIXME: Should translate token flags to a stable encoding.
1782 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001784 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001785 Record.clear();
1786 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001787 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001788 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001789 Stream.ExitBlock();
1790
1791 if (PPRec)
1792 WritePreprocessorDetail(*PPRec);
1793}
1794
1795void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1796 if (PPRec.begin(Chain) == PPRec.end(Chain))
1797 return;
1798
1799 // Enter the preprocessor block.
1800 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001801
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001802 // If the preprocessor has a preprocessing record, emit it.
1803 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001804 using namespace llvm;
1805
1806 // Set up the abbreviation for
1807 unsigned InclusionAbbrev = 0;
1808 {
1809 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1810 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1818 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1819 }
1820
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001821 unsigned FirstPreprocessorEntityID
1822 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1823 + NUM_PREDEF_PP_ENTITY_IDS;
1824 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001825 RecordData Record;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001826 uint64_t BitsInChain = Chain? Chain->TotalModulesSizeInBits : 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001827 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1828 EEnd = PPRec.end(Chain);
Douglas Gregor7338a922011-08-04 17:06:18 +00001829 E != EEnd;
1830 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001831 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001832
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001833 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1834 // Record this macro definition's location.
1835 MacroID ID = getMacroDefinitionID(MD);
1836
1837 // Don't write the macro definition if it is from another AST file.
1838 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001839 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001840
Douglas Gregor89d99802010-11-30 06:16:57 +00001841 // Notify the serialization listener that we're serializing this entity.
1842 if (SerializationListener)
1843 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001844 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001845
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001846 unsigned Position = ID - FirstMacroID;
1847 if (Position != MacroDefinitionOffsets.size()) {
1848 if (Position > MacroDefinitionOffsets.size())
1849 MacroDefinitionOffsets.resize(Position + 1);
1850
1851 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1852 } else
1853 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001854
Douglas Gregor7338a922011-08-04 17:06:18 +00001855 Record.push_back(NextPreprocessorEntityID);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001856 Record.push_back(ID);
1857 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1858 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1859 AddIdentifierRef(MD->getName(), Record);
1860 AddSourceLocation(MD->getLocation(), Record);
1861 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1862 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001863 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001864
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001865 // Notify the serialization listener that we're serializing this entity.
1866 if (SerializationListener)
1867 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001868 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001869
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001870 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Douglas Gregor7338a922011-08-04 17:06:18 +00001871 Record.push_back(NextPreprocessorEntityID);
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001872 AddSourceLocation(ME->getSourceRange().getBegin(), Record);
1873 AddSourceLocation(ME->getSourceRange().getEnd(), Record);
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001874 Record.push_back(ME->isBuiltinMacro());
1875 if (ME->isBuiltinMacro())
1876 AddIdentifierRef(ME->getName(), Record);
1877 else
1878 Record.push_back(getMacroDefinitionID(ME->getDefinition()));
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001879 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001880 continue;
1881 }
1882
1883 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1884 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor7338a922011-08-04 17:06:18 +00001885 Record.push_back(NextPreprocessorEntityID);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001886 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1887 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1888 Record.push_back(ID->getFileName().size());
1889 Record.push_back(ID->wasInQuotes());
1890 Record.push_back(static_cast<unsigned>(ID->getKind()));
1891 llvm::SmallString<64> Buffer;
1892 Buffer += ID->getFileName();
1893 Buffer += ID->getFile()->getName();
1894 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1895 continue;
1896 }
1897
1898 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1899 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001900 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001901
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001902 // Write the offsets table for the preprocessing record.
1903 if (NumPreprocessingRecords > 0) {
1904 // Write the offsets table for identifier IDs.
1905 using namespace llvm;
1906 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001907 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00001911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first macro def
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1913 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001914
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001915 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001916 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001917 Record.push_back(NumPreprocessingRecords);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001918 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001919 Record.push_back(MacroDefinitionOffsets.size());
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00001920 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001921 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001922 data(MacroDefinitionOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001923 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001924}
1925
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001926void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001927 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001928 for (Diagnostic::DiagStatePointsTy::const_iterator
1929 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1930 I != E; ++I) {
1931 const Diagnostic::DiagStatePoint &point = *I;
1932 if (point.Loc.isInvalid())
1933 continue;
1934
1935 Record.push_back(point.Loc.getRawEncoding());
1936 for (Diagnostic::DiagState::iterator
1937 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1938 unsigned diag = I->first, map = I->second;
1939 if (map & 0x10) { // mapping from a diagnostic pragma.
1940 Record.push_back(diag);
1941 Record.push_back(map & 0x7);
1942 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001943 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001944 Record.push_back(-1); // mark the end of the diag/map pairs for this
1945 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001946 }
1947
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001948 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001949 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001950}
1951
Anders Carlssonc8505782011-03-06 18:41:18 +00001952void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1953 if (CXXBaseSpecifiersOffsets.empty())
1954 return;
1955
1956 RecordData Record;
1957
1958 // Create a blob abbreviation for the C++ base specifiers offsets.
1959 using namespace llvm;
1960
1961 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1962 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1965 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1966
Douglas Gregore92b8a12011-08-04 00:01:48 +00001967 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00001968 Record.clear();
1969 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1970 Record.push_back(CXXBaseSpecifiersOffsets.size());
1971 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001972 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00001973}
1974
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001975//===----------------------------------------------------------------------===//
1976// Type Serialization
1977//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001978
Sebastian Redl3397c552010-08-18 23:56:27 +00001979/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001980void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001981 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001982 if (Idx.getIndex() == 0) // we haven't seen this type before.
1983 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Douglas Gregor97475832010-10-05 18:37:06 +00001985 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001986
Douglas Gregor2cf26342009-04-09 22:27:44 +00001987 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001988 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001989 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001990 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001991 else if (TypeOffsets.size() < Index) {
1992 TypeOffsets.resize(Index + 1);
1993 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001994 }
1995
1996 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Douglas Gregor2cf26342009-04-09 22:27:44 +00001998 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001999 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002000
Douglas Gregora4923eb2009-11-16 21:35:15 +00002001 if (T.hasLocalNonFastQualifiers()) {
2002 Qualifiers Qs = T.getLocalQualifiers();
2003 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002004 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002005 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002006 } else {
2007 switch (T->getTypeClass()) {
2008 // For all of the concrete, non-dependent types, call the
2009 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002010#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002011 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002012#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002013#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002014 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002015 }
2016
2017 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002018 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002019
2020 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002021 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002022}
2023
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002024//===----------------------------------------------------------------------===//
2025// Declaration Serialization
2026//===----------------------------------------------------------------------===//
2027
Douglas Gregor2cf26342009-04-09 22:27:44 +00002028/// \brief Write the block containing all of the declaration IDs
2029/// lexically declared within the given DeclContext.
2030///
2031/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2032/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002033uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002034 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002035 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002036 return 0;
2037
Douglas Gregorc9490c02009-04-16 22:23:12 +00002038 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002039 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002040 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002041 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002042 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2043 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002044 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002045
Douglas Gregor25123082009-04-22 22:34:57 +00002046 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002047 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002048 return Offset;
2049}
2050
Sebastian Redla4232eb2010-08-18 23:56:21 +00002051void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002052 using namespace llvm;
2053 RecordData Record;
2054
2055 // Write the type offsets array
2056 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002057 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2061 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2062 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002063 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002064 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002065 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002066 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002067
2068 // Write the declaration offsets array
2069 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002070 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2074 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2075 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002076 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002077 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002078 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002079 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002080}
2081
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002082//===----------------------------------------------------------------------===//
2083// Global Method Pool and Selector Serialization
2084//===----------------------------------------------------------------------===//
2085
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002086namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002087// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002088class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002089 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002090
2091public:
2092 typedef Selector key_type;
2093 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Sebastian Redl5d050072010-08-04 17:20:04 +00002095 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002096 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002097 ObjCMethodList Instance, Factory;
2098 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002099 typedef const data_type& data_type_ref;
2100
Sebastian Redl3397c552010-08-18 23:56:27 +00002101 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002103 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002104 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002105 }
Mike Stump1eb44332009-09-09 15:08:12 +00002106
2107 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002108 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002109 data_type_ref Methods) {
2110 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2111 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002112 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2113 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002114 Method = Method->Next)
2115 if (Method->Method)
2116 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002117 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002118 Method = Method->Next)
2119 if (Method->Method)
2120 DataLen += 4;
2121 clang::io::Emit16(Out, DataLen);
2122 return std::make_pair(KeyLen, DataLen);
2123 }
Mike Stump1eb44332009-09-09 15:08:12 +00002124
Chris Lattner5f9e2722011-07-23 10:55:15 +00002125 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002126 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002127 assert((Start >> 32) == 0 && "Selector key offset too large");
2128 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002129 unsigned N = Sel.getNumArgs();
2130 clang::io::Emit16(Out, N);
2131 if (N == 0)
2132 N = 1;
2133 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002134 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002135 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2136 }
Mike Stump1eb44332009-09-09 15:08:12 +00002137
Chris Lattner5f9e2722011-07-23 10:55:15 +00002138 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002139 data_type_ref Methods, unsigned DataLen) {
2140 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002141 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002142 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002143 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002144 Method = Method->Next)
2145 if (Method->Method)
2146 ++NumInstanceMethods;
2147
2148 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002149 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002150 Method = Method->Next)
2151 if (Method->Method)
2152 ++NumFactoryMethods;
2153
2154 clang::io::Emit16(Out, NumInstanceMethods);
2155 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002156 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002157 Method = Method->Next)
2158 if (Method->Method)
2159 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002160 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002161 Method = Method->Next)
2162 if (Method->Method)
2163 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002164
2165 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002166 }
2167};
2168} // end anonymous namespace
2169
Sebastian Redl059612d2010-08-03 21:58:15 +00002170/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002171///
2172/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002173/// in an on-disk hash table indexed by the selector. The hash table also
2174/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002175void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002176 using namespace llvm;
2177
Sebastian Redl059612d2010-08-03 21:58:15 +00002178 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002179 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002180 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002181 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002182 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002183 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002184 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002185 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002186
Sebastian Redl059612d2010-08-03 21:58:15 +00002187 // Create the on-disk hash table representation. We walk through every
2188 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002189 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002190 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002191 I = SelectorIDs.begin(), E = SelectorIDs.end();
2192 I != E; ++I) {
2193 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002194 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002195 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002196 I->second,
2197 ObjCMethodList(),
2198 ObjCMethodList()
2199 };
2200 if (F != SemaRef.MethodPool.end()) {
2201 Data.Instance = F->second.first;
2202 Data.Factory = F->second.second;
2203 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002204 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002205 // changed.
2206 if (Chain && I->second < FirstSelectorID) {
2207 // Selector already exists. Did it change?
2208 bool changed = false;
2209 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2210 M = M->Next) {
2211 if (M->Method->getPCHLevel() == 0)
2212 changed = true;
2213 }
2214 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2215 M = M->Next) {
2216 if (M->Method->getPCHLevel() == 0)
2217 changed = true;
2218 }
2219 if (!changed)
2220 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002221 } else if (Data.Instance.Method || Data.Factory.Method) {
2222 // A new method pool entry.
2223 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002224 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002225 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002226 }
2227
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002228 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002229 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002230 uint32_t BucketOffset;
2231 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002232 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002233 llvm::raw_svector_ostream Out(MethodPool);
2234 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002235 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002236 BucketOffset = Generator.Emit(Out, Trait);
2237 }
2238
2239 // Create a blob abbreviation
2240 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002241 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2245 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2246
Douglas Gregor83941df2009-04-25 17:48:32 +00002247 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002248 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002249 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002250 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002251 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002252 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002253
2254 // Create a blob abbreviation for the selector table offsets.
2255 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002256 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2260 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2261
2262 // Write the selector offsets table.
2263 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002264 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002265 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002266 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002267 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002268 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002269 }
2270}
2271
Sebastian Redl3397c552010-08-18 23:56:27 +00002272/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002273void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002274 using namespace llvm;
2275 if (SemaRef.ReferencedSelectors.empty())
2276 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002277
Fariborz Jahanian32019832010-07-23 19:11:11 +00002278 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002279
Sebastian Redl3397c552010-08-18 23:56:27 +00002280 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002281 // very tricky to fix, and given that @selector shouldn't really appear in
2282 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002283 for (DenseMap<Selector, SourceLocation>::iterator S =
2284 SemaRef.ReferencedSelectors.begin(),
2285 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2286 Selector Sel = (*S).first;
2287 SourceLocation Loc = (*S).second;
2288 AddSelectorRef(Sel, Record);
2289 AddSourceLocation(Loc, Record);
2290 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002291 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002292}
2293
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002294//===----------------------------------------------------------------------===//
2295// Identifier Table Serialization
2296//===----------------------------------------------------------------------===//
2297
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002298namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002299class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002300 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002301 Preprocessor &PP;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002302 bool IsModule;
2303
Douglas Gregora92193e2009-04-28 21:18:29 +00002304 /// \brief Determines whether this is an "interesting" identifier
2305 /// that needs a full IdentifierInfo structure written into the hash
2306 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002307 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
2308 Macro = 0;
2309
2310 if (II->isPoisoned() ||
2311 II->isExtensionToken() ||
2312 II->getObjCOrBuiltinID() ||
2313 II->getFETokenInfo<void>())
2314 return true;
2315
2316 if (!II->hasMacroDefinition())
2317 return false;
2318
2319 if (!IsModule)
2320 return true;
2321
2322 if ((Macro = PP.getMacroInfo(II)))
2323 return Macro->isExported();
2324
2325 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002326 }
2327
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002328public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002329 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002330 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002332 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002333 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002334
Douglas Gregor7143aab2011-09-01 17:04:32 +00002335 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP, bool IsModule)
2336 : Writer(Writer), PP(PP), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002337
2338 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002339 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002340 }
Mike Stump1eb44332009-09-09 15:08:12 +00002341
2342 std::pair<unsigned,unsigned>
Douglas Gregor7143aab2011-09-01 17:04:32 +00002343 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002344 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002345 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregor7143aab2011-09-01 17:04:32 +00002346 MacroInfo *Macro;
2347 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002348 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002349 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002350 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002351 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002352 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2353 DEnd = IdentifierResolver::end();
2354 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002355 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002356 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002357 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002358 // We emit the key length after the data length so that every
2359 // string is preceded by a 16-bit length. This matches the PTH
2360 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002361 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002362 return std::make_pair(KeyLen, DataLen);
2363 }
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Chris Lattner5f9e2722011-07-23 10:55:15 +00002365 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002366 unsigned KeyLen) {
2367 // Record the location of the key data. This is used when generating
2368 // the mapping from persistent IDs to strings.
2369 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002370 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002371 }
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Douglas Gregor7143aab2011-09-01 17:04:32 +00002373 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002374 IdentID ID, unsigned) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002375 MacroInfo *Macro;
2376 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002377 clang::io::Emit32(Out, ID << 1);
2378 return;
2379 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002380
Douglas Gregora92193e2009-04-28 21:18:29 +00002381 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002382 uint32_t Bits = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002383 bool hasMacroDefinition
2384 = II->hasMacroDefinition() &&
2385 (Macro || (Macro = PP.getMacroInfo(II))) && !Macro->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002386 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002387 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2388 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2389 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002390 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002391 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002392 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002393
Douglas Gregor37e26842009-04-21 23:56:24 +00002394 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002395 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002396
Douglas Gregor668c1a42009-04-21 22:25:48 +00002397 // Emit the declaration IDs in reverse order, because the
2398 // IdentifierResolver provides the declarations as they would be
2399 // visible (e.g., the function "stat" would come before the struct
2400 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2401 // adds declarations to the end of the list (so we need to see the
2402 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002403 // Only emit declarations that aren't from a chained PCH, though.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002404 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002405 IdentifierResolver::end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002406 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002407 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002408 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002409 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002410 }
2411};
2412} // end anonymous namespace
2413
Sebastian Redl3397c552010-08-18 23:56:27 +00002414/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002415///
2416/// The identifier table consists of a blob containing string data
2417/// (the actual identifiers themselves) and a separate "offsets" index
2418/// that maps identifier IDs to locations within the blob.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002419void ASTWriter::WriteIdentifierTable(Preprocessor &PP, bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002420 using namespace llvm;
2421
2422 // Create and write out the blob that contains the identifier
2423 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002424 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002425 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002426 ASTIdentifierTableTrait Trait(*this, PP, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002427
Douglas Gregor92b059e2009-04-28 20:33:11 +00002428 // Look for any identifiers that were named while processing the
2429 // headers, but are otherwise not needed. We add these to the hash
2430 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002431 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002432 // file.
2433 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2434 IDEnd = PP.getIdentifierTable().end();
2435 ID != IDEnd; ++ID)
2436 getIdentifierRef(ID->second);
2437
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002438 // Create the on-disk hash table representation. We only store offsets
2439 // for identifiers that appear here for the first time.
2440 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002441 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002442 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2443 ID != IDEnd; ++ID) {
2444 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002445 if (!Chain || !ID->first->isFromAST())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002446 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2447 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002448 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002449
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002450 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002451 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002452 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002453 {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002454 ASTIdentifierTableTrait Trait(*this, PP, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002455 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002456 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002457 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002458 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002459 }
2460
2461 // Create a blob abbreviation
2462 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002463 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002466 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002467
2468 // Write the identifier table
2469 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002470 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002471 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002472 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002473 }
2474
2475 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002476 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002477 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2481 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2482
2483 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002484 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002485 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002486 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002487 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002488 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002489}
2490
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002491//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002492// DeclContext's Name Lookup Table Serialization
2493//===----------------------------------------------------------------------===//
2494
2495namespace {
2496// Trait used for the on-disk hash table used in the method pool.
2497class ASTDeclContextNameLookupTrait {
2498 ASTWriter &Writer;
2499
2500public:
2501 typedef DeclarationName key_type;
2502 typedef key_type key_type_ref;
2503
2504 typedef DeclContext::lookup_result data_type;
2505 typedef const data_type& data_type_ref;
2506
2507 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2508
2509 unsigned ComputeHash(DeclarationName Name) {
2510 llvm::FoldingSetNodeID ID;
2511 ID.AddInteger(Name.getNameKind());
2512
2513 switch (Name.getNameKind()) {
2514 case DeclarationName::Identifier:
2515 ID.AddString(Name.getAsIdentifierInfo()->getName());
2516 break;
2517 case DeclarationName::ObjCZeroArgSelector:
2518 case DeclarationName::ObjCOneArgSelector:
2519 case DeclarationName::ObjCMultiArgSelector:
2520 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2521 break;
2522 case DeclarationName::CXXConstructorName:
2523 case DeclarationName::CXXDestructorName:
2524 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002525 break;
2526 case DeclarationName::CXXOperatorName:
2527 ID.AddInteger(Name.getCXXOverloadedOperator());
2528 break;
2529 case DeclarationName::CXXLiteralOperatorName:
2530 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2531 case DeclarationName::CXXUsingDirective:
2532 break;
2533 }
2534
2535 return ID.ComputeHash();
2536 }
2537
2538 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002539 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002540 data_type_ref Lookup) {
2541 unsigned KeyLen = 1;
2542 switch (Name.getNameKind()) {
2543 case DeclarationName::Identifier:
2544 case DeclarationName::ObjCZeroArgSelector:
2545 case DeclarationName::ObjCOneArgSelector:
2546 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002547 case DeclarationName::CXXLiteralOperatorName:
2548 KeyLen += 4;
2549 break;
2550 case DeclarationName::CXXOperatorName:
2551 KeyLen += 1;
2552 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002553 case DeclarationName::CXXConstructorName:
2554 case DeclarationName::CXXDestructorName:
2555 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002556 case DeclarationName::CXXUsingDirective:
2557 break;
2558 }
2559 clang::io::Emit16(Out, KeyLen);
2560
2561 // 2 bytes for num of decls and 4 for each DeclID.
2562 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2563 clang::io::Emit16(Out, DataLen);
2564
2565 return std::make_pair(KeyLen, DataLen);
2566 }
2567
Chris Lattner5f9e2722011-07-23 10:55:15 +00002568 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002569 using namespace clang::io;
2570
2571 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2572 Emit8(Out, Name.getNameKind());
2573 switch (Name.getNameKind()) {
2574 case DeclarationName::Identifier:
2575 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2576 break;
2577 case DeclarationName::ObjCZeroArgSelector:
2578 case DeclarationName::ObjCOneArgSelector:
2579 case DeclarationName::ObjCMultiArgSelector:
2580 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2581 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002582 case DeclarationName::CXXOperatorName:
2583 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2584 Emit8(Out, Name.getCXXOverloadedOperator());
2585 break;
2586 case DeclarationName::CXXLiteralOperatorName:
2587 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2588 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002589 case DeclarationName::CXXConstructorName:
2590 case DeclarationName::CXXDestructorName:
2591 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002592 case DeclarationName::CXXUsingDirective:
2593 break;
2594 }
2595 }
2596
Chris Lattner5f9e2722011-07-23 10:55:15 +00002597 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002598 data_type Lookup, unsigned DataLen) {
2599 uint64_t Start = Out.tell(); (void)Start;
2600 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2601 for (; Lookup.first != Lookup.second; ++Lookup.first)
2602 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2603
2604 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2605 }
2606};
2607} // end anonymous namespace
2608
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002609/// \brief Write the block containing all of the declaration IDs
2610/// visible from the given DeclContext.
2611///
2612/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002613/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002614uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2615 DeclContext *DC) {
2616 if (DC->getPrimaryContext() != DC)
2617 return 0;
2618
2619 // Since there is no name lookup into functions or methods, don't bother to
2620 // build a visible-declarations table for these entities.
2621 if (DC->isFunctionOrMethod())
2622 return 0;
2623
2624 // If not in C++, we perform name lookup for the translation unit via the
2625 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2626 // FIXME: In C++ we need the visible declarations in order to "see" the
2627 // friend declarations, is there a way to do this without writing the table ?
2628 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2629 return 0;
2630
2631 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002632 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002633 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002634
2635 // Serialize the contents of the mapping used for lookup. Note that,
2636 // although we have two very different code paths, the serialized
2637 // representation is the same for both cases: a declaration name,
2638 // followed by a size, followed by references to the visible
2639 // declarations that have that name.
2640 uint64_t Offset = Stream.GetCurrentBitNo();
2641 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2642 if (!Map || Map->empty())
2643 return 0;
2644
2645 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2646 ASTDeclContextNameLookupTrait Trait(*this);
2647
2648 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002649 DeclarationName ConversionName;
2650 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002651 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2652 D != DEnd; ++D) {
2653 DeclarationName Name = D->first;
2654 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002655 if (Result.first != Result.second) {
2656 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2657 // Hash all conversion function names to the same name. The actual
2658 // type information in conversion function name is not used in the
2659 // key (since such type information is not stable across different
2660 // modules), so the intended effect is to coalesce all of the conversion
2661 // functions under a single key.
2662 if (!ConversionName)
2663 ConversionName = Name;
2664 ConversionDecls.append(Result.first, Result.second);
2665 continue;
2666 }
2667
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002668 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002669 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002670 }
2671
Douglas Gregore5a54b62011-08-30 20:49:19 +00002672 // Add the conversion functions
2673 if (!ConversionDecls.empty()) {
2674 Generator.insert(ConversionName,
2675 DeclContext::lookup_result(ConversionDecls.begin(),
2676 ConversionDecls.end()),
2677 Trait);
2678 }
2679
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002680 // Create the on-disk hash table in a buffer.
2681 llvm::SmallString<4096> LookupTable;
2682 uint32_t BucketOffset;
2683 {
2684 llvm::raw_svector_ostream Out(LookupTable);
2685 // Make sure that no bucket is at offset 0
2686 clang::io::Emit32(Out, 0);
2687 BucketOffset = Generator.Emit(Out, Trait);
2688 }
2689
2690 // Write the lookup table
2691 RecordData Record;
2692 Record.push_back(DECL_CONTEXT_VISIBLE);
2693 Record.push_back(BucketOffset);
2694 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2695 LookupTable.str());
2696
2697 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2698 ++NumVisibleDeclContexts;
2699 return Offset;
2700}
2701
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002702/// \brief Write an UPDATE_VISIBLE block for the given context.
2703///
2704/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2705/// DeclContext in a dependent AST file. As such, they only exist for the TU
2706/// (in C++) and for namespaces.
2707void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002708 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2709 if (!Map || Map->empty())
2710 return;
2711
2712 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2713 ASTDeclContextNameLookupTrait Trait(*this);
2714
2715 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002716 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2717 D != DEnd; ++D) {
2718 DeclarationName Name = D->first;
2719 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002720 // For any name that appears in this table, the results are complete, i.e.
2721 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002722 if (Result.first != Result.second)
2723 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002724 }
2725
2726 // Create the on-disk hash table in a buffer.
2727 llvm::SmallString<4096> LookupTable;
2728 uint32_t BucketOffset;
2729 {
2730 llvm::raw_svector_ostream Out(LookupTable);
2731 // Make sure that no bucket is at offset 0
2732 clang::io::Emit32(Out, 0);
2733 BucketOffset = Generator.Emit(Out, Trait);
2734 }
2735
2736 // Write the lookup table
2737 RecordData Record;
2738 Record.push_back(UPDATE_VISIBLE);
2739 Record.push_back(getDeclID(cast<Decl>(DC)));
2740 Record.push_back(BucketOffset);
2741 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2742}
2743
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002744/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2745void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2746 RecordData Record;
2747 Record.push_back(Opts.fp_contract);
2748 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2749}
2750
2751/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2752void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2753 if (!SemaRef.Context.getLangOptions().OpenCL)
2754 return;
2755
2756 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2757 RecordData Record;
2758#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2759#include "clang/Basic/OpenCLExtensions.def"
2760 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2761}
2762
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002763//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002764// General Serialization Routines
2765//===----------------------------------------------------------------------===//
2766
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002767/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002768void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002769 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002770 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2771 const Attr * A = *i;
2772 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2773 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002774
Sean Huntcf807c42010-08-18 23:23:40 +00002775#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002776
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002777 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002778}
2779
Chris Lattner5f9e2722011-07-23 10:55:15 +00002780void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002781 Record.push_back(Str.size());
2782 Record.insert(Record.end(), Str.begin(), Str.end());
2783}
2784
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002785void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2786 RecordDataImpl &Record) {
2787 Record.push_back(Version.getMajor());
2788 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2789 Record.push_back(*Minor + 1);
2790 else
2791 Record.push_back(0);
2792 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2793 Record.push_back(*Subminor + 1);
2794 else
2795 Record.push_back(0);
2796}
2797
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002798/// \brief Note that the identifier II occurs at the given offset
2799/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002800void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002801 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002802 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002803 // up earlier in the chain and thus don't need an offset.
2804 if (ID >= FirstIdentID)
2805 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002806}
2807
Douglas Gregor83941df2009-04-25 17:48:32 +00002808/// \brief Note that the selector Sel occurs at the given offset
2809/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002810void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002811 unsigned ID = SelectorIDs[Sel];
2812 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002813 // Don't record offsets for selectors that are also available in a different
2814 // file.
2815 if (ID < FirstSelectorID)
2816 return;
2817 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002818}
2819
Sebastian Redla4232eb2010-08-18 23:56:21 +00002820ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002821 : Stream(Stream), Context(0), Chain(0), SerializationListener(0),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002822 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002823 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002824 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002825 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002826 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002827 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002828 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002829 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00002830 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002831 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002832 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2833 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2834 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002835 DeclTypedefAbbrev(0),
2836 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2837 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00002838{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002839}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002840
Sebastian Redla4232eb2010-08-18 23:56:21 +00002841void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002842 const std::string &OutputFile,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002843 bool IsModule, StringRef isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002844 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002845 Stream.Emit((unsigned)'C', 8);
2846 Stream.Emit((unsigned)'P', 8);
2847 Stream.Emit((unsigned)'C', 8);
2848 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002849
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002850 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002851
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002852 Context = &SemaRef.Context;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002853 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, IsModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002854 Context = 0;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002855}
2856
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002857template<typename Vector>
2858static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2859 ASTWriter::RecordData &Record) {
2860 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2861 I != E; ++I) {
2862 Writer.AddDeclRef(*I, Record);
2863 }
2864}
2865
Sebastian Redla4232eb2010-08-18 23:56:21 +00002866void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00002867 StringRef isysroot,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002868 const std::string &OutputFile, bool IsModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002869 using namespace llvm;
2870
2871 ASTContext &Context = SemaRef.Context;
2872 Preprocessor &PP = SemaRef.PP;
2873
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002874 // Set up predefined declaration IDs.
2875 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00002876 if (Context.ObjCIdDecl)
2877 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00002878 if (Context.ObjCSelDecl)
2879 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00002880 if (Context.ObjCClassDecl)
2881 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00002882 if (Context.Int128Decl)
2883 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
2884 if (Context.UInt128Decl)
2885 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00002886 if (Context.ObjCInstanceTypeDecl)
2887 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00002888
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002889 if (!Chain) {
2890 // Make sure that we emit IdentifierInfos (and any attached
2891 // declarations) for builtins. We don't need to do this when we're
2892 // emitting chained PCH files, because all of the builtins will be
2893 // in the original PCH file.
2894 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00002895 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002896 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002897 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2898 Context.getLangOptions().NoBuiltin);
2899 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2900 getIdentifierRef(&Table.get(BuiltinNames[I]));
2901 }
2902
Chris Lattner63d65f82009-09-08 18:19:27 +00002903 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002904 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002905 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002906 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002907 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00002908
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002909 // Build a record containing all of the file scoped decls in this file.
2910 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002911 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
2912 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002913
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002914 // Build a record containing all of the delegating constructors we still need
2915 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00002916 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00002917 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00002918
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002919 // Write the set of weak, undeclared identifiers. We always write the
2920 // entire table, since later PCH files in a PCH chain are only interested in
2921 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002922 RecordData WeakUndeclaredIdentifiers;
2923 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00002924 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002925 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2926 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2927 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2928 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2929 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2930 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2931 }
2932 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002933
Douglas Gregor14c22f22009-04-22 22:18:58 +00002934 // Build a record containing all of the locally-scoped external
2935 // declarations in this header file. Generally, this record will be
2936 // empty.
2937 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002938 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002939 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002940 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002941 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2942 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00002943 TD != TDEnd; ++TD) {
2944 if (TD->second->getPCHLevel() == 0)
2945 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2946 }
2947
Douglas Gregorb81c1702009-04-27 20:06:05 +00002948 // Build a record containing all of the ext_vector declarations.
2949 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00002950 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002951
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002952 // Build a record containing all of the VTable uses information.
2953 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002954 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002955 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2956 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2957 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2958 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2959 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002960 }
2961
2962 // Build a record containing all of dynamic classes declarations.
2963 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00002964 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002965
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002966 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002967 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002968 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002969 I = SemaRef.PendingInstantiations.begin(),
2970 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2971 AddDeclRef(I->first, PendingInstantiations);
2972 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002973 }
2974 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2975 "There are local ones at end of translation unit!");
2976
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002977 // Build a record containing some declaration references.
2978 RecordData SemaDeclRefs;
2979 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2980 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2981 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2982 }
2983
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002984 RecordData CUDASpecialDeclRefs;
2985 if (Context.getcudaConfigureCallDecl()) {
2986 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2987 }
2988
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002989 // Build a record containing all of the known namespaces.
2990 RecordData KnownNamespaces;
2991 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2992 I = SemaRef.KnownNamespaces.begin(),
2993 IEnd = SemaRef.KnownNamespaces.end();
2994 I != IEnd; ++I) {
2995 if (!I->second)
2996 AddDeclRef(I->first, KnownNamespaces);
2997 }
2998
Sebastian Redl3397c552010-08-18 23:56:27 +00002999 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003000 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003001 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003002 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003003 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00003004 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003005 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00003006 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Douglas Gregor69a9e012011-08-01 16:54:33 +00003007
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003008 if (Chain) {
3009 // Write the mapping information describing our module dependencies and how
3010 // each of those modules were mapped into our own offset/ID space, so that
3011 // the reader can build the appropriate mapping to its own offset/ID space.
3012 // The map consists solely of a blob with the following format:
3013 // *(module-name-len:i16 module-name:len*i8
3014 // source-location-offset:i32
3015 // identifier-id:i32
3016 // preprocessed-entity-id:i32
3017 // macro-definition-id:i32
3018 // selector-id:i32
3019 // declaration-id:i32
3020 // c++-base-specifiers-id:i32
3021 // type-id:i32)
3022 //
3023 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3024 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3026 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
3027 llvm::SmallString<2048> Buffer;
3028 {
3029 llvm::raw_svector_ostream Out(Buffer);
3030 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
3031 MEnd = Chain->ModuleMgr.end();
3032 M != MEnd; ++M) {
3033 StringRef FileName = (*M)->FileName;
3034 io::Emit16(Out, FileName.size());
3035 Out.write(FileName.data(), FileName.size());
3036 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3037 io::Emit32(Out, (*M)->BaseIdentifierID);
3038 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
3039 io::Emit32(Out, (*M)->BaseMacroDefinitionID);
3040 io::Emit32(Out, (*M)->BaseSelectorID);
3041 io::Emit32(Out, (*M)->BaseDeclID);
3042 io::Emit32(Out, (*M)->BaseTypeIndex);
3043 }
3044 }
3045 Record.clear();
3046 Record.push_back(MODULE_OFFSET_MAP);
3047 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3048 Buffer.data(), Buffer.size());
3049 }
3050
3051 // Create a lexical update block containing all of the declarations in the
3052 // translation unit that do not come from other AST files.
3053 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3054 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3055 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3056 E = TU->noload_decls_end();
3057 I != E; ++I) {
3058 if ((*I)->getPCHLevel() == 0)
3059 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
3060 else if ((*I)->isChangedSinceDeserialization())
3061 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
3062 }
3063
3064 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3065 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3066 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3067 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3068 Record.clear();
3069 Record.push_back(TU_UPDATE_LEXICAL);
3070 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3071 data(NewGlobalDecls));
3072
3073 // And a visible updates block for the translation unit.
3074 Abv = new llvm::BitCodeAbbrev();
3075 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3076 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3077 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3078 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3079 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3080 WriteDeclContextVisibleUpdate(TU);
3081
3082 // If the translation unit has an anonymous namespace, and we don't already
3083 // have an update block for it, write it as an update block.
3084 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3085 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3086 if (Record.empty()) {
3087 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
3088 AddDeclRef(NS, Record);
3089 }
3090 }
3091
Douglas Gregora119da02011-08-02 16:26:37 +00003092 // Form the record of special types.
3093 RecordData SpecialTypes;
3094 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor30403a62011-08-11 22:04:35 +00003095 AddTypeRef(Context.ObjCProtoType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003096 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003097 AddTypeRef(Context.getFILEType(), SpecialTypes);
3098 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3099 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3100 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3101 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003102 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003103
Douglas Gregor366809a2009-04-26 03:49:13 +00003104 // Keep writing types and declarations until all types and
3105 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003106 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003107 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003108 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3109 E = DeclsToRewrite.end();
3110 I != E; ++I)
3111 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003112 while (!DeclTypesToEmit.empty()) {
3113 DeclOrType DOT = DeclTypesToEmit.front();
3114 DeclTypesToEmit.pop();
3115 if (DOT.isType())
3116 WriteType(DOT.getType());
3117 else
3118 WriteDecl(Context, DOT.getDecl());
3119 }
3120 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003121
Douglas Gregor7143aab2011-09-01 17:04:32 +00003122 WritePreprocessor(PP, IsModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003123 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003124 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003125 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor7143aab2011-09-01 17:04:32 +00003126 WriteIdentifierTable(PP, IsModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003127 WriteFPPragmaOptions(SemaRef.getFPOptions());
3128 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003129
Sebastian Redl1476ed42010-07-16 16:36:56 +00003130 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003131 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003132
Anders Carlssonc8505782011-03-06 18:41:18 +00003133 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003134
Douglas Gregora119da02011-08-02 16:26:37 +00003135 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3136
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003137 /// Build a record containing first declarations from a chained PCH and the
3138 /// most recent declarations in this AST that they point to.
3139 RecordData FirstLatestDeclIDs;
3140 for (FirstLatestDeclMap::iterator I = FirstLatestDecls.begin(),
3141 E = FirstLatestDecls.end();
3142 I != E; ++I) {
3143 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3144 "Expected first & second to be in different PCHs");
3145 AddDeclRef(I->first, FirstLatestDeclIDs);
3146 AddDeclRef(I->second, FirstLatestDeclIDs);
3147 }
3148
3149 if (!FirstLatestDeclIDs.empty())
3150 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
3151
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003152 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003153 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003154 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003155
3156 // Write the record containing tentative definitions.
3157 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003158 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003159
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003160 // Write the record containing unused file scoped decls.
3161 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003162 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003163
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003164 // Write the record containing weak undeclared identifiers.
3165 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003166 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003167 WeakUndeclaredIdentifiers);
3168
Douglas Gregor14c22f22009-04-22 22:18:58 +00003169 // Write the record containing locally-scoped external definitions.
3170 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003171 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003172 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003173
3174 // Write the record containing ext_vector type names.
3175 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003176 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003177
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003178 // Write the record containing VTable uses information.
3179 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003180 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003181
3182 // Write the record containing dynamic classes declarations.
3183 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003184 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003185
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003186 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003187 if (!PendingInstantiations.empty())
3188 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003189
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003190 // Write the record containing declaration references of Sema.
3191 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003192 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003193
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003194 // Write the record containing CUDA-specific declaration references.
3195 if (!CUDASpecialDeclRefs.empty())
3196 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003197
3198 // Write the delegating constructors.
3199 if (!DelegatingCtorDecls.empty())
3200 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003201
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003202 // Write the known namespaces.
3203 if (!KnownNamespaces.empty())
3204 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3205
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003206 // Write the visible updates to DeclContexts.
3207 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3208 I = UpdatedDeclContexts.begin(),
3209 E = UpdatedDeclContexts.end();
3210 I != E; ++I)
3211 WriteDeclContextVisibleUpdate(*I);
3212
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003213 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003214 WriteDeclReplacementsBlock();
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003215 WriteChainedObjCCategories();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003216
Douglas Gregor3e1af842009-04-17 22:13:46 +00003217 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003218 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003219 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003220 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003221 Record.push_back(NumLexicalDeclContexts);
3222 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003223 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003224 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003225}
3226
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003227void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003228 if (DeclUpdates.empty())
3229 return;
3230
3231 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003232 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003233 for (DeclUpdateMap::iterator
3234 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3235 const Decl *D = I->first;
3236 UpdateRecord &URec = I->second;
3237
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003238 if (DeclsToRewrite.count(D))
3239 continue; // The decl will be written completely,no need to store updates.
3240
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003241 uint64_t Offset = Stream.GetCurrentBitNo();
3242 Stream.EmitRecord(DECL_UPDATES, URec);
3243
3244 OffsetsRecord.push_back(GetDeclRef(D));
3245 OffsetsRecord.push_back(Offset);
3246 }
3247 Stream.ExitBlock();
3248 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3249}
3250
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003251void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003252 if (ReplacedDecls.empty())
3253 return;
3254
3255 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003256 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003257 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3258 Record.push_back(I->first);
3259 Record.push_back(I->second);
3260 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003261 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003262}
3263
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003264void ASTWriter::WriteChainedObjCCategories() {
3265 if (LocalChainedObjCCategories.empty())
3266 return;
3267
3268 RecordData Record;
3269 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3270 I = LocalChainedObjCCategories.begin(),
3271 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3272 ChainedObjCCategoriesData &Data = *I;
3273 serialization::DeclID
3274 HeadCatID = getDeclID(Data.Interface->getCategoryList());
3275 assert(HeadCatID != 0 && "Category not written ?");
3276
3277 Record.push_back(Data.InterfaceID);
3278 Record.push_back(HeadCatID);
3279 Record.push_back(Data.TailCatID);
3280 }
3281 Stream.EmitRecord(OBJC_CHAINED_CATEGORIES, Record);
3282}
3283
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003284void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003285 Record.push_back(Loc.getRawEncoding());
3286}
3287
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003288void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003289 AddSourceLocation(Range.getBegin(), Record);
3290 AddSourceLocation(Range.getEnd(), Record);
3291}
3292
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003293void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003294 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003295 const uint64_t *Words = Value.getRawData();
3296 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003297}
3298
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003299void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003300 Record.push_back(Value.isUnsigned());
3301 AddAPInt(Value, Record);
3302}
3303
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003304void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003305 AddAPInt(Value.bitcastToAPInt(), Record);
3306}
3307
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003308void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003309 Record.push_back(getIdentifierRef(II));
3310}
3311
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003312IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003313 if (II == 0)
3314 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003315
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003316 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003317 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003318 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003319 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003320}
3321
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003322MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003323 if (MD == 0)
3324 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003325
3326 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003327 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003328 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003329 return ID;
3330}
3331
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003332void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003333 Record.push_back(getSelectorRef(SelRef));
3334}
3335
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003336SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003337 if (Sel.getAsOpaquePtr() == 0) {
3338 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003339 }
3340
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003341 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003342 if (SID == 0 && Chain) {
3343 // This might trigger a ReadSelector callback, which will set the ID for
3344 // this selector.
3345 Chain->LoadSelector(Sel);
3346 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003347 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003348 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003349 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003350 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003351}
3352
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003353void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003354 AddDeclRef(Temp->getDestructor(), Record);
3355}
3356
Douglas Gregor7c789c12010-10-29 22:39:52 +00003357void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3358 CXXBaseSpecifier const *BasesEnd,
3359 RecordDataImpl &Record) {
3360 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3361 CXXBaseSpecifiersToWrite.push_back(
3362 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3363 Bases, BasesEnd));
3364 Record.push_back(NextCXXBaseSpecifiersID++);
3365}
3366
Sebastian Redla4232eb2010-08-18 23:56:21 +00003367void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003368 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003369 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003370 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003371 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003372 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003373 break;
3374 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003375 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003376 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003377 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003378 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003379 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003380 break;
3381 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003382 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003383 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003384 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003385 break;
John McCall833ca992009-10-29 08:12:44 +00003386 case TemplateArgument::Null:
3387 case TemplateArgument::Integral:
3388 case TemplateArgument::Declaration:
3389 case TemplateArgument::Pack:
3390 break;
3391 }
3392}
3393
Sebastian Redla4232eb2010-08-18 23:56:21 +00003394void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003395 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003396 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003397
3398 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3399 bool InfoHasSameExpr
3400 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3401 Record.push_back(InfoHasSameExpr);
3402 if (InfoHasSameExpr)
3403 return; // Avoid storing the same expr twice.
3404 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003405 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3406 Record);
3407}
3408
Douglas Gregordc355712011-02-25 00:36:19 +00003409void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3410 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003411 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003412 AddTypeRef(QualType(), Record);
3413 return;
3414 }
3415
Douglas Gregordc355712011-02-25 00:36:19 +00003416 AddTypeLoc(TInfo->getTypeLoc(), Record);
3417}
3418
3419void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3420 AddTypeRef(TL.getType(), Record);
3421
John McCalla1ee0c52009-10-16 21:56:05 +00003422 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003423 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003424 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003425}
3426
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003427void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003428 Record.push_back(GetOrCreateTypeID(T));
3429}
3430
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003431TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3432 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003433 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3434}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003435
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003436TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003437 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003438 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003439}
3440
3441TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3442 if (T.isNull())
3443 return TypeIdx();
3444 assert(!T.getLocalFastQualifiers());
3445
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003446 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003447 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003448 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003449 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003450 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003451 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003452 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003453 return Idx;
3454}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003455
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003456TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003457 if (T.isNull())
3458 return TypeIdx();
3459 assert(!T.getLocalFastQualifiers());
3460
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003461 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3462 assert(I != TypeIdxs.end() && "Type not emitted!");
3463 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003464}
3465
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003466void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003467 Record.push_back(GetDeclRef(D));
3468}
3469
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003470DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003471 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003472 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003473 }
Douglas Gregor97475832010-10-05 18:37:06 +00003474 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003475 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003476 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003477 // We haven't seen this declaration before. Give it a new ID and
3478 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003479 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003480 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003481 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3482 // We don't add it to the replacement collection here, because we don't
3483 // have the offset yet.
3484 DeclTypesToEmit.push(const_cast<Decl *>(D));
3485 // Reset the flag, so that we don't add this decl multiple times.
3486 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003487 }
3488
Sebastian Redl681d7232010-07-27 00:17:23 +00003489 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003490}
3491
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003492DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003493 if (D == 0)
3494 return 0;
3495
3496 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3497 return DeclIDs[D];
3498}
3499
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003500void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003501 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003502 Record.push_back(Name.getNameKind());
3503 switch (Name.getNameKind()) {
3504 case DeclarationName::Identifier:
3505 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3506 break;
3507
3508 case DeclarationName::ObjCZeroArgSelector:
3509 case DeclarationName::ObjCOneArgSelector:
3510 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003511 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003512 break;
3513
3514 case DeclarationName::CXXConstructorName:
3515 case DeclarationName::CXXDestructorName:
3516 case DeclarationName::CXXConversionFunctionName:
3517 AddTypeRef(Name.getCXXNameType(), Record);
3518 break;
3519
3520 case DeclarationName::CXXOperatorName:
3521 Record.push_back(Name.getCXXOverloadedOperator());
3522 break;
3523
Sean Hunt3e518bd2009-11-29 07:34:05 +00003524 case DeclarationName::CXXLiteralOperatorName:
3525 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3526 break;
3527
Douglas Gregor2cf26342009-04-09 22:27:44 +00003528 case DeclarationName::CXXUsingDirective:
3529 // No extra data to emit
3530 break;
3531 }
3532}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003533
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003534void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003535 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003536 switch (Name.getNameKind()) {
3537 case DeclarationName::CXXConstructorName:
3538 case DeclarationName::CXXDestructorName:
3539 case DeclarationName::CXXConversionFunctionName:
3540 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3541 break;
3542
3543 case DeclarationName::CXXOperatorName:
3544 AddSourceLocation(
3545 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3546 Record);
3547 AddSourceLocation(
3548 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3549 Record);
3550 break;
3551
3552 case DeclarationName::CXXLiteralOperatorName:
3553 AddSourceLocation(
3554 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3555 Record);
3556 break;
3557
3558 case DeclarationName::Identifier:
3559 case DeclarationName::ObjCZeroArgSelector:
3560 case DeclarationName::ObjCOneArgSelector:
3561 case DeclarationName::ObjCMultiArgSelector:
3562 case DeclarationName::CXXUsingDirective:
3563 break;
3564 }
3565}
3566
3567void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003568 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003569 AddDeclarationName(NameInfo.getName(), Record);
3570 AddSourceLocation(NameInfo.getLoc(), Record);
3571 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3572}
3573
3574void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003575 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003576 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003577 Record.push_back(Info.NumTemplParamLists);
3578 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3579 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3580}
3581
Sebastian Redla4232eb2010-08-18 23:56:21 +00003582void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003583 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003584 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003585 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003586 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003587
3588 // Push each of the NNS's onto a stack for serialization in reverse order.
3589 while (NNS) {
3590 NestedNames.push_back(NNS);
3591 NNS = NNS->getPrefix();
3592 }
3593
3594 Record.push_back(NestedNames.size());
3595 while(!NestedNames.empty()) {
3596 NNS = NestedNames.pop_back_val();
3597 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3598 Record.push_back(Kind);
3599 switch (Kind) {
3600 case NestedNameSpecifier::Identifier:
3601 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3602 break;
3603
3604 case NestedNameSpecifier::Namespace:
3605 AddDeclRef(NNS->getAsNamespace(), Record);
3606 break;
3607
Douglas Gregor14aba762011-02-24 02:36:08 +00003608 case NestedNameSpecifier::NamespaceAlias:
3609 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3610 break;
3611
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003612 case NestedNameSpecifier::TypeSpec:
3613 case NestedNameSpecifier::TypeSpecWithTemplate:
3614 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3615 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3616 break;
3617
3618 case NestedNameSpecifier::Global:
3619 // Don't need to write an associated value.
3620 break;
3621 }
3622 }
3623}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003624
Douglas Gregordc355712011-02-25 00:36:19 +00003625void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3626 RecordDataImpl &Record) {
3627 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003628 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003629 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00003630
3631 // Push each of the nested-name-specifiers's onto a stack for
3632 // serialization in reverse order.
3633 while (NNS) {
3634 NestedNames.push_back(NNS);
3635 NNS = NNS.getPrefix();
3636 }
3637
3638 Record.push_back(NestedNames.size());
3639 while(!NestedNames.empty()) {
3640 NNS = NestedNames.pop_back_val();
3641 NestedNameSpecifier::SpecifierKind Kind
3642 = NNS.getNestedNameSpecifier()->getKind();
3643 Record.push_back(Kind);
3644 switch (Kind) {
3645 case NestedNameSpecifier::Identifier:
3646 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3647 AddSourceRange(NNS.getLocalSourceRange(), Record);
3648 break;
3649
3650 case NestedNameSpecifier::Namespace:
3651 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3652 AddSourceRange(NNS.getLocalSourceRange(), Record);
3653 break;
3654
3655 case NestedNameSpecifier::NamespaceAlias:
3656 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3657 AddSourceRange(NNS.getLocalSourceRange(), Record);
3658 break;
3659
3660 case NestedNameSpecifier::TypeSpec:
3661 case NestedNameSpecifier::TypeSpecWithTemplate:
3662 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3663 AddTypeLoc(NNS.getTypeLoc(), Record);
3664 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3665 break;
3666
3667 case NestedNameSpecifier::Global:
3668 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3669 break;
3670 }
3671 }
3672}
3673
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003674void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003675 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003676 Record.push_back(Kind);
3677 switch (Kind) {
3678 case TemplateName::Template:
3679 AddDeclRef(Name.getAsTemplateDecl(), Record);
3680 break;
3681
3682 case TemplateName::OverloadedTemplate: {
3683 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3684 Record.push_back(OvT->size());
3685 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3686 I != E; ++I)
3687 AddDeclRef(*I, Record);
3688 break;
3689 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003690
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003691 case TemplateName::QualifiedTemplate: {
3692 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3693 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3694 Record.push_back(QualT->hasTemplateKeyword());
3695 AddDeclRef(QualT->getTemplateDecl(), Record);
3696 break;
3697 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003698
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003699 case TemplateName::DependentTemplate: {
3700 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3701 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3702 Record.push_back(DepT->isIdentifier());
3703 if (DepT->isIdentifier())
3704 AddIdentifierRef(DepT->getIdentifier(), Record);
3705 else
3706 Record.push_back(DepT->getOperator());
3707 break;
3708 }
John McCall14606042011-06-30 08:33:18 +00003709
3710 case TemplateName::SubstTemplateTemplateParm: {
3711 SubstTemplateTemplateParmStorage *subst
3712 = Name.getAsSubstTemplateTemplateParm();
3713 AddDeclRef(subst->getParameter(), Record);
3714 AddTemplateName(subst->getReplacement(), Record);
3715 break;
3716 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003717
3718 case TemplateName::SubstTemplateTemplateParmPack: {
3719 SubstTemplateTemplateParmPackStorage *SubstPack
3720 = Name.getAsSubstTemplateTemplateParmPack();
3721 AddDeclRef(SubstPack->getParameterPack(), Record);
3722 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3723 break;
3724 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003725 }
3726}
3727
Michael J. Spencer20249a12010-10-21 03:16:25 +00003728void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003729 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003730 Record.push_back(Arg.getKind());
3731 switch (Arg.getKind()) {
3732 case TemplateArgument::Null:
3733 break;
3734 case TemplateArgument::Type:
3735 AddTypeRef(Arg.getAsType(), Record);
3736 break;
3737 case TemplateArgument::Declaration:
3738 AddDeclRef(Arg.getAsDecl(), Record);
3739 break;
3740 case TemplateArgument::Integral:
3741 AddAPSInt(*Arg.getAsIntegral(), Record);
3742 AddTypeRef(Arg.getIntegralType(), Record);
3743 break;
3744 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003745 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3746 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003747 case TemplateArgument::TemplateExpansion:
3748 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003749 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3750 Record.push_back(*NumExpansions + 1);
3751 else
3752 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003753 break;
3754 case TemplateArgument::Expression:
3755 AddStmt(Arg.getAsExpr());
3756 break;
3757 case TemplateArgument::Pack:
3758 Record.push_back(Arg.pack_size());
3759 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3760 I != E; ++I)
3761 AddTemplateArgument(*I, Record);
3762 break;
3763 }
3764}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003765
3766void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003767ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003768 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003769 assert(TemplateParams && "No TemplateParams!");
3770 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3771 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3772 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3773 Record.push_back(TemplateParams->size());
3774 for (TemplateParameterList::const_iterator
3775 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3776 P != PEnd; ++P)
3777 AddDeclRef(*P, Record);
3778}
3779
3780/// \brief Emit a template argument list.
3781void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003782ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003783 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003784 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003785 Record.push_back(TemplateArgs->size());
3786 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003787 AddTemplateArgument(TemplateArgs->get(i), Record);
3788}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003789
3790
3791void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003792ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003793 Record.push_back(Set.size());
3794 for (UnresolvedSetImpl::const_iterator
3795 I = Set.begin(), E = Set.end(); I != E; ++I) {
3796 AddDeclRef(I.getDecl(), Record);
3797 Record.push_back(I.getAccess());
3798 }
3799}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003800
Sebastian Redla4232eb2010-08-18 23:56:21 +00003801void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003802 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003803 Record.push_back(Base.isVirtual());
3804 Record.push_back(Base.isBaseOfClass());
3805 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003806 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003807 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003808 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003809 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3810 : SourceLocation(),
3811 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003812}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003813
Douglas Gregor7c789c12010-10-29 22:39:52 +00003814void ASTWriter::FlushCXXBaseSpecifiers() {
3815 RecordData Record;
3816 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3817 Record.clear();
3818
3819 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00003820 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003821 if (Index == CXXBaseSpecifiersOffsets.size())
3822 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3823 else {
3824 if (Index > CXXBaseSpecifiersOffsets.size())
3825 CXXBaseSpecifiersOffsets.resize(Index + 1);
3826 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3827 }
3828
3829 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3830 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3831 Record.push_back(BEnd - B);
3832 for (; B != BEnd; ++B)
3833 AddCXXBaseSpecifier(*B, Record);
3834 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003835
3836 // Flush any expressions that were written as part of the base specifiers.
3837 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003838 }
3839
3840 CXXBaseSpecifiersToWrite.clear();
3841}
3842
Sean Huntcbb67482011-01-08 20:30:50 +00003843void ASTWriter::AddCXXCtorInitializers(
3844 const CXXCtorInitializer * const *CtorInitializers,
3845 unsigned NumCtorInitializers,
3846 RecordDataImpl &Record) {
3847 Record.push_back(NumCtorInitializers);
3848 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3849 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003850
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003851 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00003852 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003853 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3854 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00003855 } else if (Init->isDelegatingInitializer()) {
3856 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3857 AddDeclRef(Init->getTargetConstructor(), Record);
3858 } else if (Init->isMemberInitializer()){
3859 Record.push_back(CTOR_INITIALIZER_MEMBER);
3860 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003861 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00003862 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3863 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003864 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003865
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003866 AddSourceLocation(Init->getMemberLocation(), Record);
3867 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003868 AddSourceLocation(Init->getLParenLoc(), Record);
3869 AddSourceLocation(Init->getRParenLoc(), Record);
3870 Record.push_back(Init->isWritten());
3871 if (Init->isWritten()) {
3872 Record.push_back(Init->getSourceOrder());
3873 } else {
3874 Record.push_back(Init->getNumArrayIndices());
3875 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3876 AddDeclRef(Init->getArrayIndex(i), Record);
3877 }
3878 }
3879}
3880
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003881void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3882 assert(D->DefinitionData);
3883 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3884 Record.push_back(Data.UserDeclaredConstructor);
3885 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00003886 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003887 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00003888 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003889 Record.push_back(Data.UserDeclaredDestructor);
3890 Record.push_back(Data.Aggregate);
3891 Record.push_back(Data.PlainOldData);
3892 Record.push_back(Data.Empty);
3893 Record.push_back(Data.Polymorphic);
3894 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00003895 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00003896 Record.push_back(Data.HasNoNonEmptyBases);
3897 Record.push_back(Data.HasPrivateFields);
3898 Record.push_back(Data.HasProtectedFields);
3899 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00003900 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00003901 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00003902 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003903 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003904 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003905 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003906 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003907 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003908 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003909 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00003910 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003911 Record.push_back(Data.DeclaredDefaultConstructor);
3912 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00003913 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003914 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00003915 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003916 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00003917 Record.push_back(Data.FailedImplicitMoveConstructor);
3918 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003919
3920 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003921 if (Data.NumBases > 0)
3922 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3923 Record);
3924
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003925 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3926 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003927 if (Data.NumVBases > 0)
3928 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3929 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003930
3931 AddUnresolvedSet(Data.Conversions, Record);
3932 AddUnresolvedSet(Data.VisibleConversions, Record);
3933 // Data.Definition is the owning decl, no need to write it.
3934 AddDeclRef(Data.FirstFriend, Record);
3935}
3936
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003937void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003938 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00003939 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003940 assert(FirstDeclID == NextDeclID &&
3941 FirstTypeID == NextTypeID &&
3942 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003943 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003944 FirstMacroID == NextMacroID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003945 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003946
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003947 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003948
Douglas Gregor10bc00f2011-08-18 04:12:04 +00003949 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
3950 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
3951 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
3952 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
3953 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacroDefinitions();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003954 NextDeclID = FirstDeclID;
3955 NextTypeID = FirstTypeID;
3956 NextIdentID = FirstIdentID;
3957 NextSelectorID = FirstSelectorID;
3958 NextMacroID = FirstMacroID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003959}
3960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003961void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003962 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003963 if (II->hasMacroDefinition())
3964 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003965}
3966
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003967void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003968 // Always take the highest-numbered type index. This copes with an interesting
3969 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003970 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003971 // keep the higher-numbered entry so that we can properly write it out to
3972 // the AST file.
3973 TypeIdx &StoredIdx = TypeIdxs[T];
3974 if (Idx.getIndex() >= StoredIdx.getIndex())
3975 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003976}
3977
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003978void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003979 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003980}
Sebastian Redl5d050072010-08-04 17:20:04 +00003981
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003982void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003983 SelectorIDs[S] = ID;
3984}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003985
Michael J. Spencer20249a12010-10-21 03:16:25 +00003986void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003987 MacroDefinition *MD) {
3988 MacroDefinitions[MD] = ID;
3989}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003990
3991void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3992 assert(D->isDefinition());
3993 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3994 // We are interested when a PCH decl is modified.
3995 if (RD->getPCHLevel() > 0) {
3996 // A forward reference was mutated into a definition. Rewrite it.
3997 // FIXME: This happens during template instantiation, should we
3998 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003999 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004000 }
4001
4002 for (CXXRecordDecl::redecl_iterator
4003 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
4004 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
4005 if (Redecl == RD)
4006 continue;
4007
4008 // We are interested when a PCH decl is modified.
4009 if (Redecl->getPCHLevel() > 0) {
4010 UpdateRecord &Record = DeclUpdates[Redecl];
4011 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
4012 assert(Redecl->DefinitionData);
4013 assert(Redecl->DefinitionData->Definition == D);
4014 AddDeclRef(D, Record); // the DefinitionDecl
4015 }
4016 }
4017 }
4018}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004019void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
4020 // TU and namespaces are handled elsewhere.
4021 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4022 return;
4023
4024 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
4025 return; // Not a source decl added to a DeclContext from PCH.
4026
4027 AddUpdatedDeclContext(DC);
4028}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004029
4030void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
4031 assert(D->isImplicit());
4032 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
4033 return; // Not a source member added to a class from PCH.
4034 if (!isa<CXXMethodDecl>(D))
4035 return; // We are interested in lazily declared implicit methods.
4036
4037 // A decl coming from PCH was modified.
4038 assert(RD->isDefinition());
4039 UpdateRecord &Record = DeclUpdates[RD];
4040 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
4041 AddDeclRef(D, Record);
4042}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004043
4044void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4045 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004046 // The specializations set is kept in the canonical template.
4047 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004048 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4049 return; // Not a source specialization added to a template from PCH.
4050
4051 UpdateRecord &Record = DeclUpdates[TD];
4052 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4053 AddDeclRef(D, Record);
4054}
Douglas Gregor89d99802010-11-30 06:16:57 +00004055
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004056void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4057 const FunctionDecl *D) {
4058 // The specializations set is kept in the canonical template.
4059 TD = TD->getCanonicalDecl();
4060 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4061 return; // Not a source specialization added to a template from PCH.
4062
4063 UpdateRecord &Record = DeclUpdates[TD];
4064 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4065 AddDeclRef(D, Record);
4066}
4067
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004068void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4069 if (D->getPCHLevel() == 0)
4070 return; // Declaration not imported from PCH.
4071
4072 // Implicit decl from a PCH was defined.
4073 // FIXME: Should implicit definition be a separate FunctionDecl?
4074 RewriteDecl(D);
4075}
4076
Sebastian Redlf79a7192011-04-29 08:19:30 +00004077void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4078 if (D->getPCHLevel() == 0)
4079 return;
4080
4081 // Since the actual instantiation is delayed, this really means that we need
4082 // to update the instantiation location.
4083 UpdateRecord &Record = DeclUpdates[D];
4084 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4085 AddSourceLocation(
4086 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4087}
4088
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004089void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4090 const ObjCInterfaceDecl *IFD) {
4091 if (IFD->getPCHLevel() == 0)
4092 return; // Declaration not imported from PCH.
4093 if (CatD->getNextClassCategory() &&
4094 CatD->getNextClassCategory()->getPCHLevel() == 0)
4095 return; // We already recorded that the tail of a category chain should be
4096 // attached to an interface.
4097
4098 ChainedObjCCategoriesData Data = { IFD, GetDeclRef(IFD), GetDeclRef(CatD) };
4099 LocalChainedObjCCategories.push_back(Data);
4100}
4101
Douglas Gregor89d99802010-11-30 06:16:57 +00004102ASTSerializationListener::~ASTSerializationListener() { }