blob: 03e33ad369aef430e344184edea9b26d8f6f409b [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
Sebastian Redlade50002010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000059static StringRef data(const std::vector<T, Allocator> &v) {
60 if (v.empty()) return StringRef();
61 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000063}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000069}
70
Douglas Gregor2cf26342009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000074
Douglas Gregor2cf26342009-04-09 22:27:44 +000075namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000086
87 void VisitArrayType(const ArrayType *T);
88 void VisitFunctionType(const FunctionType *T);
89 void VisitTagType(const TagType *T);
90
91#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
92#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000113 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114}
115
Sebastian Redl3397c552010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000117 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
118 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000123 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000124 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125}
126
Sebastian Redl3397c552010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 Writer.AddTypeRef(T->getPointeeType(), Record);
129 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000136 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137}
138
Sebastian Redl3397c552010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140 VisitArrayType(T);
141 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000147 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
Sebastian Redl3397c552010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000152 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
153 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000154 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000155 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156}
157
Sebastian Redl3397c552010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getElementType(), Record);
160 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000161 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000162 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163}
164
Sebastian Redl3397c552010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000167 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168}
169
Sebastian Redl3397c552010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000172 FunctionType::ExtInfo C = T->getExtInfo();
173 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000174 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000175 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000176 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000177 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000178 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179}
180
Sebastian Redl3397c552010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000183 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184}
185
Sebastian Redl3397c552010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187 VisitFunctionType(T);
188 Record.push_back(T->getNumArgs());
189 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
190 Writer.AddTypeRef(T->getArgType(I), Record);
191 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000192 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000194 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000195 Record.push_back(T->getExceptionSpecType());
196 if (T->getExceptionSpecType() == EST_Dynamic) {
197 Record.push_back(T->getNumExceptions());
198 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
199 Writer.AddTypeRef(T->getExceptionType(I), Record);
200 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
201 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000202 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
204 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000205 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
206 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000207 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000208 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209}
210
Sebastian Redl3397c552010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000212 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000214}
John McCalled976492009-12-04 22:46:56 +0000215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000218 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
219 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226}
227
Sebastian Redl3397c552010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000230 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231}
232
Sebastian Redl3397c552010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000234 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000235 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000236 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000237}
238
Sean Huntca63c202011-05-24 22:41:36 +0000239void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
240 Writer.AddTypeRef(T->getBaseType(), Record);
241 Writer.AddTypeRef(T->getUnderlyingType(), Record);
242 Record.push_back(T->getUTTKind());
243 Code = TYPE_UNARY_TRANSFORM;
244}
245
Richard Smith34b41d92011-02-20 03:19:35 +0000246void ASTTypeWriter::VisitAutoType(const AutoType *T) {
247 Writer.AddTypeRef(T->getDeducedType(), Record);
248 Code = TYPE_AUTO;
249}
250
Sebastian Redl3397c552010-08-18 23:56:27 +0000251void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000252 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000253 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000254 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255 "Cannot serialize in the middle of a type definition");
256}
257
Sebastian Redl3397c552010-08-18 23:56:27 +0000258void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000259 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000260 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261}
262
Sebastian Redl3397c552010-08-18 23:56:27 +0000263void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000265 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000266}
267
John McCall9d156a72011-01-06 01:58:22 +0000268void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
269 Writer.AddTypeRef(T->getModifiedType(), Record);
270 Writer.AddTypeRef(T->getEquivalentType(), Record);
271 Record.push_back(T->getAttrKind());
272 Code = TYPE_ATTRIBUTED;
273}
274
Mike Stump1eb44332009-09-09 15:08:12 +0000275void
Sebastian Redl3397c552010-08-18 23:56:27 +0000276ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000277 const SubstTemplateTypeParmType *T) {
278 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
279 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000280 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000281}
282
283void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000284ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
285 const SubstTemplateTypeParmPackType *T) {
286 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
287 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
288 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
289}
290
291void
Sebastian Redl3397c552010-08-18 23:56:27 +0000292ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000293 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000294 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000295 Writer.AddTemplateName(T->getTemplateName(), Record);
296 Record.push_back(T->getNumArgs());
297 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
298 ArgI != ArgE; ++ArgI)
299 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000300 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
301 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000302 : T->getCanonicalTypeInternal(),
303 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000304 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000305}
306
307void
Sebastian Redl3397c552010-08-18 23:56:27 +0000308ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000309 VisitArrayType(T);
310 Writer.AddStmt(T->getSizeExpr());
311 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000312 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313}
314
315void
Sebastian Redl3397c552010-08-18 23:56:27 +0000316ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317 const DependentSizedExtVectorType *T) {
318 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000319 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320}
321
322void
Sebastian Redl3397c552010-08-18 23:56:27 +0000323ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000324 Record.push_back(T->getDepth());
325 Record.push_back(T->getIndex());
326 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000327 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000333 Record.push_back(T->getKeyword());
334 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
335 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000336 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
337 : T->getCanonicalTypeInternal(),
338 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000339 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl3397c552010-08-18 23:56:27 +0000343ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000344 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000345 Record.push_back(T->getKeyword());
346 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
347 Writer.AddIdentifierRef(T->getIdentifier(), Record);
348 Record.push_back(T->getNumArgs());
349 for (DependentTemplateSpecializationType::iterator
350 I = T->begin(), E = T->end(); I != E; ++I)
351 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000352 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000353}
354
Douglas Gregor7536dd52010-12-20 02:24:11 +0000355void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
356 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000357 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000358 Record.push_back(*NumExpansions + 1);
359 else
360 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000361 Code = TYPE_PACK_EXPANSION;
362}
363
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000364void ASTTypeWriter::VisitParenType(const ParenType *T) {
365 Writer.AddTypeRef(T->getInnerType(), Record);
366 Code = TYPE_PAREN;
367}
368
Sebastian Redl3397c552010-08-18 23:56:27 +0000369void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000370 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000371 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
372 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000374}
375
Sebastian Redl3397c552010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000377 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000378 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000379 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000380}
381
Sebastian Redl3397c552010-08-18 23:56:27 +0000382void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000383 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000384 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000385}
386
Sebastian Redl3397c552010-08-18 23:56:27 +0000387void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000388 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000389 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000390 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000391 E = T->qual_end(); I != E; ++I)
392 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000393 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000394}
395
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000396void
Sebastian Redl3397c552010-08-18 23:56:27 +0000397ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000398 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000399 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000400}
401
Eli Friedmanb001de72011-10-06 23:00:33 +0000402void
403ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
404 Writer.AddTypeRef(T->getValueType(), Record);
405 Code = TYPE_ATOMIC;
406}
407
John McCalla1ee0c52009-10-16 21:56:05 +0000408namespace {
409
410class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000411 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000412 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000413
414public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000415 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000416 : Writer(Writer), Record(Record) { }
417
John McCall51bd8032009-10-18 01:05:36 +0000418#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000419#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000420 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000421#include "clang/AST/TypeLocNodes.def"
422
John McCall51bd8032009-10-18 01:05:36 +0000423 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
424 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000425};
426
427}
428
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
430 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000431}
John McCall51bd8032009-10-18 01:05:36 +0000432void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000433 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
434 if (TL.needsExtraLocalData()) {
435 Record.push_back(TL.getWrittenTypeSpec());
436 Record.push_back(TL.getWrittenSignSpec());
437 Record.push_back(TL.getWrittenWidthSpec());
438 Record.push_back(TL.hasModeAttr());
439 }
John McCalla1ee0c52009-10-16 21:56:05 +0000440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000446}
John McCall51bd8032009-10-18 01:05:36 +0000447void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000458 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000459}
John McCall51bd8032009-10-18 01:05:36 +0000460void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
462 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
463 Record.push_back(TL.getSizeExpr() ? 1 : 0);
464 if (TL.getSizeExpr())
465 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000466}
John McCall51bd8032009-10-18 01:05:36 +0000467void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
468 VisitArrayTypeLoc(TL);
469}
470void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
471 VisitArrayTypeLoc(TL);
472}
473void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
477 DependentSizedArrayTypeLoc TL) {
478 VisitArrayTypeLoc(TL);
479}
480void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
481 DependentSizedExtVectorTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
484void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
487void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
490void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000491 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000492 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
493 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000494 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000495 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
496 Writer.AddDeclRef(TL.getArg(i), Record);
497}
498void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
499 VisitFunctionTypeLoc(TL);
500}
501void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
502 VisitFunctionTypeLoc(TL);
503}
John McCalled976492009-12-04 22:46:56 +0000504void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
John McCall51bd8032009-10-18 01:05:36 +0000507void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000511 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
512 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
513 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000514}
515void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000516 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
517 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
518 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
519 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000520}
521void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
Sean Huntca63c202011-05-24 22:41:36 +0000524void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getKWLoc(), Record);
526 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
527 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
528 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
529}
Richard Smith34b41d92011-02-20 03:19:35 +0000530void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
John McCall51bd8032009-10-18 01:05:36 +0000533void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
536void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getNameLoc(), Record);
538}
John McCall9d156a72011-01-06 01:58:22 +0000539void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
540 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
541 if (TL.hasAttrOperand()) {
542 SourceRange range = TL.getAttrOperandParensRange();
543 Writer.AddSourceLocation(range.getBegin(), Record);
544 Writer.AddSourceLocation(range.getEnd(), Record);
545 }
546 if (TL.hasAttrExprOperand()) {
547 Expr *operand = TL.getAttrExprOperand();
548 Record.push_back(operand ? 1 : 0);
549 if (operand) Writer.AddStmt(operand);
550 } else if (TL.hasAttrEnumOperand()) {
551 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
552 }
553}
John McCall51bd8032009-10-18 01:05:36 +0000554void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
John McCall49a832b2009-10-18 09:09:24 +0000557void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
558 SubstTemplateTypeParmTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getNameLoc(), Record);
560}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000561void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
562 SubstTemplateTypeParmPackTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getNameLoc(), Record);
564}
John McCall51bd8032009-10-18 01:05:36 +0000565void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
566 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000567 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000568 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
569 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
570 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
571 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000572 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
573 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000574}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000575void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
576 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
577 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
578}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000579void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000580 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000582}
John McCall3cb0ebd2010-03-10 03:28:59 +0000583void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getNameLoc(), Record);
585}
Douglas Gregor4714c122010-03-31 17:34:00 +0000586void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000587 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000588 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000589 Writer.AddSourceLocation(TL.getNameLoc(), Record);
590}
John McCall33500952010-06-11 00:33:02 +0000591void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
592 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000594 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000595 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000596 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000600 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
601 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000602}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000603void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
605}
John McCall51bd8032009-10-18 01:05:36 +0000606void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
607 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000608}
609void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
610 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000611 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
612 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
613 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
614 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000615}
John McCall54e14c42009-10-22 22:37:11 +0000616void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
617 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000618}
Eli Friedmanb001de72011-10-06 23:00:33 +0000619void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
620 Writer.AddSourceLocation(TL.getKWLoc(), Record);
621 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
622 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
623}
John McCalla1ee0c52009-10-16 21:56:05 +0000624
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000625//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000626// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000627//===----------------------------------------------------------------------===//
628
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000629static void EmitBlockID(unsigned ID, const char *Name,
630 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 Record.clear();
633 Record.push_back(ID);
634 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
635
636 // Emit the block name if present.
637 if (Name == 0 || Name[0] == 0) return;
638 Record.clear();
639 while (*Name)
640 Record.push_back(*Name++);
641 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
642}
643
644static void EmitRecordID(unsigned ID, const char *Name,
645 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000646 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000647 Record.clear();
648 Record.push_back(ID);
649 while (*Name)
650 Record.push_back(*Name++);
651 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000652}
653
654static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000655 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000656#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000657 RECORD(STMT_STOP);
658 RECORD(STMT_NULL_PTR);
659 RECORD(STMT_NULL);
660 RECORD(STMT_COMPOUND);
661 RECORD(STMT_CASE);
662 RECORD(STMT_DEFAULT);
663 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000664 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000665 RECORD(STMT_IF);
666 RECORD(STMT_SWITCH);
667 RECORD(STMT_WHILE);
668 RECORD(STMT_DO);
669 RECORD(STMT_FOR);
670 RECORD(STMT_GOTO);
671 RECORD(STMT_INDIRECT_GOTO);
672 RECORD(STMT_CONTINUE);
673 RECORD(STMT_BREAK);
674 RECORD(STMT_RETURN);
675 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000676 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000677 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000678 RECORD(EXPR_PREDEFINED);
679 RECORD(EXPR_DECL_REF);
680 RECORD(EXPR_INTEGER_LITERAL);
681 RECORD(EXPR_FLOATING_LITERAL);
682 RECORD(EXPR_IMAGINARY_LITERAL);
683 RECORD(EXPR_STRING_LITERAL);
684 RECORD(EXPR_CHARACTER_LITERAL);
685 RECORD(EXPR_PAREN);
686 RECORD(EXPR_UNARY_OPERATOR);
687 RECORD(EXPR_SIZEOF_ALIGN_OF);
688 RECORD(EXPR_ARRAY_SUBSCRIPT);
689 RECORD(EXPR_CALL);
690 RECORD(EXPR_MEMBER);
691 RECORD(EXPR_BINARY_OPERATOR);
692 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
693 RECORD(EXPR_CONDITIONAL_OPERATOR);
694 RECORD(EXPR_IMPLICIT_CAST);
695 RECORD(EXPR_CSTYLE_CAST);
696 RECORD(EXPR_COMPOUND_LITERAL);
697 RECORD(EXPR_EXT_VECTOR_ELEMENT);
698 RECORD(EXPR_INIT_LIST);
699 RECORD(EXPR_DESIGNATED_INIT);
700 RECORD(EXPR_IMPLICIT_VALUE_INIT);
701 RECORD(EXPR_VA_ARG);
702 RECORD(EXPR_ADDR_LABEL);
703 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000704 RECORD(EXPR_CHOOSE);
705 RECORD(EXPR_GNU_NULL);
706 RECORD(EXPR_SHUFFLE_VECTOR);
707 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000708 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000709 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000710 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000711 RECORD(EXPR_OBJC_ARRAY_LITERAL);
712 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000713 RECORD(EXPR_OBJC_ENCODE);
714 RECORD(EXPR_OBJC_SELECTOR_EXPR);
715 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
716 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
717 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
718 RECORD(EXPR_OBJC_KVC_REF_EXPR);
719 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000720 RECORD(STMT_OBJC_FOR_COLLECTION);
721 RECORD(STMT_OBJC_CATCH);
722 RECORD(STMT_OBJC_FINALLY);
723 RECORD(STMT_OBJC_AT_TRY);
724 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
725 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000726 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000727 RECORD(EXPR_CXX_OPERATOR_CALL);
728 RECORD(EXPR_CXX_CONSTRUCT);
729 RECORD(EXPR_CXX_STATIC_CAST);
730 RECORD(EXPR_CXX_DYNAMIC_CAST);
731 RECORD(EXPR_CXX_REINTERPRET_CAST);
732 RECORD(EXPR_CXX_CONST_CAST);
733 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000734 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000735 RECORD(EXPR_CXX_BOOL_LITERAL);
736 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000737 RECORD(EXPR_CXX_TYPEID_EXPR);
738 RECORD(EXPR_CXX_TYPEID_TYPE);
739 RECORD(EXPR_CXX_UUIDOF_EXPR);
740 RECORD(EXPR_CXX_UUIDOF_TYPE);
741 RECORD(EXPR_CXX_THIS);
742 RECORD(EXPR_CXX_THROW);
743 RECORD(EXPR_CXX_DEFAULT_ARG);
744 RECORD(EXPR_CXX_BIND_TEMPORARY);
745 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
746 RECORD(EXPR_CXX_NEW);
747 RECORD(EXPR_CXX_DELETE);
748 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
749 RECORD(EXPR_EXPR_WITH_CLEANUPS);
750 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
751 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
752 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
753 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
754 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
755 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
756 RECORD(EXPR_CXX_NOEXCEPT);
757 RECORD(EXPR_OPAQUE_VALUE);
758 RECORD(EXPR_BINARY_TYPE_TRAIT);
759 RECORD(EXPR_PACK_EXPANSION);
760 RECORD(EXPR_SIZEOF_PACK);
761 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000762 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000763#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764}
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Sebastian Redla4232eb2010-08-18 23:56:21 +0000766void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000767 RecordData Record;
768 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000770#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
771#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000773 // Control Block.
774 BLOCK(CONTROL_BLOCK);
775 RECORD(METADATA);
776 RECORD(IMPORTS);
777 RECORD(LANGUAGE_OPTIONS);
778 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000779 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000780 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000781 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000782 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000783 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000784 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000785 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000786 RECORD(PREPROCESSOR_OPTIONS);
787
Douglas Gregorc337fef2012-10-19 00:45:00 +0000788 BLOCK(INPUT_FILES_BLOCK);
789 RECORD(INPUT_FILE);
790
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000791 // AST Top-Level Block.
792 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000793 RECORD(TYPE_OFFSET);
794 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000795 RECORD(IDENTIFIER_OFFSET);
796 RECORD(IDENTIFIER_TABLE);
797 RECORD(EXTERNAL_DEFINITIONS);
798 RECORD(SPECIAL_TYPES);
799 RECORD(STATISTICS);
800 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000801 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000802 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000803 RECORD(SELECTOR_OFFSETS);
804 RECORD(METHOD_POOL);
805 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000806 RECORD(SOURCE_LOCATION_OFFSETS);
807 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000808 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000809 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000810 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000811 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000812 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000813 RECORD(SEMA_DECL_REFS);
814 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
815 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
816 RECORD(DECL_REPLACEMENTS);
817 RECORD(UPDATE_VISIBLE);
818 RECORD(DECL_UPDATE_OFFSETS);
819 RECORD(DECL_UPDATES);
820 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
821 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000822 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000823 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000824 RECORD(FP_PRAGMA_OPTIONS);
825 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000826 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000827 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000828 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000829 RECORD(MODULE_OFFSET_MAP);
830 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000831 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000832 RECORD(FILE_SORTED_DECLS);
833 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000834 RECORD(MERGED_DECLARATIONS);
835 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000836 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000837 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000838 RECORD(MACRO_TABLE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000839
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000841 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000842 RECORD(SM_SLOC_FILE_ENTRY);
843 RECORD(SM_SLOC_BUFFER_ENTRY);
844 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000845 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000847 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000848 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000849 RECORD(PP_MACRO_OBJECT_LIKE);
850 RECORD(PP_MACRO_FUNCTION_LIKE);
851 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000852
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000853 // Decls and Types block.
854 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000855 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000856 RECORD(TYPE_COMPLEX);
857 RECORD(TYPE_POINTER);
858 RECORD(TYPE_BLOCK_POINTER);
859 RECORD(TYPE_LVALUE_REFERENCE);
860 RECORD(TYPE_RVALUE_REFERENCE);
861 RECORD(TYPE_MEMBER_POINTER);
862 RECORD(TYPE_CONSTANT_ARRAY);
863 RECORD(TYPE_INCOMPLETE_ARRAY);
864 RECORD(TYPE_VARIABLE_ARRAY);
865 RECORD(TYPE_VECTOR);
866 RECORD(TYPE_EXT_VECTOR);
867 RECORD(TYPE_FUNCTION_PROTO);
868 RECORD(TYPE_FUNCTION_NO_PROTO);
869 RECORD(TYPE_TYPEDEF);
870 RECORD(TYPE_TYPEOF_EXPR);
871 RECORD(TYPE_TYPEOF);
872 RECORD(TYPE_RECORD);
873 RECORD(TYPE_ENUM);
874 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000875 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000876 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000877 RECORD(TYPE_DECLTYPE);
878 RECORD(TYPE_ELABORATED);
879 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
880 RECORD(TYPE_UNRESOLVED_USING);
881 RECORD(TYPE_INJECTED_CLASS_NAME);
882 RECORD(TYPE_OBJC_OBJECT);
883 RECORD(TYPE_TEMPLATE_TYPE_PARM);
884 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
885 RECORD(TYPE_DEPENDENT_NAME);
886 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
887 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
888 RECORD(TYPE_PAREN);
889 RECORD(TYPE_PACK_EXPANSION);
890 RECORD(TYPE_ATTRIBUTED);
891 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000892 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000893 RECORD(DECL_TYPEDEF);
894 RECORD(DECL_ENUM);
895 RECORD(DECL_RECORD);
896 RECORD(DECL_ENUM_CONSTANT);
897 RECORD(DECL_FUNCTION);
898 RECORD(DECL_OBJC_METHOD);
899 RECORD(DECL_OBJC_INTERFACE);
900 RECORD(DECL_OBJC_PROTOCOL);
901 RECORD(DECL_OBJC_IVAR);
902 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000903 RECORD(DECL_OBJC_CATEGORY);
904 RECORD(DECL_OBJC_CATEGORY_IMPL);
905 RECORD(DECL_OBJC_IMPLEMENTATION);
906 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
907 RECORD(DECL_OBJC_PROPERTY);
908 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000909 RECORD(DECL_FIELD);
910 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000911 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000912 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000913 RECORD(DECL_FILE_SCOPE_ASM);
914 RECORD(DECL_BLOCK);
915 RECORD(DECL_CONTEXT_LEXICAL);
916 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000917 RECORD(DECL_NAMESPACE);
918 RECORD(DECL_NAMESPACE_ALIAS);
919 RECORD(DECL_USING);
920 RECORD(DECL_USING_SHADOW);
921 RECORD(DECL_USING_DIRECTIVE);
922 RECORD(DECL_UNRESOLVED_USING_VALUE);
923 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
924 RECORD(DECL_LINKAGE_SPEC);
925 RECORD(DECL_CXX_RECORD);
926 RECORD(DECL_CXX_METHOD);
927 RECORD(DECL_CXX_CONSTRUCTOR);
928 RECORD(DECL_CXX_DESTRUCTOR);
929 RECORD(DECL_CXX_CONVERSION);
930 RECORD(DECL_ACCESS_SPEC);
931 RECORD(DECL_FRIEND);
932 RECORD(DECL_FRIEND_TEMPLATE);
933 RECORD(DECL_CLASS_TEMPLATE);
934 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
935 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
936 RECORD(DECL_FUNCTION_TEMPLATE);
937 RECORD(DECL_TEMPLATE_TYPE_PARM);
938 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
939 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
940 RECORD(DECL_STATIC_ASSERT);
941 RECORD(DECL_CXX_BASE_SPECIFIERS);
942 RECORD(DECL_INDIRECTFIELD);
943 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
944
Douglas Gregora72d8c42011-06-03 02:27:19 +0000945 // Statements and Exprs can occur in the Decls and Types block.
946 AddStmtsExprs(Stream, Record);
947
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000948 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000949 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000950 RECORD(PPD_MACRO_DEFINITION);
951 RECORD(PPD_INCLUSION_DIRECTIVE);
952
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000953#undef RECORD
954#undef BLOCK
955 Stream.ExitBlock();
956}
957
Douglas Gregore650c8c2009-07-07 00:12:59 +0000958/// \brief Adjusts the given filename to only write out the portion of the
959/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000960///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961/// \param Filename the file name to adjust.
962///
963/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
964/// the returned filename will be adjusted by this system root.
965///
966/// \returns either the original filename (if it needs no adjustment) or the
967/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000968static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000969adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregor832d6202011-07-22 16:35:34 +0000972 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 // Verify that the filename and the system root have the same prefix.
976 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000977 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978 if (Filename[Pos] != isysroot[Pos])
979 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 // We hit the end of the filename before we hit the end of the system root.
982 if (!Filename[Pos])
983 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 // If the file name has a '/' at the current position, skip over the '/'.
986 // We distinguish sysroot-based includes from absolute includes by the
987 // absence of '/' at the beginning of sysroot-based includes.
988 if (Filename[Pos] == '/')
989 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991 return Filename + Pos;
992}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000993
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000994/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000995void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
996 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000997 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000998 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000999 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1000 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001001
Douglas Gregore650c8c2009-07-07 00:12:59 +00001002 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001003 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1011 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1012 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1013 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001014 Record.push_back(VERSION_MAJOR);
1015 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001016 Record.push_back(CLANG_VERSION_MAJOR);
1017 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001018 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001019 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001020 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1021 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001022
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001023 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001024 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001025 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001026 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001027 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001028
1029 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1030 M != MEnd; ++M) {
1031 // Skip modules that weren't directly imported.
1032 if (!(*M)->isDirectlyImported())
1033 continue;
1034
1035 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001036 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001037 Record.push_back((*M)->File->getSize());
1038 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001039 // FIXME: This writes the absolute path for AST files we depend on.
1040 const std::string &FileName = (*M)->FileName;
1041 Record.push_back(FileName.size());
1042 Record.append(FileName.begin(), FileName.end());
1043 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001044 Stream.EmitRecord(IMPORTS, Record);
1045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001047 // Language options.
1048 Record.clear();
1049 const LangOptions &LangOpts = Context.getLangOpts();
1050#define LANGOPT(Name, Bits, Default, Description) \
1051 Record.push_back(LangOpts.Name);
1052#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1053 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1054#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001055#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1056#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001057
1058 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1059 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1060
1061 Record.push_back(LangOpts.CurrentModule.size());
1062 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001063
1064 // Comment options.
1065 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1066 for (CommentOptions::BlockCommandNamesTy::const_iterator
1067 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1068 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1069 I != IEnd; ++I) {
1070 AddString(*I, Record);
1071 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001072 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001073
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001074 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1075
Douglas Gregoree097c12012-10-18 17:58:09 +00001076 // Target options.
1077 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001078 const TargetInfo &Target = Context.getTargetInfo();
1079 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001080 AddString(TargetOpts.Triple, Record);
1081 AddString(TargetOpts.CPU, Record);
1082 AddString(TargetOpts.ABI, Record);
1083 AddString(TargetOpts.CXXABI, Record);
1084 AddString(TargetOpts.LinkerVersion, Record);
1085 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1086 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1087 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1088 }
1089 Record.push_back(TargetOpts.Features.size());
1090 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1091 AddString(TargetOpts.Features[I], Record);
1092 }
1093 Stream.EmitRecord(TARGET_OPTIONS, Record);
1094
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001095 // Diagnostic options.
1096 Record.clear();
1097 const DiagnosticOptions &DiagOpts
1098 = Context.getDiagnostics().getDiagnosticOptions();
1099#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1100#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1101 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1102#include "clang/Basic/DiagnosticOptions.def"
1103 Record.push_back(DiagOpts.Warnings.size());
1104 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1105 AddString(DiagOpts.Warnings[I], Record);
1106 // Note: we don't serialize the log or serialization file names, because they
1107 // are generally transient files and will almost always be overridden.
1108 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1109
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001110 // File system options.
1111 Record.clear();
1112 const FileSystemOptions &FSOpts
1113 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1114 AddString(FSOpts.WorkingDir, Record);
1115 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1116
Douglas Gregorbbf38312012-10-24 16:50:34 +00001117 // Header search options.
1118 Record.clear();
1119 const HeaderSearchOptions &HSOpts
1120 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1121 AddString(HSOpts.Sysroot, Record);
1122
1123 // Include entries.
1124 Record.push_back(HSOpts.UserEntries.size());
1125 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1126 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1127 AddString(Entry.Path, Record);
1128 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001129 Record.push_back(Entry.IsFramework);
1130 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001131 }
1132
1133 // System header prefixes.
1134 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1135 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1136 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1137 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1138 }
1139
1140 AddString(HSOpts.ResourceDir, Record);
1141 AddString(HSOpts.ModuleCachePath, Record);
1142 Record.push_back(HSOpts.DisableModuleHash);
1143 Record.push_back(HSOpts.UseBuiltinIncludes);
1144 Record.push_back(HSOpts.UseStandardSystemIncludes);
1145 Record.push_back(HSOpts.UseStandardCXXIncludes);
1146 Record.push_back(HSOpts.UseLibcxx);
1147 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1148
Douglas Gregora71a7d82012-10-24 20:05:57 +00001149 // Preprocessor options.
1150 Record.clear();
1151 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1152
1153 // Macro definitions.
1154 Record.push_back(PPOpts.Macros.size());
1155 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1156 AddString(PPOpts.Macros[I].first, Record);
1157 Record.push_back(PPOpts.Macros[I].second);
1158 }
1159
1160 // Includes
1161 Record.push_back(PPOpts.Includes.size());
1162 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1163 AddString(PPOpts.Includes[I], Record);
1164
1165 // Macro includes
1166 Record.push_back(PPOpts.MacroIncludes.size());
1167 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1168 AddString(PPOpts.MacroIncludes[I], Record);
1169
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001170 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001171 AddString(PPOpts.ImplicitPCHInclude, Record);
1172 AddString(PPOpts.ImplicitPTHInclude, Record);
1173 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1174 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1175
Douglas Gregor31d375f2011-05-06 21:43:30 +00001176 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001177 SourceManager &SM = Context.getSourceManager();
1178 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1179 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001180 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1181 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001182 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1183 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1184
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001185 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001187 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001188
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001189 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001190 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001191 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001192 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001193 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001194 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001195 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001196 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001197
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001198 Record.clear();
1199 Record.push_back(SM.getMainFileID().getOpaqueValue());
1200 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1201
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001202 // Original PCH directory
1203 if (!OutputFile.empty() && OutputFile != "-") {
1204 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1205 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1207 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1208
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001209 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001210
1211 llvm::sys::fs::make_absolute(OutputPath);
1212 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1213
1214 RecordData Record;
1215 Record.push_back(ORIGINAL_PCH_DIR);
1216 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1217 }
1218
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001219 WriteInputFiles(Context.SourceMgr,
1220 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1221 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001222 Stream.ExitBlock();
1223}
1224
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001225namespace {
1226 /// \brief An input file.
1227 struct InputFileEntry {
1228 const FileEntry *File;
1229 bool IsSystemFile;
1230 bool BufferOverridden;
1231 };
1232}
1233
1234void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1235 HeaderSearchOptions &HSOpts,
1236 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001237 using namespace llvm;
1238 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1239 RecordData Record;
1240
1241 // Create input-file abbreviation.
1242 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1243 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001244 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001245 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1246 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001247 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001248 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1249 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1250
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001251 // Get all ContentCache objects for files, sorted by whether the file is a
1252 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001253 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001254 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1255 // Get this source location entry.
1256 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001257 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001258
1259 // We only care about file entries that were not overridden.
1260 if (!SLoc->isFile())
1261 continue;
1262 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001263 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001264 continue;
1265
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001266 InputFileEntry Entry;
1267 Entry.File = Cache->OrigEntry;
1268 Entry.IsSystemFile = Cache->IsSystemFile;
1269 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001270 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001271 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001272 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001273 SortedFiles.push_front(Entry);
1274 }
1275
1276 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1277 // the set of (non-system) input files. This is simple heuristic for
1278 // detecting whether the system headers may have changed, because it is too
1279 // expensive to stat() all of the system headers.
1280 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001281 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001282 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1283 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1284 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1285 InputFileEntry Entry = { SDKSettingsFile, false, false };
1286 SortedFiles.push_front(Entry);
1287 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001288 }
1289
1290 unsigned UserFilesNum = 0;
1291 // Write out all of the input files.
1292 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001293 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001294 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001295 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001296
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001297 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001298 if (InputFileID != 0)
1299 continue; // already recorded this file.
1300
Douglas Gregora930dc92012-10-22 18:42:04 +00001301 // Record this entry's offset.
1302 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001303
1304 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001305
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001306 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001307 ++UserFilesNum;
1308
Douglas Gregor745e6f12012-10-19 00:38:02 +00001309 Record.clear();
1310 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001311 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001312
1313 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001314 Record.push_back(Entry.File->getSize());
1315 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001316
Douglas Gregora930dc92012-10-22 18:42:04 +00001317 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001318 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001319
Douglas Gregor745e6f12012-10-19 00:38:02 +00001320 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001321 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001322 SmallString<128> FilePath(Filename);
1323
1324 // Ask the file manager to fixup the relative path for us. This will
1325 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001326 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001327
1328 // FIXME: This call to make_absolute shouldn't be necessary, the
1329 // call to FixupRelativePath should always return an absolute path.
1330 llvm::sys::fs::make_absolute(FilePath);
1331 Filename = FilePath.c_str();
1332
1333 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1334
1335 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1336 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001337
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001338 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001339
1340 // Create input file offsets abbreviation.
1341 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1342 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1343 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001344 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1345 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001346 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1347 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1348
1349 // Write input file offsets.
1350 Record.clear();
1351 Record.push_back(INPUT_FILE_OFFSETS);
1352 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001353 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001354 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001355}
1356
Douglas Gregor14f79002009-04-10 03:52:48 +00001357//===----------------------------------------------------------------------===//
1358// Source Manager Serialization
1359//===----------------------------------------------------------------------===//
1360
1361/// \brief Create an abbreviation for the SLocEntry that refers to a
1362/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001363static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001364 using namespace llvm;
1365 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001366 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001371 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001376 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001377}
1378
1379/// \brief Create an abbreviation for the SLocEntry that refers to a
1380/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001381static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001382 using namespace llvm;
1383 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001384 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001390 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001391}
1392
1393/// \brief Create an abbreviation for the SLocEntry that refers to a
1394/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001395static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001396 using namespace llvm;
1397 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001398 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001400 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001401}
1402
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001403/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1404/// expansion.
1405static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001406 using namespace llvm;
1407 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001408 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001414 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001415}
1416
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001417namespace {
1418 // Trait used for the on-disk hash table of header search information.
1419 class HeaderFileInfoTrait {
1420 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001421 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001422
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001423 // Keep track of the framework names we've used during serialization.
1424 SmallVector<char, 128> FrameworkStringData;
1425 llvm::StringMap<unsigned> FrameworkNameOffset;
1426
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001427 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001428 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1429 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001430
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001431 struct key_type {
1432 const FileEntry *FE;
1433 const char *Filename;
1434 };
1435 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001436
1437 typedef HeaderFileInfo data_type;
1438 typedef const data_type &data_type_ref;
1439
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001440 static unsigned ComputeHash(key_type_ref key) {
1441 // The hash is based only on size/time of the file, so that the reader can
1442 // match even when symlinking or excess path elements ("foo/../", "../")
1443 // change the form of the name. However, complete path is still the key.
1444 return llvm::hash_combine(key.FE->getSize(),
1445 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001446 }
1447
1448 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001449 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1450 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1451 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001452 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001453 if (Data.isModuleHeader)
1454 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001455 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001456 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001457 }
1458
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001459 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1460 clang::io::Emit64(Out, key.FE->getSize());
1461 KeyLen -= 8;
1462 clang::io::Emit64(Out, key.FE->getModificationTime());
1463 KeyLen -= 8;
1464 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001465 }
1466
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001467 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001468 data_type_ref Data, unsigned DataLen) {
1469 using namespace clang::io;
1470 uint64_t Start = Out.tell(); (void)Start;
1471
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001472 unsigned char Flags = (Data.isImport << 5)
1473 | (Data.isPragmaOnce << 4)
1474 | (Data.DirInfo << 2)
1475 | (Data.Resolved << 1)
1476 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001477 Emit8(Out, (uint8_t)Flags);
1478 Emit16(Out, (uint16_t) Data.NumIncludes);
1479
1480 if (!Data.ControllingMacro)
1481 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1482 else
1483 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001484
1485 unsigned Offset = 0;
1486 if (!Data.Framework.empty()) {
1487 // If this header refers into a framework, save the framework name.
1488 llvm::StringMap<unsigned>::iterator Pos
1489 = FrameworkNameOffset.find(Data.Framework);
1490 if (Pos == FrameworkNameOffset.end()) {
1491 Offset = FrameworkStringData.size() + 1;
1492 FrameworkStringData.append(Data.Framework.begin(),
1493 Data.Framework.end());
1494 FrameworkStringData.push_back(0);
1495
1496 FrameworkNameOffset[Data.Framework] = Offset;
1497 } else
1498 Offset = Pos->second;
1499 }
1500 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001501
1502 if (Data.isModuleHeader) {
1503 Module *Mod = HS.findModuleForHeader(key.FE);
1504 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1505 }
1506
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001507 assert(Out.tell() - Start == DataLen && "Wrong data length");
1508 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001509
1510 const char *strings_begin() const { return FrameworkStringData.begin(); }
1511 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001512 };
1513} // end anonymous namespace
1514
1515/// \brief Write the header search block for the list of files that
1516///
1517/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001518void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001519 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001520 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1521
1522 if (FilesByUID.size() > HS.header_file_size())
1523 FilesByUID.resize(HS.header_file_size());
1524
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001525 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001526 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001527 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001528 unsigned NumHeaderSearchEntries = 0;
1529 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1530 const FileEntry *File = FilesByUID[UID];
1531 if (!File)
1532 continue;
1533
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001534 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1535 // from the external source if it was not provided already.
1536 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001537 if (HFI.External && Chain)
1538 continue;
1539
1540 // Turn the file name into an absolute path, if it isn't already.
1541 const char *Filename = File->getName();
1542 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1543
1544 // If we performed any translation on the file name at all, we need to
1545 // save this string, since the generator will refer to it later.
1546 if (Filename != File->getName()) {
1547 Filename = strdup(Filename);
1548 SavedStrings.push_back(Filename);
1549 }
1550
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001551 HeaderFileInfoTrait::key_type key = { File, Filename };
1552 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001553 ++NumHeaderSearchEntries;
1554 }
1555
1556 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001557 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001558 uint32_t BucketOffset;
1559 {
1560 llvm::raw_svector_ostream Out(TableData);
1561 // Make sure that no bucket is at offset 0
1562 clang::io::Emit32(Out, 0);
1563 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1564 }
1565
1566 // Create a blob abbreviation
1567 using namespace llvm;
1568 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1569 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1574 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1575
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001576 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001577 RecordData Record;
1578 Record.push_back(HEADER_SEARCH_TABLE);
1579 Record.push_back(BucketOffset);
1580 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001581 Record.push_back(TableData.size());
1582 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001583 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1584
1585 // Free all of the strings we had to duplicate.
1586 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001587 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001588}
1589
Douglas Gregor14f79002009-04-10 03:52:48 +00001590/// \brief Writes the block containing the serialized form of the
1591/// source manager.
1592///
1593/// TODO: We should probably use an on-disk hash table (stored in a
1594/// blob), indexed based on the file name, so that we only create
1595/// entries for files that we actually need. In the common case (no
1596/// errors), we probably won't have to create file entries for any of
1597/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001598void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001599 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001600 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001601 RecordData Record;
1602
Chris Lattnerf04ad692009-04-10 17:16:57 +00001603 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001604 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001605
1606 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001607 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1608 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1609 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001610 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001611
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001612 // Write out the source location entry table. We skip the first
1613 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001614 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001615 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001616 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1617 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001618 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001619 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001620 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001621 FileID FID = FileID::get(I);
1622 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001623
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001624 // Record the offset of this source-location entry.
1625 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1626
1627 // Figure out which record code to use.
1628 unsigned Code;
1629 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001630 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1631 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001632 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001633 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001634 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001635 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001636 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001637 Record.clear();
1638 Record.push_back(Code);
1639
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001640 // Starting offset of this entry within this module, so skip the dummy.
1641 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001642 if (SLoc->isFile()) {
1643 const SrcMgr::FileInfo &File = SLoc->getFile();
1644 Record.push_back(File.getIncludeLoc().getRawEncoding());
1645 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1646 Record.push_back(File.hasLineDirectives());
1647
1648 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001649 if (Content->OrigEntry) {
1650 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001651 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001652
Douglas Gregora930dc92012-10-22 18:42:04 +00001653 // The source location entry is a file. Emit input file ID.
1654 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1655 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001657 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001658
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001659 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001660 if (FDI != FileDeclIDs.end()) {
1661 Record.push_back(FDI->second->FirstDeclIndex);
1662 Record.push_back(FDI->second->DeclIDs.size());
1663 } else {
1664 Record.push_back(0);
1665 Record.push_back(0);
1666 }
Douglas Gregora081da52011-11-16 20:05:18 +00001667
Douglas Gregora930dc92012-10-22 18:42:04 +00001668 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001669
1670 if (Content->BufferOverridden) {
1671 Record.clear();
1672 Record.push_back(SM_SLOC_BUFFER_BLOB);
1673 const llvm::MemoryBuffer *Buffer
1674 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1675 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1676 StringRef(Buffer->getBufferStart(),
1677 Buffer->getBufferSize() + 1));
1678 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001679 } else {
1680 // The source location entry is a buffer. The blob associated
1681 // with this entry contains the contents of the buffer.
1682
1683 // We add one to the size so that we capture the trailing NULL
1684 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1685 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001686 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001687 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001688 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001689 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001690 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001691 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001692 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001693 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001694 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001695 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001696
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001697 if (strcmp(Name, "<built-in>") == 0) {
1698 PreloadSLocs.push_back(SLocEntryOffsets.size());
1699 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001700 }
1701 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001702 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001703 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001704 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1705 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001706 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1707 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001708
1709 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001710 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001711 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001712 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001713 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001714 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001715 }
1716 }
1717
Douglas Gregorc9490c02009-04-16 22:23:12 +00001718 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001719
1720 if (SLocEntryOffsets.empty())
1721 return;
1722
Sebastian Redl3397c552010-08-18 23:56:27 +00001723 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001724 // table is used for lazily loading source-location information.
1725 using namespace llvm;
1726 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1731 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001733 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001734 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001735 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001736 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001737 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001738
Sebastian Redl3397c552010-08-18 23:56:27 +00001739 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001740 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001741 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001742
1743 // Write the line table. It depends on remapping working, so it must come
1744 // after the source location offsets.
1745 if (SourceMgr.hasLineTable()) {
1746 LineTableInfo &LineTable = SourceMgr.getLineTable();
1747
1748 Record.clear();
1749 // Emit the file names
1750 Record.push_back(LineTable.getNumFilenames());
1751 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1752 // Emit the file name
1753 const char *Filename = LineTable.getFilename(I);
1754 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1755 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1756 Record.push_back(FilenameLen);
1757 if (FilenameLen)
1758 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1759 }
1760
1761 // Emit the line entries
1762 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1763 L != LEnd; ++L) {
1764 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001765 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001766 continue;
1767
1768 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001769 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001770
1771 // Emit the line entries
1772 Record.push_back(L->second.size());
1773 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1774 LEEnd = L->second.end();
1775 LE != LEEnd; ++LE) {
1776 Record.push_back(LE->FileOffset);
1777 Record.push_back(LE->LineNo);
1778 Record.push_back(LE->FilenameID);
1779 Record.push_back((unsigned)LE->FileKind);
1780 Record.push_back(LE->IncludeOffset);
1781 }
1782 }
1783 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1784 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001785}
1786
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001787//===----------------------------------------------------------------------===//
1788// Preprocessor Serialization
1789//===----------------------------------------------------------------------===//
1790
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001791namespace {
1792class ASTMacroTableTrait {
1793public:
1794 typedef IdentID key_type;
1795 typedef key_type key_type_ref;
1796
1797 struct Data {
1798 uint32_t MacroDirectivesOffset;
1799 };
1800
1801 typedef Data data_type;
1802 typedef const data_type &data_type_ref;
1803
1804 static unsigned ComputeHash(IdentID IdID) {
1805 return llvm::hash_value(IdID);
1806 }
1807
1808 std::pair<unsigned,unsigned>
1809 static EmitKeyDataLength(raw_ostream& Out,
1810 key_type_ref Key, data_type_ref Data) {
1811 unsigned KeyLen = 4; // IdentID.
1812 unsigned DataLen = 4; // MacroDirectivesOffset.
1813 return std::make_pair(KeyLen, DataLen);
1814 }
1815
1816 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1817 clang::io::Emit32(Out, Key);
1818 }
1819
1820 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1821 unsigned) {
1822 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1823 }
1824};
1825} // end anonymous namespace
1826
1827static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1828 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1829 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1830 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1831 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001832 return X.first->getName().compare(Y.first->getName());
1833}
1834
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001835static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1836 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001837 if (MacroInfo *MI = MD->getMacroInfo())
1838 if (MI->isBuiltinMacro())
1839 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001840
1841 if (IsModule) {
1842 SourceLocation Loc = MD->getLocation();
1843 if (Loc.isInvalid())
1844 return true;
1845 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1846 return true;
1847 }
1848
1849 return false;
1850}
1851
Chris Lattner0b1fb982009-04-10 17:15:23 +00001852/// \brief Writes the block containing the serialized form of the
1853/// preprocessor.
1854///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001855void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001856 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1857 if (PPRec)
1858 WritePreprocessorDetail(*PPRec);
1859
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001860 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001861
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001862 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1863 if (PP.getCounterValue() != 0) {
1864 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001865 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001866 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001867 }
1868
1869 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001870 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Sebastian Redl3397c552010-08-18 23:56:27 +00001872 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001873 // FIXME: use diagnostics subsystem for localization etc.
1874 if (PP.SawDateOrTime())
1875 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Douglas Gregorecdcb882010-10-20 22:00:55 +00001877
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001878 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001879 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001880
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001881 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001882 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001883 MacroDirectives;
1884 for (Preprocessor::macro_iterator
1885 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1886 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001887 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001888 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001889 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001890
Douglas Gregor9c736102011-02-10 18:20:09 +00001891 // Sort the set of macro definitions that need to be serialized by the
1892 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001893 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1894 &compareMacroDirectives);
1895
1896 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1897
1898 // Emit the macro directives as a list and associate the offset with the
1899 // identifier they belong to.
1900 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1901 const IdentifierInfo *Name = MacroDirectives[I].first;
1902 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1903 MacroDirective *MD = MacroDirectives[I].second;
1904
1905 // If the macro or identifier need no updates, don't write the macro history
1906 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001907 // FIXME: Chain the macro history instead of re-writing it.
1908 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001909 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1910 continue;
1911
1912 // Emit the macro directives in reverse source order.
1913 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001914 if (MD->isHidden())
1915 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001916 if (shouldIgnoreMacro(MD, IsModule, PP))
1917 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001918
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001919 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001920 Record.push_back(MD->getKind());
1921 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1922 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1923 Record.push_back(InfoID);
1924 Record.push_back(DefMD->isImported());
1925 Record.push_back(DefMD->isAmbiguous());
1926
1927 } else if (VisibilityMacroDirective *
1928 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1929 Record.push_back(VisMD->isPublic());
1930 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001931 }
1932 if (Record.empty())
1933 continue;
1934
1935 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1936 Record.clear();
1937
1938 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1939
1940 IdentID NameID = getIdentifierRef(Name);
1941 ASTMacroTableTrait::Data data;
1942 data.MacroDirectivesOffset = MacroDirectiveOffset;
1943 Generator.insert(NameID, data);
1944 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001945
Douglas Gregora8235d62012-10-09 23:05:51 +00001946 /// \brief Offsets of each of the macros into the bitstream, indexed by
1947 /// the local macro ID
1948 ///
1949 /// For each identifier that is associated with a macro, this map
1950 /// provides the offset into the bitstream where that macro is
1951 /// defined.
1952 std::vector<uint32_t> MacroOffsets;
1953
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001954 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1955 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1956 MacroInfo *MI = MacroInfosToEmit[I].MI;
1957 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001958
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001959 if (ID < FirstMacroID) {
1960 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1961 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001962 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001963
1964 // Record the local offset of this macro.
1965 unsigned Index = ID - FirstMacroID;
1966 if (Index == MacroOffsets.size())
1967 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1968 else {
1969 if (Index > MacroOffsets.size())
1970 MacroOffsets.resize(Index + 1);
1971
1972 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1973 }
1974
1975 AddIdentifierRef(Name, Record);
1976 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1977 AddSourceLocation(MI->getDefinitionLoc(), Record);
1978 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1979 Record.push_back(MI->isUsed());
1980 unsigned Code;
1981 if (MI->isObjectLike()) {
1982 Code = PP_MACRO_OBJECT_LIKE;
1983 } else {
1984 Code = PP_MACRO_FUNCTION_LIKE;
1985
1986 Record.push_back(MI->isC99Varargs());
1987 Record.push_back(MI->isGNUVarargs());
1988 Record.push_back(MI->hasCommaPasting());
1989 Record.push_back(MI->getNumArgs());
1990 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1991 I != E; ++I)
1992 AddIdentifierRef(*I, Record);
1993 }
1994
1995 // If we have a detailed preprocessing record, record the macro definition
1996 // ID that corresponds to this macro.
1997 if (PPRec)
1998 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1999
2000 Stream.EmitRecord(Code, Record);
2001 Record.clear();
2002
2003 // Emit the tokens array.
2004 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2005 // Note that we know that the preprocessor does not have any annotation
2006 // tokens in it because they are created by the parser, and thus can't
2007 // be in a macro definition.
2008 const Token &Tok = MI->getReplacementToken(TokNo);
2009
2010 Record.push_back(Tok.getLocation().getRawEncoding());
2011 Record.push_back(Tok.getLength());
2012
2013 // FIXME: When reading literal tokens, reconstruct the literal pointer
2014 // if it is needed.
2015 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
2016 // FIXME: Should translate token kind to a stable encoding.
2017 Record.push_back(Tok.getKind());
2018 // FIXME: Should translate token flags to a stable encoding.
2019 Record.push_back(Tok.getFlags());
2020
2021 Stream.EmitRecord(PP_TOKEN, Record);
2022 Record.clear();
2023 }
2024 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002025 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002026
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002027 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002028
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002029 // Create the on-disk hash table in a buffer.
2030 SmallString<4096> MacroTable;
2031 uint32_t BucketOffset;
2032 {
2033 llvm::raw_svector_ostream Out(MacroTable);
2034 // Make sure that no bucket is at offset 0
2035 clang::io::Emit32(Out, 0);
2036 BucketOffset = Generator.Emit(Out);
2037 }
2038
2039 // Write the macro table
2040 using namespace llvm;
2041 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2042 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2045 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2046
2047 Record.push_back(MACRO_TABLE);
2048 Record.push_back(BucketOffset);
2049 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2050 Record.clear();
2051
Douglas Gregora8235d62012-10-09 23:05:51 +00002052 // Write the offsets table for macro IDs.
2053 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002054 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002055 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2059
2060 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2061 Record.clear();
2062 Record.push_back(MACRO_OFFSET);
2063 Record.push_back(MacroOffsets.size());
2064 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2065 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2066 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002067}
2068
2069void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002070 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002071 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002072
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002073 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002074
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002075 // Enter the preprocessor block.
2076 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002077
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002078 // If the preprocessor has a preprocessing record, emit it.
2079 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002080 using namespace llvm;
2081
2082 // Set up the abbreviation for
2083 unsigned InclusionAbbrev = 0;
2084 {
2085 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2086 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2092 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2093 }
2094
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002095 unsigned FirstPreprocessorEntityID
2096 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2097 + NUM_PREDEF_PP_ENTITY_IDS;
2098 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002099 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002100 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2101 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002102 E != EEnd;
2103 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002104 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002105
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002106 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2107 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002108
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002109 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002110 // Record this macro definition's ID.
2111 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002112
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002113 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002114 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2115 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002116 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002117
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002118 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002119 Record.push_back(ME->isBuiltinMacro());
2120 if (ME->isBuiltinMacro())
2121 AddIdentifierRef(ME->getName(), Record);
2122 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002123 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002124 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002125 continue;
2126 }
2127
2128 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2129 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002130 Record.push_back(ID->getFileName().size());
2131 Record.push_back(ID->wasInQuotes());
2132 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002133 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002134 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002135 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002136 // Check that the FileEntry is not null because it was not resolved and
2137 // we create a PCH even with compiler errors.
2138 if (ID->getFile())
2139 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002140 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2141 continue;
2142 }
2143
2144 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2145 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002146 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002147
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002148 // Write the offsets table for the preprocessing record.
2149 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002150 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2151
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002152 // Write the offsets table for identifier IDs.
2153 using namespace llvm;
2154 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002155 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002158 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002159
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002160 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002161 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002162 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002163 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2164 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002165 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002166}
2167
Douglas Gregore209e502011-12-06 01:10:29 +00002168unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2169 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2170 if (Known != SubmoduleIDs.end())
2171 return Known->second;
2172
2173 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2174}
2175
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002176unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2177 if (!Mod)
2178 return 0;
2179
2180 llvm::DenseMap<Module *, unsigned>::const_iterator
2181 Known = SubmoduleIDs.find(Mod);
2182 if (Known != SubmoduleIDs.end())
2183 return Known->second;
2184
2185 return 0;
2186}
2187
Douglas Gregor26ced122011-12-01 00:59:36 +00002188/// \brief Compute the number of modules within the given tree (including the
2189/// given module).
2190static unsigned getNumberOfModules(Module *Mod) {
2191 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002192 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2193 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002194 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002195 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002196
2197 return ChildModules + 1;
2198}
2199
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002200void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002201 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002202 // FIXME: This feels like it belongs somewhere else, but there are no
2203 // other consumers of this information.
2204 SourceManager &SrcMgr = PP->getSourceManager();
2205 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2206 for (ASTContext::import_iterator I = Context->local_import_begin(),
2207 IEnd = Context->local_import_end();
2208 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002209 if (Module *ImportedFrom
2210 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2211 SrcMgr))) {
2212 ImportedFrom->Imports.push_back(I->getImportedModule());
2213 }
2214 }
2215
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002216 // Enter the submodule description block.
2217 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2218
2219 // Write the abbreviations needed for the submodules block.
2220 using namespace llvm;
2221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2222 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2233 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2234
2235 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002236 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2238 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2239
2240 Abbrev = new BitCodeAbbrev();
2241 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2243 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002244
2245 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002246 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2248 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2249
2250 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002251 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2253 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2254
Douglas Gregor51f564f2011-12-31 04:05:44 +00002255 Abbrev = new BitCodeAbbrev();
2256 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2258 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2259
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002260 Abbrev = new BitCodeAbbrev();
2261 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2263 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2264
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002265 Abbrev = new BitCodeAbbrev();
2266 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2269 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2270
Douglas Gregor63a72682013-03-20 00:22:05 +00002271 Abbrev = new BitCodeAbbrev();
2272 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2274 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2275
Douglas Gregor906d66a2013-03-20 21:10:35 +00002276 Abbrev = new BitCodeAbbrev();
2277 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2280 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2281
Douglas Gregor26ced122011-12-01 00:59:36 +00002282 // Write the submodule metadata block.
2283 RecordData Record;
2284 Record.push_back(getNumberOfModules(WritingModule));
2285 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2286 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2287
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002288 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002289 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002290 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002291 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002292 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002293 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002294 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002295
2296 // Emit the definition of the block.
2297 Record.clear();
2298 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002299 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002300 if (Mod->Parent) {
2301 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2302 Record.push_back(SubmoduleIDs[Mod->Parent]);
2303 } else {
2304 Record.push_back(0);
2305 }
2306 Record.push_back(Mod->IsFramework);
2307 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002308 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002309 Record.push_back(Mod->InferSubmodules);
2310 Record.push_back(Mod->InferExplicitSubmodules);
2311 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002312 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002313 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2314
Douglas Gregor51f564f2011-12-31 04:05:44 +00002315 // Emit the requirements.
2316 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2317 Record.clear();
2318 Record.push_back(SUBMODULE_REQUIRES);
2319 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2320 Mod->Requires[I].data(),
2321 Mod->Requires[I].size());
2322 }
2323
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002324 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002325 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002326 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002327 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002328 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002329 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002330 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2331 Record.clear();
2332 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2333 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2334 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002335 }
2336
2337 // Emit the headers.
2338 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2339 Record.clear();
2340 Record.push_back(SUBMODULE_HEADER);
2341 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2342 Mod->Headers[I]->getName());
2343 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002344 // Emit the excluded headers.
2345 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2346 Record.clear();
2347 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2348 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2349 Mod->ExcludedHeaders[I]->getName());
2350 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002351 ArrayRef<const FileEntry *>
2352 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2353 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002354 Record.clear();
2355 Record.push_back(SUBMODULE_TOPHEADER);
2356 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002357 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002358 }
Douglas Gregor55988682011-12-05 16:33:54 +00002359
2360 // Emit the imports.
2361 if (!Mod->Imports.empty()) {
2362 Record.clear();
2363 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002364 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002365 assert(ImportedID && "Unknown submodule!");
2366 Record.push_back(ImportedID);
2367 }
2368 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2369 }
2370
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002371 // Emit the exports.
2372 if (!Mod->Exports.empty()) {
2373 Record.clear();
2374 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002375 if (Module *Exported = Mod->Exports[I].getPointer()) {
2376 unsigned ExportedID = SubmoduleIDs[Exported];
2377 assert(ExportedID > 0 && "Unknown submodule ID?");
2378 Record.push_back(ExportedID);
2379 } else {
2380 Record.push_back(0);
2381 }
2382
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002383 Record.push_back(Mod->Exports[I].getInt());
2384 }
2385 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2386 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002387
2388 // Emit the link libraries.
2389 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2390 Record.clear();
2391 Record.push_back(SUBMODULE_LINK_LIBRARY);
2392 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2393 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2394 Mod->LinkLibraries[I].Library);
2395 }
2396
Douglas Gregor906d66a2013-03-20 21:10:35 +00002397 // Emit the conflicts.
2398 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2399 Record.clear();
2400 Record.push_back(SUBMODULE_CONFLICT);
2401 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2402 assert(OtherID && "Unknown submodule!");
2403 Record.push_back(OtherID);
2404 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2405 Mod->Conflicts[I].Message);
2406 }
2407
Douglas Gregor63a72682013-03-20 00:22:05 +00002408 // Emit the configuration macros.
2409 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2410 Record.clear();
2411 Record.push_back(SUBMODULE_CONFIG_MACRO);
2412 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2413 Mod->ConfigMacros[I]);
2414 }
2415
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002416 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002417 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2418 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002419 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002420 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002421 }
2422
2423 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002424
2425 assert((NextSubmoduleID - FirstSubmoduleID
2426 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002427}
2428
Douglas Gregor185dbd72011-12-01 02:07:58 +00002429serialization::SubmoduleID
2430ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002431 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002432 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002433
2434 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002435 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002436 Module *OwningMod
2437 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002438 if (!OwningMod)
2439 return 0;
2440
Douglas Gregore209e502011-12-06 01:10:29 +00002441 // Check whether this submodule is part of our own module.
2442 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002443 return 0;
2444
Douglas Gregore209e502011-12-06 01:10:29 +00002445 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002446}
2447
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002448void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2449 bool isModule) {
2450 // Make sure set diagnostic pragmas don't affect the translation unit that
2451 // imports the module.
2452 // FIXME: Make diagnostic pragma sections work properly with modules.
2453 if (isModule)
2454 return;
2455
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002456 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2457 DiagStateIDMap;
2458 unsigned CurrID = 0;
2459 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002460 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002461 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002462 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2463 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002464 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002465 if (point.Loc.isInvalid())
2466 continue;
2467
2468 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002469 unsigned &DiagStateID = DiagStateIDMap[point.State];
2470 Record.push_back(DiagStateID);
2471
2472 if (DiagStateID == 0) {
2473 DiagStateID = ++CurrID;
2474 for (DiagnosticsEngine::DiagState::const_iterator
2475 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2476 if (I->second.isPragma()) {
2477 Record.push_back(I->first);
2478 Record.push_back(I->second.getMapping());
2479 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002480 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002481 Record.push_back(-1); // mark the end of the diag/map pairs for this
2482 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002483 }
2484 }
2485
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002486 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002487 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002488}
2489
Anders Carlssonc8505782011-03-06 18:41:18 +00002490void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2491 if (CXXBaseSpecifiersOffsets.empty())
2492 return;
2493
2494 RecordData Record;
2495
2496 // Create a blob abbreviation for the C++ base specifiers offsets.
2497 using namespace llvm;
2498
2499 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2500 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2503 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2504
Douglas Gregore92b8a12011-08-04 00:01:48 +00002505 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002506 Record.clear();
2507 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2508 Record.push_back(CXXBaseSpecifiersOffsets.size());
2509 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002510 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002511}
2512
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002513//===----------------------------------------------------------------------===//
2514// Type Serialization
2515//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002516
Sebastian Redl3397c552010-08-18 23:56:27 +00002517/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002518void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002519 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002520 if (Idx.getIndex() == 0) // we haven't seen this type before.
2521 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002522
Douglas Gregor97475832010-10-05 18:37:06 +00002523 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002524
Douglas Gregor2cf26342009-04-09 22:27:44 +00002525 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002526 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002527 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002528 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002529 else if (TypeOffsets.size() < Index) {
2530 TypeOffsets.resize(Index + 1);
2531 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002532 }
2533
2534 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002535
Douglas Gregor2cf26342009-04-09 22:27:44 +00002536 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002537 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002538
Douglas Gregora4923eb2009-11-16 21:35:15 +00002539 if (T.hasLocalNonFastQualifiers()) {
2540 Qualifiers Qs = T.getLocalQualifiers();
2541 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002542 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002543 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002544 } else {
2545 switch (T->getTypeClass()) {
2546 // For all of the concrete, non-dependent types, call the
2547 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002548#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002549 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002550#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002551#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002552 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002553 }
2554
2555 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002556 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002557
2558 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002559 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002560}
2561
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002562//===----------------------------------------------------------------------===//
2563// Declaration Serialization
2564//===----------------------------------------------------------------------===//
2565
Douglas Gregor2cf26342009-04-09 22:27:44 +00002566/// \brief Write the block containing all of the declaration IDs
2567/// lexically declared within the given DeclContext.
2568///
2569/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2570/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002571uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002572 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002573 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002574 return 0;
2575
Douglas Gregorc9490c02009-04-16 22:23:12 +00002576 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002577 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002578 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002579 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002580 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2581 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002582 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002583
Douglas Gregor25123082009-04-22 22:34:57 +00002584 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002585 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002586 return Offset;
2587}
2588
Sebastian Redla4232eb2010-08-18 23:56:21 +00002589void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002590 using namespace llvm;
2591 RecordData Record;
2592
2593 // Write the type offsets array
2594 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002595 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002596 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002597 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2599 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2600 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002601 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002602 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002603 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002604 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002605
2606 // Write the declaration offsets array
2607 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002608 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2612 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2613 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002614 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002615 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002616 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002617 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002618}
2619
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002620void ASTWriter::WriteFileDeclIDsMap() {
2621 using namespace llvm;
2622 RecordData Record;
2623
2624 // Join the vectors of DeclIDs from all files.
2625 SmallVector<DeclID, 256> FileSortedIDs;
2626 for (FileDeclIDsTy::iterator
2627 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2628 DeclIDInFileInfo &Info = *FI->second;
2629 Info.FirstDeclIndex = FileSortedIDs.size();
2630 for (LocDeclIDsTy::iterator
2631 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2632 FileSortedIDs.push_back(DI->second);
2633 }
2634
2635 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2636 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2639 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2640 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002641 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002642 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2643}
2644
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002645void ASTWriter::WriteComments() {
2646 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002647 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002648 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002649 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2650 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002651 I != E; ++I) {
2652 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002653 AddSourceRange((*I)->getSourceRange(), Record);
2654 Record.push_back((*I)->getKind());
2655 Record.push_back((*I)->isTrailingComment());
2656 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002657 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2658 }
2659 Stream.ExitBlock();
2660}
2661
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002662//===----------------------------------------------------------------------===//
2663// Global Method Pool and Selector Serialization
2664//===----------------------------------------------------------------------===//
2665
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002666namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002667// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002668class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002669 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002670
2671public:
2672 typedef Selector key_type;
2673 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Sebastian Redl5d050072010-08-04 17:20:04 +00002675 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002676 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002677 ObjCMethodList Instance, Factory;
2678 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002679 typedef const data_type& data_type_ref;
2680
Sebastian Redl3397c552010-08-18 23:56:27 +00002681 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002683 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002684 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002685 }
Mike Stump1eb44332009-09-09 15:08:12 +00002686
2687 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002688 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002689 data_type_ref Methods) {
2690 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2691 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002692 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2693 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002694 Method = Method->Next)
2695 if (Method->Method)
2696 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002697 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002698 Method = Method->Next)
2699 if (Method->Method)
2700 DataLen += 4;
2701 clang::io::Emit16(Out, DataLen);
2702 return std::make_pair(KeyLen, DataLen);
2703 }
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Chris Lattner5f9e2722011-07-23 10:55:15 +00002705 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002706 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002707 assert((Start >> 32) == 0 && "Selector key offset too large");
2708 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002709 unsigned N = Sel.getNumArgs();
2710 clang::io::Emit16(Out, N);
2711 if (N == 0)
2712 N = 1;
2713 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002714 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002715 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2716 }
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Chris Lattner5f9e2722011-07-23 10:55:15 +00002718 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002719 data_type_ref Methods, unsigned DataLen) {
2720 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002721 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002722 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002723 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002724 Method = Method->Next)
2725 if (Method->Method)
2726 ++NumInstanceMethods;
2727
2728 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002729 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002730 Method = Method->Next)
2731 if (Method->Method)
2732 ++NumFactoryMethods;
2733
2734 clang::io::Emit16(Out, NumInstanceMethods);
2735 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002736 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002737 Method = Method->Next)
2738 if (Method->Method)
2739 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002740 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002741 Method = Method->Next)
2742 if (Method->Method)
2743 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002744
2745 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002746 }
2747};
2748} // end anonymous namespace
2749
Sebastian Redl059612d2010-08-03 21:58:15 +00002750/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002751///
2752/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002753/// in an on-disk hash table indexed by the selector. The hash table also
2754/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002755void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002756 using namespace llvm;
2757
Sebastian Redl059612d2010-08-03 21:58:15 +00002758 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002759 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002760 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002761 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002762 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002763 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002764 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002765 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002766
Sebastian Redl059612d2010-08-03 21:58:15 +00002767 // Create the on-disk hash table representation. We walk through every
2768 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002769 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002770 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002771 I = SelectorIDs.begin(), E = SelectorIDs.end();
2772 I != E; ++I) {
2773 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002774 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002775 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002776 I->second,
2777 ObjCMethodList(),
2778 ObjCMethodList()
2779 };
2780 if (F != SemaRef.MethodPool.end()) {
2781 Data.Instance = F->second.first;
2782 Data.Factory = F->second.second;
2783 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002784 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002785 // changed.
2786 if (Chain && I->second < FirstSelectorID) {
2787 // Selector already exists. Did it change?
2788 bool changed = false;
2789 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2790 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002791 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002792 changed = true;
2793 }
2794 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2795 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002796 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002797 changed = true;
2798 }
2799 if (!changed)
2800 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002801 } else if (Data.Instance.Method || Data.Factory.Method) {
2802 // A new method pool entry.
2803 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002804 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002805 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002806 }
2807
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002808 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002809 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002810 uint32_t BucketOffset;
2811 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002812 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002813 llvm::raw_svector_ostream Out(MethodPool);
2814 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002815 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002816 BucketOffset = Generator.Emit(Out, Trait);
2817 }
2818
2819 // Create a blob abbreviation
2820 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002821 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002822 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002823 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002824 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2825 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2826
Douglas Gregor83941df2009-04-25 17:48:32 +00002827 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002828 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002829 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002830 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002831 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002832 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002833
2834 // Create a blob abbreviation for the selector table offsets.
2835 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002836 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002837 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2840 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2841
2842 // Write the selector offsets table.
2843 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002844 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002845 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002846 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002847 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002848 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002849 }
2850}
2851
Sebastian Redl3397c552010-08-18 23:56:27 +00002852/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002853void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002854 using namespace llvm;
2855 if (SemaRef.ReferencedSelectors.empty())
2856 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002857
Fariborz Jahanian32019832010-07-23 19:11:11 +00002858 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002859
Sebastian Redl3397c552010-08-18 23:56:27 +00002860 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002861 // very tricky to fix, and given that @selector shouldn't really appear in
2862 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002863 for (DenseMap<Selector, SourceLocation>::iterator S =
2864 SemaRef.ReferencedSelectors.begin(),
2865 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2866 Selector Sel = (*S).first;
2867 SourceLocation Loc = (*S).second;
2868 AddSelectorRef(Sel, Record);
2869 AddSourceLocation(Loc, Record);
2870 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002871 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002872}
2873
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002874//===----------------------------------------------------------------------===//
2875// Identifier Table Serialization
2876//===----------------------------------------------------------------------===//
2877
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002878namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002879class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002880 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002881 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002882 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002883 bool IsModule;
2884
Douglas Gregora92193e2009-04-28 21:18:29 +00002885 /// \brief Determines whether this is an "interesting" identifier
2886 /// that needs a full IdentifierInfo structure written into the hash
2887 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002888 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002889 if (II->isPoisoned() ||
2890 II->isExtensionToken() ||
2891 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002892 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002893 II->getFETokenInfo<void>())
2894 return true;
2895
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002896 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002897 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002898
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002899 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002900 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002901 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002902
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002903 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2904 if (!IsModule)
2905 return !shouldIgnoreMacro(Macro, IsModule, PP);
2906 SubmoduleID ModID;
2907 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2908 return true;
2909 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002910
2911 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002912 }
2913
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002914 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2915 SubmoduleID &ModID) {
2916 ModID = 0;
2917 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2918 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2919 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002920 return 0;
2921 }
2922
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002923 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2924 SubmoduleID &ModID) {
2925 if (DefMacroDirective *
2926 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2927 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2928 return DefMD;
2929 return 0;
2930 }
2931
2932 /// \brief Traverses the macro directives history and returns the latest
2933 /// macro that is public and not undefined in the same submodule.
2934 /// A macro that is defined in submodule A and undefined in submodule B,
2935 /// will still be considered as defined/exported from submodule A.
2936 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2937 SubmoduleID &ModID) {
2938 if (!MD)
2939 return 0;
2940
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002941 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002942 bool isUndefined = false;
2943 Optional<bool> isPublic;
2944 for (; MD; MD = MD->getPrevious()) {
2945 if (MD->isHidden())
2946 continue;
2947
2948 SubmoduleID ThisModID = getSubmoduleID(MD);
2949 if (ThisModID == 0) {
2950 isUndefined = false;
2951 isPublic = Optional<bool>();
2952 continue;
2953 }
2954 if (ThisModID != ModID){
2955 ModID = ThisModID;
2956 isUndefined = false;
2957 isPublic = Optional<bool>();
2958 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002959 // We are looking for a definition in a different submodule than the one
2960 // that we started with. If a submodule has re-definitions of the same
2961 // macro, only the last definition will be used as the "exported" one.
2962 if (ModID == OrigModID)
2963 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002964
2965 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2966 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
2967 return DefMD;
2968 continue;
2969 }
2970
2971 if (isa<UndefMacroDirective>(MD)) {
2972 isUndefined = true;
2973 continue;
2974 }
2975
2976 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
2977 if (!isPublic.hasValue())
2978 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002979 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002980
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002981 return 0;
2982 }
2983
2984 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002985 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2986 MacroInfo *MI = DefMD->getInfo();
2987 if (unsigned ID = MI->getOwningModuleID())
2988 return ID;
2989 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
2990 }
2991 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002992 }
2993
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002994public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002995 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002996 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002997
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002998 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002999 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003000
Douglas Gregoreee242f2011-10-27 09:33:13 +00003001 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3002 IdentifierResolver &IdResolver, bool IsModule)
3003 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003004
3005 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003006 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003007 }
Mike Stump1eb44332009-09-09 15:08:12 +00003008
3009 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003010 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003011 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003012 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003013 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003014 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003015 DataLen += 2; // 2 bytes for builtin ID
3016 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003017 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003018 DataLen += 4; // MacroDirectives offset.
3019 if (IsModule) {
3020 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003021 for (DefMacroDirective *
3022 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3023 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003024 DataLen += 4; // MacroInfo ID.
3025 }
3026 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003027 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003028 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003029
Douglas Gregoreee242f2011-10-27 09:33:13 +00003030 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3031 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003032 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003033 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003034 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003035 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003036 // We emit the key length after the data length so that every
3037 // string is preceded by a 16-bit length. This matches the PTH
3038 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003039 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003040 return std::make_pair(KeyLen, DataLen);
3041 }
Mike Stump1eb44332009-09-09 15:08:12 +00003042
Chris Lattner5f9e2722011-07-23 10:55:15 +00003043 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003044 unsigned KeyLen) {
3045 // Record the location of the key data. This is used when generating
3046 // the mapping from persistent IDs to strings.
3047 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003048 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003049 }
Mike Stump1eb44332009-09-09 15:08:12 +00003050
Douglas Gregor7143aab2011-09-01 17:04:32 +00003051 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003052 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003053 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003054 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003055 clang::io::Emit32(Out, ID << 1);
3056 return;
3057 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003058
Douglas Gregora92193e2009-04-28 21:18:29 +00003059 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003060 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3061 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3062 clang::io::Emit16(Out, Bits);
3063 Bits = 0;
3064 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003065 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003066 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003067 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3068 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003069 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003070 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003071 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003072
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003073 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003074 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3075 if (IsModule) {
3076 // Write the IDs of macros coming from different submodules.
3077 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003078 for (DefMacroDirective *
3079 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3080 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3081 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003082 assert(InfoID);
3083 clang::io::Emit32(Out, InfoID);
3084 }
3085 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003086 }
Douglas Gregor13292642011-12-02 15:45:10 +00003087 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003088
Douglas Gregor668c1a42009-04-21 22:25:48 +00003089 // Emit the declaration IDs in reverse order, because the
3090 // IdentifierResolver provides the declarations as they would be
3091 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003092 // "stat"), but the ASTReader adds declarations to the end of the list
3093 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003094 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003095 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3096 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00003097 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003098 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003099 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003100 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003101 }
3102};
3103} // end anonymous namespace
3104
Sebastian Redl3397c552010-08-18 23:56:27 +00003105/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003106///
3107/// The identifier table consists of a blob containing string data
3108/// (the actual identifiers themselves) and a separate "offsets" index
3109/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003110void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3111 IdentifierResolver &IdResolver,
3112 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003113 using namespace llvm;
3114
3115 // Create and write out the blob that contains the identifier
3116 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003117 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003118 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003119 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Douglas Gregor92b059e2009-04-28 20:33:11 +00003121 // Look for any identifiers that were named while processing the
3122 // headers, but are otherwise not needed. We add these to the hash
3123 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003124 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003125 // file.
3126 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3127 IDEnd = PP.getIdentifierTable().end();
3128 ID != IDEnd; ++ID)
3129 getIdentifierRef(ID->second);
3130
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003131 // Create the on-disk hash table representation. We only store offsets
3132 // for identifiers that appear here for the first time.
3133 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003134 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003135 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3136 ID != IDEnd; ++ID) {
3137 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003138 if (!Chain || !ID->first->isFromAST() ||
3139 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003140 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003141 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003142 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003143
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003144 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003145 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003146 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003147 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003148 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003149 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003150 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003151 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003152 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003153 }
3154
3155 // Create a blob abbreviation
3156 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003157 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003158 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003160 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003161
3162 // Write the identifier table
3163 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003164 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003165 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003166 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003167 }
3168
3169 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003170 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003171 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3175 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3176
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003177#ifndef NDEBUG
3178 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3179 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3180#endif
3181
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003182 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003183 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003184 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003185 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003186 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003187 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003188}
3189
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003190//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003191// DeclContext's Name Lookup Table Serialization
3192//===----------------------------------------------------------------------===//
3193
3194namespace {
3195// Trait used for the on-disk hash table used in the method pool.
3196class ASTDeclContextNameLookupTrait {
3197 ASTWriter &Writer;
3198
3199public:
3200 typedef DeclarationName key_type;
3201 typedef key_type key_type_ref;
3202
3203 typedef DeclContext::lookup_result data_type;
3204 typedef const data_type& data_type_ref;
3205
3206 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3207
3208 unsigned ComputeHash(DeclarationName Name) {
3209 llvm::FoldingSetNodeID ID;
3210 ID.AddInteger(Name.getNameKind());
3211
3212 switch (Name.getNameKind()) {
3213 case DeclarationName::Identifier:
3214 ID.AddString(Name.getAsIdentifierInfo()->getName());
3215 break;
3216 case DeclarationName::ObjCZeroArgSelector:
3217 case DeclarationName::ObjCOneArgSelector:
3218 case DeclarationName::ObjCMultiArgSelector:
3219 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3220 break;
3221 case DeclarationName::CXXConstructorName:
3222 case DeclarationName::CXXDestructorName:
3223 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003224 break;
3225 case DeclarationName::CXXOperatorName:
3226 ID.AddInteger(Name.getCXXOverloadedOperator());
3227 break;
3228 case DeclarationName::CXXLiteralOperatorName:
3229 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3230 case DeclarationName::CXXUsingDirective:
3231 break;
3232 }
3233
3234 return ID.ComputeHash();
3235 }
3236
3237 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003238 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003239 data_type_ref Lookup) {
3240 unsigned KeyLen = 1;
3241 switch (Name.getNameKind()) {
3242 case DeclarationName::Identifier:
3243 case DeclarationName::ObjCZeroArgSelector:
3244 case DeclarationName::ObjCOneArgSelector:
3245 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003246 case DeclarationName::CXXLiteralOperatorName:
3247 KeyLen += 4;
3248 break;
3249 case DeclarationName::CXXOperatorName:
3250 KeyLen += 1;
3251 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003252 case DeclarationName::CXXConstructorName:
3253 case DeclarationName::CXXDestructorName:
3254 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003255 case DeclarationName::CXXUsingDirective:
3256 break;
3257 }
3258 clang::io::Emit16(Out, KeyLen);
3259
3260 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003261 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003262 clang::io::Emit16(Out, DataLen);
3263
3264 return std::make_pair(KeyLen, DataLen);
3265 }
3266
Chris Lattner5f9e2722011-07-23 10:55:15 +00003267 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003268 using namespace clang::io;
3269
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003270 Emit8(Out, Name.getNameKind());
3271 switch (Name.getNameKind()) {
3272 case DeclarationName::Identifier:
3273 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003274 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003275 case DeclarationName::ObjCZeroArgSelector:
3276 case DeclarationName::ObjCOneArgSelector:
3277 case DeclarationName::ObjCMultiArgSelector:
3278 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003279 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003280 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003281 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3282 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003283 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003284 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003285 case DeclarationName::CXXLiteralOperatorName:
3286 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003287 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003288 case DeclarationName::CXXConstructorName:
3289 case DeclarationName::CXXDestructorName:
3290 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003291 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003292 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003293 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003294
3295 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003296 }
3297
Chris Lattner5f9e2722011-07-23 10:55:15 +00003298 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003299 data_type Lookup, unsigned DataLen) {
3300 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003301 clang::io::Emit16(Out, Lookup.size());
3302 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3303 I != E; ++I)
3304 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003305
3306 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3307 }
3308};
3309} // end anonymous namespace
3310
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003311/// \brief Write the block containing all of the declaration IDs
3312/// visible from the given DeclContext.
3313///
3314/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003315/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003316uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3317 DeclContext *DC) {
3318 if (DC->getPrimaryContext() != DC)
3319 return 0;
3320
3321 // Since there is no name lookup into functions or methods, don't bother to
3322 // build a visible-declarations table for these entities.
3323 if (DC->isFunctionOrMethod())
3324 return 0;
3325
3326 // If not in C++, we perform name lookup for the translation unit via the
3327 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003328 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003329 return 0;
3330
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003331 // Serialize the contents of the mapping used for lookup. Note that,
3332 // although we have two very different code paths, the serialized
3333 // representation is the same for both cases: a declaration name,
3334 // followed by a size, followed by references to the visible
3335 // declarations that have that name.
3336 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003337 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003338 if (!Map || Map->empty())
3339 return 0;
3340
3341 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3342 ASTDeclContextNameLookupTrait Trait(*this);
3343
3344 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003345 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003346 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003347 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3348 D != DEnd; ++D) {
3349 DeclarationName Name = D->first;
3350 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003351 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003352 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3353 // Hash all conversion function names to the same name. The actual
3354 // type information in conversion function name is not used in the
3355 // key (since such type information is not stable across different
3356 // modules), so the intended effect is to coalesce all of the conversion
3357 // functions under a single key.
3358 if (!ConversionName)
3359 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003360 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003361 continue;
3362 }
3363
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003364 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003365 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003366 }
3367
Douglas Gregore5a54b62011-08-30 20:49:19 +00003368 // Add the conversion functions
3369 if (!ConversionDecls.empty()) {
3370 Generator.insert(ConversionName,
3371 DeclContext::lookup_result(ConversionDecls.begin(),
3372 ConversionDecls.end()),
3373 Trait);
3374 }
3375
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003376 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003377 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003378 uint32_t BucketOffset;
3379 {
3380 llvm::raw_svector_ostream Out(LookupTable);
3381 // Make sure that no bucket is at offset 0
3382 clang::io::Emit32(Out, 0);
3383 BucketOffset = Generator.Emit(Out, Trait);
3384 }
3385
3386 // Write the lookup table
3387 RecordData Record;
3388 Record.push_back(DECL_CONTEXT_VISIBLE);
3389 Record.push_back(BucketOffset);
3390 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3391 LookupTable.str());
3392
3393 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3394 ++NumVisibleDeclContexts;
3395 return Offset;
3396}
3397
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003398/// \brief Write an UPDATE_VISIBLE block for the given context.
3399///
3400/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3401/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003402/// (in C++), for namespaces, and for classes with forward-declared unscoped
3403/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003404void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003405 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3406 if (!Map || Map->empty())
3407 return;
3408
3409 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3410 ASTDeclContextNameLookupTrait Trait(*this);
3411
3412 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003413 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3414 D != DEnd; ++D) {
3415 DeclarationName Name = D->first;
3416 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003417 // For any name that appears in this table, the results are complete, i.e.
3418 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003419 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003420 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003421 }
3422
3423 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003424 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003425 uint32_t BucketOffset;
3426 {
3427 llvm::raw_svector_ostream Out(LookupTable);
3428 // Make sure that no bucket is at offset 0
3429 clang::io::Emit32(Out, 0);
3430 BucketOffset = Generator.Emit(Out, Trait);
3431 }
3432
3433 // Write the lookup table
3434 RecordData Record;
3435 Record.push_back(UPDATE_VISIBLE);
3436 Record.push_back(getDeclID(cast<Decl>(DC)));
3437 Record.push_back(BucketOffset);
3438 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3439}
3440
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003441/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3442void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3443 RecordData Record;
3444 Record.push_back(Opts.fp_contract);
3445 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3446}
3447
3448/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3449void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003450 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003451 return;
3452
3453 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3454 RecordData Record;
3455#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3456#include "clang/Basic/OpenCLExtensions.def"
3457 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3458}
3459
Douglas Gregor2171bf12012-01-15 16:58:34 +00003460void ASTWriter::WriteRedeclarations() {
3461 RecordData LocalRedeclChains;
3462 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3463
3464 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3465 Decl *First = Redeclarations[I];
3466 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3467
3468 Decl *MostRecent = First->getMostRecentDecl();
3469
3470 // If we only have a single declaration, there is no point in storing
3471 // a redeclaration chain.
3472 if (First == MostRecent)
3473 continue;
3474
3475 unsigned Offset = LocalRedeclChains.size();
3476 unsigned Size = 0;
3477 LocalRedeclChains.push_back(0); // Placeholder for the size.
3478
3479 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003480 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003481 Prev = Prev->getPreviousDecl()) {
3482 if (!Prev->isFromASTFile()) {
3483 AddDeclRef(Prev, LocalRedeclChains);
3484 ++Size;
3485 }
3486 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003487
3488 if (!First->isFromASTFile() && Chain) {
3489 Decl *FirstFromAST = MostRecent;
3490 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3491 if (Prev->isFromASTFile())
3492 FirstFromAST = Prev;
3493 }
3494
3495 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3496 }
3497
Douglas Gregor2171bf12012-01-15 16:58:34 +00003498 LocalRedeclChains[Offset] = Size;
3499
3500 // Reverse the set of local redeclarations, so that we store them in
3501 // order (since we found them in reverse order).
3502 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3503
Douglas Gregoraa945902013-02-18 15:53:43 +00003504 // Add the mapping from the first ID from the AST to the set of local
3505 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003506 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3507 LocalRedeclsMap.push_back(Info);
3508
3509 assert(N == Redeclarations.size() &&
3510 "Deserialized a declaration we shouldn't have");
3511 }
3512
3513 if (LocalRedeclChains.empty())
3514 return;
3515
3516 // Sort the local redeclarations map by the first declaration ID,
3517 // since the reader will be performing binary searches on this information.
3518 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3519
3520 // Emit the local redeclarations map.
3521 using namespace llvm;
3522 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3523 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3524 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3526 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3527
3528 RecordData Record;
3529 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3530 Record.push_back(LocalRedeclsMap.size());
3531 Stream.EmitRecordWithBlob(AbbrevID, Record,
3532 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3533 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3534
3535 // Emit the redeclaration chains.
3536 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3537}
3538
Douglas Gregorcff9f262012-01-27 01:47:08 +00003539void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003540 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003541 RecordData Categories;
3542
3543 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3544 unsigned Size = 0;
3545 unsigned StartIndex = Categories.size();
3546
3547 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3548
3549 // Allocate space for the size.
3550 Categories.push_back(0);
3551
3552 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003553 for (ObjCInterfaceDecl::known_categories_iterator
3554 Cat = Class->known_categories_begin(),
3555 CatEnd = Class->known_categories_end();
3556 Cat != CatEnd; ++Cat, ++Size) {
3557 assert(getDeclID(*Cat) != 0 && "Bogus category");
3558 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003559 }
3560
3561 // Update the size.
3562 Categories[StartIndex] = Size;
3563
3564 // Record this interface -> category map.
3565 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3566 CategoriesMap.push_back(CatInfo);
3567 }
3568
3569 // Sort the categories map by the definition ID, since the reader will be
3570 // performing binary searches on this information.
3571 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3572
3573 // Emit the categories map.
3574 using namespace llvm;
3575 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3576 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3579 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3580
3581 RecordData Record;
3582 Record.push_back(OBJC_CATEGORIES_MAP);
3583 Record.push_back(CategoriesMap.size());
3584 Stream.EmitRecordWithBlob(AbbrevID, Record,
3585 reinterpret_cast<char*>(CategoriesMap.data()),
3586 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3587
3588 // Emit the category lists.
3589 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3590}
3591
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003592void ASTWriter::WriteMergedDecls() {
3593 if (!Chain || Chain->MergedDecls.empty())
3594 return;
3595
3596 RecordData Record;
3597 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3598 IEnd = Chain->MergedDecls.end();
3599 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003600 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003601 : getDeclID(I->first);
3602 assert(CanonID && "Merged declaration not known?");
3603
3604 Record.push_back(CanonID);
3605 Record.push_back(I->second.size());
3606 Record.append(I->second.begin(), I->second.end());
3607 }
3608 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3609}
3610
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003611//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003612// General Serialization Routines
3613//===----------------------------------------------------------------------===//
3614
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003615/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003616void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3617 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003618 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003619 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3620 e = Attrs.end(); i != e; ++i){
3621 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003622 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003623 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003624
Sean Huntcf807c42010-08-18 23:23:40 +00003625#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003626
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003627 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003628}
3629
Chris Lattner5f9e2722011-07-23 10:55:15 +00003630void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003631 Record.push_back(Str.size());
3632 Record.insert(Record.end(), Str.begin(), Str.end());
3633}
3634
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003635void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3636 RecordDataImpl &Record) {
3637 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003638 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003639 Record.push_back(*Minor + 1);
3640 else
3641 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003642 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003643 Record.push_back(*Subminor + 1);
3644 else
3645 Record.push_back(0);
3646}
3647
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003648/// \brief Note that the identifier II occurs at the given offset
3649/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003650void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003651 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003652 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003653 // up earlier in the chain and thus don't need an offset.
3654 if (ID >= FirstIdentID)
3655 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003656}
3657
Douglas Gregor83941df2009-04-25 17:48:32 +00003658/// \brief Note that the selector Sel occurs at the given offset
3659/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003660void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003661 unsigned ID = SelectorIDs[Sel];
3662 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003663 // Don't record offsets for selectors that are also available in a different
3664 // file.
3665 if (ID < FirstSelectorID)
3666 return;
3667 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003668}
3669
Sebastian Redla4232eb2010-08-18 23:56:21 +00003670ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003671 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003672 WritingAST(false), DoneWritingDeclsAndTypes(false),
3673 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003674 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003675 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003676 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3677 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003678 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3679 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003680 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003681 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003682 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003683 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003684 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003685 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003686 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3687 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3688 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003689 DeclTypedefAbbrev(0),
3690 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3691 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003692{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003693}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003694
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003695ASTWriter::~ASTWriter() {
3696 for (FileDeclIDsTy::iterator
3697 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3698 delete I->second;
3699}
3700
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003701void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003702 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003703 Module *WritingModule, StringRef isysroot,
3704 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003705 WritingAST = true;
3706
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003707 ASTHasCompilerErrors = hasErrors;
3708
Douglas Gregor2cf26342009-04-09 22:27:44 +00003709 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003710 Stream.Emit((unsigned)'C', 8);
3711 Stream.Emit((unsigned)'P', 8);
3712 Stream.Emit((unsigned)'C', 8);
3713 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003714
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003715 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003716
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003717 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003718 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003719 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003720 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003721 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003722 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003723 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003724
3725 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003726}
3727
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003728template<typename Vector>
3729static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3730 ASTWriter::RecordData &Record) {
3731 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3732 I != E; ++I) {
3733 Writer.AddDeclRef(*I, Record);
3734 }
3735}
3736
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003737void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003738 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003739 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003740 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003741 using namespace llvm;
3742
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003743 bool isModule = WritingModule != 0;
3744
Douglas Gregorecc2c092011-12-01 22:20:10 +00003745 // Make sure that the AST reader knows to finalize itself.
3746 if (Chain)
3747 Chain->finalizeForWriting();
3748
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003749 ASTContext &Context = SemaRef.Context;
3750 Preprocessor &PP = SemaRef.PP;
3751
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003752 // Set up predefined declaration IDs.
3753 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003754 if (Context.ObjCIdDecl)
3755 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003756 if (Context.ObjCSelDecl)
3757 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003758 if (Context.ObjCClassDecl)
3759 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003760 if (Context.ObjCProtocolClassDecl)
3761 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003762 if (Context.Int128Decl)
3763 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3764 if (Context.UInt128Decl)
3765 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003766 if (Context.ObjCInstanceTypeDecl)
3767 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003768 if (Context.BuiltinVaListDecl)
3769 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3770
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003771 if (!Chain) {
3772 // Make sure that we emit IdentifierInfos (and any attached
3773 // declarations) for builtins. We don't need to do this when we're
3774 // emitting chained PCH files, because all of the builtins will be
3775 // in the original PCH file.
3776 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003777 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003778 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003779 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003780 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003781 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3782 getIdentifierRef(&Table.get(BuiltinNames[I]));
3783 }
3784
Douglas Gregoreee242f2011-10-27 09:33:13 +00003785 // If there are any out-of-date identifiers, bring them up to date.
3786 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003787 // Find out-of-date identifiers.
3788 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003789 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3790 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003791 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003792 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003793 OutOfDate.push_back(ID->second);
3794 }
3795
3796 // Update the out-of-date identifiers.
3797 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3798 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3799 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003800 }
3801
Chris Lattner63d65f82009-09-08 18:19:27 +00003802 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003803 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003804 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003805 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003806 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003807
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003808 // Build a record containing all of the file scoped decls in this file.
3809 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003810 if (!isModule)
3811 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3812 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003813
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003814 // Build a record containing all of the delegating constructors we still need
3815 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003816 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003817 if (!isModule)
3818 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003819
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003820 // Write the set of weak, undeclared identifiers. We always write the
3821 // entire table, since later PCH files in a PCH chain are only interested in
3822 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003823 RecordData WeakUndeclaredIdentifiers;
3824 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003825 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003826 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3827 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3828 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3829 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3830 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3831 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3832 }
3833 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003834
Richard Smith5ea6ef42013-01-10 23:43:47 +00003835 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003836 // declarations in this header file. Generally, this record will be
3837 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003838 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003839 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003840 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003841 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003842 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3843 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003844 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003845 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003846 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003847 }
3848
Douglas Gregorb81c1702009-04-27 20:06:05 +00003849 // Build a record containing all of the ext_vector declarations.
3850 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003851 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003852
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003853 // Build a record containing all of the VTable uses information.
3854 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003855 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003856 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3857 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3858 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3859 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3860 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003861 }
3862
3863 // Build a record containing all of dynamic classes declarations.
3864 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003865 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003866
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003867 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003868 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003869 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003870 I = SemaRef.PendingInstantiations.begin(),
3871 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3872 AddDeclRef(I->first, PendingInstantiations);
3873 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003874 }
3875 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3876 "There are local ones at end of translation unit!");
3877
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003878 // Build a record containing some declaration references.
3879 RecordData SemaDeclRefs;
3880 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3881 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3882 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3883 }
3884
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003885 RecordData CUDASpecialDeclRefs;
3886 if (Context.getcudaConfigureCallDecl()) {
3887 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3888 }
3889
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003890 // Build a record containing all of the known namespaces.
3891 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003892 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003893 I = SemaRef.KnownNamespaces.begin(),
3894 IEnd = SemaRef.KnownNamespaces.end();
3895 I != IEnd; ++I) {
3896 if (!I->second)
3897 AddDeclRef(I->first, KnownNamespaces);
3898 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003899
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003900 // Build a record of all used, undefined objects that require definitions.
3901 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003902
3903 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003904 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003905 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3906 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003907 AddDeclRef(I->first, UndefinedButUsed);
3908 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003909 }
3910
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003911 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003912 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003913
Sebastian Redl3397c552010-08-18 23:56:27 +00003914 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003915 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003916 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003917
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003918 // This is so that older clang versions, before the introduction
3919 // of the control block, can read and reject the newer PCH format.
3920 Record.clear();
3921 Record.push_back(VERSION_MAJOR);
3922 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3923
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003924 // Create a lexical update block containing all of the declarations in the
3925 // translation unit that do not come from other AST files.
3926 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3927 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3928 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3929 E = TU->noload_decls_end();
3930 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003931 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003932 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003933 }
3934
3935 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3936 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3937 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3938 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3939 Record.clear();
3940 Record.push_back(TU_UPDATE_LEXICAL);
3941 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3942 data(NewGlobalDecls));
3943
3944 // And a visible updates block for the translation unit.
3945 Abv = new llvm::BitCodeAbbrev();
3946 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3947 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3948 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3949 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3950 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3951 WriteDeclContextVisibleUpdate(TU);
3952
3953 // If the translation unit has an anonymous namespace, and we don't already
3954 // have an update block for it, write it as an update block.
3955 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3956 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3957 if (Record.empty()) {
3958 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003959 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003960 }
3961 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003962
3963 // Make sure visible decls, added to DeclContexts previously loaded from
3964 // an AST file, are registered for serialization.
3965 for (SmallVector<const Decl *, 16>::iterator
3966 I = UpdatingVisibleDecls.begin(),
3967 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3968 GetDeclRef(*I);
3969 }
3970
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003971 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003972 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003973
Douglas Gregora119da02011-08-02 16:26:37 +00003974 // Form the record of special types.
3975 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003976 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003977 AddTypeRef(Context.getFILEType(), SpecialTypes);
3978 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3979 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3980 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3981 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003982 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003983 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003984
Douglas Gregor366809a2009-04-26 03:49:13 +00003985 // Keep writing types and declarations until all types and
3986 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003987 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003988 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003989 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3990 E = DeclsToRewrite.end();
3991 I != E; ++I)
3992 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003993 while (!DeclTypesToEmit.empty()) {
3994 DeclOrType DOT = DeclTypesToEmit.front();
3995 DeclTypesToEmit.pop();
3996 if (DOT.isType())
3997 WriteType(DOT.getType());
3998 else
3999 WriteDecl(Context, DOT.getDecl());
4000 }
4001 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004002
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004003 DoneWritingDeclsAndTypes = true;
4004
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004005 WriteFileDeclIDsMap();
4006 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004007 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004008
4009 if (Chain) {
4010 // Write the mapping information describing our module dependencies and how
4011 // each of those modules were mapped into our own offset/ID space, so that
4012 // the reader can build the appropriate mapping to its own offset/ID space.
4013 // The map consists solely of a blob with the following format:
4014 // *(module-name-len:i16 module-name:len*i8
4015 // source-location-offset:i32
4016 // identifier-id:i32
4017 // preprocessed-entity-id:i32
4018 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004019 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004020 // selector-id:i32
4021 // declaration-id:i32
4022 // c++-base-specifiers-id:i32
4023 // type-id:i32)
4024 //
4025 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4026 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4028 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004029 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004030 {
4031 llvm::raw_svector_ostream Out(Buffer);
4032 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004033 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004034 M != MEnd; ++M) {
4035 StringRef FileName = (*M)->FileName;
4036 io::Emit16(Out, FileName.size());
4037 Out.write(FileName.data(), FileName.size());
4038 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4039 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004040 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004041 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004042 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004043 io::Emit32(Out, (*M)->BaseSelectorID);
4044 io::Emit32(Out, (*M)->BaseDeclID);
4045 io::Emit32(Out, (*M)->BaseTypeIndex);
4046 }
4047 }
4048 Record.clear();
4049 Record.push_back(MODULE_OFFSET_MAP);
4050 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4051 Buffer.data(), Buffer.size());
4052 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004053 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004054 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004055 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004056 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004057 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004058 WriteFPPragmaOptions(SemaRef.getFPOptions());
4059 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004060
Sebastian Redl1476ed42010-07-16 16:36:56 +00004061 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004062 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004063
Anders Carlssonc8505782011-03-06 18:41:18 +00004064 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004065
Douglas Gregore209e502011-12-06 01:10:29 +00004066 // If we're emitting a module, write out the submodule information.
4067 if (WritingModule)
4068 WriteSubmodules(WritingModule);
4069
Douglas Gregora119da02011-08-02 16:26:37 +00004070 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4071
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004072 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004073 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004074 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004075
4076 // Write the record containing tentative definitions.
4077 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004078 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004079
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004080 // Write the record containing unused file scoped decls.
4081 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004082 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004083
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004084 // Write the record containing weak undeclared identifiers.
4085 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004086 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004087 WeakUndeclaredIdentifiers);
4088
Richard Smith5ea6ef42013-01-10 23:43:47 +00004089 // Write the record containing locally-scoped extern "C" definitions.
4090 if (!LocallyScopedExternCDecls.empty())
4091 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4092 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004093
4094 // Write the record containing ext_vector type names.
4095 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004096 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004097
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004098 // Write the record containing VTable uses information.
4099 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004100 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004101
4102 // Write the record containing dynamic classes declarations.
4103 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004104 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004105
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004106 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004107 if (!PendingInstantiations.empty())
4108 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004109
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004110 // Write the record containing declaration references of Sema.
4111 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004112 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004113
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004114 // Write the record containing CUDA-specific declaration references.
4115 if (!CUDASpecialDeclRefs.empty())
4116 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004117
4118 // Write the delegating constructors.
4119 if (!DelegatingCtorDecls.empty())
4120 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004121
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004122 // Write the known namespaces.
4123 if (!KnownNamespaces.empty())
4124 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004125
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004126 // Write the undefined internal functions and variables, and inline functions.
4127 if (!UndefinedButUsed.empty())
4128 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004129
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004130 // Write the visible updates to DeclContexts.
4131 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4132 I = UpdatedDeclContexts.begin(),
4133 E = UpdatedDeclContexts.end();
4134 I != E; ++I)
4135 WriteDeclContextVisibleUpdate(*I);
4136
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004137 if (!WritingModule) {
4138 // Write the submodules that were imported, if any.
4139 RecordData ImportedModules;
4140 for (ASTContext::import_iterator I = Context.local_import_begin(),
4141 IEnd = Context.local_import_end();
4142 I != IEnd; ++I) {
4143 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4144 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4145 }
4146 if (!ImportedModules.empty()) {
4147 // Sort module IDs.
4148 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4149
4150 // Unique module IDs.
4151 ImportedModules.erase(std::unique(ImportedModules.begin(),
4152 ImportedModules.end()),
4153 ImportedModules.end());
4154
4155 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4156 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004157 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004158
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004159 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004160 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004161 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004162 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004163 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00004164
Douglas Gregor3e1af842009-04-17 22:13:46 +00004165 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004166 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004167 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004168 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004169 Record.push_back(NumLexicalDeclContexts);
4170 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004171 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004172 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004173}
4174
Douglas Gregor61c5e342011-09-17 00:05:03 +00004175/// \brief Go through the declaration update blocks and resolve declaration
4176/// pointers into declaration IDs.
4177void ASTWriter::ResolveDeclUpdatesBlocks() {
4178 for (DeclUpdateMap::iterator
4179 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4180 const Decl *D = I->first;
4181 UpdateRecord &URec = I->second;
4182
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004183 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004184 continue; // The decl will be written completely
4185
4186 unsigned Idx = 0, N = URec.size();
4187 while (Idx < N) {
4188 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004189 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4190 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4191 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4192 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4193 ++Idx;
4194 break;
4195
4196 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4197 ++Idx;
4198 break;
4199 }
4200 }
4201 }
4202}
4203
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004204void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004205 if (DeclUpdates.empty())
4206 return;
4207
4208 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004209 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004210 for (DeclUpdateMap::iterator
4211 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4212 const Decl *D = I->first;
4213 UpdateRecord &URec = I->second;
4214
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004215 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004216 continue; // The decl will be written completely,no need to store updates.
4217
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004218 uint64_t Offset = Stream.GetCurrentBitNo();
4219 Stream.EmitRecord(DECL_UPDATES, URec);
4220
4221 OffsetsRecord.push_back(GetDeclRef(D));
4222 OffsetsRecord.push_back(Offset);
4223 }
4224 Stream.ExitBlock();
4225 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4226}
4227
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004228void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004229 if (ReplacedDecls.empty())
4230 return;
4231
4232 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004233 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004234 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004235 Record.push_back(I->ID);
4236 Record.push_back(I->Offset);
4237 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004238 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004239 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004240}
4241
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004242void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004243 Record.push_back(Loc.getRawEncoding());
4244}
4245
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004246void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004247 AddSourceLocation(Range.getBegin(), Record);
4248 AddSourceLocation(Range.getEnd(), Record);
4249}
4250
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004251void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004252 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004253 const uint64_t *Words = Value.getRawData();
4254 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004255}
4256
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004257void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004258 Record.push_back(Value.isUnsigned());
4259 AddAPInt(Value, Record);
4260}
4261
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004262void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004263 AddAPInt(Value.bitcastToAPInt(), Record);
4264}
4265
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004266void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004267 Record.push_back(getIdentifierRef(II));
4268}
4269
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004270IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004271 if (II == 0)
4272 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004273
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004274 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004275 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004276 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004277 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004278}
4279
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004280MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004281 // Don't emit builtin macros like __LINE__ to the AST file unless they
4282 // have been redefined by the header (in which case they are not
4283 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004284 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004285 return 0;
4286
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004287 MacroID &ID = MacroIDs[MI];
4288 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004289 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004290 MacroInfoToEmitData Info = { Name, MI, ID };
4291 MacroInfosToEmit.push_back(Info);
4292 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004293 return ID;
4294}
4295
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004296MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4297 if (MI == 0 || MI->isBuiltinMacro())
4298 return 0;
4299
4300 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4301 return MacroIDs[MI];
4302}
4303
4304uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4305 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4306 return IdentMacroDirectivesOffsetMap[Name];
4307}
4308
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004309void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004310 Record.push_back(getSelectorRef(SelRef));
4311}
4312
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004313SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004314 if (Sel.getAsOpaquePtr() == 0) {
4315 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004316 }
4317
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004318 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004319 if (SID == 0 && Chain) {
4320 // This might trigger a ReadSelector callback, which will set the ID for
4321 // this selector.
4322 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004323 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004324 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004325 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004326 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004327 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004328 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004329 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004330}
4331
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004332void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004333 AddDeclRef(Temp->getDestructor(), Record);
4334}
4335
Douglas Gregor7c789c12010-10-29 22:39:52 +00004336void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4337 CXXBaseSpecifier const *BasesEnd,
4338 RecordDataImpl &Record) {
4339 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4340 CXXBaseSpecifiersToWrite.push_back(
4341 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4342 Bases, BasesEnd));
4343 Record.push_back(NextCXXBaseSpecifiersID++);
4344}
4345
Sebastian Redla4232eb2010-08-18 23:56:21 +00004346void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004347 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004348 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004349 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004350 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004351 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004352 break;
4353 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004354 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004355 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004356 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004357 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004358 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004359 break;
4360 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004361 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004362 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004363 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004364 break;
John McCall833ca992009-10-29 08:12:44 +00004365 case TemplateArgument::Null:
4366 case TemplateArgument::Integral:
4367 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004368 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004369 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004370 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004371 break;
4372 }
4373}
4374
Sebastian Redla4232eb2010-08-18 23:56:21 +00004375void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004376 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004377 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004378
4379 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4380 bool InfoHasSameExpr
4381 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4382 Record.push_back(InfoHasSameExpr);
4383 if (InfoHasSameExpr)
4384 return; // Avoid storing the same expr twice.
4385 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004386 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4387 Record);
4388}
4389
Douglas Gregordc355712011-02-25 00:36:19 +00004390void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4391 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004392 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004393 AddTypeRef(QualType(), Record);
4394 return;
4395 }
4396
Douglas Gregordc355712011-02-25 00:36:19 +00004397 AddTypeLoc(TInfo->getTypeLoc(), Record);
4398}
4399
4400void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4401 AddTypeRef(TL.getType(), Record);
4402
John McCalla1ee0c52009-10-16 21:56:05 +00004403 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004404 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004405 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004406}
4407
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004408void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004409 Record.push_back(GetOrCreateTypeID(T));
4410}
4411
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004412TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4413 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004414 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4415}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004416
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004417TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004418 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004419 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004420}
4421
4422TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4423 if (T.isNull())
4424 return TypeIdx();
4425 assert(!T.getLocalFastQualifiers());
4426
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004427 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004428 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004429 if (DoneWritingDeclsAndTypes) {
4430 assert(0 && "New type seen after serializing all the types to emit!");
4431 return TypeIdx();
4432 }
4433
Douglas Gregor366809a2009-04-26 03:49:13 +00004434 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004435 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004436 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004437 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004438 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004439 return Idx;
4440}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004441
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004442TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004443 if (T.isNull())
4444 return TypeIdx();
4445 assert(!T.getLocalFastQualifiers());
4446
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004447 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4448 assert(I != TypeIdxs.end() && "Type not emitted!");
4449 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004450}
4451
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004452void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004453 Record.push_back(GetDeclRef(D));
4454}
4455
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004456DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004457 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4458
Douglas Gregor2cf26342009-04-09 22:27:44 +00004459 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004460 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004461 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004462
4463 // If D comes from an AST file, its declaration ID is already known and
4464 // fixed.
4465 if (D->isFromASTFile())
4466 return D->getGlobalID();
4467
Douglas Gregor97475832010-10-05 18:37:06 +00004468 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004469 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004470 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004471 if (DoneWritingDeclsAndTypes) {
4472 assert(0 && "New decl seen after serializing all the decls to emit!");
4473 return 0;
4474 }
4475
Douglas Gregor2cf26342009-04-09 22:27:44 +00004476 // We haven't seen this declaration before. Give it a new ID and
4477 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004478 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004479 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004480 }
4481
Sebastian Redl681d7232010-07-27 00:17:23 +00004482 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004483}
4484
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004485DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004486 if (D == 0)
4487 return 0;
4488
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004489 // If D comes from an AST file, its declaration ID is already known and
4490 // fixed.
4491 if (D->isFromASTFile())
4492 return D->getGlobalID();
4493
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004494 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4495 return DeclIDs[D];
4496}
4497
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004498static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4499 std::pair<unsigned, serialization::DeclID> R) {
4500 return L.first < R.first;
4501}
4502
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004503void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004504 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004505 assert(D);
4506
4507 SourceLocation Loc = D->getLocation();
4508 if (Loc.isInvalid())
4509 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004510
4511 // We only keep track of the file-level declarations of each file.
4512 if (!D->getLexicalDeclContext()->isFileContext())
4513 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004514 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4515 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004516 if (isa<ParmVarDecl>(D))
4517 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004518
4519 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004520 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004521 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004522 FileID FID;
4523 unsigned Offset;
4524 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004525 if (FID.isInvalid())
4526 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004527 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004528
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004529 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004530 if (!Info)
4531 Info = new DeclIDInFileInfo();
4532
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004533 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004534 LocDeclIDsTy &Decls = Info->DeclIDs;
4535
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004536 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004537 Decls.push_back(LocDecl);
4538 return;
4539 }
4540
4541 LocDeclIDsTy::iterator
4542 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4543
4544 Decls.insert(I, LocDecl);
4545}
4546
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004547void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004548 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004549 Record.push_back(Name.getNameKind());
4550 switch (Name.getNameKind()) {
4551 case DeclarationName::Identifier:
4552 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4553 break;
4554
4555 case DeclarationName::ObjCZeroArgSelector:
4556 case DeclarationName::ObjCOneArgSelector:
4557 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004558 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004559 break;
4560
4561 case DeclarationName::CXXConstructorName:
4562 case DeclarationName::CXXDestructorName:
4563 case DeclarationName::CXXConversionFunctionName:
4564 AddTypeRef(Name.getCXXNameType(), Record);
4565 break;
4566
4567 case DeclarationName::CXXOperatorName:
4568 Record.push_back(Name.getCXXOverloadedOperator());
4569 break;
4570
Sean Hunt3e518bd2009-11-29 07:34:05 +00004571 case DeclarationName::CXXLiteralOperatorName:
4572 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4573 break;
4574
Douglas Gregor2cf26342009-04-09 22:27:44 +00004575 case DeclarationName::CXXUsingDirective:
4576 // No extra data to emit
4577 break;
4578 }
4579}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004580
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004581void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004582 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004583 switch (Name.getNameKind()) {
4584 case DeclarationName::CXXConstructorName:
4585 case DeclarationName::CXXDestructorName:
4586 case DeclarationName::CXXConversionFunctionName:
4587 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4588 break;
4589
4590 case DeclarationName::CXXOperatorName:
4591 AddSourceLocation(
4592 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4593 Record);
4594 AddSourceLocation(
4595 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4596 Record);
4597 break;
4598
4599 case DeclarationName::CXXLiteralOperatorName:
4600 AddSourceLocation(
4601 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4602 Record);
4603 break;
4604
4605 case DeclarationName::Identifier:
4606 case DeclarationName::ObjCZeroArgSelector:
4607 case DeclarationName::ObjCOneArgSelector:
4608 case DeclarationName::ObjCMultiArgSelector:
4609 case DeclarationName::CXXUsingDirective:
4610 break;
4611 }
4612}
4613
4614void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004615 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004616 AddDeclarationName(NameInfo.getName(), Record);
4617 AddSourceLocation(NameInfo.getLoc(), Record);
4618 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4619}
4620
4621void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004622 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004623 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004624 Record.push_back(Info.NumTemplParamLists);
4625 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4626 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4627}
4628
Sebastian Redla4232eb2010-08-18 23:56:21 +00004629void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004630 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004631 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004632 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004633 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004634
4635 // Push each of the NNS's onto a stack for serialization in reverse order.
4636 while (NNS) {
4637 NestedNames.push_back(NNS);
4638 NNS = NNS->getPrefix();
4639 }
4640
4641 Record.push_back(NestedNames.size());
4642 while(!NestedNames.empty()) {
4643 NNS = NestedNames.pop_back_val();
4644 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4645 Record.push_back(Kind);
4646 switch (Kind) {
4647 case NestedNameSpecifier::Identifier:
4648 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4649 break;
4650
4651 case NestedNameSpecifier::Namespace:
4652 AddDeclRef(NNS->getAsNamespace(), Record);
4653 break;
4654
Douglas Gregor14aba762011-02-24 02:36:08 +00004655 case NestedNameSpecifier::NamespaceAlias:
4656 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4657 break;
4658
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004659 case NestedNameSpecifier::TypeSpec:
4660 case NestedNameSpecifier::TypeSpecWithTemplate:
4661 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4662 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4663 break;
4664
4665 case NestedNameSpecifier::Global:
4666 // Don't need to write an associated value.
4667 break;
4668 }
4669 }
4670}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004671
Douglas Gregordc355712011-02-25 00:36:19 +00004672void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4673 RecordDataImpl &Record) {
4674 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004675 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004676 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004677
4678 // Push each of the nested-name-specifiers's onto a stack for
4679 // serialization in reverse order.
4680 while (NNS) {
4681 NestedNames.push_back(NNS);
4682 NNS = NNS.getPrefix();
4683 }
4684
4685 Record.push_back(NestedNames.size());
4686 while(!NestedNames.empty()) {
4687 NNS = NestedNames.pop_back_val();
4688 NestedNameSpecifier::SpecifierKind Kind
4689 = NNS.getNestedNameSpecifier()->getKind();
4690 Record.push_back(Kind);
4691 switch (Kind) {
4692 case NestedNameSpecifier::Identifier:
4693 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4694 AddSourceRange(NNS.getLocalSourceRange(), Record);
4695 break;
4696
4697 case NestedNameSpecifier::Namespace:
4698 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4699 AddSourceRange(NNS.getLocalSourceRange(), Record);
4700 break;
4701
4702 case NestedNameSpecifier::NamespaceAlias:
4703 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4704 AddSourceRange(NNS.getLocalSourceRange(), Record);
4705 break;
4706
4707 case NestedNameSpecifier::TypeSpec:
4708 case NestedNameSpecifier::TypeSpecWithTemplate:
4709 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4710 AddTypeLoc(NNS.getTypeLoc(), Record);
4711 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4712 break;
4713
4714 case NestedNameSpecifier::Global:
4715 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4716 break;
4717 }
4718 }
4719}
4720
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004721void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004722 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004723 Record.push_back(Kind);
4724 switch (Kind) {
4725 case TemplateName::Template:
4726 AddDeclRef(Name.getAsTemplateDecl(), Record);
4727 break;
4728
4729 case TemplateName::OverloadedTemplate: {
4730 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4731 Record.push_back(OvT->size());
4732 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4733 I != E; ++I)
4734 AddDeclRef(*I, Record);
4735 break;
4736 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004737
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004738 case TemplateName::QualifiedTemplate: {
4739 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4740 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4741 Record.push_back(QualT->hasTemplateKeyword());
4742 AddDeclRef(QualT->getTemplateDecl(), Record);
4743 break;
4744 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004745
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004746 case TemplateName::DependentTemplate: {
4747 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4748 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4749 Record.push_back(DepT->isIdentifier());
4750 if (DepT->isIdentifier())
4751 AddIdentifierRef(DepT->getIdentifier(), Record);
4752 else
4753 Record.push_back(DepT->getOperator());
4754 break;
4755 }
John McCall14606042011-06-30 08:33:18 +00004756
4757 case TemplateName::SubstTemplateTemplateParm: {
4758 SubstTemplateTemplateParmStorage *subst
4759 = Name.getAsSubstTemplateTemplateParm();
4760 AddDeclRef(subst->getParameter(), Record);
4761 AddTemplateName(subst->getReplacement(), Record);
4762 break;
4763 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004764
4765 case TemplateName::SubstTemplateTemplateParmPack: {
4766 SubstTemplateTemplateParmPackStorage *SubstPack
4767 = Name.getAsSubstTemplateTemplateParmPack();
4768 AddDeclRef(SubstPack->getParameterPack(), Record);
4769 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4770 break;
4771 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004772 }
4773}
4774
Michael J. Spencer20249a12010-10-21 03:16:25 +00004775void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004776 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004777 Record.push_back(Arg.getKind());
4778 switch (Arg.getKind()) {
4779 case TemplateArgument::Null:
4780 break;
4781 case TemplateArgument::Type:
4782 AddTypeRef(Arg.getAsType(), Record);
4783 break;
4784 case TemplateArgument::Declaration:
4785 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004786 Record.push_back(Arg.isDeclForReferenceParam());
4787 break;
4788 case TemplateArgument::NullPtr:
4789 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004790 break;
4791 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004792 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004793 AddTypeRef(Arg.getIntegralType(), Record);
4794 break;
4795 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004796 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4797 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004798 case TemplateArgument::TemplateExpansion:
4799 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004800 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004801 Record.push_back(*NumExpansions + 1);
4802 else
4803 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004804 break;
4805 case TemplateArgument::Expression:
4806 AddStmt(Arg.getAsExpr());
4807 break;
4808 case TemplateArgument::Pack:
4809 Record.push_back(Arg.pack_size());
4810 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4811 I != E; ++I)
4812 AddTemplateArgument(*I, Record);
4813 break;
4814 }
4815}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004816
4817void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004818ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004819 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004820 assert(TemplateParams && "No TemplateParams!");
4821 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4822 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4823 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4824 Record.push_back(TemplateParams->size());
4825 for (TemplateParameterList::const_iterator
4826 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4827 P != PEnd; ++P)
4828 AddDeclRef(*P, Record);
4829}
4830
4831/// \brief Emit a template argument list.
4832void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004833ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004834 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004835 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004836 Record.push_back(TemplateArgs->size());
4837 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004838 AddTemplateArgument(TemplateArgs->get(i), Record);
4839}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004840
4841
4842void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004843ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004844 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004845 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004846 I = Set.begin(), E = Set.end(); I != E; ++I) {
4847 AddDeclRef(I.getDecl(), Record);
4848 Record.push_back(I.getAccess());
4849 }
4850}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004851
Sebastian Redla4232eb2010-08-18 23:56:21 +00004852void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004853 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004854 Record.push_back(Base.isVirtual());
4855 Record.push_back(Base.isBaseOfClass());
4856 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004857 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004858 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004859 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004860 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4861 : SourceLocation(),
4862 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004863}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004864
Douglas Gregor7c789c12010-10-29 22:39:52 +00004865void ASTWriter::FlushCXXBaseSpecifiers() {
4866 RecordData Record;
4867 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4868 Record.clear();
4869
4870 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004871 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004872 if (Index == CXXBaseSpecifiersOffsets.size())
4873 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4874 else {
4875 if (Index > CXXBaseSpecifiersOffsets.size())
4876 CXXBaseSpecifiersOffsets.resize(Index + 1);
4877 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4878 }
4879
4880 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4881 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4882 Record.push_back(BEnd - B);
4883 for (; B != BEnd; ++B)
4884 AddCXXBaseSpecifier(*B, Record);
4885 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004886
4887 // Flush any expressions that were written as part of the base specifiers.
4888 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004889 }
4890
4891 CXXBaseSpecifiersToWrite.clear();
4892}
4893
Sean Huntcbb67482011-01-08 20:30:50 +00004894void ASTWriter::AddCXXCtorInitializers(
4895 const CXXCtorInitializer * const *CtorInitializers,
4896 unsigned NumCtorInitializers,
4897 RecordDataImpl &Record) {
4898 Record.push_back(NumCtorInitializers);
4899 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4900 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004901
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004902 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004903 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004904 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004905 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004906 } else if (Init->isDelegatingInitializer()) {
4907 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004908 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004909 } else if (Init->isMemberInitializer()){
4910 Record.push_back(CTOR_INITIALIZER_MEMBER);
4911 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004912 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004913 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4914 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004915 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004916
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004917 AddSourceLocation(Init->getMemberLocation(), Record);
4918 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004919 AddSourceLocation(Init->getLParenLoc(), Record);
4920 AddSourceLocation(Init->getRParenLoc(), Record);
4921 Record.push_back(Init->isWritten());
4922 if (Init->isWritten()) {
4923 Record.push_back(Init->getSourceOrder());
4924 } else {
4925 Record.push_back(Init->getNumArrayIndices());
4926 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4927 AddDeclRef(Init->getArrayIndex(i), Record);
4928 }
4929 }
4930}
4931
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004932void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4933 assert(D->DefinitionData);
4934 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004935 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004936 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004937 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004938 Record.push_back(Data.Aggregate);
4939 Record.push_back(Data.PlainOldData);
4940 Record.push_back(Data.Empty);
4941 Record.push_back(Data.Polymorphic);
4942 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004943 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004944 Record.push_back(Data.HasNoNonEmptyBases);
4945 Record.push_back(Data.HasPrivateFields);
4946 Record.push_back(Data.HasProtectedFields);
4947 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004948 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004949 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004950 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004951 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004952 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4953 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4954 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4955 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4956 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4957 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004958 Record.push_back(Data.HasTrivialSpecialMembers);
4959 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004960 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004961 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004962 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004963 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004964 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004965 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004966 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00004967 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
4968 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
4969 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
4970 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00004971 Record.push_back(Data.FailedImplicitMoveConstructor);
4972 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004973 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004974
4975 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004976 if (Data.NumBases > 0)
4977 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4978 Record);
4979
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004980 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4981 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004982 if (Data.NumVBases > 0)
4983 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4984 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004985
4986 AddUnresolvedSet(Data.Conversions, Record);
4987 AddUnresolvedSet(Data.VisibleConversions, Record);
4988 // Data.Definition is the owning decl, no need to write it.
4989 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004990
4991 // Add lambda-specific data.
4992 if (Data.IsLambda) {
4993 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004994 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004995 Record.push_back(Lambda.NumCaptures);
4996 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004997 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004998 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004999 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005000 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5001 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5002 AddSourceLocation(Capture.getLocation(), Record);
5003 Record.push_back(Capture.isImplicit());
5004 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
5005 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
5006 AddDeclRef(Var, Record);
5007 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
5008 : SourceLocation(),
5009 Record);
5010 }
5011 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005012}
5013
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005014void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005015 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005016 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005017 assert(FirstDeclID == NextDeclID &&
5018 FirstTypeID == NextTypeID &&
5019 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005020 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005021 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005022 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005023 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005024
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005025 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005026
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005027 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5028 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5029 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005030 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005031 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005032 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005033 NextDeclID = FirstDeclID;
5034 NextTypeID = FirstTypeID;
5035 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005036 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005037 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005038 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005039}
5040
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005041void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005042 // Always keep the highest ID. See \p TypeRead() for more information.
5043 IdentID &StoredID = IdentifierIDs[II];
5044 if (ID > StoredID)
5045 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005046}
5047
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005048void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005049 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005050 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005051 if (ID > StoredID)
5052 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005053}
5054
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005055void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005056 // Always take the highest-numbered type index. This copes with an interesting
5057 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005058 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005059 // keep the higher-numbered entry so that we can properly write it out to
5060 // the AST file.
5061 TypeIdx &StoredIdx = TypeIdxs[T];
5062 if (Idx.getIndex() >= StoredIdx.getIndex())
5063 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005064}
5065
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005066void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005067 // Always keep the highest ID. See \p TypeRead() for more information.
5068 SelectorID &StoredID = SelectorIDs[S];
5069 if (ID > StoredID)
5070 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005071}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005072
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005073void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005074 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005075 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005076 MacroDefinitions[MD] = ID;
5077}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005078
Douglas Gregora015cab2011-12-02 17:30:13 +00005079void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5080 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5081 SubmoduleIDs[Mod] = ID;
5082}
5083
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005084void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005085 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005086 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005087 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5088 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005089 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005090 // A forward reference was mutated into a definition. Rewrite it.
5091 // FIXME: This happens during template instantiation, should we
5092 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005093 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005094 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005095 }
5096}
Douglas Gregora8235d62012-10-09 23:05:51 +00005097
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005098void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005099 assert(!WritingAST && "Already writing the AST!");
5100
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005101 // TU and namespaces are handled elsewhere.
5102 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5103 return;
5104
Douglas Gregor919814d2011-09-09 23:01:35 +00005105 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005106 return; // Not a source decl added to a DeclContext from PCH.
5107
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005108 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005109 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005110 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005111}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005112
5113void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005114 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005115 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005116 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005117 return; // Not a source member added to a class from PCH.
5118 if (!isa<CXXMethodDecl>(D))
5119 return; // We are interested in lazily declared implicit methods.
5120
5121 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005122 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005123 UpdateRecord &Record = DeclUpdates[RD];
5124 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005125 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005126}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005127
5128void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5129 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005130 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005131 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005132 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005133 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005134 return; // Not a source specialization added to a template from PCH.
5135
5136 UpdateRecord &Record = DeclUpdates[TD];
5137 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005138 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005139}
Douglas Gregor89d99802010-11-30 06:16:57 +00005140
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005141void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5142 const FunctionDecl *D) {
5143 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005144 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005145 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005146 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005147 return; // Not a source specialization added to a template from PCH.
5148
5149 UpdateRecord &Record = DeclUpdates[TD];
5150 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005151 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005152}
5153
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005154void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005155 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005156 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005157 return; // Declaration not imported from PCH.
5158
5159 // Implicit decl from a PCH was defined.
5160 // FIXME: Should implicit definition be a separate FunctionDecl?
5161 RewriteDecl(D);
5162}
5163
Sebastian Redlf79a7192011-04-29 08:19:30 +00005164void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005165 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005166 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005167 return;
5168
5169 // Since the actual instantiation is delayed, this really means that we need
5170 // to update the instantiation location.
5171 UpdateRecord &Record = DeclUpdates[D];
5172 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5173 AddSourceLocation(
5174 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5175}
5176
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005177void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5178 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005179 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005180 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005181 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005182
5183 assert(IFD->getDefinition() && "Category on a class without a definition?");
5184 ObjCClassesWithCategories.insert(
5185 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005186}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005187
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005188
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005189void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5190 const ObjCPropertyDecl *OrigProp,
5191 const ObjCCategoryDecl *ClassExt) {
5192 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5193 if (!D)
5194 return;
5195
5196 assert(!WritingAST && "Already writing the AST!");
5197 if (!D->isFromASTFile())
5198 return; // Declaration not imported from PCH.
5199
5200 RewriteDecl(D);
5201}