blob: e98708d8f5ed8e0a38663cba9717025a6ee51567 [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);
John McCall76da55d2013-04-16 07:28:30 +0000910 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000911 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000912 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000913 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000914 RECORD(DECL_FILE_SCOPE_ASM);
915 RECORD(DECL_BLOCK);
916 RECORD(DECL_CONTEXT_LEXICAL);
917 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000918 RECORD(DECL_NAMESPACE);
919 RECORD(DECL_NAMESPACE_ALIAS);
920 RECORD(DECL_USING);
921 RECORD(DECL_USING_SHADOW);
922 RECORD(DECL_USING_DIRECTIVE);
923 RECORD(DECL_UNRESOLVED_USING_VALUE);
924 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
925 RECORD(DECL_LINKAGE_SPEC);
926 RECORD(DECL_CXX_RECORD);
927 RECORD(DECL_CXX_METHOD);
928 RECORD(DECL_CXX_CONSTRUCTOR);
929 RECORD(DECL_CXX_DESTRUCTOR);
930 RECORD(DECL_CXX_CONVERSION);
931 RECORD(DECL_ACCESS_SPEC);
932 RECORD(DECL_FRIEND);
933 RECORD(DECL_FRIEND_TEMPLATE);
934 RECORD(DECL_CLASS_TEMPLATE);
935 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
936 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
937 RECORD(DECL_FUNCTION_TEMPLATE);
938 RECORD(DECL_TEMPLATE_TYPE_PARM);
939 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
940 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
941 RECORD(DECL_STATIC_ASSERT);
942 RECORD(DECL_CXX_BASE_SPECIFIERS);
943 RECORD(DECL_INDIRECTFIELD);
944 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
945
Douglas Gregora72d8c42011-06-03 02:27:19 +0000946 // Statements and Exprs can occur in the Decls and Types block.
947 AddStmtsExprs(Stream, Record);
948
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000949 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000950 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000951 RECORD(PPD_MACRO_DEFINITION);
952 RECORD(PPD_INCLUSION_DIRECTIVE);
953
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000954#undef RECORD
955#undef BLOCK
956 Stream.ExitBlock();
957}
958
Douglas Gregore650c8c2009-07-07 00:12:59 +0000959/// \brief Adjusts the given filename to only write out the portion of the
960/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000961///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962/// \param Filename the file name to adjust.
963///
964/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
965/// the returned filename will be adjusted by this system root.
966///
967/// \returns either the original filename (if it needs no adjustment) or the
968/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000969static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000970adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregor832d6202011-07-22 16:35:34 +0000973 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregore650c8c2009-07-07 00:12:59 +0000976 // Verify that the filename and the system root have the same prefix.
977 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000978 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 if (Filename[Pos] != isysroot[Pos])
980 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Douglas Gregore650c8c2009-07-07 00:12:59 +0000982 // We hit the end of the filename before we hit the end of the system root.
983 if (!Filename[Pos])
984 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Douglas Gregore650c8c2009-07-07 00:12:59 +0000986 // If the file name has a '/' at the current position, skip over the '/'.
987 // We distinguish sysroot-based includes from absolute includes by the
988 // absence of '/' at the beginning of sysroot-based includes.
989 if (Filename[Pos] == '/')
990 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregore650c8c2009-07-07 00:12:59 +0000992 return Filename + Pos;
993}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000994
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000995/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000996void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
997 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000998 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000999 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001000 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1001 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001002
Douglas Gregore650c8c2009-07-07 00:12:59 +00001003 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001004 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1011 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1012 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1013 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1014 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001015 Record.push_back(VERSION_MAJOR);
1016 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001017 Record.push_back(CLANG_VERSION_MAJOR);
1018 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001019 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001020 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001021 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1022 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001023
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001024 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001025 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001026 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001027 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001028 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001029
1030 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1031 M != MEnd; ++M) {
1032 // Skip modules that weren't directly imported.
1033 if (!(*M)->isDirectlyImported())
1034 continue;
1035
1036 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001037 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001038 Record.push_back((*M)->File->getSize());
1039 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001040 // FIXME: This writes the absolute path for AST files we depend on.
1041 const std::string &FileName = (*M)->FileName;
1042 Record.push_back(FileName.size());
1043 Record.append(FileName.begin(), FileName.end());
1044 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001045 Stream.EmitRecord(IMPORTS, Record);
1046 }
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001048 // Language options.
1049 Record.clear();
1050 const LangOptions &LangOpts = Context.getLangOpts();
1051#define LANGOPT(Name, Bits, Default, Description) \
1052 Record.push_back(LangOpts.Name);
1053#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1054 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1055#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001056#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1057#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001058
1059 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1060 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1061
1062 Record.push_back(LangOpts.CurrentModule.size());
1063 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001064
1065 // Comment options.
1066 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1067 for (CommentOptions::BlockCommandNamesTy::const_iterator
1068 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1069 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1070 I != IEnd; ++I) {
1071 AddString(*I, Record);
1072 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001073 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001074
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001075 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1076
Douglas Gregoree097c12012-10-18 17:58:09 +00001077 // Target options.
1078 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001079 const TargetInfo &Target = Context.getTargetInfo();
1080 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001081 AddString(TargetOpts.Triple, Record);
1082 AddString(TargetOpts.CPU, Record);
1083 AddString(TargetOpts.ABI, Record);
1084 AddString(TargetOpts.CXXABI, Record);
1085 AddString(TargetOpts.LinkerVersion, Record);
1086 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1087 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1088 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1089 }
1090 Record.push_back(TargetOpts.Features.size());
1091 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1092 AddString(TargetOpts.Features[I], Record);
1093 }
1094 Stream.EmitRecord(TARGET_OPTIONS, Record);
1095
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001096 // Diagnostic options.
1097 Record.clear();
1098 const DiagnosticOptions &DiagOpts
1099 = Context.getDiagnostics().getDiagnosticOptions();
1100#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1101#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1102 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1103#include "clang/Basic/DiagnosticOptions.def"
1104 Record.push_back(DiagOpts.Warnings.size());
1105 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1106 AddString(DiagOpts.Warnings[I], Record);
1107 // Note: we don't serialize the log or serialization file names, because they
1108 // are generally transient files and will almost always be overridden.
1109 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1110
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001111 // File system options.
1112 Record.clear();
1113 const FileSystemOptions &FSOpts
1114 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1115 AddString(FSOpts.WorkingDir, Record);
1116 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1117
Douglas Gregorbbf38312012-10-24 16:50:34 +00001118 // Header search options.
1119 Record.clear();
1120 const HeaderSearchOptions &HSOpts
1121 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1122 AddString(HSOpts.Sysroot, Record);
1123
1124 // Include entries.
1125 Record.push_back(HSOpts.UserEntries.size());
1126 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1127 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1128 AddString(Entry.Path, Record);
1129 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001130 Record.push_back(Entry.IsFramework);
1131 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001132 }
1133
1134 // System header prefixes.
1135 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1136 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1137 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1138 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1139 }
1140
1141 AddString(HSOpts.ResourceDir, Record);
1142 AddString(HSOpts.ModuleCachePath, Record);
1143 Record.push_back(HSOpts.DisableModuleHash);
1144 Record.push_back(HSOpts.UseBuiltinIncludes);
1145 Record.push_back(HSOpts.UseStandardSystemIncludes);
1146 Record.push_back(HSOpts.UseStandardCXXIncludes);
1147 Record.push_back(HSOpts.UseLibcxx);
1148 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1149
Douglas Gregora71a7d82012-10-24 20:05:57 +00001150 // Preprocessor options.
1151 Record.clear();
1152 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1153
1154 // Macro definitions.
1155 Record.push_back(PPOpts.Macros.size());
1156 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1157 AddString(PPOpts.Macros[I].first, Record);
1158 Record.push_back(PPOpts.Macros[I].second);
1159 }
1160
1161 // Includes
1162 Record.push_back(PPOpts.Includes.size());
1163 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1164 AddString(PPOpts.Includes[I], Record);
1165
1166 // Macro includes
1167 Record.push_back(PPOpts.MacroIncludes.size());
1168 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1169 AddString(PPOpts.MacroIncludes[I], Record);
1170
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001171 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001172 AddString(PPOpts.ImplicitPCHInclude, Record);
1173 AddString(PPOpts.ImplicitPTHInclude, Record);
1174 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1175 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1176
Douglas Gregor31d375f2011-05-06 21:43:30 +00001177 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001178 SourceManager &SM = Context.getSourceManager();
1179 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1180 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001181 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1182 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001183 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1184 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1185
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001186 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001188 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001189
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001190 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001191 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001192 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001193 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001194 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001195 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001196 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001197 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001198
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001199 Record.clear();
1200 Record.push_back(SM.getMainFileID().getOpaqueValue());
1201 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1202
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001203 // Original PCH directory
1204 if (!OutputFile.empty() && OutputFile != "-") {
1205 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1206 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1208 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1209
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001210 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001211
1212 llvm::sys::fs::make_absolute(OutputPath);
1213 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1214
1215 RecordData Record;
1216 Record.push_back(ORIGINAL_PCH_DIR);
1217 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1218 }
1219
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001220 WriteInputFiles(Context.SourceMgr,
1221 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1222 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001223 Stream.ExitBlock();
1224}
1225
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001226namespace {
1227 /// \brief An input file.
1228 struct InputFileEntry {
1229 const FileEntry *File;
1230 bool IsSystemFile;
1231 bool BufferOverridden;
1232 };
1233}
1234
1235void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1236 HeaderSearchOptions &HSOpts,
1237 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001238 using namespace llvm;
1239 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1240 RecordData Record;
1241
1242 // Create input-file abbreviation.
1243 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1244 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001245 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001246 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1247 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001248 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001249 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1250 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1251
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001252 // Get all ContentCache objects for files, sorted by whether the file is a
1253 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001254 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001255 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1256 // Get this source location entry.
1257 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001258 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001259
1260 // We only care about file entries that were not overridden.
1261 if (!SLoc->isFile())
1262 continue;
1263 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001264 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001265 continue;
1266
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001267 InputFileEntry Entry;
1268 Entry.File = Cache->OrigEntry;
1269 Entry.IsSystemFile = Cache->IsSystemFile;
1270 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001271 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001272 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001273 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001274 SortedFiles.push_front(Entry);
1275 }
1276
1277 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1278 // the set of (non-system) input files. This is simple heuristic for
1279 // detecting whether the system headers may have changed, because it is too
1280 // expensive to stat() all of the system headers.
1281 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001282 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001283 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1284 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1285 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1286 InputFileEntry Entry = { SDKSettingsFile, false, false };
1287 SortedFiles.push_front(Entry);
1288 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001289 }
1290
1291 unsigned UserFilesNum = 0;
1292 // Write out all of the input files.
1293 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001294 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001295 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001296 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001297
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001298 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001299 if (InputFileID != 0)
1300 continue; // already recorded this file.
1301
Douglas Gregora930dc92012-10-22 18:42:04 +00001302 // Record this entry's offset.
1303 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001304
1305 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001306
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001307 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001308 ++UserFilesNum;
1309
Douglas Gregor745e6f12012-10-19 00:38:02 +00001310 Record.clear();
1311 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001312 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001313
1314 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001315 Record.push_back(Entry.File->getSize());
1316 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001317
Douglas Gregora930dc92012-10-22 18:42:04 +00001318 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001319 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001320
Douglas Gregor745e6f12012-10-19 00:38:02 +00001321 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001322 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001323 SmallString<128> FilePath(Filename);
1324
1325 // Ask the file manager to fixup the relative path for us. This will
1326 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001327 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001328
1329 // FIXME: This call to make_absolute shouldn't be necessary, the
1330 // call to FixupRelativePath should always return an absolute path.
1331 llvm::sys::fs::make_absolute(FilePath);
1332 Filename = FilePath.c_str();
1333
1334 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1335
1336 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1337 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001338
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001339 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001340
1341 // Create input file offsets abbreviation.
1342 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1343 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1344 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001345 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1346 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001347 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1348 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1349
1350 // Write input file offsets.
1351 Record.clear();
1352 Record.push_back(INPUT_FILE_OFFSETS);
1353 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001354 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001355 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001356}
1357
Douglas Gregor14f79002009-04-10 03:52:48 +00001358//===----------------------------------------------------------------------===//
1359// Source Manager Serialization
1360//===----------------------------------------------------------------------===//
1361
1362/// \brief Create an abbreviation for the SLocEntry that refers to a
1363/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001364static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001365 using namespace llvm;
1366 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001367 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001372 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001377 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001378}
1379
1380/// \brief Create an abbreviation for the SLocEntry that refers to a
1381/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001382static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001383 using namespace llvm;
1384 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001385 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001391 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001392}
1393
1394/// \brief Create an abbreviation for the SLocEntry that refers to a
1395/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001396static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001397 using namespace llvm;
1398 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001399 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001401 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001402}
1403
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001404/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1405/// expansion.
1406static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001407 using namespace llvm;
1408 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001409 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001415 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001416}
1417
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001418namespace {
1419 // Trait used for the on-disk hash table of header search information.
1420 class HeaderFileInfoTrait {
1421 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001422 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001423
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001424 // Keep track of the framework names we've used during serialization.
1425 SmallVector<char, 128> FrameworkStringData;
1426 llvm::StringMap<unsigned> FrameworkNameOffset;
1427
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001428 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001429 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1430 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001431
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001432 struct key_type {
1433 const FileEntry *FE;
1434 const char *Filename;
1435 };
1436 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001437
1438 typedef HeaderFileInfo data_type;
1439 typedef const data_type &data_type_ref;
1440
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001441 static unsigned ComputeHash(key_type_ref key) {
1442 // The hash is based only on size/time of the file, so that the reader can
1443 // match even when symlinking or excess path elements ("foo/../", "../")
1444 // change the form of the name. However, complete path is still the key.
1445 return llvm::hash_combine(key.FE->getSize(),
1446 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001447 }
1448
1449 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001450 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1451 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1452 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001453 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001454 if (Data.isModuleHeader)
1455 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001456 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001457 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001458 }
1459
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001460 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1461 clang::io::Emit64(Out, key.FE->getSize());
1462 KeyLen -= 8;
1463 clang::io::Emit64(Out, key.FE->getModificationTime());
1464 KeyLen -= 8;
1465 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001466 }
1467
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001468 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001469 data_type_ref Data, unsigned DataLen) {
1470 using namespace clang::io;
1471 uint64_t Start = Out.tell(); (void)Start;
1472
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001473 unsigned char Flags = (Data.isImport << 5)
1474 | (Data.isPragmaOnce << 4)
1475 | (Data.DirInfo << 2)
1476 | (Data.Resolved << 1)
1477 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001478 Emit8(Out, (uint8_t)Flags);
1479 Emit16(Out, (uint16_t) Data.NumIncludes);
1480
1481 if (!Data.ControllingMacro)
1482 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1483 else
1484 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001485
1486 unsigned Offset = 0;
1487 if (!Data.Framework.empty()) {
1488 // If this header refers into a framework, save the framework name.
1489 llvm::StringMap<unsigned>::iterator Pos
1490 = FrameworkNameOffset.find(Data.Framework);
1491 if (Pos == FrameworkNameOffset.end()) {
1492 Offset = FrameworkStringData.size() + 1;
1493 FrameworkStringData.append(Data.Framework.begin(),
1494 Data.Framework.end());
1495 FrameworkStringData.push_back(0);
1496
1497 FrameworkNameOffset[Data.Framework] = Offset;
1498 } else
1499 Offset = Pos->second;
1500 }
1501 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001502
1503 if (Data.isModuleHeader) {
1504 Module *Mod = HS.findModuleForHeader(key.FE);
1505 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1506 }
1507
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001508 assert(Out.tell() - Start == DataLen && "Wrong data length");
1509 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001510
1511 const char *strings_begin() const { return FrameworkStringData.begin(); }
1512 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001513 };
1514} // end anonymous namespace
1515
1516/// \brief Write the header search block for the list of files that
1517///
1518/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001519void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001520 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001521 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1522
1523 if (FilesByUID.size() > HS.header_file_size())
1524 FilesByUID.resize(HS.header_file_size());
1525
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001526 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001527 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001528 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001529 unsigned NumHeaderSearchEntries = 0;
1530 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1531 const FileEntry *File = FilesByUID[UID];
1532 if (!File)
1533 continue;
1534
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001535 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1536 // from the external source if it was not provided already.
1537 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001538 if (HFI.External && Chain)
1539 continue;
1540
1541 // Turn the file name into an absolute path, if it isn't already.
1542 const char *Filename = File->getName();
1543 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1544
1545 // If we performed any translation on the file name at all, we need to
1546 // save this string, since the generator will refer to it later.
1547 if (Filename != File->getName()) {
1548 Filename = strdup(Filename);
1549 SavedStrings.push_back(Filename);
1550 }
1551
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001552 HeaderFileInfoTrait::key_type key = { File, Filename };
1553 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001554 ++NumHeaderSearchEntries;
1555 }
1556
1557 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001558 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001559 uint32_t BucketOffset;
1560 {
1561 llvm::raw_svector_ostream Out(TableData);
1562 // Make sure that no bucket is at offset 0
1563 clang::io::Emit32(Out, 0);
1564 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1565 }
1566
1567 // Create a blob abbreviation
1568 using namespace llvm;
1569 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1570 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1575 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1576
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001577 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001578 RecordData Record;
1579 Record.push_back(HEADER_SEARCH_TABLE);
1580 Record.push_back(BucketOffset);
1581 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001582 Record.push_back(TableData.size());
1583 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001584 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1585
1586 // Free all of the strings we had to duplicate.
1587 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001588 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001589}
1590
Douglas Gregor14f79002009-04-10 03:52:48 +00001591/// \brief Writes the block containing the serialized form of the
1592/// source manager.
1593///
1594/// TODO: We should probably use an on-disk hash table (stored in a
1595/// blob), indexed based on the file name, so that we only create
1596/// entries for files that we actually need. In the common case (no
1597/// errors), we probably won't have to create file entries for any of
1598/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001599void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001600 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001601 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001602 RecordData Record;
1603
Chris Lattnerf04ad692009-04-10 17:16:57 +00001604 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001605 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001606
1607 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001608 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1609 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1610 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001611 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001612
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001613 // Write out the source location entry table. We skip the first
1614 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001615 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001616 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001617 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1618 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001619 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001620 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001621 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001622 FileID FID = FileID::get(I);
1623 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001624
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001625 // Record the offset of this source-location entry.
1626 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1627
1628 // Figure out which record code to use.
1629 unsigned Code;
1630 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001631 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1632 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001633 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001634 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001635 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001636 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001637 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001638 Record.clear();
1639 Record.push_back(Code);
1640
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001641 // Starting offset of this entry within this module, so skip the dummy.
1642 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001643 if (SLoc->isFile()) {
1644 const SrcMgr::FileInfo &File = SLoc->getFile();
1645 Record.push_back(File.getIncludeLoc().getRawEncoding());
1646 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1647 Record.push_back(File.hasLineDirectives());
1648
1649 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001650 if (Content->OrigEntry) {
1651 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001652 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001653
Douglas Gregora930dc92012-10-22 18:42:04 +00001654 // The source location entry is a file. Emit input file ID.
1655 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1656 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001658 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001659
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001660 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001661 if (FDI != FileDeclIDs.end()) {
1662 Record.push_back(FDI->second->FirstDeclIndex);
1663 Record.push_back(FDI->second->DeclIDs.size());
1664 } else {
1665 Record.push_back(0);
1666 Record.push_back(0);
1667 }
Douglas Gregora081da52011-11-16 20:05:18 +00001668
Douglas Gregora930dc92012-10-22 18:42:04 +00001669 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001670
1671 if (Content->BufferOverridden) {
1672 Record.clear();
1673 Record.push_back(SM_SLOC_BUFFER_BLOB);
1674 const llvm::MemoryBuffer *Buffer
1675 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1676 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1677 StringRef(Buffer->getBufferStart(),
1678 Buffer->getBufferSize() + 1));
1679 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001680 } else {
1681 // The source location entry is a buffer. The blob associated
1682 // with this entry contains the contents of the buffer.
1683
1684 // We add one to the size so that we capture the trailing NULL
1685 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1686 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001687 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001688 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001689 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001690 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001691 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001692 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001693 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001694 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001695 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001696 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001697
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001698 if (strcmp(Name, "<built-in>") == 0) {
1699 PreloadSLocs.push_back(SLocEntryOffsets.size());
1700 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001701 }
1702 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001703 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001704 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001705 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1706 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001707 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1708 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001709
1710 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001711 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001712 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001713 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001714 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001715 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001716 }
1717 }
1718
Douglas Gregorc9490c02009-04-16 22:23:12 +00001719 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001720
1721 if (SLocEntryOffsets.empty())
1722 return;
1723
Sebastian Redl3397c552010-08-18 23:56:27 +00001724 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001725 // table is used for lazily loading source-location information.
1726 using namespace llvm;
1727 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001728 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001731 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1732 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001734 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001735 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001736 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001737 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001738 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001739
Sebastian Redl3397c552010-08-18 23:56:27 +00001740 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001741 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001742 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001743
1744 // Write the line table. It depends on remapping working, so it must come
1745 // after the source location offsets.
1746 if (SourceMgr.hasLineTable()) {
1747 LineTableInfo &LineTable = SourceMgr.getLineTable();
1748
1749 Record.clear();
1750 // Emit the file names
1751 Record.push_back(LineTable.getNumFilenames());
1752 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1753 // Emit the file name
1754 const char *Filename = LineTable.getFilename(I);
1755 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1756 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1757 Record.push_back(FilenameLen);
1758 if (FilenameLen)
1759 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1760 }
1761
1762 // Emit the line entries
1763 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1764 L != LEnd; ++L) {
1765 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001766 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001767 continue;
1768
1769 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001770 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001771
1772 // Emit the line entries
1773 Record.push_back(L->second.size());
1774 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1775 LEEnd = L->second.end();
1776 LE != LEEnd; ++LE) {
1777 Record.push_back(LE->FileOffset);
1778 Record.push_back(LE->LineNo);
1779 Record.push_back(LE->FilenameID);
1780 Record.push_back((unsigned)LE->FileKind);
1781 Record.push_back(LE->IncludeOffset);
1782 }
1783 }
1784 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1785 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001786}
1787
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001788//===----------------------------------------------------------------------===//
1789// Preprocessor Serialization
1790//===----------------------------------------------------------------------===//
1791
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001792namespace {
1793class ASTMacroTableTrait {
1794public:
1795 typedef IdentID key_type;
1796 typedef key_type key_type_ref;
1797
1798 struct Data {
1799 uint32_t MacroDirectivesOffset;
1800 };
1801
1802 typedef Data data_type;
1803 typedef const data_type &data_type_ref;
1804
1805 static unsigned ComputeHash(IdentID IdID) {
1806 return llvm::hash_value(IdID);
1807 }
1808
1809 std::pair<unsigned,unsigned>
1810 static EmitKeyDataLength(raw_ostream& Out,
1811 key_type_ref Key, data_type_ref Data) {
1812 unsigned KeyLen = 4; // IdentID.
1813 unsigned DataLen = 4; // MacroDirectivesOffset.
1814 return std::make_pair(KeyLen, DataLen);
1815 }
1816
1817 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1818 clang::io::Emit32(Out, Key);
1819 }
1820
1821 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1822 unsigned) {
1823 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1824 }
1825};
1826} // end anonymous namespace
1827
1828static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1829 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1830 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1831 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1832 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001833 return X.first->getName().compare(Y.first->getName());
1834}
1835
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001836static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1837 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001838 if (MacroInfo *MI = MD->getMacroInfo())
1839 if (MI->isBuiltinMacro())
1840 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001841
1842 if (IsModule) {
1843 SourceLocation Loc = MD->getLocation();
1844 if (Loc.isInvalid())
1845 return true;
1846 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1847 return true;
1848 }
1849
1850 return false;
1851}
1852
Chris Lattner0b1fb982009-04-10 17:15:23 +00001853/// \brief Writes the block containing the serialized form of the
1854/// preprocessor.
1855///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001856void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001857 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1858 if (PPRec)
1859 WritePreprocessorDetail(*PPRec);
1860
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001861 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001862
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001863 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1864 if (PP.getCounterValue() != 0) {
1865 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001866 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001867 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001868 }
1869
1870 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001871 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Sebastian Redl3397c552010-08-18 23:56:27 +00001873 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001874 // FIXME: use diagnostics subsystem for localization etc.
1875 if (PP.SawDateOrTime())
1876 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Douglas Gregorecdcb882010-10-20 22:00:55 +00001878
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001879 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001880 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001881
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001882 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001883 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001884 MacroDirectives;
1885 for (Preprocessor::macro_iterator
1886 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1887 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001888 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001889 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001890 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001891
Douglas Gregor9c736102011-02-10 18:20:09 +00001892 // Sort the set of macro definitions that need to be serialized by the
1893 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001894 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1895 &compareMacroDirectives);
1896
1897 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1898
1899 // Emit the macro directives as a list and associate the offset with the
1900 // identifier they belong to.
1901 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1902 const IdentifierInfo *Name = MacroDirectives[I].first;
1903 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1904 MacroDirective *MD = MacroDirectives[I].second;
1905
1906 // If the macro or identifier need no updates, don't write the macro history
1907 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001908 // FIXME: Chain the macro history instead of re-writing it.
1909 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001910 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1911 continue;
1912
1913 // Emit the macro directives in reverse source order.
1914 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001915 if (MD->isHidden())
1916 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001917 if (shouldIgnoreMacro(MD, IsModule, PP))
1918 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001919
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001920 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001921 Record.push_back(MD->getKind());
1922 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1923 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1924 Record.push_back(InfoID);
1925 Record.push_back(DefMD->isImported());
1926 Record.push_back(DefMD->isAmbiguous());
1927
1928 } else if (VisibilityMacroDirective *
1929 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1930 Record.push_back(VisMD->isPublic());
1931 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001932 }
1933 if (Record.empty())
1934 continue;
1935
1936 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1937 Record.clear();
1938
1939 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1940
1941 IdentID NameID = getIdentifierRef(Name);
1942 ASTMacroTableTrait::Data data;
1943 data.MacroDirectivesOffset = MacroDirectiveOffset;
1944 Generator.insert(NameID, data);
1945 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001946
Douglas Gregora8235d62012-10-09 23:05:51 +00001947 /// \brief Offsets of each of the macros into the bitstream, indexed by
1948 /// the local macro ID
1949 ///
1950 /// For each identifier that is associated with a macro, this map
1951 /// provides the offset into the bitstream where that macro is
1952 /// defined.
1953 std::vector<uint32_t> MacroOffsets;
1954
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001955 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1956 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1957 MacroInfo *MI = MacroInfosToEmit[I].MI;
1958 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001959
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001960 if (ID < FirstMacroID) {
1961 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1962 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001963 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001964
1965 // Record the local offset of this macro.
1966 unsigned Index = ID - FirstMacroID;
1967 if (Index == MacroOffsets.size())
1968 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1969 else {
1970 if (Index > MacroOffsets.size())
1971 MacroOffsets.resize(Index + 1);
1972
1973 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1974 }
1975
1976 AddIdentifierRef(Name, Record);
1977 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1978 AddSourceLocation(MI->getDefinitionLoc(), Record);
1979 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1980 Record.push_back(MI->isUsed());
1981 unsigned Code;
1982 if (MI->isObjectLike()) {
1983 Code = PP_MACRO_OBJECT_LIKE;
1984 } else {
1985 Code = PP_MACRO_FUNCTION_LIKE;
1986
1987 Record.push_back(MI->isC99Varargs());
1988 Record.push_back(MI->isGNUVarargs());
1989 Record.push_back(MI->hasCommaPasting());
1990 Record.push_back(MI->getNumArgs());
1991 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1992 I != E; ++I)
1993 AddIdentifierRef(*I, Record);
1994 }
1995
1996 // If we have a detailed preprocessing record, record the macro definition
1997 // ID that corresponds to this macro.
1998 if (PPRec)
1999 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2000
2001 Stream.EmitRecord(Code, Record);
2002 Record.clear();
2003
2004 // Emit the tokens array.
2005 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2006 // Note that we know that the preprocessor does not have any annotation
2007 // tokens in it because they are created by the parser, and thus can't
2008 // be in a macro definition.
2009 const Token &Tok = MI->getReplacementToken(TokNo);
2010
2011 Record.push_back(Tok.getLocation().getRawEncoding());
2012 Record.push_back(Tok.getLength());
2013
2014 // FIXME: When reading literal tokens, reconstruct the literal pointer
2015 // if it is needed.
2016 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
2017 // FIXME: Should translate token kind to a stable encoding.
2018 Record.push_back(Tok.getKind());
2019 // FIXME: Should translate token flags to a stable encoding.
2020 Record.push_back(Tok.getFlags());
2021
2022 Stream.EmitRecord(PP_TOKEN, Record);
2023 Record.clear();
2024 }
2025 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002026 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002027
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002028 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002029
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002030 // Create the on-disk hash table in a buffer.
2031 SmallString<4096> MacroTable;
2032 uint32_t BucketOffset;
2033 {
2034 llvm::raw_svector_ostream Out(MacroTable);
2035 // Make sure that no bucket is at offset 0
2036 clang::io::Emit32(Out, 0);
2037 BucketOffset = Generator.Emit(Out);
2038 }
2039
2040 // Write the macro table
2041 using namespace llvm;
2042 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2043 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2046 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2047
2048 Record.push_back(MACRO_TABLE);
2049 Record.push_back(BucketOffset);
2050 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2051 Record.clear();
2052
Douglas Gregora8235d62012-10-09 23:05:51 +00002053 // Write the offsets table for macro IDs.
2054 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002055 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002056 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2060
2061 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2062 Record.clear();
2063 Record.push_back(MACRO_OFFSET);
2064 Record.push_back(MacroOffsets.size());
2065 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2066 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2067 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002068}
2069
2070void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002071 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002072 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002073
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002074 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002075
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002076 // Enter the preprocessor block.
2077 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002078
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002079 // If the preprocessor has a preprocessing record, emit it.
2080 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002081 using namespace llvm;
2082
2083 // Set up the abbreviation for
2084 unsigned InclusionAbbrev = 0;
2085 {
2086 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2087 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002092 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2093 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2094 }
2095
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002096 unsigned FirstPreprocessorEntityID
2097 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2098 + NUM_PREDEF_PP_ENTITY_IDS;
2099 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002100 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002101 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2102 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002103 E != EEnd;
2104 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002105 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002106
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002107 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2108 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002109
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002110 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002111 // Record this macro definition's ID.
2112 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002113
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002114 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002115 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2116 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002117 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002118
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002119 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002120 Record.push_back(ME->isBuiltinMacro());
2121 if (ME->isBuiltinMacro())
2122 AddIdentifierRef(ME->getName(), Record);
2123 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002124 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002125 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002126 continue;
2127 }
2128
2129 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2130 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002131 Record.push_back(ID->getFileName().size());
2132 Record.push_back(ID->wasInQuotes());
2133 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002134 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002135 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002136 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002137 // Check that the FileEntry is not null because it was not resolved and
2138 // we create a PCH even with compiler errors.
2139 if (ID->getFile())
2140 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002141 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2142 continue;
2143 }
2144
2145 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2146 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002147 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002148
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002149 // Write the offsets table for the preprocessing record.
2150 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002151 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2152
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002153 // Write the offsets table for identifier IDs.
2154 using namespace llvm;
2155 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002156 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002158 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002159 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002160
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002161 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002162 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002163 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002164 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2165 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002166 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002167}
2168
Douglas Gregore209e502011-12-06 01:10:29 +00002169unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2170 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2171 if (Known != SubmoduleIDs.end())
2172 return Known->second;
2173
2174 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2175}
2176
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002177unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2178 if (!Mod)
2179 return 0;
2180
2181 llvm::DenseMap<Module *, unsigned>::const_iterator
2182 Known = SubmoduleIDs.find(Mod);
2183 if (Known != SubmoduleIDs.end())
2184 return Known->second;
2185
2186 return 0;
2187}
2188
Douglas Gregor26ced122011-12-01 00:59:36 +00002189/// \brief Compute the number of modules within the given tree (including the
2190/// given module).
2191static unsigned getNumberOfModules(Module *Mod) {
2192 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002193 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2194 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002195 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002196 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002197
2198 return ChildModules + 1;
2199}
2200
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002201void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002202 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002203 // FIXME: This feels like it belongs somewhere else, but there are no
2204 // other consumers of this information.
2205 SourceManager &SrcMgr = PP->getSourceManager();
2206 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2207 for (ASTContext::import_iterator I = Context->local_import_begin(),
2208 IEnd = Context->local_import_end();
2209 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002210 if (Module *ImportedFrom
2211 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2212 SrcMgr))) {
2213 ImportedFrom->Imports.push_back(I->getImportedModule());
2214 }
2215 }
2216
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002217 // Enter the submodule description block.
2218 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2219
2220 // Write the abbreviations needed for the submodules block.
2221 using namespace llvm;
2222 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2223 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2234 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2235
2236 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002237 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2239 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2240
2241 Abbrev = new BitCodeAbbrev();
2242 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2244 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002245
2246 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002247 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2249 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2250
2251 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002252 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2254 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2255
Douglas Gregor51f564f2011-12-31 04:05:44 +00002256 Abbrev = new BitCodeAbbrev();
2257 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2259 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2260
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002261 Abbrev = new BitCodeAbbrev();
2262 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2264 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2265
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002266 Abbrev = new BitCodeAbbrev();
2267 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2270 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2271
Douglas Gregor63a72682013-03-20 00:22:05 +00002272 Abbrev = new BitCodeAbbrev();
2273 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2275 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2276
Douglas Gregor906d66a2013-03-20 21:10:35 +00002277 Abbrev = new BitCodeAbbrev();
2278 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2281 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2282
Douglas Gregor26ced122011-12-01 00:59:36 +00002283 // Write the submodule metadata block.
2284 RecordData Record;
2285 Record.push_back(getNumberOfModules(WritingModule));
2286 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2287 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2288
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002289 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002290 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002291 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002292 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002293 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002294 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002295 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002296
2297 // Emit the definition of the block.
2298 Record.clear();
2299 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002300 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002301 if (Mod->Parent) {
2302 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2303 Record.push_back(SubmoduleIDs[Mod->Parent]);
2304 } else {
2305 Record.push_back(0);
2306 }
2307 Record.push_back(Mod->IsFramework);
2308 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002309 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002310 Record.push_back(Mod->InferSubmodules);
2311 Record.push_back(Mod->InferExplicitSubmodules);
2312 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002313 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002314 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2315
Douglas Gregor51f564f2011-12-31 04:05:44 +00002316 // Emit the requirements.
2317 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2318 Record.clear();
2319 Record.push_back(SUBMODULE_REQUIRES);
2320 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2321 Mod->Requires[I].data(),
2322 Mod->Requires[I].size());
2323 }
2324
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002325 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002326 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002327 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002328 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002329 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002330 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002331 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2332 Record.clear();
2333 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2334 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2335 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002336 }
2337
2338 // Emit the headers.
2339 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2340 Record.clear();
2341 Record.push_back(SUBMODULE_HEADER);
2342 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2343 Mod->Headers[I]->getName());
2344 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002345 // Emit the excluded headers.
2346 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2347 Record.clear();
2348 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2349 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2350 Mod->ExcludedHeaders[I]->getName());
2351 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002352 ArrayRef<const FileEntry *>
2353 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2354 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002355 Record.clear();
2356 Record.push_back(SUBMODULE_TOPHEADER);
2357 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002358 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002359 }
Douglas Gregor55988682011-12-05 16:33:54 +00002360
2361 // Emit the imports.
2362 if (!Mod->Imports.empty()) {
2363 Record.clear();
2364 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002365 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002366 assert(ImportedID && "Unknown submodule!");
2367 Record.push_back(ImportedID);
2368 }
2369 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2370 }
2371
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002372 // Emit the exports.
2373 if (!Mod->Exports.empty()) {
2374 Record.clear();
2375 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002376 if (Module *Exported = Mod->Exports[I].getPointer()) {
2377 unsigned ExportedID = SubmoduleIDs[Exported];
2378 assert(ExportedID > 0 && "Unknown submodule ID?");
2379 Record.push_back(ExportedID);
2380 } else {
2381 Record.push_back(0);
2382 }
2383
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002384 Record.push_back(Mod->Exports[I].getInt());
2385 }
2386 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2387 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002388
2389 // Emit the link libraries.
2390 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2391 Record.clear();
2392 Record.push_back(SUBMODULE_LINK_LIBRARY);
2393 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2394 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2395 Mod->LinkLibraries[I].Library);
2396 }
2397
Douglas Gregor906d66a2013-03-20 21:10:35 +00002398 // Emit the conflicts.
2399 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2400 Record.clear();
2401 Record.push_back(SUBMODULE_CONFLICT);
2402 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2403 assert(OtherID && "Unknown submodule!");
2404 Record.push_back(OtherID);
2405 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2406 Mod->Conflicts[I].Message);
2407 }
2408
Douglas Gregor63a72682013-03-20 00:22:05 +00002409 // Emit the configuration macros.
2410 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2411 Record.clear();
2412 Record.push_back(SUBMODULE_CONFIG_MACRO);
2413 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2414 Mod->ConfigMacros[I]);
2415 }
2416
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002417 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002418 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2419 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002420 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002421 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002422 }
2423
2424 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002425
2426 assert((NextSubmoduleID - FirstSubmoduleID
2427 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002428}
2429
Douglas Gregor185dbd72011-12-01 02:07:58 +00002430serialization::SubmoduleID
2431ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002432 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002433 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002434
2435 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002436 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002437 Module *OwningMod
2438 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002439 if (!OwningMod)
2440 return 0;
2441
Douglas Gregore209e502011-12-06 01:10:29 +00002442 // Check whether this submodule is part of our own module.
2443 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002444 return 0;
2445
Douglas Gregore209e502011-12-06 01:10:29 +00002446 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002447}
2448
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002449void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2450 bool isModule) {
2451 // Make sure set diagnostic pragmas don't affect the translation unit that
2452 // imports the module.
2453 // FIXME: Make diagnostic pragma sections work properly with modules.
2454 if (isModule)
2455 return;
2456
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002457 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2458 DiagStateIDMap;
2459 unsigned CurrID = 0;
2460 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002461 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002462 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002463 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2464 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002465 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002466 if (point.Loc.isInvalid())
2467 continue;
2468
2469 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002470 unsigned &DiagStateID = DiagStateIDMap[point.State];
2471 Record.push_back(DiagStateID);
2472
2473 if (DiagStateID == 0) {
2474 DiagStateID = ++CurrID;
2475 for (DiagnosticsEngine::DiagState::const_iterator
2476 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2477 if (I->second.isPragma()) {
2478 Record.push_back(I->first);
2479 Record.push_back(I->second.getMapping());
2480 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002481 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002482 Record.push_back(-1); // mark the end of the diag/map pairs for this
2483 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002484 }
2485 }
2486
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002487 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002488 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002489}
2490
Anders Carlssonc8505782011-03-06 18:41:18 +00002491void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2492 if (CXXBaseSpecifiersOffsets.empty())
2493 return;
2494
2495 RecordData Record;
2496
2497 // Create a blob abbreviation for the C++ base specifiers offsets.
2498 using namespace llvm;
2499
2500 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2501 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2504 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2505
Douglas Gregore92b8a12011-08-04 00:01:48 +00002506 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002507 Record.clear();
2508 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2509 Record.push_back(CXXBaseSpecifiersOffsets.size());
2510 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002511 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002512}
2513
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002514//===----------------------------------------------------------------------===//
2515// Type Serialization
2516//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002517
Sebastian Redl3397c552010-08-18 23:56:27 +00002518/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002519void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002520 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002521 if (Idx.getIndex() == 0) // we haven't seen this type before.
2522 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002523
Douglas Gregor97475832010-10-05 18:37:06 +00002524 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002525
Douglas Gregor2cf26342009-04-09 22:27:44 +00002526 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002527 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002528 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002529 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002530 else if (TypeOffsets.size() < Index) {
2531 TypeOffsets.resize(Index + 1);
2532 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002533 }
2534
2535 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002536
Douglas Gregor2cf26342009-04-09 22:27:44 +00002537 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002538 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002539
Douglas Gregora4923eb2009-11-16 21:35:15 +00002540 if (T.hasLocalNonFastQualifiers()) {
2541 Qualifiers Qs = T.getLocalQualifiers();
2542 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002543 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002544 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002545 } else {
2546 switch (T->getTypeClass()) {
2547 // For all of the concrete, non-dependent types, call the
2548 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002549#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002550 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002551#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002552#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002553 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002554 }
2555
2556 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002557 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002558
2559 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002560 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002561}
2562
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002563//===----------------------------------------------------------------------===//
2564// Declaration Serialization
2565//===----------------------------------------------------------------------===//
2566
Douglas Gregor2cf26342009-04-09 22:27:44 +00002567/// \brief Write the block containing all of the declaration IDs
2568/// lexically declared within the given DeclContext.
2569///
2570/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2571/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002572uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002573 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002574 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002575 return 0;
2576
Douglas Gregorc9490c02009-04-16 22:23:12 +00002577 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002578 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002579 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002580 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002581 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2582 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002583 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002584
Douglas Gregor25123082009-04-22 22:34:57 +00002585 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002586 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002587 return Offset;
2588}
2589
Sebastian Redla4232eb2010-08-18 23:56:21 +00002590void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002591 using namespace llvm;
2592 RecordData Record;
2593
2594 // Write the type offsets array
2595 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002596 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002597 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002599 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2600 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2601 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002602 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002603 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002604 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002605 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002606
2607 // Write the declaration offsets array
2608 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002609 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2613 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2614 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002615 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002616 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002617 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002618 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002619}
2620
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002621void ASTWriter::WriteFileDeclIDsMap() {
2622 using namespace llvm;
2623 RecordData Record;
2624
2625 // Join the vectors of DeclIDs from all files.
2626 SmallVector<DeclID, 256> FileSortedIDs;
2627 for (FileDeclIDsTy::iterator
2628 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2629 DeclIDInFileInfo &Info = *FI->second;
2630 Info.FirstDeclIndex = FileSortedIDs.size();
2631 for (LocDeclIDsTy::iterator
2632 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2633 FileSortedIDs.push_back(DI->second);
2634 }
2635
2636 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2637 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2640 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2641 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002642 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002643 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2644}
2645
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002646void ASTWriter::WriteComments() {
2647 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002648 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002649 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002650 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2651 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002652 I != E; ++I) {
2653 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002654 AddSourceRange((*I)->getSourceRange(), Record);
2655 Record.push_back((*I)->getKind());
2656 Record.push_back((*I)->isTrailingComment());
2657 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002658 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2659 }
2660 Stream.ExitBlock();
2661}
2662
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002663//===----------------------------------------------------------------------===//
2664// Global Method Pool and Selector Serialization
2665//===----------------------------------------------------------------------===//
2666
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002667namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002668// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002669class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002670 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002671
2672public:
2673 typedef Selector key_type;
2674 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002675
Sebastian Redl5d050072010-08-04 17:20:04 +00002676 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002677 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002678 ObjCMethodList Instance, Factory;
2679 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002680 typedef const data_type& data_type_ref;
2681
Sebastian Redl3397c552010-08-18 23:56:27 +00002682 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002684 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002685 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002686 }
Mike Stump1eb44332009-09-09 15:08:12 +00002687
2688 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002689 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002690 data_type_ref Methods) {
2691 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2692 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002693 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2694 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002695 Method = Method->Next)
2696 if (Method->Method)
2697 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002698 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002699 Method = Method->Next)
2700 if (Method->Method)
2701 DataLen += 4;
2702 clang::io::Emit16(Out, DataLen);
2703 return std::make_pair(KeyLen, DataLen);
2704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Chris Lattner5f9e2722011-07-23 10:55:15 +00002706 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002707 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002708 assert((Start >> 32) == 0 && "Selector key offset too large");
2709 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002710 unsigned N = Sel.getNumArgs();
2711 clang::io::Emit16(Out, N);
2712 if (N == 0)
2713 N = 1;
2714 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002715 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002716 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2717 }
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Chris Lattner5f9e2722011-07-23 10:55:15 +00002719 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002720 data_type_ref Methods, unsigned DataLen) {
2721 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002722 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002723 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002724 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002725 Method = Method->Next)
2726 if (Method->Method)
2727 ++NumInstanceMethods;
2728
2729 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002730 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002731 Method = Method->Next)
2732 if (Method->Method)
2733 ++NumFactoryMethods;
2734
2735 clang::io::Emit16(Out, NumInstanceMethods);
2736 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002737 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002738 Method = Method->Next)
2739 if (Method->Method)
2740 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002741 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002742 Method = Method->Next)
2743 if (Method->Method)
2744 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002745
2746 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002747 }
2748};
2749} // end anonymous namespace
2750
Sebastian Redl059612d2010-08-03 21:58:15 +00002751/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002752///
2753/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002754/// in an on-disk hash table indexed by the selector. The hash table also
2755/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002756void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002757 using namespace llvm;
2758
Sebastian Redl059612d2010-08-03 21:58:15 +00002759 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002760 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002761 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002762 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002763 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002764 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002765 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002766 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Sebastian Redl059612d2010-08-03 21:58:15 +00002768 // Create the on-disk hash table representation. We walk through every
2769 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002770 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002771 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002772 I = SelectorIDs.begin(), E = SelectorIDs.end();
2773 I != E; ++I) {
2774 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002775 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002776 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002777 I->second,
2778 ObjCMethodList(),
2779 ObjCMethodList()
2780 };
2781 if (F != SemaRef.MethodPool.end()) {
2782 Data.Instance = F->second.first;
2783 Data.Factory = F->second.second;
2784 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002785 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002786 // changed.
2787 if (Chain && I->second < FirstSelectorID) {
2788 // Selector already exists. Did it change?
2789 bool changed = false;
2790 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2791 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002792 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002793 changed = true;
2794 }
2795 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2796 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002797 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002798 changed = true;
2799 }
2800 if (!changed)
2801 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002802 } else if (Data.Instance.Method || Data.Factory.Method) {
2803 // A new method pool entry.
2804 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002805 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002806 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002807 }
2808
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002809 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002810 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002811 uint32_t BucketOffset;
2812 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002813 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002814 llvm::raw_svector_ostream Out(MethodPool);
2815 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002816 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002817 BucketOffset = Generator.Emit(Out, Trait);
2818 }
2819
2820 // Create a blob abbreviation
2821 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002822 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002823 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002824 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002825 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2826 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2827
Douglas Gregor83941df2009-04-25 17:48:32 +00002828 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002829 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002830 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002831 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002832 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002833 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002834
2835 // Create a blob abbreviation for the selector table offsets.
2836 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002837 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2841 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2842
2843 // Write the selector offsets table.
2844 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002845 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002846 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002847 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002848 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002849 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002850 }
2851}
2852
Sebastian Redl3397c552010-08-18 23:56:27 +00002853/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002854void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002855 using namespace llvm;
2856 if (SemaRef.ReferencedSelectors.empty())
2857 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002858
Fariborz Jahanian32019832010-07-23 19:11:11 +00002859 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002860
Sebastian Redl3397c552010-08-18 23:56:27 +00002861 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002862 // very tricky to fix, and given that @selector shouldn't really appear in
2863 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002864 for (DenseMap<Selector, SourceLocation>::iterator S =
2865 SemaRef.ReferencedSelectors.begin(),
2866 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2867 Selector Sel = (*S).first;
2868 SourceLocation Loc = (*S).second;
2869 AddSelectorRef(Sel, Record);
2870 AddSourceLocation(Loc, Record);
2871 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002872 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002873}
2874
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002875//===----------------------------------------------------------------------===//
2876// Identifier Table Serialization
2877//===----------------------------------------------------------------------===//
2878
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002879namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002880class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002881 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002882 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002883 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002884 bool IsModule;
2885
Douglas Gregora92193e2009-04-28 21:18:29 +00002886 /// \brief Determines whether this is an "interesting" identifier
2887 /// that needs a full IdentifierInfo structure written into the hash
2888 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002889 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002890 if (II->isPoisoned() ||
2891 II->isExtensionToken() ||
2892 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002893 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002894 II->getFETokenInfo<void>())
2895 return true;
2896
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002897 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002898 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002899
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002900 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002901 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002902 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002903
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002904 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2905 if (!IsModule)
2906 return !shouldIgnoreMacro(Macro, IsModule, PP);
2907 SubmoduleID ModID;
2908 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2909 return true;
2910 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002911
2912 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002913 }
2914
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002915 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2916 SubmoduleID &ModID) {
2917 ModID = 0;
2918 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2919 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2920 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002921 return 0;
2922 }
2923
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002924 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2925 SubmoduleID &ModID) {
2926 if (DefMacroDirective *
2927 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2928 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2929 return DefMD;
2930 return 0;
2931 }
2932
2933 /// \brief Traverses the macro directives history and returns the latest
2934 /// macro that is public and not undefined in the same submodule.
2935 /// A macro that is defined in submodule A and undefined in submodule B,
2936 /// will still be considered as defined/exported from submodule A.
2937 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2938 SubmoduleID &ModID) {
2939 if (!MD)
2940 return 0;
2941
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002942 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002943 bool isUndefined = false;
2944 Optional<bool> isPublic;
2945 for (; MD; MD = MD->getPrevious()) {
2946 if (MD->isHidden())
2947 continue;
2948
2949 SubmoduleID ThisModID = getSubmoduleID(MD);
2950 if (ThisModID == 0) {
2951 isUndefined = false;
2952 isPublic = Optional<bool>();
2953 continue;
2954 }
2955 if (ThisModID != ModID){
2956 ModID = ThisModID;
2957 isUndefined = false;
2958 isPublic = Optional<bool>();
2959 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002960 // We are looking for a definition in a different submodule than the one
2961 // that we started with. If a submodule has re-definitions of the same
2962 // macro, only the last definition will be used as the "exported" one.
2963 if (ModID == OrigModID)
2964 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002965
2966 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2967 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
2968 return DefMD;
2969 continue;
2970 }
2971
2972 if (isa<UndefMacroDirective>(MD)) {
2973 isUndefined = true;
2974 continue;
2975 }
2976
2977 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
2978 if (!isPublic.hasValue())
2979 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002980 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002981
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002982 return 0;
2983 }
2984
2985 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002986 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2987 MacroInfo *MI = DefMD->getInfo();
2988 if (unsigned ID = MI->getOwningModuleID())
2989 return ID;
2990 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
2991 }
2992 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002993 }
2994
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002995public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002996 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002997 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002998
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002999 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003000 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Douglas Gregoreee242f2011-10-27 09:33:13 +00003002 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3003 IdentifierResolver &IdResolver, bool IsModule)
3004 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003005
3006 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003007 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003008 }
Mike Stump1eb44332009-09-09 15:08:12 +00003009
3010 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003011 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003012 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003013 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003014 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003015 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003016 DataLen += 2; // 2 bytes for builtin ID
3017 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003018 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003019 DataLen += 4; // MacroDirectives offset.
3020 if (IsModule) {
3021 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003022 for (DefMacroDirective *
3023 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3024 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003025 DataLen += 4; // MacroInfo ID.
3026 }
3027 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003028 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003029 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003030
Douglas Gregoreee242f2011-10-27 09:33:13 +00003031 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3032 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003033 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003034 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003035 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003036 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003037 // We emit the key length after the data length so that every
3038 // string is preceded by a 16-bit length. This matches the PTH
3039 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003040 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003041 return std::make_pair(KeyLen, DataLen);
3042 }
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Chris Lattner5f9e2722011-07-23 10:55:15 +00003044 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003045 unsigned KeyLen) {
3046 // Record the location of the key data. This is used when generating
3047 // the mapping from persistent IDs to strings.
3048 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003049 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003050 }
Mike Stump1eb44332009-09-09 15:08:12 +00003051
Douglas Gregor7143aab2011-09-01 17:04:32 +00003052 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003053 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003054 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003055 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003056 clang::io::Emit32(Out, ID << 1);
3057 return;
3058 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003059
Douglas Gregora92193e2009-04-28 21:18:29 +00003060 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003061 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3062 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3063 clang::io::Emit16(Out, Bits);
3064 Bits = 0;
3065 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003066 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003067 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003068 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3069 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003070 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003071 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003072 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003073
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003074 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003075 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3076 if (IsModule) {
3077 // Write the IDs of macros coming from different submodules.
3078 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003079 for (DefMacroDirective *
3080 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3081 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3082 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003083 assert(InfoID);
3084 clang::io::Emit32(Out, InfoID);
3085 }
3086 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003087 }
Douglas Gregor13292642011-12-02 15:45:10 +00003088 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003089
Douglas Gregor668c1a42009-04-21 22:25:48 +00003090 // Emit the declaration IDs in reverse order, because the
3091 // IdentifierResolver provides the declarations as they would be
3092 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003093 // "stat"), but the ASTReader adds declarations to the end of the list
3094 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003095 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003096 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3097 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00003098 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003099 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003100 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003101 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003102 }
3103};
3104} // end anonymous namespace
3105
Sebastian Redl3397c552010-08-18 23:56:27 +00003106/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003107///
3108/// The identifier table consists of a blob containing string data
3109/// (the actual identifiers themselves) and a separate "offsets" index
3110/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003111void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3112 IdentifierResolver &IdResolver,
3113 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003114 using namespace llvm;
3115
3116 // Create and write out the blob that contains the identifier
3117 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003118 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003119 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003120 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Douglas Gregor92b059e2009-04-28 20:33:11 +00003122 // Look for any identifiers that were named while processing the
3123 // headers, but are otherwise not needed. We add these to the hash
3124 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003125 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003126 // file.
3127 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3128 IDEnd = PP.getIdentifierTable().end();
3129 ID != IDEnd; ++ID)
3130 getIdentifierRef(ID->second);
3131
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003132 // Create the on-disk hash table representation. We only store offsets
3133 // for identifiers that appear here for the first time.
3134 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003135 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003136 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3137 ID != IDEnd; ++ID) {
3138 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003139 if (!Chain || !ID->first->isFromAST() ||
3140 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003141 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003142 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003143 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003144
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003145 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003146 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003147 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003148 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003149 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003150 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003151 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003152 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003153 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003154 }
3155
3156 // Create a blob abbreviation
3157 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003158 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003161 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003162
3163 // Write the identifier table
3164 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003165 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003166 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003167 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003168 }
3169
3170 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003171 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003172 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3176 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3177
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003178#ifndef NDEBUG
3179 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3180 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3181#endif
3182
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003183 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003184 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003185 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003186 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003187 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003188 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003189}
3190
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003191//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003192// DeclContext's Name Lookup Table Serialization
3193//===----------------------------------------------------------------------===//
3194
3195namespace {
3196// Trait used for the on-disk hash table used in the method pool.
3197class ASTDeclContextNameLookupTrait {
3198 ASTWriter &Writer;
3199
3200public:
3201 typedef DeclarationName key_type;
3202 typedef key_type key_type_ref;
3203
3204 typedef DeclContext::lookup_result data_type;
3205 typedef const data_type& data_type_ref;
3206
3207 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3208
3209 unsigned ComputeHash(DeclarationName Name) {
3210 llvm::FoldingSetNodeID ID;
3211 ID.AddInteger(Name.getNameKind());
3212
3213 switch (Name.getNameKind()) {
3214 case DeclarationName::Identifier:
3215 ID.AddString(Name.getAsIdentifierInfo()->getName());
3216 break;
3217 case DeclarationName::ObjCZeroArgSelector:
3218 case DeclarationName::ObjCOneArgSelector:
3219 case DeclarationName::ObjCMultiArgSelector:
3220 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3221 break;
3222 case DeclarationName::CXXConstructorName:
3223 case DeclarationName::CXXDestructorName:
3224 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003225 break;
3226 case DeclarationName::CXXOperatorName:
3227 ID.AddInteger(Name.getCXXOverloadedOperator());
3228 break;
3229 case DeclarationName::CXXLiteralOperatorName:
3230 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3231 case DeclarationName::CXXUsingDirective:
3232 break;
3233 }
3234
3235 return ID.ComputeHash();
3236 }
3237
3238 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003239 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003240 data_type_ref Lookup) {
3241 unsigned KeyLen = 1;
3242 switch (Name.getNameKind()) {
3243 case DeclarationName::Identifier:
3244 case DeclarationName::ObjCZeroArgSelector:
3245 case DeclarationName::ObjCOneArgSelector:
3246 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003247 case DeclarationName::CXXLiteralOperatorName:
3248 KeyLen += 4;
3249 break;
3250 case DeclarationName::CXXOperatorName:
3251 KeyLen += 1;
3252 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003253 case DeclarationName::CXXConstructorName:
3254 case DeclarationName::CXXDestructorName:
3255 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003256 case DeclarationName::CXXUsingDirective:
3257 break;
3258 }
3259 clang::io::Emit16(Out, KeyLen);
3260
3261 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003262 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003263 clang::io::Emit16(Out, DataLen);
3264
3265 return std::make_pair(KeyLen, DataLen);
3266 }
3267
Chris Lattner5f9e2722011-07-23 10:55:15 +00003268 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003269 using namespace clang::io;
3270
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003271 Emit8(Out, Name.getNameKind());
3272 switch (Name.getNameKind()) {
3273 case DeclarationName::Identifier:
3274 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003275 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003276 case DeclarationName::ObjCZeroArgSelector:
3277 case DeclarationName::ObjCOneArgSelector:
3278 case DeclarationName::ObjCMultiArgSelector:
3279 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003280 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003281 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003282 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3283 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003284 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003285 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003286 case DeclarationName::CXXLiteralOperatorName:
3287 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003288 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003289 case DeclarationName::CXXConstructorName:
3290 case DeclarationName::CXXDestructorName:
3291 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003292 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003293 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003294 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003295
3296 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003297 }
3298
Chris Lattner5f9e2722011-07-23 10:55:15 +00003299 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003300 data_type Lookup, unsigned DataLen) {
3301 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003302 clang::io::Emit16(Out, Lookup.size());
3303 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3304 I != E; ++I)
3305 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003306
3307 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3308 }
3309};
3310} // end anonymous namespace
3311
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003312/// \brief Write the block containing all of the declaration IDs
3313/// visible from the given DeclContext.
3314///
3315/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003316/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003317uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3318 DeclContext *DC) {
3319 if (DC->getPrimaryContext() != DC)
3320 return 0;
3321
3322 // Since there is no name lookup into functions or methods, don't bother to
3323 // build a visible-declarations table for these entities.
3324 if (DC->isFunctionOrMethod())
3325 return 0;
3326
3327 // If not in C++, we perform name lookup for the translation unit via the
3328 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003329 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003330 return 0;
3331
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003332 // Serialize the contents of the mapping used for lookup. Note that,
3333 // although we have two very different code paths, the serialized
3334 // representation is the same for both cases: a declaration name,
3335 // followed by a size, followed by references to the visible
3336 // declarations that have that name.
3337 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003338 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003339 if (!Map || Map->empty())
3340 return 0;
3341
3342 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3343 ASTDeclContextNameLookupTrait Trait(*this);
3344
3345 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003346 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003347 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003348 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3349 D != DEnd; ++D) {
3350 DeclarationName Name = D->first;
3351 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003352 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003353 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3354 // Hash all conversion function names to the same name. The actual
3355 // type information in conversion function name is not used in the
3356 // key (since such type information is not stable across different
3357 // modules), so the intended effect is to coalesce all of the conversion
3358 // functions under a single key.
3359 if (!ConversionName)
3360 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003361 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003362 continue;
3363 }
3364
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003365 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003366 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003367 }
3368
Douglas Gregore5a54b62011-08-30 20:49:19 +00003369 // Add the conversion functions
3370 if (!ConversionDecls.empty()) {
3371 Generator.insert(ConversionName,
3372 DeclContext::lookup_result(ConversionDecls.begin(),
3373 ConversionDecls.end()),
3374 Trait);
3375 }
3376
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003377 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003378 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003379 uint32_t BucketOffset;
3380 {
3381 llvm::raw_svector_ostream Out(LookupTable);
3382 // Make sure that no bucket is at offset 0
3383 clang::io::Emit32(Out, 0);
3384 BucketOffset = Generator.Emit(Out, Trait);
3385 }
3386
3387 // Write the lookup table
3388 RecordData Record;
3389 Record.push_back(DECL_CONTEXT_VISIBLE);
3390 Record.push_back(BucketOffset);
3391 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3392 LookupTable.str());
3393
3394 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3395 ++NumVisibleDeclContexts;
3396 return Offset;
3397}
3398
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003399/// \brief Write an UPDATE_VISIBLE block for the given context.
3400///
3401/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3402/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003403/// (in C++), for namespaces, and for classes with forward-declared unscoped
3404/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003405void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003406 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3407 if (!Map || Map->empty())
3408 return;
3409
3410 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3411 ASTDeclContextNameLookupTrait Trait(*this);
3412
3413 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003414 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3415 D != DEnd; ++D) {
3416 DeclarationName Name = D->first;
3417 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003418 // For any name that appears in this table, the results are complete, i.e.
3419 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003420 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003421 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003422 }
3423
3424 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003425 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003426 uint32_t BucketOffset;
3427 {
3428 llvm::raw_svector_ostream Out(LookupTable);
3429 // Make sure that no bucket is at offset 0
3430 clang::io::Emit32(Out, 0);
3431 BucketOffset = Generator.Emit(Out, Trait);
3432 }
3433
3434 // Write the lookup table
3435 RecordData Record;
3436 Record.push_back(UPDATE_VISIBLE);
3437 Record.push_back(getDeclID(cast<Decl>(DC)));
3438 Record.push_back(BucketOffset);
3439 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3440}
3441
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003442/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3443void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3444 RecordData Record;
3445 Record.push_back(Opts.fp_contract);
3446 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3447}
3448
3449/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3450void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003451 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003452 return;
3453
3454 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3455 RecordData Record;
3456#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3457#include "clang/Basic/OpenCLExtensions.def"
3458 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3459}
3460
Douglas Gregor2171bf12012-01-15 16:58:34 +00003461void ASTWriter::WriteRedeclarations() {
3462 RecordData LocalRedeclChains;
3463 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3464
3465 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3466 Decl *First = Redeclarations[I];
3467 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3468
3469 Decl *MostRecent = First->getMostRecentDecl();
3470
3471 // If we only have a single declaration, there is no point in storing
3472 // a redeclaration chain.
3473 if (First == MostRecent)
3474 continue;
3475
3476 unsigned Offset = LocalRedeclChains.size();
3477 unsigned Size = 0;
3478 LocalRedeclChains.push_back(0); // Placeholder for the size.
3479
3480 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003481 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003482 Prev = Prev->getPreviousDecl()) {
3483 if (!Prev->isFromASTFile()) {
3484 AddDeclRef(Prev, LocalRedeclChains);
3485 ++Size;
3486 }
3487 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003488
3489 if (!First->isFromASTFile() && Chain) {
3490 Decl *FirstFromAST = MostRecent;
3491 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3492 if (Prev->isFromASTFile())
3493 FirstFromAST = Prev;
3494 }
3495
3496 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3497 }
3498
Douglas Gregor2171bf12012-01-15 16:58:34 +00003499 LocalRedeclChains[Offset] = Size;
3500
3501 // Reverse the set of local redeclarations, so that we store them in
3502 // order (since we found them in reverse order).
3503 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3504
Douglas Gregoraa945902013-02-18 15:53:43 +00003505 // Add the mapping from the first ID from the AST to the set of local
3506 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003507 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3508 LocalRedeclsMap.push_back(Info);
3509
3510 assert(N == Redeclarations.size() &&
3511 "Deserialized a declaration we shouldn't have");
3512 }
3513
3514 if (LocalRedeclChains.empty())
3515 return;
3516
3517 // Sort the local redeclarations map by the first declaration ID,
3518 // since the reader will be performing binary searches on this information.
3519 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3520
3521 // Emit the local redeclarations map.
3522 using namespace llvm;
3523 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3524 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3527 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3528
3529 RecordData Record;
3530 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3531 Record.push_back(LocalRedeclsMap.size());
3532 Stream.EmitRecordWithBlob(AbbrevID, Record,
3533 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3534 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3535
3536 // Emit the redeclaration chains.
3537 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3538}
3539
Douglas Gregorcff9f262012-01-27 01:47:08 +00003540void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003541 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003542 RecordData Categories;
3543
3544 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3545 unsigned Size = 0;
3546 unsigned StartIndex = Categories.size();
3547
3548 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3549
3550 // Allocate space for the size.
3551 Categories.push_back(0);
3552
3553 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003554 for (ObjCInterfaceDecl::known_categories_iterator
3555 Cat = Class->known_categories_begin(),
3556 CatEnd = Class->known_categories_end();
3557 Cat != CatEnd; ++Cat, ++Size) {
3558 assert(getDeclID(*Cat) != 0 && "Bogus category");
3559 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003560 }
3561
3562 // Update the size.
3563 Categories[StartIndex] = Size;
3564
3565 // Record this interface -> category map.
3566 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3567 CategoriesMap.push_back(CatInfo);
3568 }
3569
3570 // Sort the categories map by the definition ID, since the reader will be
3571 // performing binary searches on this information.
3572 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3573
3574 // Emit the categories map.
3575 using namespace llvm;
3576 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3577 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3580 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3581
3582 RecordData Record;
3583 Record.push_back(OBJC_CATEGORIES_MAP);
3584 Record.push_back(CategoriesMap.size());
3585 Stream.EmitRecordWithBlob(AbbrevID, Record,
3586 reinterpret_cast<char*>(CategoriesMap.data()),
3587 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3588
3589 // Emit the category lists.
3590 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3591}
3592
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003593void ASTWriter::WriteMergedDecls() {
3594 if (!Chain || Chain->MergedDecls.empty())
3595 return;
3596
3597 RecordData Record;
3598 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3599 IEnd = Chain->MergedDecls.end();
3600 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003601 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003602 : getDeclID(I->first);
3603 assert(CanonID && "Merged declaration not known?");
3604
3605 Record.push_back(CanonID);
3606 Record.push_back(I->second.size());
3607 Record.append(I->second.begin(), I->second.end());
3608 }
3609 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3610}
3611
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003612//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003613// General Serialization Routines
3614//===----------------------------------------------------------------------===//
3615
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003616/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003617void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3618 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003619 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003620 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3621 e = Attrs.end(); i != e; ++i){
3622 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003623 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003624 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003625
Sean Huntcf807c42010-08-18 23:23:40 +00003626#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003627
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003628 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003629}
3630
Chris Lattner5f9e2722011-07-23 10:55:15 +00003631void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003632 Record.push_back(Str.size());
3633 Record.insert(Record.end(), Str.begin(), Str.end());
3634}
3635
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003636void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3637 RecordDataImpl &Record) {
3638 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003639 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003640 Record.push_back(*Minor + 1);
3641 else
3642 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003643 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003644 Record.push_back(*Subminor + 1);
3645 else
3646 Record.push_back(0);
3647}
3648
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003649/// \brief Note that the identifier II occurs at the given offset
3650/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003651void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003652 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003653 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003654 // up earlier in the chain and thus don't need an offset.
3655 if (ID >= FirstIdentID)
3656 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003657}
3658
Douglas Gregor83941df2009-04-25 17:48:32 +00003659/// \brief Note that the selector Sel occurs at the given offset
3660/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003661void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003662 unsigned ID = SelectorIDs[Sel];
3663 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003664 // Don't record offsets for selectors that are also available in a different
3665 // file.
3666 if (ID < FirstSelectorID)
3667 return;
3668 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003669}
3670
Sebastian Redla4232eb2010-08-18 23:56:21 +00003671ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003672 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003673 WritingAST(false), DoneWritingDeclsAndTypes(false),
3674 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003675 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003676 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003677 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3678 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003679 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3680 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003681 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003682 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003683 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003684 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003685 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003686 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003687 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3688 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3689 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003690 DeclTypedefAbbrev(0),
3691 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3692 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003693{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003694}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003695
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003696ASTWriter::~ASTWriter() {
3697 for (FileDeclIDsTy::iterator
3698 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3699 delete I->second;
3700}
3701
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003702void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003703 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003704 Module *WritingModule, StringRef isysroot,
3705 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003706 WritingAST = true;
3707
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003708 ASTHasCompilerErrors = hasErrors;
3709
Douglas Gregor2cf26342009-04-09 22:27:44 +00003710 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003711 Stream.Emit((unsigned)'C', 8);
3712 Stream.Emit((unsigned)'P', 8);
3713 Stream.Emit((unsigned)'C', 8);
3714 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003715
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003716 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003717
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003718 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003719 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003720 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003721 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003722 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003723 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003724 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003725
3726 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003727}
3728
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003729template<typename Vector>
3730static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3731 ASTWriter::RecordData &Record) {
3732 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3733 I != E; ++I) {
3734 Writer.AddDeclRef(*I, Record);
3735 }
3736}
3737
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003738void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003739 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003740 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003741 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003742 using namespace llvm;
3743
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003744 bool isModule = WritingModule != 0;
3745
Douglas Gregorecc2c092011-12-01 22:20:10 +00003746 // Make sure that the AST reader knows to finalize itself.
3747 if (Chain)
3748 Chain->finalizeForWriting();
3749
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003750 ASTContext &Context = SemaRef.Context;
3751 Preprocessor &PP = SemaRef.PP;
3752
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003753 // Set up predefined declaration IDs.
3754 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003755 if (Context.ObjCIdDecl)
3756 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003757 if (Context.ObjCSelDecl)
3758 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003759 if (Context.ObjCClassDecl)
3760 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003761 if (Context.ObjCProtocolClassDecl)
3762 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003763 if (Context.Int128Decl)
3764 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3765 if (Context.UInt128Decl)
3766 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003767 if (Context.ObjCInstanceTypeDecl)
3768 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003769 if (Context.BuiltinVaListDecl)
3770 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3771
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003772 if (!Chain) {
3773 // Make sure that we emit IdentifierInfos (and any attached
3774 // declarations) for builtins. We don't need to do this when we're
3775 // emitting chained PCH files, because all of the builtins will be
3776 // in the original PCH file.
3777 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003778 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003779 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003780 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003781 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003782 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3783 getIdentifierRef(&Table.get(BuiltinNames[I]));
3784 }
3785
Douglas Gregoreee242f2011-10-27 09:33:13 +00003786 // If there are any out-of-date identifiers, bring them up to date.
3787 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003788 // Find out-of-date identifiers.
3789 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003790 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3791 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003792 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003793 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003794 OutOfDate.push_back(ID->second);
3795 }
3796
3797 // Update the out-of-date identifiers.
3798 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3799 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3800 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003801 }
3802
Chris Lattner63d65f82009-09-08 18:19:27 +00003803 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003804 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003805 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003806 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003807 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003808
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003809 // Build a record containing all of the file scoped decls in this file.
3810 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003811 if (!isModule)
3812 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3813 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003814
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003815 // Build a record containing all of the delegating constructors we still need
3816 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003817 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003818 if (!isModule)
3819 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003820
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003821 // Write the set of weak, undeclared identifiers. We always write the
3822 // entire table, since later PCH files in a PCH chain are only interested in
3823 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003824 RecordData WeakUndeclaredIdentifiers;
3825 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003826 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003827 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3828 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3829 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3830 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3831 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3832 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3833 }
3834 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003835
Richard Smith5ea6ef42013-01-10 23:43:47 +00003836 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003837 // declarations in this header file. Generally, this record will be
3838 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003839 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003840 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003841 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003842 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003843 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3844 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003845 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003846 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003847 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003848 }
3849
Douglas Gregorb81c1702009-04-27 20:06:05 +00003850 // Build a record containing all of the ext_vector declarations.
3851 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003852 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003853
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003854 // Build a record containing all of the VTable uses information.
3855 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003856 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003857 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3858 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3859 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3860 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3861 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003862 }
3863
3864 // Build a record containing all of dynamic classes declarations.
3865 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003866 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003867
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003868 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003869 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003870 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003871 I = SemaRef.PendingInstantiations.begin(),
3872 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3873 AddDeclRef(I->first, PendingInstantiations);
3874 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003875 }
3876 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3877 "There are local ones at end of translation unit!");
3878
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003879 // Build a record containing some declaration references.
3880 RecordData SemaDeclRefs;
3881 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3882 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3883 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3884 }
3885
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003886 RecordData CUDASpecialDeclRefs;
3887 if (Context.getcudaConfigureCallDecl()) {
3888 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3889 }
3890
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003891 // Build a record containing all of the known namespaces.
3892 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003893 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003894 I = SemaRef.KnownNamespaces.begin(),
3895 IEnd = SemaRef.KnownNamespaces.end();
3896 I != IEnd; ++I) {
3897 if (!I->second)
3898 AddDeclRef(I->first, KnownNamespaces);
3899 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003900
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003901 // Build a record of all used, undefined objects that require definitions.
3902 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003903
3904 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003905 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003906 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3907 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003908 AddDeclRef(I->first, UndefinedButUsed);
3909 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003910 }
3911
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003912 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003913 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003914
Sebastian Redl3397c552010-08-18 23:56:27 +00003915 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003916 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003917 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003918
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003919 // This is so that older clang versions, before the introduction
3920 // of the control block, can read and reject the newer PCH format.
3921 Record.clear();
3922 Record.push_back(VERSION_MAJOR);
3923 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3924
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003925 // Create a lexical update block containing all of the declarations in the
3926 // translation unit that do not come from other AST files.
3927 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3928 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3929 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3930 E = TU->noload_decls_end();
3931 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003932 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003933 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003934 }
3935
3936 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3937 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3938 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3939 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3940 Record.clear();
3941 Record.push_back(TU_UPDATE_LEXICAL);
3942 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3943 data(NewGlobalDecls));
3944
3945 // And a visible updates block for the translation unit.
3946 Abv = new llvm::BitCodeAbbrev();
3947 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3948 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3949 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3950 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3951 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3952 WriteDeclContextVisibleUpdate(TU);
3953
3954 // If the translation unit has an anonymous namespace, and we don't already
3955 // have an update block for it, write it as an update block.
3956 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3957 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3958 if (Record.empty()) {
3959 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003960 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003961 }
3962 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003963
3964 // Make sure visible decls, added to DeclContexts previously loaded from
3965 // an AST file, are registered for serialization.
3966 for (SmallVector<const Decl *, 16>::iterator
3967 I = UpdatingVisibleDecls.begin(),
3968 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3969 GetDeclRef(*I);
3970 }
3971
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003972 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003973 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003974
Douglas Gregora119da02011-08-02 16:26:37 +00003975 // Form the record of special types.
3976 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003977 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003978 AddTypeRef(Context.getFILEType(), SpecialTypes);
3979 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3980 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3981 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3982 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003983 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003984 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003985
Douglas Gregor366809a2009-04-26 03:49:13 +00003986 // Keep writing types and declarations until all types and
3987 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003988 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003989 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003990 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3991 E = DeclsToRewrite.end();
3992 I != E; ++I)
3993 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003994 while (!DeclTypesToEmit.empty()) {
3995 DeclOrType DOT = DeclTypesToEmit.front();
3996 DeclTypesToEmit.pop();
3997 if (DOT.isType())
3998 WriteType(DOT.getType());
3999 else
4000 WriteDecl(Context, DOT.getDecl());
4001 }
4002 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004003
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004004 DoneWritingDeclsAndTypes = true;
4005
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004006 WriteFileDeclIDsMap();
4007 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004008 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004009
4010 if (Chain) {
4011 // Write the mapping information describing our module dependencies and how
4012 // each of those modules were mapped into our own offset/ID space, so that
4013 // the reader can build the appropriate mapping to its own offset/ID space.
4014 // The map consists solely of a blob with the following format:
4015 // *(module-name-len:i16 module-name:len*i8
4016 // source-location-offset:i32
4017 // identifier-id:i32
4018 // preprocessed-entity-id:i32
4019 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004020 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004021 // selector-id:i32
4022 // declaration-id:i32
4023 // c++-base-specifiers-id:i32
4024 // type-id:i32)
4025 //
4026 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4027 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4029 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004030 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004031 {
4032 llvm::raw_svector_ostream Out(Buffer);
4033 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004034 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004035 M != MEnd; ++M) {
4036 StringRef FileName = (*M)->FileName;
4037 io::Emit16(Out, FileName.size());
4038 Out.write(FileName.data(), FileName.size());
4039 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4040 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004041 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004042 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004043 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004044 io::Emit32(Out, (*M)->BaseSelectorID);
4045 io::Emit32(Out, (*M)->BaseDeclID);
4046 io::Emit32(Out, (*M)->BaseTypeIndex);
4047 }
4048 }
4049 Record.clear();
4050 Record.push_back(MODULE_OFFSET_MAP);
4051 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4052 Buffer.data(), Buffer.size());
4053 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004054 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004055 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004056 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004057 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004058 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004059 WriteFPPragmaOptions(SemaRef.getFPOptions());
4060 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004061
Sebastian Redl1476ed42010-07-16 16:36:56 +00004062 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004063 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004064
Anders Carlssonc8505782011-03-06 18:41:18 +00004065 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004066
Douglas Gregore209e502011-12-06 01:10:29 +00004067 // If we're emitting a module, write out the submodule information.
4068 if (WritingModule)
4069 WriteSubmodules(WritingModule);
4070
Douglas Gregora119da02011-08-02 16:26:37 +00004071 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4072
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004073 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004074 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004075 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004076
4077 // Write the record containing tentative definitions.
4078 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004079 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004080
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004081 // Write the record containing unused file scoped decls.
4082 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004083 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004084
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004085 // Write the record containing weak undeclared identifiers.
4086 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004087 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004088 WeakUndeclaredIdentifiers);
4089
Richard Smith5ea6ef42013-01-10 23:43:47 +00004090 // Write the record containing locally-scoped extern "C" definitions.
4091 if (!LocallyScopedExternCDecls.empty())
4092 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4093 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004094
4095 // Write the record containing ext_vector type names.
4096 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004097 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004098
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004099 // Write the record containing VTable uses information.
4100 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004101 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004102
4103 // Write the record containing dynamic classes declarations.
4104 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004105 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004106
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004107 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004108 if (!PendingInstantiations.empty())
4109 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004110
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004111 // Write the record containing declaration references of Sema.
4112 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004113 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004114
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004115 // Write the record containing CUDA-specific declaration references.
4116 if (!CUDASpecialDeclRefs.empty())
4117 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004118
4119 // Write the delegating constructors.
4120 if (!DelegatingCtorDecls.empty())
4121 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004122
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004123 // Write the known namespaces.
4124 if (!KnownNamespaces.empty())
4125 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004126
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004127 // Write the undefined internal functions and variables, and inline functions.
4128 if (!UndefinedButUsed.empty())
4129 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004130
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004131 // Write the visible updates to DeclContexts.
4132 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4133 I = UpdatedDeclContexts.begin(),
4134 E = UpdatedDeclContexts.end();
4135 I != E; ++I)
4136 WriteDeclContextVisibleUpdate(*I);
4137
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004138 if (!WritingModule) {
4139 // Write the submodules that were imported, if any.
4140 RecordData ImportedModules;
4141 for (ASTContext::import_iterator I = Context.local_import_begin(),
4142 IEnd = Context.local_import_end();
4143 I != IEnd; ++I) {
4144 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4145 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4146 }
4147 if (!ImportedModules.empty()) {
4148 // Sort module IDs.
4149 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4150
4151 // Unique module IDs.
4152 ImportedModules.erase(std::unique(ImportedModules.begin(),
4153 ImportedModules.end()),
4154 ImportedModules.end());
4155
4156 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4157 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004158 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004159
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004160 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004161 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004162 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004163 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004164 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00004165
Douglas Gregor3e1af842009-04-17 22:13:46 +00004166 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004167 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004168 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004169 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004170 Record.push_back(NumLexicalDeclContexts);
4171 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004172 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004173 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004174}
4175
Douglas Gregor61c5e342011-09-17 00:05:03 +00004176/// \brief Go through the declaration update blocks and resolve declaration
4177/// pointers into declaration IDs.
4178void ASTWriter::ResolveDeclUpdatesBlocks() {
4179 for (DeclUpdateMap::iterator
4180 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4181 const Decl *D = I->first;
4182 UpdateRecord &URec = I->second;
4183
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004184 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004185 continue; // The decl will be written completely
4186
4187 unsigned Idx = 0, N = URec.size();
4188 while (Idx < N) {
4189 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004190 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4191 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4192 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4193 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4194 ++Idx;
4195 break;
4196
4197 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4198 ++Idx;
4199 break;
4200 }
4201 }
4202 }
4203}
4204
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004205void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004206 if (DeclUpdates.empty())
4207 return;
4208
4209 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004210 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004211 for (DeclUpdateMap::iterator
4212 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4213 const Decl *D = I->first;
4214 UpdateRecord &URec = I->second;
4215
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004216 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004217 continue; // The decl will be written completely,no need to store updates.
4218
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004219 uint64_t Offset = Stream.GetCurrentBitNo();
4220 Stream.EmitRecord(DECL_UPDATES, URec);
4221
4222 OffsetsRecord.push_back(GetDeclRef(D));
4223 OffsetsRecord.push_back(Offset);
4224 }
4225 Stream.ExitBlock();
4226 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4227}
4228
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004229void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004230 if (ReplacedDecls.empty())
4231 return;
4232
4233 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004234 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004235 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004236 Record.push_back(I->ID);
4237 Record.push_back(I->Offset);
4238 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004239 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004240 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004241}
4242
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004243void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004244 Record.push_back(Loc.getRawEncoding());
4245}
4246
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004247void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004248 AddSourceLocation(Range.getBegin(), Record);
4249 AddSourceLocation(Range.getEnd(), Record);
4250}
4251
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004252void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004253 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004254 const uint64_t *Words = Value.getRawData();
4255 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004256}
4257
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004258void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004259 Record.push_back(Value.isUnsigned());
4260 AddAPInt(Value, Record);
4261}
4262
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004263void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004264 AddAPInt(Value.bitcastToAPInt(), Record);
4265}
4266
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004267void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004268 Record.push_back(getIdentifierRef(II));
4269}
4270
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004271IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004272 if (II == 0)
4273 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004274
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004275 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004276 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004277 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004278 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004279}
4280
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004281MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004282 // Don't emit builtin macros like __LINE__ to the AST file unless they
4283 // have been redefined by the header (in which case they are not
4284 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004285 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004286 return 0;
4287
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004288 MacroID &ID = MacroIDs[MI];
4289 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004290 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004291 MacroInfoToEmitData Info = { Name, MI, ID };
4292 MacroInfosToEmit.push_back(Info);
4293 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004294 return ID;
4295}
4296
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004297MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4298 if (MI == 0 || MI->isBuiltinMacro())
4299 return 0;
4300
4301 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4302 return MacroIDs[MI];
4303}
4304
4305uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4306 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4307 return IdentMacroDirectivesOffsetMap[Name];
4308}
4309
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004310void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004311 Record.push_back(getSelectorRef(SelRef));
4312}
4313
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004314SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004315 if (Sel.getAsOpaquePtr() == 0) {
4316 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004317 }
4318
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004319 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004320 if (SID == 0 && Chain) {
4321 // This might trigger a ReadSelector callback, which will set the ID for
4322 // this selector.
4323 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004324 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004325 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004326 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004327 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004328 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004329 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004330 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004331}
4332
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004333void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004334 AddDeclRef(Temp->getDestructor(), Record);
4335}
4336
Douglas Gregor7c789c12010-10-29 22:39:52 +00004337void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4338 CXXBaseSpecifier const *BasesEnd,
4339 RecordDataImpl &Record) {
4340 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4341 CXXBaseSpecifiersToWrite.push_back(
4342 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4343 Bases, BasesEnd));
4344 Record.push_back(NextCXXBaseSpecifiersID++);
4345}
4346
Sebastian Redla4232eb2010-08-18 23:56:21 +00004347void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004348 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004349 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004350 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004351 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004352 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004353 break;
4354 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004355 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004356 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004357 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004358 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004359 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004360 break;
4361 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004362 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004363 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004364 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004365 break;
John McCall833ca992009-10-29 08:12:44 +00004366 case TemplateArgument::Null:
4367 case TemplateArgument::Integral:
4368 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004369 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004370 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004371 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004372 break;
4373 }
4374}
4375
Sebastian Redla4232eb2010-08-18 23:56:21 +00004376void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004377 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004378 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004379
4380 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4381 bool InfoHasSameExpr
4382 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4383 Record.push_back(InfoHasSameExpr);
4384 if (InfoHasSameExpr)
4385 return; // Avoid storing the same expr twice.
4386 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004387 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4388 Record);
4389}
4390
Douglas Gregordc355712011-02-25 00:36:19 +00004391void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4392 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004393 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004394 AddTypeRef(QualType(), Record);
4395 return;
4396 }
4397
Douglas Gregordc355712011-02-25 00:36:19 +00004398 AddTypeLoc(TInfo->getTypeLoc(), Record);
4399}
4400
4401void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4402 AddTypeRef(TL.getType(), Record);
4403
John McCalla1ee0c52009-10-16 21:56:05 +00004404 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004405 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004406 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004407}
4408
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004409void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004410 Record.push_back(GetOrCreateTypeID(T));
4411}
4412
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004413TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4414 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004415 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4416}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004417
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004418TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004419 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004420 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004421}
4422
4423TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4424 if (T.isNull())
4425 return TypeIdx();
4426 assert(!T.getLocalFastQualifiers());
4427
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004428 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004429 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004430 if (DoneWritingDeclsAndTypes) {
4431 assert(0 && "New type seen after serializing all the types to emit!");
4432 return TypeIdx();
4433 }
4434
Douglas Gregor366809a2009-04-26 03:49:13 +00004435 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004436 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004437 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004438 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004439 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004440 return Idx;
4441}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004442
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004443TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004444 if (T.isNull())
4445 return TypeIdx();
4446 assert(!T.getLocalFastQualifiers());
4447
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004448 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4449 assert(I != TypeIdxs.end() && "Type not emitted!");
4450 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004451}
4452
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004453void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004454 Record.push_back(GetDeclRef(D));
4455}
4456
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004457DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004458 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4459
Douglas Gregor2cf26342009-04-09 22:27:44 +00004460 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004461 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004462 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004463
4464 // If D comes from an AST file, its declaration ID is already known and
4465 // fixed.
4466 if (D->isFromASTFile())
4467 return D->getGlobalID();
4468
Douglas Gregor97475832010-10-05 18:37:06 +00004469 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004470 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004471 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004472 if (DoneWritingDeclsAndTypes) {
4473 assert(0 && "New decl seen after serializing all the decls to emit!");
4474 return 0;
4475 }
4476
Douglas Gregor2cf26342009-04-09 22:27:44 +00004477 // We haven't seen this declaration before. Give it a new ID and
4478 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004479 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004480 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004481 }
4482
Sebastian Redl681d7232010-07-27 00:17:23 +00004483 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004484}
4485
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004486DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004487 if (D == 0)
4488 return 0;
4489
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004490 // If D comes from an AST file, its declaration ID is already known and
4491 // fixed.
4492 if (D->isFromASTFile())
4493 return D->getGlobalID();
4494
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004495 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4496 return DeclIDs[D];
4497}
4498
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004499static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4500 std::pair<unsigned, serialization::DeclID> R) {
4501 return L.first < R.first;
4502}
4503
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004504void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004505 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004506 assert(D);
4507
4508 SourceLocation Loc = D->getLocation();
4509 if (Loc.isInvalid())
4510 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004511
4512 // We only keep track of the file-level declarations of each file.
4513 if (!D->getLexicalDeclContext()->isFileContext())
4514 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004515 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4516 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004517 if (isa<ParmVarDecl>(D))
4518 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004519
4520 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004521 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004522 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004523 FileID FID;
4524 unsigned Offset;
4525 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004526 if (FID.isInvalid())
4527 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004528 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004529
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004530 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004531 if (!Info)
4532 Info = new DeclIDInFileInfo();
4533
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004534 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004535 LocDeclIDsTy &Decls = Info->DeclIDs;
4536
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004537 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004538 Decls.push_back(LocDecl);
4539 return;
4540 }
4541
4542 LocDeclIDsTy::iterator
4543 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4544
4545 Decls.insert(I, LocDecl);
4546}
4547
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004548void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004549 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004550 Record.push_back(Name.getNameKind());
4551 switch (Name.getNameKind()) {
4552 case DeclarationName::Identifier:
4553 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4554 break;
4555
4556 case DeclarationName::ObjCZeroArgSelector:
4557 case DeclarationName::ObjCOneArgSelector:
4558 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004559 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004560 break;
4561
4562 case DeclarationName::CXXConstructorName:
4563 case DeclarationName::CXXDestructorName:
4564 case DeclarationName::CXXConversionFunctionName:
4565 AddTypeRef(Name.getCXXNameType(), Record);
4566 break;
4567
4568 case DeclarationName::CXXOperatorName:
4569 Record.push_back(Name.getCXXOverloadedOperator());
4570 break;
4571
Sean Hunt3e518bd2009-11-29 07:34:05 +00004572 case DeclarationName::CXXLiteralOperatorName:
4573 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4574 break;
4575
Douglas Gregor2cf26342009-04-09 22:27:44 +00004576 case DeclarationName::CXXUsingDirective:
4577 // No extra data to emit
4578 break;
4579 }
4580}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004581
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004582void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004583 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004584 switch (Name.getNameKind()) {
4585 case DeclarationName::CXXConstructorName:
4586 case DeclarationName::CXXDestructorName:
4587 case DeclarationName::CXXConversionFunctionName:
4588 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4589 break;
4590
4591 case DeclarationName::CXXOperatorName:
4592 AddSourceLocation(
4593 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4594 Record);
4595 AddSourceLocation(
4596 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4597 Record);
4598 break;
4599
4600 case DeclarationName::CXXLiteralOperatorName:
4601 AddSourceLocation(
4602 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4603 Record);
4604 break;
4605
4606 case DeclarationName::Identifier:
4607 case DeclarationName::ObjCZeroArgSelector:
4608 case DeclarationName::ObjCOneArgSelector:
4609 case DeclarationName::ObjCMultiArgSelector:
4610 case DeclarationName::CXXUsingDirective:
4611 break;
4612 }
4613}
4614
4615void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004616 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004617 AddDeclarationName(NameInfo.getName(), Record);
4618 AddSourceLocation(NameInfo.getLoc(), Record);
4619 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4620}
4621
4622void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004623 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004624 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004625 Record.push_back(Info.NumTemplParamLists);
4626 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4627 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4628}
4629
Sebastian Redla4232eb2010-08-18 23:56:21 +00004630void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004631 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004632 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004633 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004634 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004635
4636 // Push each of the NNS's onto a stack for serialization in reverse order.
4637 while (NNS) {
4638 NestedNames.push_back(NNS);
4639 NNS = NNS->getPrefix();
4640 }
4641
4642 Record.push_back(NestedNames.size());
4643 while(!NestedNames.empty()) {
4644 NNS = NestedNames.pop_back_val();
4645 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4646 Record.push_back(Kind);
4647 switch (Kind) {
4648 case NestedNameSpecifier::Identifier:
4649 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4650 break;
4651
4652 case NestedNameSpecifier::Namespace:
4653 AddDeclRef(NNS->getAsNamespace(), Record);
4654 break;
4655
Douglas Gregor14aba762011-02-24 02:36:08 +00004656 case NestedNameSpecifier::NamespaceAlias:
4657 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4658 break;
4659
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004660 case NestedNameSpecifier::TypeSpec:
4661 case NestedNameSpecifier::TypeSpecWithTemplate:
4662 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4663 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4664 break;
4665
4666 case NestedNameSpecifier::Global:
4667 // Don't need to write an associated value.
4668 break;
4669 }
4670 }
4671}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004672
Douglas Gregordc355712011-02-25 00:36:19 +00004673void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4674 RecordDataImpl &Record) {
4675 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004676 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004677 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004678
4679 // Push each of the nested-name-specifiers's onto a stack for
4680 // serialization in reverse order.
4681 while (NNS) {
4682 NestedNames.push_back(NNS);
4683 NNS = NNS.getPrefix();
4684 }
4685
4686 Record.push_back(NestedNames.size());
4687 while(!NestedNames.empty()) {
4688 NNS = NestedNames.pop_back_val();
4689 NestedNameSpecifier::SpecifierKind Kind
4690 = NNS.getNestedNameSpecifier()->getKind();
4691 Record.push_back(Kind);
4692 switch (Kind) {
4693 case NestedNameSpecifier::Identifier:
4694 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4695 AddSourceRange(NNS.getLocalSourceRange(), Record);
4696 break;
4697
4698 case NestedNameSpecifier::Namespace:
4699 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4700 AddSourceRange(NNS.getLocalSourceRange(), Record);
4701 break;
4702
4703 case NestedNameSpecifier::NamespaceAlias:
4704 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4705 AddSourceRange(NNS.getLocalSourceRange(), Record);
4706 break;
4707
4708 case NestedNameSpecifier::TypeSpec:
4709 case NestedNameSpecifier::TypeSpecWithTemplate:
4710 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4711 AddTypeLoc(NNS.getTypeLoc(), Record);
4712 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4713 break;
4714
4715 case NestedNameSpecifier::Global:
4716 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4717 break;
4718 }
4719 }
4720}
4721
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004722void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004723 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004724 Record.push_back(Kind);
4725 switch (Kind) {
4726 case TemplateName::Template:
4727 AddDeclRef(Name.getAsTemplateDecl(), Record);
4728 break;
4729
4730 case TemplateName::OverloadedTemplate: {
4731 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4732 Record.push_back(OvT->size());
4733 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4734 I != E; ++I)
4735 AddDeclRef(*I, Record);
4736 break;
4737 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004738
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004739 case TemplateName::QualifiedTemplate: {
4740 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4741 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4742 Record.push_back(QualT->hasTemplateKeyword());
4743 AddDeclRef(QualT->getTemplateDecl(), Record);
4744 break;
4745 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004746
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004747 case TemplateName::DependentTemplate: {
4748 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4749 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4750 Record.push_back(DepT->isIdentifier());
4751 if (DepT->isIdentifier())
4752 AddIdentifierRef(DepT->getIdentifier(), Record);
4753 else
4754 Record.push_back(DepT->getOperator());
4755 break;
4756 }
John McCall14606042011-06-30 08:33:18 +00004757
4758 case TemplateName::SubstTemplateTemplateParm: {
4759 SubstTemplateTemplateParmStorage *subst
4760 = Name.getAsSubstTemplateTemplateParm();
4761 AddDeclRef(subst->getParameter(), Record);
4762 AddTemplateName(subst->getReplacement(), Record);
4763 break;
4764 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004765
4766 case TemplateName::SubstTemplateTemplateParmPack: {
4767 SubstTemplateTemplateParmPackStorage *SubstPack
4768 = Name.getAsSubstTemplateTemplateParmPack();
4769 AddDeclRef(SubstPack->getParameterPack(), Record);
4770 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4771 break;
4772 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004773 }
4774}
4775
Michael J. Spencer20249a12010-10-21 03:16:25 +00004776void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004777 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004778 Record.push_back(Arg.getKind());
4779 switch (Arg.getKind()) {
4780 case TemplateArgument::Null:
4781 break;
4782 case TemplateArgument::Type:
4783 AddTypeRef(Arg.getAsType(), Record);
4784 break;
4785 case TemplateArgument::Declaration:
4786 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004787 Record.push_back(Arg.isDeclForReferenceParam());
4788 break;
4789 case TemplateArgument::NullPtr:
4790 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004791 break;
4792 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004793 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004794 AddTypeRef(Arg.getIntegralType(), Record);
4795 break;
4796 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004797 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4798 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004799 case TemplateArgument::TemplateExpansion:
4800 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004801 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004802 Record.push_back(*NumExpansions + 1);
4803 else
4804 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004805 break;
4806 case TemplateArgument::Expression:
4807 AddStmt(Arg.getAsExpr());
4808 break;
4809 case TemplateArgument::Pack:
4810 Record.push_back(Arg.pack_size());
4811 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4812 I != E; ++I)
4813 AddTemplateArgument(*I, Record);
4814 break;
4815 }
4816}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004817
4818void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004819ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004820 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004821 assert(TemplateParams && "No TemplateParams!");
4822 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4823 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4824 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4825 Record.push_back(TemplateParams->size());
4826 for (TemplateParameterList::const_iterator
4827 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4828 P != PEnd; ++P)
4829 AddDeclRef(*P, Record);
4830}
4831
4832/// \brief Emit a template argument list.
4833void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004834ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004835 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004836 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004837 Record.push_back(TemplateArgs->size());
4838 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004839 AddTemplateArgument(TemplateArgs->get(i), Record);
4840}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004841
4842
4843void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004844ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004845 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004846 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004847 I = Set.begin(), E = Set.end(); I != E; ++I) {
4848 AddDeclRef(I.getDecl(), Record);
4849 Record.push_back(I.getAccess());
4850 }
4851}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004852
Sebastian Redla4232eb2010-08-18 23:56:21 +00004853void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004854 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004855 Record.push_back(Base.isVirtual());
4856 Record.push_back(Base.isBaseOfClass());
4857 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004858 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004859 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004860 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004861 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4862 : SourceLocation(),
4863 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004864}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004865
Douglas Gregor7c789c12010-10-29 22:39:52 +00004866void ASTWriter::FlushCXXBaseSpecifiers() {
4867 RecordData Record;
4868 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4869 Record.clear();
4870
4871 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004872 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004873 if (Index == CXXBaseSpecifiersOffsets.size())
4874 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4875 else {
4876 if (Index > CXXBaseSpecifiersOffsets.size())
4877 CXXBaseSpecifiersOffsets.resize(Index + 1);
4878 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4879 }
4880
4881 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4882 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4883 Record.push_back(BEnd - B);
4884 for (; B != BEnd; ++B)
4885 AddCXXBaseSpecifier(*B, Record);
4886 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004887
4888 // Flush any expressions that were written as part of the base specifiers.
4889 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004890 }
4891
4892 CXXBaseSpecifiersToWrite.clear();
4893}
4894
Sean Huntcbb67482011-01-08 20:30:50 +00004895void ASTWriter::AddCXXCtorInitializers(
4896 const CXXCtorInitializer * const *CtorInitializers,
4897 unsigned NumCtorInitializers,
4898 RecordDataImpl &Record) {
4899 Record.push_back(NumCtorInitializers);
4900 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4901 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004902
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004903 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004904 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004905 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004906 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004907 } else if (Init->isDelegatingInitializer()) {
4908 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004909 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004910 } else if (Init->isMemberInitializer()){
4911 Record.push_back(CTOR_INITIALIZER_MEMBER);
4912 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004913 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004914 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4915 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004916 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004917
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004918 AddSourceLocation(Init->getMemberLocation(), Record);
4919 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004920 AddSourceLocation(Init->getLParenLoc(), Record);
4921 AddSourceLocation(Init->getRParenLoc(), Record);
4922 Record.push_back(Init->isWritten());
4923 if (Init->isWritten()) {
4924 Record.push_back(Init->getSourceOrder());
4925 } else {
4926 Record.push_back(Init->getNumArrayIndices());
4927 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4928 AddDeclRef(Init->getArrayIndex(i), Record);
4929 }
4930 }
4931}
4932
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004933void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4934 assert(D->DefinitionData);
4935 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004936 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004937 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004938 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004939 Record.push_back(Data.Aggregate);
4940 Record.push_back(Data.PlainOldData);
4941 Record.push_back(Data.Empty);
4942 Record.push_back(Data.Polymorphic);
4943 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004944 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004945 Record.push_back(Data.HasNoNonEmptyBases);
4946 Record.push_back(Data.HasPrivateFields);
4947 Record.push_back(Data.HasProtectedFields);
4948 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004949 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004950 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004951 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004952 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004953 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4954 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4955 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4956 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4957 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4958 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004959 Record.push_back(Data.HasTrivialSpecialMembers);
4960 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004961 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004962 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004963 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004964 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004965 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004966 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004967 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00004968 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
4969 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
4970 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
4971 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00004972 Record.push_back(Data.FailedImplicitMoveConstructor);
4973 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004974 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004975
4976 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004977 if (Data.NumBases > 0)
4978 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4979 Record);
4980
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004981 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4982 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004983 if (Data.NumVBases > 0)
4984 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4985 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004986
4987 AddUnresolvedSet(Data.Conversions, Record);
4988 AddUnresolvedSet(Data.VisibleConversions, Record);
4989 // Data.Definition is the owning decl, no need to write it.
4990 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004991
4992 // Add lambda-specific data.
4993 if (Data.IsLambda) {
4994 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004995 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004996 Record.push_back(Lambda.NumCaptures);
4997 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004998 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004999 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005000 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005001 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5002 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5003 AddSourceLocation(Capture.getLocation(), Record);
5004 Record.push_back(Capture.isImplicit());
5005 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
5006 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
5007 AddDeclRef(Var, Record);
5008 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
5009 : SourceLocation(),
5010 Record);
5011 }
5012 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005013}
5014
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005015void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005016 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005017 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005018 assert(FirstDeclID == NextDeclID &&
5019 FirstTypeID == NextTypeID &&
5020 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005021 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005022 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005023 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005024 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005025
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005026 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005027
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005028 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5029 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5030 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005031 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005032 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005033 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005034 NextDeclID = FirstDeclID;
5035 NextTypeID = FirstTypeID;
5036 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005037 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005038 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005039 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005040}
5041
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005042void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005043 // Always keep the highest ID. See \p TypeRead() for more information.
5044 IdentID &StoredID = IdentifierIDs[II];
5045 if (ID > StoredID)
5046 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005047}
5048
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005049void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005050 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005051 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005052 if (ID > StoredID)
5053 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005054}
5055
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005056void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005057 // Always take the highest-numbered type index. This copes with an interesting
5058 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005059 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005060 // keep the higher-numbered entry so that we can properly write it out to
5061 // the AST file.
5062 TypeIdx &StoredIdx = TypeIdxs[T];
5063 if (Idx.getIndex() >= StoredIdx.getIndex())
5064 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005065}
5066
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005067void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005068 // Always keep the highest ID. See \p TypeRead() for more information.
5069 SelectorID &StoredID = SelectorIDs[S];
5070 if (ID > StoredID)
5071 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005072}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005073
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005074void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005075 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005076 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005077 MacroDefinitions[MD] = ID;
5078}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005079
Douglas Gregora015cab2011-12-02 17:30:13 +00005080void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5081 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5082 SubmoduleIDs[Mod] = ID;
5083}
5084
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005085void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005086 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005087 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005088 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5089 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005090 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005091 // A forward reference was mutated into a definition. Rewrite it.
5092 // FIXME: This happens during template instantiation, should we
5093 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005094 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005095 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005096 }
5097}
Douglas Gregora8235d62012-10-09 23:05:51 +00005098
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005099void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005100 assert(!WritingAST && "Already writing the AST!");
5101
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005102 // TU and namespaces are handled elsewhere.
5103 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5104 return;
5105
Douglas Gregor919814d2011-09-09 23:01:35 +00005106 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005107 return; // Not a source decl added to a DeclContext from PCH.
5108
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005109 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005110 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005111 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005112}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005113
5114void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005115 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005116 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005117 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005118 return; // Not a source member added to a class from PCH.
5119 if (!isa<CXXMethodDecl>(D))
5120 return; // We are interested in lazily declared implicit methods.
5121
5122 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005123 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005124 UpdateRecord &Record = DeclUpdates[RD];
5125 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005126 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005127}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005128
5129void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5130 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005131 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005132 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005133 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005134 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005135 return; // Not a source specialization added to a template from PCH.
5136
5137 UpdateRecord &Record = DeclUpdates[TD];
5138 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005139 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005140}
Douglas Gregor89d99802010-11-30 06:16:57 +00005141
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005142void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5143 const FunctionDecl *D) {
5144 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005145 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005146 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005147 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005148 return; // Not a source specialization added to a template from PCH.
5149
5150 UpdateRecord &Record = DeclUpdates[TD];
5151 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005152 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005153}
5154
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005155void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005156 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005157 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005158 return; // Declaration not imported from PCH.
5159
5160 // Implicit decl from a PCH was defined.
5161 // FIXME: Should implicit definition be a separate FunctionDecl?
5162 RewriteDecl(D);
5163}
5164
Sebastian Redlf79a7192011-04-29 08:19:30 +00005165void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005166 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005167 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005168 return;
5169
5170 // Since the actual instantiation is delayed, this really means that we need
5171 // to update the instantiation location.
5172 UpdateRecord &Record = DeclUpdates[D];
5173 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5174 AddSourceLocation(
5175 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5176}
5177
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005178void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5179 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005180 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005181 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005182 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005183
5184 assert(IFD->getDefinition() && "Category on a class without a definition?");
5185 ObjCClassesWithCategories.insert(
5186 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005187}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005188
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005189
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005190void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5191 const ObjCPropertyDecl *OrigProp,
5192 const ObjCCategoryDecl *ClassExt) {
5193 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5194 if (!D)
5195 return;
5196
5197 assert(!WritingAST && "Already writing the AST!");
5198 if (!D->isFromASTFile())
5199 return; // Declaration not imported from PCH.
5200
5201 RewriteDecl(D);
5202}