blob: a34244d3f3cf687e399cbffe292c4f05cdc98c7a [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"
Douglas Gregor2d302362012-10-24 16:50:34 +000028#include "clang/Lex/HeaderSearchOptions.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Douglas Gregorb6af6c22012-10-24 20:05:57 +000032#include "clang/Lex/PreprocessorOptions.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000033#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000035#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000036#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000037#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000038#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000039#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000040#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000041#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000042#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000045#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000047#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000048#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000049#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000050#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000051#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000052#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000053#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000054using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000055using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
Sebastian Redl3df5a082010-07-30 17:03:48 +000057template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000058static StringRef data(const std::vector<T, Allocator> &v) {
59 if (v.empty()) return StringRef();
60 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000061 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000062}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000063
64template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000065static StringRef data(const SmallVectorImpl<T> &v) {
66 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000067 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000068}
69
Douglas Gregoref84c4b2009-04-09 22:27:44 +000070//===----------------------------------------------------------------------===//
71// Type serialization
72//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000073
Douglas Gregoref84c4b2009-04-09 22:27:44 +000074namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000075 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000076 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000077 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000078
79 public:
80 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000081 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000083 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000084 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000085
86 void VisitArrayType(const ArrayType *T);
87 void VisitFunctionType(const FunctionType *T);
88 void VisitTagType(const TagType *T);
89
90#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
91#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092#include "clang/AST/TypeNodes.def"
93 };
94}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000097 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098}
99
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000102 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103}
104
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000107 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108}
109
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000112 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000116 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
117 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000118 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119}
120
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000122 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000123 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000124}
125
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000126void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000127 Writer.AddTypeRef(T->getPointeeType(), Record);
128 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000129 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130}
131
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 Writer.AddTypeRef(T->getElementType(), Record);
134 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000135 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139 VisitArrayType(T);
140 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000146 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147}
148
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000151 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
152 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000153 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000154 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155}
156
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158 Writer.AddTypeRef(T->getElementType(), Record);
159 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000160 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000161 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162}
163
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000166 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167}
168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000171 FunctionType::ExtInfo C = T->getExtInfo();
172 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000173 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000174 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000175 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000176 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000177 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178}
179
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000182 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183}
184
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000185void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000186 VisitFunctionType(T);
187 Record.push_back(T->getNumArgs());
188 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
189 Writer.AddTypeRef(T->getArgType(I), Record);
190 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000191 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000193 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000194 Record.push_back(T->getExceptionSpecType());
195 if (T->getExceptionSpecType() == EST_Dynamic) {
196 Record.push_back(T->getNumExceptions());
197 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
198 Writer.AddTypeRef(T->getExceptionType(I), Record);
199 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
200 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000201 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
203 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000204 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
205 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000206 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000207 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000208}
209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000211 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000212 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000213}
John McCallb96ec562009-12-04 22:46:56 +0000214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000217 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
218 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000223 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000229 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230}
231
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000232void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000233 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000234 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000235 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000236}
237
Alexis Hunte852b102011-05-24 22:41:36 +0000238void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
239 Writer.AddTypeRef(T->getBaseType(), Record);
240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
241 Record.push_back(T->getUTTKind());
242 Code = TYPE_UNARY_TRANSFORM;
243}
244
Richard Smith30482bc2011-02-20 03:19:35 +0000245void ASTTypeWriter::VisitAutoType(const AutoType *T) {
246 Writer.AddTypeRef(T->getDeducedType(), Record);
247 Code = TYPE_AUTO;
248}
249
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000251 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000252 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000253 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000254 "Cannot serialize in the middle of a type definition");
255}
256
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000257void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000259 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260}
261
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000262void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000264 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000265}
266
John McCall81904512011-01-06 01:58:22 +0000267void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
268 Writer.AddTypeRef(T->getModifiedType(), Record);
269 Writer.AddTypeRef(T->getEquivalentType(), Record);
270 Record.push_back(T->getAttrKind());
271 Code = TYPE_ATTRIBUTED;
272}
273
Mike Stump11289f42009-09-09 15:08:12 +0000274void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000275ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000276 const SubstTemplateTypeParmType *T) {
277 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
278 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000279 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000280}
281
282void
Douglas Gregorada4b792011-01-14 02:55:32 +0000283ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
284 const SubstTemplateTypeParmPackType *T) {
285 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
286 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
287 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
288}
289
290void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000291ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000292 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000293 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000294 Writer.AddTemplateName(T->getTemplateName(), Record);
295 Record.push_back(T->getNumArgs());
296 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
297 ArgI != ArgE; ++ArgI)
298 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000299 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
300 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000301 : T->getCanonicalTypeInternal(),
302 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000303 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000308 VisitArrayType(T);
309 Writer.AddStmt(T->getSizeExpr());
310 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000311 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000312}
313
314void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000315ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000316 const DependentSizedExtVectorType *T) {
317 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000318 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000322ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000323 Record.push_back(T->getDepth());
324 Record.push_back(T->getIndex());
325 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000326 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000327 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000328}
329
330void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000331ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000332 Record.push_back(T->getKeyword());
333 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
334 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000335 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
336 : T->getCanonicalTypeInternal(),
337 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000338 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000339}
340
341void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000342ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000343 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000344 Record.push_back(T->getKeyword());
345 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
346 Writer.AddIdentifierRef(T->getIdentifier(), Record);
347 Record.push_back(T->getNumArgs());
348 for (DependentTemplateSpecializationType::iterator
349 I = T->begin(), E = T->end(); I != E; ++I)
350 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000351 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000352}
353
Douglas Gregord2fa7662010-12-20 02:24:11 +0000354void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
355 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000356 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
357 Record.push_back(*NumExpansions + 1);
358 else
359 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000360 Code = TYPE_PACK_EXPANSION;
361}
362
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000363void ASTTypeWriter::VisitParenType(const ParenType *T) {
364 Writer.AddTypeRef(T->getInnerType(), Record);
365 Code = TYPE_PAREN;
366}
367
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000368void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000369 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000370 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
371 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000372 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000373}
374
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000375void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000376 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000377 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000378 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000379}
380
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000381void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000382 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000384}
385
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000386void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000387 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000389 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000390 E = T->qual_end(); I != E; ++I)
391 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000392 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000393}
394
Steve Narofffb4330f2009-06-17 22:40:22 +0000395void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000396ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000397 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000398 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000399}
400
Eli Friedman0dfb8892011-10-06 23:00:33 +0000401void
402ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
403 Writer.AddTypeRef(T->getValueType(), Record);
404 Code = TYPE_ATOMIC;
405}
406
John McCall8f115c62009-10-16 21:56:05 +0000407namespace {
408
409class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000410 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000411 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000412
413public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000414 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000415 : Writer(Writer), Record(Record) { }
416
John McCall17001972009-10-18 01:05:36 +0000417#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000418#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000419 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000420#include "clang/AST/TypeLocNodes.def"
421
John McCall17001972009-10-18 01:05:36 +0000422 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
423 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000424};
425
426}
427
John McCall17001972009-10-18 01:05:36 +0000428void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
429 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000430}
John McCall17001972009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000432 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
433 if (TL.needsExtraLocalData()) {
434 Record.push_back(TL.getWrittenTypeSpec());
435 Record.push_back(TL.getWrittenSignSpec());
436 Record.push_back(TL.getWrittenWidthSpec());
437 Record.push_back(TL.hasModeAttr());
438 }
John McCall8f115c62009-10-16 21:56:05 +0000439}
John McCall17001972009-10-18 01:05:36 +0000440void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000442}
John McCall17001972009-10-18 01:05:36 +0000443void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000445}
John McCall17001972009-10-18 01:05:36 +0000446void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000448}
John McCall17001972009-10-18 01:05:36 +0000449void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000451}
John McCall17001972009-10-18 01:05:36 +0000452void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000454}
John McCall17001972009-10-18 01:05:36 +0000455void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000457 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000458}
John McCall17001972009-10-18 01:05:36 +0000459void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
461 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
462 Record.push_back(TL.getSizeExpr() ? 1 : 0);
463 if (TL.getSizeExpr())
464 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000465}
John McCall17001972009-10-18 01:05:36 +0000466void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
473 VisitArrayTypeLoc(TL);
474}
475void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
476 DependentSizedArrayTypeLoc TL) {
477 VisitArrayTypeLoc(TL);
478}
479void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
480 DependentSizedExtVectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getNameLoc(), Record);
488}
489void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000490 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000491 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
492 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000493 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall17001972009-10-18 01:05:36 +0000494 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
495 Writer.AddDeclRef(TL.getArg(i), Record);
496}
497void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
498 VisitFunctionTypeLoc(TL);
499}
500void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
501 VisitFunctionTypeLoc(TL);
502}
John McCallb96ec562009-12-04 22:46:56 +0000503void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
John McCall17001972009-10-18 01:05:36 +0000506void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getNameLoc(), Record);
508}
509void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000510 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
511 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
512 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000513}
514void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000515 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
516 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
517 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
518 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000519}
520void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
Alexis Hunte852b102011-05-24 22:41:36 +0000523void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getKWLoc(), Record);
525 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
526 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
527 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
528}
Richard Smith30482bc2011-02-20 03:19:35 +0000529void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
John McCall17001972009-10-18 01:05:36 +0000532void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
535void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getNameLoc(), Record);
537}
John McCall81904512011-01-06 01:58:22 +0000538void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
540 if (TL.hasAttrOperand()) {
541 SourceRange range = TL.getAttrOperandParensRange();
542 Writer.AddSourceLocation(range.getBegin(), Record);
543 Writer.AddSourceLocation(range.getEnd(), Record);
544 }
545 if (TL.hasAttrExprOperand()) {
546 Expr *operand = TL.getAttrExprOperand();
547 Record.push_back(operand ? 1 : 0);
548 if (operand) Writer.AddStmt(operand);
549 } else if (TL.hasAttrEnumOperand()) {
550 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
551 }
552}
John McCall17001972009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
John McCallcebee162009-10-18 09:09:24 +0000556void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
557 SubstTemplateTypeParmTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
Douglas Gregorada4b792011-01-14 02:55:32 +0000560void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
561 SubstTemplateTypeParmPackTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
John McCall17001972009-10-18 01:05:36 +0000564void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
565 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000566 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000567 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
568 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
569 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
570 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000571 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
572 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000573}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000574void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
576 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
577}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000578void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000579 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000580 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000581}
John McCalle78aac42010-03-10 03:28:59 +0000582void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000585void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000586 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000587 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
589}
John McCallc392f372010-06-11 00:33:02 +0000590void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
591 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000592 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000593 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000594 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000595 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000596 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
597 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
598 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000599 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
600 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000601}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000602void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
604}
John McCall17001972009-10-18 01:05:36 +0000605void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000607}
608void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
609 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000610 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
611 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
612 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
613 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000614}
John McCallfc93cf92009-10-22 22:37:11 +0000615void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000617}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000618void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
619 Writer.AddSourceLocation(TL.getKWLoc(), Record);
620 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
621 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
622}
John McCall8f115c62009-10-16 21:56:05 +0000623
Chris Lattner19cea4e2009-04-22 05:57:30 +0000624//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000625// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000626//===----------------------------------------------------------------------===//
627
Chris Lattner28fa4e62009-04-26 22:26:21 +0000628static void EmitBlockID(unsigned ID, const char *Name,
629 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000630 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000631 Record.clear();
632 Record.push_back(ID);
633 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
634
635 // Emit the block name if present.
636 if (Name == 0 || Name[0] == 0) return;
637 Record.clear();
638 while (*Name)
639 Record.push_back(*Name++);
640 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
641}
642
643static void EmitRecordID(unsigned ID, const char *Name,
644 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000645 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000646 Record.clear();
647 Record.push_back(ID);
648 while (*Name)
649 Record.push_back(*Name++);
650 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000651}
652
653static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000654 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000655#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000656 RECORD(STMT_STOP);
657 RECORD(STMT_NULL_PTR);
658 RECORD(STMT_NULL);
659 RECORD(STMT_COMPOUND);
660 RECORD(STMT_CASE);
661 RECORD(STMT_DEFAULT);
662 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000663 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000664 RECORD(STMT_IF);
665 RECORD(STMT_SWITCH);
666 RECORD(STMT_WHILE);
667 RECORD(STMT_DO);
668 RECORD(STMT_FOR);
669 RECORD(STMT_GOTO);
670 RECORD(STMT_INDIRECT_GOTO);
671 RECORD(STMT_CONTINUE);
672 RECORD(STMT_BREAK);
673 RECORD(STMT_RETURN);
674 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000675 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000676 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000677 RECORD(EXPR_PREDEFINED);
678 RECORD(EXPR_DECL_REF);
679 RECORD(EXPR_INTEGER_LITERAL);
680 RECORD(EXPR_FLOATING_LITERAL);
681 RECORD(EXPR_IMAGINARY_LITERAL);
682 RECORD(EXPR_STRING_LITERAL);
683 RECORD(EXPR_CHARACTER_LITERAL);
684 RECORD(EXPR_PAREN);
685 RECORD(EXPR_UNARY_OPERATOR);
686 RECORD(EXPR_SIZEOF_ALIGN_OF);
687 RECORD(EXPR_ARRAY_SUBSCRIPT);
688 RECORD(EXPR_CALL);
689 RECORD(EXPR_MEMBER);
690 RECORD(EXPR_BINARY_OPERATOR);
691 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
692 RECORD(EXPR_CONDITIONAL_OPERATOR);
693 RECORD(EXPR_IMPLICIT_CAST);
694 RECORD(EXPR_CSTYLE_CAST);
695 RECORD(EXPR_COMPOUND_LITERAL);
696 RECORD(EXPR_EXT_VECTOR_ELEMENT);
697 RECORD(EXPR_INIT_LIST);
698 RECORD(EXPR_DESIGNATED_INIT);
699 RECORD(EXPR_IMPLICIT_VALUE_INIT);
700 RECORD(EXPR_VA_ARG);
701 RECORD(EXPR_ADDR_LABEL);
702 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000703 RECORD(EXPR_CHOOSE);
704 RECORD(EXPR_GNU_NULL);
705 RECORD(EXPR_SHUFFLE_VECTOR);
706 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000707 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000708 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000709 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000710 RECORD(EXPR_OBJC_ARRAY_LITERAL);
711 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000712 RECORD(EXPR_OBJC_ENCODE);
713 RECORD(EXPR_OBJC_SELECTOR_EXPR);
714 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
715 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
716 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
717 RECORD(EXPR_OBJC_KVC_REF_EXPR);
718 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000719 RECORD(STMT_OBJC_FOR_COLLECTION);
720 RECORD(STMT_OBJC_CATCH);
721 RECORD(STMT_OBJC_FINALLY);
722 RECORD(STMT_OBJC_AT_TRY);
723 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
724 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000725 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000726 RECORD(EXPR_CXX_OPERATOR_CALL);
727 RECORD(EXPR_CXX_CONSTRUCT);
728 RECORD(EXPR_CXX_STATIC_CAST);
729 RECORD(EXPR_CXX_DYNAMIC_CAST);
730 RECORD(EXPR_CXX_REINTERPRET_CAST);
731 RECORD(EXPR_CXX_CONST_CAST);
732 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000733 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000734 RECORD(EXPR_CXX_BOOL_LITERAL);
735 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000736 RECORD(EXPR_CXX_TYPEID_EXPR);
737 RECORD(EXPR_CXX_TYPEID_TYPE);
738 RECORD(EXPR_CXX_UUIDOF_EXPR);
739 RECORD(EXPR_CXX_UUIDOF_TYPE);
740 RECORD(EXPR_CXX_THIS);
741 RECORD(EXPR_CXX_THROW);
742 RECORD(EXPR_CXX_DEFAULT_ARG);
743 RECORD(EXPR_CXX_BIND_TEMPORARY);
744 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
745 RECORD(EXPR_CXX_NEW);
746 RECORD(EXPR_CXX_DELETE);
747 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
748 RECORD(EXPR_EXPR_WITH_CLEANUPS);
749 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
750 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
751 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
752 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
753 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
754 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
755 RECORD(EXPR_CXX_NOEXCEPT);
756 RECORD(EXPR_OPAQUE_VALUE);
757 RECORD(EXPR_BINARY_TYPE_TRAIT);
758 RECORD(EXPR_PACK_EXPANSION);
759 RECORD(EXPR_SIZEOF_PACK);
760 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000761 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000762#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000763}
Mike Stump11289f42009-09-09 15:08:12 +0000764
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000765void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000766 RecordData Record;
767 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000768
Sebastian Redl539c5062010-08-18 23:57:32 +0000769#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
770#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000771
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000772 // Control Block.
773 BLOCK(CONTROL_BLOCK);
774 RECORD(METADATA);
775 RECORD(IMPORTS);
776 RECORD(LANGUAGE_OPTIONS);
777 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000778 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000779 RECORD(ORIGINAL_PCH_DIR);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000780 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000781 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000782 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000783 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000784 RECORD(PREPROCESSOR_OPTIONS);
785
Douglas Gregor108cb222012-10-19 00:45:00 +0000786 BLOCK(INPUT_FILES_BLOCK);
787 RECORD(INPUT_FILE);
788
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000789 // AST Top-Level Block.
790 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000791 RECORD(TYPE_OFFSET);
792 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000793 RECORD(IDENTIFIER_OFFSET);
794 RECORD(IDENTIFIER_TABLE);
795 RECORD(EXTERNAL_DEFINITIONS);
796 RECORD(SPECIAL_TYPES);
797 RECORD(STATISTICS);
798 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000799 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000800 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
801 RECORD(SELECTOR_OFFSETS);
802 RECORD(METHOD_POOL);
803 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000804 RECORD(SOURCE_LOCATION_OFFSETS);
805 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000806 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000807 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000808 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000809 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000810 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000811 RECORD(SEMA_DECL_REFS);
812 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
813 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
814 RECORD(DECL_REPLACEMENTS);
815 RECORD(UPDATE_VISIBLE);
816 RECORD(DECL_UPDATE_OFFSETS);
817 RECORD(DECL_UPDATES);
818 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
819 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000820 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000821 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000822 RECORD(FP_PRAGMA_OPTIONS);
823 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000824 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000825 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000826 RECORD(MODULE_OFFSET_MAP);
827 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000828 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000829 RECORD(FILE_SORTED_DECLS);
830 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000831 RECORD(MERGED_DECLARATIONS);
832 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000833 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000834 RECORD(MACRO_OFFSET);
835 RECORD(MACRO_UPDATES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000836
Chris Lattner28fa4e62009-04-26 22:26:21 +0000837 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000838 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000839 RECORD(SM_SLOC_FILE_ENTRY);
840 RECORD(SM_SLOC_BUFFER_ENTRY);
841 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000842 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattner28fa4e62009-04-26 22:26:21 +0000844 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000845 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000846 RECORD(PP_MACRO_OBJECT_LIKE);
847 RECORD(PP_MACRO_FUNCTION_LIKE);
848 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000849
Douglas Gregor12bfa382009-10-17 00:13:19 +0000850 // Decls and Types block.
851 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000852 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000853 RECORD(TYPE_COMPLEX);
854 RECORD(TYPE_POINTER);
855 RECORD(TYPE_BLOCK_POINTER);
856 RECORD(TYPE_LVALUE_REFERENCE);
857 RECORD(TYPE_RVALUE_REFERENCE);
858 RECORD(TYPE_MEMBER_POINTER);
859 RECORD(TYPE_CONSTANT_ARRAY);
860 RECORD(TYPE_INCOMPLETE_ARRAY);
861 RECORD(TYPE_VARIABLE_ARRAY);
862 RECORD(TYPE_VECTOR);
863 RECORD(TYPE_EXT_VECTOR);
864 RECORD(TYPE_FUNCTION_PROTO);
865 RECORD(TYPE_FUNCTION_NO_PROTO);
866 RECORD(TYPE_TYPEDEF);
867 RECORD(TYPE_TYPEOF_EXPR);
868 RECORD(TYPE_TYPEOF);
869 RECORD(TYPE_RECORD);
870 RECORD(TYPE_ENUM);
871 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000872 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000873 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000874 RECORD(TYPE_DECLTYPE);
875 RECORD(TYPE_ELABORATED);
876 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
877 RECORD(TYPE_UNRESOLVED_USING);
878 RECORD(TYPE_INJECTED_CLASS_NAME);
879 RECORD(TYPE_OBJC_OBJECT);
880 RECORD(TYPE_TEMPLATE_TYPE_PARM);
881 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
882 RECORD(TYPE_DEPENDENT_NAME);
883 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
884 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
885 RECORD(TYPE_PAREN);
886 RECORD(TYPE_PACK_EXPANSION);
887 RECORD(TYPE_ATTRIBUTED);
888 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000889 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000890 RECORD(DECL_TYPEDEF);
891 RECORD(DECL_ENUM);
892 RECORD(DECL_RECORD);
893 RECORD(DECL_ENUM_CONSTANT);
894 RECORD(DECL_FUNCTION);
895 RECORD(DECL_OBJC_METHOD);
896 RECORD(DECL_OBJC_INTERFACE);
897 RECORD(DECL_OBJC_PROTOCOL);
898 RECORD(DECL_OBJC_IVAR);
899 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000900 RECORD(DECL_OBJC_CATEGORY);
901 RECORD(DECL_OBJC_CATEGORY_IMPL);
902 RECORD(DECL_OBJC_IMPLEMENTATION);
903 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
904 RECORD(DECL_OBJC_PROPERTY);
905 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000906 RECORD(DECL_FIELD);
907 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000908 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000909 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000910 RECORD(DECL_FILE_SCOPE_ASM);
911 RECORD(DECL_BLOCK);
912 RECORD(DECL_CONTEXT_LEXICAL);
913 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000914 RECORD(DECL_NAMESPACE);
915 RECORD(DECL_NAMESPACE_ALIAS);
916 RECORD(DECL_USING);
917 RECORD(DECL_USING_SHADOW);
918 RECORD(DECL_USING_DIRECTIVE);
919 RECORD(DECL_UNRESOLVED_USING_VALUE);
920 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
921 RECORD(DECL_LINKAGE_SPEC);
922 RECORD(DECL_CXX_RECORD);
923 RECORD(DECL_CXX_METHOD);
924 RECORD(DECL_CXX_CONSTRUCTOR);
925 RECORD(DECL_CXX_DESTRUCTOR);
926 RECORD(DECL_CXX_CONVERSION);
927 RECORD(DECL_ACCESS_SPEC);
928 RECORD(DECL_FRIEND);
929 RECORD(DECL_FRIEND_TEMPLATE);
930 RECORD(DECL_CLASS_TEMPLATE);
931 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
932 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
933 RECORD(DECL_FUNCTION_TEMPLATE);
934 RECORD(DECL_TEMPLATE_TYPE_PARM);
935 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
936 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
937 RECORD(DECL_STATIC_ASSERT);
938 RECORD(DECL_CXX_BASE_SPECIFIERS);
939 RECORD(DECL_INDIRECTFIELD);
940 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
941
Douglas Gregor03412ba2011-06-03 02:27:19 +0000942 // Statements and Exprs can occur in the Decls and Types block.
943 AddStmtsExprs(Stream, Record);
944
Douglas Gregor92a96f52011-02-08 21:58:10 +0000945 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000946 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000947 RECORD(PPD_MACRO_DEFINITION);
948 RECORD(PPD_INCLUSION_DIRECTIVE);
949
Chris Lattner28fa4e62009-04-26 22:26:21 +0000950#undef RECORD
951#undef BLOCK
952 Stream.ExitBlock();
953}
954
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000955/// \brief Adjusts the given filename to only write out the portion of the
956/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000957///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958/// \param Filename the file name to adjust.
959///
960/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
961/// the returned filename will be adjusted by this system root.
962///
963/// \returns either the original filename (if it needs no adjustment) or the
964/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000965static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000966adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000967 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000968
Douglas Gregorc567ba22011-07-22 16:35:34 +0000969 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000970 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000971
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000972 // Verify that the filename and the system root have the same prefix.
973 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000974 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000975 if (Filename[Pos] != isysroot[Pos])
976 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000977
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000978 // We hit the end of the filename before we hit the end of the system root.
979 if (!Filename[Pos])
980 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000981
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000982 // If the file name has a '/' at the current position, skip over the '/'.
983 // We distinguish sysroot-based includes from absolute includes by the
984 // absence of '/' at the beginning of sysroot-based includes.
985 if (Filename[Pos] == '/')
986 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000987
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000988 return Filename + Pos;
989}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000990
Douglas Gregor112b9072012-10-18 05:31:06 +0000991/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +0000992void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
993 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +0000994 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000995 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000996 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
997 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +0000998
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000999 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001000 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1001 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1002 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1003 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1009 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1010 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001011 Record.push_back(VERSION_MAJOR);
1012 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001013 Record.push_back(CLANG_VERSION_MAJOR);
1014 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001015 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001016 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001017 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1018 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001019
Douglas Gregor112b9072012-10-18 05:31:06 +00001020 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001021 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001022 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1023 llvm::SmallVector<char, 128> ModulePaths;
1024 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001025
1026 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1027 M != MEnd; ++M) {
1028 // Skip modules that weren't directly imported.
1029 if (!(*M)->isDirectlyImported())
1030 continue;
1031
1032 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1033 // FIXME: Write import location, once it matters.
1034 // FIXME: This writes the absolute path for AST files we depend on.
1035 const std::string &FileName = (*M)->FileName;
1036 Record.push_back(FileName.size());
1037 Record.append(FileName.begin(), FileName.end());
1038 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001039 Stream.EmitRecord(IMPORTS, Record);
1040 }
Mike Stump11289f42009-09-09 15:08:12 +00001041
Douglas Gregor112b9072012-10-18 05:31:06 +00001042 // Language options.
1043 Record.clear();
1044 const LangOptions &LangOpts = Context.getLangOpts();
1045#define LANGOPT(Name, Bits, Default, Description) \
1046 Record.push_back(LangOpts.Name);
1047#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1048 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1049#include "clang/Basic/LangOptions.def"
1050
1051 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1052 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1053
1054 Record.push_back(LangOpts.CurrentModule.size());
1055 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1056 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1057
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001058 // Target options.
1059 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001060 const TargetInfo &Target = Context.getTargetInfo();
1061 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001062 AddString(TargetOpts.Triple, Record);
1063 AddString(TargetOpts.CPU, Record);
1064 AddString(TargetOpts.ABI, Record);
1065 AddString(TargetOpts.CXXABI, Record);
1066 AddString(TargetOpts.LinkerVersion, Record);
1067 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1068 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1069 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1070 }
1071 Record.push_back(TargetOpts.Features.size());
1072 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1073 AddString(TargetOpts.Features[I], Record);
1074 }
1075 Stream.EmitRecord(TARGET_OPTIONS, Record);
1076
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001077 // Diagnostic options.
1078 Record.clear();
1079 const DiagnosticOptions &DiagOpts
1080 = Context.getDiagnostics().getDiagnosticOptions();
1081#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1082#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1083 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1084#include "clang/Basic/DiagnosticOptions.def"
1085 Record.push_back(DiagOpts.Warnings.size());
1086 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1087 AddString(DiagOpts.Warnings[I], Record);
1088 // Note: we don't serialize the log or serialization file names, because they
1089 // are generally transient files and will almost always be overridden.
1090 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1091
Douglas Gregorc6317db2012-10-24 15:49:58 +00001092 // File system options.
1093 Record.clear();
1094 const FileSystemOptions &FSOpts
1095 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1096 AddString(FSOpts.WorkingDir, Record);
1097 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1098
Douglas Gregor2d302362012-10-24 16:50:34 +00001099 // Header search options.
1100 Record.clear();
1101 const HeaderSearchOptions &HSOpts
1102 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1103 AddString(HSOpts.Sysroot, Record);
1104
1105 // Include entries.
1106 Record.push_back(HSOpts.UserEntries.size());
1107 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1108 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1109 AddString(Entry.Path, Record);
1110 Record.push_back(static_cast<unsigned>(Entry.Group));
1111 Record.push_back(Entry.IsUserSupplied);
1112 Record.push_back(Entry.IsFramework);
1113 Record.push_back(Entry.IgnoreSysRoot);
1114 Record.push_back(Entry.IsInternal);
1115 Record.push_back(Entry.ImplicitExternC);
1116 }
1117
1118 // System header prefixes.
1119 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1120 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1121 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1122 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1123 }
1124
1125 AddString(HSOpts.ResourceDir, Record);
1126 AddString(HSOpts.ModuleCachePath, Record);
1127 Record.push_back(HSOpts.DisableModuleHash);
1128 Record.push_back(HSOpts.UseBuiltinIncludes);
1129 Record.push_back(HSOpts.UseStandardSystemIncludes);
1130 Record.push_back(HSOpts.UseStandardCXXIncludes);
1131 Record.push_back(HSOpts.UseLibcxx);
1132 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1133
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001134 // Preprocessor options.
1135 Record.clear();
1136 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1137
1138 // Macro definitions.
1139 Record.push_back(PPOpts.Macros.size());
1140 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1141 AddString(PPOpts.Macros[I].first, Record);
1142 Record.push_back(PPOpts.Macros[I].second);
1143 }
1144
1145 // Includes
1146 Record.push_back(PPOpts.Includes.size());
1147 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1148 AddString(PPOpts.Includes[I], Record);
1149
1150 // Macro includes
1151 Record.push_back(PPOpts.MacroIncludes.size());
1152 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1153 AddString(PPOpts.MacroIncludes[I], Record);
1154
Douglas Gregorb6368752012-10-24 23:41:50 +00001155 Record.push_back(PPOpts.UsePredefines);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001156 AddString(PPOpts.ImplicitPCHInclude, Record);
1157 AddString(PPOpts.ImplicitPTHInclude, Record);
1158 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1159 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1160
Douglas Gregora3b20262011-05-06 21:43:30 +00001161 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001162 SourceManager &SM = Context.getSourceManager();
1163 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1164 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001165 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1166 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001167 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1168 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1169
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001170 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001171
Michael J. Spencer740857f2010-12-21 16:45:57 +00001172 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001173
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001174 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001175 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001176 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001177 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001178 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001179 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001180 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001181 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001182
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001183 // Original PCH directory
1184 if (!OutputFile.empty() && OutputFile != "-") {
1185 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1186 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1188 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1189
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001190 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001191
1192 llvm::sys::fs::make_absolute(OutputPath);
1193 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1194
1195 RecordData Record;
1196 Record.push_back(ORIGINAL_PCH_DIR);
1197 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1198 }
1199
Douglas Gregor72be3902012-10-19 00:38:02 +00001200 WriteInputFiles(Context.SourceMgr, isysroot);
1201 Stream.ExitBlock();
1202}
1203
1204void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1205 using namespace llvm;
1206 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1207 RecordData Record;
1208
1209 // Create input-file abbreviation.
1210 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1211 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001212 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001213 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1214 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001215 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001216 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1217 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1218
1219 // Write out all of the input files.
1220 std::vector<uint32_t> InputFileOffsets;
1221 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1222 // Get this source location entry.
1223 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001224 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001225
1226 // We only care about file entries that were not overridden.
1227 if (!SLoc->isFile())
1228 continue;
1229 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001230 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001231 continue;
1232
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001233 // Record this entry's offset.
1234 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1235 InputFileIDs[Cache->OrigEntry] = InputFileOffsets.size();
1236
Douglas Gregor72be3902012-10-19 00:38:02 +00001237 Record.clear();
1238 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001239 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001240
1241 // Emit size/modification time for this file.
1242 Record.push_back(Cache->OrigEntry->getSize());
1243 Record.push_back(Cache->OrigEntry->getModificationTime());
1244
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001245 // Whether this file was overridden.
1246 Record.push_back(Cache->BufferOverridden);
1247
Douglas Gregor72be3902012-10-19 00:38:02 +00001248 // Turn the file name into an absolute path, if it isn't already.
1249 const char *Filename = Cache->OrigEntry->getName();
1250 SmallString<128> FilePath(Filename);
1251
1252 // Ask the file manager to fixup the relative path for us. This will
1253 // honor the working directory.
1254 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1255
1256 // FIXME: This call to make_absolute shouldn't be necessary, the
1257 // call to FixupRelativePath should always return an absolute path.
1258 llvm::sys::fs::make_absolute(FilePath);
1259 Filename = FilePath.c_str();
1260
1261 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1262
1263 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1264 }
1265
Douglas Gregor112b9072012-10-18 05:31:06 +00001266 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001267
1268 // Create input file offsets abbreviation.
1269 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1270 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1271 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1272 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1273 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1274
1275 // Write input file offsets.
1276 Record.clear();
1277 Record.push_back(INPUT_FILE_OFFSETS);
1278 Record.push_back(InputFileOffsets.size());
1279 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001280}
1281
Douglas Gregora7f71a92009-04-10 03:52:48 +00001282//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001283// stat cache Serialization
1284//===----------------------------------------------------------------------===//
1285
1286namespace {
1287// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001288class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001289public:
1290 typedef const char * key_type;
1291 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001292
Chris Lattner2a6fa472010-11-23 19:28:12 +00001293 typedef struct stat data_type;
1294 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001295
1296 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001297 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001298 }
Mike Stump11289f42009-09-09 15:08:12 +00001299
1300 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001301 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001302 data_type_ref Data) {
1303 unsigned StrLen = strlen(path);
1304 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001305 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001306 clang::io::Emit8(Out, DataLen);
1307 return std::make_pair(StrLen + 1, DataLen);
1308 }
Mike Stump11289f42009-09-09 15:08:12 +00001309
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001310 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001311 Out.write(path, KeyLen);
1312 }
Mike Stump11289f42009-09-09 15:08:12 +00001313
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001314 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001315 data_type_ref Data, unsigned DataLen) {
1316 using namespace clang::io;
1317 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001318
Chris Lattner2a6fa472010-11-23 19:28:12 +00001319 Emit32(Out, (uint32_t) Data.st_ino);
1320 Emit32(Out, (uint32_t) Data.st_dev);
1321 Emit16(Out, (uint16_t) Data.st_mode);
1322 Emit64(Out, (uint64_t) Data.st_mtime);
1323 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001324
1325 assert(Out.tell() - Start == DataLen && "Wrong data length");
1326 }
1327};
1328} // end anonymous namespace
1329
Douglas Gregorc5046832009-04-27 18:38:38 +00001330//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001331// Source Manager Serialization
1332//===----------------------------------------------------------------------===//
1333
1334/// \brief Create an abbreviation for the SLocEntry that refers to a
1335/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001336static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001337 using namespace llvm;
1338 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001339 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1343 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001344 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1348 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001349 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001350}
1351
1352/// \brief Create an abbreviation for the SLocEntry that refers to a
1353/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001354static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001355 using namespace llvm;
1356 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001357 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1361 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1362 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001363 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001364}
1365
1366/// \brief Create an abbreviation for the SLocEntry that refers to a
1367/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001368static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001369 using namespace llvm;
1370 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001371 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001373 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001374}
1375
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001376/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1377/// expansion.
1378static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001379 using namespace llvm;
1380 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001381 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001387 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001388}
1389
Douglas Gregor09b69892011-02-10 17:09:37 +00001390namespace {
1391 // Trait used for the on-disk hash table of header search information.
1392 class HeaderFileInfoTrait {
1393 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001394
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001395 // Keep track of the framework names we've used during serialization.
1396 SmallVector<char, 128> FrameworkStringData;
1397 llvm::StringMap<unsigned> FrameworkNameOffset;
1398
Douglas Gregor09b69892011-02-10 17:09:37 +00001399 public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001400 HeaderFileInfoTrait(ASTWriter &Writer)
1401 : Writer(Writer) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001402
1403 typedef const char *key_type;
1404 typedef key_type key_type_ref;
1405
1406 typedef HeaderFileInfo data_type;
1407 typedef const data_type &data_type_ref;
1408
1409 static unsigned ComputeHash(const char *path) {
1410 // The hash is based only on the filename portion of the key, so that the
1411 // reader can match based on filenames when symlinking or excess path
1412 // elements ("foo/../", "../") change the form of the name. However,
1413 // complete path is still the key.
1414 return llvm::HashString(llvm::sys::path::filename(path));
1415 }
1416
1417 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001418 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001419 data_type_ref Data) {
1420 unsigned StrLen = strlen(path);
1421 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001422 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001423 clang::io::Emit8(Out, DataLen);
1424 return std::make_pair(StrLen + 1, DataLen);
1425 }
1426
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001427 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001428 Out.write(path, KeyLen);
1429 }
1430
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001431 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001432 data_type_ref Data, unsigned DataLen) {
1433 using namespace clang::io;
1434 uint64_t Start = Out.tell(); (void)Start;
1435
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001436 unsigned char Flags = (Data.isImport << 5)
1437 | (Data.isPragmaOnce << 4)
1438 | (Data.DirInfo << 2)
1439 | (Data.Resolved << 1)
1440 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001441 Emit8(Out, (uint8_t)Flags);
1442 Emit16(Out, (uint16_t) Data.NumIncludes);
1443
1444 if (!Data.ControllingMacro)
1445 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1446 else
1447 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001448
1449 unsigned Offset = 0;
1450 if (!Data.Framework.empty()) {
1451 // If this header refers into a framework, save the framework name.
1452 llvm::StringMap<unsigned>::iterator Pos
1453 = FrameworkNameOffset.find(Data.Framework);
1454 if (Pos == FrameworkNameOffset.end()) {
1455 Offset = FrameworkStringData.size() + 1;
1456 FrameworkStringData.append(Data.Framework.begin(),
1457 Data.Framework.end());
1458 FrameworkStringData.push_back(0);
1459
1460 FrameworkNameOffset[Data.Framework] = Offset;
1461 } else
1462 Offset = Pos->second;
1463 }
1464 Emit32(Out, Offset);
1465
Douglas Gregor09b69892011-02-10 17:09:37 +00001466 assert(Out.tell() - Start == DataLen && "Wrong data length");
1467 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001468
1469 const char *strings_begin() const { return FrameworkStringData.begin(); }
1470 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001471 };
1472} // end anonymous namespace
1473
1474/// \brief Write the header search block for the list of files that
1475///
1476/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001477void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001478 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001479 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1480
1481 if (FilesByUID.size() > HS.header_file_size())
1482 FilesByUID.resize(HS.header_file_size());
1483
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001484 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregor09b69892011-02-10 17:09:37 +00001485 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001486 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001487 unsigned NumHeaderSearchEntries = 0;
1488 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1489 const FileEntry *File = FilesByUID[UID];
1490 if (!File)
1491 continue;
1492
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001493 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1494 // from the external source if it was not provided already.
1495 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001496 if (HFI.External && Chain)
1497 continue;
1498
1499 // Turn the file name into an absolute path, if it isn't already.
1500 const char *Filename = File->getName();
1501 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1502
1503 // If we performed any translation on the file name at all, we need to
1504 // save this string, since the generator will refer to it later.
1505 if (Filename != File->getName()) {
1506 Filename = strdup(Filename);
1507 SavedStrings.push_back(Filename);
1508 }
1509
1510 Generator.insert(Filename, HFI, GeneratorTrait);
1511 ++NumHeaderSearchEntries;
1512 }
1513
1514 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001515 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001516 uint32_t BucketOffset;
1517 {
1518 llvm::raw_svector_ostream Out(TableData);
1519 // Make sure that no bucket is at offset 0
1520 clang::io::Emit32(Out, 0);
1521 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1522 }
1523
1524 // Create a blob abbreviation
1525 using namespace llvm;
1526 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1527 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1532 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1533
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001534 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001535 RecordData Record;
1536 Record.push_back(HEADER_SEARCH_TABLE);
1537 Record.push_back(BucketOffset);
1538 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001539 Record.push_back(TableData.size());
1540 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001541 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1542
1543 // Free all of the strings we had to duplicate.
1544 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1545 free((void*)SavedStrings[I]);
1546}
1547
Douglas Gregora7f71a92009-04-10 03:52:48 +00001548/// \brief Writes the block containing the serialized form of the
1549/// source manager.
1550///
1551/// TODO: We should probably use an on-disk hash table (stored in a
1552/// blob), indexed based on the file name, so that we only create
1553/// entries for files that we actually need. In the common case (no
1554/// errors), we probably won't have to create file entries for any of
1555/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001556void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001557 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001558 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001559 RecordData Record;
1560
Chris Lattner0910e3b2009-04-10 17:16:57 +00001561 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001562 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001563
1564 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001565 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1566 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1567 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001568 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001569
Douglas Gregor258ae542009-04-27 06:38:32 +00001570 // Write out the source location entry table. We skip the first
1571 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001572 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001573 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001574 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1575 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001576 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001577 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001578 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001579 FileID FID = FileID::get(I);
1580 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001581
Douglas Gregor258ae542009-04-27 06:38:32 +00001582 // Record the offset of this source-location entry.
1583 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1584
1585 // Figure out which record code to use.
1586 unsigned Code;
1587 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001588 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1589 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001590 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001591 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001592 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001593 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001594 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001595 Record.clear();
1596 Record.push_back(Code);
1597
Douglas Gregor925296b2011-07-19 16:10:42 +00001598 // Starting offset of this entry within this module, so skip the dummy.
1599 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001600 if (SLoc->isFile()) {
1601 const SrcMgr::FileInfo &File = SLoc->getFile();
1602 Record.push_back(File.getIncludeLoc().getRawEncoding());
1603 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1604 Record.push_back(File.hasLineDirectives());
1605
1606 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001607 if (Content->OrigEntry) {
1608 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001609 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001610
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001611 // The source location entry is a file. Emit input file ID.
1612 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1613 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001614
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001615 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001616
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001617 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001618 if (FDI != FileDeclIDs.end()) {
1619 Record.push_back(FDI->second->FirstDeclIndex);
1620 Record.push_back(FDI->second->DeclIDs.size());
1621 } else {
1622 Record.push_back(0);
1623 Record.push_back(0);
1624 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001625
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001626 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001627
1628 if (Content->BufferOverridden) {
1629 Record.clear();
1630 Record.push_back(SM_SLOC_BUFFER_BLOB);
1631 const llvm::MemoryBuffer *Buffer
1632 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1633 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1634 StringRef(Buffer->getBufferStart(),
1635 Buffer->getBufferSize() + 1));
1636 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001637 } else {
1638 // The source location entry is a buffer. The blob associated
1639 // with this entry contains the contents of the buffer.
1640
1641 // We add one to the size so that we capture the trailing NULL
1642 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1643 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001644 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001645 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001646 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001647 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001648 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001649 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001650 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001651 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001652 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001653 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001654
Douglas Gregor925296b2011-07-19 16:10:42 +00001655 if (strcmp(Name, "<built-in>") == 0) {
1656 PreloadSLocs.push_back(SLocEntryOffsets.size());
1657 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001658 }
1659 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001660 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001661 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001662 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1663 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001664 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1665 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001666
1667 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001668 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001669 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001670 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001671 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001672 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001673 }
1674 }
1675
Douglas Gregor8f45df52009-04-16 22:23:12 +00001676 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001677
1678 if (SLocEntryOffsets.empty())
1679 return;
1680
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001681 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001682 // table is used for lazily loading source-location information.
1683 using namespace llvm;
1684 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001685 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001688 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1689 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001690
Douglas Gregor258ae542009-04-27 06:38:32 +00001691 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001692 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001693 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001694 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001695 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001696
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001697 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001698 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001699 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001700
1701 // Write the line table. It depends on remapping working, so it must come
1702 // after the source location offsets.
1703 if (SourceMgr.hasLineTable()) {
1704 LineTableInfo &LineTable = SourceMgr.getLineTable();
1705
1706 Record.clear();
1707 // Emit the file names
1708 Record.push_back(LineTable.getNumFilenames());
1709 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1710 // Emit the file name
1711 const char *Filename = LineTable.getFilename(I);
1712 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1713 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1714 Record.push_back(FilenameLen);
1715 if (FilenameLen)
1716 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1717 }
1718
1719 // Emit the line entries
1720 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1721 L != LEnd; ++L) {
1722 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001723 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001724 continue;
1725
1726 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001727 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001728
1729 // Emit the line entries
1730 Record.push_back(L->second.size());
1731 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1732 LEEnd = L->second.end();
1733 LE != LEEnd; ++LE) {
1734 Record.push_back(LE->FileOffset);
1735 Record.push_back(LE->LineNo);
1736 Record.push_back(LE->FilenameID);
1737 Record.push_back((unsigned)LE->FileKind);
1738 Record.push_back(LE->IncludeOffset);
1739 }
1740 }
1741 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1742 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001743}
1744
Douglas Gregorc5046832009-04-27 18:38:38 +00001745//===----------------------------------------------------------------------===//
1746// Preprocessor Serialization
1747//===----------------------------------------------------------------------===//
1748
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001749static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1750 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1751 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1752 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1753 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1754 return X.first->getName().compare(Y.first->getName());
1755}
1756
Chris Lattnereeffaef2009-04-10 17:15:23 +00001757/// \brief Writes the block containing the serialized form of the
1758/// preprocessor.
1759///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001760void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001761 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1762 if (PPRec)
1763 WritePreprocessorDetail(*PPRec);
1764
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001765 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001766
Chris Lattner0af3ba12009-04-13 01:29:17 +00001767 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1768 if (PP.getCounterValue() != 0) {
1769 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001770 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001771 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001772 }
1773
1774 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001775 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001776
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001777 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001778 // FIXME: use diagnostics subsystem for localization etc.
1779 if (PP.SawDateOrTime())
1780 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001781
Douglas Gregor796d76a2010-10-20 22:00:55 +00001782
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001783 // Loop over all the macro definitions that are live at the end of the file,
1784 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001785
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001786 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001787 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001788 MacrosToEmit;
1789 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001790 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor68051a72011-02-11 00:26:14 +00001791 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001792 I != E; ++I) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001793 if (!IsModule || I->second->isPublic()) {
1794 MacroDefinitionsSeen.insert(I->first);
1795 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001796 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001797 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001798
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001799 // Sort the set of macro definitions that need to be serialized by the
1800 // name of the macro, to provide a stable ordering.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001801 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001802 &compareMacroDefinitions);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001803
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001804 /// \brief Offsets of each of the macros into the bitstream, indexed by
1805 /// the local macro ID
1806 ///
1807 /// For each identifier that is associated with a macro, this map
1808 /// provides the offset into the bitstream where that macro is
1809 /// defined.
1810 std::vector<uint32_t> MacroOffsets;
1811
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001812 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1813 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001814
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001815 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1816 MI = MI->getPreviousDefinition()) {
1817 MacroID ID = getMacroRef(MI);
1818 if (!ID)
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001819 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001820
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001821 // Skip macros from a AST file if we're chaining.
1822 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1823 continue;
1824
1825 if (ID < FirstMacroID) {
1826 // This will have been dealt with via an update record.
1827 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1828 continue;
1829 }
1830
1831 // Record the local offset of this macro.
1832 unsigned Index = ID - FirstMacroID;
1833 if (Index == MacroOffsets.size())
1834 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1835 else {
1836 if (Index > MacroOffsets.size())
1837 MacroOffsets.resize(Index + 1);
1838
1839 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1840 }
1841
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001842 AddIdentifierRef(Name, Record);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001843 addMacroRef(MI, Record);
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001844 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001845 AddSourceLocation(MI->getDefinitionLoc(), Record);
1846 AddSourceLocation(MI->getUndefLoc(), Record);
1847 Record.push_back(MI->isUsed());
1848 Record.push_back(MI->isPublic());
1849 AddSourceLocation(MI->getVisibilityLocation(), Record);
1850 unsigned Code;
1851 if (MI->isObjectLike()) {
1852 Code = PP_MACRO_OBJECT_LIKE;
1853 } else {
1854 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001855
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001856 Record.push_back(MI->isC99Varargs());
1857 Record.push_back(MI->isGNUVarargs());
Eli Friedman14d3c792012-11-14 02:18:46 +00001858 Record.push_back(MI->hasCommaPasting());
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001859 Record.push_back(MI->getNumArgs());
1860 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1861 I != E; ++I)
1862 AddIdentifierRef(*I, Record);
1863 }
Mike Stump11289f42009-09-09 15:08:12 +00001864
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001865 // If we have a detailed preprocessing record, record the macro definition
1866 // ID that corresponds to this macro.
1867 if (PPRec)
1868 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1869
1870 Stream.EmitRecord(Code, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001871 Record.clear();
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001872
1873 // Emit the tokens array.
1874 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1875 // Note that we know that the preprocessor does not have any annotation
1876 // tokens in it because they are created by the parser, and thus can't
1877 // be in a macro definition.
1878 const Token &Tok = MI->getReplacementToken(TokNo);
1879
1880 Record.push_back(Tok.getLocation().getRawEncoding());
1881 Record.push_back(Tok.getLength());
1882
1883 // FIXME: When reading literal tokens, reconstruct the literal pointer
1884 // if it is needed.
1885 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1886 // FIXME: Should translate token kind to a stable encoding.
1887 Record.push_back(Tok.getKind());
1888 // FIXME: Should translate token flags to a stable encoding.
1889 Record.push_back(Tok.getFlags());
1890
1891 Stream.EmitRecord(PP_TOKEN, Record);
1892 Record.clear();
1893 }
1894 ++NumMacros;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001895 }
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001896 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001897 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001898
1899 // Write the offsets table for macro IDs.
1900 using namespace llvm;
1901 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1902 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1906
1907 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1908 Record.clear();
1909 Record.push_back(MACRO_OFFSET);
1910 Record.push_back(MacroOffsets.size());
1911 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1912 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1913 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001914}
1915
1916void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001917 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001918 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001919
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001920 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001921
Douglas Gregor92a96f52011-02-08 21:58:10 +00001922 // Enter the preprocessor block.
1923 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001924
Douglas Gregoraae92242010-03-19 21:51:54 +00001925 // If the preprocessor has a preprocessing record, emit it.
1926 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001927 using namespace llvm;
1928
1929 // Set up the abbreviation for
1930 unsigned InclusionAbbrev = 0;
1931 {
1932 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1933 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00001938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1939 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1940 }
1941
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001942 unsigned FirstPreprocessorEntityID
1943 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1944 + NUM_PREDEF_PP_ENTITY_IDS;
1945 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001946 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001947 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1948 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001949 E != EEnd;
1950 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001951 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001952
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001953 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1954 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001955
Douglas Gregor92a96f52011-02-08 21:58:10 +00001956 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001957 // Record this macro definition's ID.
1958 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001959
Douglas Gregor92a96f52011-02-08 21:58:10 +00001960 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001961 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1962 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001963 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001964
Chandler Carrutha88a22182011-07-14 08:20:46 +00001965 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001966 Record.push_back(ME->isBuiltinMacro());
1967 if (ME->isBuiltinMacro())
1968 AddIdentifierRef(ME->getName(), Record);
1969 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001970 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001971 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001972 continue;
1973 }
1974
1975 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1976 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001977 Record.push_back(ID->getFileName().size());
1978 Record.push_back(ID->wasInQuotes());
1979 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001980 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001981 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001982 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00001983 // Check that the FileEntry is not null because it was not resolved and
1984 // we create a PCH even with compiler errors.
1985 if (ID->getFile())
1986 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001987 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1988 continue;
1989 }
1990
1991 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1992 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001993 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001994
Douglas Gregoraae92242010-03-19 21:51:54 +00001995 // Write the offsets table for the preprocessing record.
1996 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001997 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1998
Douglas Gregoraae92242010-03-19 21:51:54 +00001999 // Write the offsets table for identifier IDs.
2000 using namespace llvm;
2001 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002002 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002005 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002006
Douglas Gregoraae92242010-03-19 21:51:54 +00002007 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002008 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002009 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002010 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2011 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002012 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002013}
2014
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002015unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2016 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2017 if (Known != SubmoduleIDs.end())
2018 return Known->second;
2019
2020 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2021}
2022
Douglas Gregor253eefe2011-12-01 00:59:36 +00002023/// \brief Compute the number of modules within the given tree (including the
2024/// given module).
2025static unsigned getNumberOfModules(Module *Mod) {
2026 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002027 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2028 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002029 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002030 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002031
2032 return ChildModules + 1;
2033}
2034
Douglas Gregorde3ef502011-11-30 23:21:26 +00002035void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002036 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002037 // FIXME: This feels like it belongs somewhere else, but there are no
2038 // other consumers of this information.
2039 SourceManager &SrcMgr = PP->getSourceManager();
2040 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2041 for (ASTContext::import_iterator I = Context->local_import_begin(),
2042 IEnd = Context->local_import_end();
2043 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002044 if (Module *ImportedFrom
2045 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2046 SrcMgr))) {
2047 ImportedFrom->Imports.push_back(I->getImportedModule());
2048 }
2049 }
2050
Douglas Gregor69021972011-11-30 17:33:56 +00002051 // Enter the submodule description block.
2052 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2053
2054 // Write the abbreviations needed for the submodules block.
2055 using namespace llvm;
2056 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2057 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00002066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2067 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2068
2069 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002070 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2072 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2073
2074 Abbrev = new BitCodeAbbrev();
2075 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2077 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002078
2079 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002080 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2082 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2083
2084 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002085 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2087 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2088
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002089 Abbrev = new BitCodeAbbrev();
2090 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2092 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2093
Douglas Gregor59527662012-10-15 06:28:11 +00002094 Abbrev = new BitCodeAbbrev();
2095 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2097 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2098
Douglas Gregor253eefe2011-12-01 00:59:36 +00002099 // Write the submodule metadata block.
2100 RecordData Record;
2101 Record.push_back(getNumberOfModules(WritingModule));
2102 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2103 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2104
Douglas Gregor69021972011-11-30 17:33:56 +00002105 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002106 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002107 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002108 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002109 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002110 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002111 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002112
2113 // Emit the definition of the block.
2114 Record.clear();
2115 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002116 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002117 if (Mod->Parent) {
2118 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2119 Record.push_back(SubmoduleIDs[Mod->Parent]);
2120 } else {
2121 Record.push_back(0);
2122 }
2123 Record.push_back(Mod->IsFramework);
2124 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002125 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002126 Record.push_back(Mod->InferSubmodules);
2127 Record.push_back(Mod->InferExplicitSubmodules);
2128 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00002129 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2130
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002131 // Emit the requirements.
2132 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2133 Record.clear();
2134 Record.push_back(SUBMODULE_REQUIRES);
2135 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2136 Mod->Requires[I].data(),
2137 Mod->Requires[I].size());
2138 }
2139
Douglas Gregor69021972011-11-30 17:33:56 +00002140 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002141 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002142 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002143 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002144 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002145 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002146 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2147 Record.clear();
2148 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2149 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2150 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002151 }
2152
2153 // Emit the headers.
2154 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2155 Record.clear();
2156 Record.push_back(SUBMODULE_HEADER);
2157 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2158 Mod->Headers[I]->getName());
2159 }
Douglas Gregor59527662012-10-15 06:28:11 +00002160 // Emit the excluded headers.
2161 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2162 Record.clear();
2163 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2164 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2165 Mod->ExcludedHeaders[I]->getName());
2166 }
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002167 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2168 Record.clear();
2169 Record.push_back(SUBMODULE_TOPHEADER);
2170 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2171 Mod->TopHeaders[I]->getName());
2172 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002173
2174 // Emit the imports.
2175 if (!Mod->Imports.empty()) {
2176 Record.clear();
2177 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002178 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002179 assert(ImportedID && "Unknown submodule!");
2180 Record.push_back(ImportedID);
2181 }
2182 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2183 }
2184
Douglas Gregor24bb9232011-12-02 18:58:38 +00002185 // Emit the exports.
2186 if (!Mod->Exports.empty()) {
2187 Record.clear();
2188 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002189 if (Module *Exported = Mod->Exports[I].getPointer()) {
2190 unsigned ExportedID = SubmoduleIDs[Exported];
2191 assert(ExportedID > 0 && "Unknown submodule ID?");
2192 Record.push_back(ExportedID);
2193 } else {
2194 Record.push_back(0);
2195 }
2196
Douglas Gregor24bb9232011-12-02 18:58:38 +00002197 Record.push_back(Mod->Exports[I].getInt());
2198 }
2199 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2200 }
2201
Douglas Gregor69021972011-11-30 17:33:56 +00002202 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002203 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2204 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002205 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002206 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002207 }
2208
2209 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002210
2211 assert((NextSubmoduleID - FirstSubmoduleID
2212 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002213}
2214
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002215serialization::SubmoduleID
2216ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002217 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002218 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002219
2220 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002221 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002222 Module *OwningMod
2223 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002224 if (!OwningMod)
2225 return 0;
2226
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002227 // Check whether this submodule is part of our own module.
2228 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002229 return 0;
2230
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002231 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002232}
2233
David Blaikie9c902b52011-09-25 23:23:43 +00002234void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002235 // FIXME: Make it work properly with modules.
2236 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2237 DiagStateIDMap;
2238 unsigned CurrID = 0;
2239 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002240 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002241 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002242 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2243 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002244 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002245 if (point.Loc.isInvalid())
2246 continue;
2247
2248 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002249 unsigned &DiagStateID = DiagStateIDMap[point.State];
2250 Record.push_back(DiagStateID);
2251
2252 if (DiagStateID == 0) {
2253 DiagStateID = ++CurrID;
2254 for (DiagnosticsEngine::DiagState::const_iterator
2255 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2256 if (I->second.isPragma()) {
2257 Record.push_back(I->first);
2258 Record.push_back(I->second.getMapping());
2259 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002260 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002261 Record.push_back(-1); // mark the end of the diag/map pairs for this
2262 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002263 }
2264 }
2265
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002266 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002267 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002268}
2269
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002270void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2271 if (CXXBaseSpecifiersOffsets.empty())
2272 return;
2273
2274 RecordData Record;
2275
2276 // Create a blob abbreviation for the C++ base specifiers offsets.
2277 using namespace llvm;
2278
2279 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2280 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2283 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2284
Douglas Gregorc27b2872011-08-04 00:01:48 +00002285 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002286 Record.clear();
2287 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2288 Record.push_back(CXXBaseSpecifiersOffsets.size());
2289 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002290 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002291}
2292
Douglas Gregorc5046832009-04-27 18:38:38 +00002293//===----------------------------------------------------------------------===//
2294// Type Serialization
2295//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002296
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002297/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002298void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002299 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002300 if (Idx.getIndex() == 0) // we haven't seen this type before.
2301 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002302
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002303 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002304
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002305 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002306 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002307 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002308 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002309 else if (TypeOffsets.size() < Index) {
2310 TypeOffsets.resize(Index + 1);
2311 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002312 }
2313
2314 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002315
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002316 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002317 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002318
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002319 if (T.hasLocalNonFastQualifiers()) {
2320 Qualifiers Qs = T.getLocalQualifiers();
2321 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002322 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002323 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002324 } else {
2325 switch (T->getTypeClass()) {
2326 // For all of the concrete, non-dependent types, call the
2327 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002328#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002329 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002330#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002331#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002332 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002333 }
2334
2335 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002336 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002337
2338 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002339 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002340}
2341
Douglas Gregorc5046832009-04-27 18:38:38 +00002342//===----------------------------------------------------------------------===//
2343// Declaration Serialization
2344//===----------------------------------------------------------------------===//
2345
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002346/// \brief Write the block containing all of the declaration IDs
2347/// lexically declared within the given DeclContext.
2348///
2349/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2350/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002351uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002352 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002353 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002354 return 0;
2355
Douglas Gregor8f45df52009-04-16 22:23:12 +00002356 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002357 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002358 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002359 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002360 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2361 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002362 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002363
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002364 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002365 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002366 return Offset;
2367}
2368
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002369void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002370 using namespace llvm;
2371 RecordData Record;
2372
2373 // Write the type offsets array
2374 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002375 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2379 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2380 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002381 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002382 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002383 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002384 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002385
2386 // Write the declaration offsets array
2387 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002388 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2392 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2393 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002394 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002395 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002396 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002397 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002398}
2399
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002400void ASTWriter::WriteFileDeclIDsMap() {
2401 using namespace llvm;
2402 RecordData Record;
2403
2404 // Join the vectors of DeclIDs from all files.
2405 SmallVector<DeclID, 256> FileSortedIDs;
2406 for (FileDeclIDsTy::iterator
2407 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2408 DeclIDInFileInfo &Info = *FI->second;
2409 Info.FirstDeclIndex = FileSortedIDs.size();
2410 for (LocDeclIDsTy::iterator
2411 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2412 FileSortedIDs.push_back(DI->second);
2413 }
2414
2415 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2416 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2419 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2420 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002421 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002422 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2423}
2424
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002425void ASTWriter::WriteComments() {
2426 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002427 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002428 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002429 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2430 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002431 I != E; ++I) {
2432 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002433 AddSourceRange((*I)->getSourceRange(), Record);
2434 Record.push_back((*I)->getKind());
2435 Record.push_back((*I)->isTrailingComment());
2436 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002437 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2438 }
2439 Stream.ExitBlock();
2440}
2441
Douglas Gregorc5046832009-04-27 18:38:38 +00002442//===----------------------------------------------------------------------===//
2443// Global Method Pool and Selector Serialization
2444//===----------------------------------------------------------------------===//
2445
Douglas Gregore84a9da2009-04-20 20:36:09 +00002446namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002447// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002448class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002449 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002450
2451public:
2452 typedef Selector key_type;
2453 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002454
Sebastian Redl834bb972010-08-04 17:20:04 +00002455 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002456 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002457 ObjCMethodList Instance, Factory;
2458 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002459 typedef const data_type& data_type_ref;
2460
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002461 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002462
Douglas Gregorc78d3462009-04-24 21:10:55 +00002463 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002464 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002465 }
Mike Stump11289f42009-09-09 15:08:12 +00002466
2467 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002468 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002469 data_type_ref Methods) {
2470 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2471 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002472 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2473 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002474 Method = Method->Next)
2475 if (Method->Method)
2476 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002477 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002478 Method = Method->Next)
2479 if (Method->Method)
2480 DataLen += 4;
2481 clang::io::Emit16(Out, DataLen);
2482 return std::make_pair(KeyLen, DataLen);
2483 }
Mike Stump11289f42009-09-09 15:08:12 +00002484
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002485 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002486 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002487 assert((Start >> 32) == 0 && "Selector key offset too large");
2488 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002489 unsigned N = Sel.getNumArgs();
2490 clang::io::Emit16(Out, N);
2491 if (N == 0)
2492 N = 1;
2493 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002494 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002495 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2496 }
Mike Stump11289f42009-09-09 15:08:12 +00002497
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002498 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002499 data_type_ref Methods, unsigned DataLen) {
2500 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002501 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002502 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002503 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002504 Method = Method->Next)
2505 if (Method->Method)
2506 ++NumInstanceMethods;
2507
2508 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002509 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002510 Method = Method->Next)
2511 if (Method->Method)
2512 ++NumFactoryMethods;
2513
2514 clang::io::Emit16(Out, NumInstanceMethods);
2515 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002516 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002517 Method = Method->Next)
2518 if (Method->Method)
2519 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002520 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002521 Method = Method->Next)
2522 if (Method->Method)
2523 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002524
2525 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002526 }
2527};
2528} // end anonymous namespace
2529
Sebastian Redla19a67f2010-08-03 21:58:15 +00002530/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002531///
2532/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002533/// in an on-disk hash table indexed by the selector. The hash table also
2534/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002535void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002536 using namespace llvm;
2537
Sebastian Redla19a67f2010-08-03 21:58:15 +00002538 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002539 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002540 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002541 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002542 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002543 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002544 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002545 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002546
Sebastian Redla19a67f2010-08-03 21:58:15 +00002547 // Create the on-disk hash table representation. We walk through every
2548 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002549 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002550 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002551 I = SelectorIDs.begin(), E = SelectorIDs.end();
2552 I != E; ++I) {
2553 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002554 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002555 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002556 I->second,
2557 ObjCMethodList(),
2558 ObjCMethodList()
2559 };
2560 if (F != SemaRef.MethodPool.end()) {
2561 Data.Instance = F->second.first;
2562 Data.Factory = F->second.second;
2563 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002564 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002565 // changed.
2566 if (Chain && I->second < FirstSelectorID) {
2567 // Selector already exists. Did it change?
2568 bool changed = false;
2569 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2570 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002571 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002572 changed = true;
2573 }
2574 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2575 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002576 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002577 changed = true;
2578 }
2579 if (!changed)
2580 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002581 } else if (Data.Instance.Method || Data.Factory.Method) {
2582 // A new method pool entry.
2583 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002584 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002585 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002586 }
2587
Douglas Gregorc78d3462009-04-24 21:10:55 +00002588 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002589 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002590 uint32_t BucketOffset;
2591 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002592 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002593 llvm::raw_svector_ostream Out(MethodPool);
2594 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002595 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002596 BucketOffset = Generator.Emit(Out, Trait);
2597 }
2598
2599 // Create a blob abbreviation
2600 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002601 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2605 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2606
Douglas Gregor95c13f52009-04-25 17:48:32 +00002607 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002608 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002609 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002610 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002611 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002612 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002613
2614 // Create a blob abbreviation for the selector table offsets.
2615 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002616 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2620 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2621
2622 // Write the selector offsets table.
2623 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002624 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002625 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002626 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002627 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002628 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002629 }
2630}
2631
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002632/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002633void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002634 using namespace llvm;
2635 if (SemaRef.ReferencedSelectors.empty())
2636 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002637
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002638 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002639
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002640 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002641 // very tricky to fix, and given that @selector shouldn't really appear in
2642 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002643 for (DenseMap<Selector, SourceLocation>::iterator S =
2644 SemaRef.ReferencedSelectors.begin(),
2645 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2646 Selector Sel = (*S).first;
2647 SourceLocation Loc = (*S).second;
2648 AddSelectorRef(Sel, Record);
2649 AddSourceLocation(Loc, Record);
2650 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002651 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002652}
2653
Douglas Gregorc5046832009-04-27 18:38:38 +00002654//===----------------------------------------------------------------------===//
2655// Identifier Table Serialization
2656//===----------------------------------------------------------------------===//
2657
Douglas Gregorc78d3462009-04-24 21:10:55 +00002658namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002659class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002660 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002661 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002662 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002663 bool IsModule;
2664
Douglas Gregor1d583f22009-04-28 21:18:29 +00002665 /// \brief Determines whether this is an "interesting" identifier
2666 /// that needs a full IdentifierInfo structure written into the hash
2667 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002668 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002669 if (II->isPoisoned() ||
2670 II->isExtensionToken() ||
2671 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002672 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002673 II->getFETokenInfo<void>())
2674 return true;
2675
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002676 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002677 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002678
2679 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2680 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002681 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002682
2683 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002684 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002685
2686 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002687 }
2688
Douglas Gregore84a9da2009-04-20 20:36:09 +00002689public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002690 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002691 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002692
Sebastian Redl539c5062010-08-18 23:57:32 +00002693 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002694 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002695
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002696 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2697 IdentifierResolver &IdResolver, bool IsModule)
2698 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002699
2700 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002701 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002702 }
Mike Stump11289f42009-09-09 15:08:12 +00002703
2704 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002705 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002706 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002707 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002708 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002709 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002710 DataLen += 2; // 2 bytes for builtin ID
2711 DataLen += 2; // 2 bytes for flags
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002712 if (hadMacroDefinition(II, Macro)) {
2713 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2714 if (Writer.getMacroRef(M) != 0)
2715 DataLen += 4;
2716 }
2717
2718 DataLen += 4;
2719 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002720
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002721 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2722 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002723 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002724 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002725 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002726 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002727 // We emit the key length after the data length so that every
2728 // string is preceded by a 16-bit length. This matches the PTH
2729 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002730 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002731 return std::make_pair(KeyLen, DataLen);
2732 }
Mike Stump11289f42009-09-09 15:08:12 +00002733
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002734 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002735 unsigned KeyLen) {
2736 // Record the location of the key data. This is used when generating
2737 // the mapping from persistent IDs to strings.
2738 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002739 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002740 }
Mike Stump11289f42009-09-09 15:08:12 +00002741
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002742 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002743 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002744 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002745 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002746 clang::io::Emit32(Out, ID << 1);
2747 return;
2748 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002749
Douglas Gregor1d583f22009-04-28 21:18:29 +00002750 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002751 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2752 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2753 clang::io::Emit16(Out, Bits);
2754 Bits = 0;
2755 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002756 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002757 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2758 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002759 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002760 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002761 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002762
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002763 if (HadMacroDefinition) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002764 // Write all of the macro IDs associated with this identifier.
2765 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2766 if (MacroID ID = Writer.getMacroRef(M))
2767 clang::io::Emit32(Out, ID);
2768 }
2769
2770 clang::io::Emit32(Out, 0);
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002771 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002772
Douglas Gregora868bbd2009-04-21 22:25:48 +00002773 // Emit the declaration IDs in reverse order, because the
2774 // IdentifierResolver provides the declarations as they would be
2775 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002776 // "stat"), but the ASTReader adds declarations to the end of the list
2777 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002778 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002779 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2780 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002781 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002782 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002783 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002784 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002785 }
2786};
2787} // end anonymous namespace
2788
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002789/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002790///
2791/// The identifier table consists of a blob containing string data
2792/// (the actual identifiers themselves) and a separate "offsets" index
2793/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002794void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2795 IdentifierResolver &IdResolver,
2796 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002797 using namespace llvm;
2798
2799 // Create and write out the blob that contains the identifier
2800 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002801 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002802 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002803 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002804
Douglas Gregore6648fb2009-04-28 20:33:11 +00002805 // Look for any identifiers that were named while processing the
2806 // headers, but are otherwise not needed. We add these to the hash
2807 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002808 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002809 // file.
2810 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2811 IDEnd = PP.getIdentifierTable().end();
2812 ID != IDEnd; ++ID)
2813 getIdentifierRef(ID->second);
2814
Sebastian Redlff4a2952010-07-23 23:49:55 +00002815 // Create the on-disk hash table representation. We only store offsets
2816 // for identifiers that appear here for the first time.
2817 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002818 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002819 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2820 ID != IDEnd; ++ID) {
2821 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002822 if (!Chain || !ID->first->isFromAST() ||
2823 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002824 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2825 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002826 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002827
Douglas Gregore84a9da2009-04-20 20:36:09 +00002828 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002829 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002830 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002831 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002832 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002833 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002834 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002835 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002836 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002837 }
2838
2839 // Create a blob abbreviation
2840 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002841 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002844 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002845
2846 // Write the identifier table
2847 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002848 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002849 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002850 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002851 }
2852
2853 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002854 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002855 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002856 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002857 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002858 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2859 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2860
2861 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002862 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002863 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002864 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002865 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002866 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002867}
2868
Douglas Gregorc5046832009-04-27 18:38:38 +00002869//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002870// DeclContext's Name Lookup Table Serialization
2871//===----------------------------------------------------------------------===//
2872
2873namespace {
2874// Trait used for the on-disk hash table used in the method pool.
2875class ASTDeclContextNameLookupTrait {
2876 ASTWriter &Writer;
2877
2878public:
2879 typedef DeclarationName key_type;
2880 typedef key_type key_type_ref;
2881
2882 typedef DeclContext::lookup_result data_type;
2883 typedef const data_type& data_type_ref;
2884
2885 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2886
2887 unsigned ComputeHash(DeclarationName Name) {
2888 llvm::FoldingSetNodeID ID;
2889 ID.AddInteger(Name.getNameKind());
2890
2891 switch (Name.getNameKind()) {
2892 case DeclarationName::Identifier:
2893 ID.AddString(Name.getAsIdentifierInfo()->getName());
2894 break;
2895 case DeclarationName::ObjCZeroArgSelector:
2896 case DeclarationName::ObjCOneArgSelector:
2897 case DeclarationName::ObjCMultiArgSelector:
2898 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2899 break;
2900 case DeclarationName::CXXConstructorName:
2901 case DeclarationName::CXXDestructorName:
2902 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002903 break;
2904 case DeclarationName::CXXOperatorName:
2905 ID.AddInteger(Name.getCXXOverloadedOperator());
2906 break;
2907 case DeclarationName::CXXLiteralOperatorName:
2908 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2909 case DeclarationName::CXXUsingDirective:
2910 break;
2911 }
2912
2913 return ID.ComputeHash();
2914 }
2915
2916 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002917 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002918 data_type_ref Lookup) {
2919 unsigned KeyLen = 1;
2920 switch (Name.getNameKind()) {
2921 case DeclarationName::Identifier:
2922 case DeclarationName::ObjCZeroArgSelector:
2923 case DeclarationName::ObjCOneArgSelector:
2924 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002925 case DeclarationName::CXXLiteralOperatorName:
2926 KeyLen += 4;
2927 break;
2928 case DeclarationName::CXXOperatorName:
2929 KeyLen += 1;
2930 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002931 case DeclarationName::CXXConstructorName:
2932 case DeclarationName::CXXDestructorName:
2933 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002934 case DeclarationName::CXXUsingDirective:
2935 break;
2936 }
2937 clang::io::Emit16(Out, KeyLen);
2938
2939 // 2 bytes for num of decls and 4 for each DeclID.
2940 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2941 clang::io::Emit16(Out, DataLen);
2942
2943 return std::make_pair(KeyLen, DataLen);
2944 }
2945
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002946 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002947 using namespace clang::io;
2948
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002949 Emit8(Out, Name.getNameKind());
2950 switch (Name.getNameKind()) {
2951 case DeclarationName::Identifier:
2952 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002953 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002954 case DeclarationName::ObjCZeroArgSelector:
2955 case DeclarationName::ObjCOneArgSelector:
2956 case DeclarationName::ObjCMultiArgSelector:
2957 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002958 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002959 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002960 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2961 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002962 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00002963 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002964 case DeclarationName::CXXLiteralOperatorName:
2965 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002966 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002967 case DeclarationName::CXXConstructorName:
2968 case DeclarationName::CXXDestructorName:
2969 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002970 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002971 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002972 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00002973
2974 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002975 }
2976
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002977 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002978 data_type Lookup, unsigned DataLen) {
2979 uint64_t Start = Out.tell(); (void)Start;
2980 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2981 for (; Lookup.first != Lookup.second; ++Lookup.first)
2982 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2983
2984 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2985 }
2986};
2987} // end anonymous namespace
2988
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002989/// \brief Write the block containing all of the declaration IDs
2990/// visible from the given DeclContext.
2991///
2992/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002993/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002994uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2995 DeclContext *DC) {
2996 if (DC->getPrimaryContext() != DC)
2997 return 0;
2998
2999 // Since there is no name lookup into functions or methods, don't bother to
3000 // build a visible-declarations table for these entities.
3001 if (DC->isFunctionOrMethod())
3002 return 0;
3003
3004 // If not in C++, we perform name lookup for the translation unit via the
3005 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3006 // FIXME: In C++ we need the visible declarations in order to "see" the
3007 // friend declarations, is there a way to do this without writing the table ?
David Blaikiebbafb8a2012-03-11 07:00:24 +00003008 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003009 return 0;
3010
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003011 // Serialize the contents of the mapping used for lookup. Note that,
3012 // although we have two very different code paths, the serialized
3013 // representation is the same for both cases: a declaration name,
3014 // followed by a size, followed by references to the visible
3015 // declarations that have that name.
3016 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003017 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003018 if (!Map || Map->empty())
3019 return 0;
3020
3021 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3022 ASTDeclContextNameLookupTrait Trait(*this);
3023
3024 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003025 DeclarationName ConversionName;
3026 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003027 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3028 D != DEnd; ++D) {
3029 DeclarationName Name = D->first;
3030 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00003031 if (Result.first != Result.second) {
3032 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3033 // Hash all conversion function names to the same name. The actual
3034 // type information in conversion function name is not used in the
3035 // key (since such type information is not stable across different
3036 // modules), so the intended effect is to coalesce all of the conversion
3037 // functions under a single key.
3038 if (!ConversionName)
3039 ConversionName = Name;
3040 ConversionDecls.append(Result.first, Result.second);
3041 continue;
3042 }
3043
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003044 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003045 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003046 }
3047
Douglas Gregor05ef9312011-08-30 20:49:19 +00003048 // Add the conversion functions
3049 if (!ConversionDecls.empty()) {
3050 Generator.insert(ConversionName,
3051 DeclContext::lookup_result(ConversionDecls.begin(),
3052 ConversionDecls.end()),
3053 Trait);
3054 }
3055
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003056 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003057 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003058 uint32_t BucketOffset;
3059 {
3060 llvm::raw_svector_ostream Out(LookupTable);
3061 // Make sure that no bucket is at offset 0
3062 clang::io::Emit32(Out, 0);
3063 BucketOffset = Generator.Emit(Out, Trait);
3064 }
3065
3066 // Write the lookup table
3067 RecordData Record;
3068 Record.push_back(DECL_CONTEXT_VISIBLE);
3069 Record.push_back(BucketOffset);
3070 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3071 LookupTable.str());
3072
3073 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3074 ++NumVisibleDeclContexts;
3075 return Offset;
3076}
3077
Sebastian Redla4071b42010-08-24 00:50:09 +00003078/// \brief Write an UPDATE_VISIBLE block for the given context.
3079///
3080/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3081/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003082/// (in C++), for namespaces, and for classes with forward-declared unscoped
3083/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003084void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003085 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3086 if (!Map || Map->empty())
3087 return;
3088
3089 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3090 ASTDeclContextNameLookupTrait Trait(*this);
3091
3092 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003093 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3094 D != DEnd; ++D) {
3095 DeclarationName Name = D->first;
3096 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003097 // For any name that appears in this table, the results are complete, i.e.
3098 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003099 if (Result.first != Result.second)
3100 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003101 }
3102
3103 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003104 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003105 uint32_t BucketOffset;
3106 {
3107 llvm::raw_svector_ostream Out(LookupTable);
3108 // Make sure that no bucket is at offset 0
3109 clang::io::Emit32(Out, 0);
3110 BucketOffset = Generator.Emit(Out, Trait);
3111 }
3112
3113 // Write the lookup table
3114 RecordData Record;
3115 Record.push_back(UPDATE_VISIBLE);
3116 Record.push_back(getDeclID(cast<Decl>(DC)));
3117 Record.push_back(BucketOffset);
3118 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3119}
3120
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003121/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3122void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3123 RecordData Record;
3124 Record.push_back(Opts.fp_contract);
3125 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3126}
3127
3128/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3129void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003130 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003131 return;
3132
3133 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3134 RecordData Record;
3135#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3136#include "clang/Basic/OpenCLExtensions.def"
3137 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3138}
3139
Douglas Gregor358cd442012-01-15 16:58:34 +00003140void ASTWriter::WriteRedeclarations() {
3141 RecordData LocalRedeclChains;
3142 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3143
3144 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3145 Decl *First = Redeclarations[I];
3146 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3147
3148 Decl *MostRecent = First->getMostRecentDecl();
3149
3150 // If we only have a single declaration, there is no point in storing
3151 // a redeclaration chain.
3152 if (First == MostRecent)
3153 continue;
3154
3155 unsigned Offset = LocalRedeclChains.size();
3156 unsigned Size = 0;
3157 LocalRedeclChains.push_back(0); // Placeholder for the size.
3158
3159 // Collect the set of local redeclarations of this declaration.
3160 for (Decl *Prev = MostRecent; Prev != First;
3161 Prev = Prev->getPreviousDecl()) {
3162 if (!Prev->isFromASTFile()) {
3163 AddDeclRef(Prev, LocalRedeclChains);
3164 ++Size;
3165 }
3166 }
3167 LocalRedeclChains[Offset] = Size;
3168
3169 // Reverse the set of local redeclarations, so that we store them in
3170 // order (since we found them in reverse order).
3171 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3172
3173 // Add the mapping from the first ID to the set of local declarations.
3174 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3175 LocalRedeclsMap.push_back(Info);
3176
3177 assert(N == Redeclarations.size() &&
3178 "Deserialized a declaration we shouldn't have");
3179 }
3180
3181 if (LocalRedeclChains.empty())
3182 return;
3183
3184 // Sort the local redeclarations map by the first declaration ID,
3185 // since the reader will be performing binary searches on this information.
3186 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3187
3188 // Emit the local redeclarations map.
3189 using namespace llvm;
3190 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3191 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3194 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3195
3196 RecordData Record;
3197 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3198 Record.push_back(LocalRedeclsMap.size());
3199 Stream.EmitRecordWithBlob(AbbrevID, Record,
3200 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3201 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3202
3203 // Emit the redeclaration chains.
3204 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3205}
3206
Douglas Gregor404cdde2012-01-27 01:47:08 +00003207void ASTWriter::WriteObjCCategories() {
3208 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3209 RecordData Categories;
3210
3211 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3212 unsigned Size = 0;
3213 unsigned StartIndex = Categories.size();
3214
3215 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3216
3217 // Allocate space for the size.
3218 Categories.push_back(0);
3219
3220 // Add the categories.
3221 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3222 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3223 assert(getDeclID(Cat) != 0 && "Bogus category");
3224 AddDeclRef(Cat, Categories);
3225 }
3226
3227 // Update the size.
3228 Categories[StartIndex] = Size;
3229
3230 // Record this interface -> category map.
3231 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3232 CategoriesMap.push_back(CatInfo);
3233 }
3234
3235 // Sort the categories map by the definition ID, since the reader will be
3236 // performing binary searches on this information.
3237 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3238
3239 // Emit the categories map.
3240 using namespace llvm;
3241 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3242 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3245 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3246
3247 RecordData Record;
3248 Record.push_back(OBJC_CATEGORIES_MAP);
3249 Record.push_back(CategoriesMap.size());
3250 Stream.EmitRecordWithBlob(AbbrevID, Record,
3251 reinterpret_cast<char*>(CategoriesMap.data()),
3252 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3253
3254 // Emit the category lists.
3255 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3256}
3257
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003258void ASTWriter::WriteMergedDecls() {
3259 if (!Chain || Chain->MergedDecls.empty())
3260 return;
3261
3262 RecordData Record;
3263 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3264 IEnd = Chain->MergedDecls.end();
3265 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003266 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003267 : getDeclID(I->first);
3268 assert(CanonID && "Merged declaration not known?");
3269
3270 Record.push_back(CanonID);
3271 Record.push_back(I->second.size());
3272 Record.append(I->second.begin(), I->second.end());
3273 }
3274 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3275}
3276
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003277//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003278// General Serialization Routines
3279//===----------------------------------------------------------------------===//
3280
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003281/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003282void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3283 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003284 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003285 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3286 e = Attrs.end(); i != e; ++i){
3287 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003288 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003289 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003290
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003291#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003292
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003293 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003294}
3295
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003296void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003297 Record.push_back(Str.size());
3298 Record.insert(Record.end(), Str.begin(), Str.end());
3299}
3300
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003301void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3302 RecordDataImpl &Record) {
3303 Record.push_back(Version.getMajor());
3304 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3305 Record.push_back(*Minor + 1);
3306 else
3307 Record.push_back(0);
3308 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3309 Record.push_back(*Subminor + 1);
3310 else
3311 Record.push_back(0);
3312}
3313
Douglas Gregore84a9da2009-04-20 20:36:09 +00003314/// \brief Note that the identifier II occurs at the given offset
3315/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003316void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003317 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003318 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003319 // up earlier in the chain and thus don't need an offset.
3320 if (ID >= FirstIdentID)
3321 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003322}
3323
Douglas Gregor95c13f52009-04-25 17:48:32 +00003324/// \brief Note that the selector Sel occurs at the given offset
3325/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003326void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003327 unsigned ID = SelectorIDs[Sel];
3328 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003329 // Don't record offsets for selectors that are also available in a different
3330 // file.
3331 if (ID < FirstSelectorID)
3332 return;
3333 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003334}
3335
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003336ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003337 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003338 WritingAST(false), DoneWritingDeclsAndTypes(false),
3339 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003340 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003341 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003342 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3343 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003344 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3345 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003346 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003347 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003348 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003349 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003350 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003351 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003352 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3353 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3354 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003355 DeclTypedefAbbrev(0),
3356 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3357 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003358{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003359}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003360
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003361ASTWriter::~ASTWriter() {
3362 for (FileDeclIDsTy::iterator
3363 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3364 delete I->second;
3365}
3366
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003367void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003368 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003369 Module *WritingModule, StringRef isysroot,
3370 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003371 WritingAST = true;
3372
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003373 ASTHasCompilerErrors = hasErrors;
3374
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003375 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003376 Stream.Emit((unsigned)'C', 8);
3377 Stream.Emit((unsigned)'P', 8);
3378 Stream.Emit((unsigned)'C', 8);
3379 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003380
Chris Lattner28fa4e62009-04-26 22:26:21 +00003381 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003382
Douglas Gregoreda8e122011-08-09 15:13:55 +00003383 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003384 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003385 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003386 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003387 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003388 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003389 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003390
3391 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003392}
3393
Douglas Gregora94a1542011-07-27 21:45:57 +00003394template<typename Vector>
3395static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3396 ASTWriter::RecordData &Record) {
3397 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3398 I != E; ++I) {
3399 Writer.AddDeclRef(*I, Record);
3400 }
3401}
3402
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003403void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003404 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003405 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003406 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003407 using namespace llvm;
3408
Douglas Gregorcf68c582011-12-01 22:20:10 +00003409 // Make sure that the AST reader knows to finalize itself.
3410 if (Chain)
3411 Chain->finalizeForWriting();
3412
Sebastian Redl143413f2010-07-12 22:02:52 +00003413 ASTContext &Context = SemaRef.Context;
3414 Preprocessor &PP = SemaRef.PP;
3415
Douglas Gregordab42432011-08-12 00:15:20 +00003416 // Set up predefined declaration IDs.
3417 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003418 if (Context.ObjCIdDecl)
3419 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003420 if (Context.ObjCSelDecl)
3421 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003422 if (Context.ObjCClassDecl)
3423 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003424 if (Context.ObjCProtocolClassDecl)
3425 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003426 if (Context.Int128Decl)
3427 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3428 if (Context.UInt128Decl)
3429 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003430 if (Context.ObjCInstanceTypeDecl)
3431 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003432 if (Context.BuiltinVaListDecl)
3433 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3434
Douglas Gregor851443c2011-08-12 01:39:19 +00003435 if (!Chain) {
3436 // Make sure that we emit IdentifierInfos (and any attached
3437 // declarations) for builtins. We don't need to do this when we're
3438 // emitting chained PCH files, because all of the builtins will be
3439 // in the original PCH file.
3440 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003441 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003442 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003443 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003444 Context.getLangOpts().NoBuiltin);
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003445 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3446 getIdentifierRef(&Table.get(BuiltinNames[I]));
3447 }
3448
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003449 // If there are any out-of-date identifiers, bring them up to date.
3450 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3451 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3452 IDEnd = PP.getIdentifierTable().end();
3453 ID != IDEnd; ++ID)
3454 if (ID->second->isOutOfDate())
3455 ExtSource->updateOutOfDateIdentifier(*ID->second);
3456 }
3457
Chris Lattner0c797362009-09-08 18:19:27 +00003458 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003459 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003460 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003461 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003462 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003463
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003464 // Build a record containing all of the file scoped decls in this file.
3465 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003466 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3467 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003468
Douglas Gregor851443c2011-08-12 01:39:19 +00003469 // Build a record containing all of the delegating constructors we still need
3470 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003471 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003472 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003473
Douglas Gregor851443c2011-08-12 01:39:19 +00003474 // Write the set of weak, undeclared identifiers. We always write the
3475 // entire table, since later PCH files in a PCH chain are only interested in
3476 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003477 RecordData WeakUndeclaredIdentifiers;
3478 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003479 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003480 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3481 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3482 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3483 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3484 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3485 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3486 }
3487 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003488
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003489 // Build a record containing all of the locally-scoped external
3490 // declarations in this header file. Generally, this record will be
3491 // empty.
3492 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003493 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003494 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003495 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003496 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3497 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003498 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003499 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003500 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3501 }
3502
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003503 // Build a record containing all of the ext_vector declarations.
3504 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003505 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003506
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003507 // Build a record containing all of the VTable uses information.
3508 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003509 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003510 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3511 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3512 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3513 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3514 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003515 }
3516
3517 // Build a record containing all of dynamic classes declarations.
3518 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003519 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003520
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003521 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003522 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003523 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003524 I = SemaRef.PendingInstantiations.begin(),
3525 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3526 AddDeclRef(I->first, PendingInstantiations);
3527 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003528 }
3529 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3530 "There are local ones at end of translation unit!");
3531
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003532 // Build a record containing some declaration references.
3533 RecordData SemaDeclRefs;
3534 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3535 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3536 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3537 }
3538
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003539 RecordData CUDASpecialDeclRefs;
3540 if (Context.getcudaConfigureCallDecl()) {
3541 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3542 }
3543
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003544 // Build a record containing all of the known namespaces.
3545 RecordData KnownNamespaces;
3546 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3547 I = SemaRef.KnownNamespaces.begin(),
3548 IEnd = SemaRef.KnownNamespaces.end();
3549 I != IEnd; ++I) {
3550 if (!I->second)
3551 AddDeclRef(I->first, KnownNamespaces);
3552 }
Douglas Gregor112b9072012-10-18 05:31:06 +00003553
3554 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00003555 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00003556
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003557 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003558 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003559 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00003560
3561 // Create a lexical update block containing all of the declarations in the
3562 // translation unit that do not come from other AST files.
3563 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3564 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3565 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3566 E = TU->noload_decls_end();
3567 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003568 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003569 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003570 }
3571
3572 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3573 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3574 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3575 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3576 Record.clear();
3577 Record.push_back(TU_UPDATE_LEXICAL);
3578 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3579 data(NewGlobalDecls));
3580
3581 // And a visible updates block for the translation unit.
3582 Abv = new llvm::BitCodeAbbrev();
3583 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3584 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3585 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3586 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3587 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3588 WriteDeclContextVisibleUpdate(TU);
3589
3590 // If the translation unit has an anonymous namespace, and we don't already
3591 // have an update block for it, write it as an update block.
3592 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3593 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3594 if (Record.empty()) {
3595 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003596 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003597 }
3598 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003599
3600 // Make sure visible decls, added to DeclContexts previously loaded from
3601 // an AST file, are registered for serialization.
3602 for (SmallVector<const Decl *, 16>::iterator
3603 I = UpdatingVisibleDecls.begin(),
3604 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3605 GetDeclRef(*I);
3606 }
3607
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003608 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003609 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003610
Douglas Gregor5204bde2011-08-02 16:26:37 +00003611 // Form the record of special types.
3612 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00003613 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003614 AddTypeRef(Context.getFILEType(), SpecialTypes);
3615 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3616 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3617 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3618 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003619 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003620 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003621
Douglas Gregor1970d882009-04-26 03:49:13 +00003622 // Keep writing types and declarations until all types and
3623 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003624 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003625 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003626 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3627 E = DeclsToRewrite.end();
3628 I != E; ++I)
3629 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003630 while (!DeclTypesToEmit.empty()) {
3631 DeclOrType DOT = DeclTypesToEmit.front();
3632 DeclTypesToEmit.pop();
3633 if (DOT.isType())
3634 WriteType(DOT.getType());
3635 else
3636 WriteDecl(Context, DOT.getDecl());
3637 }
3638 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003639
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003640 DoneWritingDeclsAndTypes = true;
3641
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003642 WriteFileDeclIDsMap();
3643 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003644 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003645
3646 if (Chain) {
3647 // Write the mapping information describing our module dependencies and how
3648 // each of those modules were mapped into our own offset/ID space, so that
3649 // the reader can build the appropriate mapping to its own offset/ID space.
3650 // The map consists solely of a blob with the following format:
3651 // *(module-name-len:i16 module-name:len*i8
3652 // source-location-offset:i32
3653 // identifier-id:i32
3654 // preprocessed-entity-id:i32
3655 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003656 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003657 // selector-id:i32
3658 // declaration-id:i32
3659 // c++-base-specifiers-id:i32
3660 // type-id:i32)
3661 //
3662 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3663 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3665 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003666 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003667 {
3668 llvm::raw_svector_ostream Out(Buffer);
3669 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003670 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003671 M != MEnd; ++M) {
3672 StringRef FileName = (*M)->FileName;
3673 io::Emit16(Out, FileName.size());
3674 Out.write(FileName.data(), FileName.size());
3675 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3676 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003677 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003678 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003679 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003680 io::Emit32(Out, (*M)->BaseSelectorID);
3681 io::Emit32(Out, (*M)->BaseDeclID);
3682 io::Emit32(Out, (*M)->BaseTypeIndex);
3683 }
3684 }
3685 Record.clear();
3686 Record.push_back(MODULE_OFFSET_MAP);
3687 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3688 Buffer.data(), Buffer.size());
3689 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003690 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003691 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003692 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003693 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003694 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003695 WriteFPPragmaOptions(SemaRef.getFPOptions());
3696 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003697
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003698 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003699 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003700
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003701 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003702
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003703 // If we're emitting a module, write out the submodule information.
3704 if (WritingModule)
3705 WriteSubmodules(WritingModule);
3706
Douglas Gregor5204bde2011-08-02 16:26:37 +00003707 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3708
Douglas Gregord4df8652009-04-22 22:02:47 +00003709 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003710 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003711 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003712
3713 // Write the record containing tentative definitions.
3714 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003715 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003716
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003717 // Write the record containing unused file scoped decls.
3718 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003719 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003720
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003721 // Write the record containing weak undeclared identifiers.
3722 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003723 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003724 WeakUndeclaredIdentifiers);
3725
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003726 // Write the record containing locally-scoped external definitions.
3727 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003728 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003729 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003730
3731 // Write the record containing ext_vector type names.
3732 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003733 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003734
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003735 // Write the record containing VTable uses information.
3736 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003737 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003738
3739 // Write the record containing dynamic classes declarations.
3740 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003741 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003742
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003743 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003744 if (!PendingInstantiations.empty())
3745 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003746
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003747 // Write the record containing declaration references of Sema.
3748 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003749 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003750
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003751 // Write the record containing CUDA-specific declaration references.
3752 if (!CUDASpecialDeclRefs.empty())
3753 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003754
3755 // Write the delegating constructors.
3756 if (!DelegatingCtorDecls.empty())
3757 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003758
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003759 // Write the known namespaces.
3760 if (!KnownNamespaces.empty())
3761 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3762
Douglas Gregor851443c2011-08-12 01:39:19 +00003763 // Write the visible updates to DeclContexts.
3764 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3765 I = UpdatedDeclContexts.begin(),
3766 E = UpdatedDeclContexts.end();
3767 I != E; ++I)
3768 WriteDeclContextVisibleUpdate(*I);
3769
Douglas Gregor959bb062011-12-03 01:15:29 +00003770 if (!WritingModule) {
3771 // Write the submodules that were imported, if any.
3772 RecordData ImportedModules;
3773 for (ASTContext::import_iterator I = Context.local_import_begin(),
3774 IEnd = Context.local_import_end();
3775 I != IEnd; ++I) {
3776 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3777 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3778 }
3779 if (!ImportedModules.empty()) {
3780 // Sort module IDs.
3781 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3782
3783 // Unique module IDs.
3784 ImportedModules.erase(std::unique(ImportedModules.begin(),
3785 ImportedModules.end()),
3786 ImportedModules.end());
3787
3788 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3789 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003790 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003791
3792 WriteMacroUpdates();
Douglas Gregordab42432011-08-12 00:15:20 +00003793 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003794 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003795 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003796 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003797 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003798
Douglas Gregor08f01292009-04-17 22:13:46 +00003799 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003800 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003801 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003802 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003803 Record.push_back(NumLexicalDeclContexts);
3804 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003805 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003806 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003807}
3808
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003809void ASTWriter::WriteMacroUpdates() {
3810 if (MacroUpdates.empty())
3811 return;
3812
3813 RecordData Record;
3814 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3815 E = MacroUpdates.end();
3816 I != E; ++I) {
3817 addMacroRef(I->first, Record);
3818 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregorcfa46a82012-10-12 00:16:50 +00003819 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003820 }
3821 Stream.EmitRecord(MACRO_UPDATES, Record);
3822}
3823
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003824/// \brief Go through the declaration update blocks and resolve declaration
3825/// pointers into declaration IDs.
3826void ASTWriter::ResolveDeclUpdatesBlocks() {
3827 for (DeclUpdateMap::iterator
3828 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3829 const Decl *D = I->first;
3830 UpdateRecord &URec = I->second;
3831
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003832 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003833 continue; // The decl will be written completely
3834
3835 unsigned Idx = 0, N = URec.size();
3836 while (Idx < N) {
3837 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003838 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3839 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3840 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3841 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3842 ++Idx;
3843 break;
3844
3845 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3846 ++Idx;
3847 break;
3848 }
3849 }
3850 }
3851}
3852
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003853void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003854 if (DeclUpdates.empty())
3855 return;
3856
3857 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003858 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003859 for (DeclUpdateMap::iterator
3860 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3861 const Decl *D = I->first;
3862 UpdateRecord &URec = I->second;
3863
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003864 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003865 continue; // The decl will be written completely,no need to store updates.
3866
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003867 uint64_t Offset = Stream.GetCurrentBitNo();
3868 Stream.EmitRecord(DECL_UPDATES, URec);
3869
3870 OffsetsRecord.push_back(GetDeclRef(D));
3871 OffsetsRecord.push_back(Offset);
3872 }
3873 Stream.ExitBlock();
3874 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3875}
3876
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003877void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003878 if (ReplacedDecls.empty())
3879 return;
3880
3881 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003882 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003883 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003884 Record.push_back(I->ID);
3885 Record.push_back(I->Offset);
3886 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003887 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003888 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003889}
3890
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003891void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003892 Record.push_back(Loc.getRawEncoding());
3893}
3894
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003895void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003896 AddSourceLocation(Range.getBegin(), Record);
3897 AddSourceLocation(Range.getEnd(), Record);
3898}
3899
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003900void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003901 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003902 const uint64_t *Words = Value.getRawData();
3903 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003904}
3905
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003906void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003907 Record.push_back(Value.isUnsigned());
3908 AddAPInt(Value, Record);
3909}
3910
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003911void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003912 AddAPInt(Value.bitcastToAPInt(), Record);
3913}
3914
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003915void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003916 Record.push_back(getIdentifierRef(II));
3917}
3918
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003919void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3920 Record.push_back(getMacroRef(MI));
3921}
3922
Sebastian Redl539c5062010-08-18 23:57:32 +00003923IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003924 if (II == 0)
3925 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003926
Sebastian Redl539c5062010-08-18 23:57:32 +00003927 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003928 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003929 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003930 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003931}
3932
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003933MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3934 // Don't emit builtin macros like __LINE__ to the AST file unless they
3935 // have been redefined by the header (in which case they are not
3936 // isBuiltinMacro).
3937 if (MI == 0 || MI->isBuiltinMacro())
3938 return 0;
3939
3940 MacroID &ID = MacroIDs[MI];
3941 if (ID == 0)
3942 ID = NextMacroID++;
3943 return ID;
3944}
3945
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003946void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003947 Record.push_back(getSelectorRef(SelRef));
3948}
3949
Sebastian Redl539c5062010-08-18 23:57:32 +00003950SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003951 if (Sel.getAsOpaquePtr() == 0) {
3952 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003953 }
3954
Sebastian Redl539c5062010-08-18 23:57:32 +00003955 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003956 if (SID == 0 && Chain) {
3957 // This might trigger a ReadSelector callback, which will set the ID for
3958 // this selector.
3959 Chain->LoadSelector(Sel);
3960 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003961 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003962 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003963 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003964 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003965}
3966
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003967void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003968 AddDeclRef(Temp->getDestructor(), Record);
3969}
3970
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003971void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3972 CXXBaseSpecifier const *BasesEnd,
3973 RecordDataImpl &Record) {
3974 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3975 CXXBaseSpecifiersToWrite.push_back(
3976 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3977 Bases, BasesEnd));
3978 Record.push_back(NextCXXBaseSpecifiersID++);
3979}
3980
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003981void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003982 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003983 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003984 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003985 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003986 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003987 break;
3988 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003989 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003990 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003991 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003992 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003993 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003994 break;
3995 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003996 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003997 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003998 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003999 break;
John McCall0ad16662009-10-29 08:12:44 +00004000 case TemplateArgument::Null:
4001 case TemplateArgument::Integral:
4002 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004003 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004004 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004005 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004006 break;
4007 }
4008}
4009
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004010void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004011 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004012 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004013
4014 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4015 bool InfoHasSameExpr
4016 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4017 Record.push_back(InfoHasSameExpr);
4018 if (InfoHasSameExpr)
4019 return; // Avoid storing the same expr twice.
4020 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004021 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4022 Record);
4023}
4024
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004025void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4026 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004027 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004028 AddTypeRef(QualType(), Record);
4029 return;
4030 }
4031
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004032 AddTypeLoc(TInfo->getTypeLoc(), Record);
4033}
4034
4035void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4036 AddTypeRef(TL.getType(), Record);
4037
John McCall8f115c62009-10-16 21:56:05 +00004038 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004039 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004040 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004041}
4042
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004043void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004044 Record.push_back(GetOrCreateTypeID(T));
4045}
4046
Douglas Gregoreda8e122011-08-09 15:13:55 +00004047TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4048 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004049 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4050}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004051
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004052TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00004053 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004054 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004055}
4056
4057TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4058 if (T.isNull())
4059 return TypeIdx();
4060 assert(!T.getLocalFastQualifiers());
4061
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004062 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004063 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004064 if (DoneWritingDeclsAndTypes) {
4065 assert(0 && "New type seen after serializing all the types to emit!");
4066 return TypeIdx();
4067 }
4068
Douglas Gregor1970d882009-04-26 03:49:13 +00004069 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004070 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004071 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004072 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004073 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004074 return Idx;
4075}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004076
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004077TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004078 if (T.isNull())
4079 return TypeIdx();
4080 assert(!T.getLocalFastQualifiers());
4081
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004082 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4083 assert(I != TypeIdxs.end() && "Type not emitted!");
4084 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004085}
4086
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004087void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004088 Record.push_back(GetDeclRef(D));
4089}
4090
Sebastian Redl539c5062010-08-18 23:57:32 +00004091DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004092 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4093
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004094 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004095 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004096 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004097
4098 // If D comes from an AST file, its declaration ID is already known and
4099 // fixed.
4100 if (D->isFromASTFile())
4101 return D->getGlobalID();
4102
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004103 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004104 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004105 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004106 if (DoneWritingDeclsAndTypes) {
4107 assert(0 && "New decl seen after serializing all the decls to emit!");
4108 return 0;
4109 }
4110
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004111 // We haven't seen this declaration before. Give it a new ID and
4112 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004113 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004114 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004115 }
4116
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004117 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004118}
4119
Sebastian Redl539c5062010-08-18 23:57:32 +00004120DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004121 if (D == 0)
4122 return 0;
4123
Douglas Gregorb3163e52012-01-05 22:33:30 +00004124 // If D comes from an AST file, its declaration ID is already known and
4125 // fixed.
4126 if (D->isFromASTFile())
4127 return D->getGlobalID();
4128
Douglas Gregore84a9da2009-04-20 20:36:09 +00004129 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4130 return DeclIDs[D];
4131}
4132
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004133static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4134 std::pair<unsigned, serialization::DeclID> R) {
4135 return L.first < R.first;
4136}
4137
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004138void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004139 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004140 assert(D);
4141
4142 SourceLocation Loc = D->getLocation();
4143 if (Loc.isInvalid())
4144 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004145
4146 // We only keep track of the file-level declarations of each file.
4147 if (!D->getLexicalDeclContext()->isFileContext())
4148 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004149 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4150 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004151 if (isa<ParmVarDecl>(D))
4152 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004153
4154 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004155 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004156 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004157 FileID FID;
4158 unsigned Offset;
4159 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004160 if (FID.isInvalid())
4161 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004162 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004163
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004164 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004165 if (!Info)
4166 Info = new DeclIDInFileInfo();
4167
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004168 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004169 LocDeclIDsTy &Decls = Info->DeclIDs;
4170
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004171 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004172 Decls.push_back(LocDecl);
4173 return;
4174 }
4175
4176 LocDeclIDsTy::iterator
4177 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4178
4179 Decls.insert(I, LocDecl);
4180}
4181
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004182void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004183 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004184 Record.push_back(Name.getNameKind());
4185 switch (Name.getNameKind()) {
4186 case DeclarationName::Identifier:
4187 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4188 break;
4189
4190 case DeclarationName::ObjCZeroArgSelector:
4191 case DeclarationName::ObjCOneArgSelector:
4192 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004193 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004194 break;
4195
4196 case DeclarationName::CXXConstructorName:
4197 case DeclarationName::CXXDestructorName:
4198 case DeclarationName::CXXConversionFunctionName:
4199 AddTypeRef(Name.getCXXNameType(), Record);
4200 break;
4201
4202 case DeclarationName::CXXOperatorName:
4203 Record.push_back(Name.getCXXOverloadedOperator());
4204 break;
4205
Alexis Hunt3d221f22009-11-29 07:34:05 +00004206 case DeclarationName::CXXLiteralOperatorName:
4207 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4208 break;
4209
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004210 case DeclarationName::CXXUsingDirective:
4211 // No extra data to emit
4212 break;
4213 }
4214}
Chris Lattnerca025db2010-05-07 21:43:38 +00004215
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004216void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004217 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004218 switch (Name.getNameKind()) {
4219 case DeclarationName::CXXConstructorName:
4220 case DeclarationName::CXXDestructorName:
4221 case DeclarationName::CXXConversionFunctionName:
4222 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4223 break;
4224
4225 case DeclarationName::CXXOperatorName:
4226 AddSourceLocation(
4227 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4228 Record);
4229 AddSourceLocation(
4230 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4231 Record);
4232 break;
4233
4234 case DeclarationName::CXXLiteralOperatorName:
4235 AddSourceLocation(
4236 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4237 Record);
4238 break;
4239
4240 case DeclarationName::Identifier:
4241 case DeclarationName::ObjCZeroArgSelector:
4242 case DeclarationName::ObjCOneArgSelector:
4243 case DeclarationName::ObjCMultiArgSelector:
4244 case DeclarationName::CXXUsingDirective:
4245 break;
4246 }
4247}
4248
4249void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004250 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004251 AddDeclarationName(NameInfo.getName(), Record);
4252 AddSourceLocation(NameInfo.getLoc(), Record);
4253 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4254}
4255
4256void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004257 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004258 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004259 Record.push_back(Info.NumTemplParamLists);
4260 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4261 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4262}
4263
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004264void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004265 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004266 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004267 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004268 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004269
4270 // Push each of the NNS's onto a stack for serialization in reverse order.
4271 while (NNS) {
4272 NestedNames.push_back(NNS);
4273 NNS = NNS->getPrefix();
4274 }
4275
4276 Record.push_back(NestedNames.size());
4277 while(!NestedNames.empty()) {
4278 NNS = NestedNames.pop_back_val();
4279 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4280 Record.push_back(Kind);
4281 switch (Kind) {
4282 case NestedNameSpecifier::Identifier:
4283 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4284 break;
4285
4286 case NestedNameSpecifier::Namespace:
4287 AddDeclRef(NNS->getAsNamespace(), Record);
4288 break;
4289
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004290 case NestedNameSpecifier::NamespaceAlias:
4291 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4292 break;
4293
Chris Lattnerca025db2010-05-07 21:43:38 +00004294 case NestedNameSpecifier::TypeSpec:
4295 case NestedNameSpecifier::TypeSpecWithTemplate:
4296 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4297 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4298 break;
4299
4300 case NestedNameSpecifier::Global:
4301 // Don't need to write an associated value.
4302 break;
4303 }
4304 }
4305}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004306
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004307void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4308 RecordDataImpl &Record) {
4309 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004310 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004311 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004312
4313 // Push each of the nested-name-specifiers's onto a stack for
4314 // serialization in reverse order.
4315 while (NNS) {
4316 NestedNames.push_back(NNS);
4317 NNS = NNS.getPrefix();
4318 }
4319
4320 Record.push_back(NestedNames.size());
4321 while(!NestedNames.empty()) {
4322 NNS = NestedNames.pop_back_val();
4323 NestedNameSpecifier::SpecifierKind Kind
4324 = NNS.getNestedNameSpecifier()->getKind();
4325 Record.push_back(Kind);
4326 switch (Kind) {
4327 case NestedNameSpecifier::Identifier:
4328 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4329 AddSourceRange(NNS.getLocalSourceRange(), Record);
4330 break;
4331
4332 case NestedNameSpecifier::Namespace:
4333 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4334 AddSourceRange(NNS.getLocalSourceRange(), Record);
4335 break;
4336
4337 case NestedNameSpecifier::NamespaceAlias:
4338 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4339 AddSourceRange(NNS.getLocalSourceRange(), Record);
4340 break;
4341
4342 case NestedNameSpecifier::TypeSpec:
4343 case NestedNameSpecifier::TypeSpecWithTemplate:
4344 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4345 AddTypeLoc(NNS.getTypeLoc(), Record);
4346 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4347 break;
4348
4349 case NestedNameSpecifier::Global:
4350 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4351 break;
4352 }
4353 }
4354}
4355
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004356void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004357 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004358 Record.push_back(Kind);
4359 switch (Kind) {
4360 case TemplateName::Template:
4361 AddDeclRef(Name.getAsTemplateDecl(), Record);
4362 break;
4363
4364 case TemplateName::OverloadedTemplate: {
4365 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4366 Record.push_back(OvT->size());
4367 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4368 I != E; ++I)
4369 AddDeclRef(*I, Record);
4370 break;
4371 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004372
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004373 case TemplateName::QualifiedTemplate: {
4374 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4375 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4376 Record.push_back(QualT->hasTemplateKeyword());
4377 AddDeclRef(QualT->getTemplateDecl(), Record);
4378 break;
4379 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004380
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004381 case TemplateName::DependentTemplate: {
4382 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4383 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4384 Record.push_back(DepT->isIdentifier());
4385 if (DepT->isIdentifier())
4386 AddIdentifierRef(DepT->getIdentifier(), Record);
4387 else
4388 Record.push_back(DepT->getOperator());
4389 break;
4390 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004391
4392 case TemplateName::SubstTemplateTemplateParm: {
4393 SubstTemplateTemplateParmStorage *subst
4394 = Name.getAsSubstTemplateTemplateParm();
4395 AddDeclRef(subst->getParameter(), Record);
4396 AddTemplateName(subst->getReplacement(), Record);
4397 break;
4398 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004399
4400 case TemplateName::SubstTemplateTemplateParmPack: {
4401 SubstTemplateTemplateParmPackStorage *SubstPack
4402 = Name.getAsSubstTemplateTemplateParmPack();
4403 AddDeclRef(SubstPack->getParameterPack(), Record);
4404 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4405 break;
4406 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004407 }
4408}
4409
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004410void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004411 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004412 Record.push_back(Arg.getKind());
4413 switch (Arg.getKind()) {
4414 case TemplateArgument::Null:
4415 break;
4416 case TemplateArgument::Type:
4417 AddTypeRef(Arg.getAsType(), Record);
4418 break;
4419 case TemplateArgument::Declaration:
4420 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004421 Record.push_back(Arg.isDeclForReferenceParam());
4422 break;
4423 case TemplateArgument::NullPtr:
4424 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004425 break;
4426 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004427 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004428 AddTypeRef(Arg.getIntegralType(), Record);
4429 break;
4430 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004431 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4432 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004433 case TemplateArgument::TemplateExpansion:
4434 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004435 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4436 Record.push_back(*NumExpansions + 1);
4437 else
4438 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004439 break;
4440 case TemplateArgument::Expression:
4441 AddStmt(Arg.getAsExpr());
4442 break;
4443 case TemplateArgument::Pack:
4444 Record.push_back(Arg.pack_size());
4445 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4446 I != E; ++I)
4447 AddTemplateArgument(*I, Record);
4448 break;
4449 }
4450}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004451
4452void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004453ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004454 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004455 assert(TemplateParams && "No TemplateParams!");
4456 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4457 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4458 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4459 Record.push_back(TemplateParams->size());
4460 for (TemplateParameterList::const_iterator
4461 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4462 P != PEnd; ++P)
4463 AddDeclRef(*P, Record);
4464}
4465
4466/// \brief Emit a template argument list.
4467void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004468ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004469 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004470 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004471 Record.push_back(TemplateArgs->size());
4472 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004473 AddTemplateArgument(TemplateArgs->get(i), Record);
4474}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004475
4476
4477void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004478ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004479 Record.push_back(Set.size());
4480 for (UnresolvedSetImpl::const_iterator
4481 I = Set.begin(), E = Set.end(); I != E; ++I) {
4482 AddDeclRef(I.getDecl(), Record);
4483 Record.push_back(I.getAccess());
4484 }
4485}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004486
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004487void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004488 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004489 Record.push_back(Base.isVirtual());
4490 Record.push_back(Base.isBaseOfClass());
4491 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004492 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004493 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004494 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004495 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4496 : SourceLocation(),
4497 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004498}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004499
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004500void ASTWriter::FlushCXXBaseSpecifiers() {
4501 RecordData Record;
4502 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4503 Record.clear();
4504
4505 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004506 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004507 if (Index == CXXBaseSpecifiersOffsets.size())
4508 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4509 else {
4510 if (Index > CXXBaseSpecifiersOffsets.size())
4511 CXXBaseSpecifiersOffsets.resize(Index + 1);
4512 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4513 }
4514
4515 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4516 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4517 Record.push_back(BEnd - B);
4518 for (; B != BEnd; ++B)
4519 AddCXXBaseSpecifier(*B, Record);
4520 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004521
4522 // Flush any expressions that were written as part of the base specifiers.
4523 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004524 }
4525
4526 CXXBaseSpecifiersToWrite.clear();
4527}
4528
Alexis Hunt1d792652011-01-08 20:30:50 +00004529void ASTWriter::AddCXXCtorInitializers(
4530 const CXXCtorInitializer * const *CtorInitializers,
4531 unsigned NumCtorInitializers,
4532 RecordDataImpl &Record) {
4533 Record.push_back(NumCtorInitializers);
4534 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4535 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004536
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004537 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004538 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004539 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004540 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004541 } else if (Init->isDelegatingInitializer()) {
4542 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004543 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004544 } else if (Init->isMemberInitializer()){
4545 Record.push_back(CTOR_INITIALIZER_MEMBER);
4546 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004547 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004548 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4549 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004550 }
Francois Pichetd583da02010-12-04 09:14:42 +00004551
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004552 AddSourceLocation(Init->getMemberLocation(), Record);
4553 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004554 AddSourceLocation(Init->getLParenLoc(), Record);
4555 AddSourceLocation(Init->getRParenLoc(), Record);
4556 Record.push_back(Init->isWritten());
4557 if (Init->isWritten()) {
4558 Record.push_back(Init->getSourceOrder());
4559 } else {
4560 Record.push_back(Init->getNumArrayIndices());
4561 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4562 AddDeclRef(Init->getArrayIndex(i), Record);
4563 }
4564 }
4565}
4566
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004567void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4568 assert(D->DefinitionData);
4569 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004570 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004571 Record.push_back(Data.UserDeclaredConstructor);
4572 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004573 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004574 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004575 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004576 Record.push_back(Data.UserDeclaredDestructor);
4577 Record.push_back(Data.Aggregate);
4578 Record.push_back(Data.PlainOldData);
4579 Record.push_back(Data.Empty);
4580 Record.push_back(Data.Polymorphic);
4581 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004582 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004583 Record.push_back(Data.HasNoNonEmptyBases);
4584 Record.push_back(Data.HasPrivateFields);
4585 Record.push_back(Data.HasProtectedFields);
4586 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004587 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004588 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00004589 Record.push_back(Data.HasInClassInitializer);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004590 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004591 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004592 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00004593 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004594 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004595 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004596 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004597 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004598 Record.push_back(Data.HasTrivialDestructor);
Richard Smith561fb152012-02-25 07:33:38 +00004599 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004600 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004601 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004602 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004603 Record.push_back(Data.DeclaredDefaultConstructor);
4604 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004605 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004606 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004607 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004608 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004609 Record.push_back(Data.FailedImplicitMoveConstructor);
4610 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004611 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004612
4613 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004614 if (Data.NumBases > 0)
4615 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4616 Record);
4617
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004618 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4619 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004620 if (Data.NumVBases > 0)
4621 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4622 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004623
4624 AddUnresolvedSet(Data.Conversions, Record);
4625 AddUnresolvedSet(Data.VisibleConversions, Record);
4626 // Data.Definition is the owning decl, no need to write it.
4627 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004628
4629 // Add lambda-specific data.
4630 if (Data.IsLambda) {
4631 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004632 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004633 Record.push_back(Lambda.NumCaptures);
4634 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004635 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004636 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00004637 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004638 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4639 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4640 AddSourceLocation(Capture.getLocation(), Record);
4641 Record.push_back(Capture.isImplicit());
4642 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4643 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4644 AddDeclRef(Var, Record);
4645 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4646 : SourceLocation(),
4647 Record);
4648 }
4649 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004650}
4651
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004652void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004653 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004654 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004655 assert(FirstDeclID == NextDeclID &&
4656 FirstTypeID == NextTypeID &&
4657 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004658 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004659 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004660 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004661 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004662
Sebastian Redl07a89a82010-07-30 00:29:29 +00004663 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004664
Douglas Gregordf0c1512011-08-18 04:12:04 +00004665 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4666 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4667 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004668 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004669 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004670 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004671 NextDeclID = FirstDeclID;
4672 NextTypeID = FirstTypeID;
4673 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004674 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004675 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004676 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004677}
4678
Sebastian Redl539c5062010-08-18 23:57:32 +00004679void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004680 IdentifierIDs[II] = ID;
4681}
4682
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004683void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4684 MacroIDs[MI] = ID;
4685}
4686
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004687void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004688 // Always take the highest-numbered type index. This copes with an interesting
4689 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004690 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004691 // keep the higher-numbered entry so that we can properly write it out to
4692 // the AST file.
4693 TypeIdx &StoredIdx = TypeIdxs[T];
4694 if (Idx.getIndex() >= StoredIdx.getIndex())
4695 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004696}
4697
Sebastian Redl539c5062010-08-18 23:57:32 +00004698void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004699 SelectorIDs[S] = ID;
4700}
Douglas Gregor91096292010-10-02 19:29:26 +00004701
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004702void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004703 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004704 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004705 MacroDefinitions[MD] = ID;
4706}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004707
Douglas Gregore37a85a2011-12-02 17:30:13 +00004708void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4709 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4710 SubmoduleIDs[Mod] = ID;
4711}
4712
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004713void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4714 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4715}
4716
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004717void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004718 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004719 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004720 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4721 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004722 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004723 // A forward reference was mutated into a definition. Rewrite it.
4724 // FIXME: This happens during template instantiation, should we
4725 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004726 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004727 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004728 }
4729}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004730
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004731void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004732 assert(!WritingAST && "Already writing the AST!");
4733
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004734 // TU and namespaces are handled elsewhere.
4735 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4736 return;
4737
Douglas Gregorb3722e22011-09-09 23:01:35 +00004738 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004739 return; // Not a source decl added to a DeclContext from PCH.
4740
4741 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004742 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004743}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004744
4745void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004746 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004747 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004748 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004749 return; // Not a source member added to a class from PCH.
4750 if (!isa<CXXMethodDecl>(D))
4751 return; // We are interested in lazily declared implicit methods.
4752
4753 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004754 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004755 UpdateRecord &Record = DeclUpdates[RD];
4756 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004757 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004758}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004759
4760void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4761 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004762 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004763 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004764 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004765 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004766 return; // Not a source specialization added to a template from PCH.
4767
4768 UpdateRecord &Record = DeclUpdates[TD];
4769 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004770 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004771}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004772
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004773void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4774 const FunctionDecl *D) {
4775 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004776 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004777 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004778 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004779 return; // Not a source specialization added to a template from PCH.
4780
4781 UpdateRecord &Record = DeclUpdates[TD];
4782 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004783 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004784}
4785
Sebastian Redlab238a72011-04-24 16:28:06 +00004786void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004787 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004788 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004789 return; // Declaration not imported from PCH.
4790
4791 // Implicit decl from a PCH was defined.
4792 // FIXME: Should implicit definition be a separate FunctionDecl?
4793 RewriteDecl(D);
4794}
4795
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004796void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004797 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004798 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004799 return;
4800
4801 // Since the actual instantiation is delayed, this really means that we need
4802 // to update the instantiation location.
4803 UpdateRecord &Record = DeclUpdates[D];
4804 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4805 AddSourceLocation(
4806 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4807}
4808
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004809void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4810 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004811 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004812 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004813 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004814
4815 assert(IFD->getDefinition() && "Category on a class without a definition?");
4816 ObjCClassesWithCategories.insert(
4817 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004818}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004819
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004820
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004821void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4822 const ObjCPropertyDecl *OrigProp,
4823 const ObjCCategoryDecl *ClassExt) {
4824 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4825 if (!D)
4826 return;
4827
4828 assert(!WritingAST && "Already writing the AST!");
4829 if (!D->isFromASTFile())
4830 return; // Declaration not imported from PCH.
4831
4832 RewriteDecl(D);
4833}