blob: 9d9d619eca8677b4a0929b736aee5829930eda38 [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);
Richard Smitha2c36462013-04-26 16:15:35 +0000248 Record.push_back(T->isDecltypeAuto());
Richard Smith34b41d92011-02-20 03:19:35 +0000249 Code = TYPE_AUTO;
250}
251
Sebastian Redl3397c552010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000253 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000254 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000255 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256 "Cannot serialize in the middle of a type definition");
257}
258
Sebastian Redl3397c552010-08-18 23:56:27 +0000259void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000261 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262}
263
Sebastian Redl3397c552010-08-18 23:56:27 +0000264void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000265 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000266 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000267}
268
John McCall9d156a72011-01-06 01:58:22 +0000269void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
270 Writer.AddTypeRef(T->getModifiedType(), Record);
271 Writer.AddTypeRef(T->getEquivalentType(), Record);
272 Record.push_back(T->getAttrKind());
273 Code = TYPE_ATTRIBUTED;
274}
275
Mike Stump1eb44332009-09-09 15:08:12 +0000276void
Sebastian Redl3397c552010-08-18 23:56:27 +0000277ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000278 const SubstTemplateTypeParmType *T) {
279 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
280 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000281 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000282}
283
284void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000285ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
286 const SubstTemplateTypeParmPackType *T) {
287 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
288 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
289 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
290}
291
292void
Sebastian Redl3397c552010-08-18 23:56:27 +0000293ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000294 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000295 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000296 Writer.AddTemplateName(T->getTemplateName(), Record);
297 Record.push_back(T->getNumArgs());
298 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
299 ArgI != ArgE; ++ArgI)
300 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000301 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
302 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000303 : T->getCanonicalTypeInternal(),
304 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000305 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000306}
307
308void
Sebastian Redl3397c552010-08-18 23:56:27 +0000309ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000310 VisitArrayType(T);
311 Writer.AddStmt(T->getSizeExpr());
312 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000313 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314}
315
316void
Sebastian Redl3397c552010-08-18 23:56:27 +0000317ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000318 const DependentSizedExtVectorType *T) {
319 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000320 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000321}
322
323void
Sebastian Redl3397c552010-08-18 23:56:27 +0000324ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000325 Record.push_back(T->getDepth());
326 Record.push_back(T->getIndex());
327 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000328 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000329 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000330}
331
332void
Sebastian Redl3397c552010-08-18 23:56:27 +0000333ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000334 Record.push_back(T->getKeyword());
335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000337 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
338 : T->getCanonicalTypeInternal(),
339 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000340 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000341}
342
343void
Sebastian Redl3397c552010-08-18 23:56:27 +0000344ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000345 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000346 Record.push_back(T->getKeyword());
347 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
348 Writer.AddIdentifierRef(T->getIdentifier(), Record);
349 Record.push_back(T->getNumArgs());
350 for (DependentTemplateSpecializationType::iterator
351 I = T->begin(), E = T->end(); I != E; ++I)
352 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000353 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000354}
355
Douglas Gregor7536dd52010-12-20 02:24:11 +0000356void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
357 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000358 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000359 Record.push_back(*NumExpansions + 1);
360 else
361 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000362 Code = TYPE_PACK_EXPANSION;
363}
364
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000365void ASTTypeWriter::VisitParenType(const ParenType *T) {
366 Writer.AddTypeRef(T->getInnerType(), Record);
367 Code = TYPE_PAREN;
368}
369
Sebastian Redl3397c552010-08-18 23:56:27 +0000370void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000371 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000372 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
373 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000374 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000375}
376
Sebastian Redl3397c552010-08-18 23:56:27 +0000377void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000378 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000379 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000380 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000381}
382
Sebastian Redl3397c552010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000384 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000385 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000386}
387
Sebastian Redl3397c552010-08-18 23:56:27 +0000388void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000389 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000390 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000391 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000392 E = T->qual_end(); I != E; ++I)
393 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000394 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000395}
396
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000397void
Sebastian Redl3397c552010-08-18 23:56:27 +0000398ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000399 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000400 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000401}
402
Eli Friedmanb001de72011-10-06 23:00:33 +0000403void
404ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
405 Writer.AddTypeRef(T->getValueType(), Record);
406 Code = TYPE_ATOMIC;
407}
408
John McCalla1ee0c52009-10-16 21:56:05 +0000409namespace {
410
411class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000412 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000413 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000414
415public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000416 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000417 : Writer(Writer), Record(Record) { }
418
John McCall51bd8032009-10-18 01:05:36 +0000419#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000420#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000421 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000422#include "clang/AST/TypeLocNodes.def"
423
John McCall51bd8032009-10-18 01:05:36 +0000424 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
425 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000426};
427
428}
429
John McCall51bd8032009-10-18 01:05:36 +0000430void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
431 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000432}
John McCall51bd8032009-10-18 01:05:36 +0000433void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000434 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
435 if (TL.needsExtraLocalData()) {
436 Record.push_back(TL.getWrittenTypeSpec());
437 Record.push_back(TL.getWrittenSignSpec());
438 Record.push_back(TL.getWrittenWidthSpec());
439 Record.push_back(TL.hasModeAttr());
440 }
John McCalla1ee0c52009-10-16 21:56:05 +0000441}
John McCall51bd8032009-10-18 01:05:36 +0000442void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000444}
John McCall51bd8032009-10-18 01:05:36 +0000445void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000447}
John McCall51bd8032009-10-18 01:05:36 +0000448void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
449 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000450}
John McCall51bd8032009-10-18 01:05:36 +0000451void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
452 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000453}
John McCall51bd8032009-10-18 01:05:36 +0000454void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000456}
John McCall51bd8032009-10-18 01:05:36 +0000457void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000459 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000460}
John McCall51bd8032009-10-18 01:05:36 +0000461void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
462 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
463 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
464 Record.push_back(TL.getSizeExpr() ? 1 : 0);
465 if (TL.getSizeExpr())
466 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000467}
John McCall51bd8032009-10-18 01:05:36 +0000468void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
469 VisitArrayTypeLoc(TL);
470}
471void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
472 VisitArrayTypeLoc(TL);
473}
474void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
475 VisitArrayTypeLoc(TL);
476}
477void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
478 DependentSizedArrayTypeLoc TL) {
479 VisitArrayTypeLoc(TL);
480}
481void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
482 DependentSizedExtVectorTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
484}
485void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
487}
488void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
490}
491void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000492 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000493 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
494 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000495 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000496 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
497 Writer.AddDeclRef(TL.getArg(i), Record);
498}
499void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
500 VisitFunctionTypeLoc(TL);
501}
502void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
503 VisitFunctionTypeLoc(TL);
504}
John McCalled976492009-12-04 22:46:56 +0000505void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getNameLoc(), Record);
507}
John McCall51bd8032009-10-18 01:05:36 +0000508void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getNameLoc(), Record);
510}
511void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000512 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
513 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
514 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000515}
516void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000517 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
518 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
519 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
520 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000521}
522void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
523 Writer.AddSourceLocation(TL.getNameLoc(), Record);
524}
Sean Huntca63c202011-05-24 22:41:36 +0000525void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
526 Writer.AddSourceLocation(TL.getKWLoc(), Record);
527 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
528 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
529 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
530}
Richard Smith34b41d92011-02-20 03:19:35 +0000531void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
532 Writer.AddSourceLocation(TL.getNameLoc(), Record);
533}
John McCall51bd8032009-10-18 01:05:36 +0000534void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
537void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
John McCall9d156a72011-01-06 01:58:22 +0000540void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
541 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
542 if (TL.hasAttrOperand()) {
543 SourceRange range = TL.getAttrOperandParensRange();
544 Writer.AddSourceLocation(range.getBegin(), Record);
545 Writer.AddSourceLocation(range.getEnd(), Record);
546 }
547 if (TL.hasAttrExprOperand()) {
548 Expr *operand = TL.getAttrExprOperand();
549 Record.push_back(operand ? 1 : 0);
550 if (operand) Writer.AddStmt(operand);
551 } else if (TL.hasAttrEnumOperand()) {
552 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
553 }
554}
John McCall51bd8032009-10-18 01:05:36 +0000555void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557}
John McCall49a832b2009-10-18 09:09:24 +0000558void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
559 SubstTemplateTypeParmTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000562void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
563 SubstTemplateTypeParmPackTypeLoc TL) {
564 Writer.AddSourceLocation(TL.getNameLoc(), Record);
565}
John McCall51bd8032009-10-18 01:05:36 +0000566void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
567 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000568 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000569 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
570 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
571 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
572 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000573 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
574 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000575}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000576void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
577 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
578 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
579}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000580void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000581 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000582 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000583}
John McCall3cb0ebd2010-03-10 03:28:59 +0000584void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getNameLoc(), Record);
586}
Douglas Gregor4714c122010-03-31 17:34:00 +0000587void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000588 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000589 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000590 Writer.AddSourceLocation(TL.getNameLoc(), Record);
591}
John McCall33500952010-06-11 00:33:02 +0000592void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
593 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000594 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000595 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000596 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000597 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000598 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
599 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
600 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000601 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
602 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000603}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000604void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
605 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
606}
John McCall51bd8032009-10-18 01:05:36 +0000607void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
608 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000609}
610void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
611 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000612 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
613 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
614 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
615 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000616}
John McCall54e14c42009-10-22 22:37:11 +0000617void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
618 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000619}
Eli Friedmanb001de72011-10-06 23:00:33 +0000620void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
621 Writer.AddSourceLocation(TL.getKWLoc(), Record);
622 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
623 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
624}
John McCalla1ee0c52009-10-16 21:56:05 +0000625
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000626//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000627// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000628//===----------------------------------------------------------------------===//
629
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000630static void EmitBlockID(unsigned ID, const char *Name,
631 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000632 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 Record.clear();
634 Record.push_back(ID);
635 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
636
637 // Emit the block name if present.
638 if (Name == 0 || Name[0] == 0) return;
639 Record.clear();
640 while (*Name)
641 Record.push_back(*Name++);
642 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
643}
644
645static void EmitRecordID(unsigned ID, const char *Name,
646 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000647 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000648 Record.clear();
649 Record.push_back(ID);
650 while (*Name)
651 Record.push_back(*Name++);
652 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000653}
654
655static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000656 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000657#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000658 RECORD(STMT_STOP);
659 RECORD(STMT_NULL_PTR);
660 RECORD(STMT_NULL);
661 RECORD(STMT_COMPOUND);
662 RECORD(STMT_CASE);
663 RECORD(STMT_DEFAULT);
664 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000665 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000666 RECORD(STMT_IF);
667 RECORD(STMT_SWITCH);
668 RECORD(STMT_WHILE);
669 RECORD(STMT_DO);
670 RECORD(STMT_FOR);
671 RECORD(STMT_GOTO);
672 RECORD(STMT_INDIRECT_GOTO);
673 RECORD(STMT_CONTINUE);
674 RECORD(STMT_BREAK);
675 RECORD(STMT_RETURN);
676 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000677 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000678 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000679 RECORD(EXPR_PREDEFINED);
680 RECORD(EXPR_DECL_REF);
681 RECORD(EXPR_INTEGER_LITERAL);
682 RECORD(EXPR_FLOATING_LITERAL);
683 RECORD(EXPR_IMAGINARY_LITERAL);
684 RECORD(EXPR_STRING_LITERAL);
685 RECORD(EXPR_CHARACTER_LITERAL);
686 RECORD(EXPR_PAREN);
687 RECORD(EXPR_UNARY_OPERATOR);
688 RECORD(EXPR_SIZEOF_ALIGN_OF);
689 RECORD(EXPR_ARRAY_SUBSCRIPT);
690 RECORD(EXPR_CALL);
691 RECORD(EXPR_MEMBER);
692 RECORD(EXPR_BINARY_OPERATOR);
693 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
694 RECORD(EXPR_CONDITIONAL_OPERATOR);
695 RECORD(EXPR_IMPLICIT_CAST);
696 RECORD(EXPR_CSTYLE_CAST);
697 RECORD(EXPR_COMPOUND_LITERAL);
698 RECORD(EXPR_EXT_VECTOR_ELEMENT);
699 RECORD(EXPR_INIT_LIST);
700 RECORD(EXPR_DESIGNATED_INIT);
701 RECORD(EXPR_IMPLICIT_VALUE_INIT);
702 RECORD(EXPR_VA_ARG);
703 RECORD(EXPR_ADDR_LABEL);
704 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000705 RECORD(EXPR_CHOOSE);
706 RECORD(EXPR_GNU_NULL);
707 RECORD(EXPR_SHUFFLE_VECTOR);
708 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000709 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000710 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000711 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000712 RECORD(EXPR_OBJC_ARRAY_LITERAL);
713 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000714 RECORD(EXPR_OBJC_ENCODE);
715 RECORD(EXPR_OBJC_SELECTOR_EXPR);
716 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
717 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
718 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
719 RECORD(EXPR_OBJC_KVC_REF_EXPR);
720 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000721 RECORD(STMT_OBJC_FOR_COLLECTION);
722 RECORD(STMT_OBJC_CATCH);
723 RECORD(STMT_OBJC_FINALLY);
724 RECORD(STMT_OBJC_AT_TRY);
725 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
726 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000727 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000728 RECORD(EXPR_CXX_OPERATOR_CALL);
729 RECORD(EXPR_CXX_CONSTRUCT);
730 RECORD(EXPR_CXX_STATIC_CAST);
731 RECORD(EXPR_CXX_DYNAMIC_CAST);
732 RECORD(EXPR_CXX_REINTERPRET_CAST);
733 RECORD(EXPR_CXX_CONST_CAST);
734 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000735 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000736 RECORD(EXPR_CXX_BOOL_LITERAL);
737 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000738 RECORD(EXPR_CXX_TYPEID_EXPR);
739 RECORD(EXPR_CXX_TYPEID_TYPE);
740 RECORD(EXPR_CXX_UUIDOF_EXPR);
741 RECORD(EXPR_CXX_UUIDOF_TYPE);
742 RECORD(EXPR_CXX_THIS);
743 RECORD(EXPR_CXX_THROW);
744 RECORD(EXPR_CXX_DEFAULT_ARG);
745 RECORD(EXPR_CXX_BIND_TEMPORARY);
746 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
747 RECORD(EXPR_CXX_NEW);
748 RECORD(EXPR_CXX_DELETE);
749 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
750 RECORD(EXPR_EXPR_WITH_CLEANUPS);
751 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
752 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
753 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
754 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
755 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
756 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
757 RECORD(EXPR_CXX_NOEXCEPT);
758 RECORD(EXPR_OPAQUE_VALUE);
759 RECORD(EXPR_BINARY_TYPE_TRAIT);
760 RECORD(EXPR_PACK_EXPANSION);
761 RECORD(EXPR_SIZEOF_PACK);
762 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000763 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000764#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000765}
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Sebastian Redla4232eb2010-08-18 23:56:21 +0000767void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000768 RecordData Record;
769 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000771#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
772#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000774 // Control Block.
775 BLOCK(CONTROL_BLOCK);
776 RECORD(METADATA);
777 RECORD(IMPORTS);
778 RECORD(LANGUAGE_OPTIONS);
779 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000780 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000781 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000782 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000783 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000784 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000785 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000786 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000787 RECORD(PREPROCESSOR_OPTIONS);
788
Douglas Gregorc337fef2012-10-19 00:45:00 +0000789 BLOCK(INPUT_FILES_BLOCK);
790 RECORD(INPUT_FILE);
791
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000792 // AST Top-Level Block.
793 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000794 RECORD(TYPE_OFFSET);
795 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000796 RECORD(IDENTIFIER_OFFSET);
797 RECORD(IDENTIFIER_TABLE);
798 RECORD(EXTERNAL_DEFINITIONS);
799 RECORD(SPECIAL_TYPES);
800 RECORD(STATISTICS);
801 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000802 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000803 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000804 RECORD(SELECTOR_OFFSETS);
805 RECORD(METHOD_POOL);
806 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000807 RECORD(SOURCE_LOCATION_OFFSETS);
808 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000809 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000810 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000811 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000812 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000813 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000814 RECORD(SEMA_DECL_REFS);
815 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
816 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
817 RECORD(DECL_REPLACEMENTS);
818 RECORD(UPDATE_VISIBLE);
819 RECORD(DECL_UPDATE_OFFSETS);
820 RECORD(DECL_UPDATES);
821 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
822 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000823 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000824 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000825 RECORD(FP_PRAGMA_OPTIONS);
826 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000827 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000828 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000829 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000830 RECORD(MODULE_OFFSET_MAP);
831 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000832 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000833 RECORD(FILE_SORTED_DECLS);
834 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000835 RECORD(MERGED_DECLARATIONS);
836 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000837 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000838 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000839 RECORD(MACRO_TABLE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000840
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000841 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000842 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000843 RECORD(SM_SLOC_FILE_ENTRY);
844 RECORD(SM_SLOC_BUFFER_ENTRY);
845 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000846 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000848 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000849 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000850 RECORD(PP_MACRO_OBJECT_LIKE);
851 RECORD(PP_MACRO_FUNCTION_LIKE);
852 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000853
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000854 // Decls and Types block.
855 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000856 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000857 RECORD(TYPE_COMPLEX);
858 RECORD(TYPE_POINTER);
859 RECORD(TYPE_BLOCK_POINTER);
860 RECORD(TYPE_LVALUE_REFERENCE);
861 RECORD(TYPE_RVALUE_REFERENCE);
862 RECORD(TYPE_MEMBER_POINTER);
863 RECORD(TYPE_CONSTANT_ARRAY);
864 RECORD(TYPE_INCOMPLETE_ARRAY);
865 RECORD(TYPE_VARIABLE_ARRAY);
866 RECORD(TYPE_VECTOR);
867 RECORD(TYPE_EXT_VECTOR);
868 RECORD(TYPE_FUNCTION_PROTO);
869 RECORD(TYPE_FUNCTION_NO_PROTO);
870 RECORD(TYPE_TYPEDEF);
871 RECORD(TYPE_TYPEOF_EXPR);
872 RECORD(TYPE_TYPEOF);
873 RECORD(TYPE_RECORD);
874 RECORD(TYPE_ENUM);
875 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000876 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000877 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000878 RECORD(TYPE_DECLTYPE);
879 RECORD(TYPE_ELABORATED);
880 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
881 RECORD(TYPE_UNRESOLVED_USING);
882 RECORD(TYPE_INJECTED_CLASS_NAME);
883 RECORD(TYPE_OBJC_OBJECT);
884 RECORD(TYPE_TEMPLATE_TYPE_PARM);
885 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
886 RECORD(TYPE_DEPENDENT_NAME);
887 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
888 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
889 RECORD(TYPE_PAREN);
890 RECORD(TYPE_PACK_EXPANSION);
891 RECORD(TYPE_ATTRIBUTED);
892 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000893 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000894 RECORD(DECL_TYPEDEF);
895 RECORD(DECL_ENUM);
896 RECORD(DECL_RECORD);
897 RECORD(DECL_ENUM_CONSTANT);
898 RECORD(DECL_FUNCTION);
899 RECORD(DECL_OBJC_METHOD);
900 RECORD(DECL_OBJC_INTERFACE);
901 RECORD(DECL_OBJC_PROTOCOL);
902 RECORD(DECL_OBJC_IVAR);
903 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000904 RECORD(DECL_OBJC_CATEGORY);
905 RECORD(DECL_OBJC_CATEGORY_IMPL);
906 RECORD(DECL_OBJC_IMPLEMENTATION);
907 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
908 RECORD(DECL_OBJC_PROPERTY);
909 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000910 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +0000911 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000912 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000913 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000914 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000915 RECORD(DECL_FILE_SCOPE_ASM);
916 RECORD(DECL_BLOCK);
917 RECORD(DECL_CONTEXT_LEXICAL);
918 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000919 RECORD(DECL_NAMESPACE);
920 RECORD(DECL_NAMESPACE_ALIAS);
921 RECORD(DECL_USING);
922 RECORD(DECL_USING_SHADOW);
923 RECORD(DECL_USING_DIRECTIVE);
924 RECORD(DECL_UNRESOLVED_USING_VALUE);
925 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
926 RECORD(DECL_LINKAGE_SPEC);
927 RECORD(DECL_CXX_RECORD);
928 RECORD(DECL_CXX_METHOD);
929 RECORD(DECL_CXX_CONSTRUCTOR);
930 RECORD(DECL_CXX_DESTRUCTOR);
931 RECORD(DECL_CXX_CONVERSION);
932 RECORD(DECL_ACCESS_SPEC);
933 RECORD(DECL_FRIEND);
934 RECORD(DECL_FRIEND_TEMPLATE);
935 RECORD(DECL_CLASS_TEMPLATE);
936 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
937 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
938 RECORD(DECL_FUNCTION_TEMPLATE);
939 RECORD(DECL_TEMPLATE_TYPE_PARM);
940 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
941 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
942 RECORD(DECL_STATIC_ASSERT);
943 RECORD(DECL_CXX_BASE_SPECIFIERS);
944 RECORD(DECL_INDIRECTFIELD);
945 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
946
Douglas Gregora72d8c42011-06-03 02:27:19 +0000947 // Statements and Exprs can occur in the Decls and Types block.
948 AddStmtsExprs(Stream, Record);
949
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000950 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000951 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000952 RECORD(PPD_MACRO_DEFINITION);
953 RECORD(PPD_INCLUSION_DIRECTIVE);
954
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000955#undef RECORD
956#undef BLOCK
957 Stream.ExitBlock();
958}
959
Douglas Gregore650c8c2009-07-07 00:12:59 +0000960/// \brief Adjusts the given filename to only write out the portion of the
961/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000962///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000963/// \param Filename the file name to adjust.
964///
965/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
966/// the returned filename will be adjusted by this system root.
967///
968/// \returns either the original filename (if it needs no adjustment) or the
969/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000970static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000971adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Douglas Gregor832d6202011-07-22 16:35:34 +0000974 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 // Verify that the filename and the system root have the same prefix.
978 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000979 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000980 if (Filename[Pos] != isysroot[Pos])
981 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 // We hit the end of the filename before we hit the end of the system root.
984 if (!Filename[Pos])
985 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Douglas Gregore650c8c2009-07-07 00:12:59 +0000987 // If the file name has a '/' at the current position, skip over the '/'.
988 // We distinguish sysroot-based includes from absolute includes by the
989 // absence of '/' at the beginning of sysroot-based includes.
990 if (Filename[Pos] == '/')
991 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregore650c8c2009-07-07 00:12:59 +0000993 return Filename + Pos;
994}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000995
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000996/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000997void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
998 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000999 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001000 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001001 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1002 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001003
Douglas Gregore650c8c2009-07-07 00:12:59 +00001004 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001005 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1011 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1012 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1013 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1014 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1015 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001016 Record.push_back(VERSION_MAJOR);
1017 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001018 Record.push_back(CLANG_VERSION_MAJOR);
1019 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001020 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001021 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001022 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1023 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001024
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001025 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001026 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001027 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001028 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001029 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001030
1031 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1032 M != MEnd; ++M) {
1033 // Skip modules that weren't directly imported.
1034 if (!(*M)->isDirectlyImported())
1035 continue;
1036
1037 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001038 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001039 Record.push_back((*M)->File->getSize());
1040 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001041 // FIXME: This writes the absolute path for AST files we depend on.
1042 const std::string &FileName = (*M)->FileName;
1043 Record.push_back(FileName.size());
1044 Record.append(FileName.begin(), FileName.end());
1045 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001046 Stream.EmitRecord(IMPORTS, Record);
1047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001049 // Language options.
1050 Record.clear();
1051 const LangOptions &LangOpts = Context.getLangOpts();
1052#define LANGOPT(Name, Bits, Default, Description) \
1053 Record.push_back(LangOpts.Name);
1054#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1055 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1056#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001057#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1058#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001059
1060 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1061 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1062
1063 Record.push_back(LangOpts.CurrentModule.size());
1064 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001065
1066 // Comment options.
1067 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1068 for (CommentOptions::BlockCommandNamesTy::const_iterator
1069 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1070 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1071 I != IEnd; ++I) {
1072 AddString(*I, Record);
1073 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001074 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001075
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001076 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1077
Douglas Gregoree097c12012-10-18 17:58:09 +00001078 // Target options.
1079 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001080 const TargetInfo &Target = Context.getTargetInfo();
1081 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001082 AddString(TargetOpts.Triple, Record);
1083 AddString(TargetOpts.CPU, Record);
1084 AddString(TargetOpts.ABI, Record);
1085 AddString(TargetOpts.CXXABI, Record);
1086 AddString(TargetOpts.LinkerVersion, Record);
1087 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1088 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1089 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1090 }
1091 Record.push_back(TargetOpts.Features.size());
1092 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1093 AddString(TargetOpts.Features[I], Record);
1094 }
1095 Stream.EmitRecord(TARGET_OPTIONS, Record);
1096
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001097 // Diagnostic options.
1098 Record.clear();
1099 const DiagnosticOptions &DiagOpts
1100 = Context.getDiagnostics().getDiagnosticOptions();
1101#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1102#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1103 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1104#include "clang/Basic/DiagnosticOptions.def"
1105 Record.push_back(DiagOpts.Warnings.size());
1106 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1107 AddString(DiagOpts.Warnings[I], Record);
1108 // Note: we don't serialize the log or serialization file names, because they
1109 // are generally transient files and will almost always be overridden.
1110 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1111
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001112 // File system options.
1113 Record.clear();
1114 const FileSystemOptions &FSOpts
1115 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1116 AddString(FSOpts.WorkingDir, Record);
1117 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1118
Douglas Gregorbbf38312012-10-24 16:50:34 +00001119 // Header search options.
1120 Record.clear();
1121 const HeaderSearchOptions &HSOpts
1122 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1123 AddString(HSOpts.Sysroot, Record);
1124
1125 // Include entries.
1126 Record.push_back(HSOpts.UserEntries.size());
1127 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1128 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1129 AddString(Entry.Path, Record);
1130 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001131 Record.push_back(Entry.IsFramework);
1132 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001133 }
1134
1135 // System header prefixes.
1136 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1137 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1138 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1139 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1140 }
1141
1142 AddString(HSOpts.ResourceDir, Record);
1143 AddString(HSOpts.ModuleCachePath, Record);
1144 Record.push_back(HSOpts.DisableModuleHash);
1145 Record.push_back(HSOpts.UseBuiltinIncludes);
1146 Record.push_back(HSOpts.UseStandardSystemIncludes);
1147 Record.push_back(HSOpts.UseStandardCXXIncludes);
1148 Record.push_back(HSOpts.UseLibcxx);
1149 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1150
Douglas Gregora71a7d82012-10-24 20:05:57 +00001151 // Preprocessor options.
1152 Record.clear();
1153 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1154
1155 // Macro definitions.
1156 Record.push_back(PPOpts.Macros.size());
1157 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1158 AddString(PPOpts.Macros[I].first, Record);
1159 Record.push_back(PPOpts.Macros[I].second);
1160 }
1161
1162 // Includes
1163 Record.push_back(PPOpts.Includes.size());
1164 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1165 AddString(PPOpts.Includes[I], Record);
1166
1167 // Macro includes
1168 Record.push_back(PPOpts.MacroIncludes.size());
1169 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1170 AddString(PPOpts.MacroIncludes[I], Record);
1171
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001172 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001173 // Detailed record is important since it is used for the module cache hash.
1174 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001175 AddString(PPOpts.ImplicitPCHInclude, Record);
1176 AddString(PPOpts.ImplicitPTHInclude, Record);
1177 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1178 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1179
Douglas Gregor31d375f2011-05-06 21:43:30 +00001180 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001181 SourceManager &SM = Context.getSourceManager();
1182 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1183 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001184 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1185 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001186 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1187 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1188
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001189 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001191 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001192
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001193 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001194 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001195 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001196 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001197 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001198 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001199 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001200 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001201
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001202 Record.clear();
1203 Record.push_back(SM.getMainFileID().getOpaqueValue());
1204 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1205
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001206 // Original PCH directory
1207 if (!OutputFile.empty() && OutputFile != "-") {
1208 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1209 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1211 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1212
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001213 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001214
1215 llvm::sys::fs::make_absolute(OutputPath);
1216 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1217
1218 RecordData Record;
1219 Record.push_back(ORIGINAL_PCH_DIR);
1220 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1221 }
1222
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001223 WriteInputFiles(Context.SourceMgr,
1224 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1225 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001226 Stream.ExitBlock();
1227}
1228
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001229namespace {
1230 /// \brief An input file.
1231 struct InputFileEntry {
1232 const FileEntry *File;
1233 bool IsSystemFile;
1234 bool BufferOverridden;
1235 };
1236}
1237
1238void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1239 HeaderSearchOptions &HSOpts,
1240 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001241 using namespace llvm;
1242 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1243 RecordData Record;
1244
1245 // Create input-file abbreviation.
1246 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1247 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001248 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001249 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1250 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001251 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001252 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1253 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1254
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001255 // Get all ContentCache objects for files, sorted by whether the file is a
1256 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001257 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001258 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1259 // Get this source location entry.
1260 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001261 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001262
1263 // We only care about file entries that were not overridden.
1264 if (!SLoc->isFile())
1265 continue;
1266 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001267 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001268 continue;
1269
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001270 InputFileEntry Entry;
1271 Entry.File = Cache->OrigEntry;
1272 Entry.IsSystemFile = Cache->IsSystemFile;
1273 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001274 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001275 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001276 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001277 SortedFiles.push_front(Entry);
1278 }
1279
1280 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1281 // the set of (non-system) input files. This is simple heuristic for
1282 // detecting whether the system headers may have changed, because it is too
1283 // expensive to stat() all of the system headers.
1284 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001285 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001286 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1287 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1288 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1289 InputFileEntry Entry = { SDKSettingsFile, false, false };
1290 SortedFiles.push_front(Entry);
1291 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001292 }
1293
1294 unsigned UserFilesNum = 0;
1295 // Write out all of the input files.
1296 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001297 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001298 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001299 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001300
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001301 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001302 if (InputFileID != 0)
1303 continue; // already recorded this file.
1304
Douglas Gregora930dc92012-10-22 18:42:04 +00001305 // Record this entry's offset.
1306 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001307
1308 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001309
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001310 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001311 ++UserFilesNum;
1312
Douglas Gregor745e6f12012-10-19 00:38:02 +00001313 Record.clear();
1314 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001315 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001316
1317 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001318 Record.push_back(Entry.File->getSize());
1319 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001320
Douglas Gregora930dc92012-10-22 18:42:04 +00001321 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001322 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001323
Douglas Gregor745e6f12012-10-19 00:38:02 +00001324 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001325 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001326 SmallString<128> FilePath(Filename);
1327
1328 // Ask the file manager to fixup the relative path for us. This will
1329 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001330 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001331
1332 // FIXME: This call to make_absolute shouldn't be necessary, the
1333 // call to FixupRelativePath should always return an absolute path.
1334 llvm::sys::fs::make_absolute(FilePath);
1335 Filename = FilePath.c_str();
1336
1337 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1338
1339 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1340 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001341
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001342 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001343
1344 // Create input file offsets abbreviation.
1345 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1346 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1347 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001348 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1349 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001350 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1351 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1352
1353 // Write input file offsets.
1354 Record.clear();
1355 Record.push_back(INPUT_FILE_OFFSETS);
1356 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001357 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001358 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001359}
1360
Douglas Gregor14f79002009-04-10 03:52:48 +00001361//===----------------------------------------------------------------------===//
1362// Source Manager Serialization
1363//===----------------------------------------------------------------------===//
1364
1365/// \brief Create an abbreviation for the SLocEntry that refers to a
1366/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001367static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001368 using namespace llvm;
1369 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001370 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001375 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001380 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001381}
1382
1383/// \brief Create an abbreviation for the SLocEntry that refers to a
1384/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001385static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001386 using namespace llvm;
1387 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001388 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001394 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001395}
1396
1397/// \brief Create an abbreviation for the SLocEntry that refers to a
1398/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001399static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001400 using namespace llvm;
1401 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001402 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001404 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001405}
1406
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001407/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1408/// expansion.
1409static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001410 using namespace llvm;
1411 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001412 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001418 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001419}
1420
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001421namespace {
1422 // Trait used for the on-disk hash table of header search information.
1423 class HeaderFileInfoTrait {
1424 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001425 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001426
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001427 // Keep track of the framework names we've used during serialization.
1428 SmallVector<char, 128> FrameworkStringData;
1429 llvm::StringMap<unsigned> FrameworkNameOffset;
1430
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001431 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001432 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1433 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001434
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001435 struct key_type {
1436 const FileEntry *FE;
1437 const char *Filename;
1438 };
1439 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001440
1441 typedef HeaderFileInfo data_type;
1442 typedef const data_type &data_type_ref;
1443
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001444 static unsigned ComputeHash(key_type_ref key) {
1445 // The hash is based only on size/time of the file, so that the reader can
1446 // match even when symlinking or excess path elements ("foo/../", "../")
1447 // change the form of the name. However, complete path is still the key.
1448 return llvm::hash_combine(key.FE->getSize(),
1449 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001450 }
1451
1452 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001453 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1454 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1455 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001456 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001457 if (Data.isModuleHeader)
1458 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001459 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001460 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001461 }
1462
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001463 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1464 clang::io::Emit64(Out, key.FE->getSize());
1465 KeyLen -= 8;
1466 clang::io::Emit64(Out, key.FE->getModificationTime());
1467 KeyLen -= 8;
1468 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001469 }
1470
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001471 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001472 data_type_ref Data, unsigned DataLen) {
1473 using namespace clang::io;
1474 uint64_t Start = Out.tell(); (void)Start;
1475
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001476 unsigned char Flags = (Data.isImport << 5)
1477 | (Data.isPragmaOnce << 4)
1478 | (Data.DirInfo << 2)
1479 | (Data.Resolved << 1)
1480 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001481 Emit8(Out, (uint8_t)Flags);
1482 Emit16(Out, (uint16_t) Data.NumIncludes);
1483
1484 if (!Data.ControllingMacro)
1485 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1486 else
1487 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001488
1489 unsigned Offset = 0;
1490 if (!Data.Framework.empty()) {
1491 // If this header refers into a framework, save the framework name.
1492 llvm::StringMap<unsigned>::iterator Pos
1493 = FrameworkNameOffset.find(Data.Framework);
1494 if (Pos == FrameworkNameOffset.end()) {
1495 Offset = FrameworkStringData.size() + 1;
1496 FrameworkStringData.append(Data.Framework.begin(),
1497 Data.Framework.end());
1498 FrameworkStringData.push_back(0);
1499
1500 FrameworkNameOffset[Data.Framework] = Offset;
1501 } else
1502 Offset = Pos->second;
1503 }
1504 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001505
1506 if (Data.isModuleHeader) {
1507 Module *Mod = HS.findModuleForHeader(key.FE);
1508 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1509 }
1510
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001511 assert(Out.tell() - Start == DataLen && "Wrong data length");
1512 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001513
1514 const char *strings_begin() const { return FrameworkStringData.begin(); }
1515 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001516 };
1517} // end anonymous namespace
1518
1519/// \brief Write the header search block for the list of files that
1520///
1521/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001522void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001523 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001524 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1525
1526 if (FilesByUID.size() > HS.header_file_size())
1527 FilesByUID.resize(HS.header_file_size());
1528
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001529 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001530 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001531 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001532 unsigned NumHeaderSearchEntries = 0;
1533 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1534 const FileEntry *File = FilesByUID[UID];
1535 if (!File)
1536 continue;
1537
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001538 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1539 // from the external source if it was not provided already.
1540 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001541 if (HFI.External && Chain)
1542 continue;
1543
1544 // Turn the file name into an absolute path, if it isn't already.
1545 const char *Filename = File->getName();
1546 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1547
1548 // If we performed any translation on the file name at all, we need to
1549 // save this string, since the generator will refer to it later.
1550 if (Filename != File->getName()) {
1551 Filename = strdup(Filename);
1552 SavedStrings.push_back(Filename);
1553 }
1554
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001555 HeaderFileInfoTrait::key_type key = { File, Filename };
1556 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001557 ++NumHeaderSearchEntries;
1558 }
1559
1560 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001561 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001562 uint32_t BucketOffset;
1563 {
1564 llvm::raw_svector_ostream Out(TableData);
1565 // Make sure that no bucket is at offset 0
1566 clang::io::Emit32(Out, 0);
1567 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1568 }
1569
1570 // Create a blob abbreviation
1571 using namespace llvm;
1572 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1573 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1578 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1579
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001580 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001581 RecordData Record;
1582 Record.push_back(HEADER_SEARCH_TABLE);
1583 Record.push_back(BucketOffset);
1584 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001585 Record.push_back(TableData.size());
1586 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001587 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1588
1589 // Free all of the strings we had to duplicate.
1590 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001591 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001592}
1593
Douglas Gregor14f79002009-04-10 03:52:48 +00001594/// \brief Writes the block containing the serialized form of the
1595/// source manager.
1596///
1597/// TODO: We should probably use an on-disk hash table (stored in a
1598/// blob), indexed based on the file name, so that we only create
1599/// entries for files that we actually need. In the common case (no
1600/// errors), we probably won't have to create file entries for any of
1601/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001602void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001603 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001604 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001605 RecordData Record;
1606
Chris Lattnerf04ad692009-04-10 17:16:57 +00001607 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001608 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001609
1610 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001611 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1612 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1613 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001614 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001615
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001616 // Write out the source location entry table. We skip the first
1617 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001618 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001619 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001620 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1621 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001622 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001623 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001624 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001625 FileID FID = FileID::get(I);
1626 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001627
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001628 // Record the offset of this source-location entry.
1629 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1630
1631 // Figure out which record code to use.
1632 unsigned Code;
1633 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001634 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1635 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001636 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001637 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001638 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001639 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001640 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001641 Record.clear();
1642 Record.push_back(Code);
1643
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001644 // Starting offset of this entry within this module, so skip the dummy.
1645 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001646 if (SLoc->isFile()) {
1647 const SrcMgr::FileInfo &File = SLoc->getFile();
1648 Record.push_back(File.getIncludeLoc().getRawEncoding());
1649 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1650 Record.push_back(File.hasLineDirectives());
1651
1652 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001653 if (Content->OrigEntry) {
1654 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001655 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001656
Douglas Gregora930dc92012-10-22 18:42:04 +00001657 // The source location entry is a file. Emit input file ID.
1658 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1659 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001661 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001662
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001663 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001664 if (FDI != FileDeclIDs.end()) {
1665 Record.push_back(FDI->second->FirstDeclIndex);
1666 Record.push_back(FDI->second->DeclIDs.size());
1667 } else {
1668 Record.push_back(0);
1669 Record.push_back(0);
1670 }
Douglas Gregora081da52011-11-16 20:05:18 +00001671
Douglas Gregora930dc92012-10-22 18:42:04 +00001672 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001673
1674 if (Content->BufferOverridden) {
1675 Record.clear();
1676 Record.push_back(SM_SLOC_BUFFER_BLOB);
1677 const llvm::MemoryBuffer *Buffer
1678 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1679 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1680 StringRef(Buffer->getBufferStart(),
1681 Buffer->getBufferSize() + 1));
1682 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001683 } else {
1684 // The source location entry is a buffer. The blob associated
1685 // with this entry contains the contents of the buffer.
1686
1687 // We add one to the size so that we capture the trailing NULL
1688 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1689 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001690 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001691 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001692 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001693 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001694 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001695 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001696 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001697 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001698 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001699 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001700
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001701 if (strcmp(Name, "<built-in>") == 0) {
1702 PreloadSLocs.push_back(SLocEntryOffsets.size());
1703 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001704 }
1705 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001706 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001707 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001708 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1709 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001710 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1711 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001712
1713 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001714 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001715 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001716 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001717 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001718 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001719 }
1720 }
1721
Douglas Gregorc9490c02009-04-16 22:23:12 +00001722 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001723
1724 if (SLocEntryOffsets.empty())
1725 return;
1726
Sebastian Redl3397c552010-08-18 23:56:27 +00001727 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001728 // table is used for lazily loading source-location information.
1729 using namespace llvm;
1730 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001731 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1735 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001737 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001738 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001739 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001740 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001741 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001742
Sebastian Redl3397c552010-08-18 23:56:27 +00001743 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001744 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001745 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001746
1747 // Write the line table. It depends on remapping working, so it must come
1748 // after the source location offsets.
1749 if (SourceMgr.hasLineTable()) {
1750 LineTableInfo &LineTable = SourceMgr.getLineTable();
1751
1752 Record.clear();
1753 // Emit the file names
1754 Record.push_back(LineTable.getNumFilenames());
1755 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1756 // Emit the file name
1757 const char *Filename = LineTable.getFilename(I);
1758 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1759 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1760 Record.push_back(FilenameLen);
1761 if (FilenameLen)
1762 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1763 }
1764
1765 // Emit the line entries
1766 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1767 L != LEnd; ++L) {
1768 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001769 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001770 continue;
1771
1772 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001773 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001774
1775 // Emit the line entries
1776 Record.push_back(L->second.size());
1777 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1778 LEEnd = L->second.end();
1779 LE != LEEnd; ++LE) {
1780 Record.push_back(LE->FileOffset);
1781 Record.push_back(LE->LineNo);
1782 Record.push_back(LE->FilenameID);
1783 Record.push_back((unsigned)LE->FileKind);
1784 Record.push_back(LE->IncludeOffset);
1785 }
1786 }
1787 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1788 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001789}
1790
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001791//===----------------------------------------------------------------------===//
1792// Preprocessor Serialization
1793//===----------------------------------------------------------------------===//
1794
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001795namespace {
1796class ASTMacroTableTrait {
1797public:
1798 typedef IdentID key_type;
1799 typedef key_type key_type_ref;
1800
1801 struct Data {
1802 uint32_t MacroDirectivesOffset;
1803 };
1804
1805 typedef Data data_type;
1806 typedef const data_type &data_type_ref;
1807
1808 static unsigned ComputeHash(IdentID IdID) {
1809 return llvm::hash_value(IdID);
1810 }
1811
1812 std::pair<unsigned,unsigned>
1813 static EmitKeyDataLength(raw_ostream& Out,
1814 key_type_ref Key, data_type_ref Data) {
1815 unsigned KeyLen = 4; // IdentID.
1816 unsigned DataLen = 4; // MacroDirectivesOffset.
1817 return std::make_pair(KeyLen, DataLen);
1818 }
1819
1820 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1821 clang::io::Emit32(Out, Key);
1822 }
1823
1824 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1825 unsigned) {
1826 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1827 }
1828};
1829} // end anonymous namespace
1830
1831static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1832 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1833 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1834 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1835 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001836 return X.first->getName().compare(Y.first->getName());
1837}
1838
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001839static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1840 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001841 if (MacroInfo *MI = MD->getMacroInfo())
1842 if (MI->isBuiltinMacro())
1843 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001844
1845 if (IsModule) {
1846 SourceLocation Loc = MD->getLocation();
1847 if (Loc.isInvalid())
1848 return true;
1849 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1850 return true;
1851 }
1852
1853 return false;
1854}
1855
Chris Lattner0b1fb982009-04-10 17:15:23 +00001856/// \brief Writes the block containing the serialized form of the
1857/// preprocessor.
1858///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001859void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001860 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1861 if (PPRec)
1862 WritePreprocessorDetail(*PPRec);
1863
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001864 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001865
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001866 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1867 if (PP.getCounterValue() != 0) {
1868 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001869 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001870 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001871 }
1872
1873 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001874 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Sebastian Redl3397c552010-08-18 23:56:27 +00001876 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001877 // FIXME: use diagnostics subsystem for localization etc.
1878 if (PP.SawDateOrTime())
1879 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Douglas Gregorecdcb882010-10-20 22:00:55 +00001881
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001882 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001883 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001884
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001885 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001886 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001887 MacroDirectives;
1888 for (Preprocessor::macro_iterator
1889 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1890 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001891 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001892 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001893 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001894
Douglas Gregor9c736102011-02-10 18:20:09 +00001895 // Sort the set of macro definitions that need to be serialized by the
1896 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001897 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1898 &compareMacroDirectives);
1899
1900 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1901
1902 // Emit the macro directives as a list and associate the offset with the
1903 // identifier they belong to.
1904 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1905 const IdentifierInfo *Name = MacroDirectives[I].first;
1906 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1907 MacroDirective *MD = MacroDirectives[I].second;
1908
1909 // If the macro or identifier need no updates, don't write the macro history
1910 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001911 // FIXME: Chain the macro history instead of re-writing it.
1912 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001913 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1914 continue;
1915
1916 // Emit the macro directives in reverse source order.
1917 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001918 if (MD->isHidden())
1919 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001920 if (shouldIgnoreMacro(MD, IsModule, PP))
1921 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001922
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001923 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001924 Record.push_back(MD->getKind());
1925 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1926 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1927 Record.push_back(InfoID);
1928 Record.push_back(DefMD->isImported());
1929 Record.push_back(DefMD->isAmbiguous());
1930
1931 } else if (VisibilityMacroDirective *
1932 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1933 Record.push_back(VisMD->isPublic());
1934 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001935 }
1936 if (Record.empty())
1937 continue;
1938
1939 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1940 Record.clear();
1941
1942 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1943
1944 IdentID NameID = getIdentifierRef(Name);
1945 ASTMacroTableTrait::Data data;
1946 data.MacroDirectivesOffset = MacroDirectiveOffset;
1947 Generator.insert(NameID, data);
1948 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001949
Douglas Gregora8235d62012-10-09 23:05:51 +00001950 /// \brief Offsets of each of the macros into the bitstream, indexed by
1951 /// the local macro ID
1952 ///
1953 /// For each identifier that is associated with a macro, this map
1954 /// provides the offset into the bitstream where that macro is
1955 /// defined.
1956 std::vector<uint32_t> MacroOffsets;
1957
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001958 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1959 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1960 MacroInfo *MI = MacroInfosToEmit[I].MI;
1961 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001962
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001963 if (ID < FirstMacroID) {
1964 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1965 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001966 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001967
1968 // Record the local offset of this macro.
1969 unsigned Index = ID - FirstMacroID;
1970 if (Index == MacroOffsets.size())
1971 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1972 else {
1973 if (Index > MacroOffsets.size())
1974 MacroOffsets.resize(Index + 1);
1975
1976 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1977 }
1978
1979 AddIdentifierRef(Name, Record);
1980 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1981 AddSourceLocation(MI->getDefinitionLoc(), Record);
1982 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1983 Record.push_back(MI->isUsed());
1984 unsigned Code;
1985 if (MI->isObjectLike()) {
1986 Code = PP_MACRO_OBJECT_LIKE;
1987 } else {
1988 Code = PP_MACRO_FUNCTION_LIKE;
1989
1990 Record.push_back(MI->isC99Varargs());
1991 Record.push_back(MI->isGNUVarargs());
1992 Record.push_back(MI->hasCommaPasting());
1993 Record.push_back(MI->getNumArgs());
1994 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1995 I != E; ++I)
1996 AddIdentifierRef(*I, Record);
1997 }
1998
1999 // If we have a detailed preprocessing record, record the macro definition
2000 // ID that corresponds to this macro.
2001 if (PPRec)
2002 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2003
2004 Stream.EmitRecord(Code, Record);
2005 Record.clear();
2006
2007 // Emit the tokens array.
2008 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2009 // Note that we know that the preprocessor does not have any annotation
2010 // tokens in it because they are created by the parser, and thus can't
2011 // be in a macro definition.
2012 const Token &Tok = MI->getReplacementToken(TokNo);
2013
2014 Record.push_back(Tok.getLocation().getRawEncoding());
2015 Record.push_back(Tok.getLength());
2016
2017 // FIXME: When reading literal tokens, reconstruct the literal pointer
2018 // if it is needed.
2019 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
2020 // FIXME: Should translate token kind to a stable encoding.
2021 Record.push_back(Tok.getKind());
2022 // FIXME: Should translate token flags to a stable encoding.
2023 Record.push_back(Tok.getFlags());
2024
2025 Stream.EmitRecord(PP_TOKEN, Record);
2026 Record.clear();
2027 }
2028 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002029 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002030
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002031 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002032
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002033 // Create the on-disk hash table in a buffer.
2034 SmallString<4096> MacroTable;
2035 uint32_t BucketOffset;
2036 {
2037 llvm::raw_svector_ostream Out(MacroTable);
2038 // Make sure that no bucket is at offset 0
2039 clang::io::Emit32(Out, 0);
2040 BucketOffset = Generator.Emit(Out);
2041 }
2042
2043 // Write the macro table
2044 using namespace llvm;
2045 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2046 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2049 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2050
2051 Record.push_back(MACRO_TABLE);
2052 Record.push_back(BucketOffset);
2053 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2054 Record.clear();
2055
Douglas Gregora8235d62012-10-09 23:05:51 +00002056 // Write the offsets table for macro IDs.
2057 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002058 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002059 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2063
2064 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2065 Record.clear();
2066 Record.push_back(MACRO_OFFSET);
2067 Record.push_back(MacroOffsets.size());
2068 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2069 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2070 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002071}
2072
2073void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002074 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002075 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002076
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002077 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002078
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002079 // Enter the preprocessor block.
2080 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002081
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002082 // If the preprocessor has a preprocessing record, emit it.
2083 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002084 using namespace llvm;
2085
2086 // Set up the abbreviation for
2087 unsigned InclusionAbbrev = 0;
2088 {
2089 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2090 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2092 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2096 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2097 }
2098
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002099 unsigned FirstPreprocessorEntityID
2100 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2101 + NUM_PREDEF_PP_ENTITY_IDS;
2102 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002103 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002104 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2105 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002106 E != EEnd;
2107 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002108 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002109
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002110 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2111 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002112
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002113 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002114 // Record this macro definition's ID.
2115 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002116
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002117 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002118 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2119 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002120 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002121
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002122 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002123 Record.push_back(ME->isBuiltinMacro());
2124 if (ME->isBuiltinMacro())
2125 AddIdentifierRef(ME->getName(), Record);
2126 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002127 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002128 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002129 continue;
2130 }
2131
2132 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2133 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002134 Record.push_back(ID->getFileName().size());
2135 Record.push_back(ID->wasInQuotes());
2136 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002137 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002138 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002139 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002140 // Check that the FileEntry is not null because it was not resolved and
2141 // we create a PCH even with compiler errors.
2142 if (ID->getFile())
2143 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002144 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2145 continue;
2146 }
2147
2148 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2149 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002150 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002151
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002152 // Write the offsets table for the preprocessing record.
2153 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002154 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2155
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002156 // Write the offsets table for identifier IDs.
2157 using namespace llvm;
2158 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002159 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002162 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002163
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002164 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002165 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002166 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002167 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2168 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002169 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002170}
2171
Douglas Gregore209e502011-12-06 01:10:29 +00002172unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2173 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2174 if (Known != SubmoduleIDs.end())
2175 return Known->second;
2176
2177 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2178}
2179
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002180unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2181 if (!Mod)
2182 return 0;
2183
2184 llvm::DenseMap<Module *, unsigned>::const_iterator
2185 Known = SubmoduleIDs.find(Mod);
2186 if (Known != SubmoduleIDs.end())
2187 return Known->second;
2188
2189 return 0;
2190}
2191
Douglas Gregor26ced122011-12-01 00:59:36 +00002192/// \brief Compute the number of modules within the given tree (including the
2193/// given module).
2194static unsigned getNumberOfModules(Module *Mod) {
2195 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002196 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2197 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002198 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002199 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002200
2201 return ChildModules + 1;
2202}
2203
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002204void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002205 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002206 // FIXME: This feels like it belongs somewhere else, but there are no
2207 // other consumers of this information.
2208 SourceManager &SrcMgr = PP->getSourceManager();
2209 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2210 for (ASTContext::import_iterator I = Context->local_import_begin(),
2211 IEnd = Context->local_import_end();
2212 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002213 if (Module *ImportedFrom
2214 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2215 SrcMgr))) {
2216 ImportedFrom->Imports.push_back(I->getImportedModule());
2217 }
2218 }
2219
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002220 // Enter the submodule description block.
2221 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2222
2223 // Write the abbreviations needed for the submodules block.
2224 using namespace llvm;
2225 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2226 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2237 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2238
2239 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002240 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2242 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2243
2244 Abbrev = new BitCodeAbbrev();
2245 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2247 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002248
2249 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002250 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2252 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2253
2254 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002255 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2257 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2258
Douglas Gregor51f564f2011-12-31 04:05:44 +00002259 Abbrev = new BitCodeAbbrev();
2260 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2262 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2263
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002264 Abbrev = new BitCodeAbbrev();
2265 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2267 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2268
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002269 Abbrev = new BitCodeAbbrev();
2270 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2273 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2274
Douglas Gregor63a72682013-03-20 00:22:05 +00002275 Abbrev = new BitCodeAbbrev();
2276 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2278 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2279
Douglas Gregor906d66a2013-03-20 21:10:35 +00002280 Abbrev = new BitCodeAbbrev();
2281 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2284 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2285
Douglas Gregor26ced122011-12-01 00:59:36 +00002286 // Write the submodule metadata block.
2287 RecordData Record;
2288 Record.push_back(getNumberOfModules(WritingModule));
2289 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2290 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2291
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002292 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002293 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002294 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002295 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002296 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002297 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002298 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002299
2300 // Emit the definition of the block.
2301 Record.clear();
2302 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002303 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002304 if (Mod->Parent) {
2305 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2306 Record.push_back(SubmoduleIDs[Mod->Parent]);
2307 } else {
2308 Record.push_back(0);
2309 }
2310 Record.push_back(Mod->IsFramework);
2311 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002312 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002313 Record.push_back(Mod->InferSubmodules);
2314 Record.push_back(Mod->InferExplicitSubmodules);
2315 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002316 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002317 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2318
Douglas Gregor51f564f2011-12-31 04:05:44 +00002319 // Emit the requirements.
2320 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2321 Record.clear();
2322 Record.push_back(SUBMODULE_REQUIRES);
2323 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2324 Mod->Requires[I].data(),
2325 Mod->Requires[I].size());
2326 }
2327
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002328 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002329 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002330 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002331 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002332 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002333 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002334 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2335 Record.clear();
2336 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2337 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2338 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002339 }
2340
2341 // Emit the headers.
2342 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2343 Record.clear();
2344 Record.push_back(SUBMODULE_HEADER);
2345 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2346 Mod->Headers[I]->getName());
2347 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002348 // Emit the excluded headers.
2349 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2350 Record.clear();
2351 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2352 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2353 Mod->ExcludedHeaders[I]->getName());
2354 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002355 ArrayRef<const FileEntry *>
2356 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2357 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002358 Record.clear();
2359 Record.push_back(SUBMODULE_TOPHEADER);
2360 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002361 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002362 }
Douglas Gregor55988682011-12-05 16:33:54 +00002363
2364 // Emit the imports.
2365 if (!Mod->Imports.empty()) {
2366 Record.clear();
2367 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002368 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002369 assert(ImportedID && "Unknown submodule!");
2370 Record.push_back(ImportedID);
2371 }
2372 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2373 }
2374
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002375 // Emit the exports.
2376 if (!Mod->Exports.empty()) {
2377 Record.clear();
2378 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002379 if (Module *Exported = Mod->Exports[I].getPointer()) {
2380 unsigned ExportedID = SubmoduleIDs[Exported];
2381 assert(ExportedID > 0 && "Unknown submodule ID?");
2382 Record.push_back(ExportedID);
2383 } else {
2384 Record.push_back(0);
2385 }
2386
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002387 Record.push_back(Mod->Exports[I].getInt());
2388 }
2389 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2390 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002391
2392 // Emit the link libraries.
2393 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2394 Record.clear();
2395 Record.push_back(SUBMODULE_LINK_LIBRARY);
2396 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2397 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2398 Mod->LinkLibraries[I].Library);
2399 }
2400
Douglas Gregor906d66a2013-03-20 21:10:35 +00002401 // Emit the conflicts.
2402 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2403 Record.clear();
2404 Record.push_back(SUBMODULE_CONFLICT);
2405 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2406 assert(OtherID && "Unknown submodule!");
2407 Record.push_back(OtherID);
2408 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2409 Mod->Conflicts[I].Message);
2410 }
2411
Douglas Gregor63a72682013-03-20 00:22:05 +00002412 // Emit the configuration macros.
2413 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2414 Record.clear();
2415 Record.push_back(SUBMODULE_CONFIG_MACRO);
2416 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2417 Mod->ConfigMacros[I]);
2418 }
2419
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002420 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002421 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2422 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002423 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002424 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002425 }
2426
2427 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002428
2429 assert((NextSubmoduleID - FirstSubmoduleID
2430 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002431}
2432
Douglas Gregor185dbd72011-12-01 02:07:58 +00002433serialization::SubmoduleID
2434ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002435 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002436 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002437
2438 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002439 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002440 Module *OwningMod
2441 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002442 if (!OwningMod)
2443 return 0;
2444
Douglas Gregore209e502011-12-06 01:10:29 +00002445 // Check whether this submodule is part of our own module.
2446 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002447 return 0;
2448
Douglas Gregore209e502011-12-06 01:10:29 +00002449 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002450}
2451
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002452void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2453 bool isModule) {
2454 // Make sure set diagnostic pragmas don't affect the translation unit that
2455 // imports the module.
2456 // FIXME: Make diagnostic pragma sections work properly with modules.
2457 if (isModule)
2458 return;
2459
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002460 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2461 DiagStateIDMap;
2462 unsigned CurrID = 0;
2463 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002464 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002465 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002466 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2467 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002468 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002469 if (point.Loc.isInvalid())
2470 continue;
2471
2472 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002473 unsigned &DiagStateID = DiagStateIDMap[point.State];
2474 Record.push_back(DiagStateID);
2475
2476 if (DiagStateID == 0) {
2477 DiagStateID = ++CurrID;
2478 for (DiagnosticsEngine::DiagState::const_iterator
2479 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2480 if (I->second.isPragma()) {
2481 Record.push_back(I->first);
2482 Record.push_back(I->second.getMapping());
2483 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002484 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002485 Record.push_back(-1); // mark the end of the diag/map pairs for this
2486 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002487 }
2488 }
2489
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002490 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002491 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002492}
2493
Anders Carlssonc8505782011-03-06 18:41:18 +00002494void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2495 if (CXXBaseSpecifiersOffsets.empty())
2496 return;
2497
2498 RecordData Record;
2499
2500 // Create a blob abbreviation for the C++ base specifiers offsets.
2501 using namespace llvm;
2502
2503 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2504 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2505 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2506 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2507 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2508
Douglas Gregore92b8a12011-08-04 00:01:48 +00002509 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002510 Record.clear();
2511 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2512 Record.push_back(CXXBaseSpecifiersOffsets.size());
2513 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002514 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002515}
2516
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002517//===----------------------------------------------------------------------===//
2518// Type Serialization
2519//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002520
Sebastian Redl3397c552010-08-18 23:56:27 +00002521/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002522void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002523 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002524 if (Idx.getIndex() == 0) // we haven't seen this type before.
2525 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002526
Douglas Gregor97475832010-10-05 18:37:06 +00002527 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002528
Douglas Gregor2cf26342009-04-09 22:27:44 +00002529 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002530 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002531 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002532 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002533 else if (TypeOffsets.size() < Index) {
2534 TypeOffsets.resize(Index + 1);
2535 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002536 }
2537
2538 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002539
Douglas Gregor2cf26342009-04-09 22:27:44 +00002540 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002541 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002542
Douglas Gregora4923eb2009-11-16 21:35:15 +00002543 if (T.hasLocalNonFastQualifiers()) {
2544 Qualifiers Qs = T.getLocalQualifiers();
2545 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002546 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002547 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002548 } else {
2549 switch (T->getTypeClass()) {
2550 // For all of the concrete, non-dependent types, call the
2551 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002552#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002553 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002554#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002555#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002556 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002557 }
2558
2559 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002560 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002561
2562 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002563 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002564}
2565
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002566//===----------------------------------------------------------------------===//
2567// Declaration Serialization
2568//===----------------------------------------------------------------------===//
2569
Douglas Gregor2cf26342009-04-09 22:27:44 +00002570/// \brief Write the block containing all of the declaration IDs
2571/// lexically declared within the given DeclContext.
2572///
2573/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2574/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002575uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002576 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002577 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002578 return 0;
2579
Douglas Gregorc9490c02009-04-16 22:23:12 +00002580 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002581 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002582 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002583 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002584 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2585 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002586 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002587
Douglas Gregor25123082009-04-22 22:34:57 +00002588 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002589 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002590 return Offset;
2591}
2592
Sebastian Redla4232eb2010-08-18 23:56:21 +00002593void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002594 using namespace llvm;
2595 RecordData Record;
2596
2597 // Write the type offsets array
2598 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002599 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002600 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2603 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2604 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002605 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002606 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002607 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002608 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002609
2610 // Write the declaration offsets array
2611 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2616 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2617 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002618 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002619 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002620 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002621 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002622}
2623
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002624void ASTWriter::WriteFileDeclIDsMap() {
2625 using namespace llvm;
2626 RecordData Record;
2627
2628 // Join the vectors of DeclIDs from all files.
2629 SmallVector<DeclID, 256> FileSortedIDs;
2630 for (FileDeclIDsTy::iterator
2631 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2632 DeclIDInFileInfo &Info = *FI->second;
2633 Info.FirstDeclIndex = FileSortedIDs.size();
2634 for (LocDeclIDsTy::iterator
2635 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2636 FileSortedIDs.push_back(DI->second);
2637 }
2638
2639 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2640 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002641 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002642 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2643 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2644 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002645 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002646 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2647}
2648
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002649void ASTWriter::WriteComments() {
2650 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002651 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002652 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002653 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2654 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002655 I != E; ++I) {
2656 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002657 AddSourceRange((*I)->getSourceRange(), Record);
2658 Record.push_back((*I)->getKind());
2659 Record.push_back((*I)->isTrailingComment());
2660 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002661 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2662 }
2663 Stream.ExitBlock();
2664}
2665
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002666//===----------------------------------------------------------------------===//
2667// Global Method Pool and Selector Serialization
2668//===----------------------------------------------------------------------===//
2669
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002670namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002671// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002672class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002673 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002674
2675public:
2676 typedef Selector key_type;
2677 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002678
Sebastian Redl5d050072010-08-04 17:20:04 +00002679 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002680 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002681 ObjCMethodList Instance, Factory;
2682 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002683 typedef const data_type& data_type_ref;
2684
Sebastian Redl3397c552010-08-18 23:56:27 +00002685 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002687 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002688 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002689 }
Mike Stump1eb44332009-09-09 15:08:12 +00002690
2691 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002692 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002693 data_type_ref Methods) {
2694 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2695 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002696 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2697 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002698 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002699 if (Method->Method)
2700 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002701 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002702 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002703 if (Method->Method)
2704 DataLen += 4;
2705 clang::io::Emit16(Out, DataLen);
2706 return std::make_pair(KeyLen, DataLen);
2707 }
Mike Stump1eb44332009-09-09 15:08:12 +00002708
Chris Lattner5f9e2722011-07-23 10:55:15 +00002709 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002710 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002711 assert((Start >> 32) == 0 && "Selector key offset too large");
2712 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002713 unsigned N = Sel.getNumArgs();
2714 clang::io::Emit16(Out, N);
2715 if (N == 0)
2716 N = 1;
2717 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002718 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002719 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2720 }
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Chris Lattner5f9e2722011-07-23 10:55:15 +00002722 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002723 data_type_ref Methods, unsigned DataLen) {
2724 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002725 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002726 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002727 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002728 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002729 if (Method->Method)
2730 ++NumInstanceMethods;
2731
2732 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002733 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002734 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002735 if (Method->Method)
2736 ++NumFactoryMethods;
2737
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002738 unsigned InstanceBits = Methods.Instance.getBits();
2739 assert(InstanceBits < 4);
2740 unsigned NumInstanceMethodsAndBits =
2741 (NumInstanceMethods << 2) | InstanceBits;
2742 unsigned FactoryBits = Methods.Factory.getBits();
2743 assert(FactoryBits < 4);
2744 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2745 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2746 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002747 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002748 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002749 if (Method->Method)
2750 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002751 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002752 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002753 if (Method->Method)
2754 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002755
2756 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002757 }
2758};
2759} // end anonymous namespace
2760
Sebastian Redl059612d2010-08-03 21:58:15 +00002761/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002762///
2763/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002764/// in an on-disk hash table indexed by the selector. The hash table also
2765/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002766void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002767 using namespace llvm;
2768
Sebastian Redl059612d2010-08-03 21:58:15 +00002769 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002770 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002771 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002772 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002773 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002774 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002775 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002776 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002777
Sebastian Redl059612d2010-08-03 21:58:15 +00002778 // Create the on-disk hash table representation. We walk through every
2779 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002780 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002781 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002782 I = SelectorIDs.begin(), E = SelectorIDs.end();
2783 I != E; ++I) {
2784 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002785 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002786 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002787 I->second,
2788 ObjCMethodList(),
2789 ObjCMethodList()
2790 };
2791 if (F != SemaRef.MethodPool.end()) {
2792 Data.Instance = F->second.first;
2793 Data.Factory = F->second.second;
2794 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002795 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002796 // changed.
2797 if (Chain && I->second < FirstSelectorID) {
2798 // Selector already exists. Did it change?
2799 bool changed = false;
2800 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002801 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002802 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002803 changed = true;
2804 }
2805 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002806 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002807 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002808 changed = true;
2809 }
2810 if (!changed)
2811 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002812 } else if (Data.Instance.Method || Data.Factory.Method) {
2813 // A new method pool entry.
2814 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002815 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002816 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002817 }
2818
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002819 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002820 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002821 uint32_t BucketOffset;
2822 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002823 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002824 llvm::raw_svector_ostream Out(MethodPool);
2825 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002826 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002827 BucketOffset = Generator.Emit(Out, Trait);
2828 }
2829
2830 // Create a blob abbreviation
2831 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002832 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002834 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002835 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2836 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2837
Douglas Gregor83941df2009-04-25 17:48:32 +00002838 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002839 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002840 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002841 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002842 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002843 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002844
2845 // Create a blob abbreviation for the selector table offsets.
2846 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002847 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2851 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2852
2853 // Write the selector offsets table.
2854 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002855 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002856 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002857 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002858 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002859 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002860 }
2861}
2862
Sebastian Redl3397c552010-08-18 23:56:27 +00002863/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002864void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002865 using namespace llvm;
2866 if (SemaRef.ReferencedSelectors.empty())
2867 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002868
Fariborz Jahanian32019832010-07-23 19:11:11 +00002869 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002870
Sebastian Redl3397c552010-08-18 23:56:27 +00002871 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002872 // very tricky to fix, and given that @selector shouldn't really appear in
2873 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002874 for (DenseMap<Selector, SourceLocation>::iterator S =
2875 SemaRef.ReferencedSelectors.begin(),
2876 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2877 Selector Sel = (*S).first;
2878 SourceLocation Loc = (*S).second;
2879 AddSelectorRef(Sel, Record);
2880 AddSourceLocation(Loc, Record);
2881 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002882 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002883}
2884
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002885//===----------------------------------------------------------------------===//
2886// Identifier Table Serialization
2887//===----------------------------------------------------------------------===//
2888
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002889namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002890class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002891 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002892 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002893 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002894 bool IsModule;
2895
Douglas Gregora92193e2009-04-28 21:18:29 +00002896 /// \brief Determines whether this is an "interesting" identifier
2897 /// that needs a full IdentifierInfo structure written into the hash
2898 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002899 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002900 if (II->isPoisoned() ||
2901 II->isExtensionToken() ||
2902 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002903 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002904 II->getFETokenInfo<void>())
2905 return true;
2906
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002907 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002908 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002909
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002910 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002911 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002912 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002913
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002914 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2915 if (!IsModule)
2916 return !shouldIgnoreMacro(Macro, IsModule, PP);
2917 SubmoduleID ModID;
2918 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2919 return true;
2920 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002921
2922 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002923 }
2924
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002925 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2926 SubmoduleID &ModID) {
2927 ModID = 0;
2928 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2929 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2930 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002931 return 0;
2932 }
2933
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002934 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2935 SubmoduleID &ModID) {
2936 if (DefMacroDirective *
2937 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2938 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2939 return DefMD;
2940 return 0;
2941 }
2942
2943 /// \brief Traverses the macro directives history and returns the latest
2944 /// macro that is public and not undefined in the same submodule.
2945 /// A macro that is defined in submodule A and undefined in submodule B,
2946 /// will still be considered as defined/exported from submodule A.
2947 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2948 SubmoduleID &ModID) {
2949 if (!MD)
2950 return 0;
2951
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002952 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002953 bool isUndefined = false;
2954 Optional<bool> isPublic;
2955 for (; MD; MD = MD->getPrevious()) {
2956 if (MD->isHidden())
2957 continue;
2958
2959 SubmoduleID ThisModID = getSubmoduleID(MD);
2960 if (ThisModID == 0) {
2961 isUndefined = false;
2962 isPublic = Optional<bool>();
2963 continue;
2964 }
2965 if (ThisModID != ModID){
2966 ModID = ThisModID;
2967 isUndefined = false;
2968 isPublic = Optional<bool>();
2969 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002970 // We are looking for a definition in a different submodule than the one
2971 // that we started with. If a submodule has re-definitions of the same
2972 // macro, only the last definition will be used as the "exported" one.
2973 if (ModID == OrigModID)
2974 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002975
2976 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2977 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
2978 return DefMD;
2979 continue;
2980 }
2981
2982 if (isa<UndefMacroDirective>(MD)) {
2983 isUndefined = true;
2984 continue;
2985 }
2986
2987 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
2988 if (!isPublic.hasValue())
2989 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002990 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002991
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002992 return 0;
2993 }
2994
2995 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002996 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2997 MacroInfo *MI = DefMD->getInfo();
2998 if (unsigned ID = MI->getOwningModuleID())
2999 return ID;
3000 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
3001 }
3002 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003003 }
3004
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003005public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003006 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003007 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003008
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003009 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003010 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003011
Douglas Gregoreee242f2011-10-27 09:33:13 +00003012 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3013 IdentifierResolver &IdResolver, bool IsModule)
3014 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003015
3016 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003017 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003018 }
Mike Stump1eb44332009-09-09 15:08:12 +00003019
3020 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003021 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003022 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003023 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003024 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003025 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003026 DataLen += 2; // 2 bytes for builtin ID
3027 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003028 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003029 DataLen += 4; // MacroDirectives offset.
3030 if (IsModule) {
3031 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003032 for (DefMacroDirective *
3033 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3034 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003035 DataLen += 4; // MacroInfo ID.
3036 }
3037 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003038 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003039 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003040
Douglas Gregoreee242f2011-10-27 09:33:13 +00003041 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3042 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003043 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003044 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003045 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003046 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003047 // We emit the key length after the data length so that every
3048 // string is preceded by a 16-bit length. This matches the PTH
3049 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003050 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003051 return std::make_pair(KeyLen, DataLen);
3052 }
Mike Stump1eb44332009-09-09 15:08:12 +00003053
Chris Lattner5f9e2722011-07-23 10:55:15 +00003054 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003055 unsigned KeyLen) {
3056 // Record the location of the key data. This is used when generating
3057 // the mapping from persistent IDs to strings.
3058 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003059 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003060 }
Mike Stump1eb44332009-09-09 15:08:12 +00003061
Douglas Gregor7143aab2011-09-01 17:04:32 +00003062 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003063 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003064 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003065 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003066 clang::io::Emit32(Out, ID << 1);
3067 return;
3068 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003069
Douglas Gregora92193e2009-04-28 21:18:29 +00003070 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003071 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3072 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3073 clang::io::Emit16(Out, Bits);
3074 Bits = 0;
3075 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003076 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003077 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003078 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3079 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003080 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003081 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003082 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003083
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003084 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003085 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3086 if (IsModule) {
3087 // Write the IDs of macros coming from different submodules.
3088 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003089 for (DefMacroDirective *
3090 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3091 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3092 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003093 assert(InfoID);
3094 clang::io::Emit32(Out, InfoID);
3095 }
3096 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003097 }
Douglas Gregor13292642011-12-02 15:45:10 +00003098 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003099
Douglas Gregor668c1a42009-04-21 22:25:48 +00003100 // Emit the declaration IDs in reverse order, because the
3101 // IdentifierResolver provides the declarations as they would be
3102 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003103 // "stat"), but the ASTReader adds declarations to the end of the list
3104 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003105 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003106 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3107 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00003108 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003109 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003110 D != DEnd; ++D)
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003111 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3112 }
3113
3114 /// \brief Returns the most recent local decl or the given decl if there are
3115 /// no local ones. The given decl is assumed to be the most recent one.
3116 Decl *getMostRecentLocalDecl(Decl *Orig) {
3117 // The only way a "from AST file" decl would be more recent from a local one
3118 // is if it came from a module.
3119 if (!PP.getLangOpts().Modules)
3120 return Orig;
3121
3122 // Look for a local in the decl chain.
3123 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3124 if (!D->isFromASTFile())
3125 return D;
3126 // If we come up a decl from a (chained-)PCH stop since we won't find a
3127 // local one.
3128 if (D->getOwningModuleID() == 0)
3129 break;
3130 }
3131
3132 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003133 }
3134};
3135} // end anonymous namespace
3136
Sebastian Redl3397c552010-08-18 23:56:27 +00003137/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003138///
3139/// The identifier table consists of a blob containing string data
3140/// (the actual identifiers themselves) and a separate "offsets" index
3141/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003142void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3143 IdentifierResolver &IdResolver,
3144 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003145 using namespace llvm;
3146
3147 // Create and write out the blob that contains the identifier
3148 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003149 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003150 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003151 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003152
Douglas Gregor92b059e2009-04-28 20:33:11 +00003153 // Look for any identifiers that were named while processing the
3154 // headers, but are otherwise not needed. We add these to the hash
3155 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003156 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003157 // file.
3158 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3159 IDEnd = PP.getIdentifierTable().end();
3160 ID != IDEnd; ++ID)
3161 getIdentifierRef(ID->second);
3162
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003163 // Create the on-disk hash table representation. We only store offsets
3164 // for identifiers that appear here for the first time.
3165 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003166 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003167 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3168 ID != IDEnd; ++ID) {
3169 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003170 if (!Chain || !ID->first->isFromAST() ||
3171 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003172 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003173 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003174 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003175
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003176 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003177 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003178 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003179 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003180 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003181 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003182 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003183 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003184 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003185 }
3186
3187 // Create a blob abbreviation
3188 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003189 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003192 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003193
3194 // Write the identifier table
3195 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003196 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003197 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003198 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003199 }
3200
3201 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003202 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003203 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3207 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3208
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003209#ifndef NDEBUG
3210 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3211 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3212#endif
3213
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003214 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003215 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003216 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003217 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003218 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003219 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003220}
3221
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003222//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003223// DeclContext's Name Lookup Table Serialization
3224//===----------------------------------------------------------------------===//
3225
3226namespace {
3227// Trait used for the on-disk hash table used in the method pool.
3228class ASTDeclContextNameLookupTrait {
3229 ASTWriter &Writer;
3230
3231public:
3232 typedef DeclarationName key_type;
3233 typedef key_type key_type_ref;
3234
3235 typedef DeclContext::lookup_result data_type;
3236 typedef const data_type& data_type_ref;
3237
3238 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3239
3240 unsigned ComputeHash(DeclarationName Name) {
3241 llvm::FoldingSetNodeID ID;
3242 ID.AddInteger(Name.getNameKind());
3243
3244 switch (Name.getNameKind()) {
3245 case DeclarationName::Identifier:
3246 ID.AddString(Name.getAsIdentifierInfo()->getName());
3247 break;
3248 case DeclarationName::ObjCZeroArgSelector:
3249 case DeclarationName::ObjCOneArgSelector:
3250 case DeclarationName::ObjCMultiArgSelector:
3251 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3252 break;
3253 case DeclarationName::CXXConstructorName:
3254 case DeclarationName::CXXDestructorName:
3255 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003256 break;
3257 case DeclarationName::CXXOperatorName:
3258 ID.AddInteger(Name.getCXXOverloadedOperator());
3259 break;
3260 case DeclarationName::CXXLiteralOperatorName:
3261 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3262 case DeclarationName::CXXUsingDirective:
3263 break;
3264 }
3265
3266 return ID.ComputeHash();
3267 }
3268
3269 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003270 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003271 data_type_ref Lookup) {
3272 unsigned KeyLen = 1;
3273 switch (Name.getNameKind()) {
3274 case DeclarationName::Identifier:
3275 case DeclarationName::ObjCZeroArgSelector:
3276 case DeclarationName::ObjCOneArgSelector:
3277 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003278 case DeclarationName::CXXLiteralOperatorName:
3279 KeyLen += 4;
3280 break;
3281 case DeclarationName::CXXOperatorName:
3282 KeyLen += 1;
3283 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003284 case DeclarationName::CXXConstructorName:
3285 case DeclarationName::CXXDestructorName:
3286 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003287 case DeclarationName::CXXUsingDirective:
3288 break;
3289 }
3290 clang::io::Emit16(Out, KeyLen);
3291
3292 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003293 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003294 clang::io::Emit16(Out, DataLen);
3295
3296 return std::make_pair(KeyLen, DataLen);
3297 }
3298
Chris Lattner5f9e2722011-07-23 10:55:15 +00003299 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003300 using namespace clang::io;
3301
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003302 Emit8(Out, Name.getNameKind());
3303 switch (Name.getNameKind()) {
3304 case DeclarationName::Identifier:
3305 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003306 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003307 case DeclarationName::ObjCZeroArgSelector:
3308 case DeclarationName::ObjCOneArgSelector:
3309 case DeclarationName::ObjCMultiArgSelector:
3310 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003311 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003312 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003313 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3314 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003315 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003316 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003317 case DeclarationName::CXXLiteralOperatorName:
3318 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003319 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003320 case DeclarationName::CXXConstructorName:
3321 case DeclarationName::CXXDestructorName:
3322 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003323 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003324 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003325 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003326
3327 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003328 }
3329
Chris Lattner5f9e2722011-07-23 10:55:15 +00003330 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003331 data_type Lookup, unsigned DataLen) {
3332 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003333 clang::io::Emit16(Out, Lookup.size());
3334 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3335 I != E; ++I)
3336 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003337
3338 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3339 }
3340};
3341} // end anonymous namespace
3342
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003343/// \brief Write the block containing all of the declaration IDs
3344/// visible from the given DeclContext.
3345///
3346/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003347/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003348uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3349 DeclContext *DC) {
3350 if (DC->getPrimaryContext() != DC)
3351 return 0;
3352
3353 // Since there is no name lookup into functions or methods, don't bother to
3354 // build a visible-declarations table for these entities.
3355 if (DC->isFunctionOrMethod())
3356 return 0;
3357
3358 // If not in C++, we perform name lookup for the translation unit via the
3359 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003360 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003361 return 0;
3362
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003363 // Serialize the contents of the mapping used for lookup. Note that,
3364 // although we have two very different code paths, the serialized
3365 // representation is the same for both cases: a declaration name,
3366 // followed by a size, followed by references to the visible
3367 // declarations that have that name.
3368 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003369 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003370 if (!Map || Map->empty())
3371 return 0;
3372
3373 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3374 ASTDeclContextNameLookupTrait Trait(*this);
3375
3376 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003377 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003378 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003379 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3380 D != DEnd; ++D) {
3381 DeclarationName Name = D->first;
3382 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003383 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003384 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3385 // Hash all conversion function names to the same name. The actual
3386 // type information in conversion function name is not used in the
3387 // key (since such type information is not stable across different
3388 // modules), so the intended effect is to coalesce all of the conversion
3389 // functions under a single key.
3390 if (!ConversionName)
3391 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003392 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003393 continue;
3394 }
3395
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003396 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003397 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003398 }
3399
Douglas Gregore5a54b62011-08-30 20:49:19 +00003400 // Add the conversion functions
3401 if (!ConversionDecls.empty()) {
3402 Generator.insert(ConversionName,
3403 DeclContext::lookup_result(ConversionDecls.begin(),
3404 ConversionDecls.end()),
3405 Trait);
3406 }
3407
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003408 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003409 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003410 uint32_t BucketOffset;
3411 {
3412 llvm::raw_svector_ostream Out(LookupTable);
3413 // Make sure that no bucket is at offset 0
3414 clang::io::Emit32(Out, 0);
3415 BucketOffset = Generator.Emit(Out, Trait);
3416 }
3417
3418 // Write the lookup table
3419 RecordData Record;
3420 Record.push_back(DECL_CONTEXT_VISIBLE);
3421 Record.push_back(BucketOffset);
3422 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3423 LookupTable.str());
3424
3425 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3426 ++NumVisibleDeclContexts;
3427 return Offset;
3428}
3429
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003430/// \brief Write an UPDATE_VISIBLE block for the given context.
3431///
3432/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3433/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003434/// (in C++), for namespaces, and for classes with forward-declared unscoped
3435/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003436void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003437 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3438 if (!Map || Map->empty())
3439 return;
3440
3441 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3442 ASTDeclContextNameLookupTrait Trait(*this);
3443
3444 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003445 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3446 D != DEnd; ++D) {
3447 DeclarationName Name = D->first;
3448 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003449 // For any name that appears in this table, the results are complete, i.e.
3450 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003451 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003452 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003453 }
3454
3455 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003456 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003457 uint32_t BucketOffset;
3458 {
3459 llvm::raw_svector_ostream Out(LookupTable);
3460 // Make sure that no bucket is at offset 0
3461 clang::io::Emit32(Out, 0);
3462 BucketOffset = Generator.Emit(Out, Trait);
3463 }
3464
3465 // Write the lookup table
3466 RecordData Record;
3467 Record.push_back(UPDATE_VISIBLE);
3468 Record.push_back(getDeclID(cast<Decl>(DC)));
3469 Record.push_back(BucketOffset);
3470 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3471}
3472
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003473/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3474void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3475 RecordData Record;
3476 Record.push_back(Opts.fp_contract);
3477 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3478}
3479
3480/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3481void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003482 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003483 return;
3484
3485 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3486 RecordData Record;
3487#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3488#include "clang/Basic/OpenCLExtensions.def"
3489 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3490}
3491
Douglas Gregor2171bf12012-01-15 16:58:34 +00003492void ASTWriter::WriteRedeclarations() {
3493 RecordData LocalRedeclChains;
3494 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3495
3496 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3497 Decl *First = Redeclarations[I];
3498 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3499
3500 Decl *MostRecent = First->getMostRecentDecl();
3501
3502 // If we only have a single declaration, there is no point in storing
3503 // a redeclaration chain.
3504 if (First == MostRecent)
3505 continue;
3506
3507 unsigned Offset = LocalRedeclChains.size();
3508 unsigned Size = 0;
3509 LocalRedeclChains.push_back(0); // Placeholder for the size.
3510
3511 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003512 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003513 Prev = Prev->getPreviousDecl()) {
3514 if (!Prev->isFromASTFile()) {
3515 AddDeclRef(Prev, LocalRedeclChains);
3516 ++Size;
3517 }
3518 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003519
3520 if (!First->isFromASTFile() && Chain) {
3521 Decl *FirstFromAST = MostRecent;
3522 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3523 if (Prev->isFromASTFile())
3524 FirstFromAST = Prev;
3525 }
3526
3527 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3528 }
3529
Douglas Gregor2171bf12012-01-15 16:58:34 +00003530 LocalRedeclChains[Offset] = Size;
3531
3532 // Reverse the set of local redeclarations, so that we store them in
3533 // order (since we found them in reverse order).
3534 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3535
Douglas Gregoraa945902013-02-18 15:53:43 +00003536 // Add the mapping from the first ID from the AST to the set of local
3537 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003538 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3539 LocalRedeclsMap.push_back(Info);
3540
3541 assert(N == Redeclarations.size() &&
3542 "Deserialized a declaration we shouldn't have");
3543 }
3544
3545 if (LocalRedeclChains.empty())
3546 return;
3547
3548 // Sort the local redeclarations map by the first declaration ID,
3549 // since the reader will be performing binary searches on this information.
3550 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3551
3552 // Emit the local redeclarations map.
3553 using namespace llvm;
3554 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3555 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3558 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3559
3560 RecordData Record;
3561 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3562 Record.push_back(LocalRedeclsMap.size());
3563 Stream.EmitRecordWithBlob(AbbrevID, Record,
3564 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3565 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3566
3567 // Emit the redeclaration chains.
3568 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3569}
3570
Douglas Gregorcff9f262012-01-27 01:47:08 +00003571void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003572 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003573 RecordData Categories;
3574
3575 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3576 unsigned Size = 0;
3577 unsigned StartIndex = Categories.size();
3578
3579 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3580
3581 // Allocate space for the size.
3582 Categories.push_back(0);
3583
3584 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003585 for (ObjCInterfaceDecl::known_categories_iterator
3586 Cat = Class->known_categories_begin(),
3587 CatEnd = Class->known_categories_end();
3588 Cat != CatEnd; ++Cat, ++Size) {
3589 assert(getDeclID(*Cat) != 0 && "Bogus category");
3590 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003591 }
3592
3593 // Update the size.
3594 Categories[StartIndex] = Size;
3595
3596 // Record this interface -> category map.
3597 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3598 CategoriesMap.push_back(CatInfo);
3599 }
3600
3601 // Sort the categories map by the definition ID, since the reader will be
3602 // performing binary searches on this information.
3603 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3604
3605 // Emit the categories map.
3606 using namespace llvm;
3607 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3608 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3611 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3612
3613 RecordData Record;
3614 Record.push_back(OBJC_CATEGORIES_MAP);
3615 Record.push_back(CategoriesMap.size());
3616 Stream.EmitRecordWithBlob(AbbrevID, Record,
3617 reinterpret_cast<char*>(CategoriesMap.data()),
3618 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3619
3620 // Emit the category lists.
3621 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3622}
3623
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003624void ASTWriter::WriteMergedDecls() {
3625 if (!Chain || Chain->MergedDecls.empty())
3626 return;
3627
3628 RecordData Record;
3629 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3630 IEnd = Chain->MergedDecls.end();
3631 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003632 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003633 : getDeclID(I->first);
3634 assert(CanonID && "Merged declaration not known?");
3635
3636 Record.push_back(CanonID);
3637 Record.push_back(I->second.size());
3638 Record.append(I->second.begin(), I->second.end());
3639 }
3640 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3641}
3642
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003643//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003644// General Serialization Routines
3645//===----------------------------------------------------------------------===//
3646
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003647/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003648void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3649 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003650 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003651 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3652 e = Attrs.end(); i != e; ++i){
3653 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003654 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003655 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003656
Sean Huntcf807c42010-08-18 23:23:40 +00003657#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003658
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003659 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003660}
3661
Chris Lattner5f9e2722011-07-23 10:55:15 +00003662void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003663 Record.push_back(Str.size());
3664 Record.insert(Record.end(), Str.begin(), Str.end());
3665}
3666
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003667void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3668 RecordDataImpl &Record) {
3669 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003670 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003671 Record.push_back(*Minor + 1);
3672 else
3673 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003674 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003675 Record.push_back(*Subminor + 1);
3676 else
3677 Record.push_back(0);
3678}
3679
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003680/// \brief Note that the identifier II occurs at the given offset
3681/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003682void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003683 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003684 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003685 // up earlier in the chain and thus don't need an offset.
3686 if (ID >= FirstIdentID)
3687 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003688}
3689
Douglas Gregor83941df2009-04-25 17:48:32 +00003690/// \brief Note that the selector Sel occurs at the given offset
3691/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003692void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003693 unsigned ID = SelectorIDs[Sel];
3694 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003695 // Don't record offsets for selectors that are also available in a different
3696 // file.
3697 if (ID < FirstSelectorID)
3698 return;
3699 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003700}
3701
Sebastian Redla4232eb2010-08-18 23:56:21 +00003702ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003703 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003704 WritingAST(false), DoneWritingDeclsAndTypes(false),
3705 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003706 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003707 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003708 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3709 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003710 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3711 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003712 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003713 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003714 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003715 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003716 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003717 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003718 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3719 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3720 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003721 DeclTypedefAbbrev(0),
3722 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3723 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003724{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003725}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003726
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003727ASTWriter::~ASTWriter() {
3728 for (FileDeclIDsTy::iterator
3729 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3730 delete I->second;
3731}
3732
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003733void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003734 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003735 Module *WritingModule, StringRef isysroot,
3736 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003737 WritingAST = true;
3738
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003739 ASTHasCompilerErrors = hasErrors;
3740
Douglas Gregor2cf26342009-04-09 22:27:44 +00003741 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003742 Stream.Emit((unsigned)'C', 8);
3743 Stream.Emit((unsigned)'P', 8);
3744 Stream.Emit((unsigned)'C', 8);
3745 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003746
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003747 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003748
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003749 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003750 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003751 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003752 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003753 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003754 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003755 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003756
3757 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003758}
3759
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003760template<typename Vector>
3761static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3762 ASTWriter::RecordData &Record) {
3763 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3764 I != E; ++I) {
3765 Writer.AddDeclRef(*I, Record);
3766 }
3767}
3768
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003769void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003770 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003771 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003772 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003773 using namespace llvm;
3774
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003775 bool isModule = WritingModule != 0;
3776
Douglas Gregorecc2c092011-12-01 22:20:10 +00003777 // Make sure that the AST reader knows to finalize itself.
3778 if (Chain)
3779 Chain->finalizeForWriting();
3780
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003781 ASTContext &Context = SemaRef.Context;
3782 Preprocessor &PP = SemaRef.PP;
3783
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003784 // Set up predefined declaration IDs.
3785 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003786 if (Context.ObjCIdDecl)
3787 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003788 if (Context.ObjCSelDecl)
3789 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003790 if (Context.ObjCClassDecl)
3791 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003792 if (Context.ObjCProtocolClassDecl)
3793 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003794 if (Context.Int128Decl)
3795 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3796 if (Context.UInt128Decl)
3797 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003798 if (Context.ObjCInstanceTypeDecl)
3799 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003800 if (Context.BuiltinVaListDecl)
3801 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3802
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003803 if (!Chain) {
3804 // Make sure that we emit IdentifierInfos (and any attached
3805 // declarations) for builtins. We don't need to do this when we're
3806 // emitting chained PCH files, because all of the builtins will be
3807 // in the original PCH file.
3808 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003809 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003810 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003811 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003812 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003813 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3814 getIdentifierRef(&Table.get(BuiltinNames[I]));
3815 }
3816
Douglas Gregoreee242f2011-10-27 09:33:13 +00003817 // If there are any out-of-date identifiers, bring them up to date.
3818 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003819 // Find out-of-date identifiers.
3820 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003821 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3822 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003823 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003824 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003825 OutOfDate.push_back(ID->second);
3826 }
3827
3828 // Update the out-of-date identifiers.
3829 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3830 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3831 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003832 }
3833
Chris Lattner63d65f82009-09-08 18:19:27 +00003834 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003835 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003836 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003837 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003838 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003839
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003840 // Build a record containing all of the file scoped decls in this file.
3841 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003842 if (!isModule)
3843 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3844 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003845
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003846 // Build a record containing all of the delegating constructors we still need
3847 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003848 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003849 if (!isModule)
3850 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003851
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003852 // Write the set of weak, undeclared identifiers. We always write the
3853 // entire table, since later PCH files in a PCH chain are only interested in
3854 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003855 RecordData WeakUndeclaredIdentifiers;
3856 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003857 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003858 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3859 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3860 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3861 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3862 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3863 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3864 }
3865 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003866
Richard Smith5ea6ef42013-01-10 23:43:47 +00003867 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003868 // declarations in this header file. Generally, this record will be
3869 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003870 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003871 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003872 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003873 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003874 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3875 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003876 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003877 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003878 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003879 }
3880
Douglas Gregorb81c1702009-04-27 20:06:05 +00003881 // Build a record containing all of the ext_vector declarations.
3882 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003883 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003884
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003885 // Build a record containing all of the VTable uses information.
3886 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003887 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003888 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3889 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3890 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3891 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3892 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003893 }
3894
3895 // Build a record containing all of dynamic classes declarations.
3896 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003897 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003898
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003899 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003900 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003901 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003902 I = SemaRef.PendingInstantiations.begin(),
3903 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3904 AddDeclRef(I->first, PendingInstantiations);
3905 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003906 }
3907 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3908 "There are local ones at end of translation unit!");
3909
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003910 // Build a record containing some declaration references.
3911 RecordData SemaDeclRefs;
3912 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3913 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3914 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3915 }
3916
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003917 RecordData CUDASpecialDeclRefs;
3918 if (Context.getcudaConfigureCallDecl()) {
3919 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3920 }
3921
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003922 // Build a record containing all of the known namespaces.
3923 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003924 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003925 I = SemaRef.KnownNamespaces.begin(),
3926 IEnd = SemaRef.KnownNamespaces.end();
3927 I != IEnd; ++I) {
3928 if (!I->second)
3929 AddDeclRef(I->first, KnownNamespaces);
3930 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003931
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003932 // Build a record of all used, undefined objects that require definitions.
3933 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003934
3935 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003936 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003937 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3938 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003939 AddDeclRef(I->first, UndefinedButUsed);
3940 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003941 }
3942
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003943 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003944 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003945
Sebastian Redl3397c552010-08-18 23:56:27 +00003946 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003947 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003948 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003949
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003950 // This is so that older clang versions, before the introduction
3951 // of the control block, can read and reject the newer PCH format.
3952 Record.clear();
3953 Record.push_back(VERSION_MAJOR);
3954 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3955
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003956 // Create a lexical update block containing all of the declarations in the
3957 // translation unit that do not come from other AST files.
3958 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3959 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3960 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3961 E = TU->noload_decls_end();
3962 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003963 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003964 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003965 }
3966
3967 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3968 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3969 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3970 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3971 Record.clear();
3972 Record.push_back(TU_UPDATE_LEXICAL);
3973 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3974 data(NewGlobalDecls));
3975
3976 // And a visible updates block for the translation unit.
3977 Abv = new llvm::BitCodeAbbrev();
3978 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3979 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3980 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3981 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3982 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3983 WriteDeclContextVisibleUpdate(TU);
3984
3985 // If the translation unit has an anonymous namespace, and we don't already
3986 // have an update block for it, write it as an update block.
3987 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3988 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3989 if (Record.empty()) {
3990 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003991 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003992 }
3993 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003994
3995 // Make sure visible decls, added to DeclContexts previously loaded from
3996 // an AST file, are registered for serialization.
3997 for (SmallVector<const Decl *, 16>::iterator
3998 I = UpdatingVisibleDecls.begin(),
3999 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4000 GetDeclRef(*I);
4001 }
4002
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004003 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004004 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00004005
Douglas Gregora119da02011-08-02 16:26:37 +00004006 // Form the record of special types.
4007 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004008 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004009 AddTypeRef(Context.getFILEType(), SpecialTypes);
4010 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4011 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4012 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4013 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004014 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004015 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004016
Douglas Gregor366809a2009-04-26 03:49:13 +00004017 // Keep writing types and declarations until all types and
4018 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00004019 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004020 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004021 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4022 E = DeclsToRewrite.end();
4023 I != E; ++I)
4024 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004025 while (!DeclTypesToEmit.empty()) {
4026 DeclOrType DOT = DeclTypesToEmit.front();
4027 DeclTypesToEmit.pop();
4028 if (DOT.isType())
4029 WriteType(DOT.getType());
4030 else
4031 WriteDecl(Context, DOT.getDecl());
4032 }
4033 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004034
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004035 DoneWritingDeclsAndTypes = true;
4036
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004037 WriteFileDeclIDsMap();
4038 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004039 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004040
4041 if (Chain) {
4042 // Write the mapping information describing our module dependencies and how
4043 // each of those modules were mapped into our own offset/ID space, so that
4044 // the reader can build the appropriate mapping to its own offset/ID space.
4045 // The map consists solely of a blob with the following format:
4046 // *(module-name-len:i16 module-name:len*i8
4047 // source-location-offset:i32
4048 // identifier-id:i32
4049 // preprocessed-entity-id:i32
4050 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004051 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004052 // selector-id:i32
4053 // declaration-id:i32
4054 // c++-base-specifiers-id:i32
4055 // type-id:i32)
4056 //
4057 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4058 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4060 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004061 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004062 {
4063 llvm::raw_svector_ostream Out(Buffer);
4064 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004065 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004066 M != MEnd; ++M) {
4067 StringRef FileName = (*M)->FileName;
4068 io::Emit16(Out, FileName.size());
4069 Out.write(FileName.data(), FileName.size());
4070 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4071 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004072 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004073 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004074 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004075 io::Emit32(Out, (*M)->BaseSelectorID);
4076 io::Emit32(Out, (*M)->BaseDeclID);
4077 io::Emit32(Out, (*M)->BaseTypeIndex);
4078 }
4079 }
4080 Record.clear();
4081 Record.push_back(MODULE_OFFSET_MAP);
4082 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4083 Buffer.data(), Buffer.size());
4084 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004085 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004086 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004087 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004088 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004089 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004090 WriteFPPragmaOptions(SemaRef.getFPOptions());
4091 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004092
Sebastian Redl1476ed42010-07-16 16:36:56 +00004093 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004094 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004095
Anders Carlssonc8505782011-03-06 18:41:18 +00004096 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004097
Douglas Gregore209e502011-12-06 01:10:29 +00004098 // If we're emitting a module, write out the submodule information.
4099 if (WritingModule)
4100 WriteSubmodules(WritingModule);
4101
Douglas Gregora119da02011-08-02 16:26:37 +00004102 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4103
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004104 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004105 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004106 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004107
4108 // Write the record containing tentative definitions.
4109 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004110 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004111
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004112 // Write the record containing unused file scoped decls.
4113 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004114 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004115
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004116 // Write the record containing weak undeclared identifiers.
4117 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004118 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004119 WeakUndeclaredIdentifiers);
4120
Richard Smith5ea6ef42013-01-10 23:43:47 +00004121 // Write the record containing locally-scoped extern "C" definitions.
4122 if (!LocallyScopedExternCDecls.empty())
4123 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4124 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004125
4126 // Write the record containing ext_vector type names.
4127 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004128 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004129
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004130 // Write the record containing VTable uses information.
4131 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004132 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004133
4134 // Write the record containing dynamic classes declarations.
4135 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004136 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004137
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004138 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004139 if (!PendingInstantiations.empty())
4140 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004141
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004142 // Write the record containing declaration references of Sema.
4143 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004144 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004145
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004146 // Write the record containing CUDA-specific declaration references.
4147 if (!CUDASpecialDeclRefs.empty())
4148 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004149
4150 // Write the delegating constructors.
4151 if (!DelegatingCtorDecls.empty())
4152 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004153
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004154 // Write the known namespaces.
4155 if (!KnownNamespaces.empty())
4156 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004157
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004158 // Write the undefined internal functions and variables, and inline functions.
4159 if (!UndefinedButUsed.empty())
4160 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004161
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004162 // Write the visible updates to DeclContexts.
4163 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4164 I = UpdatedDeclContexts.begin(),
4165 E = UpdatedDeclContexts.end();
4166 I != E; ++I)
4167 WriteDeclContextVisibleUpdate(*I);
4168
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004169 if (!WritingModule) {
4170 // Write the submodules that were imported, if any.
4171 RecordData ImportedModules;
4172 for (ASTContext::import_iterator I = Context.local_import_begin(),
4173 IEnd = Context.local_import_end();
4174 I != IEnd; ++I) {
4175 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4176 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4177 }
4178 if (!ImportedModules.empty()) {
4179 // Sort module IDs.
4180 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4181
4182 // Unique module IDs.
4183 ImportedModules.erase(std::unique(ImportedModules.begin(),
4184 ImportedModules.end()),
4185 ImportedModules.end());
4186
4187 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4188 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004189 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004190
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004191 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004192 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004193 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004194 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004195 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00004196
Douglas Gregor3e1af842009-04-17 22:13:46 +00004197 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004198 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004199 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004200 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004201 Record.push_back(NumLexicalDeclContexts);
4202 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004203 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004204 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004205}
4206
Douglas Gregor61c5e342011-09-17 00:05:03 +00004207/// \brief Go through the declaration update blocks and resolve declaration
4208/// pointers into declaration IDs.
4209void ASTWriter::ResolveDeclUpdatesBlocks() {
4210 for (DeclUpdateMap::iterator
4211 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4212 const Decl *D = I->first;
4213 UpdateRecord &URec = I->second;
4214
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004215 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004216 continue; // The decl will be written completely
4217
4218 unsigned Idx = 0, N = URec.size();
4219 while (Idx < N) {
4220 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004221 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4222 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4223 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4224 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4225 ++Idx;
4226 break;
4227
4228 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4229 ++Idx;
4230 break;
4231 }
4232 }
4233 }
4234}
4235
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004236void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004237 if (DeclUpdates.empty())
4238 return;
4239
4240 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004241 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004242 for (DeclUpdateMap::iterator
4243 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4244 const Decl *D = I->first;
4245 UpdateRecord &URec = I->second;
4246
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004247 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004248 continue; // The decl will be written completely,no need to store updates.
4249
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004250 uint64_t Offset = Stream.GetCurrentBitNo();
4251 Stream.EmitRecord(DECL_UPDATES, URec);
4252
4253 OffsetsRecord.push_back(GetDeclRef(D));
4254 OffsetsRecord.push_back(Offset);
4255 }
4256 Stream.ExitBlock();
4257 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4258}
4259
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004260void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004261 if (ReplacedDecls.empty())
4262 return;
4263
4264 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004265 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004266 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004267 Record.push_back(I->ID);
4268 Record.push_back(I->Offset);
4269 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004270 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004271 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004272}
4273
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004274void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004275 Record.push_back(Loc.getRawEncoding());
4276}
4277
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004278void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004279 AddSourceLocation(Range.getBegin(), Record);
4280 AddSourceLocation(Range.getEnd(), Record);
4281}
4282
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004283void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004284 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004285 const uint64_t *Words = Value.getRawData();
4286 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004287}
4288
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004289void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004290 Record.push_back(Value.isUnsigned());
4291 AddAPInt(Value, Record);
4292}
4293
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004294void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004295 AddAPInt(Value.bitcastToAPInt(), Record);
4296}
4297
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004298void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004299 Record.push_back(getIdentifierRef(II));
4300}
4301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004302IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004303 if (II == 0)
4304 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004305
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004306 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004307 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004308 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004309 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004310}
4311
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004312MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004313 // Don't emit builtin macros like __LINE__ to the AST file unless they
4314 // have been redefined by the header (in which case they are not
4315 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004316 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004317 return 0;
4318
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004319 MacroID &ID = MacroIDs[MI];
4320 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004321 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004322 MacroInfoToEmitData Info = { Name, MI, ID };
4323 MacroInfosToEmit.push_back(Info);
4324 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004325 return ID;
4326}
4327
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004328MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4329 if (MI == 0 || MI->isBuiltinMacro())
4330 return 0;
4331
4332 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4333 return MacroIDs[MI];
4334}
4335
4336uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4337 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4338 return IdentMacroDirectivesOffsetMap[Name];
4339}
4340
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004341void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004342 Record.push_back(getSelectorRef(SelRef));
4343}
4344
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004345SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004346 if (Sel.getAsOpaquePtr() == 0) {
4347 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004348 }
4349
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004350 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004351 if (SID == 0 && Chain) {
4352 // This might trigger a ReadSelector callback, which will set the ID for
4353 // this selector.
4354 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004355 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004356 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004357 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004358 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004359 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004360 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004361 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004362}
4363
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004364void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004365 AddDeclRef(Temp->getDestructor(), Record);
4366}
4367
Douglas Gregor7c789c12010-10-29 22:39:52 +00004368void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4369 CXXBaseSpecifier const *BasesEnd,
4370 RecordDataImpl &Record) {
4371 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4372 CXXBaseSpecifiersToWrite.push_back(
4373 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4374 Bases, BasesEnd));
4375 Record.push_back(NextCXXBaseSpecifiersID++);
4376}
4377
Sebastian Redla4232eb2010-08-18 23:56:21 +00004378void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004379 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004380 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004381 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004382 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004383 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004384 break;
4385 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004386 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004387 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004388 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004389 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004390 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004391 break;
4392 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004393 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004394 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004395 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004396 break;
John McCall833ca992009-10-29 08:12:44 +00004397 case TemplateArgument::Null:
4398 case TemplateArgument::Integral:
4399 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004400 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004401 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004402 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004403 break;
4404 }
4405}
4406
Sebastian Redla4232eb2010-08-18 23:56:21 +00004407void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004408 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004409 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004410
4411 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4412 bool InfoHasSameExpr
4413 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4414 Record.push_back(InfoHasSameExpr);
4415 if (InfoHasSameExpr)
4416 return; // Avoid storing the same expr twice.
4417 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004418 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4419 Record);
4420}
4421
Douglas Gregordc355712011-02-25 00:36:19 +00004422void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4423 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004424 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004425 AddTypeRef(QualType(), Record);
4426 return;
4427 }
4428
Douglas Gregordc355712011-02-25 00:36:19 +00004429 AddTypeLoc(TInfo->getTypeLoc(), Record);
4430}
4431
4432void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4433 AddTypeRef(TL.getType(), Record);
4434
John McCalla1ee0c52009-10-16 21:56:05 +00004435 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004436 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004437 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004438}
4439
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004440void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004441 Record.push_back(GetOrCreateTypeID(T));
4442}
4443
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004444TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4445 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004446 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4447}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004448
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004449TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004450 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004451 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004452}
4453
4454TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4455 if (T.isNull())
4456 return TypeIdx();
4457 assert(!T.getLocalFastQualifiers());
4458
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004459 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004460 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004461 if (DoneWritingDeclsAndTypes) {
4462 assert(0 && "New type seen after serializing all the types to emit!");
4463 return TypeIdx();
4464 }
4465
Douglas Gregor366809a2009-04-26 03:49:13 +00004466 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004467 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004468 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004469 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004470 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004471 return Idx;
4472}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004473
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004474TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004475 if (T.isNull())
4476 return TypeIdx();
4477 assert(!T.getLocalFastQualifiers());
4478
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004479 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4480 assert(I != TypeIdxs.end() && "Type not emitted!");
4481 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004482}
4483
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004484void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004485 Record.push_back(GetDeclRef(D));
4486}
4487
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004488DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004489 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4490
Douglas Gregor2cf26342009-04-09 22:27:44 +00004491 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004492 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004493 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004494
4495 // If D comes from an AST file, its declaration ID is already known and
4496 // fixed.
4497 if (D->isFromASTFile())
4498 return D->getGlobalID();
4499
Douglas Gregor97475832010-10-05 18:37:06 +00004500 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004501 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004502 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004503 if (DoneWritingDeclsAndTypes) {
4504 assert(0 && "New decl seen after serializing all the decls to emit!");
4505 return 0;
4506 }
4507
Douglas Gregor2cf26342009-04-09 22:27:44 +00004508 // We haven't seen this declaration before. Give it a new ID and
4509 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004510 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004511 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004512 }
4513
Sebastian Redl681d7232010-07-27 00:17:23 +00004514 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004515}
4516
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004517DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004518 if (D == 0)
4519 return 0;
4520
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004521 // If D comes from an AST file, its declaration ID is already known and
4522 // fixed.
4523 if (D->isFromASTFile())
4524 return D->getGlobalID();
4525
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004526 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4527 return DeclIDs[D];
4528}
4529
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004530static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4531 std::pair<unsigned, serialization::DeclID> R) {
4532 return L.first < R.first;
4533}
4534
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004535void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004536 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004537 assert(D);
4538
4539 SourceLocation Loc = D->getLocation();
4540 if (Loc.isInvalid())
4541 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004542
4543 // We only keep track of the file-level declarations of each file.
4544 if (!D->getLexicalDeclContext()->isFileContext())
4545 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004546 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4547 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004548 if (isa<ParmVarDecl>(D))
4549 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004550
4551 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004552 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004553 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004554 FileID FID;
4555 unsigned Offset;
4556 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004557 if (FID.isInvalid())
4558 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004559 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004560
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004561 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004562 if (!Info)
4563 Info = new DeclIDInFileInfo();
4564
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004565 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004566 LocDeclIDsTy &Decls = Info->DeclIDs;
4567
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004568 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004569 Decls.push_back(LocDecl);
4570 return;
4571 }
4572
4573 LocDeclIDsTy::iterator
4574 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4575
4576 Decls.insert(I, LocDecl);
4577}
4578
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004579void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004580 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004581 Record.push_back(Name.getNameKind());
4582 switch (Name.getNameKind()) {
4583 case DeclarationName::Identifier:
4584 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4585 break;
4586
4587 case DeclarationName::ObjCZeroArgSelector:
4588 case DeclarationName::ObjCOneArgSelector:
4589 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004590 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004591 break;
4592
4593 case DeclarationName::CXXConstructorName:
4594 case DeclarationName::CXXDestructorName:
4595 case DeclarationName::CXXConversionFunctionName:
4596 AddTypeRef(Name.getCXXNameType(), Record);
4597 break;
4598
4599 case DeclarationName::CXXOperatorName:
4600 Record.push_back(Name.getCXXOverloadedOperator());
4601 break;
4602
Sean Hunt3e518bd2009-11-29 07:34:05 +00004603 case DeclarationName::CXXLiteralOperatorName:
4604 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4605 break;
4606
Douglas Gregor2cf26342009-04-09 22:27:44 +00004607 case DeclarationName::CXXUsingDirective:
4608 // No extra data to emit
4609 break;
4610 }
4611}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004612
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004613void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004614 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004615 switch (Name.getNameKind()) {
4616 case DeclarationName::CXXConstructorName:
4617 case DeclarationName::CXXDestructorName:
4618 case DeclarationName::CXXConversionFunctionName:
4619 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4620 break;
4621
4622 case DeclarationName::CXXOperatorName:
4623 AddSourceLocation(
4624 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4625 Record);
4626 AddSourceLocation(
4627 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4628 Record);
4629 break;
4630
4631 case DeclarationName::CXXLiteralOperatorName:
4632 AddSourceLocation(
4633 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4634 Record);
4635 break;
4636
4637 case DeclarationName::Identifier:
4638 case DeclarationName::ObjCZeroArgSelector:
4639 case DeclarationName::ObjCOneArgSelector:
4640 case DeclarationName::ObjCMultiArgSelector:
4641 case DeclarationName::CXXUsingDirective:
4642 break;
4643 }
4644}
4645
4646void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004647 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004648 AddDeclarationName(NameInfo.getName(), Record);
4649 AddSourceLocation(NameInfo.getLoc(), Record);
4650 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4651}
4652
4653void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004654 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004655 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004656 Record.push_back(Info.NumTemplParamLists);
4657 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4658 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4659}
4660
Sebastian Redla4232eb2010-08-18 23:56:21 +00004661void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004662 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004663 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004664 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004665 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004666
4667 // Push each of the NNS's onto a stack for serialization in reverse order.
4668 while (NNS) {
4669 NestedNames.push_back(NNS);
4670 NNS = NNS->getPrefix();
4671 }
4672
4673 Record.push_back(NestedNames.size());
4674 while(!NestedNames.empty()) {
4675 NNS = NestedNames.pop_back_val();
4676 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4677 Record.push_back(Kind);
4678 switch (Kind) {
4679 case NestedNameSpecifier::Identifier:
4680 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4681 break;
4682
4683 case NestedNameSpecifier::Namespace:
4684 AddDeclRef(NNS->getAsNamespace(), Record);
4685 break;
4686
Douglas Gregor14aba762011-02-24 02:36:08 +00004687 case NestedNameSpecifier::NamespaceAlias:
4688 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4689 break;
4690
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004691 case NestedNameSpecifier::TypeSpec:
4692 case NestedNameSpecifier::TypeSpecWithTemplate:
4693 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4694 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4695 break;
4696
4697 case NestedNameSpecifier::Global:
4698 // Don't need to write an associated value.
4699 break;
4700 }
4701 }
4702}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004703
Douglas Gregordc355712011-02-25 00:36:19 +00004704void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4705 RecordDataImpl &Record) {
4706 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004707 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004708 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004709
4710 // Push each of the nested-name-specifiers's onto a stack for
4711 // serialization in reverse order.
4712 while (NNS) {
4713 NestedNames.push_back(NNS);
4714 NNS = NNS.getPrefix();
4715 }
4716
4717 Record.push_back(NestedNames.size());
4718 while(!NestedNames.empty()) {
4719 NNS = NestedNames.pop_back_val();
4720 NestedNameSpecifier::SpecifierKind Kind
4721 = NNS.getNestedNameSpecifier()->getKind();
4722 Record.push_back(Kind);
4723 switch (Kind) {
4724 case NestedNameSpecifier::Identifier:
4725 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4726 AddSourceRange(NNS.getLocalSourceRange(), Record);
4727 break;
4728
4729 case NestedNameSpecifier::Namespace:
4730 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4731 AddSourceRange(NNS.getLocalSourceRange(), Record);
4732 break;
4733
4734 case NestedNameSpecifier::NamespaceAlias:
4735 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4736 AddSourceRange(NNS.getLocalSourceRange(), Record);
4737 break;
4738
4739 case NestedNameSpecifier::TypeSpec:
4740 case NestedNameSpecifier::TypeSpecWithTemplate:
4741 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4742 AddTypeLoc(NNS.getTypeLoc(), Record);
4743 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4744 break;
4745
4746 case NestedNameSpecifier::Global:
4747 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4748 break;
4749 }
4750 }
4751}
4752
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004753void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004754 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004755 Record.push_back(Kind);
4756 switch (Kind) {
4757 case TemplateName::Template:
4758 AddDeclRef(Name.getAsTemplateDecl(), Record);
4759 break;
4760
4761 case TemplateName::OverloadedTemplate: {
4762 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4763 Record.push_back(OvT->size());
4764 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4765 I != E; ++I)
4766 AddDeclRef(*I, Record);
4767 break;
4768 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004769
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004770 case TemplateName::QualifiedTemplate: {
4771 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4772 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4773 Record.push_back(QualT->hasTemplateKeyword());
4774 AddDeclRef(QualT->getTemplateDecl(), Record);
4775 break;
4776 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004777
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004778 case TemplateName::DependentTemplate: {
4779 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4780 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4781 Record.push_back(DepT->isIdentifier());
4782 if (DepT->isIdentifier())
4783 AddIdentifierRef(DepT->getIdentifier(), Record);
4784 else
4785 Record.push_back(DepT->getOperator());
4786 break;
4787 }
John McCall14606042011-06-30 08:33:18 +00004788
4789 case TemplateName::SubstTemplateTemplateParm: {
4790 SubstTemplateTemplateParmStorage *subst
4791 = Name.getAsSubstTemplateTemplateParm();
4792 AddDeclRef(subst->getParameter(), Record);
4793 AddTemplateName(subst->getReplacement(), Record);
4794 break;
4795 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004796
4797 case TemplateName::SubstTemplateTemplateParmPack: {
4798 SubstTemplateTemplateParmPackStorage *SubstPack
4799 = Name.getAsSubstTemplateTemplateParmPack();
4800 AddDeclRef(SubstPack->getParameterPack(), Record);
4801 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4802 break;
4803 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004804 }
4805}
4806
Michael J. Spencer20249a12010-10-21 03:16:25 +00004807void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004808 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004809 Record.push_back(Arg.getKind());
4810 switch (Arg.getKind()) {
4811 case TemplateArgument::Null:
4812 break;
4813 case TemplateArgument::Type:
4814 AddTypeRef(Arg.getAsType(), Record);
4815 break;
4816 case TemplateArgument::Declaration:
4817 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004818 Record.push_back(Arg.isDeclForReferenceParam());
4819 break;
4820 case TemplateArgument::NullPtr:
4821 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004822 break;
4823 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004824 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004825 AddTypeRef(Arg.getIntegralType(), Record);
4826 break;
4827 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004828 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4829 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004830 case TemplateArgument::TemplateExpansion:
4831 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004832 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004833 Record.push_back(*NumExpansions + 1);
4834 else
4835 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004836 break;
4837 case TemplateArgument::Expression:
4838 AddStmt(Arg.getAsExpr());
4839 break;
4840 case TemplateArgument::Pack:
4841 Record.push_back(Arg.pack_size());
4842 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4843 I != E; ++I)
4844 AddTemplateArgument(*I, Record);
4845 break;
4846 }
4847}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004848
4849void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004850ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004851 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004852 assert(TemplateParams && "No TemplateParams!");
4853 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4854 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4855 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4856 Record.push_back(TemplateParams->size());
4857 for (TemplateParameterList::const_iterator
4858 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4859 P != PEnd; ++P)
4860 AddDeclRef(*P, Record);
4861}
4862
4863/// \brief Emit a template argument list.
4864void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004865ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004866 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004867 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004868 Record.push_back(TemplateArgs->size());
4869 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004870 AddTemplateArgument(TemplateArgs->get(i), Record);
4871}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004872
4873
4874void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004875ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004876 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004877 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004878 I = Set.begin(), E = Set.end(); I != E; ++I) {
4879 AddDeclRef(I.getDecl(), Record);
4880 Record.push_back(I.getAccess());
4881 }
4882}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004883
Sebastian Redla4232eb2010-08-18 23:56:21 +00004884void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004885 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004886 Record.push_back(Base.isVirtual());
4887 Record.push_back(Base.isBaseOfClass());
4888 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004889 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004890 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004891 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004892 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4893 : SourceLocation(),
4894 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004895}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004896
Douglas Gregor7c789c12010-10-29 22:39:52 +00004897void ASTWriter::FlushCXXBaseSpecifiers() {
4898 RecordData Record;
4899 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4900 Record.clear();
4901
4902 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004903 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004904 if (Index == CXXBaseSpecifiersOffsets.size())
4905 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4906 else {
4907 if (Index > CXXBaseSpecifiersOffsets.size())
4908 CXXBaseSpecifiersOffsets.resize(Index + 1);
4909 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4910 }
4911
4912 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4913 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4914 Record.push_back(BEnd - B);
4915 for (; B != BEnd; ++B)
4916 AddCXXBaseSpecifier(*B, Record);
4917 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004918
4919 // Flush any expressions that were written as part of the base specifiers.
4920 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004921 }
4922
4923 CXXBaseSpecifiersToWrite.clear();
4924}
4925
Sean Huntcbb67482011-01-08 20:30:50 +00004926void ASTWriter::AddCXXCtorInitializers(
4927 const CXXCtorInitializer * const *CtorInitializers,
4928 unsigned NumCtorInitializers,
4929 RecordDataImpl &Record) {
4930 Record.push_back(NumCtorInitializers);
4931 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4932 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004933
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004934 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004935 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004936 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004937 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004938 } else if (Init->isDelegatingInitializer()) {
4939 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004940 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004941 } else if (Init->isMemberInitializer()){
4942 Record.push_back(CTOR_INITIALIZER_MEMBER);
4943 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004944 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004945 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4946 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004947 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004948
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004949 AddSourceLocation(Init->getMemberLocation(), Record);
4950 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004951 AddSourceLocation(Init->getLParenLoc(), Record);
4952 AddSourceLocation(Init->getRParenLoc(), Record);
4953 Record.push_back(Init->isWritten());
4954 if (Init->isWritten()) {
4955 Record.push_back(Init->getSourceOrder());
4956 } else {
4957 Record.push_back(Init->getNumArrayIndices());
4958 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4959 AddDeclRef(Init->getArrayIndex(i), Record);
4960 }
4961 }
4962}
4963
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004964void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4965 assert(D->DefinitionData);
4966 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004967 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004968 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004969 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004970 Record.push_back(Data.Aggregate);
4971 Record.push_back(Data.PlainOldData);
4972 Record.push_back(Data.Empty);
4973 Record.push_back(Data.Polymorphic);
4974 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004975 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004976 Record.push_back(Data.HasNoNonEmptyBases);
4977 Record.push_back(Data.HasPrivateFields);
4978 Record.push_back(Data.HasProtectedFields);
4979 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004980 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004981 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004982 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004983 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004984 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4985 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4986 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4987 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4988 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4989 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004990 Record.push_back(Data.HasTrivialSpecialMembers);
4991 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004992 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004993 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004994 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004995 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004996 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004997 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004998 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00004999 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5000 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5001 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5002 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00005003 Record.push_back(Data.FailedImplicitMoveConstructor);
5004 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00005005 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005006
5007 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005008 if (Data.NumBases > 0)
5009 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5010 Record);
5011
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005012 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5013 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005014 if (Data.NumVBases > 0)
5015 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5016 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005017
5018 AddUnresolvedSet(Data.Conversions, Record);
5019 AddUnresolvedSet(Data.VisibleConversions, Record);
5020 // Data.Definition is the owning decl, no need to write it.
5021 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005022
5023 // Add lambda-specific data.
5024 if (Data.IsLambda) {
5025 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005026 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005027 Record.push_back(Lambda.NumCaptures);
5028 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005029 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005030 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005031 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005032 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5033 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5034 AddSourceLocation(Capture.getLocation(), Record);
5035 Record.push_back(Capture.isImplicit());
5036 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
5037 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
5038 AddDeclRef(Var, Record);
5039 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
5040 : SourceLocation(),
5041 Record);
5042 }
5043 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005044}
5045
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005046void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005047 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005048 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005049 assert(FirstDeclID == NextDeclID &&
5050 FirstTypeID == NextTypeID &&
5051 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005052 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005053 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005054 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005055 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005056
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005057 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005058
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005059 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5060 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5061 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005062 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005063 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005064 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005065 NextDeclID = FirstDeclID;
5066 NextTypeID = FirstTypeID;
5067 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005068 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005069 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005070 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005071}
5072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005073void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005074 // Always keep the highest ID. See \p TypeRead() for more information.
5075 IdentID &StoredID = IdentifierIDs[II];
5076 if (ID > StoredID)
5077 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005078}
5079
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005080void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005081 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005082 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005083 if (ID > StoredID)
5084 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005085}
5086
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005087void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005088 // Always take the highest-numbered type index. This copes with an interesting
5089 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005090 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005091 // keep the higher-numbered entry so that we can properly write it out to
5092 // the AST file.
5093 TypeIdx &StoredIdx = TypeIdxs[T];
5094 if (Idx.getIndex() >= StoredIdx.getIndex())
5095 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005096}
5097
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005098void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005099 // Always keep the highest ID. See \p TypeRead() for more information.
5100 SelectorID &StoredID = SelectorIDs[S];
5101 if (ID > StoredID)
5102 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005103}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005104
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005105void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005106 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005107 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005108 MacroDefinitions[MD] = ID;
5109}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005110
Douglas Gregora015cab2011-12-02 17:30:13 +00005111void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5112 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5113 SubmoduleIDs[Mod] = ID;
5114}
5115
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005116void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005117 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005118 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005119 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5120 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005121 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005122 // A forward reference was mutated into a definition. Rewrite it.
5123 // FIXME: This happens during template instantiation, should we
5124 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005125 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005126 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005127 }
5128}
Douglas Gregora8235d62012-10-09 23:05:51 +00005129
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005130void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005131 assert(!WritingAST && "Already writing the AST!");
5132
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005133 // TU and namespaces are handled elsewhere.
5134 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5135 return;
5136
Douglas Gregor919814d2011-09-09 23:01:35 +00005137 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005138 return; // Not a source decl added to a DeclContext from PCH.
5139
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005140 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005141 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005142 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005143}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005144
5145void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005146 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005147 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005148 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005149 return; // Not a source member added to a class from PCH.
5150 if (!isa<CXXMethodDecl>(D))
5151 return; // We are interested in lazily declared implicit methods.
5152
5153 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005154 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005155 UpdateRecord &Record = DeclUpdates[RD];
5156 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005157 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005158}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005159
5160void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5161 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005162 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005163 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005164 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005165 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005166 return; // Not a source specialization added to a template from PCH.
5167
5168 UpdateRecord &Record = DeclUpdates[TD];
5169 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005170 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005171}
Douglas Gregor89d99802010-11-30 06:16:57 +00005172
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005173void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5174 const FunctionDecl *D) {
5175 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005176 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005177 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005178 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005179 return; // Not a source specialization added to a template from PCH.
5180
5181 UpdateRecord &Record = DeclUpdates[TD];
5182 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005183 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005184}
5185
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005186void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005187 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005188 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005189 return; // Declaration not imported from PCH.
5190
5191 // Implicit decl from a PCH was defined.
5192 // FIXME: Should implicit definition be a separate FunctionDecl?
5193 RewriteDecl(D);
5194}
5195
Sebastian Redlf79a7192011-04-29 08:19:30 +00005196void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005197 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005198 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005199 return;
5200
5201 // Since the actual instantiation is delayed, this really means that we need
5202 // to update the instantiation location.
5203 UpdateRecord &Record = DeclUpdates[D];
5204 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5205 AddSourceLocation(
5206 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5207}
5208
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005209void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5210 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005211 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005212 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005213 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005214
5215 assert(IFD->getDefinition() && "Category on a class without a definition?");
5216 ObjCClassesWithCategories.insert(
5217 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005218}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005219
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005220
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005221void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5222 const ObjCPropertyDecl *OrigProp,
5223 const ObjCCategoryDecl *ClassExt) {
5224 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5225 if (!D)
5226 return;
5227
5228 assert(!WritingAST && "Already writing the AST!");
5229 if (!D->isFromASTFile())
5230 return; // Declaration not imported from PCH.
5231
5232 RewriteDecl(D);
5233}