blob: b8a0c28938f7ed4179f115841e03f2d5e0cdf7b4 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Sebastian Redlade50002010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000059}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
66
Douglas Gregor2cf26342009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000070
Douglas Gregor2cf26342009-04-09 22:27:44 +000071namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl3397c552010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000116}
117
Sebastian Redl3397c552010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159}
160
Sebastian Redl3397c552010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175}
176
Sebastian Redl3397c552010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000188 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000189 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());
Richard Smith7bb698a2012-04-21 17:47:47 +0000198 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
199 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
200 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000201 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000203 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000204 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000205}
206
Sebastian Redl3397c552010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000209 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000210}
John McCalled976492009-12-04 22:46:56 +0000211
Sebastian Redl3397c552010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000213 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000214 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
215 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000216 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217}
218
Sebastian Redl3397c552010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000220 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222}
223
Sebastian Redl3397c552010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227}
228
Sebastian Redl3397c552010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000231 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000232 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000233}
234
Sean Huntca63c202011-05-24 22:41:36 +0000235void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
236 Writer.AddTypeRef(T->getBaseType(), Record);
237 Writer.AddTypeRef(T->getUnderlyingType(), Record);
238 Record.push_back(T->getUTTKind());
239 Code = TYPE_UNARY_TRANSFORM;
240}
241
Richard Smith34b41d92011-02-20 03:19:35 +0000242void ASTTypeWriter::VisitAutoType(const AutoType *T) {
243 Writer.AddTypeRef(T->getDeducedType(), Record);
244 Code = TYPE_AUTO;
245}
246
Sebastian Redl3397c552010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000248 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000249 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000250 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251 "Cannot serialize in the middle of a type definition");
252}
253
Sebastian Redl3397c552010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000256 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257}
258
Sebastian Redl3397c552010-08-18 23:56:27 +0000259void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000261 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262}
263
John McCall9d156a72011-01-06 01:58:22 +0000264void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
265 Writer.AddTypeRef(T->getModifiedType(), Record);
266 Writer.AddTypeRef(T->getEquivalentType(), Record);
267 Record.push_back(T->getAttrKind());
268 Code = TYPE_ATTRIBUTED;
269}
270
Mike Stump1eb44332009-09-09 15:08:12 +0000271void
Sebastian Redl3397c552010-08-18 23:56:27 +0000272ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000273 const SubstTemplateTypeParmType *T) {
274 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
275 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000276 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000277}
278
279void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000280ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
281 const SubstTemplateTypeParmPackType *T) {
282 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
283 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
284 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
285}
286
287void
Sebastian Redl3397c552010-08-18 23:56:27 +0000288ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000289 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000290 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000291 Writer.AddTemplateName(T->getTemplateName(), Record);
292 Record.push_back(T->getNumArgs());
293 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
294 ArgI != ArgE; ++ArgI)
295 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000296 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
297 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000298 : T->getCanonicalTypeInternal(),
299 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000300 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000301}
302
303void
Sebastian Redl3397c552010-08-18 23:56:27 +0000304ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000305 VisitArrayType(T);
306 Writer.AddStmt(T->getSizeExpr());
307 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000308 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl3397c552010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313 const DependentSizedExtVectorType *T) {
314 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000315 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000316}
317
318void
Sebastian Redl3397c552010-08-18 23:56:27 +0000319ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320 Record.push_back(T->getDepth());
321 Record.push_back(T->getIndex());
322 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000323 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000324 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000325}
326
327void
Sebastian Redl3397c552010-08-18 23:56:27 +0000328ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000329 Record.push_back(T->getKeyword());
330 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
331 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000332 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
333 : T->getCanonicalTypeInternal(),
334 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000335 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000336}
337
338void
Sebastian Redl3397c552010-08-18 23:56:27 +0000339ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000340 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000341 Record.push_back(T->getKeyword());
342 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
343 Writer.AddIdentifierRef(T->getIdentifier(), Record);
344 Record.push_back(T->getNumArgs());
345 for (DependentTemplateSpecializationType::iterator
346 I = T->begin(), E = T->end(); I != E; ++I)
347 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000348 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000349}
350
Douglas Gregor7536dd52010-12-20 02:24:11 +0000351void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
352 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000353 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
354 Record.push_back(*NumExpansions + 1);
355 else
356 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000357 Code = TYPE_PACK_EXPANSION;
358}
359
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000360void ASTTypeWriter::VisitParenType(const ParenType *T) {
361 Writer.AddTypeRef(T->getInnerType(), Record);
362 Code = TYPE_PAREN;
363}
364
Sebastian Redl3397c552010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000366 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000367 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
368 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000369 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000370}
371
Sebastian Redl3397c552010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000373 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000374 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000375 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000376}
377
Sebastian Redl3397c552010-08-18 23:56:27 +0000378void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000379 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000380 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000381}
382
Sebastian Redl3397c552010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000384 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000385 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000386 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000387 E = T->qual_end(); I != E; ++I)
388 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000390}
391
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000392void
Sebastian Redl3397c552010-08-18 23:56:27 +0000393ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000394 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000395 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000396}
397
Eli Friedmanb001de72011-10-06 23:00:33 +0000398void
399ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
400 Writer.AddTypeRef(T->getValueType(), Record);
401 Code = TYPE_ATOMIC;
402}
403
John McCalla1ee0c52009-10-16 21:56:05 +0000404namespace {
405
406class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000407 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000408 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000409
410public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000411 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000412 : Writer(Writer), Record(Record) { }
413
John McCall51bd8032009-10-18 01:05:36 +0000414#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000415#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000416 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000417#include "clang/AST/TypeLocNodes.def"
418
John McCall51bd8032009-10-18 01:05:36 +0000419 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
420 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000421};
422
423}
424
John McCall51bd8032009-10-18 01:05:36 +0000425void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
426 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000429 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
430 if (TL.needsExtraLocalData()) {
431 Record.push_back(TL.getWrittenTypeSpec());
432 Record.push_back(TL.getWrittenSignSpec());
433 Record.push_back(TL.getWrittenWidthSpec());
434 Record.push_back(TL.hasModeAttr());
435 }
John McCalla1ee0c52009-10-16 21:56:05 +0000436}
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000442}
John McCall51bd8032009-10-18 01:05:36 +0000443void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000445}
John McCall51bd8032009-10-18 01:05:36 +0000446void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000451}
John McCall51bd8032009-10-18 01:05:36 +0000452void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000454 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
458 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
459 Record.push_back(TL.getSizeExpr() ? 1 : 0);
460 if (TL.getSizeExpr())
461 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000462}
John McCall51bd8032009-10-18 01:05:36 +0000463void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
464 VisitArrayTypeLoc(TL);
465}
466void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
473 DependentSizedArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
477 DependentSizedExtVectorTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
480void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000487 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000488 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
489 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000490 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000491 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
492 Writer.AddDeclRef(TL.getArg(i), Record);
493}
494void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
495 VisitFunctionTypeLoc(TL);
496}
497void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
498 VisitFunctionTypeLoc(TL);
499}
John McCalled976492009-12-04 22:46:56 +0000500void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502}
John McCall51bd8032009-10-18 01:05:36 +0000503void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
506void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000507 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
508 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
509 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000510}
511void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000512 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
513 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
514 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
515 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000516}
517void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
518 Writer.AddSourceLocation(TL.getNameLoc(), Record);
519}
Sean Huntca63c202011-05-24 22:41:36 +0000520void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getKWLoc(), Record);
522 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
523 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
524 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
525}
Richard Smith34b41d92011-02-20 03:19:35 +0000526void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
John McCall51bd8032009-10-18 01:05:36 +0000529void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
532void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
John McCall9d156a72011-01-06 01:58:22 +0000535void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
537 if (TL.hasAttrOperand()) {
538 SourceRange range = TL.getAttrOperandParensRange();
539 Writer.AddSourceLocation(range.getBegin(), Record);
540 Writer.AddSourceLocation(range.getEnd(), Record);
541 }
542 if (TL.hasAttrExprOperand()) {
543 Expr *operand = TL.getAttrExprOperand();
544 Record.push_back(operand ? 1 : 0);
545 if (operand) Writer.AddStmt(operand);
546 } else if (TL.hasAttrEnumOperand()) {
547 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
548 }
549}
John McCall51bd8032009-10-18 01:05:36 +0000550void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall49a832b2009-10-18 09:09:24 +0000553void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
554 SubstTemplateTypeParmTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000557void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
558 SubstTemplateTypeParmPackTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getNameLoc(), Record);
560}
John McCall51bd8032009-10-18 01:05:36 +0000561void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
562 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000563 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000564 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
565 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
566 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
567 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000568 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
569 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000570}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000571void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
572 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
573 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
574}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000575void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000576 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000577 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000578}
John McCall3cb0ebd2010-03-10 03:28:59 +0000579void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getNameLoc(), Record);
581}
Douglas Gregor4714c122010-03-31 17:34:00 +0000582void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000583 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000584 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000585 Writer.AddSourceLocation(TL.getNameLoc(), Record);
586}
John McCall33500952010-06-11 00:33:02 +0000587void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
588 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000589 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000590 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000591 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000592 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000593 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
594 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
595 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000596 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
597 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000598}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000599void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
600 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
601}
John McCall51bd8032009-10-18 01:05:36 +0000602void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000604}
605void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
606 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000607 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
608 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
609 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
610 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000611}
John McCall54e14c42009-10-22 22:37:11 +0000612void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
613 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000614}
Eli Friedmanb001de72011-10-06 23:00:33 +0000615void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getKWLoc(), Record);
617 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
618 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
619}
John McCalla1ee0c52009-10-16 21:56:05 +0000620
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000621//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000622// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000623//===----------------------------------------------------------------------===//
624
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000625static void EmitBlockID(unsigned ID, const char *Name,
626 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000627 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000628 Record.clear();
629 Record.push_back(ID);
630 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
631
632 // Emit the block name if present.
633 if (Name == 0 || Name[0] == 0) return;
634 Record.clear();
635 while (*Name)
636 Record.push_back(*Name++);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
638}
639
640static void EmitRecordID(unsigned ID, const char *Name,
641 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000642 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000643 Record.clear();
644 Record.push_back(ID);
645 while (*Name)
646 Record.push_back(*Name++);
647 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000648}
649
650static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000651 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000652#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000653 RECORD(STMT_STOP);
654 RECORD(STMT_NULL_PTR);
655 RECORD(STMT_NULL);
656 RECORD(STMT_COMPOUND);
657 RECORD(STMT_CASE);
658 RECORD(STMT_DEFAULT);
659 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000660 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000661 RECORD(STMT_IF);
662 RECORD(STMT_SWITCH);
663 RECORD(STMT_WHILE);
664 RECORD(STMT_DO);
665 RECORD(STMT_FOR);
666 RECORD(STMT_GOTO);
667 RECORD(STMT_INDIRECT_GOTO);
668 RECORD(STMT_CONTINUE);
669 RECORD(STMT_BREAK);
670 RECORD(STMT_RETURN);
671 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000672 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000673 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000674 RECORD(EXPR_PREDEFINED);
675 RECORD(EXPR_DECL_REF);
676 RECORD(EXPR_INTEGER_LITERAL);
677 RECORD(EXPR_FLOATING_LITERAL);
678 RECORD(EXPR_IMAGINARY_LITERAL);
679 RECORD(EXPR_STRING_LITERAL);
680 RECORD(EXPR_CHARACTER_LITERAL);
681 RECORD(EXPR_PAREN);
682 RECORD(EXPR_UNARY_OPERATOR);
683 RECORD(EXPR_SIZEOF_ALIGN_OF);
684 RECORD(EXPR_ARRAY_SUBSCRIPT);
685 RECORD(EXPR_CALL);
686 RECORD(EXPR_MEMBER);
687 RECORD(EXPR_BINARY_OPERATOR);
688 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
689 RECORD(EXPR_CONDITIONAL_OPERATOR);
690 RECORD(EXPR_IMPLICIT_CAST);
691 RECORD(EXPR_CSTYLE_CAST);
692 RECORD(EXPR_COMPOUND_LITERAL);
693 RECORD(EXPR_EXT_VECTOR_ELEMENT);
694 RECORD(EXPR_INIT_LIST);
695 RECORD(EXPR_DESIGNATED_INIT);
696 RECORD(EXPR_IMPLICIT_VALUE_INIT);
697 RECORD(EXPR_VA_ARG);
698 RECORD(EXPR_ADDR_LABEL);
699 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000700 RECORD(EXPR_CHOOSE);
701 RECORD(EXPR_GNU_NULL);
702 RECORD(EXPR_SHUFFLE_VECTOR);
703 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000704 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000705 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000706 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000707 RECORD(EXPR_OBJC_ARRAY_LITERAL);
708 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000709 RECORD(EXPR_OBJC_ENCODE);
710 RECORD(EXPR_OBJC_SELECTOR_EXPR);
711 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
712 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
713 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
714 RECORD(EXPR_OBJC_KVC_REF_EXPR);
715 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000716 RECORD(STMT_OBJC_FOR_COLLECTION);
717 RECORD(STMT_OBJC_CATCH);
718 RECORD(STMT_OBJC_FINALLY);
719 RECORD(STMT_OBJC_AT_TRY);
720 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
721 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000722 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000723 RECORD(EXPR_CXX_OPERATOR_CALL);
724 RECORD(EXPR_CXX_CONSTRUCT);
725 RECORD(EXPR_CXX_STATIC_CAST);
726 RECORD(EXPR_CXX_DYNAMIC_CAST);
727 RECORD(EXPR_CXX_REINTERPRET_CAST);
728 RECORD(EXPR_CXX_CONST_CAST);
729 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000730 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000731 RECORD(EXPR_CXX_BOOL_LITERAL);
732 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000733 RECORD(EXPR_CXX_TYPEID_EXPR);
734 RECORD(EXPR_CXX_TYPEID_TYPE);
735 RECORD(EXPR_CXX_UUIDOF_EXPR);
736 RECORD(EXPR_CXX_UUIDOF_TYPE);
737 RECORD(EXPR_CXX_THIS);
738 RECORD(EXPR_CXX_THROW);
739 RECORD(EXPR_CXX_DEFAULT_ARG);
740 RECORD(EXPR_CXX_BIND_TEMPORARY);
741 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
742 RECORD(EXPR_CXX_NEW);
743 RECORD(EXPR_CXX_DELETE);
744 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
745 RECORD(EXPR_EXPR_WITH_CLEANUPS);
746 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
747 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
748 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
749 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
750 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
751 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
752 RECORD(EXPR_CXX_NOEXCEPT);
753 RECORD(EXPR_OPAQUE_VALUE);
754 RECORD(EXPR_BINARY_TYPE_TRAIT);
755 RECORD(EXPR_PACK_EXPANSION);
756 RECORD(EXPR_SIZEOF_PACK);
757 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000758 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000759#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000760}
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Sebastian Redla4232eb2010-08-18 23:56:21 +0000762void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000763 RecordData Record;
764 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000766#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
767#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Sebastian Redl3397c552010-08-18 23:56:27 +0000769 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000770 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000771 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000772 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000773 RECORD(TYPE_OFFSET);
774 RECORD(DECL_OFFSET);
775 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000776 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000777 RECORD(IDENTIFIER_OFFSET);
778 RECORD(IDENTIFIER_TABLE);
779 RECORD(EXTERNAL_DEFINITIONS);
780 RECORD(SPECIAL_TYPES);
781 RECORD(STATISTICS);
782 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000783 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000784 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
785 RECORD(SELECTOR_OFFSETS);
786 RECORD(METHOD_POOL);
787 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000788 RECORD(SOURCE_LOCATION_OFFSETS);
789 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000790 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000791 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000792 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000793 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000794 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000795 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000796 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000797 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000798 RECORD(SEMA_DECL_REFS);
799 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
800 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
801 RECORD(DECL_REPLACEMENTS);
802 RECORD(UPDATE_VISIBLE);
803 RECORD(DECL_UPDATE_OFFSETS);
804 RECORD(DECL_UPDATES);
805 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
806 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000807 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000808 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000809 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000810 RECORD(FP_PRAGMA_OPTIONS);
811 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000812 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000813 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
814 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000815 RECORD(MODULE_OFFSET_MAP);
816 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000817 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000818 RECORD(FILE_SORTED_DECLS);
819 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000820 RECORD(MERGED_DECLARATIONS);
821 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000822 RECORD(OBJC_CATEGORIES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000823
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000824 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000825 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000826 RECORD(SM_SLOC_FILE_ENTRY);
827 RECORD(SM_SLOC_BUFFER_ENTRY);
828 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000829 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000831 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000832 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000833 RECORD(PP_MACRO_OBJECT_LIKE);
834 RECORD(PP_MACRO_FUNCTION_LIKE);
835 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000836
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000837 // Decls and Types block.
838 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000839 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 RECORD(TYPE_COMPLEX);
841 RECORD(TYPE_POINTER);
842 RECORD(TYPE_BLOCK_POINTER);
843 RECORD(TYPE_LVALUE_REFERENCE);
844 RECORD(TYPE_RVALUE_REFERENCE);
845 RECORD(TYPE_MEMBER_POINTER);
846 RECORD(TYPE_CONSTANT_ARRAY);
847 RECORD(TYPE_INCOMPLETE_ARRAY);
848 RECORD(TYPE_VARIABLE_ARRAY);
849 RECORD(TYPE_VECTOR);
850 RECORD(TYPE_EXT_VECTOR);
851 RECORD(TYPE_FUNCTION_PROTO);
852 RECORD(TYPE_FUNCTION_NO_PROTO);
853 RECORD(TYPE_TYPEDEF);
854 RECORD(TYPE_TYPEOF_EXPR);
855 RECORD(TYPE_TYPEOF);
856 RECORD(TYPE_RECORD);
857 RECORD(TYPE_ENUM);
858 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000859 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000860 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000861 RECORD(TYPE_DECLTYPE);
862 RECORD(TYPE_ELABORATED);
863 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
864 RECORD(TYPE_UNRESOLVED_USING);
865 RECORD(TYPE_INJECTED_CLASS_NAME);
866 RECORD(TYPE_OBJC_OBJECT);
867 RECORD(TYPE_TEMPLATE_TYPE_PARM);
868 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
869 RECORD(TYPE_DEPENDENT_NAME);
870 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
871 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
872 RECORD(TYPE_PAREN);
873 RECORD(TYPE_PACK_EXPANSION);
874 RECORD(TYPE_ATTRIBUTED);
875 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000876 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000877 RECORD(DECL_TYPEDEF);
878 RECORD(DECL_ENUM);
879 RECORD(DECL_RECORD);
880 RECORD(DECL_ENUM_CONSTANT);
881 RECORD(DECL_FUNCTION);
882 RECORD(DECL_OBJC_METHOD);
883 RECORD(DECL_OBJC_INTERFACE);
884 RECORD(DECL_OBJC_PROTOCOL);
885 RECORD(DECL_OBJC_IVAR);
886 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000887 RECORD(DECL_OBJC_CATEGORY);
888 RECORD(DECL_OBJC_CATEGORY_IMPL);
889 RECORD(DECL_OBJC_IMPLEMENTATION);
890 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
891 RECORD(DECL_OBJC_PROPERTY);
892 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000893 RECORD(DECL_FIELD);
894 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000895 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000896 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000897 RECORD(DECL_FILE_SCOPE_ASM);
898 RECORD(DECL_BLOCK);
899 RECORD(DECL_CONTEXT_LEXICAL);
900 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000901 RECORD(DECL_NAMESPACE);
902 RECORD(DECL_NAMESPACE_ALIAS);
903 RECORD(DECL_USING);
904 RECORD(DECL_USING_SHADOW);
905 RECORD(DECL_USING_DIRECTIVE);
906 RECORD(DECL_UNRESOLVED_USING_VALUE);
907 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
908 RECORD(DECL_LINKAGE_SPEC);
909 RECORD(DECL_CXX_RECORD);
910 RECORD(DECL_CXX_METHOD);
911 RECORD(DECL_CXX_CONSTRUCTOR);
912 RECORD(DECL_CXX_DESTRUCTOR);
913 RECORD(DECL_CXX_CONVERSION);
914 RECORD(DECL_ACCESS_SPEC);
915 RECORD(DECL_FRIEND);
916 RECORD(DECL_FRIEND_TEMPLATE);
917 RECORD(DECL_CLASS_TEMPLATE);
918 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
919 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
920 RECORD(DECL_FUNCTION_TEMPLATE);
921 RECORD(DECL_TEMPLATE_TYPE_PARM);
922 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
923 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
924 RECORD(DECL_STATIC_ASSERT);
925 RECORD(DECL_CXX_BASE_SPECIFIERS);
926 RECORD(DECL_INDIRECTFIELD);
927 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
928
Douglas Gregora72d8c42011-06-03 02:27:19 +0000929 // Statements and Exprs can occur in the Decls and Types block.
930 AddStmtsExprs(Stream, Record);
931
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000932 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000933 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000934 RECORD(PPD_MACRO_DEFINITION);
935 RECORD(PPD_INCLUSION_DIRECTIVE);
936
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000937#undef RECORD
938#undef BLOCK
939 Stream.ExitBlock();
940}
941
Douglas Gregore650c8c2009-07-07 00:12:59 +0000942/// \brief Adjusts the given filename to only write out the portion of the
943/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000944///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945/// \param Filename the file name to adjust.
946///
947/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
948/// the returned filename will be adjusted by this system root.
949///
950/// \returns either the original filename (if it needs no adjustment) or the
951/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000952static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000953adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000954 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Douglas Gregor832d6202011-07-22 16:35:34 +0000956 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Douglas Gregore650c8c2009-07-07 00:12:59 +0000959 // Verify that the filename and the system root have the same prefix.
960 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000961 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 if (Filename[Pos] != isysroot[Pos])
963 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Douglas Gregore650c8c2009-07-07 00:12:59 +0000965 // We hit the end of the filename before we hit the end of the system root.
966 if (!Filename[Pos])
967 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 // If the file name has a '/' at the current position, skip over the '/'.
970 // We distinguish sysroot-based includes from absolute includes by the
971 // absence of '/' at the beginning of sysroot-based includes.
972 if (Filename[Pos] == '/')
973 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 return Filename + Pos;
976}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000977
Sebastian Redl3397c552010-08-18 23:56:27 +0000978/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000979void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000980 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000981 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000982
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000984 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000986 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000987 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
988 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000989 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
990 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
991 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000992 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors
Douglas Gregore95b9192011-08-17 21:07:30 +0000993 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000994 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Douglas Gregore650c8c2009-07-07 00:12:59 +0000996 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000997 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000998 Record.push_back(VERSION_MAJOR);
999 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001000 Record.push_back(CLANG_VERSION_MAJOR);
1001 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001002 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001003 Record.push_back(ASTHasCompilerErrors);
Douglas Gregore95b9192011-08-17 21:07:30 +00001004 const std::string &Triple = Target.getTriple().getTriple();
1005 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
1006
1007 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001008 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1009 llvm::SmallVector<char, 128> ModulePaths;
1010 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001011
1012 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1013 M != MEnd; ++M) {
1014 // Skip modules that weren't directly imported.
1015 if (!(*M)->isDirectlyImported())
1016 continue;
1017
1018 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1019 // FIXME: Write import location, once it matters.
1020 // FIXME: This writes the absolute path for AST files we depend on.
1021 const std::string &FileName = (*M)->FileName;
1022 Record.push_back(FileName.size());
1023 Record.append(FileName.begin(), FileName.end());
1024 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001025 Stream.EmitRecord(IMPORTS, Record);
1026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Douglas Gregor31d375f2011-05-06 21:43:30 +00001028 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001029 SourceManager &SM = Context.getSourceManager();
1030 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1031 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001032 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001033 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1034 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1035
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001036 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001038 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001039
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001040 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001041 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001042 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001043 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001044 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001045 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001046
1047 Record.clear();
1048 Record.push_back(SM.getMainFileID().getOpaqueValue());
1049 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001050 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001051
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001052 // Original PCH directory
1053 if (!OutputFile.empty() && OutputFile != "-") {
1054 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1055 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1057 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1058
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001059 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001060
1061 llvm::sys::fs::make_absolute(OutputPath);
1062 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1063
1064 RecordData Record;
1065 Record.push_back(ORIGINAL_PCH_DIR);
1066 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1067 }
1068
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001069 // Repository branch/version information.
1070 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001071 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001072 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1073 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001074 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001075 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001076 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1077 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001078}
1079
1080/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001081void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001082 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001083#define LANGOPT(Name, Bits, Default, Description) \
1084 Record.push_back(LangOpts.Name);
1085#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1086 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1087#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00001088
1089 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1090 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001091
1092 Record.push_back(LangOpts.CurrentModule.size());
1093 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001094 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001095}
1096
Douglas Gregor14f79002009-04-10 03:52:48 +00001097//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001098// stat cache Serialization
1099//===----------------------------------------------------------------------===//
1100
1101namespace {
1102// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001103class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001104public:
1105 typedef const char * key_type;
1106 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Chris Lattner74e976b2010-11-23 19:28:12 +00001108 typedef struct stat data_type;
1109 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001110
1111 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001112 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
1115 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001116 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001117 data_type_ref Data) {
1118 unsigned StrLen = strlen(path);
1119 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001120 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001121 clang::io::Emit8(Out, DataLen);
1122 return std::make_pair(StrLen + 1, DataLen);
1123 }
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Chris Lattner5f9e2722011-07-23 10:55:15 +00001125 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001126 Out.write(path, KeyLen);
1127 }
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Chris Lattner5f9e2722011-07-23 10:55:15 +00001129 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001130 data_type_ref Data, unsigned DataLen) {
1131 using namespace clang::io;
1132 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Chris Lattner74e976b2010-11-23 19:28:12 +00001134 Emit32(Out, (uint32_t) Data.st_ino);
1135 Emit32(Out, (uint32_t) Data.st_dev);
1136 Emit16(Out, (uint16_t) Data.st_mode);
1137 Emit64(Out, (uint64_t) Data.st_mtime);
1138 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001139
1140 assert(Out.tell() - Start == DataLen && "Wrong data length");
1141 }
1142};
1143} // end anonymous namespace
1144
Sebastian Redl3397c552010-08-18 23:56:27 +00001145/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001146void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001147 // Build the on-disk hash table containing information about every
1148 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001149 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001150 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001151 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001152 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001153 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001154 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001155 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001158 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001159 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001160 uint32_t BucketOffset;
1161 {
1162 llvm::raw_svector_ostream Out(StatCacheData);
1163 // Make sure that no bucket is at offset 0
1164 clang::io::Emit32(Out, 0);
1165 BucketOffset = Generator.Emit(Out);
1166 }
1167
1168 // Create a blob abbreviation
1169 using namespace llvm;
1170 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001171 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1175 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1176
1177 // Write the stat cache
1178 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001179 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001180 Record.push_back(BucketOffset);
1181 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001182 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001183}
1184
1185//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001186// Source Manager Serialization
1187//===----------------------------------------------------------------------===//
1188
1189/// \brief Create an abbreviation for the SLocEntry that refers to a
1190/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001191static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001192 using namespace llvm;
1193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001194 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001199 // FileEntry fields.
1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001207 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001208}
1209
1210/// \brief Create an abbreviation for the SLocEntry that refers to a
1211/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001212static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001213 using namespace llvm;
1214 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001215 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001221 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001222}
1223
1224/// \brief Create an abbreviation for the SLocEntry that refers to a
1225/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001226static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001227 using namespace llvm;
1228 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001229 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001231 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001232}
1233
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001234/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1235/// expansion.
1236static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001237 using namespace llvm;
1238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001239 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001245 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001246}
1247
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001248namespace {
1249 // Trait used for the on-disk hash table of header search information.
1250 class HeaderFileInfoTrait {
1251 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001252
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001253 // Keep track of the framework names we've used during serialization.
1254 SmallVector<char, 128> FrameworkStringData;
1255 llvm::StringMap<unsigned> FrameworkNameOffset;
1256
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001257 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001258 HeaderFileInfoTrait(ASTWriter &Writer)
1259 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001260
1261 typedef const char *key_type;
1262 typedef key_type key_type_ref;
1263
1264 typedef HeaderFileInfo data_type;
1265 typedef const data_type &data_type_ref;
1266
1267 static unsigned ComputeHash(const char *path) {
1268 // The hash is based only on the filename portion of the key, so that the
1269 // reader can match based on filenames when symlinking or excess path
1270 // elements ("foo/../", "../") change the form of the name. However,
1271 // complete path is still the key.
1272 return llvm::HashString(llvm::sys::path::filename(path));
1273 }
1274
1275 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001276 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001277 data_type_ref Data) {
1278 unsigned StrLen = strlen(path);
1279 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001280 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001281 clang::io::Emit8(Out, DataLen);
1282 return std::make_pair(StrLen + 1, DataLen);
1283 }
1284
Chris Lattner5f9e2722011-07-23 10:55:15 +00001285 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001286 Out.write(path, KeyLen);
1287 }
1288
Chris Lattner5f9e2722011-07-23 10:55:15 +00001289 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001290 data_type_ref Data, unsigned DataLen) {
1291 using namespace clang::io;
1292 uint64_t Start = Out.tell(); (void)Start;
1293
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001294 unsigned char Flags = (Data.isImport << 5)
1295 | (Data.isPragmaOnce << 4)
1296 | (Data.DirInfo << 2)
1297 | (Data.Resolved << 1)
1298 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001299 Emit8(Out, (uint8_t)Flags);
1300 Emit16(Out, (uint16_t) Data.NumIncludes);
1301
1302 if (!Data.ControllingMacro)
1303 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1304 else
1305 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001306
1307 unsigned Offset = 0;
1308 if (!Data.Framework.empty()) {
1309 // If this header refers into a framework, save the framework name.
1310 llvm::StringMap<unsigned>::iterator Pos
1311 = FrameworkNameOffset.find(Data.Framework);
1312 if (Pos == FrameworkNameOffset.end()) {
1313 Offset = FrameworkStringData.size() + 1;
1314 FrameworkStringData.append(Data.Framework.begin(),
1315 Data.Framework.end());
1316 FrameworkStringData.push_back(0);
1317
1318 FrameworkNameOffset[Data.Framework] = Offset;
1319 } else
1320 Offset = Pos->second;
1321 }
1322 Emit32(Out, Offset);
1323
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001324 assert(Out.tell() - Start == DataLen && "Wrong data length");
1325 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001326
1327 const char *strings_begin() const { return FrameworkStringData.begin(); }
1328 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001329 };
1330} // end anonymous namespace
1331
1332/// \brief Write the header search block for the list of files that
1333///
1334/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001335void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001336 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001337 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1338
1339 if (FilesByUID.size() > HS.header_file_size())
1340 FilesByUID.resize(HS.header_file_size());
1341
Benjamin Kramerfacde172012-06-06 17:32:50 +00001342 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001343 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001344 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001345 unsigned NumHeaderSearchEntries = 0;
1346 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1347 const FileEntry *File = FilesByUID[UID];
1348 if (!File)
1349 continue;
1350
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001351 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1352 // from the external source if it was not provided already.
1353 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001354 if (HFI.External && Chain)
1355 continue;
1356
1357 // Turn the file name into an absolute path, if it isn't already.
1358 const char *Filename = File->getName();
1359 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1360
1361 // If we performed any translation on the file name at all, we need to
1362 // save this string, since the generator will refer to it later.
1363 if (Filename != File->getName()) {
1364 Filename = strdup(Filename);
1365 SavedStrings.push_back(Filename);
1366 }
1367
1368 Generator.insert(Filename, HFI, GeneratorTrait);
1369 ++NumHeaderSearchEntries;
1370 }
1371
1372 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001373 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001374 uint32_t BucketOffset;
1375 {
1376 llvm::raw_svector_ostream Out(TableData);
1377 // Make sure that no bucket is at offset 0
1378 clang::io::Emit32(Out, 0);
1379 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1380 }
1381
1382 // Create a blob abbreviation
1383 using namespace llvm;
1384 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1385 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1390 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1391
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001392 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001393 RecordData Record;
1394 Record.push_back(HEADER_SEARCH_TABLE);
1395 Record.push_back(BucketOffset);
1396 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001397 Record.push_back(TableData.size());
1398 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001399 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1400
1401 // Free all of the strings we had to duplicate.
1402 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1403 free((void*)SavedStrings[I]);
1404}
1405
Douglas Gregor14f79002009-04-10 03:52:48 +00001406/// \brief Writes the block containing the serialized form of the
1407/// source manager.
1408///
1409/// TODO: We should probably use an on-disk hash table (stored in a
1410/// blob), indexed based on the file name, so that we only create
1411/// entries for files that we actually need. In the common case (no
1412/// errors), we probably won't have to create file entries for any of
1413/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001414void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001415 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001416 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001417 RecordData Record;
1418
Chris Lattnerf04ad692009-04-10 17:16:57 +00001419 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001420 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001421
1422 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001423 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1424 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1425 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001426 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001427
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001428 // Write out the source location entry table. We skip the first
1429 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001430 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001431 // Write out the offsets of only source location file entries.
1432 // We will go through them in ASTReader::validateFileEntries().
1433 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001434 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001435 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1436 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001437 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001438 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001439 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001440 FileID FID = FileID::get(I);
1441 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001442
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001443 // Record the offset of this source-location entry.
1444 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1445
1446 // Figure out which record code to use.
1447 unsigned Code;
1448 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001449 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1450 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001451 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001452 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1453 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001454 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001455 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001456 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001457 Record.clear();
1458 Record.push_back(Code);
1459
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001460 // Starting offset of this entry within this module, so skip the dummy.
1461 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001462 if (SLoc->isFile()) {
1463 const SrcMgr::FileInfo &File = SLoc->getFile();
1464 Record.push_back(File.getIncludeLoc().getRawEncoding());
1465 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1466 Record.push_back(File.hasLineDirectives());
1467
1468 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001469 if (Content->OrigEntry) {
1470 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001471 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001472
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001473 // The source location entry is a file. The blob associated
1474 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Douglas Gregor2d52be52010-03-21 22:49:54 +00001476 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001477 Record.push_back(Content->OrigEntry->getSize());
1478 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001479 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001480 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001481
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001482 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001483 if (FDI != FileDeclIDs.end()) {
1484 Record.push_back(FDI->second->FirstDeclIndex);
1485 Record.push_back(FDI->second->DeclIDs.size());
1486 } else {
1487 Record.push_back(0);
1488 Record.push_back(0);
1489 }
Douglas Gregora081da52011-11-16 20:05:18 +00001490
Douglas Gregore650c8c2009-07-07 00:12:59 +00001491 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001492 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001493 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001494
1495 // Ask the file manager to fixup the relative path for us. This will
1496 // honor the working directory.
1497 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1498
1499 // FIXME: This call to make_absolute shouldn't be necessary, the
1500 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001501 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001502 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Douglas Gregore650c8c2009-07-07 00:12:59 +00001504 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001505 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001506
1507 if (Content->BufferOverridden) {
1508 Record.clear();
1509 Record.push_back(SM_SLOC_BUFFER_BLOB);
1510 const llvm::MemoryBuffer *Buffer
1511 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1512 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1513 StringRef(Buffer->getBufferStart(),
1514 Buffer->getBufferSize() + 1));
1515 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001516 } else {
1517 // The source location entry is a buffer. The blob associated
1518 // with this entry contains the contents of the buffer.
1519
1520 // We add one to the size so that we capture the trailing NULL
1521 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1522 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001523 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001524 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001525 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001526 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001527 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001528 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001529 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001530 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001531 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001532 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001533
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001534 if (strcmp(Name, "<built-in>") == 0) {
1535 PreloadSLocs.push_back(SLocEntryOffsets.size());
1536 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001537 }
1538 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001539 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001540 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001541 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1542 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001543 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1544 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001545
1546 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001547 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001548 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001549 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001550 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001551 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552 }
1553 }
1554
Douglas Gregorc9490c02009-04-16 22:23:12 +00001555 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001556
1557 if (SLocEntryOffsets.empty())
1558 return;
1559
Sebastian Redl3397c552010-08-18 23:56:27 +00001560 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001561 // table is used for lazily loading source-location information.
1562 using namespace llvm;
1563 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001564 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1568 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001570 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001571 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001572 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001573 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001574 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001575
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001576 Abbrev = new BitCodeAbbrev();
1577 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1580 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1581
1582 Record.clear();
1583 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1584 Record.push_back(SLocFileEntryOffsets.size());
1585 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1586 data(SLocFileEntryOffsets));
1587
Sebastian Redl3397c552010-08-18 23:56:27 +00001588 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001589 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001590 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001591
1592 // Write the line table. It depends on remapping working, so it must come
1593 // after the source location offsets.
1594 if (SourceMgr.hasLineTable()) {
1595 LineTableInfo &LineTable = SourceMgr.getLineTable();
1596
1597 Record.clear();
1598 // Emit the file names
1599 Record.push_back(LineTable.getNumFilenames());
1600 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1601 // Emit the file name
1602 const char *Filename = LineTable.getFilename(I);
1603 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1604 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1605 Record.push_back(FilenameLen);
1606 if (FilenameLen)
1607 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1608 }
1609
1610 // Emit the line entries
1611 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1612 L != LEnd; ++L) {
1613 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001614 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001615 continue;
1616
1617 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001618 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001619
1620 // Emit the line entries
1621 Record.push_back(L->second.size());
1622 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1623 LEEnd = L->second.end();
1624 LE != LEEnd; ++LE) {
1625 Record.push_back(LE->FileOffset);
1626 Record.push_back(LE->LineNo);
1627 Record.push_back(LE->FilenameID);
1628 Record.push_back((unsigned)LE->FileKind);
1629 Record.push_back(LE->IncludeOffset);
1630 }
1631 }
1632 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1633 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001634}
1635
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001636//===----------------------------------------------------------------------===//
1637// Preprocessor Serialization
1638//===----------------------------------------------------------------------===//
1639
Douglas Gregor9c736102011-02-10 18:20:09 +00001640static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1641 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1642 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1643 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1644 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1645 return X.first->getName().compare(Y.first->getName());
1646}
1647
Chris Lattner0b1fb982009-04-10 17:15:23 +00001648/// \brief Writes the block containing the serialized form of the
1649/// preprocessor.
1650///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001651void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001652 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1653 if (PPRec)
1654 WritePreprocessorDetail(*PPRec);
1655
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001656 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001657
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001658 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1659 if (PP.getCounterValue() != 0) {
1660 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001661 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001662 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001663 }
1664
1665 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001666 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Sebastian Redl3397c552010-08-18 23:56:27 +00001668 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001669 // FIXME: use diagnostics subsystem for localization etc.
1670 if (PP.SawDateOrTime())
1671 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Douglas Gregorecdcb882010-10-20 22:00:55 +00001673
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001674 // Loop over all the macro definitions that are live at the end of the file,
1675 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001676
Douglas Gregor9c736102011-02-10 18:20:09 +00001677 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001678 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001679 MacrosToEmit;
1680 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001681 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001682 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001683 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001684 if (!IsModule || I->second->isPublic()) {
1685 MacroDefinitionsSeen.insert(I->first);
1686 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001687 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001688 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001689
Douglas Gregor9c736102011-02-10 18:20:09 +00001690 // Sort the set of macro definitions that need to be serialized by the
1691 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001692 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001693 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001694
Douglas Gregor040a8042011-02-11 00:26:14 +00001695 // Resolve any identifiers that defined macros at the time they were
1696 // deserialized, adding them to the list of macros to emit (if appropriate).
1697 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1698 IdentifierInfo *Name
1699 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001700 if (Name->hadMacroDefinition() && MacroDefinitionsSeen.insert(Name))
Douglas Gregor040a8042011-02-11 00:26:14 +00001701 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1702 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001703
Douglas Gregor9c736102011-02-10 18:20:09 +00001704 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1705 const IdentifierInfo *Name = MacrosToEmit[I].first;
1706 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001707 if (!MI)
1708 continue;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001709
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001710 // History of macro definitions for this identifier in chronological order.
1711 SmallVector<MacroInfo*, 8> MacroHistory;
1712 while (MI) {
1713 MacroHistory.push_back(MI);
1714 MI = MI->getPreviousDefinition();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001715 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001716
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001717 while (!MacroHistory.empty()) {
1718 MI = MacroHistory.pop_back_val();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001719
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001720 // Don't emit builtin macros like __LINE__ to the AST file unless they
1721 // have been redefined by the header (in which case they are not
1722 // isBuiltinMacro).
1723 // Also skip macros from a AST file if we're chaining.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001724
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001725 // FIXME: There is a (probably minor) optimization we could do here, if
1726 // the macro comes from the original PCH but the identifier comes from a
1727 // chained PCH, by storing the offset into the original PCH rather than
1728 // writing the macro definition a second time.
1729 if (MI->isBuiltinMacro() ||
1730 (Chain &&
1731 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1732 MI->isFromAST() && !MI->hasChangedAfterLoad()))
1733 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001735 AddIdentifierRef(Name, Record);
1736 MacroOffsets[Name] = Stream.GetCurrentBitNo();
1737 AddSourceLocation(MI->getDefinitionLoc(), Record);
1738 AddSourceLocation(MI->getUndefLoc(), Record);
1739 Record.push_back(MI->isUsed());
1740 Record.push_back(MI->isPublic());
1741 AddSourceLocation(MI->getVisibilityLocation(), Record);
1742 unsigned Code;
1743 if (MI->isObjectLike()) {
1744 Code = PP_MACRO_OBJECT_LIKE;
1745 } else {
1746 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001747
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001748 Record.push_back(MI->isC99Varargs());
1749 Record.push_back(MI->isGNUVarargs());
1750 Record.push_back(MI->getNumArgs());
1751 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1752 I != E; ++I)
1753 AddIdentifierRef(*I, Record);
1754 }
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001756 // If we have a detailed preprocessing record, record the macro definition
1757 // ID that corresponds to this macro.
1758 if (PPRec)
1759 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1760
1761 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001762 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001763
1764 // Emit the tokens array.
1765 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1766 // Note that we know that the preprocessor does not have any annotation
1767 // tokens in it because they are created by the parser, and thus can't
1768 // be in a macro definition.
1769 const Token &Tok = MI->getReplacementToken(TokNo);
1770
1771 Record.push_back(Tok.getLocation().getRawEncoding());
1772 Record.push_back(Tok.getLength());
1773
1774 // FIXME: When reading literal tokens, reconstruct the literal pointer
1775 // if it is needed.
1776 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1777 // FIXME: Should translate token kind to a stable encoding.
1778 Record.push_back(Tok.getKind());
1779 // FIXME: Should translate token flags to a stable encoding.
1780 Record.push_back(Tok.getFlags());
1781
1782 Stream.EmitRecord(PP_TOKEN, Record);
1783 Record.clear();
1784 }
1785 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001786 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001787 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001788 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001789}
1790
1791void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001792 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001793 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001794
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001795 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001796
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001797 // Enter the preprocessor block.
1798 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001799
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001800 // If the preprocessor has a preprocessing record, emit it.
1801 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001802 using namespace llvm;
1803
1804 // Set up the abbreviation for
1805 unsigned InclusionAbbrev = 0;
1806 {
1807 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1808 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1814 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1815 }
1816
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001817 unsigned FirstPreprocessorEntityID
1818 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1819 + NUM_PREDEF_PP_ENTITY_IDS;
1820 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001821 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001822 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1823 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001824 E != EEnd;
1825 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001826 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001827
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001828 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1829 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001830
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001831 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001832 // Record this macro definition's ID.
1833 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001834
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001835 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001836 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1837 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001838 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001839
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001840 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001841 Record.push_back(ME->isBuiltinMacro());
1842 if (ME->isBuiltinMacro())
1843 AddIdentifierRef(ME->getName(), Record);
1844 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001845 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001846 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001847 continue;
1848 }
1849
1850 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1851 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001852 Record.push_back(ID->getFileName().size());
1853 Record.push_back(ID->wasInQuotes());
1854 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001855 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001856 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001857 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001858 // Check that the FileEntry is not null because it was not resolved and
1859 // we create a PCH even with compiler errors.
1860 if (ID->getFile())
1861 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001862 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1863 continue;
1864 }
1865
1866 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1867 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001868 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001869
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001870 // Write the offsets table for the preprocessing record.
1871 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001872 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1873
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001874 // Write the offsets table for identifier IDs.
1875 using namespace llvm;
1876 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001877 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001880 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001881
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001882 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001883 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001884 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001885 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1886 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001887 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001888}
1889
Douglas Gregore209e502011-12-06 01:10:29 +00001890unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1891 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1892 if (Known != SubmoduleIDs.end())
1893 return Known->second;
1894
1895 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1896}
1897
Douglas Gregor26ced122011-12-01 00:59:36 +00001898/// \brief Compute the number of modules within the given tree (including the
1899/// given module).
1900static unsigned getNumberOfModules(Module *Mod) {
1901 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001902 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1903 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001904 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001905 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001906
1907 return ChildModules + 1;
1908}
1909
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001910void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001911 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001912 // FIXME: This feels like it belongs somewhere else, but there are no
1913 // other consumers of this information.
1914 SourceManager &SrcMgr = PP->getSourceManager();
1915 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1916 for (ASTContext::import_iterator I = Context->local_import_begin(),
1917 IEnd = Context->local_import_end();
1918 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001919 if (Module *ImportedFrom
1920 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1921 SrcMgr))) {
1922 ImportedFrom->Imports.push_back(I->getImportedModule());
1923 }
1924 }
1925
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001926 // Enter the submodule description block.
1927 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1928
1929 // Write the abbreviations needed for the submodules block.
1930 using namespace llvm;
1931 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1932 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1942 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1943
1944 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001945 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1947 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1948
1949 Abbrev = new BitCodeAbbrev();
1950 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1952 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001953
1954 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00001955 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
1956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1957 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
1958
1959 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001960 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1962 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1963
Douglas Gregor51f564f2011-12-31 04:05:44 +00001964 Abbrev = new BitCodeAbbrev();
1965 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1967 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1968
Douglas Gregor26ced122011-12-01 00:59:36 +00001969 // Write the submodule metadata block.
1970 RecordData Record;
1971 Record.push_back(getNumberOfModules(WritingModule));
1972 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1973 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1974
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001975 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001976 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001977 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001978 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001979 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001980 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001981 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001982
1983 // Emit the definition of the block.
1984 Record.clear();
1985 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001986 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001987 if (Mod->Parent) {
1988 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1989 Record.push_back(SubmoduleIDs[Mod->Parent]);
1990 } else {
1991 Record.push_back(0);
1992 }
1993 Record.push_back(Mod->IsFramework);
1994 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001995 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00001996 Record.push_back(Mod->InferSubmodules);
1997 Record.push_back(Mod->InferExplicitSubmodules);
1998 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001999 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2000
Douglas Gregor51f564f2011-12-31 04:05:44 +00002001 // Emit the requirements.
2002 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2003 Record.clear();
2004 Record.push_back(SUBMODULE_REQUIRES);
2005 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2006 Mod->Requires[I].data(),
2007 Mod->Requires[I].size());
2008 }
2009
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002010 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002011 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002012 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002013 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002014 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002015 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002016 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2017 Record.clear();
2018 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2019 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2020 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002021 }
2022
2023 // Emit the headers.
2024 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2025 Record.clear();
2026 Record.push_back(SUBMODULE_HEADER);
2027 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2028 Mod->Headers[I]->getName());
2029 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002030 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2031 Record.clear();
2032 Record.push_back(SUBMODULE_TOPHEADER);
2033 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2034 Mod->TopHeaders[I]->getName());
2035 }
Douglas Gregor55988682011-12-05 16:33:54 +00002036
2037 // Emit the imports.
2038 if (!Mod->Imports.empty()) {
2039 Record.clear();
2040 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002041 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002042 assert(ImportedID && "Unknown submodule!");
2043 Record.push_back(ImportedID);
2044 }
2045 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2046 }
2047
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002048 // Emit the exports.
2049 if (!Mod->Exports.empty()) {
2050 Record.clear();
2051 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002052 if (Module *Exported = Mod->Exports[I].getPointer()) {
2053 unsigned ExportedID = SubmoduleIDs[Exported];
2054 assert(ExportedID > 0 && "Unknown submodule ID?");
2055 Record.push_back(ExportedID);
2056 } else {
2057 Record.push_back(0);
2058 }
2059
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002060 Record.push_back(Mod->Exports[I].getInt());
2061 }
2062 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2063 }
2064
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002065 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002066 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2067 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002068 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002069 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002070 }
2071
2072 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002073
2074 assert((NextSubmoduleID - FirstSubmoduleID
2075 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002076}
2077
Douglas Gregor185dbd72011-12-01 02:07:58 +00002078serialization::SubmoduleID
2079ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002080 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002081 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002082
2083 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002084 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002085 Module *OwningMod
2086 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002087 if (!OwningMod)
2088 return 0;
2089
Douglas Gregore209e502011-12-06 01:10:29 +00002090 // Check whether this submodule is part of our own module.
2091 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002092 return 0;
2093
Douglas Gregore209e502011-12-06 01:10:29 +00002094 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002095}
2096
David Blaikied6471f72011-09-25 23:23:43 +00002097void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002098 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002099 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002100 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2101 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002102 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002103 if (point.Loc.isInvalid())
2104 continue;
2105
2106 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002107 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002108 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002109 if (I->second.isPragma()) {
2110 Record.push_back(I->first);
2111 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002112 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002113 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002114 Record.push_back(-1); // mark the end of the diag/map pairs for this
2115 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002116 }
2117
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002118 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002119 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002120}
2121
Anders Carlssonc8505782011-03-06 18:41:18 +00002122void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2123 if (CXXBaseSpecifiersOffsets.empty())
2124 return;
2125
2126 RecordData Record;
2127
2128 // Create a blob abbreviation for the C++ base specifiers offsets.
2129 using namespace llvm;
2130
2131 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2132 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2135 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2136
Douglas Gregore92b8a12011-08-04 00:01:48 +00002137 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002138 Record.clear();
2139 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2140 Record.push_back(CXXBaseSpecifiersOffsets.size());
2141 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002142 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002143}
2144
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002145//===----------------------------------------------------------------------===//
2146// Type Serialization
2147//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002148
Sebastian Redl3397c552010-08-18 23:56:27 +00002149/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002150void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002151 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002152 if (Idx.getIndex() == 0) // we haven't seen this type before.
2153 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Douglas Gregor97475832010-10-05 18:37:06 +00002155 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002156
Douglas Gregor2cf26342009-04-09 22:27:44 +00002157 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002158 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002159 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002160 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002161 else if (TypeOffsets.size() < Index) {
2162 TypeOffsets.resize(Index + 1);
2163 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002164 }
2165
2166 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002167
Douglas Gregor2cf26342009-04-09 22:27:44 +00002168 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002169 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002170
Douglas Gregora4923eb2009-11-16 21:35:15 +00002171 if (T.hasLocalNonFastQualifiers()) {
2172 Qualifiers Qs = T.getLocalQualifiers();
2173 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002174 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002175 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002176 } else {
2177 switch (T->getTypeClass()) {
2178 // For all of the concrete, non-dependent types, call the
2179 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002180#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002181 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002182#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002183#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002184 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002185 }
2186
2187 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002188 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002189
2190 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002191 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002192}
2193
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002194//===----------------------------------------------------------------------===//
2195// Declaration Serialization
2196//===----------------------------------------------------------------------===//
2197
Douglas Gregor2cf26342009-04-09 22:27:44 +00002198/// \brief Write the block containing all of the declaration IDs
2199/// lexically declared within the given DeclContext.
2200///
2201/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2202/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002203uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002204 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002205 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002206 return 0;
2207
Douglas Gregorc9490c02009-04-16 22:23:12 +00002208 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002209 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002210 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002211 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002212 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2213 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002214 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002215
Douglas Gregor25123082009-04-22 22:34:57 +00002216 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002217 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002218 return Offset;
2219}
2220
Sebastian Redla4232eb2010-08-18 23:56:21 +00002221void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002222 using namespace llvm;
2223 RecordData Record;
2224
2225 // Write the type offsets array
2226 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002227 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2231 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2232 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002233 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002234 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002235 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002236 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002237
2238 // Write the declaration offsets array
2239 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002240 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2244 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2245 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002246 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002247 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002248 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002249 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002250}
2251
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002252void ASTWriter::WriteFileDeclIDsMap() {
2253 using namespace llvm;
2254 RecordData Record;
2255
2256 // Join the vectors of DeclIDs from all files.
2257 SmallVector<DeclID, 256> FileSortedIDs;
2258 for (FileDeclIDsTy::iterator
2259 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2260 DeclIDInFileInfo &Info = *FI->second;
2261 Info.FirstDeclIndex = FileSortedIDs.size();
2262 for (LocDeclIDsTy::iterator
2263 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2264 FileSortedIDs.push_back(DI->second);
2265 }
2266
2267 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2268 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2271 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2272 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002273 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002274 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2275}
2276
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002277void ASTWriter::WriteComments() {
2278 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002279 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002280 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002281 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2282 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002283 I != E; ++I) {
2284 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002285 AddSourceRange((*I)->getSourceRange(), Record);
2286 Record.push_back((*I)->getKind());
2287 Record.push_back((*I)->isTrailingComment());
2288 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002289 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2290 }
2291 Stream.ExitBlock();
2292}
2293
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002294//===----------------------------------------------------------------------===//
2295// Global Method Pool and Selector Serialization
2296//===----------------------------------------------------------------------===//
2297
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002298namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002299// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002300class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002301 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002302
2303public:
2304 typedef Selector key_type;
2305 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002306
Sebastian Redl5d050072010-08-04 17:20:04 +00002307 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002308 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002309 ObjCMethodList Instance, Factory;
2310 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002311 typedef const data_type& data_type_ref;
2312
Sebastian Redl3397c552010-08-18 23:56:27 +00002313 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002315 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002316 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002317 }
Mike Stump1eb44332009-09-09 15:08:12 +00002318
2319 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002320 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002321 data_type_ref Methods) {
2322 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2323 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002324 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2325 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002326 Method = Method->Next)
2327 if (Method->Method)
2328 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002329 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002330 Method = Method->Next)
2331 if (Method->Method)
2332 DataLen += 4;
2333 clang::io::Emit16(Out, DataLen);
2334 return std::make_pair(KeyLen, DataLen);
2335 }
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Chris Lattner5f9e2722011-07-23 10:55:15 +00002337 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002338 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002339 assert((Start >> 32) == 0 && "Selector key offset too large");
2340 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002341 unsigned N = Sel.getNumArgs();
2342 clang::io::Emit16(Out, N);
2343 if (N == 0)
2344 N = 1;
2345 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002346 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002347 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2348 }
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Chris Lattner5f9e2722011-07-23 10:55:15 +00002350 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002351 data_type_ref Methods, unsigned DataLen) {
2352 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002353 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002354 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002355 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002356 Method = Method->Next)
2357 if (Method->Method)
2358 ++NumInstanceMethods;
2359
2360 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002361 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002362 Method = Method->Next)
2363 if (Method->Method)
2364 ++NumFactoryMethods;
2365
2366 clang::io::Emit16(Out, NumInstanceMethods);
2367 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002368 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002369 Method = Method->Next)
2370 if (Method->Method)
2371 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002372 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002373 Method = Method->Next)
2374 if (Method->Method)
2375 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002376
2377 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002378 }
2379};
2380} // end anonymous namespace
2381
Sebastian Redl059612d2010-08-03 21:58:15 +00002382/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002383///
2384/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002385/// in an on-disk hash table indexed by the selector. The hash table also
2386/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002387void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002388 using namespace llvm;
2389
Sebastian Redl059612d2010-08-03 21:58:15 +00002390 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002391 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002392 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002393 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002394 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002395 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002396 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002397 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002398
Sebastian Redl059612d2010-08-03 21:58:15 +00002399 // Create the on-disk hash table representation. We walk through every
2400 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002401 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002402 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002403 I = SelectorIDs.begin(), E = SelectorIDs.end();
2404 I != E; ++I) {
2405 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002406 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002407 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002408 I->second,
2409 ObjCMethodList(),
2410 ObjCMethodList()
2411 };
2412 if (F != SemaRef.MethodPool.end()) {
2413 Data.Instance = F->second.first;
2414 Data.Factory = F->second.second;
2415 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002416 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002417 // changed.
2418 if (Chain && I->second < FirstSelectorID) {
2419 // Selector already exists. Did it change?
2420 bool changed = false;
2421 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2422 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002423 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002424 changed = true;
2425 }
2426 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2427 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002428 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002429 changed = true;
2430 }
2431 if (!changed)
2432 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002433 } else if (Data.Instance.Method || Data.Factory.Method) {
2434 // A new method pool entry.
2435 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002436 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002437 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002438 }
2439
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002440 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002441 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002442 uint32_t BucketOffset;
2443 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002444 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002445 llvm::raw_svector_ostream Out(MethodPool);
2446 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002447 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002448 BucketOffset = Generator.Emit(Out, Trait);
2449 }
2450
2451 // Create a blob abbreviation
2452 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002453 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2457 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2458
Douglas Gregor83941df2009-04-25 17:48:32 +00002459 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002460 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002461 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002462 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002463 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002464 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002465
2466 // Create a blob abbreviation for the selector table offsets.
2467 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002468 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002469 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002470 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2472 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2473
2474 // Write the selector offsets table.
2475 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002476 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002477 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002478 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002479 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002480 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002481 }
2482}
2483
Sebastian Redl3397c552010-08-18 23:56:27 +00002484/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002485void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002486 using namespace llvm;
2487 if (SemaRef.ReferencedSelectors.empty())
2488 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002489
Fariborz Jahanian32019832010-07-23 19:11:11 +00002490 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002491
Sebastian Redl3397c552010-08-18 23:56:27 +00002492 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002493 // very tricky to fix, and given that @selector shouldn't really appear in
2494 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002495 for (DenseMap<Selector, SourceLocation>::iterator S =
2496 SemaRef.ReferencedSelectors.begin(),
2497 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2498 Selector Sel = (*S).first;
2499 SourceLocation Loc = (*S).second;
2500 AddSelectorRef(Sel, Record);
2501 AddSourceLocation(Loc, Record);
2502 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002503 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002504}
2505
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002506//===----------------------------------------------------------------------===//
2507// Identifier Table Serialization
2508//===----------------------------------------------------------------------===//
2509
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002510namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002511class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002512 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002513 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002514 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002515 bool IsModule;
2516
Douglas Gregora92193e2009-04-28 21:18:29 +00002517 /// \brief Determines whether this is an "interesting" identifier
2518 /// that needs a full IdentifierInfo structure written into the hash
2519 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002520 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002521 if (II->isPoisoned() ||
2522 II->isExtensionToken() ||
2523 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002524 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002525 II->getFETokenInfo<void>())
2526 return true;
2527
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002528 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002529 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002530
2531 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2532 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002533 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002534
2535 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002536 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002537
2538 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002539 }
2540
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002541public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002542 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002543 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002544
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002545 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002546 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002547
Douglas Gregoreee242f2011-10-27 09:33:13 +00002548 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2549 IdentifierResolver &IdResolver, bool IsModule)
2550 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002551
2552 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002553 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002554 }
Mike Stump1eb44332009-09-09 15:08:12 +00002555
2556 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002557 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002558 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002559 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002560 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002561 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002562 DataLen += 2; // 2 bytes for builtin ID
2563 DataLen += 2; // 2 bytes for flags
2564 if (hadMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002565 DataLen += 8;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002566
Douglas Gregoreee242f2011-10-27 09:33:13 +00002567 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2568 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002569 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002570 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002571 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002572 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002573 // We emit the key length after the data length so that every
2574 // string is preceded by a 16-bit length. This matches the PTH
2575 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002576 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002577 return std::make_pair(KeyLen, DataLen);
2578 }
Mike Stump1eb44332009-09-09 15:08:12 +00002579
Chris Lattner5f9e2722011-07-23 10:55:15 +00002580 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002581 unsigned KeyLen) {
2582 // Record the location of the key data. This is used when generating
2583 // the mapping from persistent IDs to strings.
2584 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002585 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002586 }
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Douglas Gregor7143aab2011-09-01 17:04:32 +00002588 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002589 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002590 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002591 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002592 clang::io::Emit32(Out, ID << 1);
2593 return;
2594 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002595
Douglas Gregora92193e2009-04-28 21:18:29 +00002596 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002597 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2598 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2599 clang::io::Emit16(Out, Bits);
2600 Bits = 0;
2601 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
2602 bool HasMacroDefinition = HadMacroDefinition && II->hasMacroDefinition();
Douglas Gregorce835df2011-09-14 22:14:14 +00002603 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002604 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002605 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2606 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002607 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002608 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002609 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002610
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002611 if (HadMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002612 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002613 clang::io::Emit32(Out,
Douglas Gregor13292642011-12-02 15:45:10 +00002614 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2615 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002616
Douglas Gregor668c1a42009-04-21 22:25:48 +00002617 // Emit the declaration IDs in reverse order, because the
2618 // IdentifierResolver provides the declarations as they would be
2619 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002620 // "stat"), but the ASTReader adds declarations to the end of the list
2621 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002622 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002623 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2624 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002625 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002626 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002627 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002628 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002629 }
2630};
2631} // end anonymous namespace
2632
Sebastian Redl3397c552010-08-18 23:56:27 +00002633/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002634///
2635/// The identifier table consists of a blob containing string data
2636/// (the actual identifiers themselves) and a separate "offsets" index
2637/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002638void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2639 IdentifierResolver &IdResolver,
2640 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002641 using namespace llvm;
2642
2643 // Create and write out the blob that contains the identifier
2644 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002645 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002646 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002647 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002648
Douglas Gregor92b059e2009-04-28 20:33:11 +00002649 // Look for any identifiers that were named while processing the
2650 // headers, but are otherwise not needed. We add these to the hash
2651 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002652 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002653 // file.
2654 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2655 IDEnd = PP.getIdentifierTable().end();
2656 ID != IDEnd; ++ID)
2657 getIdentifierRef(ID->second);
2658
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002659 // Create the on-disk hash table representation. We only store offsets
2660 // for identifiers that appear here for the first time.
2661 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002662 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002663 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2664 ID != IDEnd; ++ID) {
2665 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002666 if (!Chain || !ID->first->isFromAST() ||
2667 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002668 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2669 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002670 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002671
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002672 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002673 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002674 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002675 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002676 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002677 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002678 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002679 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002680 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002681 }
2682
2683 // Create a blob abbreviation
2684 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002685 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002688 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002689
2690 // Write the identifier table
2691 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002692 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002693 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002694 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002695 }
2696
2697 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002698 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002699 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002701 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002702 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2703 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2704
2705 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002706 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002707 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002708 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002709 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002710 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002711}
2712
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002713//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002714// DeclContext's Name Lookup Table Serialization
2715//===----------------------------------------------------------------------===//
2716
2717namespace {
2718// Trait used for the on-disk hash table used in the method pool.
2719class ASTDeclContextNameLookupTrait {
2720 ASTWriter &Writer;
2721
2722public:
2723 typedef DeclarationName key_type;
2724 typedef key_type key_type_ref;
2725
2726 typedef DeclContext::lookup_result data_type;
2727 typedef const data_type& data_type_ref;
2728
2729 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2730
2731 unsigned ComputeHash(DeclarationName Name) {
2732 llvm::FoldingSetNodeID ID;
2733 ID.AddInteger(Name.getNameKind());
2734
2735 switch (Name.getNameKind()) {
2736 case DeclarationName::Identifier:
2737 ID.AddString(Name.getAsIdentifierInfo()->getName());
2738 break;
2739 case DeclarationName::ObjCZeroArgSelector:
2740 case DeclarationName::ObjCOneArgSelector:
2741 case DeclarationName::ObjCMultiArgSelector:
2742 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2743 break;
2744 case DeclarationName::CXXConstructorName:
2745 case DeclarationName::CXXDestructorName:
2746 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002747 break;
2748 case DeclarationName::CXXOperatorName:
2749 ID.AddInteger(Name.getCXXOverloadedOperator());
2750 break;
2751 case DeclarationName::CXXLiteralOperatorName:
2752 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2753 case DeclarationName::CXXUsingDirective:
2754 break;
2755 }
2756
2757 return ID.ComputeHash();
2758 }
2759
2760 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002761 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002762 data_type_ref Lookup) {
2763 unsigned KeyLen = 1;
2764 switch (Name.getNameKind()) {
2765 case DeclarationName::Identifier:
2766 case DeclarationName::ObjCZeroArgSelector:
2767 case DeclarationName::ObjCOneArgSelector:
2768 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002769 case DeclarationName::CXXLiteralOperatorName:
2770 KeyLen += 4;
2771 break;
2772 case DeclarationName::CXXOperatorName:
2773 KeyLen += 1;
2774 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002775 case DeclarationName::CXXConstructorName:
2776 case DeclarationName::CXXDestructorName:
2777 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002778 case DeclarationName::CXXUsingDirective:
2779 break;
2780 }
2781 clang::io::Emit16(Out, KeyLen);
2782
2783 // 2 bytes for num of decls and 4 for each DeclID.
2784 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2785 clang::io::Emit16(Out, DataLen);
2786
2787 return std::make_pair(KeyLen, DataLen);
2788 }
2789
Chris Lattner5f9e2722011-07-23 10:55:15 +00002790 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002791 using namespace clang::io;
2792
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002793 Emit8(Out, Name.getNameKind());
2794 switch (Name.getNameKind()) {
2795 case DeclarationName::Identifier:
2796 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002797 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002798 case DeclarationName::ObjCZeroArgSelector:
2799 case DeclarationName::ObjCOneArgSelector:
2800 case DeclarationName::ObjCMultiArgSelector:
2801 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002802 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002803 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002804 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2805 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002806 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002807 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002808 case DeclarationName::CXXLiteralOperatorName:
2809 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002810 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002811 case DeclarationName::CXXConstructorName:
2812 case DeclarationName::CXXDestructorName:
2813 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002814 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002815 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002816 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002817
2818 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002819 }
2820
Chris Lattner5f9e2722011-07-23 10:55:15 +00002821 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002822 data_type Lookup, unsigned DataLen) {
2823 uint64_t Start = Out.tell(); (void)Start;
2824 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2825 for (; Lookup.first != Lookup.second; ++Lookup.first)
2826 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2827
2828 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2829 }
2830};
2831} // end anonymous namespace
2832
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002833/// \brief Write the block containing all of the declaration IDs
2834/// visible from the given DeclContext.
2835///
2836/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002837/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002838uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2839 DeclContext *DC) {
2840 if (DC->getPrimaryContext() != DC)
2841 return 0;
2842
2843 // Since there is no name lookup into functions or methods, don't bother to
2844 // build a visible-declarations table for these entities.
2845 if (DC->isFunctionOrMethod())
2846 return 0;
2847
2848 // If not in C++, we perform name lookup for the translation unit via the
2849 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2850 // FIXME: In C++ we need the visible declarations in order to "see" the
2851 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002852 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002853 return 0;
2854
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002855 // Serialize the contents of the mapping used for lookup. Note that,
2856 // although we have two very different code paths, the serialized
2857 // representation is the same for both cases: a declaration name,
2858 // followed by a size, followed by references to the visible
2859 // declarations that have that name.
2860 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002861 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002862 if (!Map || Map->empty())
2863 return 0;
2864
2865 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2866 ASTDeclContextNameLookupTrait Trait(*this);
2867
2868 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002869 DeclarationName ConversionName;
2870 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002871 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2872 D != DEnd; ++D) {
2873 DeclarationName Name = D->first;
2874 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002875 if (Result.first != Result.second) {
2876 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2877 // Hash all conversion function names to the same name. The actual
2878 // type information in conversion function name is not used in the
2879 // key (since such type information is not stable across different
2880 // modules), so the intended effect is to coalesce all of the conversion
2881 // functions under a single key.
2882 if (!ConversionName)
2883 ConversionName = Name;
2884 ConversionDecls.append(Result.first, Result.second);
2885 continue;
2886 }
2887
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002888 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002889 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002890 }
2891
Douglas Gregore5a54b62011-08-30 20:49:19 +00002892 // Add the conversion functions
2893 if (!ConversionDecls.empty()) {
2894 Generator.insert(ConversionName,
2895 DeclContext::lookup_result(ConversionDecls.begin(),
2896 ConversionDecls.end()),
2897 Trait);
2898 }
2899
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002900 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002901 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002902 uint32_t BucketOffset;
2903 {
2904 llvm::raw_svector_ostream Out(LookupTable);
2905 // Make sure that no bucket is at offset 0
2906 clang::io::Emit32(Out, 0);
2907 BucketOffset = Generator.Emit(Out, Trait);
2908 }
2909
2910 // Write the lookup table
2911 RecordData Record;
2912 Record.push_back(DECL_CONTEXT_VISIBLE);
2913 Record.push_back(BucketOffset);
2914 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2915 LookupTable.str());
2916
2917 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2918 ++NumVisibleDeclContexts;
2919 return Offset;
2920}
2921
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002922/// \brief Write an UPDATE_VISIBLE block for the given context.
2923///
2924/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2925/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00002926/// (in C++), for namespaces, and for classes with forward-declared unscoped
2927/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002928void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002929 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2930 if (!Map || Map->empty())
2931 return;
2932
2933 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2934 ASTDeclContextNameLookupTrait Trait(*this);
2935
2936 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002937 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2938 D != DEnd; ++D) {
2939 DeclarationName Name = D->first;
2940 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002941 // For any name that appears in this table, the results are complete, i.e.
2942 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002943 if (Result.first != Result.second)
2944 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002945 }
2946
2947 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002948 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002949 uint32_t BucketOffset;
2950 {
2951 llvm::raw_svector_ostream Out(LookupTable);
2952 // Make sure that no bucket is at offset 0
2953 clang::io::Emit32(Out, 0);
2954 BucketOffset = Generator.Emit(Out, Trait);
2955 }
2956
2957 // Write the lookup table
2958 RecordData Record;
2959 Record.push_back(UPDATE_VISIBLE);
2960 Record.push_back(getDeclID(cast<Decl>(DC)));
2961 Record.push_back(BucketOffset);
2962 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2963}
2964
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002965/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2966void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2967 RecordData Record;
2968 Record.push_back(Opts.fp_contract);
2969 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2970}
2971
2972/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2973void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002974 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002975 return;
2976
2977 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2978 RecordData Record;
2979#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2980#include "clang/Basic/OpenCLExtensions.def"
2981 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2982}
2983
Douglas Gregor2171bf12012-01-15 16:58:34 +00002984void ASTWriter::WriteRedeclarations() {
2985 RecordData LocalRedeclChains;
2986 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2987
2988 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2989 Decl *First = Redeclarations[I];
2990 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2991
2992 Decl *MostRecent = First->getMostRecentDecl();
2993
2994 // If we only have a single declaration, there is no point in storing
2995 // a redeclaration chain.
2996 if (First == MostRecent)
2997 continue;
2998
2999 unsigned Offset = LocalRedeclChains.size();
3000 unsigned Size = 0;
3001 LocalRedeclChains.push_back(0); // Placeholder for the size.
3002
3003 // Collect the set of local redeclarations of this declaration.
3004 for (Decl *Prev = MostRecent; Prev != First;
3005 Prev = Prev->getPreviousDecl()) {
3006 if (!Prev->isFromASTFile()) {
3007 AddDeclRef(Prev, LocalRedeclChains);
3008 ++Size;
3009 }
3010 }
3011 LocalRedeclChains[Offset] = Size;
3012
3013 // Reverse the set of local redeclarations, so that we store them in
3014 // order (since we found them in reverse order).
3015 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3016
3017 // Add the mapping from the first ID to the set of local declarations.
3018 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3019 LocalRedeclsMap.push_back(Info);
3020
3021 assert(N == Redeclarations.size() &&
3022 "Deserialized a declaration we shouldn't have");
3023 }
3024
3025 if (LocalRedeclChains.empty())
3026 return;
3027
3028 // Sort the local redeclarations map by the first declaration ID,
3029 // since the reader will be performing binary searches on this information.
3030 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3031
3032 // Emit the local redeclarations map.
3033 using namespace llvm;
3034 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3035 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3038 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3039
3040 RecordData Record;
3041 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3042 Record.push_back(LocalRedeclsMap.size());
3043 Stream.EmitRecordWithBlob(AbbrevID, Record,
3044 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3045 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3046
3047 // Emit the redeclaration chains.
3048 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3049}
3050
Douglas Gregorcff9f262012-01-27 01:47:08 +00003051void ASTWriter::WriteObjCCategories() {
3052 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3053 RecordData Categories;
3054
3055 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3056 unsigned Size = 0;
3057 unsigned StartIndex = Categories.size();
3058
3059 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3060
3061 // Allocate space for the size.
3062 Categories.push_back(0);
3063
3064 // Add the categories.
3065 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3066 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3067 assert(getDeclID(Cat) != 0 && "Bogus category");
3068 AddDeclRef(Cat, Categories);
3069 }
3070
3071 // Update the size.
3072 Categories[StartIndex] = Size;
3073
3074 // Record this interface -> category map.
3075 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3076 CategoriesMap.push_back(CatInfo);
3077 }
3078
3079 // Sort the categories map by the definition ID, since the reader will be
3080 // performing binary searches on this information.
3081 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3082
3083 // Emit the categories map.
3084 using namespace llvm;
3085 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3086 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3089 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3090
3091 RecordData Record;
3092 Record.push_back(OBJC_CATEGORIES_MAP);
3093 Record.push_back(CategoriesMap.size());
3094 Stream.EmitRecordWithBlob(AbbrevID, Record,
3095 reinterpret_cast<char*>(CategoriesMap.data()),
3096 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3097
3098 // Emit the category lists.
3099 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3100}
3101
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003102void ASTWriter::WriteMergedDecls() {
3103 if (!Chain || Chain->MergedDecls.empty())
3104 return;
3105
3106 RecordData Record;
3107 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3108 IEnd = Chain->MergedDecls.end();
3109 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003110 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003111 : getDeclID(I->first);
3112 assert(CanonID && "Merged declaration not known?");
3113
3114 Record.push_back(CanonID);
3115 Record.push_back(I->second.size());
3116 Record.append(I->second.begin(), I->second.end());
3117 }
3118 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3119}
3120
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003121//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003122// General Serialization Routines
3123//===----------------------------------------------------------------------===//
3124
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003125/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003126void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3127 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003128 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003129 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3130 e = Attrs.end(); i != e; ++i){
3131 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003132 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003133 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003134
Sean Huntcf807c42010-08-18 23:23:40 +00003135#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003136
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003137 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003138}
3139
Chris Lattner5f9e2722011-07-23 10:55:15 +00003140void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003141 Record.push_back(Str.size());
3142 Record.insert(Record.end(), Str.begin(), Str.end());
3143}
3144
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003145void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3146 RecordDataImpl &Record) {
3147 Record.push_back(Version.getMajor());
3148 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3149 Record.push_back(*Minor + 1);
3150 else
3151 Record.push_back(0);
3152 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3153 Record.push_back(*Subminor + 1);
3154 else
3155 Record.push_back(0);
3156}
3157
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003158/// \brief Note that the identifier II occurs at the given offset
3159/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003160void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003161 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003162 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003163 // up earlier in the chain and thus don't need an offset.
3164 if (ID >= FirstIdentID)
3165 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003166}
3167
Douglas Gregor83941df2009-04-25 17:48:32 +00003168/// \brief Note that the selector Sel occurs at the given offset
3169/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003170void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003171 unsigned ID = SelectorIDs[Sel];
3172 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003173 // Don't record offsets for selectors that are also available in a different
3174 // file.
3175 if (ID < FirstSelectorID)
3176 return;
3177 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003178}
3179
Sebastian Redla4232eb2010-08-18 23:56:21 +00003180ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003181 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003182 WritingAST(false), DoneWritingDeclsAndTypes(false),
3183 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003184 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003185 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003186 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003187 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3188 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003189 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003190 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003191 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003192 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003193 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003194 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003195 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3196 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3197 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003198 DeclTypedefAbbrev(0),
3199 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3200 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003201{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003202}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003203
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003204ASTWriter::~ASTWriter() {
3205 for (FileDeclIDsTy::iterator
3206 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3207 delete I->second;
3208}
3209
Sebastian Redla4232eb2010-08-18 23:56:21 +00003210void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003211 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003212 Module *WritingModule, StringRef isysroot,
3213 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003214 WritingAST = true;
3215
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003216 ASTHasCompilerErrors = hasErrors;
3217
Douglas Gregor2cf26342009-04-09 22:27:44 +00003218 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003219 Stream.Emit((unsigned)'C', 8);
3220 Stream.Emit((unsigned)'P', 8);
3221 Stream.Emit((unsigned)'C', 8);
3222 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003223
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003224 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003225
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003226 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003227 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003228 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003229 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003230 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003231 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003232 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003233
3234 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003235}
3236
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003237template<typename Vector>
3238static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3239 ASTWriter::RecordData &Record) {
3240 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3241 I != E; ++I) {
3242 Writer.AddDeclRef(*I, Record);
3243 }
3244}
3245
Sebastian Redla4232eb2010-08-18 23:56:21 +00003246void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003247 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003248 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003249 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003250 using namespace llvm;
3251
Douglas Gregorecc2c092011-12-01 22:20:10 +00003252 // Make sure that the AST reader knows to finalize itself.
3253 if (Chain)
3254 Chain->finalizeForWriting();
3255
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003256 ASTContext &Context = SemaRef.Context;
3257 Preprocessor &PP = SemaRef.PP;
3258
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003259 // Set up predefined declaration IDs.
3260 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003261 if (Context.ObjCIdDecl)
3262 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003263 if (Context.ObjCSelDecl)
3264 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003265 if (Context.ObjCClassDecl)
3266 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003267 if (Context.ObjCProtocolClassDecl)
3268 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003269 if (Context.Int128Decl)
3270 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3271 if (Context.UInt128Decl)
3272 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003273 if (Context.ObjCInstanceTypeDecl)
3274 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003275 if (Context.BuiltinVaListDecl)
3276 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3277
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003278 if (!Chain) {
3279 // Make sure that we emit IdentifierInfos (and any attached
3280 // declarations) for builtins. We don't need to do this when we're
3281 // emitting chained PCH files, because all of the builtins will be
3282 // in the original PCH file.
3283 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003284 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003285 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003286 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003287 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003288 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3289 getIdentifierRef(&Table.get(BuiltinNames[I]));
3290 }
3291
Douglas Gregoreee242f2011-10-27 09:33:13 +00003292 // If there are any out-of-date identifiers, bring them up to date.
3293 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3294 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3295 IDEnd = PP.getIdentifierTable().end();
3296 ID != IDEnd; ++ID)
3297 if (ID->second->isOutOfDate())
3298 ExtSource->updateOutOfDateIdentifier(*ID->second);
3299 }
3300
Chris Lattner63d65f82009-09-08 18:19:27 +00003301 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003302 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003303 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003304 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003305 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003306
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003307 // Build a record containing all of the file scoped decls in this file.
3308 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003309 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3310 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003311
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003312 // Build a record containing all of the delegating constructors we still need
3313 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003314 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003315 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003316
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003317 // Write the set of weak, undeclared identifiers. We always write the
3318 // entire table, since later PCH files in a PCH chain are only interested in
3319 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003320 RecordData WeakUndeclaredIdentifiers;
3321 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003322 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003323 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3324 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3325 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3326 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3327 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3328 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3329 }
3330 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003331
Douglas Gregor14c22f22009-04-22 22:18:58 +00003332 // Build a record containing all of the locally-scoped external
3333 // declarations in this header file. Generally, this record will be
3334 // empty.
3335 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003336 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003337 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003338 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003339 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3340 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003341 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003342 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003343 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3344 }
3345
Douglas Gregorb81c1702009-04-27 20:06:05 +00003346 // Build a record containing all of the ext_vector declarations.
3347 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003348 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003349
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003350 // Build a record containing all of the VTable uses information.
3351 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003352 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003353 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3354 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3355 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3356 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3357 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003358 }
3359
3360 // Build a record containing all of dynamic classes declarations.
3361 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003362 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003363
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003364 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003365 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003366 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003367 I = SemaRef.PendingInstantiations.begin(),
3368 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3369 AddDeclRef(I->first, PendingInstantiations);
3370 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003371 }
3372 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3373 "There are local ones at end of translation unit!");
3374
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003375 // Build a record containing some declaration references.
3376 RecordData SemaDeclRefs;
3377 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3378 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3379 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3380 }
3381
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003382 RecordData CUDASpecialDeclRefs;
3383 if (Context.getcudaConfigureCallDecl()) {
3384 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3385 }
3386
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003387 // Build a record containing all of the known namespaces.
3388 RecordData KnownNamespaces;
3389 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3390 I = SemaRef.KnownNamespaces.begin(),
3391 IEnd = SemaRef.KnownNamespaces.end();
3392 I != IEnd; ++I) {
3393 if (!I->second)
3394 AddDeclRef(I->first, KnownNamespaces);
3395 }
3396
Sebastian Redl3397c552010-08-18 23:56:27 +00003397 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003398 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003399 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003400 WriteMetadata(Context, isysroot, OutputFile);
David Blaikie4e4d0842012-03-11 07:00:24 +00003401 WriteLanguageOptions(Context.getLangOpts());
Douglas Gregor832d6202011-07-22 16:35:34 +00003402 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003403 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003404
3405 // Create a lexical update block containing all of the declarations in the
3406 // translation unit that do not come from other AST files.
3407 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3408 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3409 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3410 E = TU->noload_decls_end();
3411 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003412 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003413 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003414 }
3415
3416 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3417 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3418 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3419 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3420 Record.clear();
3421 Record.push_back(TU_UPDATE_LEXICAL);
3422 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3423 data(NewGlobalDecls));
3424
3425 // And a visible updates block for the translation unit.
3426 Abv = new llvm::BitCodeAbbrev();
3427 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3428 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3429 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3430 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3431 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3432 WriteDeclContextVisibleUpdate(TU);
3433
3434 // If the translation unit has an anonymous namespace, and we don't already
3435 // have an update block for it, write it as an update block.
3436 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3437 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3438 if (Record.empty()) {
3439 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003440 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003441 }
3442 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003443
3444 // Make sure visible decls, added to DeclContexts previously loaded from
3445 // an AST file, are registered for serialization.
3446 for (SmallVector<const Decl *, 16>::iterator
3447 I = UpdatingVisibleDecls.begin(),
3448 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3449 GetDeclRef(*I);
3450 }
3451
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003452 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003453 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003454
Douglas Gregora119da02011-08-02 16:26:37 +00003455 // Form the record of special types.
3456 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003457 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003458 AddTypeRef(Context.getFILEType(), SpecialTypes);
3459 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3460 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3461 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3462 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003463 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003464 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003465
Douglas Gregor366809a2009-04-26 03:49:13 +00003466 // Keep writing types and declarations until all types and
3467 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003468 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003469 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003470 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3471 E = DeclsToRewrite.end();
3472 I != E; ++I)
3473 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003474 while (!DeclTypesToEmit.empty()) {
3475 DeclOrType DOT = DeclTypesToEmit.front();
3476 DeclTypesToEmit.pop();
3477 if (DOT.isType())
3478 WriteType(DOT.getType());
3479 else
3480 WriteDecl(Context, DOT.getDecl());
3481 }
3482 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003483
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003484 DoneWritingDeclsAndTypes = true;
3485
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003486 WriteFileDeclIDsMap();
3487 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003488 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003489
3490 if (Chain) {
3491 // Write the mapping information describing our module dependencies and how
3492 // each of those modules were mapped into our own offset/ID space, so that
3493 // the reader can build the appropriate mapping to its own offset/ID space.
3494 // The map consists solely of a blob with the following format:
3495 // *(module-name-len:i16 module-name:len*i8
3496 // source-location-offset:i32
3497 // identifier-id:i32
3498 // preprocessed-entity-id:i32
3499 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003500 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003501 // selector-id:i32
3502 // declaration-id:i32
3503 // c++-base-specifiers-id:i32
3504 // type-id:i32)
3505 //
3506 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3507 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3508 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3509 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003510 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003511 {
3512 llvm::raw_svector_ostream Out(Buffer);
3513 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003514 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003515 M != MEnd; ++M) {
3516 StringRef FileName = (*M)->FileName;
3517 io::Emit16(Out, FileName.size());
3518 Out.write(FileName.data(), FileName.size());
3519 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3520 io::Emit32(Out, (*M)->BaseIdentifierID);
3521 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003522 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003523 io::Emit32(Out, (*M)->BaseSelectorID);
3524 io::Emit32(Out, (*M)->BaseDeclID);
3525 io::Emit32(Out, (*M)->BaseTypeIndex);
3526 }
3527 }
3528 Record.clear();
3529 Record.push_back(MODULE_OFFSET_MAP);
3530 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3531 Buffer.data(), Buffer.size());
3532 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003533 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003534 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003535 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003536 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003537 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003538 WriteFPPragmaOptions(SemaRef.getFPOptions());
3539 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003540
Sebastian Redl1476ed42010-07-16 16:36:56 +00003541 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003542 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003543
Anders Carlssonc8505782011-03-06 18:41:18 +00003544 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003545
Douglas Gregore209e502011-12-06 01:10:29 +00003546 // If we're emitting a module, write out the submodule information.
3547 if (WritingModule)
3548 WriteSubmodules(WritingModule);
3549
Douglas Gregora119da02011-08-02 16:26:37 +00003550 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3551
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003552 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003553 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003554 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003555
3556 // Write the record containing tentative definitions.
3557 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003558 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003559
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003560 // Write the record containing unused file scoped decls.
3561 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003562 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003563
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003564 // Write the record containing weak undeclared identifiers.
3565 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003566 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003567 WeakUndeclaredIdentifiers);
3568
Douglas Gregor14c22f22009-04-22 22:18:58 +00003569 // Write the record containing locally-scoped external definitions.
3570 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003571 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003572 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003573
3574 // Write the record containing ext_vector type names.
3575 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003576 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003577
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003578 // Write the record containing VTable uses information.
3579 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003580 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003581
3582 // Write the record containing dynamic classes declarations.
3583 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003584 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003585
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003586 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003587 if (!PendingInstantiations.empty())
3588 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003589
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003590 // Write the record containing declaration references of Sema.
3591 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003592 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003593
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003594 // Write the record containing CUDA-specific declaration references.
3595 if (!CUDASpecialDeclRefs.empty())
3596 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003597
3598 // Write the delegating constructors.
3599 if (!DelegatingCtorDecls.empty())
3600 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003601
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003602 // Write the known namespaces.
3603 if (!KnownNamespaces.empty())
3604 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3605
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003606 // Write the visible updates to DeclContexts.
3607 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3608 I = UpdatedDeclContexts.begin(),
3609 E = UpdatedDeclContexts.end();
3610 I != E; ++I)
3611 WriteDeclContextVisibleUpdate(*I);
3612
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003613 if (!WritingModule) {
3614 // Write the submodules that were imported, if any.
3615 RecordData ImportedModules;
3616 for (ASTContext::import_iterator I = Context.local_import_begin(),
3617 IEnd = Context.local_import_end();
3618 I != IEnd; ++I) {
3619 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3620 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3621 }
3622 if (!ImportedModules.empty()) {
3623 // Sort module IDs.
3624 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3625
3626 // Unique module IDs.
3627 ImportedModules.erase(std::unique(ImportedModules.begin(),
3628 ImportedModules.end()),
3629 ImportedModules.end());
3630
3631 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3632 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003633 }
3634
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003635 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003636 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003637 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003638 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003639 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003640
Douglas Gregor3e1af842009-04-17 22:13:46 +00003641 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003642 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003643 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003644 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003645 Record.push_back(NumLexicalDeclContexts);
3646 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003647 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003648 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003649}
3650
Douglas Gregor61c5e342011-09-17 00:05:03 +00003651/// \brief Go through the declaration update blocks and resolve declaration
3652/// pointers into declaration IDs.
3653void ASTWriter::ResolveDeclUpdatesBlocks() {
3654 for (DeclUpdateMap::iterator
3655 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3656 const Decl *D = I->first;
3657 UpdateRecord &URec = I->second;
3658
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003659 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003660 continue; // The decl will be written completely
3661
3662 unsigned Idx = 0, N = URec.size();
3663 while (Idx < N) {
3664 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003665 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3666 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3667 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3668 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3669 ++Idx;
3670 break;
3671
3672 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3673 ++Idx;
3674 break;
3675 }
3676 }
3677 }
3678}
3679
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003680void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003681 if (DeclUpdates.empty())
3682 return;
3683
3684 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003685 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003686 for (DeclUpdateMap::iterator
3687 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3688 const Decl *D = I->first;
3689 UpdateRecord &URec = I->second;
3690
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003691 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003692 continue; // The decl will be written completely,no need to store updates.
3693
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003694 uint64_t Offset = Stream.GetCurrentBitNo();
3695 Stream.EmitRecord(DECL_UPDATES, URec);
3696
3697 OffsetsRecord.push_back(GetDeclRef(D));
3698 OffsetsRecord.push_back(Offset);
3699 }
3700 Stream.ExitBlock();
3701 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3702}
3703
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003704void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003705 if (ReplacedDecls.empty())
3706 return;
3707
3708 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003709 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003710 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003711 Record.push_back(I->ID);
3712 Record.push_back(I->Offset);
3713 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003714 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003715 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003716}
3717
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003718void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003719 Record.push_back(Loc.getRawEncoding());
3720}
3721
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003722void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003723 AddSourceLocation(Range.getBegin(), Record);
3724 AddSourceLocation(Range.getEnd(), Record);
3725}
3726
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003727void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003728 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003729 const uint64_t *Words = Value.getRawData();
3730 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003731}
3732
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003733void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003734 Record.push_back(Value.isUnsigned());
3735 AddAPInt(Value, Record);
3736}
3737
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003738void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003739 AddAPInt(Value.bitcastToAPInt(), Record);
3740}
3741
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003742void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003743 Record.push_back(getIdentifierRef(II));
3744}
3745
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003746IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003747 if (II == 0)
3748 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003749
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003750 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003751 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003752 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003753 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003754}
3755
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003756void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003757 Record.push_back(getSelectorRef(SelRef));
3758}
3759
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003760SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003761 if (Sel.getAsOpaquePtr() == 0) {
3762 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003763 }
3764
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003765 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003766 if (SID == 0 && Chain) {
3767 // This might trigger a ReadSelector callback, which will set the ID for
3768 // this selector.
3769 Chain->LoadSelector(Sel);
3770 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003771 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003772 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003773 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003774 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003775}
3776
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003777void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003778 AddDeclRef(Temp->getDestructor(), Record);
3779}
3780
Douglas Gregor7c789c12010-10-29 22:39:52 +00003781void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3782 CXXBaseSpecifier const *BasesEnd,
3783 RecordDataImpl &Record) {
3784 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3785 CXXBaseSpecifiersToWrite.push_back(
3786 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3787 Bases, BasesEnd));
3788 Record.push_back(NextCXXBaseSpecifiersID++);
3789}
3790
Sebastian Redla4232eb2010-08-18 23:56:21 +00003791void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003792 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003793 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003794 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003795 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003796 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003797 break;
3798 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003799 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003800 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003801 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003802 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003803 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003804 break;
3805 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003806 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003807 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003808 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003809 break;
John McCall833ca992009-10-29 08:12:44 +00003810 case TemplateArgument::Null:
3811 case TemplateArgument::Integral:
3812 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003813 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003814 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003815 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003816 break;
3817 }
3818}
3819
Sebastian Redla4232eb2010-08-18 23:56:21 +00003820void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003821 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003822 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003823
3824 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3825 bool InfoHasSameExpr
3826 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3827 Record.push_back(InfoHasSameExpr);
3828 if (InfoHasSameExpr)
3829 return; // Avoid storing the same expr twice.
3830 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003831 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3832 Record);
3833}
3834
Douglas Gregordc355712011-02-25 00:36:19 +00003835void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3836 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003837 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003838 AddTypeRef(QualType(), Record);
3839 return;
3840 }
3841
Douglas Gregordc355712011-02-25 00:36:19 +00003842 AddTypeLoc(TInfo->getTypeLoc(), Record);
3843}
3844
3845void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3846 AddTypeRef(TL.getType(), Record);
3847
John McCalla1ee0c52009-10-16 21:56:05 +00003848 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003849 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003850 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003851}
3852
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003853void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003854 Record.push_back(GetOrCreateTypeID(T));
3855}
3856
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003857TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3858 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003859 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3860}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003861
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003862TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003863 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003864 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003865}
3866
3867TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3868 if (T.isNull())
3869 return TypeIdx();
3870 assert(!T.getLocalFastQualifiers());
3871
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003872 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003873 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003874 if (DoneWritingDeclsAndTypes) {
3875 assert(0 && "New type seen after serializing all the types to emit!");
3876 return TypeIdx();
3877 }
3878
Douglas Gregor366809a2009-04-26 03:49:13 +00003879 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003880 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003881 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003882 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003883 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003884 return Idx;
3885}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003886
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003887TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003888 if (T.isNull())
3889 return TypeIdx();
3890 assert(!T.getLocalFastQualifiers());
3891
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003892 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3893 assert(I != TypeIdxs.end() && "Type not emitted!");
3894 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003895}
3896
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003897void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003898 Record.push_back(GetDeclRef(D));
3899}
3900
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003901DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003902 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3903
Douglas Gregor2cf26342009-04-09 22:27:44 +00003904 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003905 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003906 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003907
3908 // If D comes from an AST file, its declaration ID is already known and
3909 // fixed.
3910 if (D->isFromASTFile())
3911 return D->getGlobalID();
3912
Douglas Gregor97475832010-10-05 18:37:06 +00003913 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003914 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003915 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003916 if (DoneWritingDeclsAndTypes) {
3917 assert(0 && "New decl seen after serializing all the decls to emit!");
3918 return 0;
3919 }
3920
Douglas Gregor2cf26342009-04-09 22:27:44 +00003921 // We haven't seen this declaration before. Give it a new ID and
3922 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003923 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003924 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003925 }
3926
Sebastian Redl681d7232010-07-27 00:17:23 +00003927 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003928}
3929
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003930DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003931 if (D == 0)
3932 return 0;
3933
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003934 // If D comes from an AST file, its declaration ID is already known and
3935 // fixed.
3936 if (D->isFromASTFile())
3937 return D->getGlobalID();
3938
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003939 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3940 return DeclIDs[D];
3941}
3942
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003943static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3944 std::pair<unsigned, serialization::DeclID> R) {
3945 return L.first < R.first;
3946}
3947
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003948void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003949 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003950 assert(D);
3951
3952 SourceLocation Loc = D->getLocation();
3953 if (Loc.isInvalid())
3954 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003955
3956 // We only keep track of the file-level declarations of each file.
3957 if (!D->getLexicalDeclContext()->isFileContext())
3958 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00003959 // FIXME: ParmVarDecls that are part of a function type of a parameter of
3960 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00003961 if (isa<ParmVarDecl>(D))
3962 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003963
3964 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003965 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003966 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003967 FileID FID;
3968 unsigned Offset;
3969 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003970 if (FID.isInvalid())
3971 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00003972 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003973
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00003974 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003975 if (!Info)
3976 Info = new DeclIDInFileInfo();
3977
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003978 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003979 LocDeclIDsTy &Decls = Info->DeclIDs;
3980
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003981 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003982 Decls.push_back(LocDecl);
3983 return;
3984 }
3985
3986 LocDeclIDsTy::iterator
3987 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3988
3989 Decls.insert(I, LocDecl);
3990}
3991
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003992void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003993 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003994 Record.push_back(Name.getNameKind());
3995 switch (Name.getNameKind()) {
3996 case DeclarationName::Identifier:
3997 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3998 break;
3999
4000 case DeclarationName::ObjCZeroArgSelector:
4001 case DeclarationName::ObjCOneArgSelector:
4002 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004003 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004004 break;
4005
4006 case DeclarationName::CXXConstructorName:
4007 case DeclarationName::CXXDestructorName:
4008 case DeclarationName::CXXConversionFunctionName:
4009 AddTypeRef(Name.getCXXNameType(), Record);
4010 break;
4011
4012 case DeclarationName::CXXOperatorName:
4013 Record.push_back(Name.getCXXOverloadedOperator());
4014 break;
4015
Sean Hunt3e518bd2009-11-29 07:34:05 +00004016 case DeclarationName::CXXLiteralOperatorName:
4017 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4018 break;
4019
Douglas Gregor2cf26342009-04-09 22:27:44 +00004020 case DeclarationName::CXXUsingDirective:
4021 // No extra data to emit
4022 break;
4023 }
4024}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004025
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004026void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004027 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004028 switch (Name.getNameKind()) {
4029 case DeclarationName::CXXConstructorName:
4030 case DeclarationName::CXXDestructorName:
4031 case DeclarationName::CXXConversionFunctionName:
4032 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4033 break;
4034
4035 case DeclarationName::CXXOperatorName:
4036 AddSourceLocation(
4037 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4038 Record);
4039 AddSourceLocation(
4040 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4041 Record);
4042 break;
4043
4044 case DeclarationName::CXXLiteralOperatorName:
4045 AddSourceLocation(
4046 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4047 Record);
4048 break;
4049
4050 case DeclarationName::Identifier:
4051 case DeclarationName::ObjCZeroArgSelector:
4052 case DeclarationName::ObjCOneArgSelector:
4053 case DeclarationName::ObjCMultiArgSelector:
4054 case DeclarationName::CXXUsingDirective:
4055 break;
4056 }
4057}
4058
4059void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004060 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004061 AddDeclarationName(NameInfo.getName(), Record);
4062 AddSourceLocation(NameInfo.getLoc(), Record);
4063 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4064}
4065
4066void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004067 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004068 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004069 Record.push_back(Info.NumTemplParamLists);
4070 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4071 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4072}
4073
Sebastian Redla4232eb2010-08-18 23:56:21 +00004074void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004075 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004076 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004077 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004078 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004079
4080 // Push each of the NNS's onto a stack for serialization in reverse order.
4081 while (NNS) {
4082 NestedNames.push_back(NNS);
4083 NNS = NNS->getPrefix();
4084 }
4085
4086 Record.push_back(NestedNames.size());
4087 while(!NestedNames.empty()) {
4088 NNS = NestedNames.pop_back_val();
4089 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4090 Record.push_back(Kind);
4091 switch (Kind) {
4092 case NestedNameSpecifier::Identifier:
4093 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4094 break;
4095
4096 case NestedNameSpecifier::Namespace:
4097 AddDeclRef(NNS->getAsNamespace(), Record);
4098 break;
4099
Douglas Gregor14aba762011-02-24 02:36:08 +00004100 case NestedNameSpecifier::NamespaceAlias:
4101 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4102 break;
4103
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004104 case NestedNameSpecifier::TypeSpec:
4105 case NestedNameSpecifier::TypeSpecWithTemplate:
4106 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4107 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4108 break;
4109
4110 case NestedNameSpecifier::Global:
4111 // Don't need to write an associated value.
4112 break;
4113 }
4114 }
4115}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004116
Douglas Gregordc355712011-02-25 00:36:19 +00004117void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4118 RecordDataImpl &Record) {
4119 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004120 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004121 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004122
4123 // Push each of the nested-name-specifiers's onto a stack for
4124 // serialization in reverse order.
4125 while (NNS) {
4126 NestedNames.push_back(NNS);
4127 NNS = NNS.getPrefix();
4128 }
4129
4130 Record.push_back(NestedNames.size());
4131 while(!NestedNames.empty()) {
4132 NNS = NestedNames.pop_back_val();
4133 NestedNameSpecifier::SpecifierKind Kind
4134 = NNS.getNestedNameSpecifier()->getKind();
4135 Record.push_back(Kind);
4136 switch (Kind) {
4137 case NestedNameSpecifier::Identifier:
4138 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4139 AddSourceRange(NNS.getLocalSourceRange(), Record);
4140 break;
4141
4142 case NestedNameSpecifier::Namespace:
4143 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4144 AddSourceRange(NNS.getLocalSourceRange(), Record);
4145 break;
4146
4147 case NestedNameSpecifier::NamespaceAlias:
4148 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4149 AddSourceRange(NNS.getLocalSourceRange(), Record);
4150 break;
4151
4152 case NestedNameSpecifier::TypeSpec:
4153 case NestedNameSpecifier::TypeSpecWithTemplate:
4154 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4155 AddTypeLoc(NNS.getTypeLoc(), Record);
4156 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4157 break;
4158
4159 case NestedNameSpecifier::Global:
4160 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4161 break;
4162 }
4163 }
4164}
4165
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004166void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004167 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004168 Record.push_back(Kind);
4169 switch (Kind) {
4170 case TemplateName::Template:
4171 AddDeclRef(Name.getAsTemplateDecl(), Record);
4172 break;
4173
4174 case TemplateName::OverloadedTemplate: {
4175 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4176 Record.push_back(OvT->size());
4177 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4178 I != E; ++I)
4179 AddDeclRef(*I, Record);
4180 break;
4181 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004182
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004183 case TemplateName::QualifiedTemplate: {
4184 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4185 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4186 Record.push_back(QualT->hasTemplateKeyword());
4187 AddDeclRef(QualT->getTemplateDecl(), Record);
4188 break;
4189 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004190
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004191 case TemplateName::DependentTemplate: {
4192 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4193 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4194 Record.push_back(DepT->isIdentifier());
4195 if (DepT->isIdentifier())
4196 AddIdentifierRef(DepT->getIdentifier(), Record);
4197 else
4198 Record.push_back(DepT->getOperator());
4199 break;
4200 }
John McCall14606042011-06-30 08:33:18 +00004201
4202 case TemplateName::SubstTemplateTemplateParm: {
4203 SubstTemplateTemplateParmStorage *subst
4204 = Name.getAsSubstTemplateTemplateParm();
4205 AddDeclRef(subst->getParameter(), Record);
4206 AddTemplateName(subst->getReplacement(), Record);
4207 break;
4208 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004209
4210 case TemplateName::SubstTemplateTemplateParmPack: {
4211 SubstTemplateTemplateParmPackStorage *SubstPack
4212 = Name.getAsSubstTemplateTemplateParmPack();
4213 AddDeclRef(SubstPack->getParameterPack(), Record);
4214 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4215 break;
4216 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004217 }
4218}
4219
Michael J. Spencer20249a12010-10-21 03:16:25 +00004220void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004221 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004222 Record.push_back(Arg.getKind());
4223 switch (Arg.getKind()) {
4224 case TemplateArgument::Null:
4225 break;
4226 case TemplateArgument::Type:
4227 AddTypeRef(Arg.getAsType(), Record);
4228 break;
4229 case TemplateArgument::Declaration:
4230 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004231 Record.push_back(Arg.isDeclForReferenceParam());
4232 break;
4233 case TemplateArgument::NullPtr:
4234 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004235 break;
4236 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004237 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004238 AddTypeRef(Arg.getIntegralType(), Record);
4239 break;
4240 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004241 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4242 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004243 case TemplateArgument::TemplateExpansion:
4244 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004245 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4246 Record.push_back(*NumExpansions + 1);
4247 else
4248 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004249 break;
4250 case TemplateArgument::Expression:
4251 AddStmt(Arg.getAsExpr());
4252 break;
4253 case TemplateArgument::Pack:
4254 Record.push_back(Arg.pack_size());
4255 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4256 I != E; ++I)
4257 AddTemplateArgument(*I, Record);
4258 break;
4259 }
4260}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004261
4262void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004263ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004264 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004265 assert(TemplateParams && "No TemplateParams!");
4266 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4267 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4268 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4269 Record.push_back(TemplateParams->size());
4270 for (TemplateParameterList::const_iterator
4271 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4272 P != PEnd; ++P)
4273 AddDeclRef(*P, Record);
4274}
4275
4276/// \brief Emit a template argument list.
4277void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004278ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004279 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004280 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004281 Record.push_back(TemplateArgs->size());
4282 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004283 AddTemplateArgument(TemplateArgs->get(i), Record);
4284}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004285
4286
4287void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004288ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004289 Record.push_back(Set.size());
4290 for (UnresolvedSetImpl::const_iterator
4291 I = Set.begin(), E = Set.end(); I != E; ++I) {
4292 AddDeclRef(I.getDecl(), Record);
4293 Record.push_back(I.getAccess());
4294 }
4295}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004296
Sebastian Redla4232eb2010-08-18 23:56:21 +00004297void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004298 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004299 Record.push_back(Base.isVirtual());
4300 Record.push_back(Base.isBaseOfClass());
4301 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004302 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004303 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004304 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004305 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4306 : SourceLocation(),
4307 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004308}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004309
Douglas Gregor7c789c12010-10-29 22:39:52 +00004310void ASTWriter::FlushCXXBaseSpecifiers() {
4311 RecordData Record;
4312 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4313 Record.clear();
4314
4315 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004316 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004317 if (Index == CXXBaseSpecifiersOffsets.size())
4318 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4319 else {
4320 if (Index > CXXBaseSpecifiersOffsets.size())
4321 CXXBaseSpecifiersOffsets.resize(Index + 1);
4322 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4323 }
4324
4325 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4326 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4327 Record.push_back(BEnd - B);
4328 for (; B != BEnd; ++B)
4329 AddCXXBaseSpecifier(*B, Record);
4330 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004331
4332 // Flush any expressions that were written as part of the base specifiers.
4333 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004334 }
4335
4336 CXXBaseSpecifiersToWrite.clear();
4337}
4338
Sean Huntcbb67482011-01-08 20:30:50 +00004339void ASTWriter::AddCXXCtorInitializers(
4340 const CXXCtorInitializer * const *CtorInitializers,
4341 unsigned NumCtorInitializers,
4342 RecordDataImpl &Record) {
4343 Record.push_back(NumCtorInitializers);
4344 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4345 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004346
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004347 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004348 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004349 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004350 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004351 } else if (Init->isDelegatingInitializer()) {
4352 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004353 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004354 } else if (Init->isMemberInitializer()){
4355 Record.push_back(CTOR_INITIALIZER_MEMBER);
4356 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004357 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004358 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4359 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004360 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004361
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004362 AddSourceLocation(Init->getMemberLocation(), Record);
4363 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004364 AddSourceLocation(Init->getLParenLoc(), Record);
4365 AddSourceLocation(Init->getRParenLoc(), Record);
4366 Record.push_back(Init->isWritten());
4367 if (Init->isWritten()) {
4368 Record.push_back(Init->getSourceOrder());
4369 } else {
4370 Record.push_back(Init->getNumArrayIndices());
4371 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4372 AddDeclRef(Init->getArrayIndex(i), Record);
4373 }
4374 }
4375}
4376
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004377void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4378 assert(D->DefinitionData);
4379 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004380 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004381 Record.push_back(Data.UserDeclaredConstructor);
4382 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004383 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004384 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004385 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004386 Record.push_back(Data.UserDeclaredDestructor);
4387 Record.push_back(Data.Aggregate);
4388 Record.push_back(Data.PlainOldData);
4389 Record.push_back(Data.Empty);
4390 Record.push_back(Data.Polymorphic);
4391 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004392 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004393 Record.push_back(Data.HasNoNonEmptyBases);
4394 Record.push_back(Data.HasPrivateFields);
4395 Record.push_back(Data.HasProtectedFields);
4396 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004397 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004398 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004399 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004400 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004401 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004402 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004403 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004404 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004405 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004406 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004407 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004408 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004409 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004410 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004411 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004412 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004413 Record.push_back(Data.DeclaredDefaultConstructor);
4414 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004415 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004416 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004417 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004418 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004419 Record.push_back(Data.FailedImplicitMoveConstructor);
4420 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004421 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004422
4423 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004424 if (Data.NumBases > 0)
4425 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4426 Record);
4427
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004428 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4429 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004430 if (Data.NumVBases > 0)
4431 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4432 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004433
4434 AddUnresolvedSet(Data.Conversions, Record);
4435 AddUnresolvedSet(Data.VisibleConversions, Record);
4436 // Data.Definition is the owning decl, no need to write it.
4437 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004438
4439 // Add lambda-specific data.
4440 if (Data.IsLambda) {
4441 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004442 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004443 Record.push_back(Lambda.NumCaptures);
4444 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004445 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004446 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004447 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004448 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4449 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4450 AddSourceLocation(Capture.getLocation(), Record);
4451 Record.push_back(Capture.isImplicit());
4452 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4453 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4454 AddDeclRef(Var, Record);
4455 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4456 : SourceLocation(),
4457 Record);
4458 }
4459 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004460}
4461
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004462void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004463 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004464 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004465 assert(FirstDeclID == NextDeclID &&
4466 FirstTypeID == NextTypeID &&
4467 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004468 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004469 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004470 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004471
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004472 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004473
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004474 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4475 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4476 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004477 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004478 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004479 NextDeclID = FirstDeclID;
4480 NextTypeID = FirstTypeID;
4481 NextIdentID = FirstIdentID;
4482 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004483 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004484}
4485
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004486void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004487 IdentifierIDs[II] = ID;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00004488 if (II->hadMacroDefinition())
Douglas Gregor040a8042011-02-11 00:26:14 +00004489 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004490}
4491
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004492void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004493 // Always take the highest-numbered type index. This copes with an interesting
4494 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004495 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004496 // keep the higher-numbered entry so that we can properly write it out to
4497 // the AST file.
4498 TypeIdx &StoredIdx = TypeIdxs[T];
4499 if (Idx.getIndex() >= StoredIdx.getIndex())
4500 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004501}
4502
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004503void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004504 SelectorIDs[S] = ID;
4505}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004506
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004507void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004508 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004509 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004510 MacroDefinitions[MD] = ID;
4511}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004512
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004513void ASTWriter::MacroVisible(IdentifierInfo *II) {
4514 DeserializedMacroNames.push_back(II);
4515}
4516
Douglas Gregora015cab2011-12-02 17:30:13 +00004517void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4518 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4519 SubmoduleIDs[Mod] = ID;
4520}
4521
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004522void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004523 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004524 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004525 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4526 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004527 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004528 // A forward reference was mutated into a definition. Rewrite it.
4529 // FIXME: This happens during template instantiation, should we
4530 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004531 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004532 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004533 }
4534}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004535void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004536 assert(!WritingAST && "Already writing the AST!");
4537
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004538 // TU and namespaces are handled elsewhere.
4539 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4540 return;
4541
Douglas Gregor919814d2011-09-09 23:01:35 +00004542 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004543 return; // Not a source decl added to a DeclContext from PCH.
4544
4545 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004546 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004547}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004548
4549void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004550 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004551 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004552 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004553 return; // Not a source member added to a class from PCH.
4554 if (!isa<CXXMethodDecl>(D))
4555 return; // We are interested in lazily declared implicit methods.
4556
4557 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004558 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004559 UpdateRecord &Record = DeclUpdates[RD];
4560 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004561 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004562}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004563
4564void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4565 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004566 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004567 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004568 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004569 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004570 return; // Not a source specialization added to a template from PCH.
4571
4572 UpdateRecord &Record = DeclUpdates[TD];
4573 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004574 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004575}
Douglas Gregor89d99802010-11-30 06:16:57 +00004576
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004577void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4578 const FunctionDecl *D) {
4579 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004580 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004581 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004582 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004583 return; // Not a source specialization added to a template from PCH.
4584
4585 UpdateRecord &Record = DeclUpdates[TD];
4586 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004587 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004588}
4589
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004590void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004591 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004592 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004593 return; // Declaration not imported from PCH.
4594
4595 // Implicit decl from a PCH was defined.
4596 // FIXME: Should implicit definition be a separate FunctionDecl?
4597 RewriteDecl(D);
4598}
4599
Sebastian Redlf79a7192011-04-29 08:19:30 +00004600void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004601 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004602 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004603 return;
4604
4605 // Since the actual instantiation is delayed, this really means that we need
4606 // to update the instantiation location.
4607 UpdateRecord &Record = DeclUpdates[D];
4608 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4609 AddSourceLocation(
4610 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4611}
4612
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004613void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4614 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004615 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004616 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004617 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004618
4619 assert(IFD->getDefinition() && "Category on a class without a definition?");
4620 ObjCClassesWithCategories.insert(
4621 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004622}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004623
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004624
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004625void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4626 const ObjCPropertyDecl *OrigProp,
4627 const ObjCCategoryDecl *ClassExt) {
4628 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4629 if (!D)
4630 return;
4631
4632 assert(!WritingAST && "Already writing the AST!");
4633 if (!D->isFromASTFile())
4634 return; // Declaration not imported from PCH.
4635
4636 RewriteDecl(D);
4637}