blob: e37aa7320fe3b957e3b686d91c7e609b0ebe9d52 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-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 Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053
Sebastian Redl3df5a082010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-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 Kramerd47a12a2011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000059}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000065}
66
Douglas Gregoref84c4b2009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000070
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-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 Gregoref84c4b2009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100}
101
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116}
117
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121}
122
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000127}
128
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133}
134
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139}
140
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175}
176
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180}
181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
188 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000189 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000190 Record.push_back(T->getExceptionSpecType());
191 if (T->getExceptionSpecType() == EST_Dynamic) {
192 Record.push_back(T->getNumExceptions());
193 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
194 Writer.AddTypeRef(T->getExceptionType(I), Record);
195 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
196 Writer.AddStmt(T->getNoexceptExpr());
197 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000198 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000202 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000204}
John McCallb96ec562009-12-04 22:46:56 +0000205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000208 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
209 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000210 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000211}
212
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000214 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000215 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216}
217
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000220 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000221}
222
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000225 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000226}
227
Alexis Hunte852b102011-05-24 22:41:36 +0000228void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
229 Writer.AddTypeRef(T->getBaseType(), Record);
230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
231 Record.push_back(T->getUTTKind());
232 Code = TYPE_UNARY_TRANSFORM;
233}
234
Richard Smith30482bc2011-02-20 03:19:35 +0000235void ASTTypeWriter::VisitAutoType(const AutoType *T) {
236 Writer.AddTypeRef(T->getDeducedType(), Record);
237 Code = TYPE_AUTO;
238}
239
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000240void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000241 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000243 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244 "Cannot serialize in the middle of a type definition");
245}
246
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000249 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250}
251
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000253 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000254 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255}
256
John McCall81904512011-01-06 01:58:22 +0000257void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
258 Writer.AddTypeRef(T->getModifiedType(), Record);
259 Writer.AddTypeRef(T->getEquivalentType(), Record);
260 Record.push_back(T->getAttrKind());
261 Code = TYPE_ATTRIBUTED;
262}
263
Mike Stump11289f42009-09-09 15:08:12 +0000264void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000266 const SubstTemplateTypeParmType *T) {
267 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
268 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000269 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000270}
271
272void
Douglas Gregorada4b792011-01-14 02:55:32 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
274 const SubstTemplateTypeParmPackType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
278}
279
280void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000281ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000282 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000283 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000284 Writer.AddTemplateName(T->getTemplateName(), Record);
285 Record.push_back(T->getNumArgs());
286 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
287 ArgI != ArgE; ++ArgI)
288 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000289 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
290 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000291 : T->getCanonicalTypeInternal(),
292 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000293 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000294}
295
296void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000297ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000298 VisitArrayType(T);
299 Writer.AddStmt(T->getSizeExpr());
300 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000301 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000306 const DependentSizedExtVectorType *T) {
307 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000308 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000313 Record.push_back(T->getDepth());
314 Record.push_back(T->getIndex());
315 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000316 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000317 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000318}
319
320void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000321ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000322 Record.push_back(T->getKeyword());
323 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
324 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000325 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
326 : T->getCanonicalTypeInternal(),
327 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000328 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000333 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000334 Record.push_back(T->getKeyword());
335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddIdentifierRef(T->getIdentifier(), Record);
337 Record.push_back(T->getNumArgs());
338 for (DependentTemplateSpecializationType::iterator
339 I = T->begin(), E = T->end(); I != E; ++I)
340 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000341 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000342}
343
Douglas Gregord2fa7662010-12-20 02:24:11 +0000344void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
345 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000346 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
347 Record.push_back(*NumExpansions + 1);
348 else
349 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000350 Code = TYPE_PACK_EXPANSION;
351}
352
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000353void ASTTypeWriter::VisitParenType(const ParenType *T) {
354 Writer.AddTypeRef(T->getInnerType(), Record);
355 Code = TYPE_PAREN;
356}
357
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000358void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000359 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000360 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
361 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000362 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000363}
364
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000366 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000367 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000368 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000369}
370
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000371void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000372 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000373 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000374}
375
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000377 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000378 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000379 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000380 E = T->qual_end(); I != E; ++I)
381 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000382 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000383}
384
Steve Narofffb4330f2009-06-17 22:40:22 +0000385void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000386ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000387 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000388 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000389}
390
Eli Friedman0dfb8892011-10-06 23:00:33 +0000391void
392ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
393 Writer.AddTypeRef(T->getValueType(), Record);
394 Code = TYPE_ATOMIC;
395}
396
John McCall8f115c62009-10-16 21:56:05 +0000397namespace {
398
399class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000400 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000401 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000402
403public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000404 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000405 : Writer(Writer), Record(Record) { }
406
John McCall17001972009-10-18 01:05:36 +0000407#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000408#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000409 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000410#include "clang/AST/TypeLocNodes.def"
411
John McCall17001972009-10-18 01:05:36 +0000412 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
413 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000414};
415
416}
417
John McCall17001972009-10-18 01:05:36 +0000418void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
419 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000420}
John McCall17001972009-10-18 01:05:36 +0000421void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000422 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
423 if (TL.needsExtraLocalData()) {
424 Record.push_back(TL.getWrittenTypeSpec());
425 Record.push_back(TL.getWrittenSignSpec());
426 Record.push_back(TL.getWrittenWidthSpec());
427 Record.push_back(TL.hasModeAttr());
428 }
John McCall8f115c62009-10-16 21:56:05 +0000429}
John McCall17001972009-10-18 01:05:36 +0000430void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000432}
John McCall17001972009-10-18 01:05:36 +0000433void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000435}
John McCall17001972009-10-18 01:05:36 +0000436void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000438}
John McCall17001972009-10-18 01:05:36 +0000439void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000441}
John McCall17001972009-10-18 01:05:36 +0000442void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000444}
John McCall17001972009-10-18 01:05:36 +0000445void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000447 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000448}
John McCall17001972009-10-18 01:05:36 +0000449void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
451 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
452 Record.push_back(TL.getSizeExpr() ? 1 : 0);
453 if (TL.getSizeExpr())
454 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000455}
John McCall17001972009-10-18 01:05:36 +0000456void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
457 VisitArrayTypeLoc(TL);
458}
459void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
460 VisitArrayTypeLoc(TL);
461}
462void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
463 VisitArrayTypeLoc(TL);
464}
465void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
466 DependentSizedArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
470 DependentSizedExtVectorTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
473void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
475}
476void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
479void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000480 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
481 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000482 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000483 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
484 Writer.AddDeclRef(TL.getArg(i), Record);
485}
486void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
487 VisitFunctionTypeLoc(TL);
488}
489void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
490 VisitFunctionTypeLoc(TL);
491}
John McCallb96ec562009-12-04 22:46:56 +0000492void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
John McCall17001972009-10-18 01:05:36 +0000495void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getNameLoc(), Record);
497}
498void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
500 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
501 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000502}
503void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000504 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
505 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
506 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
507 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000508}
509void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
Alexis Hunte852b102011-05-24 22:41:36 +0000512void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
513 Writer.AddSourceLocation(TL.getKWLoc(), Record);
514 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
515 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
516 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
517}
Richard Smith30482bc2011-02-20 03:19:35 +0000518void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
John McCall17001972009-10-18 01:05:36 +0000521void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall81904512011-01-06 01:58:22 +0000527void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
529 if (TL.hasAttrOperand()) {
530 SourceRange range = TL.getAttrOperandParensRange();
531 Writer.AddSourceLocation(range.getBegin(), Record);
532 Writer.AddSourceLocation(range.getEnd(), Record);
533 }
534 if (TL.hasAttrExprOperand()) {
535 Expr *operand = TL.getAttrExprOperand();
536 Record.push_back(operand ? 1 : 0);
537 if (operand) Writer.AddStmt(operand);
538 } else if (TL.hasAttrEnumOperand()) {
539 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
540 }
541}
John McCall17001972009-10-18 01:05:36 +0000542void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCallcebee162009-10-18 09:09:24 +0000545void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
546 SubstTemplateTypeParmTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getNameLoc(), Record);
548}
Douglas Gregorada4b792011-01-14 02:55:32 +0000549void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
550 SubstTemplateTypeParmPackTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall17001972009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
554 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000555 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000559 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
560 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000561}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000562void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
564 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
565}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000566void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000567 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000568 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000569}
John McCalle78aac42010-03-10 03:28:59 +0000570void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000573void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000574 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000575 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000576 Writer.AddSourceLocation(TL.getNameLoc(), Record);
577}
John McCallc392f372010-06-11 00:33:02 +0000578void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
579 DependentTemplateSpecializationTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
584 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
585 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000586 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
587 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000588}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000589void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
590 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
591}
John McCall17001972009-10-18 01:05:36 +0000592void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
593 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000594}
595void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
596 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
600 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000601}
John McCallfc93cf92009-10-22 22:37:11 +0000602void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000604}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000605void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getKWLoc(), Record);
607 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
608 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
609}
John McCall8f115c62009-10-16 21:56:05 +0000610
Chris Lattner19cea4e2009-04-22 05:57:30 +0000611//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000612// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000613//===----------------------------------------------------------------------===//
614
Chris Lattner28fa4e62009-04-26 22:26:21 +0000615static void EmitBlockID(unsigned ID, const char *Name,
616 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000617 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000618 Record.clear();
619 Record.push_back(ID);
620 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
621
622 // Emit the block name if present.
623 if (Name == 0 || Name[0] == 0) return;
624 Record.clear();
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
628}
629
630static void EmitRecordID(unsigned ID, const char *Name,
631 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000632 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000633 Record.clear();
634 Record.push_back(ID);
635 while (*Name)
636 Record.push_back(*Name++);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000638}
639
640static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000641 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000642#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000643 RECORD(STMT_STOP);
644 RECORD(STMT_NULL_PTR);
645 RECORD(STMT_NULL);
646 RECORD(STMT_COMPOUND);
647 RECORD(STMT_CASE);
648 RECORD(STMT_DEFAULT);
649 RECORD(STMT_LABEL);
650 RECORD(STMT_IF);
651 RECORD(STMT_SWITCH);
652 RECORD(STMT_WHILE);
653 RECORD(STMT_DO);
654 RECORD(STMT_FOR);
655 RECORD(STMT_GOTO);
656 RECORD(STMT_INDIRECT_GOTO);
657 RECORD(STMT_CONTINUE);
658 RECORD(STMT_BREAK);
659 RECORD(STMT_RETURN);
660 RECORD(STMT_DECL);
661 RECORD(STMT_ASM);
662 RECORD(EXPR_PREDEFINED);
663 RECORD(EXPR_DECL_REF);
664 RECORD(EXPR_INTEGER_LITERAL);
665 RECORD(EXPR_FLOATING_LITERAL);
666 RECORD(EXPR_IMAGINARY_LITERAL);
667 RECORD(EXPR_STRING_LITERAL);
668 RECORD(EXPR_CHARACTER_LITERAL);
669 RECORD(EXPR_PAREN);
670 RECORD(EXPR_UNARY_OPERATOR);
671 RECORD(EXPR_SIZEOF_ALIGN_OF);
672 RECORD(EXPR_ARRAY_SUBSCRIPT);
673 RECORD(EXPR_CALL);
674 RECORD(EXPR_MEMBER);
675 RECORD(EXPR_BINARY_OPERATOR);
676 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
677 RECORD(EXPR_CONDITIONAL_OPERATOR);
678 RECORD(EXPR_IMPLICIT_CAST);
679 RECORD(EXPR_CSTYLE_CAST);
680 RECORD(EXPR_COMPOUND_LITERAL);
681 RECORD(EXPR_EXT_VECTOR_ELEMENT);
682 RECORD(EXPR_INIT_LIST);
683 RECORD(EXPR_DESIGNATED_INIT);
684 RECORD(EXPR_IMPLICIT_VALUE_INIT);
685 RECORD(EXPR_VA_ARG);
686 RECORD(EXPR_ADDR_LABEL);
687 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000688 RECORD(EXPR_CHOOSE);
689 RECORD(EXPR_GNU_NULL);
690 RECORD(EXPR_SHUFFLE_VECTOR);
691 RECORD(EXPR_BLOCK);
692 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbourne91147592011-04-15 00:35:48 +0000693 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000694 RECORD(EXPR_OBJC_STRING_LITERAL);
695 RECORD(EXPR_OBJC_ENCODE);
696 RECORD(EXPR_OBJC_SELECTOR_EXPR);
697 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
698 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
699 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
700 RECORD(EXPR_OBJC_KVC_REF_EXPR);
701 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000702 RECORD(STMT_OBJC_FOR_COLLECTION);
703 RECORD(STMT_OBJC_CATCH);
704 RECORD(STMT_OBJC_FINALLY);
705 RECORD(STMT_OBJC_AT_TRY);
706 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
707 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000708 RECORD(EXPR_CXX_OPERATOR_CALL);
709 RECORD(EXPR_CXX_CONSTRUCT);
710 RECORD(EXPR_CXX_STATIC_CAST);
711 RECORD(EXPR_CXX_DYNAMIC_CAST);
712 RECORD(EXPR_CXX_REINTERPRET_CAST);
713 RECORD(EXPR_CXX_CONST_CAST);
714 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
715 RECORD(EXPR_CXX_BOOL_LITERAL);
716 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000717 RECORD(EXPR_CXX_TYPEID_EXPR);
718 RECORD(EXPR_CXX_TYPEID_TYPE);
719 RECORD(EXPR_CXX_UUIDOF_EXPR);
720 RECORD(EXPR_CXX_UUIDOF_TYPE);
721 RECORD(EXPR_CXX_THIS);
722 RECORD(EXPR_CXX_THROW);
723 RECORD(EXPR_CXX_DEFAULT_ARG);
724 RECORD(EXPR_CXX_BIND_TEMPORARY);
725 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
726 RECORD(EXPR_CXX_NEW);
727 RECORD(EXPR_CXX_DELETE);
728 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
729 RECORD(EXPR_EXPR_WITH_CLEANUPS);
730 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
731 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
732 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
733 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
734 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
735 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
736 RECORD(EXPR_CXX_NOEXCEPT);
737 RECORD(EXPR_OPAQUE_VALUE);
738 RECORD(EXPR_BINARY_TYPE_TRAIT);
739 RECORD(EXPR_PACK_EXPANSION);
740 RECORD(EXPR_SIZEOF_PACK);
741 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000742 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000743#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000744}
Mike Stump11289f42009-09-09 15:08:12 +0000745
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000746void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000747 RecordData Record;
748 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000749
Sebastian Redl539c5062010-08-18 23:57:32 +0000750#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
751#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000752
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000753 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000754 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000755 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000756 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000757 RECORD(TYPE_OFFSET);
758 RECORD(DECL_OFFSET);
759 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000760 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000761 RECORD(IDENTIFIER_OFFSET);
762 RECORD(IDENTIFIER_TABLE);
763 RECORD(EXTERNAL_DEFINITIONS);
764 RECORD(SPECIAL_TYPES);
765 RECORD(STATISTICS);
766 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000767 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000768 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
769 RECORD(SELECTOR_OFFSETS);
770 RECORD(METHOD_POOL);
771 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000772 RECORD(SOURCE_LOCATION_OFFSETS);
773 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000774 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000775 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000776 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000777 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregor29cc6422011-08-17 21:07:30 +0000778 RECORD(IMPORTS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000779 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000780 RECORD(TU_UPDATE_LEXICAL);
781 RECORD(REDECLS_UPDATE_LATEST);
782 RECORD(SEMA_DECL_REFS);
783 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
784 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
785 RECORD(DECL_REPLACEMENTS);
786 RECORD(UPDATE_VISIBLE);
787 RECORD(DECL_UPDATE_OFFSETS);
788 RECORD(DECL_UPDATES);
789 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
790 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000791 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000792 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000793 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000794 RECORD(FP_PRAGMA_OPTIONS);
795 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000796 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000797 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
798 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000799 RECORD(MODULE_OFFSET_MAP);
800 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor09b69892011-02-10 17:09:37 +0000801
Chris Lattner28fa4e62009-04-26 22:26:21 +0000802 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000803 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000804 RECORD(SM_SLOC_FILE_ENTRY);
805 RECORD(SM_SLOC_BUFFER_ENTRY);
806 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000807 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000808
Chris Lattner28fa4e62009-04-26 22:26:21 +0000809 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000810 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000811 RECORD(PP_MACRO_OBJECT_LIKE);
812 RECORD(PP_MACRO_FUNCTION_LIKE);
813 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000814
Douglas Gregor12bfa382009-10-17 00:13:19 +0000815 // Decls and Types block.
816 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000817 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000818 RECORD(TYPE_COMPLEX);
819 RECORD(TYPE_POINTER);
820 RECORD(TYPE_BLOCK_POINTER);
821 RECORD(TYPE_LVALUE_REFERENCE);
822 RECORD(TYPE_RVALUE_REFERENCE);
823 RECORD(TYPE_MEMBER_POINTER);
824 RECORD(TYPE_CONSTANT_ARRAY);
825 RECORD(TYPE_INCOMPLETE_ARRAY);
826 RECORD(TYPE_VARIABLE_ARRAY);
827 RECORD(TYPE_VECTOR);
828 RECORD(TYPE_EXT_VECTOR);
829 RECORD(TYPE_FUNCTION_PROTO);
830 RECORD(TYPE_FUNCTION_NO_PROTO);
831 RECORD(TYPE_TYPEDEF);
832 RECORD(TYPE_TYPEOF_EXPR);
833 RECORD(TYPE_TYPEOF);
834 RECORD(TYPE_RECORD);
835 RECORD(TYPE_ENUM);
836 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000837 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000838 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000839 RECORD(TYPE_DECLTYPE);
840 RECORD(TYPE_ELABORATED);
841 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
842 RECORD(TYPE_UNRESOLVED_USING);
843 RECORD(TYPE_INJECTED_CLASS_NAME);
844 RECORD(TYPE_OBJC_OBJECT);
845 RECORD(TYPE_TEMPLATE_TYPE_PARM);
846 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
847 RECORD(TYPE_DEPENDENT_NAME);
848 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
849 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
850 RECORD(TYPE_PAREN);
851 RECORD(TYPE_PACK_EXPANSION);
852 RECORD(TYPE_ATTRIBUTED);
853 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000854 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000855 RECORD(DECL_TYPEDEF);
856 RECORD(DECL_ENUM);
857 RECORD(DECL_RECORD);
858 RECORD(DECL_ENUM_CONSTANT);
859 RECORD(DECL_FUNCTION);
860 RECORD(DECL_OBJC_METHOD);
861 RECORD(DECL_OBJC_INTERFACE);
862 RECORD(DECL_OBJC_PROTOCOL);
863 RECORD(DECL_OBJC_IVAR);
864 RECORD(DECL_OBJC_AT_DEFS_FIELD);
865 RECORD(DECL_OBJC_CLASS);
866 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
867 RECORD(DECL_OBJC_CATEGORY);
868 RECORD(DECL_OBJC_CATEGORY_IMPL);
869 RECORD(DECL_OBJC_IMPLEMENTATION);
870 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
871 RECORD(DECL_OBJC_PROPERTY);
872 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000873 RECORD(DECL_FIELD);
874 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000875 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000876 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000877 RECORD(DECL_FILE_SCOPE_ASM);
878 RECORD(DECL_BLOCK);
879 RECORD(DECL_CONTEXT_LEXICAL);
880 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000881 RECORD(DECL_NAMESPACE);
882 RECORD(DECL_NAMESPACE_ALIAS);
883 RECORD(DECL_USING);
884 RECORD(DECL_USING_SHADOW);
885 RECORD(DECL_USING_DIRECTIVE);
886 RECORD(DECL_UNRESOLVED_USING_VALUE);
887 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
888 RECORD(DECL_LINKAGE_SPEC);
889 RECORD(DECL_CXX_RECORD);
890 RECORD(DECL_CXX_METHOD);
891 RECORD(DECL_CXX_CONSTRUCTOR);
892 RECORD(DECL_CXX_DESTRUCTOR);
893 RECORD(DECL_CXX_CONVERSION);
894 RECORD(DECL_ACCESS_SPEC);
895 RECORD(DECL_FRIEND);
896 RECORD(DECL_FRIEND_TEMPLATE);
897 RECORD(DECL_CLASS_TEMPLATE);
898 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
899 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
900 RECORD(DECL_FUNCTION_TEMPLATE);
901 RECORD(DECL_TEMPLATE_TYPE_PARM);
902 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
903 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
904 RECORD(DECL_STATIC_ASSERT);
905 RECORD(DECL_CXX_BASE_SPECIFIERS);
906 RECORD(DECL_INDIRECTFIELD);
907 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
908
Douglas Gregor03412ba2011-06-03 02:27:19 +0000909 // Statements and Exprs can occur in the Decls and Types block.
910 AddStmtsExprs(Stream, Record);
911
Douglas Gregor92a96f52011-02-08 21:58:10 +0000912 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000913 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000914 RECORD(PPD_MACRO_DEFINITION);
915 RECORD(PPD_INCLUSION_DIRECTIVE);
916
Chris Lattner28fa4e62009-04-26 22:26:21 +0000917#undef RECORD
918#undef BLOCK
919 Stream.ExitBlock();
920}
921
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000922/// \brief Adjusts the given filename to only write out the portion of the
923/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000924///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000925/// \param Filename the file name to adjust.
926///
927/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
928/// the returned filename will be adjusted by this system root.
929///
930/// \returns either the original filename (if it needs no adjustment) or the
931/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000932static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000933adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000934 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000935
Douglas Gregorc567ba22011-07-22 16:35:34 +0000936 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000937 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000938
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000939 // Verify that the filename and the system root have the same prefix.
940 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000941 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000942 if (Filename[Pos] != isysroot[Pos])
943 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000944
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000945 // We hit the end of the filename before we hit the end of the system root.
946 if (!Filename[Pos])
947 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000948
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000949 // If the file name has a '/' at the current position, skip over the '/'.
950 // We distinguish sysroot-based includes from absolute includes by the
951 // absence of '/' at the beginning of sysroot-based includes.
952 if (Filename[Pos] == '/')
953 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000954
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000955 return Filename + Pos;
956}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000957
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000958/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregorc567ba22011-07-22 16:35:34 +0000959void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000960 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000961 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000962
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000963 // Metadata
Douglas Gregore8bbc122011-09-02 00:18:52 +0000964 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000965 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregor29cc6422011-08-17 21:07:30 +0000966 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000967 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
968 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000969 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
970 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
971 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregor29cc6422011-08-17 21:07:30 +0000972 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000973 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000974
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000975 RecordData Record;
Douglas Gregor29cc6422011-08-17 21:07:30 +0000976 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +0000977 Record.push_back(VERSION_MAJOR);
978 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000979 Record.push_back(CLANG_VERSION_MAJOR);
980 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +0000981 Record.push_back(!isysroot.empty());
Douglas Gregor29cc6422011-08-17 21:07:30 +0000982 const std::string &Triple = Target.getTriple().getTriple();
983 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
984
985 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +0000986 serialization::ModuleManager &Mgr = Chain->getModuleManager();
987 llvm::SmallVector<char, 128> ModulePaths;
988 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +0000989
990 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
991 M != MEnd; ++M) {
992 // Skip modules that weren't directly imported.
993 if (!(*M)->isDirectlyImported())
994 continue;
995
996 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
997 // FIXME: Write import location, once it matters.
998 // FIXME: This writes the absolute path for AST files we depend on.
999 const std::string &FileName = (*M)->FileName;
1000 Record.push_back(FileName.size());
1001 Record.append(FileName.begin(), FileName.end());
1002 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001003 Stream.EmitRecord(IMPORTS, Record);
1004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Douglas Gregora3b20262011-05-06 21:43:30 +00001006 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001007 SourceManager &SM = Context.getSourceManager();
1008 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1009 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001010 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001011 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1012 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1013
Michael J. Spencer740857f2010-12-21 16:45:57 +00001014 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001015
Michael J. Spencer740857f2010-12-21 16:45:57 +00001016 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001017
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001018 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001019 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001020 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001021 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001022 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001023 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +00001024
1025 Record.clear();
1026 Record.push_back(SM.getMainFileID().getOpaqueValue());
1027 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001028 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001029
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001030 // Original PCH directory
1031 if (!OutputFile.empty() && OutputFile != "-") {
1032 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1033 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1035 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1036
1037 llvm::SmallString<128> OutputPath(OutputFile);
1038
1039 llvm::sys::fs::make_absolute(OutputPath);
1040 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1041
1042 RecordData Record;
1043 Record.push_back(ORIGINAL_PCH_DIR);
1044 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1045 }
1046
Ted Kremenek18e066f2010-01-22 22:12:47 +00001047 // Repository branch/version information.
1048 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001049 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001050 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1051 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001052 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001053 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001054 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1055 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001056}
1057
1058/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001059void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001060 RecordData Record;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00001061#define LANGOPT(Name, Bits, Default, Description) \
1062 Record.push_back(LangOpts.Name);
1063#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1064 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1065#include "clang/Basic/LangOptions.def"
Douglas Gregor7d106e42011-11-15 19:35:01 +00001066
1067 Record.push_back(LangOpts.CurrentModule.size());
1068 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl539c5062010-08-18 23:57:32 +00001069 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001070}
1071
Douglas Gregora7f71a92009-04-10 03:52:48 +00001072//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001073// stat cache Serialization
1074//===----------------------------------------------------------------------===//
1075
1076namespace {
1077// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001078class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001079public:
1080 typedef const char * key_type;
1081 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattner2a6fa472010-11-23 19:28:12 +00001083 typedef struct stat data_type;
1084 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001085
1086 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001087 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001088 }
Mike Stump11289f42009-09-09 15:08:12 +00001089
1090 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001091 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001092 data_type_ref Data) {
1093 unsigned StrLen = strlen(path);
1094 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001095 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 clang::io::Emit8(Out, DataLen);
1097 return std::make_pair(StrLen + 1, DataLen);
1098 }
Mike Stump11289f42009-09-09 15:08:12 +00001099
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001100 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001101 Out.write(path, KeyLen);
1102 }
Mike Stump11289f42009-09-09 15:08:12 +00001103
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001104 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001105 data_type_ref Data, unsigned DataLen) {
1106 using namespace clang::io;
1107 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001108
Chris Lattner2a6fa472010-11-23 19:28:12 +00001109 Emit32(Out, (uint32_t) Data.st_ino);
1110 Emit32(Out, (uint32_t) Data.st_dev);
1111 Emit16(Out, (uint16_t) Data.st_mode);
1112 Emit64(Out, (uint64_t) Data.st_mtime);
1113 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001114
1115 assert(Out.tell() - Start == DataLen && "Wrong data length");
1116 }
1117};
1118} // end anonymous namespace
1119
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001120/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001121void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001122 // Build the on-disk hash table containing information about every
1123 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001124 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001125 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001126 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001127 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001128 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001129 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001130 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001131 }
Mike Stump11289f42009-09-09 15:08:12 +00001132
Douglas Gregorc5046832009-04-27 18:38:38 +00001133 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001134 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001135 uint32_t BucketOffset;
1136 {
1137 llvm::raw_svector_ostream Out(StatCacheData);
1138 // Make sure that no bucket is at offset 0
1139 clang::io::Emit32(Out, 0);
1140 BucketOffset = Generator.Emit(Out);
1141 }
1142
1143 // Create a blob abbreviation
1144 using namespace llvm;
1145 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001146 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1150 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1151
1152 // Write the stat cache
1153 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001154 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001155 Record.push_back(BucketOffset);
1156 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001157 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001158}
1159
1160//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001161// Source Manager Serialization
1162//===----------------------------------------------------------------------===//
1163
1164/// \brief Create an abbreviation for the SLocEntry that refers to a
1165/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001166static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001167 using namespace llvm;
1168 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001169 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001174 // FileEntry fields.
1175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor9dc32122011-11-16 20:05:18 +00001177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregora7f71a92009-04-10 03:52:48 +00001181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001182 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001183}
1184
1185/// \brief Create an abbreviation for the SLocEntry that refers to a
1186/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001187static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001188 using namespace llvm;
1189 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001190 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001196 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001197}
1198
1199/// \brief Create an abbreviation for the SLocEntry that refers to a
1200/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001201static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001202 using namespace llvm;
1203 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001204 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001206 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001207}
1208
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001209/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1210/// expansion.
1211static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001212 using namespace llvm;
1213 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001214 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001220 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001221}
1222
Douglas Gregor09b69892011-02-10 17:09:37 +00001223namespace {
1224 // Trait used for the on-disk hash table of header search information.
1225 class HeaderFileInfoTrait {
1226 ASTWriter &Writer;
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001227 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001228
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001229 // Keep track of the framework names we've used during serialization.
1230 SmallVector<char, 128> FrameworkStringData;
1231 llvm::StringMap<unsigned> FrameworkNameOffset;
1232
Douglas Gregor09b69892011-02-10 17:09:37 +00001233 public:
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001234 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregor09b69892011-02-10 17:09:37 +00001235 : Writer(Writer), HS(HS) { }
1236
1237 typedef const char *key_type;
1238 typedef key_type key_type_ref;
1239
1240 typedef HeaderFileInfo data_type;
1241 typedef const data_type &data_type_ref;
1242
1243 static unsigned ComputeHash(const char *path) {
1244 // The hash is based only on the filename portion of the key, so that the
1245 // reader can match based on filenames when symlinking or excess path
1246 // elements ("foo/../", "../") change the form of the name. However,
1247 // complete path is still the key.
1248 return llvm::HashString(llvm::sys::path::filename(path));
1249 }
1250
1251 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001252 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001253 data_type_ref Data) {
1254 unsigned StrLen = strlen(path);
1255 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001256 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001257 clang::io::Emit8(Out, DataLen);
1258 return std::make_pair(StrLen + 1, DataLen);
1259 }
1260
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001261 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001262 Out.write(path, KeyLen);
1263 }
1264
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001265 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001266 data_type_ref Data, unsigned DataLen) {
1267 using namespace clang::io;
1268 uint64_t Start = Out.tell(); (void)Start;
1269
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001270 unsigned char Flags = (Data.isImport << 5)
1271 | (Data.isPragmaOnce << 4)
1272 | (Data.DirInfo << 2)
1273 | (Data.Resolved << 1)
1274 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001275 Emit8(Out, (uint8_t)Flags);
1276 Emit16(Out, (uint16_t) Data.NumIncludes);
1277
1278 if (!Data.ControllingMacro)
1279 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1280 else
1281 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001282
1283 unsigned Offset = 0;
1284 if (!Data.Framework.empty()) {
1285 // If this header refers into a framework, save the framework name.
1286 llvm::StringMap<unsigned>::iterator Pos
1287 = FrameworkNameOffset.find(Data.Framework);
1288 if (Pos == FrameworkNameOffset.end()) {
1289 Offset = FrameworkStringData.size() + 1;
1290 FrameworkStringData.append(Data.Framework.begin(),
1291 Data.Framework.end());
1292 FrameworkStringData.push_back(0);
1293
1294 FrameworkNameOffset[Data.Framework] = Offset;
1295 } else
1296 Offset = Pos->second;
1297 }
1298 Emit32(Out, Offset);
1299
Douglas Gregor09b69892011-02-10 17:09:37 +00001300 assert(Out.tell() - Start == DataLen && "Wrong data length");
1301 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001302
1303 const char *strings_begin() const { return FrameworkStringData.begin(); }
1304 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001305 };
1306} // end anonymous namespace
1307
1308/// \brief Write the header search block for the list of files that
1309///
1310/// \param HS The header search structure to save.
1311///
1312/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001313void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001314 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001315 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1316
1317 if (FilesByUID.size() > HS.header_file_size())
1318 FilesByUID.resize(HS.header_file_size());
1319
1320 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1321 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001322 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001323 unsigned NumHeaderSearchEntries = 0;
1324 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1325 const FileEntry *File = FilesByUID[UID];
1326 if (!File)
1327 continue;
1328
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001329 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1330 // from the external source if it was not provided already.
1331 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001332 if (HFI.External && Chain)
1333 continue;
1334
1335 // Turn the file name into an absolute path, if it isn't already.
1336 const char *Filename = File->getName();
1337 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1338
1339 // If we performed any translation on the file name at all, we need to
1340 // save this string, since the generator will refer to it later.
1341 if (Filename != File->getName()) {
1342 Filename = strdup(Filename);
1343 SavedStrings.push_back(Filename);
1344 }
1345
1346 Generator.insert(Filename, HFI, GeneratorTrait);
1347 ++NumHeaderSearchEntries;
1348 }
1349
1350 // Create the on-disk hash table in a buffer.
1351 llvm::SmallString<4096> TableData;
1352 uint32_t BucketOffset;
1353 {
1354 llvm::raw_svector_ostream Out(TableData);
1355 // Make sure that no bucket is at offset 0
1356 clang::io::Emit32(Out, 0);
1357 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1358 }
1359
1360 // Create a blob abbreviation
1361 using namespace llvm;
1362 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1363 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1368 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1369
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001370 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001371 RecordData Record;
1372 Record.push_back(HEADER_SEARCH_TABLE);
1373 Record.push_back(BucketOffset);
1374 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001375 Record.push_back(TableData.size());
1376 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001377 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1378
1379 // Free all of the strings we had to duplicate.
1380 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1381 free((void*)SavedStrings[I]);
1382}
1383
Douglas Gregora7f71a92009-04-10 03:52:48 +00001384/// \brief Writes the block containing the serialized form of the
1385/// source manager.
1386///
1387/// TODO: We should probably use an on-disk hash table (stored in a
1388/// blob), indexed based on the file name, so that we only create
1389/// entries for files that we actually need. In the common case (no
1390/// errors), we probably won't have to create file entries for any of
1391/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001392void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001393 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001394 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001395 RecordData Record;
1396
Chris Lattner0910e3b2009-04-10 17:16:57 +00001397 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001398 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001399
1400 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001401 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1402 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1403 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001404 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001405
Douglas Gregor258ae542009-04-27 06:38:32 +00001406 // Write out the source location entry table. We skip the first
1407 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001408 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001409 // Write out the offsets of only source location file entries.
1410 // We will go through them in ASTReader::validateFileEntries().
1411 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001412 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001413 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1414 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001415 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001416 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001417 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001418
Douglas Gregor258ae542009-04-27 06:38:32 +00001419 // Record the offset of this source-location entry.
1420 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1421
1422 // Figure out which record code to use.
1423 unsigned Code;
1424 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001425 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1426 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001427 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001428 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1429 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001430 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001431 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001432 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001433 Record.clear();
1434 Record.push_back(Code);
1435
Douglas Gregor925296b2011-07-19 16:10:42 +00001436 // Starting offset of this entry within this module, so skip the dummy.
1437 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001438 if (SLoc->isFile()) {
1439 const SrcMgr::FileInfo &File = SLoc->getFile();
1440 Record.push_back(File.getIncludeLoc().getRawEncoding());
1441 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1442 Record.push_back(File.hasLineDirectives());
1443
1444 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001445 if (Content->OrigEntry) {
1446 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001447 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001448
Douglas Gregor258ae542009-04-27 06:38:32 +00001449 // The source location entry is a file. The blob associated
1450 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001451
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001452 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001453 Record.push_back(Content->OrigEntry->getSize());
1454 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor9dc32122011-11-16 20:05:18 +00001455 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001456 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001457
1458 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1459 if (FDI != FileDeclIDs.end()) {
1460 Record.push_back(FDI->second->FirstDeclIndex);
1461 Record.push_back(FDI->second->DeclIDs.size());
1462 } else {
1463 Record.push_back(0);
1464 Record.push_back(0);
1465 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001466
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001467 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001468 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001469 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001470
1471 // Ask the file manager to fixup the relative path for us. This will
1472 // honor the working directory.
1473 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1474
1475 // FIXME: This call to make_absolute shouldn't be necessary, the
1476 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001477 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001478 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001479
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001480 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001481 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001482
1483 if (Content->BufferOverridden) {
1484 Record.clear();
1485 Record.push_back(SM_SLOC_BUFFER_BLOB);
1486 const llvm::MemoryBuffer *Buffer
1487 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1488 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1489 StringRef(Buffer->getBufferStart(),
1490 Buffer->getBufferSize() + 1));
1491 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001492 } else {
1493 // The source location entry is a buffer. The blob associated
1494 // with this entry contains the contents of the buffer.
1495
1496 // We add one to the size so that we capture the trailing NULL
1497 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1498 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001499 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001500 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001501 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001502 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001503 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001504 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001505 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001506 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001507 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001508 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001509
Douglas Gregor925296b2011-07-19 16:10:42 +00001510 if (strcmp(Name, "<built-in>") == 0) {
1511 PreloadSLocs.push_back(SLocEntryOffsets.size());
1512 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001513 }
1514 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001515 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001516 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001517 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1518 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001519 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1520 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001521
1522 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001523 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001524 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001525 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001526 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001527 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001528 }
1529 }
1530
Douglas Gregor8f45df52009-04-16 22:23:12 +00001531 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001532
1533 if (SLocEntryOffsets.empty())
1534 return;
1535
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001536 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001537 // table is used for lazily loading source-location information.
1538 using namespace llvm;
1539 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001540 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1544 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001545
Douglas Gregor258ae542009-04-27 06:38:32 +00001546 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001547 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001548 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001549 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001550 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001551
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001552 Abbrev = new BitCodeAbbrev();
1553 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1556 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1557
1558 Record.clear();
1559 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1560 Record.push_back(SLocFileEntryOffsets.size());
1561 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1562 data(SLocFileEntryOffsets));
1563
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001564 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001565 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001566 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001567
1568 // Write the line table. It depends on remapping working, so it must come
1569 // after the source location offsets.
1570 if (SourceMgr.hasLineTable()) {
1571 LineTableInfo &LineTable = SourceMgr.getLineTable();
1572
1573 Record.clear();
1574 // Emit the file names
1575 Record.push_back(LineTable.getNumFilenames());
1576 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1577 // Emit the file name
1578 const char *Filename = LineTable.getFilename(I);
1579 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1580 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1581 Record.push_back(FilenameLen);
1582 if (FilenameLen)
1583 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1584 }
1585
1586 // Emit the line entries
1587 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1588 L != LEnd; ++L) {
1589 // Only emit entries for local files.
1590 if (L->first < 0)
1591 continue;
1592
1593 // Emit the file ID
1594 Record.push_back(L->first);
1595
1596 // Emit the line entries
1597 Record.push_back(L->second.size());
1598 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1599 LEEnd = L->second.end();
1600 LE != LEEnd; ++LE) {
1601 Record.push_back(LE->FileOffset);
1602 Record.push_back(LE->LineNo);
1603 Record.push_back(LE->FilenameID);
1604 Record.push_back((unsigned)LE->FileKind);
1605 Record.push_back(LE->IncludeOffset);
1606 }
1607 }
1608 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1609 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001610}
1611
Douglas Gregorc5046832009-04-27 18:38:38 +00001612//===----------------------------------------------------------------------===//
1613// Preprocessor Serialization
1614//===----------------------------------------------------------------------===//
1615
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001616static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1617 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1618 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1619 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1620 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1621 return X.first->getName().compare(Y.first->getName());
1622}
1623
Chris Lattnereeffaef2009-04-10 17:15:23 +00001624/// \brief Writes the block containing the serialized form of the
1625/// preprocessor.
1626///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001627void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001628 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1629 if (PPRec)
1630 WritePreprocessorDetail(*PPRec);
1631
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001632 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001633
Chris Lattner0af3ba12009-04-13 01:29:17 +00001634 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1635 if (PP.getCounterValue() != 0) {
1636 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001637 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001638 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001639 }
1640
1641 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001642 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001643
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001644 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001645 // FIXME: use diagnostics subsystem for localization etc.
1646 if (PP.SawDateOrTime())
1647 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001648
Douglas Gregor796d76a2010-10-20 22:00:55 +00001649
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001650 // Loop over all the macro definitions that are live at the end of the file,
1651 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001652
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001653 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001654 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001655 MacrosToEmit;
1656 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001657 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1658 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001659 I != E; ++I) {
Douglas Gregorebf00492011-10-17 15:32:29 +00001660 if (!IsModule || I->second->isPublic()) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001661 MacroDefinitionsSeen.insert(I->first);
1662 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1663 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001664 }
1665
1666 // Sort the set of macro definitions that need to be serialized by the
1667 // name of the macro, to provide a stable ordering.
1668 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1669 &compareMacroDefinitions);
1670
Douglas Gregor68051a72011-02-11 00:26:14 +00001671 // Resolve any identifiers that defined macros at the time they were
1672 // deserialized, adding them to the list of macros to emit (if appropriate).
1673 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1674 IdentifierInfo *Name
1675 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1676 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1677 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1678 }
1679
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001680 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1681 const IdentifierInfo *Name = MacrosToEmit[I].first;
1682 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001683 if (!MI)
1684 continue;
1685
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001686 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001687 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001688 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001689
1690 // FIXME: There is a (probably minor) optimization we could do here, if
1691 // the macro comes from the original PCH but the identifier comes from a
1692 // chained PCH, by storing the offset into the original PCH rather than
1693 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001694 if (MI->isBuiltinMacro() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001695 (Chain &&
1696 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1697 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001698 continue;
1699
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001700 AddIdentifierRef(Name, Record);
1701 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001702 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1703 Record.push_back(MI->isUsed());
Douglas Gregorebf00492011-10-17 15:32:29 +00001704 Record.push_back(MI->isPublic());
1705 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001706 unsigned Code;
1707 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001708 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001709 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001710 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001711
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001712 Record.push_back(MI->isC99Varargs());
1713 Record.push_back(MI->isGNUVarargs());
1714 Record.push_back(MI->getNumArgs());
1715 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1716 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001717 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001718 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001719
Douglas Gregoraae92242010-03-19 21:51:54 +00001720 // If we have a detailed preprocessing record, record the macro definition
1721 // ID that corresponds to this macro.
1722 if (PPRec)
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001723 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001724
Douglas Gregor8f45df52009-04-16 22:23:12 +00001725 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001726 Record.clear();
1727
Chris Lattner2199f5b2009-04-10 18:08:30 +00001728 // Emit the tokens array.
1729 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1730 // Note that we know that the preprocessor does not have any annotation
1731 // tokens in it because they are created by the parser, and thus can't be
1732 // in a macro definition.
1733 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001734
Chris Lattner2199f5b2009-04-10 18:08:30 +00001735 Record.push_back(Tok.getLocation().getRawEncoding());
1736 Record.push_back(Tok.getLength());
1737
Chris Lattner2199f5b2009-04-10 18:08:30 +00001738 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1739 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001740 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001741 // FIXME: Should translate token kind to a stable encoding.
1742 Record.push_back(Tok.getKind());
1743 // FIXME: Should translate token flags to a stable encoding.
1744 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001745
Sebastian Redl539c5062010-08-18 23:57:32 +00001746 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001747 Record.clear();
1748 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001749 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001750 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001751 Stream.ExitBlock();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001752}
1753
1754void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001755 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001756 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001757
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001758 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001759
Douglas Gregor92a96f52011-02-08 21:58:10 +00001760 // Enter the preprocessor block.
1761 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001762
Douglas Gregoraae92242010-03-19 21:51:54 +00001763 // If the preprocessor has a preprocessing record, emit it.
1764 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001765 using namespace llvm;
1766
1767 // Set up the abbreviation for
1768 unsigned InclusionAbbrev = 0;
1769 {
1770 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1771 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1776 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1777 }
1778
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001779 unsigned FirstPreprocessorEntityID
1780 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1781 + NUM_PREDEF_PP_ENTITY_IDS;
1782 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001783 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001784 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1785 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001786 E != EEnd;
1787 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001788 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001789
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001790 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1791 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001792
Douglas Gregor92a96f52011-02-08 21:58:10 +00001793 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001794 // Record this macro definition's ID.
1795 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001796
Douglas Gregor92a96f52011-02-08 21:58:10 +00001797 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001798 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1799 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001800 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001801
Chandler Carrutha88a22182011-07-14 08:20:46 +00001802 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001803 Record.push_back(ME->isBuiltinMacro());
1804 if (ME->isBuiltinMacro())
1805 AddIdentifierRef(ME->getName(), Record);
1806 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001807 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001808 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001809 continue;
1810 }
1811
1812 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1813 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001814 Record.push_back(ID->getFileName().size());
1815 Record.push_back(ID->wasInQuotes());
1816 Record.push_back(static_cast<unsigned>(ID->getKind()));
1817 llvm::SmallString<64> Buffer;
1818 Buffer += ID->getFileName();
1819 Buffer += ID->getFile()->getName();
1820 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1821 continue;
1822 }
1823
1824 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1825 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001826 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001827
Douglas Gregoraae92242010-03-19 21:51:54 +00001828 // Write the offsets table for the preprocessing record.
1829 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001830 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1831
Douglas Gregoraae92242010-03-19 21:51:54 +00001832 // Write the offsets table for identifier IDs.
1833 using namespace llvm;
1834 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001835 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001837 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001838 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001839
Douglas Gregoraae92242010-03-19 21:51:54 +00001840 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001841 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001842 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001843 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1844 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001845 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001846}
1847
Douglas Gregor253eefe2011-12-01 00:59:36 +00001848/// \brief Compute the number of modules within the given tree (including the
1849/// given module).
1850static unsigned getNumberOfModules(Module *Mod) {
1851 unsigned ChildModules = 0;
1852 for (llvm::StringMap<Module *>::iterator Sub = Mod->SubModules.begin(),
1853 SubEnd = Mod->SubModules.end();
1854 Sub != SubEnd; ++Sub)
1855 ChildModules += getNumberOfModules(Sub->getValue());
1856
1857 return ChildModules + 1;
1858}
1859
Douglas Gregorde3ef502011-11-30 23:21:26 +00001860void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00001861 // Enter the submodule description block.
1862 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1863
1864 // Write the abbreviations needed for the submodules block.
1865 using namespace llvm;
1866 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1867 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
1868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
1871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1872 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1873
1874 Abbrev = new BitCodeAbbrev();
1875 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA));
1876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1877 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1878
1879 Abbrev = new BitCodeAbbrev();
1880 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1882 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor253eefe2011-12-01 00:59:36 +00001883
1884 // Write the submodule metadata block.
1885 RecordData Record;
1886 Record.push_back(getNumberOfModules(WritingModule));
1887 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1888 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1889
Douglas Gregor69021972011-11-30 17:33:56 +00001890 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001891 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00001892 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00001893 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001894 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00001895 Q.pop();
Douglas Gregor253eefe2011-12-01 00:59:36 +00001896 SubmoduleIDs[Mod] = NextSubmoduleID++;
Douglas Gregor69021972011-11-30 17:33:56 +00001897
1898 // Emit the definition of the block.
1899 Record.clear();
1900 Record.push_back(SUBMODULE_DEFINITION);
1901 if (Mod->Parent) {
1902 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1903 Record.push_back(SubmoduleIDs[Mod->Parent]);
1904 } else {
1905 Record.push_back(0);
1906 }
1907 Record.push_back(Mod->IsFramework);
1908 Record.push_back(Mod->IsExplicit);
1909 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1910
1911 // Emit the umbrella header, if there is one.
1912 if (Mod->UmbrellaHeader) {
1913 Record.clear();
1914 Record.push_back(SUBMODULE_UMBRELLA);
1915 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
1916 Mod->UmbrellaHeader->getName());
1917 }
1918
1919 // Emit the headers.
1920 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1921 Record.clear();
1922 Record.push_back(SUBMODULE_HEADER);
1923 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1924 Mod->Headers[I]->getName());
1925 }
1926
1927 // Queue up the submodules of this module.
1928 llvm::SmallVector<StringRef, 2> SubModules;
1929
1930 // Sort the submodules first, so we get a predictable ordering in the AST
1931 // file.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001932 for (llvm::StringMap<Module *>::iterator
Douglas Gregor69021972011-11-30 17:33:56 +00001933 Sub = Mod->SubModules.begin(),
1934 SubEnd = Mod->SubModules.end();
1935 Sub != SubEnd; ++Sub)
1936 SubModules.push_back(Sub->getKey());
1937 llvm::array_pod_sort(SubModules.begin(), SubModules.end());
1938
1939 for (unsigned I = 0, N = SubModules.size(); I != N; ++I)
1940 Q.push(Mod->SubModules[SubModules[I]]);
1941 }
1942
1943 Stream.ExitBlock();
1944}
1945
Douglas Gregora28bcdd2011-12-01 02:07:58 +00001946serialization::SubmoduleID
1947ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
1948 if (Loc.isInvalid() || SubmoduleIDs.empty())
1949 return 0; // No submodule
1950
1951 // Use the expansion location to determine which module we're in.
1952 SourceManager &SrcMgr = PP->getSourceManager();
1953 SourceLocation ExpansionLoc = SrcMgr.getExpansionLoc(Loc);
1954 if (!ExpansionLoc.isFileID())
1955 return 0;
1956
1957
1958 FileID ExpansionFileID = SrcMgr.getFileID(ExpansionLoc);
1959 const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
1960 if (!ExpansionFile)
1961 return 0;
1962
1963 // Find the module that owns this header.
1964 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1965 Module *OwningMod = ModMap.findModuleForHeader(ExpansionFile);
1966 if (!OwningMod)
1967 return 0;
1968
1969 // Check whether we known about this submodule.
1970 llvm::DenseMap<Module *, unsigned>::iterator Known
1971 = SubmoduleIDs.find(OwningMod);
1972 if (Known == SubmoduleIDs.end())
1973 return 0;
1974
1975 return Known->second;
1976}
1977
David Blaikie9c902b52011-09-25 23:23:43 +00001978void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001979 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00001980 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001981 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1982 I != E; ++I) {
David Blaikie9c902b52011-09-25 23:23:43 +00001983 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001984 if (point.Loc.isInvalid())
1985 continue;
1986
1987 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbare8c12a22011-09-29 01:42:25 +00001988 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001989 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbara3637e62011-09-29 01:30:00 +00001990 if (I->second.isPragma()) {
1991 Record.push_back(I->first);
1992 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001993 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001994 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001995 Record.push_back(-1); // mark the end of the diag/map pairs for this
1996 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001997 }
1998
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001999 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002000 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002001}
2002
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002003void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2004 if (CXXBaseSpecifiersOffsets.empty())
2005 return;
2006
2007 RecordData Record;
2008
2009 // Create a blob abbreviation for the C++ base specifiers offsets.
2010 using namespace llvm;
2011
2012 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2013 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2016 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2017
Douglas Gregorc27b2872011-08-04 00:01:48 +00002018 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002019 Record.clear();
2020 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2021 Record.push_back(CXXBaseSpecifiersOffsets.size());
2022 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002023 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002024}
2025
Douglas Gregorc5046832009-04-27 18:38:38 +00002026//===----------------------------------------------------------------------===//
2027// Type Serialization
2028//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002029
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002030/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002031void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002032 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002033 if (Idx.getIndex() == 0) // we haven't seen this type before.
2034 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002035
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002036 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002037
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002038 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002039 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002040 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002041 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002042 else if (TypeOffsets.size() < Index) {
2043 TypeOffsets.resize(Index + 1);
2044 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002045 }
2046
2047 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002048
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002049 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002050 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002051
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002052 if (T.hasLocalNonFastQualifiers()) {
2053 Qualifiers Qs = T.getLocalQualifiers();
2054 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002055 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002056 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002057 } else {
2058 switch (T->getTypeClass()) {
2059 // For all of the concrete, non-dependent types, call the
2060 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002061#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002062 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002063#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002064#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002065 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002066 }
2067
2068 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002069 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002070
2071 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002072 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002073}
2074
Douglas Gregorc5046832009-04-27 18:38:38 +00002075//===----------------------------------------------------------------------===//
2076// Declaration Serialization
2077//===----------------------------------------------------------------------===//
2078
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002079/// \brief Write the block containing all of the declaration IDs
2080/// lexically declared within the given DeclContext.
2081///
2082/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2083/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002084uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002085 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002086 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002087 return 0;
2088
Douglas Gregor8f45df52009-04-16 22:23:12 +00002089 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002090 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002091 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002092 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002093 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2094 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002095 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002096
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002097 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002098 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002099 return Offset;
2100}
2101
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002102void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002103 using namespace llvm;
2104 RecordData Record;
2105
2106 // Write the type offsets array
2107 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002108 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002109 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002110 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002111 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2112 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2113 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002114 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002115 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002116 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002117 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002118
2119 // Write the declaration offsets array
2120 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002121 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2125 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2126 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002127 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002128 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002129 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002130 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002131}
2132
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002133void ASTWriter::WriteFileDeclIDsMap() {
2134 using namespace llvm;
2135 RecordData Record;
2136
2137 // Join the vectors of DeclIDs from all files.
2138 SmallVector<DeclID, 256> FileSortedIDs;
2139 for (FileDeclIDsTy::iterator
2140 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2141 DeclIDInFileInfo &Info = *FI->second;
2142 Info.FirstDeclIndex = FileSortedIDs.size();
2143 for (LocDeclIDsTy::iterator
2144 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2145 FileSortedIDs.push_back(DI->second);
2146 }
2147
2148 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2149 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2151 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2152 Record.push_back(FILE_SORTED_DECLS);
2153 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2154}
2155
Douglas Gregorc5046832009-04-27 18:38:38 +00002156//===----------------------------------------------------------------------===//
2157// Global Method Pool and Selector Serialization
2158//===----------------------------------------------------------------------===//
2159
Douglas Gregore84a9da2009-04-20 20:36:09 +00002160namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002161// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002162class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002163 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002164
2165public:
2166 typedef Selector key_type;
2167 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002168
Sebastian Redl834bb972010-08-04 17:20:04 +00002169 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002170 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002171 ObjCMethodList Instance, Factory;
2172 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002173 typedef const data_type& data_type_ref;
2174
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002175 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002176
Douglas Gregorc78d3462009-04-24 21:10:55 +00002177 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002178 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002179 }
Mike Stump11289f42009-09-09 15:08:12 +00002180
2181 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002182 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002183 data_type_ref Methods) {
2184 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2185 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002186 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2187 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002188 Method = Method->Next)
2189 if (Method->Method)
2190 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002191 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002192 Method = Method->Next)
2193 if (Method->Method)
2194 DataLen += 4;
2195 clang::io::Emit16(Out, DataLen);
2196 return std::make_pair(KeyLen, DataLen);
2197 }
Mike Stump11289f42009-09-09 15:08:12 +00002198
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002199 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002200 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002201 assert((Start >> 32) == 0 && "Selector key offset too large");
2202 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002203 unsigned N = Sel.getNumArgs();
2204 clang::io::Emit16(Out, N);
2205 if (N == 0)
2206 N = 1;
2207 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002208 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002209 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2210 }
Mike Stump11289f42009-09-09 15:08:12 +00002211
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002212 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002213 data_type_ref Methods, unsigned DataLen) {
2214 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002215 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002216 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002217 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002218 Method = Method->Next)
2219 if (Method->Method)
2220 ++NumInstanceMethods;
2221
2222 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002223 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002224 Method = Method->Next)
2225 if (Method->Method)
2226 ++NumFactoryMethods;
2227
2228 clang::io::Emit16(Out, NumInstanceMethods);
2229 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002230 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002231 Method = Method->Next)
2232 if (Method->Method)
2233 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002234 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002235 Method = Method->Next)
2236 if (Method->Method)
2237 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002238
2239 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002240 }
2241};
2242} // end anonymous namespace
2243
Sebastian Redla19a67f2010-08-03 21:58:15 +00002244/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002245///
2246/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002247/// in an on-disk hash table indexed by the selector. The hash table also
2248/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002249void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002250 using namespace llvm;
2251
Sebastian Redla19a67f2010-08-03 21:58:15 +00002252 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002253 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002254 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002255 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002256 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002257 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002258 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002259 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002260
Sebastian Redla19a67f2010-08-03 21:58:15 +00002261 // Create the on-disk hash table representation. We walk through every
2262 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002263 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002264 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002265 I = SelectorIDs.begin(), E = SelectorIDs.end();
2266 I != E; ++I) {
2267 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002268 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002269 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002270 I->second,
2271 ObjCMethodList(),
2272 ObjCMethodList()
2273 };
2274 if (F != SemaRef.MethodPool.end()) {
2275 Data.Instance = F->second.first;
2276 Data.Factory = F->second.second;
2277 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002278 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002279 // changed.
2280 if (Chain && I->second < FirstSelectorID) {
2281 // Selector already exists. Did it change?
2282 bool changed = false;
2283 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2284 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002285 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002286 changed = true;
2287 }
2288 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2289 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002290 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002291 changed = true;
2292 }
2293 if (!changed)
2294 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002295 } else if (Data.Instance.Method || Data.Factory.Method) {
2296 // A new method pool entry.
2297 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002298 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002299 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002300 }
2301
Douglas Gregorc78d3462009-04-24 21:10:55 +00002302 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002303 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002304 uint32_t BucketOffset;
2305 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002306 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002307 llvm::raw_svector_ostream Out(MethodPool);
2308 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002309 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002310 BucketOffset = Generator.Emit(Out, Trait);
2311 }
2312
2313 // Create a blob abbreviation
2314 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002315 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2319 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2320
Douglas Gregor95c13f52009-04-25 17:48:32 +00002321 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002322 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002323 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002324 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002325 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002326 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002327
2328 // Create a blob abbreviation for the selector table offsets.
2329 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002330 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2334 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2335
2336 // Write the selector offsets table.
2337 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002338 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002339 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002340 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002341 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002342 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002343 }
2344}
2345
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002346/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002347void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002348 using namespace llvm;
2349 if (SemaRef.ReferencedSelectors.empty())
2350 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002351
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002352 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002353
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002354 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002355 // very tricky to fix, and given that @selector shouldn't really appear in
2356 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002357 for (DenseMap<Selector, SourceLocation>::iterator S =
2358 SemaRef.ReferencedSelectors.begin(),
2359 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2360 Selector Sel = (*S).first;
2361 SourceLocation Loc = (*S).second;
2362 AddSelectorRef(Sel, Record);
2363 AddSourceLocation(Loc, Record);
2364 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002365 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002366}
2367
Douglas Gregorc5046832009-04-27 18:38:38 +00002368//===----------------------------------------------------------------------===//
2369// Identifier Table Serialization
2370//===----------------------------------------------------------------------===//
2371
Douglas Gregorc78d3462009-04-24 21:10:55 +00002372namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002373class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002374 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002375 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002376 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002377 bool IsModule;
2378
Douglas Gregor1d583f22009-04-28 21:18:29 +00002379 /// \brief Determines whether this is an "interesting" identifier
2380 /// that needs a full IdentifierInfo structure written into the hash
2381 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002382 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002383 if (II->isPoisoned() ||
2384 II->isExtensionToken() ||
2385 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002386 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002387 II->getFETokenInfo<void>())
2388 return true;
2389
Douglas Gregord7910e92011-09-14 22:14:14 +00002390 return hasMacroDefinition(II, Macro);
2391 }
2392
2393 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002394 if (!II->hasMacroDefinition())
2395 return false;
2396
Douglas Gregord7910e92011-09-14 22:14:14 +00002397 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002398 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002399
Douglas Gregord7910e92011-09-14 22:14:14 +00002400 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002401 }
2402
Douglas Gregore84a9da2009-04-20 20:36:09 +00002403public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002404 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002405 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002406
Sebastian Redl539c5062010-08-18 23:57:32 +00002407 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002408 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002409
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002410 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2411 IdentifierResolver &IdResolver, bool IsModule)
2412 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002413
2414 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002415 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002416 }
Mike Stump11289f42009-09-09 15:08:12 +00002417
2418 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002419 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002420 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002421 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002422 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002423 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002424 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregord7910e92011-09-14 22:14:14 +00002425 if (hasMacroDefinition(II, Macro))
Douglas Gregorb9256522009-04-28 21:32:13 +00002426 DataLen += 4;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002427
2428 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2429 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002430 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002431 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002432 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002433 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002434 // We emit the key length after the data length so that every
2435 // string is preceded by a 16-bit length. This matches the PTH
2436 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002437 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002438 return std::make_pair(KeyLen, DataLen);
2439 }
Mike Stump11289f42009-09-09 15:08:12 +00002440
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002441 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002442 unsigned KeyLen) {
2443 // Record the location of the key data. This is used when generating
2444 // the mapping from persistent IDs to strings.
2445 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002446 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002447 }
Mike Stump11289f42009-09-09 15:08:12 +00002448
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002449 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002450 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002451 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002452 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002453 clang::io::Emit32(Out, ID << 1);
2454 return;
2455 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002456
Douglas Gregor1d583f22009-04-28 21:18:29 +00002457 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002458 uint32_t Bits = 0;
Douglas Gregord7910e92011-09-14 22:14:14 +00002459 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregorb9256522009-04-28 21:32:13 +00002460 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregord7910e92011-09-14 22:14:14 +00002461 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002462 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2463 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002464 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002465 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002466 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002467
Douglas Gregord7910e92011-09-14 22:14:14 +00002468 if (HasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002469 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002470
Douglas Gregora868bbd2009-04-21 22:25:48 +00002471 // Emit the declaration IDs in reverse order, because the
2472 // IdentifierResolver provides the declarations as they would be
2473 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002474 // "stat"), but the ASTReader adds declarations to the end of the list
2475 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002476 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002477 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2478 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002479 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002480 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002481 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002482 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002483 }
2484};
2485} // end anonymous namespace
2486
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002487/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002488///
2489/// The identifier table consists of a blob containing string data
2490/// (the actual identifiers themselves) and a separate "offsets" index
2491/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002492void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2493 IdentifierResolver &IdResolver,
2494 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002495 using namespace llvm;
2496
2497 // Create and write out the blob that contains the identifier
2498 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002499 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002500 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002501 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002502
Douglas Gregore6648fb2009-04-28 20:33:11 +00002503 // Look for any identifiers that were named while processing the
2504 // headers, but are otherwise not needed. We add these to the hash
2505 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002506 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002507 // file.
2508 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2509 IDEnd = PP.getIdentifierTable().end();
2510 ID != IDEnd; ++ID)
2511 getIdentifierRef(ID->second);
2512
Sebastian Redlff4a2952010-07-23 23:49:55 +00002513 // Create the on-disk hash table representation. We only store offsets
2514 // for identifiers that appear here for the first time.
2515 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002516 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002517 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2518 ID != IDEnd; ++ID) {
2519 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002520 if (!Chain || !ID->first->isFromAST() ||
2521 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002522 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2523 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002524 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002525
Douglas Gregore84a9da2009-04-20 20:36:09 +00002526 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002527 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002528 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002529 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002530 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002531 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002532 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002533 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002534 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002535 }
2536
2537 // Create a blob abbreviation
2538 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002539 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002542 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002543
2544 // Write the identifier table
2545 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002546 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002547 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002548 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002549 }
2550
2551 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002552 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002553 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2557 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2558
2559 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002560 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002561 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002562 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002563 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002564 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002565}
2566
Douglas Gregorc5046832009-04-27 18:38:38 +00002567//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002568// DeclContext's Name Lookup Table Serialization
2569//===----------------------------------------------------------------------===//
2570
2571namespace {
2572// Trait used for the on-disk hash table used in the method pool.
2573class ASTDeclContextNameLookupTrait {
2574 ASTWriter &Writer;
2575
2576public:
2577 typedef DeclarationName key_type;
2578 typedef key_type key_type_ref;
2579
2580 typedef DeclContext::lookup_result data_type;
2581 typedef const data_type& data_type_ref;
2582
2583 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2584
2585 unsigned ComputeHash(DeclarationName Name) {
2586 llvm::FoldingSetNodeID ID;
2587 ID.AddInteger(Name.getNameKind());
2588
2589 switch (Name.getNameKind()) {
2590 case DeclarationName::Identifier:
2591 ID.AddString(Name.getAsIdentifierInfo()->getName());
2592 break;
2593 case DeclarationName::ObjCZeroArgSelector:
2594 case DeclarationName::ObjCOneArgSelector:
2595 case DeclarationName::ObjCMultiArgSelector:
2596 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2597 break;
2598 case DeclarationName::CXXConstructorName:
2599 case DeclarationName::CXXDestructorName:
2600 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002601 break;
2602 case DeclarationName::CXXOperatorName:
2603 ID.AddInteger(Name.getCXXOverloadedOperator());
2604 break;
2605 case DeclarationName::CXXLiteralOperatorName:
2606 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2607 case DeclarationName::CXXUsingDirective:
2608 break;
2609 }
2610
2611 return ID.ComputeHash();
2612 }
2613
2614 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002615 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002616 data_type_ref Lookup) {
2617 unsigned KeyLen = 1;
2618 switch (Name.getNameKind()) {
2619 case DeclarationName::Identifier:
2620 case DeclarationName::ObjCZeroArgSelector:
2621 case DeclarationName::ObjCOneArgSelector:
2622 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002623 case DeclarationName::CXXLiteralOperatorName:
2624 KeyLen += 4;
2625 break;
2626 case DeclarationName::CXXOperatorName:
2627 KeyLen += 1;
2628 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002629 case DeclarationName::CXXConstructorName:
2630 case DeclarationName::CXXDestructorName:
2631 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002632 case DeclarationName::CXXUsingDirective:
2633 break;
2634 }
2635 clang::io::Emit16(Out, KeyLen);
2636
2637 // 2 bytes for num of decls and 4 for each DeclID.
2638 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2639 clang::io::Emit16(Out, DataLen);
2640
2641 return std::make_pair(KeyLen, DataLen);
2642 }
2643
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002644 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002645 using namespace clang::io;
2646
2647 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2648 Emit8(Out, Name.getNameKind());
2649 switch (Name.getNameKind()) {
2650 case DeclarationName::Identifier:
2651 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2652 break;
2653 case DeclarationName::ObjCZeroArgSelector:
2654 case DeclarationName::ObjCOneArgSelector:
2655 case DeclarationName::ObjCMultiArgSelector:
2656 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2657 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002658 case DeclarationName::CXXOperatorName:
2659 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2660 Emit8(Out, Name.getCXXOverloadedOperator());
2661 break;
2662 case DeclarationName::CXXLiteralOperatorName:
2663 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2664 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002665 case DeclarationName::CXXConstructorName:
2666 case DeclarationName::CXXDestructorName:
2667 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002668 case DeclarationName::CXXUsingDirective:
2669 break;
2670 }
2671 }
2672
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002673 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002674 data_type Lookup, unsigned DataLen) {
2675 uint64_t Start = Out.tell(); (void)Start;
2676 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2677 for (; Lookup.first != Lookup.second; ++Lookup.first)
2678 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2679
2680 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2681 }
2682};
2683} // end anonymous namespace
2684
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002685/// \brief Write the block containing all of the declaration IDs
2686/// visible from the given DeclContext.
2687///
2688/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002689/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002690uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2691 DeclContext *DC) {
2692 if (DC->getPrimaryContext() != DC)
2693 return 0;
2694
2695 // Since there is no name lookup into functions or methods, don't bother to
2696 // build a visible-declarations table for these entities.
2697 if (DC->isFunctionOrMethod())
2698 return 0;
2699
2700 // If not in C++, we perform name lookup for the translation unit via the
2701 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2702 // FIXME: In C++ we need the visible declarations in order to "see" the
2703 // friend declarations, is there a way to do this without writing the table ?
2704 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2705 return 0;
2706
2707 // Force the DeclContext to build a its name-lookup table.
Douglas Gregora3e59b42011-08-24 21:56:08 +00002708 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002709 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002710
2711 // Serialize the contents of the mapping used for lookup. Note that,
2712 // although we have two very different code paths, the serialized
2713 // representation is the same for both cases: a declaration name,
2714 // followed by a size, followed by references to the visible
2715 // declarations that have that name.
2716 uint64_t Offset = Stream.GetCurrentBitNo();
2717 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2718 if (!Map || Map->empty())
2719 return 0;
2720
2721 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2722 ASTDeclContextNameLookupTrait Trait(*this);
2723
2724 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002725 DeclarationName ConversionName;
2726 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002727 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2728 D != DEnd; ++D) {
2729 DeclarationName Name = D->first;
2730 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002731 if (Result.first != Result.second) {
2732 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2733 // Hash all conversion function names to the same name. The actual
2734 // type information in conversion function name is not used in the
2735 // key (since such type information is not stable across different
2736 // modules), so the intended effect is to coalesce all of the conversion
2737 // functions under a single key.
2738 if (!ConversionName)
2739 ConversionName = Name;
2740 ConversionDecls.append(Result.first, Result.second);
2741 continue;
2742 }
2743
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002744 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002745 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002746 }
2747
Douglas Gregor05ef9312011-08-30 20:49:19 +00002748 // Add the conversion functions
2749 if (!ConversionDecls.empty()) {
2750 Generator.insert(ConversionName,
2751 DeclContext::lookup_result(ConversionDecls.begin(),
2752 ConversionDecls.end()),
2753 Trait);
2754 }
2755
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002756 // Create the on-disk hash table in a buffer.
2757 llvm::SmallString<4096> LookupTable;
2758 uint32_t BucketOffset;
2759 {
2760 llvm::raw_svector_ostream Out(LookupTable);
2761 // Make sure that no bucket is at offset 0
2762 clang::io::Emit32(Out, 0);
2763 BucketOffset = Generator.Emit(Out, Trait);
2764 }
2765
2766 // Write the lookup table
2767 RecordData Record;
2768 Record.push_back(DECL_CONTEXT_VISIBLE);
2769 Record.push_back(BucketOffset);
2770 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2771 LookupTable.str());
2772
2773 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2774 ++NumVisibleDeclContexts;
2775 return Offset;
2776}
2777
Sebastian Redla4071b42010-08-24 00:50:09 +00002778/// \brief Write an UPDATE_VISIBLE block for the given context.
2779///
2780/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2781/// DeclContext in a dependent AST file. As such, they only exist for the TU
2782/// (in C++) and for namespaces.
2783void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002784 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2785 if (!Map || Map->empty())
2786 return;
2787
2788 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2789 ASTDeclContextNameLookupTrait Trait(*this);
2790
2791 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002792 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2793 D != DEnd; ++D) {
2794 DeclarationName Name = D->first;
2795 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002796 // For any name that appears in this table, the results are complete, i.e.
2797 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002798 if (Result.first != Result.second)
2799 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002800 }
2801
2802 // Create the on-disk hash table in a buffer.
2803 llvm::SmallString<4096> LookupTable;
2804 uint32_t BucketOffset;
2805 {
2806 llvm::raw_svector_ostream Out(LookupTable);
2807 // Make sure that no bucket is at offset 0
2808 clang::io::Emit32(Out, 0);
2809 BucketOffset = Generator.Emit(Out, Trait);
2810 }
2811
2812 // Write the lookup table
2813 RecordData Record;
2814 Record.push_back(UPDATE_VISIBLE);
2815 Record.push_back(getDeclID(cast<Decl>(DC)));
2816 Record.push_back(BucketOffset);
2817 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2818}
2819
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002820/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2821void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2822 RecordData Record;
2823 Record.push_back(Opts.fp_contract);
2824 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2825}
2826
2827/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2828void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2829 if (!SemaRef.Context.getLangOptions().OpenCL)
2830 return;
2831
2832 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2833 RecordData Record;
2834#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2835#include "clang/Basic/OpenCLExtensions.def"
2836 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2837}
2838
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002839//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002840// General Serialization Routines
2841//===----------------------------------------------------------------------===//
2842
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002843/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002844void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002845 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002846 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2847 const Attr * A = *i;
2848 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00002849 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002850
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002851#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002852
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002853 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002854}
2855
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002856void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002857 Record.push_back(Str.size());
2858 Record.insert(Record.end(), Str.begin(), Str.end());
2859}
2860
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002861void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2862 RecordDataImpl &Record) {
2863 Record.push_back(Version.getMajor());
2864 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2865 Record.push_back(*Minor + 1);
2866 else
2867 Record.push_back(0);
2868 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2869 Record.push_back(*Subminor + 1);
2870 else
2871 Record.push_back(0);
2872}
2873
Douglas Gregore84a9da2009-04-20 20:36:09 +00002874/// \brief Note that the identifier II occurs at the given offset
2875/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002876void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002877 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002878 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002879 // up earlier in the chain and thus don't need an offset.
2880 if (ID >= FirstIdentID)
2881 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002882}
2883
Douglas Gregor95c13f52009-04-25 17:48:32 +00002884/// \brief Note that the selector Sel occurs at the given offset
2885/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002886void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002887 unsigned ID = SelectorIDs[Sel];
2888 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002889 // Don't record offsets for selectors that are also available in a different
2890 // file.
2891 if (ID < FirstSelectorID)
2892 return;
2893 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002894}
2895
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002896ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002897 : Stream(Stream), Context(0), PP(0), Chain(0), WritingAST(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002898 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002899 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002900 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00002901 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
2902 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002903 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00002904 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002905 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002906 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00002907 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002908 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002909 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2910 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2911 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002912 DeclTypedefAbbrev(0),
2913 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2914 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002915{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002916}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002917
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002918ASTWriter::~ASTWriter() {
2919 for (FileDeclIDsTy::iterator
2920 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
2921 delete I->second;
2922}
2923
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002924void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002925 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00002926 Module *WritingModule, StringRef isysroot) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00002927 WritingAST = true;
2928
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002929 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002930 Stream.Emit((unsigned)'C', 8);
2931 Stream.Emit((unsigned)'P', 8);
2932 Stream.Emit((unsigned)'C', 8);
2933 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002934
Chris Lattner28fa4e62009-04-26 22:26:21 +00002935 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002936
Douglas Gregoreda8e122011-08-09 15:13:55 +00002937 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002938 PP = &SemaRef.PP;
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00002939 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00002940 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002941 PP = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00002942
2943 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00002944}
2945
Douglas Gregora94a1542011-07-27 21:45:57 +00002946template<typename Vector>
2947static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2948 ASTWriter::RecordData &Record) {
2949 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2950 I != E; ++I) {
2951 Writer.AddDeclRef(*I, Record);
2952 }
2953}
2954
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002955void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00002956 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00002957 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00002958 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002959 using namespace llvm;
2960
2961 ASTContext &Context = SemaRef.Context;
2962 Preprocessor &PP = SemaRef.PP;
2963
Douglas Gregordab42432011-08-12 00:15:20 +00002964 // Set up predefined declaration IDs.
2965 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00002966 if (Context.ObjCIdDecl)
2967 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00002968 if (Context.ObjCSelDecl)
2969 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00002970 if (Context.ObjCClassDecl)
2971 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00002972 if (Context.Int128Decl)
2973 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
2974 if (Context.UInt128Decl)
2975 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00002976 if (Context.ObjCInstanceTypeDecl)
2977 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00002978
Douglas Gregor851443c2011-08-12 01:39:19 +00002979 if (!Chain) {
2980 // Make sure that we emit IdentifierInfos (and any attached
2981 // declarations) for builtins. We don't need to do this when we're
2982 // emitting chained PCH files, because all of the builtins will be
2983 // in the original PCH file.
2984 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002985 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002986 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002987 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2988 Context.getLangOptions().NoBuiltin);
2989 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2990 getIdentifierRef(&Table.get(BuiltinNames[I]));
2991 }
2992
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002993 // If there are any out-of-date identifiers, bring them up to date.
2994 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
2995 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2996 IDEnd = PP.getIdentifierTable().end();
2997 ID != IDEnd; ++ID)
2998 if (ID->second->isOutOfDate())
2999 ExtSource->updateOutOfDateIdentifier(*ID->second);
3000 }
3001
Chris Lattner0c797362009-09-08 18:19:27 +00003002 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003003 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003004 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003005 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003006 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003007
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003008 // Build a record containing all of the file scoped decls in this file.
3009 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003010 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3011 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003012
Douglas Gregor851443c2011-08-12 01:39:19 +00003013 // Build a record containing all of the delegating constructors we still need
3014 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003015 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003016 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003017
Douglas Gregor851443c2011-08-12 01:39:19 +00003018 // Write the set of weak, undeclared identifiers. We always write the
3019 // entire table, since later PCH files in a PCH chain are only interested in
3020 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003021 RecordData WeakUndeclaredIdentifiers;
3022 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003023 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003024 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3025 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3026 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3027 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3028 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3029 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3030 }
3031 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003032
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003033 // Build a record containing all of the locally-scoped external
3034 // declarations in this header file. Generally, this record will be
3035 // empty.
3036 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003037 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003038 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003039 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003040 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3041 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003042 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003043 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003044 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3045 }
3046
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003047 // Build a record containing all of the ext_vector declarations.
3048 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003049 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003050
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003051 // Build a record containing all of the VTable uses information.
3052 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003053 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003054 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3055 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3056 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3057 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3058 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003059 }
3060
3061 // Build a record containing all of dynamic classes declarations.
3062 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003063 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003064
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003065 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003066 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003067 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003068 I = SemaRef.PendingInstantiations.begin(),
3069 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3070 AddDeclRef(I->first, PendingInstantiations);
3071 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003072 }
3073 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3074 "There are local ones at end of translation unit!");
3075
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003076 // Build a record containing some declaration references.
3077 RecordData SemaDeclRefs;
3078 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3079 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3080 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3081 }
3082
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003083 RecordData CUDASpecialDeclRefs;
3084 if (Context.getcudaConfigureCallDecl()) {
3085 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3086 }
3087
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003088 // Build a record containing all of the known namespaces.
3089 RecordData KnownNamespaces;
3090 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3091 I = SemaRef.KnownNamespaces.begin(),
3092 IEnd = SemaRef.KnownNamespaces.end();
3093 I != IEnd; ++I) {
3094 if (!I->second)
3095 AddDeclRef(I->first, KnownNamespaces);
3096 }
3097
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003098 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003099 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003100 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003101 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00003102 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorc567ba22011-07-22 16:35:34 +00003103 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00003104 WriteStatCache(*StatCalls);
Douglas Gregor851443c2011-08-12 01:39:19 +00003105
3106 // Create a lexical update block containing all of the declarations in the
3107 // translation unit that do not come from other AST files.
3108 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3109 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3110 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3111 E = TU->noload_decls_end();
3112 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003113 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003114 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003115 }
3116
3117 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3118 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3119 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3120 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3121 Record.clear();
3122 Record.push_back(TU_UPDATE_LEXICAL);
3123 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3124 data(NewGlobalDecls));
3125
3126 // And a visible updates block for the translation unit.
3127 Abv = new llvm::BitCodeAbbrev();
3128 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3129 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3130 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3131 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3132 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3133 WriteDeclContextVisibleUpdate(TU);
3134
3135 // If the translation unit has an anonymous namespace, and we don't already
3136 // have an update block for it, write it as an update block.
3137 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3138 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3139 if (Record.empty()) {
3140 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003141 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003142 }
3143 }
3144
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003145 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003146 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003147
Douglas Gregor5204bde2011-08-02 16:26:37 +00003148 // Form the record of special types.
3149 RecordData SpecialTypes;
3150 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor09c4aa82011-08-11 22:04:35 +00003151 AddTypeRef(Context.ObjCProtoType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003152 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003153 AddTypeRef(Context.getFILEType(), SpecialTypes);
3154 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3155 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3156 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3157 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003158 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003159 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003160
3161 // If we're emitting a module, write out the submodule information.
3162 if (WritingModule)
3163 WriteSubmodules(WritingModule);
3164
Douglas Gregor1970d882009-04-26 03:49:13 +00003165 // Keep writing types and declarations until all types and
3166 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003167 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003168 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003169 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3170 E = DeclsToRewrite.end();
3171 I != E; ++I)
3172 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003173 while (!DeclTypesToEmit.empty()) {
3174 DeclOrType DOT = DeclTypesToEmit.front();
3175 DeclTypesToEmit.pop();
3176 if (DOT.isType())
3177 WriteType(DOT.getType());
3178 else
3179 WriteDecl(Context, DOT.getDecl());
3180 }
3181 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003182
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003183 WriteFileDeclIDsMap();
3184 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3185
3186 if (Chain) {
3187 // Write the mapping information describing our module dependencies and how
3188 // each of those modules were mapped into our own offset/ID space, so that
3189 // the reader can build the appropriate mapping to its own offset/ID space.
3190 // The map consists solely of a blob with the following format:
3191 // *(module-name-len:i16 module-name:len*i8
3192 // source-location-offset:i32
3193 // identifier-id:i32
3194 // preprocessed-entity-id:i32
3195 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003196 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003197 // selector-id:i32
3198 // declaration-id:i32
3199 // c++-base-specifiers-id:i32
3200 // type-id:i32)
3201 //
3202 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3203 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3205 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
3206 llvm::SmallString<2048> Buffer;
3207 {
3208 llvm::raw_svector_ostream Out(Buffer);
3209 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
3210 MEnd = Chain->ModuleMgr.end();
3211 M != MEnd; ++M) {
3212 StringRef FileName = (*M)->FileName;
3213 io::Emit16(Out, FileName.size());
3214 Out.write(FileName.data(), FileName.size());
3215 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3216 io::Emit32(Out, (*M)->BaseIdentifierID);
3217 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003218 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003219 io::Emit32(Out, (*M)->BaseSelectorID);
3220 io::Emit32(Out, (*M)->BaseDeclID);
3221 io::Emit32(Out, (*M)->BaseTypeIndex);
3222 }
3223 }
3224 Record.clear();
3225 Record.push_back(MODULE_OFFSET_MAP);
3226 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3227 Buffer.data(), Buffer.size());
3228 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003229 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003230 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003231 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003232 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003233 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003234 WriteFPPragmaOptions(SemaRef.getFPOptions());
3235 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003236
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003237 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003238 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003239
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003240 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003241
Douglas Gregor5204bde2011-08-02 16:26:37 +00003242 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3243
Douglas Gregor851443c2011-08-12 01:39:19 +00003244 /// Build a record containing first declarations from a chained PCH and the
3245 /// most recent declarations in this AST that they point to.
3246 RecordData FirstLatestDeclIDs;
3247 for (FirstLatestDeclMap::iterator I = FirstLatestDecls.begin(),
3248 E = FirstLatestDecls.end();
3249 I != E; ++I) {
Douglas Gregor851443c2011-08-12 01:39:19 +00003250 AddDeclRef(I->first, FirstLatestDeclIDs);
3251 AddDeclRef(I->second, FirstLatestDeclIDs);
3252 }
3253
3254 if (!FirstLatestDeclIDs.empty())
3255 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
3256
Douglas Gregord4df8652009-04-22 22:02:47 +00003257 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003258 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003259 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003260
3261 // Write the record containing tentative definitions.
3262 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003263 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003264
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003265 // Write the record containing unused file scoped decls.
3266 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003267 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003268
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003269 // Write the record containing weak undeclared identifiers.
3270 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003271 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003272 WeakUndeclaredIdentifiers);
3273
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003274 // Write the record containing locally-scoped external definitions.
3275 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003276 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003277 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003278
3279 // Write the record containing ext_vector type names.
3280 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003281 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003282
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003283 // Write the record containing VTable uses information.
3284 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003285 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003286
3287 // Write the record containing dynamic classes declarations.
3288 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003289 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003290
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003291 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003292 if (!PendingInstantiations.empty())
3293 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003294
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003295 // Write the record containing declaration references of Sema.
3296 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003297 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003298
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003299 // Write the record containing CUDA-specific declaration references.
3300 if (!CUDASpecialDeclRefs.empty())
3301 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003302
3303 // Write the delegating constructors.
3304 if (!DelegatingCtorDecls.empty())
3305 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003306
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003307 // Write the known namespaces.
3308 if (!KnownNamespaces.empty())
3309 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3310
Douglas Gregor851443c2011-08-12 01:39:19 +00003311 // Write the visible updates to DeclContexts.
3312 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3313 I = UpdatedDeclContexts.begin(),
3314 E = UpdatedDeclContexts.end();
3315 I != E; ++I)
3316 WriteDeclContextVisibleUpdate(*I);
3317
Douglas Gregordab42432011-08-12 00:15:20 +00003318 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003319 WriteDeclReplacementsBlock();
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003320 WriteChainedObjCCategories();
Douglas Gregordab42432011-08-12 00:15:20 +00003321
Douglas Gregor08f01292009-04-17 22:13:46 +00003322 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003323 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003324 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003325 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003326 Record.push_back(NumLexicalDeclContexts);
3327 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003328 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003329 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003330}
3331
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003332/// \brief Go through the declaration update blocks and resolve declaration
3333/// pointers into declaration IDs.
3334void ASTWriter::ResolveDeclUpdatesBlocks() {
3335 for (DeclUpdateMap::iterator
3336 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3337 const Decl *D = I->first;
3338 UpdateRecord &URec = I->second;
3339
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003340 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003341 continue; // The decl will be written completely
3342
3343 unsigned Idx = 0, N = URec.size();
3344 while (Idx < N) {
3345 switch ((DeclUpdateKind)URec[Idx++]) {
3346 case UPD_CXX_SET_DEFINITIONDATA:
3347 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3348 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3349 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3350 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3351 ++Idx;
3352 break;
3353
3354 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3355 ++Idx;
3356 break;
3357 }
3358 }
3359 }
3360}
3361
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003362void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003363 if (DeclUpdates.empty())
3364 return;
3365
3366 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003367 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003368 for (DeclUpdateMap::iterator
3369 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3370 const Decl *D = I->first;
3371 UpdateRecord &URec = I->second;
3372
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003373 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003374 continue; // The decl will be written completely,no need to store updates.
3375
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003376 uint64_t Offset = Stream.GetCurrentBitNo();
3377 Stream.EmitRecord(DECL_UPDATES, URec);
3378
3379 OffsetsRecord.push_back(GetDeclRef(D));
3380 OffsetsRecord.push_back(Offset);
3381 }
3382 Stream.ExitBlock();
3383 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3384}
3385
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003386void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003387 if (ReplacedDecls.empty())
3388 return;
3389
3390 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003391 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003392 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003393 Record.push_back(I->ID);
3394 Record.push_back(I->Offset);
3395 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003396 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003397 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003398}
3399
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003400void ASTWriter::WriteChainedObjCCategories() {
3401 if (LocalChainedObjCCategories.empty())
3402 return;
3403
3404 RecordData Record;
3405 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3406 I = LocalChainedObjCCategories.begin(),
3407 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3408 ChainedObjCCategoriesData &Data = *I;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003409 if (isRewritten(Data.Interface))
3410 continue;
3411
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003412 assert(Data.Interface->getCategoryList());
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003413 serialization::DeclID
3414 HeadCatID = getDeclID(Data.Interface->getCategoryList());
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003415
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003416 Record.push_back(getDeclID(Data.Interface));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003417 Record.push_back(HeadCatID);
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003418 Record.push_back(getDeclID(Data.TailCategory));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00003419 }
3420 Stream.EmitRecord(OBJC_CHAINED_CATEGORIES, Record);
3421}
3422
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003423void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003424 Record.push_back(Loc.getRawEncoding());
3425}
3426
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003427void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003428 AddSourceLocation(Range.getBegin(), Record);
3429 AddSourceLocation(Range.getEnd(), Record);
3430}
3431
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003432void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003433 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003434 const uint64_t *Words = Value.getRawData();
3435 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003436}
3437
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003438void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003439 Record.push_back(Value.isUnsigned());
3440 AddAPInt(Value, Record);
3441}
3442
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003443void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003444 AddAPInt(Value.bitcastToAPInt(), Record);
3445}
3446
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003447void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003448 Record.push_back(getIdentifierRef(II));
3449}
3450
Sebastian Redl539c5062010-08-18 23:57:32 +00003451IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003452 if (II == 0)
3453 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003454
Sebastian Redl539c5062010-08-18 23:57:32 +00003455 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003456 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003457 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003458 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003459}
3460
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003461void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003462 Record.push_back(getSelectorRef(SelRef));
3463}
3464
Sebastian Redl539c5062010-08-18 23:57:32 +00003465SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003466 if (Sel.getAsOpaquePtr() == 0) {
3467 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003468 }
3469
Sebastian Redl539c5062010-08-18 23:57:32 +00003470 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003471 if (SID == 0 && Chain) {
3472 // This might trigger a ReadSelector callback, which will set the ID for
3473 // this selector.
3474 Chain->LoadSelector(Sel);
3475 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003476 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003477 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003478 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003479 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003480}
3481
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003482void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003483 AddDeclRef(Temp->getDestructor(), Record);
3484}
3485
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003486void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3487 CXXBaseSpecifier const *BasesEnd,
3488 RecordDataImpl &Record) {
3489 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3490 CXXBaseSpecifiersToWrite.push_back(
3491 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3492 Bases, BasesEnd));
3493 Record.push_back(NextCXXBaseSpecifiersID++);
3494}
3495
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003496void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003497 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003498 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003499 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003500 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003501 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003502 break;
3503 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003504 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003505 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003506 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003507 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003508 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003509 break;
3510 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003511 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003512 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003513 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003514 break;
John McCall0ad16662009-10-29 08:12:44 +00003515 case TemplateArgument::Null:
3516 case TemplateArgument::Integral:
3517 case TemplateArgument::Declaration:
3518 case TemplateArgument::Pack:
3519 break;
3520 }
3521}
3522
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003523void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003524 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003525 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003526
3527 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3528 bool InfoHasSameExpr
3529 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3530 Record.push_back(InfoHasSameExpr);
3531 if (InfoHasSameExpr)
3532 return; // Avoid storing the same expr twice.
3533 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003534 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3535 Record);
3536}
3537
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003538void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3539 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003540 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003541 AddTypeRef(QualType(), Record);
3542 return;
3543 }
3544
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003545 AddTypeLoc(TInfo->getTypeLoc(), Record);
3546}
3547
3548void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3549 AddTypeRef(TL.getType(), Record);
3550
John McCall8f115c62009-10-16 21:56:05 +00003551 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003552 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003553 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003554}
3555
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003556void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003557 Record.push_back(GetOrCreateTypeID(T));
3558}
3559
Douglas Gregoreda8e122011-08-09 15:13:55 +00003560TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3561 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003562 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3563}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003564
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003565TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003566 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003567 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003568}
3569
3570TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3571 if (T.isNull())
3572 return TypeIdx();
3573 assert(!T.getLocalFastQualifiers());
3574
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003575 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003576 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003577 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003578 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003579 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003580 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003581 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003582 return Idx;
3583}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003584
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003585TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003586 if (T.isNull())
3587 return TypeIdx();
3588 assert(!T.getLocalFastQualifiers());
3589
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003590 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3591 assert(I != TypeIdxs.end() && "Type not emitted!");
3592 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003593}
3594
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003595void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003596 Record.push_back(GetDeclRef(D));
3597}
3598
Sebastian Redl539c5062010-08-18 23:57:32 +00003599DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003600 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3601
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003602 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003603 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003604 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003605 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003606 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003607 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003608 // We haven't seen this declaration before. Give it a new ID and
3609 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003610 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003611 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003612 }
3613
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003614 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003615}
3616
Sebastian Redl539c5062010-08-18 23:57:32 +00003617DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003618 if (D == 0)
3619 return 0;
3620
3621 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3622 return DeclIDs[D];
3623}
3624
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003625static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3626 std::pair<unsigned, serialization::DeclID> R) {
3627 return L.first < R.first;
3628}
3629
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003630void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003631 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003632 assert(D);
3633
3634 SourceLocation Loc = D->getLocation();
3635 if (Loc.isInvalid())
3636 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003637
3638 // We only keep track of the file-level declarations of each file.
3639 if (!D->getLexicalDeclContext()->isFileContext())
3640 return;
3641
3642 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003643 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003644 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003645 FileID FID;
3646 unsigned Offset;
3647 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003648 if (FID.isInvalid())
3649 return;
3650 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3651 assert(Entry->isFile());
3652
3653 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3654 if (!Info)
3655 Info = new DeclIDInFileInfo();
3656
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003657 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003658 LocDeclIDsTy &Decls = Info->DeclIDs;
3659
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003660 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003661 Decls.push_back(LocDecl);
3662 return;
3663 }
3664
3665 LocDeclIDsTy::iterator
3666 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3667
3668 Decls.insert(I, LocDecl);
3669}
3670
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003671void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003672 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003673 Record.push_back(Name.getNameKind());
3674 switch (Name.getNameKind()) {
3675 case DeclarationName::Identifier:
3676 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3677 break;
3678
3679 case DeclarationName::ObjCZeroArgSelector:
3680 case DeclarationName::ObjCOneArgSelector:
3681 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003682 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003683 break;
3684
3685 case DeclarationName::CXXConstructorName:
3686 case DeclarationName::CXXDestructorName:
3687 case DeclarationName::CXXConversionFunctionName:
3688 AddTypeRef(Name.getCXXNameType(), Record);
3689 break;
3690
3691 case DeclarationName::CXXOperatorName:
3692 Record.push_back(Name.getCXXOverloadedOperator());
3693 break;
3694
Alexis Hunt3d221f22009-11-29 07:34:05 +00003695 case DeclarationName::CXXLiteralOperatorName:
3696 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3697 break;
3698
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003699 case DeclarationName::CXXUsingDirective:
3700 // No extra data to emit
3701 break;
3702 }
3703}
Chris Lattnerca025db2010-05-07 21:43:38 +00003704
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003705void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003706 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003707 switch (Name.getNameKind()) {
3708 case DeclarationName::CXXConstructorName:
3709 case DeclarationName::CXXDestructorName:
3710 case DeclarationName::CXXConversionFunctionName:
3711 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3712 break;
3713
3714 case DeclarationName::CXXOperatorName:
3715 AddSourceLocation(
3716 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3717 Record);
3718 AddSourceLocation(
3719 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3720 Record);
3721 break;
3722
3723 case DeclarationName::CXXLiteralOperatorName:
3724 AddSourceLocation(
3725 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3726 Record);
3727 break;
3728
3729 case DeclarationName::Identifier:
3730 case DeclarationName::ObjCZeroArgSelector:
3731 case DeclarationName::ObjCOneArgSelector:
3732 case DeclarationName::ObjCMultiArgSelector:
3733 case DeclarationName::CXXUsingDirective:
3734 break;
3735 }
3736}
3737
3738void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003739 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003740 AddDeclarationName(NameInfo.getName(), Record);
3741 AddSourceLocation(NameInfo.getLoc(), Record);
3742 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3743}
3744
3745void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003746 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003747 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003748 Record.push_back(Info.NumTemplParamLists);
3749 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3750 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3751}
3752
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003753void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003754 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003755 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003756 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003757 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00003758
3759 // Push each of the NNS's onto a stack for serialization in reverse order.
3760 while (NNS) {
3761 NestedNames.push_back(NNS);
3762 NNS = NNS->getPrefix();
3763 }
3764
3765 Record.push_back(NestedNames.size());
3766 while(!NestedNames.empty()) {
3767 NNS = NestedNames.pop_back_val();
3768 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3769 Record.push_back(Kind);
3770 switch (Kind) {
3771 case NestedNameSpecifier::Identifier:
3772 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3773 break;
3774
3775 case NestedNameSpecifier::Namespace:
3776 AddDeclRef(NNS->getAsNamespace(), Record);
3777 break;
3778
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003779 case NestedNameSpecifier::NamespaceAlias:
3780 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3781 break;
3782
Chris Lattnerca025db2010-05-07 21:43:38 +00003783 case NestedNameSpecifier::TypeSpec:
3784 case NestedNameSpecifier::TypeSpecWithTemplate:
3785 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3786 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3787 break;
3788
3789 case NestedNameSpecifier::Global:
3790 // Don't need to write an associated value.
3791 break;
3792 }
3793 }
3794}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003795
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003796void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3797 RecordDataImpl &Record) {
3798 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003799 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003800 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003801
3802 // Push each of the nested-name-specifiers's onto a stack for
3803 // serialization in reverse order.
3804 while (NNS) {
3805 NestedNames.push_back(NNS);
3806 NNS = NNS.getPrefix();
3807 }
3808
3809 Record.push_back(NestedNames.size());
3810 while(!NestedNames.empty()) {
3811 NNS = NestedNames.pop_back_val();
3812 NestedNameSpecifier::SpecifierKind Kind
3813 = NNS.getNestedNameSpecifier()->getKind();
3814 Record.push_back(Kind);
3815 switch (Kind) {
3816 case NestedNameSpecifier::Identifier:
3817 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3818 AddSourceRange(NNS.getLocalSourceRange(), Record);
3819 break;
3820
3821 case NestedNameSpecifier::Namespace:
3822 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3823 AddSourceRange(NNS.getLocalSourceRange(), Record);
3824 break;
3825
3826 case NestedNameSpecifier::NamespaceAlias:
3827 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3828 AddSourceRange(NNS.getLocalSourceRange(), Record);
3829 break;
3830
3831 case NestedNameSpecifier::TypeSpec:
3832 case NestedNameSpecifier::TypeSpecWithTemplate:
3833 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3834 AddTypeLoc(NNS.getTypeLoc(), Record);
3835 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3836 break;
3837
3838 case NestedNameSpecifier::Global:
3839 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3840 break;
3841 }
3842 }
3843}
3844
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003845void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003846 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003847 Record.push_back(Kind);
3848 switch (Kind) {
3849 case TemplateName::Template:
3850 AddDeclRef(Name.getAsTemplateDecl(), Record);
3851 break;
3852
3853 case TemplateName::OverloadedTemplate: {
3854 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3855 Record.push_back(OvT->size());
3856 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3857 I != E; ++I)
3858 AddDeclRef(*I, Record);
3859 break;
3860 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003861
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003862 case TemplateName::QualifiedTemplate: {
3863 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3864 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3865 Record.push_back(QualT->hasTemplateKeyword());
3866 AddDeclRef(QualT->getTemplateDecl(), Record);
3867 break;
3868 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003869
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003870 case TemplateName::DependentTemplate: {
3871 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3872 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3873 Record.push_back(DepT->isIdentifier());
3874 if (DepT->isIdentifier())
3875 AddIdentifierRef(DepT->getIdentifier(), Record);
3876 else
3877 Record.push_back(DepT->getOperator());
3878 break;
3879 }
John McCalld9dfe3a2011-06-30 08:33:18 +00003880
3881 case TemplateName::SubstTemplateTemplateParm: {
3882 SubstTemplateTemplateParmStorage *subst
3883 = Name.getAsSubstTemplateTemplateParm();
3884 AddDeclRef(subst->getParameter(), Record);
3885 AddTemplateName(subst->getReplacement(), Record);
3886 break;
3887 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003888
3889 case TemplateName::SubstTemplateTemplateParmPack: {
3890 SubstTemplateTemplateParmPackStorage *SubstPack
3891 = Name.getAsSubstTemplateTemplateParmPack();
3892 AddDeclRef(SubstPack->getParameterPack(), Record);
3893 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3894 break;
3895 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003896 }
3897}
3898
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003899void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003900 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003901 Record.push_back(Arg.getKind());
3902 switch (Arg.getKind()) {
3903 case TemplateArgument::Null:
3904 break;
3905 case TemplateArgument::Type:
3906 AddTypeRef(Arg.getAsType(), Record);
3907 break;
3908 case TemplateArgument::Declaration:
3909 AddDeclRef(Arg.getAsDecl(), Record);
3910 break;
3911 case TemplateArgument::Integral:
3912 AddAPSInt(*Arg.getAsIntegral(), Record);
3913 AddTypeRef(Arg.getIntegralType(), Record);
3914 break;
3915 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003916 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3917 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003918 case TemplateArgument::TemplateExpansion:
3919 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003920 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3921 Record.push_back(*NumExpansions + 1);
3922 else
3923 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003924 break;
3925 case TemplateArgument::Expression:
3926 AddStmt(Arg.getAsExpr());
3927 break;
3928 case TemplateArgument::Pack:
3929 Record.push_back(Arg.pack_size());
3930 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3931 I != E; ++I)
3932 AddTemplateArgument(*I, Record);
3933 break;
3934 }
3935}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003936
3937void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003938ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003939 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003940 assert(TemplateParams && "No TemplateParams!");
3941 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3942 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3943 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3944 Record.push_back(TemplateParams->size());
3945 for (TemplateParameterList::const_iterator
3946 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3947 P != PEnd; ++P)
3948 AddDeclRef(*P, Record);
3949}
3950
3951/// \brief Emit a template argument list.
3952void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003953ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003954 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003955 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003956 Record.push_back(TemplateArgs->size());
3957 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003958 AddTemplateArgument(TemplateArgs->get(i), Record);
3959}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003960
3961
3962void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003963ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003964 Record.push_back(Set.size());
3965 for (UnresolvedSetImpl::const_iterator
3966 I = Set.begin(), E = Set.end(); I != E; ++I) {
3967 AddDeclRef(I.getDecl(), Record);
3968 Record.push_back(I.getAccess());
3969 }
3970}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003971
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003972void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003973 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003974 Record.push_back(Base.isVirtual());
3975 Record.push_back(Base.isBaseOfClass());
3976 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003977 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003978 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003979 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003980 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3981 : SourceLocation(),
3982 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003983}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003984
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003985void ASTWriter::FlushCXXBaseSpecifiers() {
3986 RecordData Record;
3987 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3988 Record.clear();
3989
3990 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00003991 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003992 if (Index == CXXBaseSpecifiersOffsets.size())
3993 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3994 else {
3995 if (Index > CXXBaseSpecifiersOffsets.size())
3996 CXXBaseSpecifiersOffsets.resize(Index + 1);
3997 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3998 }
3999
4000 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4001 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4002 Record.push_back(BEnd - B);
4003 for (; B != BEnd; ++B)
4004 AddCXXBaseSpecifier(*B, Record);
4005 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004006
4007 // Flush any expressions that were written as part of the base specifiers.
4008 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004009 }
4010
4011 CXXBaseSpecifiersToWrite.clear();
4012}
4013
Alexis Hunt1d792652011-01-08 20:30:50 +00004014void ASTWriter::AddCXXCtorInitializers(
4015 const CXXCtorInitializer * const *CtorInitializers,
4016 unsigned NumCtorInitializers,
4017 RecordDataImpl &Record) {
4018 Record.push_back(NumCtorInitializers);
4019 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4020 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004021
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004022 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004023 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004024 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004025 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004026 } else if (Init->isDelegatingInitializer()) {
4027 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004028 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004029 } else if (Init->isMemberInitializer()){
4030 Record.push_back(CTOR_INITIALIZER_MEMBER);
4031 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004032 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004033 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4034 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004035 }
Francois Pichetd583da02010-12-04 09:14:42 +00004036
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004037 AddSourceLocation(Init->getMemberLocation(), Record);
4038 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004039 AddSourceLocation(Init->getLParenLoc(), Record);
4040 AddSourceLocation(Init->getRParenLoc(), Record);
4041 Record.push_back(Init->isWritten());
4042 if (Init->isWritten()) {
4043 Record.push_back(Init->getSourceOrder());
4044 } else {
4045 Record.push_back(Init->getNumArrayIndices());
4046 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4047 AddDeclRef(Init->getArrayIndex(i), Record);
4048 }
4049 }
4050}
4051
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004052void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4053 assert(D->DefinitionData);
4054 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
4055 Record.push_back(Data.UserDeclaredConstructor);
4056 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004057 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004058 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004059 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004060 Record.push_back(Data.UserDeclaredDestructor);
4061 Record.push_back(Data.Aggregate);
4062 Record.push_back(Data.PlainOldData);
4063 Record.push_back(Data.Empty);
4064 Record.push_back(Data.Polymorphic);
4065 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004066 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004067 Record.push_back(Data.HasNoNonEmptyBases);
4068 Record.push_back(Data.HasPrivateFields);
4069 Record.push_back(Data.HasProtectedFields);
4070 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004071 Record.push_back(Data.HasMutableFields);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004072 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004073 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004074 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004075 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004076 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004077 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004078 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004079 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004080 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004081 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004082 Record.push_back(Data.DeclaredDefaultConstructor);
4083 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004084 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004085 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004086 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004087 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004088 Record.push_back(Data.FailedImplicitMoveConstructor);
4089 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004090
4091 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004092 if (Data.NumBases > 0)
4093 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4094 Record);
4095
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004096 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4097 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004098 if (Data.NumVBases > 0)
4099 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4100 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004101
4102 AddUnresolvedSet(Data.Conversions, Record);
4103 AddUnresolvedSet(Data.VisibleConversions, Record);
4104 // Data.Definition is the owning decl, no need to write it.
4105 AddDeclRef(Data.FirstFriend, Record);
4106}
4107
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004108void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004109 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004110 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004111 assert(FirstDeclID == NextDeclID &&
4112 FirstTypeID == NextTypeID &&
4113 FirstIdentID == NextIdentID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004114 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004115 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004116 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004117
Sebastian Redl07a89a82010-07-30 00:29:29 +00004118 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004119
Douglas Gregordf0c1512011-08-18 04:12:04 +00004120 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4121 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4122 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004123 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004124 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004125 NextDeclID = FirstDeclID;
4126 NextTypeID = FirstTypeID;
4127 NextIdentID = FirstIdentID;
4128 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004129 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004130}
4131
Sebastian Redl539c5062010-08-18 23:57:32 +00004132void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004133 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00004134 if (II->hasMacroDefinition())
4135 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004136}
4137
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004138void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004139 // Always take the highest-numbered type index. This copes with an interesting
4140 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004141 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004142 // keep the higher-numbered entry so that we can properly write it out to
4143 // the AST file.
4144 TypeIdx &StoredIdx = TypeIdxs[T];
4145 if (Idx.getIndex() >= StoredIdx.getIndex())
4146 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004147}
4148
Sebastian Redl539c5062010-08-18 23:57:32 +00004149void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004150 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004151}
Sebastian Redl834bb972010-08-04 17:20:04 +00004152
Sebastian Redl539c5062010-08-18 23:57:32 +00004153void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004154 SelectorIDs[S] = ID;
4155}
Douglas Gregor91096292010-10-02 19:29:26 +00004156
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004157void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004158 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004159 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004160 MacroDefinitions[MD] = ID;
4161}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004162
4163void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004164 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004165 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004166 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4167 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004168 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004169 // A forward reference was mutated into a definition. Rewrite it.
4170 // FIXME: This happens during template instantiation, should we
4171 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004172 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004173 }
4174
4175 for (CXXRecordDecl::redecl_iterator
4176 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
4177 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
4178 if (Redecl == RD)
4179 continue;
4180
4181 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004182 if (Redecl->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004183 UpdateRecord &Record = DeclUpdates[Redecl];
4184 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
4185 assert(Redecl->DefinitionData);
4186 assert(Redecl->DefinitionData->Definition == D);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004187 Record.push_back(reinterpret_cast<uint64_t>(D)); // the DefinitionDecl
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004188 }
4189 }
4190 }
4191}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004192void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004193 assert(!WritingAST && "Already writing the AST!");
4194
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004195 // TU and namespaces are handled elsewhere.
4196 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4197 return;
4198
Douglas Gregorb3722e22011-09-09 23:01:35 +00004199 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004200 return; // Not a source decl added to a DeclContext from PCH.
4201
4202 AddUpdatedDeclContext(DC);
4203}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004204
4205void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004206 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004207 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004208 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004209 return; // Not a source member added to a class from PCH.
4210 if (!isa<CXXMethodDecl>(D))
4211 return; // We are interested in lazily declared implicit methods.
4212
4213 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004214 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004215 UpdateRecord &Record = DeclUpdates[RD];
4216 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004217 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004218}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004219
4220void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4221 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004222 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004223 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004224 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004225 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004226 return; // Not a source specialization added to a template from PCH.
4227
4228 UpdateRecord &Record = DeclUpdates[TD];
4229 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004230 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004231}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004232
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004233void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4234 const FunctionDecl *D) {
4235 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004236 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004237 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004238 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004239 return; // Not a source specialization added to a template from PCH.
4240
4241 UpdateRecord &Record = DeclUpdates[TD];
4242 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004243 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004244}
4245
Sebastian Redlab238a72011-04-24 16:28:06 +00004246void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004247 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004248 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004249 return; // Declaration not imported from PCH.
4250
4251 // Implicit decl from a PCH was defined.
4252 // FIXME: Should implicit definition be a separate FunctionDecl?
4253 RewriteDecl(D);
4254}
4255
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004256void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004257 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004258 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004259 return;
4260
4261 // Since the actual instantiation is delayed, this really means that we need
4262 // to update the instantiation location.
4263 UpdateRecord &Record = DeclUpdates[D];
4264 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4265 AddSourceLocation(
4266 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4267}
4268
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004269void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4270 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004271 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004272 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004273 return; // Declaration not imported from PCH.
4274 if (CatD->getNextClassCategory() &&
Douglas Gregorb3722e22011-09-09 23:01:35 +00004275 !CatD->getNextClassCategory()->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004276 return; // We already recorded that the tail of a category chain should be
4277 // attached to an interface.
4278
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00004279 ChainedObjCCategoriesData Data = { IFD, CatD };
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004280 LocalChainedObjCCategories.push_back(Data);
4281}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004282
4283void ASTWriter::CompletedObjCForwardRef(const ObjCContainerDecl *D) {
4284 assert(!WritingAST && "Already writing the AST!");
4285 if (!D->isFromASTFile())
4286 return; // Declaration not imported from PCH.
4287
4288 RewriteDecl(D);
4289}
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004290
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004291void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4292 const ObjCPropertyDecl *OrigProp,
4293 const ObjCCategoryDecl *ClassExt) {
4294 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4295 if (!D)
4296 return;
4297
4298 assert(!WritingAST && "Already writing the AST!");
4299 if (!D->isFromASTFile())
4300 return; // Declaration not imported from PCH.
4301
4302 RewriteDecl(D);
4303}
4304
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004305void ASTWriter::UpdatedAttributeList(const Decl *D) {
4306 assert(!WritingAST && "Already writing the AST!");
4307 if (!D->isFromASTFile())
4308 return; // Declaration not imported from PCH.
4309
4310 RewriteDecl(D);
4311}