blob: 716b1fbb3064bbc5c59700118100a90e08ade707 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
Sebastian Redlade50002010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000059static StringRef data(const std::vector<T, Allocator> &v) {
60 if (v.empty()) return StringRef();
61 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000063}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000069}
70
Douglas Gregor2cf26342009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000074
Douglas Gregor2cf26342009-04-09 22:27:44 +000075namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000086
87 void VisitArrayType(const ArrayType *T);
88 void VisitFunctionType(const FunctionType *T);
89 void VisitTagType(const TagType *T);
90
91#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
92#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000113 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114}
115
Sebastian Redl3397c552010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000117 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
118 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000123 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000124 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125}
126
Sebastian Redl3397c552010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 Writer.AddTypeRef(T->getPointeeType(), Record);
129 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000136 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137}
138
Sebastian Redl3397c552010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140 VisitArrayType(T);
141 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000147 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
Sebastian Redl3397c552010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000152 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
153 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000154 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000155 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156}
157
Sebastian Redl3397c552010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getElementType(), Record);
160 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000161 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000162 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163}
164
Sebastian Redl3397c552010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000167 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168}
169
Sebastian Redl3397c552010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000172 FunctionType::ExtInfo C = T->getExtInfo();
173 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000174 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000175 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000176 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000177 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000178 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179}
180
Sebastian Redl3397c552010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000183 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184}
185
Sebastian Redl3397c552010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187 VisitFunctionType(T);
188 Record.push_back(T->getNumArgs());
189 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
190 Writer.AddTypeRef(T->getArgType(I), Record);
191 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000192 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000194 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000195 Record.push_back(T->getExceptionSpecType());
196 if (T->getExceptionSpecType() == EST_Dynamic) {
197 Record.push_back(T->getNumExceptions());
198 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
199 Writer.AddTypeRef(T->getExceptionType(I), Record);
200 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
201 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000202 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
204 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000205 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
206 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000207 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000208 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209}
210
Sebastian Redl3397c552010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000212 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000214}
John McCalled976492009-12-04 22:46:56 +0000215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000218 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
219 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226}
227
Sebastian Redl3397c552010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000230 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231}
232
Sebastian Redl3397c552010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000234 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000235 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000236 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000237}
238
Sean Huntca63c202011-05-24 22:41:36 +0000239void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
240 Writer.AddTypeRef(T->getBaseType(), Record);
241 Writer.AddTypeRef(T->getUnderlyingType(), Record);
242 Record.push_back(T->getUTTKind());
243 Code = TYPE_UNARY_TRANSFORM;
244}
245
Richard Smith34b41d92011-02-20 03:19:35 +0000246void ASTTypeWriter::VisitAutoType(const AutoType *T) {
247 Writer.AddTypeRef(T->getDeducedType(), Record);
248 Code = TYPE_AUTO;
249}
250
Sebastian Redl3397c552010-08-18 23:56:27 +0000251void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000252 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000253 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000254 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255 "Cannot serialize in the middle of a type definition");
256}
257
Sebastian Redl3397c552010-08-18 23:56:27 +0000258void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000259 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000260 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261}
262
Sebastian Redl3397c552010-08-18 23:56:27 +0000263void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000265 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000266}
267
John McCall9d156a72011-01-06 01:58:22 +0000268void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
269 Writer.AddTypeRef(T->getModifiedType(), Record);
270 Writer.AddTypeRef(T->getEquivalentType(), Record);
271 Record.push_back(T->getAttrKind());
272 Code = TYPE_ATTRIBUTED;
273}
274
Mike Stump1eb44332009-09-09 15:08:12 +0000275void
Sebastian Redl3397c552010-08-18 23:56:27 +0000276ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000277 const SubstTemplateTypeParmType *T) {
278 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
279 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000280 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000281}
282
283void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000284ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
285 const SubstTemplateTypeParmPackType *T) {
286 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
287 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
288 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
289}
290
291void
Sebastian Redl3397c552010-08-18 23:56:27 +0000292ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000293 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000294 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000295 Writer.AddTemplateName(T->getTemplateName(), Record);
296 Record.push_back(T->getNumArgs());
297 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
298 ArgI != ArgE; ++ArgI)
299 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000300 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
301 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000302 : T->getCanonicalTypeInternal(),
303 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000304 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000305}
306
307void
Sebastian Redl3397c552010-08-18 23:56:27 +0000308ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000309 VisitArrayType(T);
310 Writer.AddStmt(T->getSizeExpr());
311 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000312 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313}
314
315void
Sebastian Redl3397c552010-08-18 23:56:27 +0000316ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317 const DependentSizedExtVectorType *T) {
318 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000319 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320}
321
322void
Sebastian Redl3397c552010-08-18 23:56:27 +0000323ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000324 Record.push_back(T->getDepth());
325 Record.push_back(T->getIndex());
326 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000327 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000333 Record.push_back(T->getKeyword());
334 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
335 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000336 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
337 : T->getCanonicalTypeInternal(),
338 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000339 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl3397c552010-08-18 23:56:27 +0000343ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000344 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000345 Record.push_back(T->getKeyword());
346 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
347 Writer.AddIdentifierRef(T->getIdentifier(), Record);
348 Record.push_back(T->getNumArgs());
349 for (DependentTemplateSpecializationType::iterator
350 I = T->begin(), E = T->end(); I != E; ++I)
351 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000352 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000353}
354
Douglas Gregor7536dd52010-12-20 02:24:11 +0000355void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
356 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000357 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000358 Record.push_back(*NumExpansions + 1);
359 else
360 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000361 Code = TYPE_PACK_EXPANSION;
362}
363
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000364void ASTTypeWriter::VisitParenType(const ParenType *T) {
365 Writer.AddTypeRef(T->getInnerType(), Record);
366 Code = TYPE_PAREN;
367}
368
Sebastian Redl3397c552010-08-18 23:56:27 +0000369void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000370 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000371 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
372 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000374}
375
Sebastian Redl3397c552010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000377 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000378 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000379 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000380}
381
Sebastian Redl3397c552010-08-18 23:56:27 +0000382void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000383 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000384 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000385}
386
Sebastian Redl3397c552010-08-18 23:56:27 +0000387void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000388 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000389 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000390 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000391 E = T->qual_end(); I != E; ++I)
392 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000393 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000394}
395
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000396void
Sebastian Redl3397c552010-08-18 23:56:27 +0000397ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000398 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000399 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000400}
401
Eli Friedmanb001de72011-10-06 23:00:33 +0000402void
403ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
404 Writer.AddTypeRef(T->getValueType(), Record);
405 Code = TYPE_ATOMIC;
406}
407
John McCalla1ee0c52009-10-16 21:56:05 +0000408namespace {
409
410class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000411 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000412 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000413
414public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000415 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000416 : Writer(Writer), Record(Record) { }
417
John McCall51bd8032009-10-18 01:05:36 +0000418#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000419#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000420 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000421#include "clang/AST/TypeLocNodes.def"
422
John McCall51bd8032009-10-18 01:05:36 +0000423 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
424 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000425};
426
427}
428
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
430 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000431}
John McCall51bd8032009-10-18 01:05:36 +0000432void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000433 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
434 if (TL.needsExtraLocalData()) {
435 Record.push_back(TL.getWrittenTypeSpec());
436 Record.push_back(TL.getWrittenSignSpec());
437 Record.push_back(TL.getWrittenWidthSpec());
438 Record.push_back(TL.hasModeAttr());
439 }
John McCalla1ee0c52009-10-16 21:56:05 +0000440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000446}
John McCall51bd8032009-10-18 01:05:36 +0000447void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000458 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000459}
John McCall51bd8032009-10-18 01:05:36 +0000460void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
462 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
463 Record.push_back(TL.getSizeExpr() ? 1 : 0);
464 if (TL.getSizeExpr())
465 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000466}
John McCall51bd8032009-10-18 01:05:36 +0000467void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
468 VisitArrayTypeLoc(TL);
469}
470void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
471 VisitArrayTypeLoc(TL);
472}
473void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
477 DependentSizedArrayTypeLoc TL) {
478 VisitArrayTypeLoc(TL);
479}
480void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
481 DependentSizedExtVectorTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
484void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
487void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
490void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000491 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000492 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
493 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000494 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000495 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
496 Writer.AddDeclRef(TL.getArg(i), Record);
497}
498void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
499 VisitFunctionTypeLoc(TL);
500}
501void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
502 VisitFunctionTypeLoc(TL);
503}
John McCalled976492009-12-04 22:46:56 +0000504void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
John McCall51bd8032009-10-18 01:05:36 +0000507void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000511 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
512 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
513 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000514}
515void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000516 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
517 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
518 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
519 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000520}
521void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
Sean Huntca63c202011-05-24 22:41:36 +0000524void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getKWLoc(), Record);
526 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
527 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
528 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
529}
Richard Smith34b41d92011-02-20 03:19:35 +0000530void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
John McCall51bd8032009-10-18 01:05:36 +0000533void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
536void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getNameLoc(), Record);
538}
John McCall9d156a72011-01-06 01:58:22 +0000539void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
540 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
541 if (TL.hasAttrOperand()) {
542 SourceRange range = TL.getAttrOperandParensRange();
543 Writer.AddSourceLocation(range.getBegin(), Record);
544 Writer.AddSourceLocation(range.getEnd(), Record);
545 }
546 if (TL.hasAttrExprOperand()) {
547 Expr *operand = TL.getAttrExprOperand();
548 Record.push_back(operand ? 1 : 0);
549 if (operand) Writer.AddStmt(operand);
550 } else if (TL.hasAttrEnumOperand()) {
551 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
552 }
553}
John McCall51bd8032009-10-18 01:05:36 +0000554void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
John McCall49a832b2009-10-18 09:09:24 +0000557void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
558 SubstTemplateTypeParmTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getNameLoc(), Record);
560}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000561void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
562 SubstTemplateTypeParmPackTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getNameLoc(), Record);
564}
John McCall51bd8032009-10-18 01:05:36 +0000565void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
566 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000567 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000568 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
569 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
570 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
571 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000572 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
573 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000574}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000575void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
576 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
577 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
578}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000579void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000580 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000582}
John McCall3cb0ebd2010-03-10 03:28:59 +0000583void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getNameLoc(), Record);
585}
Douglas Gregor4714c122010-03-31 17:34:00 +0000586void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000587 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000588 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000589 Writer.AddSourceLocation(TL.getNameLoc(), Record);
590}
John McCall33500952010-06-11 00:33:02 +0000591void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
592 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000594 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000595 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000596 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000600 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
601 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000602}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000603void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
605}
John McCall51bd8032009-10-18 01:05:36 +0000606void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
607 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000608}
609void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
610 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000611 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
612 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
613 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
614 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000615}
John McCall54e14c42009-10-22 22:37:11 +0000616void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
617 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000618}
Eli Friedmanb001de72011-10-06 23:00:33 +0000619void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
620 Writer.AddSourceLocation(TL.getKWLoc(), Record);
621 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
622 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
623}
John McCalla1ee0c52009-10-16 21:56:05 +0000624
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000625//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000626// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000627//===----------------------------------------------------------------------===//
628
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000629static void EmitBlockID(unsigned ID, const char *Name,
630 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 Record.clear();
633 Record.push_back(ID);
634 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
635
636 // Emit the block name if present.
637 if (Name == 0 || Name[0] == 0) return;
638 Record.clear();
639 while (*Name)
640 Record.push_back(*Name++);
641 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
642}
643
644static void EmitRecordID(unsigned ID, const char *Name,
645 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000646 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000647 Record.clear();
648 Record.push_back(ID);
649 while (*Name)
650 Record.push_back(*Name++);
651 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000652}
653
654static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000655 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000656#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000657 RECORD(STMT_STOP);
658 RECORD(STMT_NULL_PTR);
659 RECORD(STMT_NULL);
660 RECORD(STMT_COMPOUND);
661 RECORD(STMT_CASE);
662 RECORD(STMT_DEFAULT);
663 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000664 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000665 RECORD(STMT_IF);
666 RECORD(STMT_SWITCH);
667 RECORD(STMT_WHILE);
668 RECORD(STMT_DO);
669 RECORD(STMT_FOR);
670 RECORD(STMT_GOTO);
671 RECORD(STMT_INDIRECT_GOTO);
672 RECORD(STMT_CONTINUE);
673 RECORD(STMT_BREAK);
674 RECORD(STMT_RETURN);
675 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000676 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000677 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000678 RECORD(EXPR_PREDEFINED);
679 RECORD(EXPR_DECL_REF);
680 RECORD(EXPR_INTEGER_LITERAL);
681 RECORD(EXPR_FLOATING_LITERAL);
682 RECORD(EXPR_IMAGINARY_LITERAL);
683 RECORD(EXPR_STRING_LITERAL);
684 RECORD(EXPR_CHARACTER_LITERAL);
685 RECORD(EXPR_PAREN);
686 RECORD(EXPR_UNARY_OPERATOR);
687 RECORD(EXPR_SIZEOF_ALIGN_OF);
688 RECORD(EXPR_ARRAY_SUBSCRIPT);
689 RECORD(EXPR_CALL);
690 RECORD(EXPR_MEMBER);
691 RECORD(EXPR_BINARY_OPERATOR);
692 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
693 RECORD(EXPR_CONDITIONAL_OPERATOR);
694 RECORD(EXPR_IMPLICIT_CAST);
695 RECORD(EXPR_CSTYLE_CAST);
696 RECORD(EXPR_COMPOUND_LITERAL);
697 RECORD(EXPR_EXT_VECTOR_ELEMENT);
698 RECORD(EXPR_INIT_LIST);
699 RECORD(EXPR_DESIGNATED_INIT);
700 RECORD(EXPR_IMPLICIT_VALUE_INIT);
701 RECORD(EXPR_VA_ARG);
702 RECORD(EXPR_ADDR_LABEL);
703 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000704 RECORD(EXPR_CHOOSE);
705 RECORD(EXPR_GNU_NULL);
706 RECORD(EXPR_SHUFFLE_VECTOR);
707 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000708 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000709 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000710 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000711 RECORD(EXPR_OBJC_ARRAY_LITERAL);
712 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000713 RECORD(EXPR_OBJC_ENCODE);
714 RECORD(EXPR_OBJC_SELECTOR_EXPR);
715 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
716 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
717 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
718 RECORD(EXPR_OBJC_KVC_REF_EXPR);
719 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000720 RECORD(STMT_OBJC_FOR_COLLECTION);
721 RECORD(STMT_OBJC_CATCH);
722 RECORD(STMT_OBJC_FINALLY);
723 RECORD(STMT_OBJC_AT_TRY);
724 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
725 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000726 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000727 RECORD(EXPR_CXX_OPERATOR_CALL);
728 RECORD(EXPR_CXX_CONSTRUCT);
729 RECORD(EXPR_CXX_STATIC_CAST);
730 RECORD(EXPR_CXX_DYNAMIC_CAST);
731 RECORD(EXPR_CXX_REINTERPRET_CAST);
732 RECORD(EXPR_CXX_CONST_CAST);
733 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000734 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000735 RECORD(EXPR_CXX_BOOL_LITERAL);
736 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000737 RECORD(EXPR_CXX_TYPEID_EXPR);
738 RECORD(EXPR_CXX_TYPEID_TYPE);
739 RECORD(EXPR_CXX_UUIDOF_EXPR);
740 RECORD(EXPR_CXX_UUIDOF_TYPE);
741 RECORD(EXPR_CXX_THIS);
742 RECORD(EXPR_CXX_THROW);
743 RECORD(EXPR_CXX_DEFAULT_ARG);
744 RECORD(EXPR_CXX_BIND_TEMPORARY);
745 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
746 RECORD(EXPR_CXX_NEW);
747 RECORD(EXPR_CXX_DELETE);
748 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
749 RECORD(EXPR_EXPR_WITH_CLEANUPS);
750 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
751 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
752 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
753 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
754 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
755 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
756 RECORD(EXPR_CXX_NOEXCEPT);
757 RECORD(EXPR_OPAQUE_VALUE);
758 RECORD(EXPR_BINARY_TYPE_TRAIT);
759 RECORD(EXPR_PACK_EXPANSION);
760 RECORD(EXPR_SIZEOF_PACK);
761 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000762 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000763#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764}
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Sebastian Redla4232eb2010-08-18 23:56:21 +0000766void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000767 RecordData Record;
768 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000770#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
771#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000773 // Control Block.
774 BLOCK(CONTROL_BLOCK);
775 RECORD(METADATA);
776 RECORD(IMPORTS);
777 RECORD(LANGUAGE_OPTIONS);
778 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000779 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000780 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000781 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000782 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000783 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000784 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000785 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000786 RECORD(PREPROCESSOR_OPTIONS);
787
Douglas Gregorc337fef2012-10-19 00:45:00 +0000788 BLOCK(INPUT_FILES_BLOCK);
789 RECORD(INPUT_FILE);
790
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000791 // AST Top-Level Block.
792 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000793 RECORD(TYPE_OFFSET);
794 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000795 RECORD(IDENTIFIER_OFFSET);
796 RECORD(IDENTIFIER_TABLE);
797 RECORD(EXTERNAL_DEFINITIONS);
798 RECORD(SPECIAL_TYPES);
799 RECORD(STATISTICS);
800 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000801 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000802 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000803 RECORD(SELECTOR_OFFSETS);
804 RECORD(METHOD_POOL);
805 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000806 RECORD(SOURCE_LOCATION_OFFSETS);
807 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000808 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000809 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000810 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000811 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000812 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000813 RECORD(SEMA_DECL_REFS);
814 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
815 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
816 RECORD(DECL_REPLACEMENTS);
817 RECORD(UPDATE_VISIBLE);
818 RECORD(DECL_UPDATE_OFFSETS);
819 RECORD(DECL_UPDATES);
820 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
821 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000822 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000823 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000824 RECORD(FP_PRAGMA_OPTIONS);
825 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000826 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000827 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000828 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000829 RECORD(MODULE_OFFSET_MAP);
830 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000831 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000832 RECORD(FILE_SORTED_DECLS);
833 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000834 RECORD(MERGED_DECLARATIONS);
835 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000836 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000837 RECORD(MACRO_OFFSET);
838 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000839
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000841 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000842 RECORD(SM_SLOC_FILE_ENTRY);
843 RECORD(SM_SLOC_BUFFER_ENTRY);
844 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000845 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000847 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000848 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000849 RECORD(PP_MACRO_OBJECT_LIKE);
850 RECORD(PP_MACRO_FUNCTION_LIKE);
851 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000852
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000853 // Decls and Types block.
854 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000855 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000856 RECORD(TYPE_COMPLEX);
857 RECORD(TYPE_POINTER);
858 RECORD(TYPE_BLOCK_POINTER);
859 RECORD(TYPE_LVALUE_REFERENCE);
860 RECORD(TYPE_RVALUE_REFERENCE);
861 RECORD(TYPE_MEMBER_POINTER);
862 RECORD(TYPE_CONSTANT_ARRAY);
863 RECORD(TYPE_INCOMPLETE_ARRAY);
864 RECORD(TYPE_VARIABLE_ARRAY);
865 RECORD(TYPE_VECTOR);
866 RECORD(TYPE_EXT_VECTOR);
867 RECORD(TYPE_FUNCTION_PROTO);
868 RECORD(TYPE_FUNCTION_NO_PROTO);
869 RECORD(TYPE_TYPEDEF);
870 RECORD(TYPE_TYPEOF_EXPR);
871 RECORD(TYPE_TYPEOF);
872 RECORD(TYPE_RECORD);
873 RECORD(TYPE_ENUM);
874 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000875 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000876 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000877 RECORD(TYPE_DECLTYPE);
878 RECORD(TYPE_ELABORATED);
879 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
880 RECORD(TYPE_UNRESOLVED_USING);
881 RECORD(TYPE_INJECTED_CLASS_NAME);
882 RECORD(TYPE_OBJC_OBJECT);
883 RECORD(TYPE_TEMPLATE_TYPE_PARM);
884 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
885 RECORD(TYPE_DEPENDENT_NAME);
886 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
887 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
888 RECORD(TYPE_PAREN);
889 RECORD(TYPE_PACK_EXPANSION);
890 RECORD(TYPE_ATTRIBUTED);
891 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000892 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000893 RECORD(DECL_TYPEDEF);
894 RECORD(DECL_ENUM);
895 RECORD(DECL_RECORD);
896 RECORD(DECL_ENUM_CONSTANT);
897 RECORD(DECL_FUNCTION);
898 RECORD(DECL_OBJC_METHOD);
899 RECORD(DECL_OBJC_INTERFACE);
900 RECORD(DECL_OBJC_PROTOCOL);
901 RECORD(DECL_OBJC_IVAR);
902 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000903 RECORD(DECL_OBJC_CATEGORY);
904 RECORD(DECL_OBJC_CATEGORY_IMPL);
905 RECORD(DECL_OBJC_IMPLEMENTATION);
906 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
907 RECORD(DECL_OBJC_PROPERTY);
908 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000909 RECORD(DECL_FIELD);
910 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000911 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000912 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000913 RECORD(DECL_FILE_SCOPE_ASM);
914 RECORD(DECL_BLOCK);
915 RECORD(DECL_CONTEXT_LEXICAL);
916 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000917 RECORD(DECL_NAMESPACE);
918 RECORD(DECL_NAMESPACE_ALIAS);
919 RECORD(DECL_USING);
920 RECORD(DECL_USING_SHADOW);
921 RECORD(DECL_USING_DIRECTIVE);
922 RECORD(DECL_UNRESOLVED_USING_VALUE);
923 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
924 RECORD(DECL_LINKAGE_SPEC);
925 RECORD(DECL_CXX_RECORD);
926 RECORD(DECL_CXX_METHOD);
927 RECORD(DECL_CXX_CONSTRUCTOR);
928 RECORD(DECL_CXX_DESTRUCTOR);
929 RECORD(DECL_CXX_CONVERSION);
930 RECORD(DECL_ACCESS_SPEC);
931 RECORD(DECL_FRIEND);
932 RECORD(DECL_FRIEND_TEMPLATE);
933 RECORD(DECL_CLASS_TEMPLATE);
934 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
935 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
936 RECORD(DECL_FUNCTION_TEMPLATE);
937 RECORD(DECL_TEMPLATE_TYPE_PARM);
938 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
939 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
940 RECORD(DECL_STATIC_ASSERT);
941 RECORD(DECL_CXX_BASE_SPECIFIERS);
942 RECORD(DECL_INDIRECTFIELD);
943 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
944
Douglas Gregora72d8c42011-06-03 02:27:19 +0000945 // Statements and Exprs can occur in the Decls and Types block.
946 AddStmtsExprs(Stream, Record);
947
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000948 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000949 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000950 RECORD(PPD_MACRO_DEFINITION);
951 RECORD(PPD_INCLUSION_DIRECTIVE);
952
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000953#undef RECORD
954#undef BLOCK
955 Stream.ExitBlock();
956}
957
Douglas Gregore650c8c2009-07-07 00:12:59 +0000958/// \brief Adjusts the given filename to only write out the portion of the
959/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000960///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961/// \param Filename the file name to adjust.
962///
963/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
964/// the returned filename will be adjusted by this system root.
965///
966/// \returns either the original filename (if it needs no adjustment) or the
967/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000968static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000969adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregor832d6202011-07-22 16:35:34 +0000972 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 // Verify that the filename and the system root have the same prefix.
976 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000977 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978 if (Filename[Pos] != isysroot[Pos])
979 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 // We hit the end of the filename before we hit the end of the system root.
982 if (!Filename[Pos])
983 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 // If the file name has a '/' at the current position, skip over the '/'.
986 // We distinguish sysroot-based includes from absolute includes by the
987 // absence of '/' at the beginning of sysroot-based includes.
988 if (Filename[Pos] == '/')
989 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991 return Filename + Pos;
992}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000993
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000994/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000995void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
996 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000997 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000998 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000999 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1000 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001001
Douglas Gregore650c8c2009-07-07 00:12:59 +00001002 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001003 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1011 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1012 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1013 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001014 Record.push_back(VERSION_MAJOR);
1015 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001016 Record.push_back(CLANG_VERSION_MAJOR);
1017 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001018 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001019 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001020 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1021 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001022
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001023 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001024 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001025 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001026 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001027 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001028
1029 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1030 M != MEnd; ++M) {
1031 // Skip modules that weren't directly imported.
1032 if (!(*M)->isDirectlyImported())
1033 continue;
1034
1035 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001036 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001037 // FIXME: This writes the absolute path for AST files we depend on.
1038 const std::string &FileName = (*M)->FileName;
1039 Record.push_back(FileName.size());
1040 Record.append(FileName.begin(), FileName.end());
1041 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001042 Stream.EmitRecord(IMPORTS, Record);
1043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001045 // Language options.
1046 Record.clear();
1047 const LangOptions &LangOpts = Context.getLangOpts();
1048#define LANGOPT(Name, Bits, Default, Description) \
1049 Record.push_back(LangOpts.Name);
1050#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1051 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1052#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001053#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1054#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001055
1056 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1057 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1058
1059 Record.push_back(LangOpts.CurrentModule.size());
1060 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001061
1062 // Comment options.
1063 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1064 for (CommentOptions::BlockCommandNamesTy::const_iterator
1065 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1066 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1067 I != IEnd; ++I) {
1068 AddString(*I, Record);
1069 }
1070
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001071 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1072
Douglas Gregoree097c12012-10-18 17:58:09 +00001073 // Target options.
1074 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001075 const TargetInfo &Target = Context.getTargetInfo();
1076 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001077 AddString(TargetOpts.Triple, Record);
1078 AddString(TargetOpts.CPU, Record);
1079 AddString(TargetOpts.ABI, Record);
1080 AddString(TargetOpts.CXXABI, Record);
1081 AddString(TargetOpts.LinkerVersion, Record);
1082 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1083 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1084 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1085 }
1086 Record.push_back(TargetOpts.Features.size());
1087 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1088 AddString(TargetOpts.Features[I], Record);
1089 }
1090 Stream.EmitRecord(TARGET_OPTIONS, Record);
1091
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001092 // Diagnostic options.
1093 Record.clear();
1094 const DiagnosticOptions &DiagOpts
1095 = Context.getDiagnostics().getDiagnosticOptions();
1096#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1097#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1098 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1099#include "clang/Basic/DiagnosticOptions.def"
1100 Record.push_back(DiagOpts.Warnings.size());
1101 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1102 AddString(DiagOpts.Warnings[I], Record);
1103 // Note: we don't serialize the log or serialization file names, because they
1104 // are generally transient files and will almost always be overridden.
1105 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1106
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001107 // File system options.
1108 Record.clear();
1109 const FileSystemOptions &FSOpts
1110 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1111 AddString(FSOpts.WorkingDir, Record);
1112 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1113
Douglas Gregorbbf38312012-10-24 16:50:34 +00001114 // Header search options.
1115 Record.clear();
1116 const HeaderSearchOptions &HSOpts
1117 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1118 AddString(HSOpts.Sysroot, Record);
1119
1120 // Include entries.
1121 Record.push_back(HSOpts.UserEntries.size());
1122 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1123 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1124 AddString(Entry.Path, Record);
1125 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001126 Record.push_back(Entry.IsFramework);
1127 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001128 }
1129
1130 // System header prefixes.
1131 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1132 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1133 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1134 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1135 }
1136
1137 AddString(HSOpts.ResourceDir, Record);
1138 AddString(HSOpts.ModuleCachePath, Record);
1139 Record.push_back(HSOpts.DisableModuleHash);
1140 Record.push_back(HSOpts.UseBuiltinIncludes);
1141 Record.push_back(HSOpts.UseStandardSystemIncludes);
1142 Record.push_back(HSOpts.UseStandardCXXIncludes);
1143 Record.push_back(HSOpts.UseLibcxx);
1144 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1145
Douglas Gregora71a7d82012-10-24 20:05:57 +00001146 // Preprocessor options.
1147 Record.clear();
1148 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1149
1150 // Macro definitions.
1151 Record.push_back(PPOpts.Macros.size());
1152 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1153 AddString(PPOpts.Macros[I].first, Record);
1154 Record.push_back(PPOpts.Macros[I].second);
1155 }
1156
1157 // Includes
1158 Record.push_back(PPOpts.Includes.size());
1159 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1160 AddString(PPOpts.Includes[I], Record);
1161
1162 // Macro includes
1163 Record.push_back(PPOpts.MacroIncludes.size());
1164 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1165 AddString(PPOpts.MacroIncludes[I], Record);
1166
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001167 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001168 AddString(PPOpts.ImplicitPCHInclude, Record);
1169 AddString(PPOpts.ImplicitPTHInclude, Record);
1170 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1171 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1172
Douglas Gregor31d375f2011-05-06 21:43:30 +00001173 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001174 SourceManager &SM = Context.getSourceManager();
1175 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1176 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001177 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1178 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001179 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1180 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1181
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001182 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001184 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001185
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001186 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001187 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001188 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001189 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001190 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001191 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001192 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001193 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001194
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001195 Record.clear();
1196 Record.push_back(SM.getMainFileID().getOpaqueValue());
1197 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1198
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001199 // Original PCH directory
1200 if (!OutputFile.empty() && OutputFile != "-") {
1201 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1202 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1204 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1205
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001206 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001207
1208 llvm::sys::fs::make_absolute(OutputPath);
1209 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1210
1211 RecordData Record;
1212 Record.push_back(ORIGINAL_PCH_DIR);
1213 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1214 }
1215
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001216 WriteInputFiles(Context.SourceMgr,
1217 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1218 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001219 Stream.ExitBlock();
1220}
1221
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001222namespace {
1223 /// \brief An input file.
1224 struct InputFileEntry {
1225 const FileEntry *File;
1226 bool IsSystemFile;
1227 bool BufferOverridden;
1228 };
1229}
1230
1231void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1232 HeaderSearchOptions &HSOpts,
1233 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001234 using namespace llvm;
1235 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1236 RecordData Record;
1237
1238 // Create input-file abbreviation.
1239 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1240 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001241 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001242 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1243 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001244 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001245 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1246 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1247
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001248 // Get all ContentCache objects for files, sorted by whether the file is a
1249 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001250 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001251 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1252 // Get this source location entry.
1253 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001254 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001255
1256 // We only care about file entries that were not overridden.
1257 if (!SLoc->isFile())
1258 continue;
1259 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001260 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001261 continue;
1262
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001263 InputFileEntry Entry;
1264 Entry.File = Cache->OrigEntry;
1265 Entry.IsSystemFile = Cache->IsSystemFile;
1266 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001267 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001268 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001269 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001270 SortedFiles.push_front(Entry);
1271 }
1272
1273 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1274 // the set of (non-system) input files. This is simple heuristic for
1275 // detecting whether the system headers may have changed, because it is too
1276 // expensive to stat() all of the system headers.
1277 FileManager &FileMgr = SourceMgr.getFileManager();
1278 if (!HSOpts.Sysroot.empty()) {
1279 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1280 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1281 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1282 InputFileEntry Entry = { SDKSettingsFile, false, false };
1283 SortedFiles.push_front(Entry);
1284 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001285 }
1286
1287 unsigned UserFilesNum = 0;
1288 // Write out all of the input files.
1289 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001290 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001291 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001292 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001293
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001294 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001295 if (InputFileID != 0)
1296 continue; // already recorded this file.
1297
Douglas Gregora930dc92012-10-22 18:42:04 +00001298 // Record this entry's offset.
1299 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001300
1301 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001302
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001303 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001304 ++UserFilesNum;
1305
Douglas Gregor745e6f12012-10-19 00:38:02 +00001306 Record.clear();
1307 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001308 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001309
1310 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001311 Record.push_back(Entry.File->getSize());
1312 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001313
Douglas Gregora930dc92012-10-22 18:42:04 +00001314 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001315 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001316
Douglas Gregor745e6f12012-10-19 00:38:02 +00001317 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001318 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001319 SmallString<128> FilePath(Filename);
1320
1321 // Ask the file manager to fixup the relative path for us. This will
1322 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001323 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001324
1325 // FIXME: This call to make_absolute shouldn't be necessary, the
1326 // call to FixupRelativePath should always return an absolute path.
1327 llvm::sys::fs::make_absolute(FilePath);
1328 Filename = FilePath.c_str();
1329
1330 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1331
1332 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1333 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001334
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001335 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001336
1337 // Create input file offsets abbreviation.
1338 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1339 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1340 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001341 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1342 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001343 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1344 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1345
1346 // Write input file offsets.
1347 Record.clear();
1348 Record.push_back(INPUT_FILE_OFFSETS);
1349 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001350 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001351 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001352}
1353
Douglas Gregor14f79002009-04-10 03:52:48 +00001354//===----------------------------------------------------------------------===//
1355// Source Manager Serialization
1356//===----------------------------------------------------------------------===//
1357
1358/// \brief Create an abbreviation for the SLocEntry that refers to a
1359/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001360static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001361 using namespace llvm;
1362 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001363 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001368 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001373 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001374}
1375
1376/// \brief Create an abbreviation for the SLocEntry that refers to a
1377/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001378static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001379 using namespace llvm;
1380 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001381 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001387 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001388}
1389
1390/// \brief Create an abbreviation for the SLocEntry that refers to a
1391/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001392static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001393 using namespace llvm;
1394 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001395 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001397 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001398}
1399
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001400/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1401/// expansion.
1402static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001403 using namespace llvm;
1404 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001405 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001411 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001412}
1413
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001414namespace {
1415 // Trait used for the on-disk hash table of header search information.
1416 class HeaderFileInfoTrait {
1417 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001418 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001419
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001420 // Keep track of the framework names we've used during serialization.
1421 SmallVector<char, 128> FrameworkStringData;
1422 llvm::StringMap<unsigned> FrameworkNameOffset;
1423
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001424 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001425 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1426 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001427
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001428 struct key_type {
1429 const FileEntry *FE;
1430 const char *Filename;
1431 };
1432 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001433
1434 typedef HeaderFileInfo data_type;
1435 typedef const data_type &data_type_ref;
1436
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001437 static unsigned ComputeHash(key_type_ref key) {
1438 // The hash is based only on size/time of the file, so that the reader can
1439 // match even when symlinking or excess path elements ("foo/../", "../")
1440 // change the form of the name. However, complete path is still the key.
1441 return llvm::hash_combine(key.FE->getSize(),
1442 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001443 }
1444
1445 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001446 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1447 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1448 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001449 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001450 if (Data.isModuleHeader)
1451 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001452 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001453 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001454 }
1455
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001456 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1457 clang::io::Emit64(Out, key.FE->getSize());
1458 KeyLen -= 8;
1459 clang::io::Emit64(Out, key.FE->getModificationTime());
1460 KeyLen -= 8;
1461 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001462 }
1463
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001464 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001465 data_type_ref Data, unsigned DataLen) {
1466 using namespace clang::io;
1467 uint64_t Start = Out.tell(); (void)Start;
1468
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001469 unsigned char Flags = (Data.isImport << 5)
1470 | (Data.isPragmaOnce << 4)
1471 | (Data.DirInfo << 2)
1472 | (Data.Resolved << 1)
1473 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001474 Emit8(Out, (uint8_t)Flags);
1475 Emit16(Out, (uint16_t) Data.NumIncludes);
1476
1477 if (!Data.ControllingMacro)
1478 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1479 else
1480 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001481
1482 unsigned Offset = 0;
1483 if (!Data.Framework.empty()) {
1484 // If this header refers into a framework, save the framework name.
1485 llvm::StringMap<unsigned>::iterator Pos
1486 = FrameworkNameOffset.find(Data.Framework);
1487 if (Pos == FrameworkNameOffset.end()) {
1488 Offset = FrameworkStringData.size() + 1;
1489 FrameworkStringData.append(Data.Framework.begin(),
1490 Data.Framework.end());
1491 FrameworkStringData.push_back(0);
1492
1493 FrameworkNameOffset[Data.Framework] = Offset;
1494 } else
1495 Offset = Pos->second;
1496 }
1497 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001498
1499 if (Data.isModuleHeader) {
1500 Module *Mod = HS.findModuleForHeader(key.FE);
1501 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1502 }
1503
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001504 assert(Out.tell() - Start == DataLen && "Wrong data length");
1505 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001506
1507 const char *strings_begin() const { return FrameworkStringData.begin(); }
1508 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001509 };
1510} // end anonymous namespace
1511
1512/// \brief Write the header search block for the list of files that
1513///
1514/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001515void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001516 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001517 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1518
1519 if (FilesByUID.size() > HS.header_file_size())
1520 FilesByUID.resize(HS.header_file_size());
1521
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001522 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001523 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001524 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001525 unsigned NumHeaderSearchEntries = 0;
1526 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1527 const FileEntry *File = FilesByUID[UID];
1528 if (!File)
1529 continue;
1530
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001531 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1532 // from the external source if it was not provided already.
1533 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001534 if (HFI.External && Chain)
1535 continue;
1536
1537 // Turn the file name into an absolute path, if it isn't already.
1538 const char *Filename = File->getName();
1539 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1540
1541 // If we performed any translation on the file name at all, we need to
1542 // save this string, since the generator will refer to it later.
1543 if (Filename != File->getName()) {
1544 Filename = strdup(Filename);
1545 SavedStrings.push_back(Filename);
1546 }
1547
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001548 HeaderFileInfoTrait::key_type key = { File, Filename };
1549 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001550 ++NumHeaderSearchEntries;
1551 }
1552
1553 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001554 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001555 uint32_t BucketOffset;
1556 {
1557 llvm::raw_svector_ostream Out(TableData);
1558 // Make sure that no bucket is at offset 0
1559 clang::io::Emit32(Out, 0);
1560 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1561 }
1562
1563 // Create a blob abbreviation
1564 using namespace llvm;
1565 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1566 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1571 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1572
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001573 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001574 RecordData Record;
1575 Record.push_back(HEADER_SEARCH_TABLE);
1576 Record.push_back(BucketOffset);
1577 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001578 Record.push_back(TableData.size());
1579 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001580 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1581
1582 // Free all of the strings we had to duplicate.
1583 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001584 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001585}
1586
Douglas Gregor14f79002009-04-10 03:52:48 +00001587/// \brief Writes the block containing the serialized form of the
1588/// source manager.
1589///
1590/// TODO: We should probably use an on-disk hash table (stored in a
1591/// blob), indexed based on the file name, so that we only create
1592/// entries for files that we actually need. In the common case (no
1593/// errors), we probably won't have to create file entries for any of
1594/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001595void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001596 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001597 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001598 RecordData Record;
1599
Chris Lattnerf04ad692009-04-10 17:16:57 +00001600 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001601 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001602
1603 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001604 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1605 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1606 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001607 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001608
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001609 // Write out the source location entry table. We skip the first
1610 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001611 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001612 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001613 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1614 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001615 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001616 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001617 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001618 FileID FID = FileID::get(I);
1619 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001620
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001621 // Record the offset of this source-location entry.
1622 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1623
1624 // Figure out which record code to use.
1625 unsigned Code;
1626 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001627 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1628 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001629 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001630 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001631 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001632 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001633 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001634 Record.clear();
1635 Record.push_back(Code);
1636
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001637 // Starting offset of this entry within this module, so skip the dummy.
1638 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001639 if (SLoc->isFile()) {
1640 const SrcMgr::FileInfo &File = SLoc->getFile();
1641 Record.push_back(File.getIncludeLoc().getRawEncoding());
1642 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1643 Record.push_back(File.hasLineDirectives());
1644
1645 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001646 if (Content->OrigEntry) {
1647 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001648 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001649
Douglas Gregora930dc92012-10-22 18:42:04 +00001650 // The source location entry is a file. Emit input file ID.
1651 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1652 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001654 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001655
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001656 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001657 if (FDI != FileDeclIDs.end()) {
1658 Record.push_back(FDI->second->FirstDeclIndex);
1659 Record.push_back(FDI->second->DeclIDs.size());
1660 } else {
1661 Record.push_back(0);
1662 Record.push_back(0);
1663 }
Douglas Gregora081da52011-11-16 20:05:18 +00001664
Douglas Gregora930dc92012-10-22 18:42:04 +00001665 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001666
1667 if (Content->BufferOverridden) {
1668 Record.clear();
1669 Record.push_back(SM_SLOC_BUFFER_BLOB);
1670 const llvm::MemoryBuffer *Buffer
1671 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1672 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1673 StringRef(Buffer->getBufferStart(),
1674 Buffer->getBufferSize() + 1));
1675 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001676 } else {
1677 // The source location entry is a buffer. The blob associated
1678 // with this entry contains the contents of the buffer.
1679
1680 // We add one to the size so that we capture the trailing NULL
1681 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1682 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001683 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001684 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001685 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001686 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001687 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001688 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001689 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001690 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001691 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001692 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001693
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001694 if (strcmp(Name, "<built-in>") == 0) {
1695 PreloadSLocs.push_back(SLocEntryOffsets.size());
1696 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001697 }
1698 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001699 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001700 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001701 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1702 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001703 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1704 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001705
1706 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001707 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001708 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001709 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001710 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001711 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001712 }
1713 }
1714
Douglas Gregorc9490c02009-04-16 22:23:12 +00001715 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001716
1717 if (SLocEntryOffsets.empty())
1718 return;
1719
Sebastian Redl3397c552010-08-18 23:56:27 +00001720 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001721 // table is used for lazily loading source-location information.
1722 using namespace llvm;
1723 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001724 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1728 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001730 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001731 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001732 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001733 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001734 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001735
Sebastian Redl3397c552010-08-18 23:56:27 +00001736 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001737 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001738 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001739
1740 // Write the line table. It depends on remapping working, so it must come
1741 // after the source location offsets.
1742 if (SourceMgr.hasLineTable()) {
1743 LineTableInfo &LineTable = SourceMgr.getLineTable();
1744
1745 Record.clear();
1746 // Emit the file names
1747 Record.push_back(LineTable.getNumFilenames());
1748 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1749 // Emit the file name
1750 const char *Filename = LineTable.getFilename(I);
1751 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1752 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1753 Record.push_back(FilenameLen);
1754 if (FilenameLen)
1755 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1756 }
1757
1758 // Emit the line entries
1759 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1760 L != LEnd; ++L) {
1761 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001762 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001763 continue;
1764
1765 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001766 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001767
1768 // Emit the line entries
1769 Record.push_back(L->second.size());
1770 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1771 LEEnd = L->second.end();
1772 LE != LEEnd; ++LE) {
1773 Record.push_back(LE->FileOffset);
1774 Record.push_back(LE->LineNo);
1775 Record.push_back(LE->FilenameID);
1776 Record.push_back((unsigned)LE->FileKind);
1777 Record.push_back(LE->IncludeOffset);
1778 }
1779 }
1780 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1781 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001782}
1783
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001784//===----------------------------------------------------------------------===//
1785// Preprocessor Serialization
1786//===----------------------------------------------------------------------===//
1787
Douglas Gregor9c736102011-02-10 18:20:09 +00001788static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1789 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1790 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1791 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1792 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1793 return X.first->getName().compare(Y.first->getName());
1794}
1795
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001796static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1797 const Preprocessor &PP) {
1798 if (MD->getInfo()->isBuiltinMacro())
1799 return true;
1800
1801 if (IsModule) {
1802 SourceLocation Loc = MD->getLocation();
1803 if (Loc.isInvalid())
1804 return true;
1805 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1806 return true;
1807 }
1808
1809 return false;
1810}
1811
Chris Lattner0b1fb982009-04-10 17:15:23 +00001812/// \brief Writes the block containing the serialized form of the
1813/// preprocessor.
1814///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001815void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001816 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1817 if (PPRec)
1818 WritePreprocessorDetail(*PPRec);
1819
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001820 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001821
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001822 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1823 if (PP.getCounterValue() != 0) {
1824 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001825 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001826 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001827 }
1828
1829 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001830 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Sebastian Redl3397c552010-08-18 23:56:27 +00001832 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001833 // FIXME: use diagnostics subsystem for localization etc.
1834 if (PP.SawDateOrTime())
1835 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Douglas Gregorecdcb882010-10-20 22:00:55 +00001837
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001838 // Loop over all the macro definitions that are live at the end of the file,
1839 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001840
Douglas Gregor9c736102011-02-10 18:20:09 +00001841 // Construct the list of macro definitions that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001842 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001843 MacrosToEmit;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001844 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001845 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001846 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001847 if (!IsModule || I->second->isPublic()) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001848 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001849 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001850 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001851
Douglas Gregor9c736102011-02-10 18:20:09 +00001852 // Sort the set of macro definitions that need to be serialized by the
1853 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001854 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001855 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001856
Douglas Gregora8235d62012-10-09 23:05:51 +00001857 /// \brief Offsets of each of the macros into the bitstream, indexed by
1858 /// the local macro ID
1859 ///
1860 /// For each identifier that is associated with a macro, this map
1861 /// provides the offset into the bitstream where that macro is
1862 /// defined.
1863 std::vector<uint32_t> MacroOffsets;
1864
Douglas Gregor9c736102011-02-10 18:20:09 +00001865 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1866 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001867
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001868 for (MacroDirective *MD = MacrosToEmit[I].second; MD;
1869 MD = MD->getPrevious()) {
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001870 if (shouldIgnoreMacro(MD, IsModule, PP))
1871 continue;
1872
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001873 MacroID ID = getMacroRef(MD);
Douglas Gregora8235d62012-10-09 23:05:51 +00001874 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001875 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Douglas Gregora8235d62012-10-09 23:05:51 +00001877 // Skip macros from a AST file if we're chaining.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001878 if (Chain && MD->isImported() && !MD->hasChangedAfterLoad())
Douglas Gregora8235d62012-10-09 23:05:51 +00001879 continue;
1880
1881 if (ID < FirstMacroID) {
1882 // This will have been dealt with via an update record.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001883 assert(MacroUpdates.count(MD) > 0 && "Missing macro update");
Douglas Gregora8235d62012-10-09 23:05:51 +00001884 continue;
1885 }
1886
1887 // Record the local offset of this macro.
1888 unsigned Index = ID - FirstMacroID;
1889 if (Index == MacroOffsets.size())
1890 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1891 else {
1892 if (Index > MacroOffsets.size())
1893 MacroOffsets.resize(Index + 1);
1894
1895 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1896 }
1897
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001898 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001899 addMacroRef(MD, Record);
1900 const MacroInfo *MI = MD->getInfo();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001901 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001902 AddSourceLocation(MI->getDefinitionLoc(), Record);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001903 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001904 AddSourceLocation(MD->getUndefLoc(), Record);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001905 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001906 Record.push_back(MD->isPublic());
1907 AddSourceLocation(MD->getVisibilityLocation(), Record);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001908 unsigned Code;
1909 if (MI->isObjectLike()) {
1910 Code = PP_MACRO_OBJECT_LIKE;
1911 } else {
1912 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001913
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001914 Record.push_back(MI->isC99Varargs());
1915 Record.push_back(MI->isGNUVarargs());
Eli Friedman4fa4b482012-11-14 02:18:46 +00001916 Record.push_back(MI->hasCommaPasting());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001917 Record.push_back(MI->getNumArgs());
1918 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1919 I != E; ++I)
1920 AddIdentifierRef(*I, Record);
1921 }
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001923 // If we have a detailed preprocessing record, record the macro definition
1924 // ID that corresponds to this macro.
1925 if (PPRec)
1926 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1927
1928 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001929 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001930
1931 // Emit the tokens array.
1932 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1933 // Note that we know that the preprocessor does not have any annotation
1934 // tokens in it because they are created by the parser, and thus can't
1935 // be in a macro definition.
1936 const Token &Tok = MI->getReplacementToken(TokNo);
1937
1938 Record.push_back(Tok.getLocation().getRawEncoding());
1939 Record.push_back(Tok.getLength());
1940
1941 // FIXME: When reading literal tokens, reconstruct the literal pointer
1942 // if it is needed.
1943 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1944 // FIXME: Should translate token kind to a stable encoding.
1945 Record.push_back(Tok.getKind());
1946 // FIXME: Should translate token flags to a stable encoding.
1947 Record.push_back(Tok.getFlags());
1948
1949 Stream.EmitRecord(PP_TOKEN, Record);
1950 Record.clear();
1951 }
1952 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001953 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001954 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001955 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001956
1957 // Write the offsets table for macro IDs.
1958 using namespace llvm;
1959 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1960 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1964
1965 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1966 Record.clear();
1967 Record.push_back(MACRO_OFFSET);
1968 Record.push_back(MacroOffsets.size());
1969 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1970 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1971 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001972}
1973
1974void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001975 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001976 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001977
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001978 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001979
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001980 // Enter the preprocessor block.
1981 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001982
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001983 // If the preprocessor has a preprocessing record, emit it.
1984 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001985 using namespace llvm;
1986
1987 // Set up the abbreviation for
1988 unsigned InclusionAbbrev = 0;
1989 {
1990 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1991 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1997 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1998 }
1999
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002000 unsigned FirstPreprocessorEntityID
2001 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2002 + NUM_PREDEF_PP_ENTITY_IDS;
2003 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002004 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002005 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2006 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002007 E != EEnd;
2008 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002009 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002010
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002011 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2012 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002013
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002014 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002015 // Record this macro definition's ID.
2016 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002017
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002018 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002019 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2020 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002021 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002022
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002023 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002024 Record.push_back(ME->isBuiltinMacro());
2025 if (ME->isBuiltinMacro())
2026 AddIdentifierRef(ME->getName(), Record);
2027 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002028 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002029 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002030 continue;
2031 }
2032
2033 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2034 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002035 Record.push_back(ID->getFileName().size());
2036 Record.push_back(ID->wasInQuotes());
2037 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002038 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002039 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002040 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002041 // Check that the FileEntry is not null because it was not resolved and
2042 // we create a PCH even with compiler errors.
2043 if (ID->getFile())
2044 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002045 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2046 continue;
2047 }
2048
2049 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2050 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002051 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002052
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002053 // Write the offsets table for the preprocessing record.
2054 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002055 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2056
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002057 // Write the offsets table for identifier IDs.
2058 using namespace llvm;
2059 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002060 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002063 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002064
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002065 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002066 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002067 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002068 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2069 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002070 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002071}
2072
Douglas Gregore209e502011-12-06 01:10:29 +00002073unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2074 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2075 if (Known != SubmoduleIDs.end())
2076 return Known->second;
2077
2078 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2079}
2080
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002081unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2082 if (!Mod)
2083 return 0;
2084
2085 llvm::DenseMap<Module *, unsigned>::const_iterator
2086 Known = SubmoduleIDs.find(Mod);
2087 if (Known != SubmoduleIDs.end())
2088 return Known->second;
2089
2090 return 0;
2091}
2092
Douglas Gregor26ced122011-12-01 00:59:36 +00002093/// \brief Compute the number of modules within the given tree (including the
2094/// given module).
2095static unsigned getNumberOfModules(Module *Mod) {
2096 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002097 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2098 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002099 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002100 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002101
2102 return ChildModules + 1;
2103}
2104
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002105void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002106 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002107 // FIXME: This feels like it belongs somewhere else, but there are no
2108 // other consumers of this information.
2109 SourceManager &SrcMgr = PP->getSourceManager();
2110 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2111 for (ASTContext::import_iterator I = Context->local_import_begin(),
2112 IEnd = Context->local_import_end();
2113 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002114 if (Module *ImportedFrom
2115 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2116 SrcMgr))) {
2117 ImportedFrom->Imports.push_back(I->getImportedModule());
2118 }
2119 }
2120
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002121 // Enter the submodule description block.
2122 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2123
2124 // Write the abbreviations needed for the submodules block.
2125 using namespace llvm;
2126 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2127 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002129 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2130 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2137 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2138
2139 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002140 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002141 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2142 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2143
2144 Abbrev = new BitCodeAbbrev();
2145 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2147 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002148
2149 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002150 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2152 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2153
2154 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002155 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2157 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2158
Douglas Gregor51f564f2011-12-31 04:05:44 +00002159 Abbrev = new BitCodeAbbrev();
2160 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2162 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2163
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002164 Abbrev = new BitCodeAbbrev();
2165 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2167 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2168
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002169 Abbrev = new BitCodeAbbrev();
2170 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2173 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2174
Douglas Gregor26ced122011-12-01 00:59:36 +00002175 // Write the submodule metadata block.
2176 RecordData Record;
2177 Record.push_back(getNumberOfModules(WritingModule));
2178 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2179 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2180
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002181 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002182 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002183 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002184 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002185 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002186 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002187 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002188
2189 // Emit the definition of the block.
2190 Record.clear();
2191 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002192 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002193 if (Mod->Parent) {
2194 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2195 Record.push_back(SubmoduleIDs[Mod->Parent]);
2196 } else {
2197 Record.push_back(0);
2198 }
2199 Record.push_back(Mod->IsFramework);
2200 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002201 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002202 Record.push_back(Mod->InferSubmodules);
2203 Record.push_back(Mod->InferExplicitSubmodules);
2204 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002205 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2206
Douglas Gregor51f564f2011-12-31 04:05:44 +00002207 // Emit the requirements.
2208 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2209 Record.clear();
2210 Record.push_back(SUBMODULE_REQUIRES);
2211 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2212 Mod->Requires[I].data(),
2213 Mod->Requires[I].size());
2214 }
2215
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002216 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002217 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002218 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002219 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002220 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002221 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002222 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2223 Record.clear();
2224 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2225 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2226 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002227 }
2228
2229 // Emit the headers.
2230 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2231 Record.clear();
2232 Record.push_back(SUBMODULE_HEADER);
2233 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2234 Mod->Headers[I]->getName());
2235 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002236 // Emit the excluded headers.
2237 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2238 Record.clear();
2239 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2240 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2241 Mod->ExcludedHeaders[I]->getName());
2242 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002243 ArrayRef<const FileEntry *>
2244 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2245 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002246 Record.clear();
2247 Record.push_back(SUBMODULE_TOPHEADER);
2248 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002249 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002250 }
Douglas Gregor55988682011-12-05 16:33:54 +00002251
2252 // Emit the imports.
2253 if (!Mod->Imports.empty()) {
2254 Record.clear();
2255 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002256 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002257 assert(ImportedID && "Unknown submodule!");
2258 Record.push_back(ImportedID);
2259 }
2260 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2261 }
2262
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002263 // Emit the exports.
2264 if (!Mod->Exports.empty()) {
2265 Record.clear();
2266 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002267 if (Module *Exported = Mod->Exports[I].getPointer()) {
2268 unsigned ExportedID = SubmoduleIDs[Exported];
2269 assert(ExportedID > 0 && "Unknown submodule ID?");
2270 Record.push_back(ExportedID);
2271 } else {
2272 Record.push_back(0);
2273 }
2274
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002275 Record.push_back(Mod->Exports[I].getInt());
2276 }
2277 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2278 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002279
2280 // Emit the link libraries.
2281 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2282 Record.clear();
2283 Record.push_back(SUBMODULE_LINK_LIBRARY);
2284 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2285 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2286 Mod->LinkLibraries[I].Library);
2287 }
2288
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002289 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002290 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2291 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002292 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002293 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002294 }
2295
2296 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002297
2298 assert((NextSubmoduleID - FirstSubmoduleID
2299 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002300}
2301
Douglas Gregor185dbd72011-12-01 02:07:58 +00002302serialization::SubmoduleID
2303ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002304 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002305 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002306
2307 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002308 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002309 Module *OwningMod
2310 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002311 if (!OwningMod)
2312 return 0;
2313
Douglas Gregore209e502011-12-06 01:10:29 +00002314 // Check whether this submodule is part of our own module.
2315 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002316 return 0;
2317
Douglas Gregore209e502011-12-06 01:10:29 +00002318 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002319}
2320
David Blaikied6471f72011-09-25 23:23:43 +00002321void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002322 // FIXME: Make it work properly with modules.
2323 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2324 DiagStateIDMap;
2325 unsigned CurrID = 0;
2326 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002327 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002328 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002329 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2330 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002331 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002332 if (point.Loc.isInvalid())
2333 continue;
2334
2335 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002336 unsigned &DiagStateID = DiagStateIDMap[point.State];
2337 Record.push_back(DiagStateID);
2338
2339 if (DiagStateID == 0) {
2340 DiagStateID = ++CurrID;
2341 for (DiagnosticsEngine::DiagState::const_iterator
2342 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2343 if (I->second.isPragma()) {
2344 Record.push_back(I->first);
2345 Record.push_back(I->second.getMapping());
2346 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002347 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002348 Record.push_back(-1); // mark the end of the diag/map pairs for this
2349 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002350 }
2351 }
2352
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002353 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002354 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002355}
2356
Anders Carlssonc8505782011-03-06 18:41:18 +00002357void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2358 if (CXXBaseSpecifiersOffsets.empty())
2359 return;
2360
2361 RecordData Record;
2362
2363 // Create a blob abbreviation for the C++ base specifiers offsets.
2364 using namespace llvm;
2365
2366 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2367 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2370 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2371
Douglas Gregore92b8a12011-08-04 00:01:48 +00002372 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002373 Record.clear();
2374 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2375 Record.push_back(CXXBaseSpecifiersOffsets.size());
2376 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002377 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002378}
2379
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002380//===----------------------------------------------------------------------===//
2381// Type Serialization
2382//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002383
Sebastian Redl3397c552010-08-18 23:56:27 +00002384/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002385void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002386 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002387 if (Idx.getIndex() == 0) // we haven't seen this type before.
2388 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Douglas Gregor97475832010-10-05 18:37:06 +00002390 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002391
Douglas Gregor2cf26342009-04-09 22:27:44 +00002392 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002393 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002394 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002395 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002396 else if (TypeOffsets.size() < Index) {
2397 TypeOffsets.resize(Index + 1);
2398 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002399 }
2400
2401 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Douglas Gregor2cf26342009-04-09 22:27:44 +00002403 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002404 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002405
Douglas Gregora4923eb2009-11-16 21:35:15 +00002406 if (T.hasLocalNonFastQualifiers()) {
2407 Qualifiers Qs = T.getLocalQualifiers();
2408 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002409 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002410 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002411 } else {
2412 switch (T->getTypeClass()) {
2413 // For all of the concrete, non-dependent types, call the
2414 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002415#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002416 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002417#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002418#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002419 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002420 }
2421
2422 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002423 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002424
2425 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002426 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002427}
2428
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002429//===----------------------------------------------------------------------===//
2430// Declaration Serialization
2431//===----------------------------------------------------------------------===//
2432
Douglas Gregor2cf26342009-04-09 22:27:44 +00002433/// \brief Write the block containing all of the declaration IDs
2434/// lexically declared within the given DeclContext.
2435///
2436/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2437/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002438uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002439 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002440 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002441 return 0;
2442
Douglas Gregorc9490c02009-04-16 22:23:12 +00002443 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002444 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002445 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002446 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002447 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2448 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002449 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002450
Douglas Gregor25123082009-04-22 22:34:57 +00002451 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002452 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002453 return Offset;
2454}
2455
Sebastian Redla4232eb2010-08-18 23:56:21 +00002456void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002457 using namespace llvm;
2458 RecordData Record;
2459
2460 // Write the type offsets array
2461 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002462 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002463 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2466 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2467 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002468 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002469 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002470 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002471 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002472
2473 // Write the declaration offsets array
2474 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002475 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002476 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002477 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2479 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2480 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002481 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002482 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002483 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002484 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002485}
2486
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002487void ASTWriter::WriteFileDeclIDsMap() {
2488 using namespace llvm;
2489 RecordData Record;
2490
2491 // Join the vectors of DeclIDs from all files.
2492 SmallVector<DeclID, 256> FileSortedIDs;
2493 for (FileDeclIDsTy::iterator
2494 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2495 DeclIDInFileInfo &Info = *FI->second;
2496 Info.FirstDeclIndex = FileSortedIDs.size();
2497 for (LocDeclIDsTy::iterator
2498 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2499 FileSortedIDs.push_back(DI->second);
2500 }
2501
2502 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2503 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002505 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2506 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2507 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002508 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002509 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2510}
2511
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002512void ASTWriter::WriteComments() {
2513 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002514 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002515 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002516 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2517 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002518 I != E; ++I) {
2519 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002520 AddSourceRange((*I)->getSourceRange(), Record);
2521 Record.push_back((*I)->getKind());
2522 Record.push_back((*I)->isTrailingComment());
2523 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002524 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2525 }
2526 Stream.ExitBlock();
2527}
2528
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002529//===----------------------------------------------------------------------===//
2530// Global Method Pool and Selector Serialization
2531//===----------------------------------------------------------------------===//
2532
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002533namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002534// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002535class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002536 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002537
2538public:
2539 typedef Selector key_type;
2540 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002541
Sebastian Redl5d050072010-08-04 17:20:04 +00002542 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002543 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002544 ObjCMethodList Instance, Factory;
2545 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002546 typedef const data_type& data_type_ref;
2547
Sebastian Redl3397c552010-08-18 23:56:27 +00002548 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002550 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002551 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002552 }
Mike Stump1eb44332009-09-09 15:08:12 +00002553
2554 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002555 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002556 data_type_ref Methods) {
2557 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2558 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002559 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2560 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002561 Method = Method->Next)
2562 if (Method->Method)
2563 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002564 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002565 Method = Method->Next)
2566 if (Method->Method)
2567 DataLen += 4;
2568 clang::io::Emit16(Out, DataLen);
2569 return std::make_pair(KeyLen, DataLen);
2570 }
Mike Stump1eb44332009-09-09 15:08:12 +00002571
Chris Lattner5f9e2722011-07-23 10:55:15 +00002572 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002573 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002574 assert((Start >> 32) == 0 && "Selector key offset too large");
2575 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002576 unsigned N = Sel.getNumArgs();
2577 clang::io::Emit16(Out, N);
2578 if (N == 0)
2579 N = 1;
2580 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002581 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002582 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2583 }
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Chris Lattner5f9e2722011-07-23 10:55:15 +00002585 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002586 data_type_ref Methods, unsigned DataLen) {
2587 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002588 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002589 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002590 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002591 Method = Method->Next)
2592 if (Method->Method)
2593 ++NumInstanceMethods;
2594
2595 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002596 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002597 Method = Method->Next)
2598 if (Method->Method)
2599 ++NumFactoryMethods;
2600
2601 clang::io::Emit16(Out, NumInstanceMethods);
2602 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002603 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002604 Method = Method->Next)
2605 if (Method->Method)
2606 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002607 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002608 Method = Method->Next)
2609 if (Method->Method)
2610 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002611
2612 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002613 }
2614};
2615} // end anonymous namespace
2616
Sebastian Redl059612d2010-08-03 21:58:15 +00002617/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002618///
2619/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002620/// in an on-disk hash table indexed by the selector. The hash table also
2621/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002622void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002623 using namespace llvm;
2624
Sebastian Redl059612d2010-08-03 21:58:15 +00002625 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002626 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002627 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002628 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002629 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002630 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002631 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002632 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002633
Sebastian Redl059612d2010-08-03 21:58:15 +00002634 // Create the on-disk hash table representation. We walk through every
2635 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002636 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002637 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002638 I = SelectorIDs.begin(), E = SelectorIDs.end();
2639 I != E; ++I) {
2640 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002641 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002642 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002643 I->second,
2644 ObjCMethodList(),
2645 ObjCMethodList()
2646 };
2647 if (F != SemaRef.MethodPool.end()) {
2648 Data.Instance = F->second.first;
2649 Data.Factory = F->second.second;
2650 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002651 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002652 // changed.
2653 if (Chain && I->second < FirstSelectorID) {
2654 // Selector already exists. Did it change?
2655 bool changed = false;
2656 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2657 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002658 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002659 changed = true;
2660 }
2661 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2662 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002663 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002664 changed = true;
2665 }
2666 if (!changed)
2667 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002668 } else if (Data.Instance.Method || Data.Factory.Method) {
2669 // A new method pool entry.
2670 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002671 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002672 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002673 }
2674
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002675 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002676 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002677 uint32_t BucketOffset;
2678 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002679 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002680 llvm::raw_svector_ostream Out(MethodPool);
2681 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002682 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002683 BucketOffset = Generator.Emit(Out, Trait);
2684 }
2685
2686 // Create a blob abbreviation
2687 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002688 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002690 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002691 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2692 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2693
Douglas Gregor83941df2009-04-25 17:48:32 +00002694 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002695 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002696 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002697 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002698 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002699 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002700
2701 // Create a blob abbreviation for the selector table offsets.
2702 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002703 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002704 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002705 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002706 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2707 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2708
2709 // Write the selector offsets table.
2710 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002711 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002712 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002713 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002714 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002715 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002716 }
2717}
2718
Sebastian Redl3397c552010-08-18 23:56:27 +00002719/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002720void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002721 using namespace llvm;
2722 if (SemaRef.ReferencedSelectors.empty())
2723 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002724
Fariborz Jahanian32019832010-07-23 19:11:11 +00002725 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002726
Sebastian Redl3397c552010-08-18 23:56:27 +00002727 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002728 // very tricky to fix, and given that @selector shouldn't really appear in
2729 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002730 for (DenseMap<Selector, SourceLocation>::iterator S =
2731 SemaRef.ReferencedSelectors.begin(),
2732 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2733 Selector Sel = (*S).first;
2734 SourceLocation Loc = (*S).second;
2735 AddSelectorRef(Sel, Record);
2736 AddSourceLocation(Loc, Record);
2737 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002738 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002739}
2740
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002741//===----------------------------------------------------------------------===//
2742// Identifier Table Serialization
2743//===----------------------------------------------------------------------===//
2744
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002745namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002746class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002747 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002748 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002749 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002750 bool IsModule;
2751
Douglas Gregora92193e2009-04-28 21:18:29 +00002752 /// \brief Determines whether this is an "interesting" identifier
2753 /// that needs a full IdentifierInfo structure written into the hash
2754 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002755 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002756 if (II->isPoisoned() ||
2757 II->isExtensionToken() ||
2758 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002759 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002760 II->getFETokenInfo<void>())
2761 return true;
2762
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002763 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002764 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002765
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002766 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002767 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002768 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002769
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002770 if (Macro || (Macro = PP.getMacroDirectiveHistory(II)))
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00002771 return !shouldIgnoreMacro(Macro, IsModule, PP) &&
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002772 (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002773
2774 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002775 }
2776
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002777public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002778 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002779 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002780
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002781 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002782 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002783
Douglas Gregoreee242f2011-10-27 09:33:13 +00002784 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2785 IdentifierResolver &IdResolver, bool IsModule)
2786 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002787
2788 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002789 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002790 }
Mike Stump1eb44332009-09-09 15:08:12 +00002791
2792 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002793 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002794 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002795 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002796 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002797 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002798 DataLen += 2; // 2 bytes for builtin ID
2799 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002800 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002801 for (MacroDirective *M = Macro; M; M = M->getPrevious()) {
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00002802 if (shouldIgnoreMacro(M, IsModule, PP))
2803 continue;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002804 if (Writer.getMacroRef(M) != 0)
2805 DataLen += 4;
2806 }
2807
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002808 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002809 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002810
Douglas Gregoreee242f2011-10-27 09:33:13 +00002811 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2812 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002813 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002814 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002815 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002816 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002817 // We emit the key length after the data length so that every
2818 // string is preceded by a 16-bit length. This matches the PTH
2819 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002820 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002821 return std::make_pair(KeyLen, DataLen);
2822 }
Mike Stump1eb44332009-09-09 15:08:12 +00002823
Chris Lattner5f9e2722011-07-23 10:55:15 +00002824 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002825 unsigned KeyLen) {
2826 // Record the location of the key data. This is used when generating
2827 // the mapping from persistent IDs to strings.
2828 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002829 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002830 }
Mike Stump1eb44332009-09-09 15:08:12 +00002831
Douglas Gregor7143aab2011-09-01 17:04:32 +00002832 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002833 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002834 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002835 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002836 clang::io::Emit32(Out, ID << 1);
2837 return;
2838 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002839
Douglas Gregora92193e2009-04-28 21:18:29 +00002840 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002841 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2842 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2843 clang::io::Emit16(Out, Bits);
2844 Bits = 0;
2845 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002846 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002847 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2848 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002849 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002850 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002851 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002852
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002853 if (HadMacroDefinition) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002854 // Write all of the macro IDs associated with this identifier.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002855 for (MacroDirective *M = Macro; M; M = M->getPrevious()) {
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00002856 if (shouldIgnoreMacro(M, IsModule, PP))
2857 continue;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002858 if (MacroID ID = Writer.getMacroRef(M))
2859 clang::io::Emit32(Out, ID);
2860 }
2861
2862 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002863 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002864
Douglas Gregor668c1a42009-04-21 22:25:48 +00002865 // Emit the declaration IDs in reverse order, because the
2866 // IdentifierResolver provides the declarations as they would be
2867 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002868 // "stat"), but the ASTReader adds declarations to the end of the list
2869 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002870 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002871 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2872 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002873 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002874 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002875 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002876 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002877 }
2878};
2879} // end anonymous namespace
2880
Sebastian Redl3397c552010-08-18 23:56:27 +00002881/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002882///
2883/// The identifier table consists of a blob containing string data
2884/// (the actual identifiers themselves) and a separate "offsets" index
2885/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002886void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2887 IdentifierResolver &IdResolver,
2888 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002889 using namespace llvm;
2890
2891 // Create and write out the blob that contains the identifier
2892 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002893 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002894 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002895 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Douglas Gregor92b059e2009-04-28 20:33:11 +00002897 // Look for any identifiers that were named while processing the
2898 // headers, but are otherwise not needed. We add these to the hash
2899 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002900 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002901 // file.
2902 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2903 IDEnd = PP.getIdentifierTable().end();
2904 ID != IDEnd; ++ID)
2905 getIdentifierRef(ID->second);
2906
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002907 // Create the on-disk hash table representation. We only store offsets
2908 // for identifiers that appear here for the first time.
2909 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002910 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002911 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2912 ID != IDEnd; ++ID) {
2913 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002914 if (!Chain || !ID->first->isFromAST() ||
2915 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00002916 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002917 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002918 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002919
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002920 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002921 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002922 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002923 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002924 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002925 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002926 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002927 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002928 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002929 }
2930
2931 // Create a blob abbreviation
2932 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002933 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002936 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002937
2938 // Write the identifier table
2939 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002940 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002941 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002942 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002943 }
2944
2945 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002946 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002947 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2951 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2952
Douglas Gregor2d1ece82013-02-08 21:30:59 +00002953#ifndef NDEBUG
2954 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
2955 assert(IdentifierOffsets[I] && "Missing identifier offset?");
2956#endif
2957
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002958 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002959 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002960 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002961 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002962 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002963 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002964}
2965
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002966//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002967// DeclContext's Name Lookup Table Serialization
2968//===----------------------------------------------------------------------===//
2969
2970namespace {
2971// Trait used for the on-disk hash table used in the method pool.
2972class ASTDeclContextNameLookupTrait {
2973 ASTWriter &Writer;
2974
2975public:
2976 typedef DeclarationName key_type;
2977 typedef key_type key_type_ref;
2978
2979 typedef DeclContext::lookup_result data_type;
2980 typedef const data_type& data_type_ref;
2981
2982 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2983
2984 unsigned ComputeHash(DeclarationName Name) {
2985 llvm::FoldingSetNodeID ID;
2986 ID.AddInteger(Name.getNameKind());
2987
2988 switch (Name.getNameKind()) {
2989 case DeclarationName::Identifier:
2990 ID.AddString(Name.getAsIdentifierInfo()->getName());
2991 break;
2992 case DeclarationName::ObjCZeroArgSelector:
2993 case DeclarationName::ObjCOneArgSelector:
2994 case DeclarationName::ObjCMultiArgSelector:
2995 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2996 break;
2997 case DeclarationName::CXXConstructorName:
2998 case DeclarationName::CXXDestructorName:
2999 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003000 break;
3001 case DeclarationName::CXXOperatorName:
3002 ID.AddInteger(Name.getCXXOverloadedOperator());
3003 break;
3004 case DeclarationName::CXXLiteralOperatorName:
3005 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3006 case DeclarationName::CXXUsingDirective:
3007 break;
3008 }
3009
3010 return ID.ComputeHash();
3011 }
3012
3013 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003014 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003015 data_type_ref Lookup) {
3016 unsigned KeyLen = 1;
3017 switch (Name.getNameKind()) {
3018 case DeclarationName::Identifier:
3019 case DeclarationName::ObjCZeroArgSelector:
3020 case DeclarationName::ObjCOneArgSelector:
3021 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003022 case DeclarationName::CXXLiteralOperatorName:
3023 KeyLen += 4;
3024 break;
3025 case DeclarationName::CXXOperatorName:
3026 KeyLen += 1;
3027 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003028 case DeclarationName::CXXConstructorName:
3029 case DeclarationName::CXXDestructorName:
3030 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003031 case DeclarationName::CXXUsingDirective:
3032 break;
3033 }
3034 clang::io::Emit16(Out, KeyLen);
3035
3036 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003037 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003038 clang::io::Emit16(Out, DataLen);
3039
3040 return std::make_pair(KeyLen, DataLen);
3041 }
3042
Chris Lattner5f9e2722011-07-23 10:55:15 +00003043 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003044 using namespace clang::io;
3045
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003046 Emit8(Out, Name.getNameKind());
3047 switch (Name.getNameKind()) {
3048 case DeclarationName::Identifier:
3049 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003050 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003051 case DeclarationName::ObjCZeroArgSelector:
3052 case DeclarationName::ObjCOneArgSelector:
3053 case DeclarationName::ObjCMultiArgSelector:
3054 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003055 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003056 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003057 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3058 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003059 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003060 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003061 case DeclarationName::CXXLiteralOperatorName:
3062 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003063 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003064 case DeclarationName::CXXConstructorName:
3065 case DeclarationName::CXXDestructorName:
3066 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003067 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003068 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003069 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003070
3071 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003072 }
3073
Chris Lattner5f9e2722011-07-23 10:55:15 +00003074 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003075 data_type Lookup, unsigned DataLen) {
3076 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003077 clang::io::Emit16(Out, Lookup.size());
3078 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3079 I != E; ++I)
3080 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003081
3082 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3083 }
3084};
3085} // end anonymous namespace
3086
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003087/// \brief Write the block containing all of the declaration IDs
3088/// visible from the given DeclContext.
3089///
3090/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003091/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003092uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3093 DeclContext *DC) {
3094 if (DC->getPrimaryContext() != DC)
3095 return 0;
3096
3097 // Since there is no name lookup into functions or methods, don't bother to
3098 // build a visible-declarations table for these entities.
3099 if (DC->isFunctionOrMethod())
3100 return 0;
3101
3102 // If not in C++, we perform name lookup for the translation unit via the
3103 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3104 // FIXME: In C++ we need the visible declarations in order to "see" the
3105 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00003106 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003107 return 0;
3108
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003109 // Serialize the contents of the mapping used for lookup. Note that,
3110 // although we have two very different code paths, the serialized
3111 // representation is the same for both cases: a declaration name,
3112 // followed by a size, followed by references to the visible
3113 // declarations that have that name.
3114 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003115 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003116 if (!Map || Map->empty())
3117 return 0;
3118
3119 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3120 ASTDeclContextNameLookupTrait Trait(*this);
3121
3122 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003123 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003124 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003125 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3126 D != DEnd; ++D) {
3127 DeclarationName Name = D->first;
3128 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003129 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003130 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3131 // Hash all conversion function names to the same name. The actual
3132 // type information in conversion function name is not used in the
3133 // key (since such type information is not stable across different
3134 // modules), so the intended effect is to coalesce all of the conversion
3135 // functions under a single key.
3136 if (!ConversionName)
3137 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003138 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003139 continue;
3140 }
3141
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003142 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003143 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003144 }
3145
Douglas Gregore5a54b62011-08-30 20:49:19 +00003146 // Add the conversion functions
3147 if (!ConversionDecls.empty()) {
3148 Generator.insert(ConversionName,
3149 DeclContext::lookup_result(ConversionDecls.begin(),
3150 ConversionDecls.end()),
3151 Trait);
3152 }
3153
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003154 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003155 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003156 uint32_t BucketOffset;
3157 {
3158 llvm::raw_svector_ostream Out(LookupTable);
3159 // Make sure that no bucket is at offset 0
3160 clang::io::Emit32(Out, 0);
3161 BucketOffset = Generator.Emit(Out, Trait);
3162 }
3163
3164 // Write the lookup table
3165 RecordData Record;
3166 Record.push_back(DECL_CONTEXT_VISIBLE);
3167 Record.push_back(BucketOffset);
3168 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3169 LookupTable.str());
3170
3171 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3172 ++NumVisibleDeclContexts;
3173 return Offset;
3174}
3175
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003176/// \brief Write an UPDATE_VISIBLE block for the given context.
3177///
3178/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3179/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003180/// (in C++), for namespaces, and for classes with forward-declared unscoped
3181/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003182void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003183 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3184 if (!Map || Map->empty())
3185 return;
3186
3187 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3188 ASTDeclContextNameLookupTrait Trait(*this);
3189
3190 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003191 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3192 D != DEnd; ++D) {
3193 DeclarationName Name = D->first;
3194 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003195 // For any name that appears in this table, the results are complete, i.e.
3196 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003197 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003198 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003199 }
3200
3201 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003202 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003203 uint32_t BucketOffset;
3204 {
3205 llvm::raw_svector_ostream Out(LookupTable);
3206 // Make sure that no bucket is at offset 0
3207 clang::io::Emit32(Out, 0);
3208 BucketOffset = Generator.Emit(Out, Trait);
3209 }
3210
3211 // Write the lookup table
3212 RecordData Record;
3213 Record.push_back(UPDATE_VISIBLE);
3214 Record.push_back(getDeclID(cast<Decl>(DC)));
3215 Record.push_back(BucketOffset);
3216 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3217}
3218
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003219/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3220void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3221 RecordData Record;
3222 Record.push_back(Opts.fp_contract);
3223 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3224}
3225
3226/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3227void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003228 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003229 return;
3230
3231 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3232 RecordData Record;
3233#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3234#include "clang/Basic/OpenCLExtensions.def"
3235 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3236}
3237
Douglas Gregor2171bf12012-01-15 16:58:34 +00003238void ASTWriter::WriteRedeclarations() {
3239 RecordData LocalRedeclChains;
3240 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3241
3242 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3243 Decl *First = Redeclarations[I];
3244 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3245
3246 Decl *MostRecent = First->getMostRecentDecl();
3247
3248 // If we only have a single declaration, there is no point in storing
3249 // a redeclaration chain.
3250 if (First == MostRecent)
3251 continue;
3252
3253 unsigned Offset = LocalRedeclChains.size();
3254 unsigned Size = 0;
3255 LocalRedeclChains.push_back(0); // Placeholder for the size.
3256
3257 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003258 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003259 Prev = Prev->getPreviousDecl()) {
3260 if (!Prev->isFromASTFile()) {
3261 AddDeclRef(Prev, LocalRedeclChains);
3262 ++Size;
3263 }
3264 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003265
3266 if (!First->isFromASTFile() && Chain) {
3267 Decl *FirstFromAST = MostRecent;
3268 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3269 if (Prev->isFromASTFile())
3270 FirstFromAST = Prev;
3271 }
3272
3273 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3274 }
3275
Douglas Gregor2171bf12012-01-15 16:58:34 +00003276 LocalRedeclChains[Offset] = Size;
3277
3278 // Reverse the set of local redeclarations, so that we store them in
3279 // order (since we found them in reverse order).
3280 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3281
Douglas Gregoraa945902013-02-18 15:53:43 +00003282 // Add the mapping from the first ID from the AST to the set of local
3283 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003284 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3285 LocalRedeclsMap.push_back(Info);
3286
3287 assert(N == Redeclarations.size() &&
3288 "Deserialized a declaration we shouldn't have");
3289 }
3290
3291 if (LocalRedeclChains.empty())
3292 return;
3293
3294 // Sort the local redeclarations map by the first declaration ID,
3295 // since the reader will be performing binary searches on this information.
3296 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3297
3298 // Emit the local redeclarations map.
3299 using namespace llvm;
3300 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3301 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3304 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3305
3306 RecordData Record;
3307 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3308 Record.push_back(LocalRedeclsMap.size());
3309 Stream.EmitRecordWithBlob(AbbrevID, Record,
3310 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3311 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3312
3313 // Emit the redeclaration chains.
3314 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3315}
3316
Douglas Gregorcff9f262012-01-27 01:47:08 +00003317void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003318 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003319 RecordData Categories;
3320
3321 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3322 unsigned Size = 0;
3323 unsigned StartIndex = Categories.size();
3324
3325 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3326
3327 // Allocate space for the size.
3328 Categories.push_back(0);
3329
3330 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003331 for (ObjCInterfaceDecl::known_categories_iterator
3332 Cat = Class->known_categories_begin(),
3333 CatEnd = Class->known_categories_end();
3334 Cat != CatEnd; ++Cat, ++Size) {
3335 assert(getDeclID(*Cat) != 0 && "Bogus category");
3336 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003337 }
3338
3339 // Update the size.
3340 Categories[StartIndex] = Size;
3341
3342 // Record this interface -> category map.
3343 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3344 CategoriesMap.push_back(CatInfo);
3345 }
3346
3347 // Sort the categories map by the definition ID, since the reader will be
3348 // performing binary searches on this information.
3349 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3350
3351 // Emit the categories map.
3352 using namespace llvm;
3353 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3354 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3357 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3358
3359 RecordData Record;
3360 Record.push_back(OBJC_CATEGORIES_MAP);
3361 Record.push_back(CategoriesMap.size());
3362 Stream.EmitRecordWithBlob(AbbrevID, Record,
3363 reinterpret_cast<char*>(CategoriesMap.data()),
3364 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3365
3366 // Emit the category lists.
3367 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3368}
3369
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003370void ASTWriter::WriteMergedDecls() {
3371 if (!Chain || Chain->MergedDecls.empty())
3372 return;
3373
3374 RecordData Record;
3375 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3376 IEnd = Chain->MergedDecls.end();
3377 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003378 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003379 : getDeclID(I->first);
3380 assert(CanonID && "Merged declaration not known?");
3381
3382 Record.push_back(CanonID);
3383 Record.push_back(I->second.size());
3384 Record.append(I->second.begin(), I->second.end());
3385 }
3386 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3387}
3388
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003389//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003390// General Serialization Routines
3391//===----------------------------------------------------------------------===//
3392
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003393/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003394void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3395 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003396 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003397 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3398 e = Attrs.end(); i != e; ++i){
3399 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003400 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003401 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003402
Sean Huntcf807c42010-08-18 23:23:40 +00003403#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003404
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003405 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003406}
3407
Chris Lattner5f9e2722011-07-23 10:55:15 +00003408void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003409 Record.push_back(Str.size());
3410 Record.insert(Record.end(), Str.begin(), Str.end());
3411}
3412
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003413void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3414 RecordDataImpl &Record) {
3415 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003416 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003417 Record.push_back(*Minor + 1);
3418 else
3419 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003420 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003421 Record.push_back(*Subminor + 1);
3422 else
3423 Record.push_back(0);
3424}
3425
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003426/// \brief Note that the identifier II occurs at the given offset
3427/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003428void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003429 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003430 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003431 // up earlier in the chain and thus don't need an offset.
3432 if (ID >= FirstIdentID)
3433 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003434}
3435
Douglas Gregor83941df2009-04-25 17:48:32 +00003436/// \brief Note that the selector Sel occurs at the given offset
3437/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003438void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003439 unsigned ID = SelectorIDs[Sel];
3440 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003441 // Don't record offsets for selectors that are also available in a different
3442 // file.
3443 if (ID < FirstSelectorID)
3444 return;
3445 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003446}
3447
Sebastian Redla4232eb2010-08-18 23:56:21 +00003448ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003449 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003450 WritingAST(false), DoneWritingDeclsAndTypes(false),
3451 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003452 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003453 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003454 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3455 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003456 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3457 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003458 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003459 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003460 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003461 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003462 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003463 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003464 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3465 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3466 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003467 DeclTypedefAbbrev(0),
3468 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3469 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003470{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003471}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003472
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003473ASTWriter::~ASTWriter() {
3474 for (FileDeclIDsTy::iterator
3475 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3476 delete I->second;
3477}
3478
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003479void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003480 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003481 Module *WritingModule, StringRef isysroot,
3482 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003483 WritingAST = true;
3484
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003485 ASTHasCompilerErrors = hasErrors;
3486
Douglas Gregor2cf26342009-04-09 22:27:44 +00003487 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003488 Stream.Emit((unsigned)'C', 8);
3489 Stream.Emit((unsigned)'P', 8);
3490 Stream.Emit((unsigned)'C', 8);
3491 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003492
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003493 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003494
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003495 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003496 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003497 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003498 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003499 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003500 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003501 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003502
3503 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003504}
3505
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003506template<typename Vector>
3507static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3508 ASTWriter::RecordData &Record) {
3509 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3510 I != E; ++I) {
3511 Writer.AddDeclRef(*I, Record);
3512 }
3513}
3514
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003515void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003516 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003517 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003518 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003519 using namespace llvm;
3520
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003521 bool isModule = WritingModule != 0;
3522
Douglas Gregorecc2c092011-12-01 22:20:10 +00003523 // Make sure that the AST reader knows to finalize itself.
3524 if (Chain)
3525 Chain->finalizeForWriting();
3526
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003527 ASTContext &Context = SemaRef.Context;
3528 Preprocessor &PP = SemaRef.PP;
3529
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003530 // Set up predefined declaration IDs.
3531 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003532 if (Context.ObjCIdDecl)
3533 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003534 if (Context.ObjCSelDecl)
3535 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003536 if (Context.ObjCClassDecl)
3537 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003538 if (Context.ObjCProtocolClassDecl)
3539 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003540 if (Context.Int128Decl)
3541 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3542 if (Context.UInt128Decl)
3543 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003544 if (Context.ObjCInstanceTypeDecl)
3545 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003546 if (Context.BuiltinVaListDecl)
3547 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3548
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003549 if (!Chain) {
3550 // Make sure that we emit IdentifierInfos (and any attached
3551 // declarations) for builtins. We don't need to do this when we're
3552 // emitting chained PCH files, because all of the builtins will be
3553 // in the original PCH file.
3554 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003555 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003556 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003557 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003558 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003559 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3560 getIdentifierRef(&Table.get(BuiltinNames[I]));
3561 }
3562
Douglas Gregoreee242f2011-10-27 09:33:13 +00003563 // If there are any out-of-date identifiers, bring them up to date.
3564 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003565 // Find out-of-date identifiers.
3566 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003567 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3568 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003569 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003570 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003571 OutOfDate.push_back(ID->second);
3572 }
3573
3574 // Update the out-of-date identifiers.
3575 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3576 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3577 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003578 }
3579
Chris Lattner63d65f82009-09-08 18:19:27 +00003580 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003581 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003582 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003583 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003584 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003585
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003586 // Build a record containing all of the file scoped decls in this file.
3587 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003588 if (!isModule)
3589 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3590 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003591
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003592 // Build a record containing all of the delegating constructors we still need
3593 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003594 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003595 if (!isModule)
3596 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003597
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003598 // Write the set of weak, undeclared identifiers. We always write the
3599 // entire table, since later PCH files in a PCH chain are only interested in
3600 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003601 RecordData WeakUndeclaredIdentifiers;
3602 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003603 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003604 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3605 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3606 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3607 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3608 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3609 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3610 }
3611 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003612
Richard Smith5ea6ef42013-01-10 23:43:47 +00003613 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003614 // declarations in this header file. Generally, this record will be
3615 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003616 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003617 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003618 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003619 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003620 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3621 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003622 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003623 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003624 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003625 }
3626
Douglas Gregorb81c1702009-04-27 20:06:05 +00003627 // Build a record containing all of the ext_vector declarations.
3628 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003629 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003630
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003631 // Build a record containing all of the VTable uses information.
3632 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003633 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003634 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3635 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3636 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3637 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3638 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003639 }
3640
3641 // Build a record containing all of dynamic classes declarations.
3642 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003643 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003644
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003645 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003646 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003647 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003648 I = SemaRef.PendingInstantiations.begin(),
3649 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3650 AddDeclRef(I->first, PendingInstantiations);
3651 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003652 }
3653 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3654 "There are local ones at end of translation unit!");
3655
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003656 // Build a record containing some declaration references.
3657 RecordData SemaDeclRefs;
3658 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3659 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3660 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3661 }
3662
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003663 RecordData CUDASpecialDeclRefs;
3664 if (Context.getcudaConfigureCallDecl()) {
3665 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3666 }
3667
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003668 // Build a record containing all of the known namespaces.
3669 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003670 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003671 I = SemaRef.KnownNamespaces.begin(),
3672 IEnd = SemaRef.KnownNamespaces.end();
3673 I != IEnd; ++I) {
3674 if (!I->second)
3675 AddDeclRef(I->first, KnownNamespaces);
3676 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003677
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003678 // Build a record of all used, undefined objects that require definitions.
3679 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003680
3681 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003682 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003683 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3684 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003685 AddDeclRef(I->first, UndefinedButUsed);
3686 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003687 }
3688
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003689 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003690 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003691
Sebastian Redl3397c552010-08-18 23:56:27 +00003692 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003693 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003694 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003695
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003696 // This is so that older clang versions, before the introduction
3697 // of the control block, can read and reject the newer PCH format.
3698 Record.clear();
3699 Record.push_back(VERSION_MAJOR);
3700 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3701
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003702 // Create a lexical update block containing all of the declarations in the
3703 // translation unit that do not come from other AST files.
3704 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3705 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3706 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3707 E = TU->noload_decls_end();
3708 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003709 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003710 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003711 }
3712
3713 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3714 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3715 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3716 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3717 Record.clear();
3718 Record.push_back(TU_UPDATE_LEXICAL);
3719 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3720 data(NewGlobalDecls));
3721
3722 // And a visible updates block for the translation unit.
3723 Abv = new llvm::BitCodeAbbrev();
3724 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3725 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3726 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3727 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3728 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3729 WriteDeclContextVisibleUpdate(TU);
3730
3731 // If the translation unit has an anonymous namespace, and we don't already
3732 // have an update block for it, write it as an update block.
3733 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3734 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3735 if (Record.empty()) {
3736 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003737 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003738 }
3739 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003740
3741 // Make sure visible decls, added to DeclContexts previously loaded from
3742 // an AST file, are registered for serialization.
3743 for (SmallVector<const Decl *, 16>::iterator
3744 I = UpdatingVisibleDecls.begin(),
3745 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3746 GetDeclRef(*I);
3747 }
3748
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003749 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003750 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003751
Douglas Gregora119da02011-08-02 16:26:37 +00003752 // Form the record of special types.
3753 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003754 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003755 AddTypeRef(Context.getFILEType(), SpecialTypes);
3756 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3757 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3758 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3759 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003760 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003761 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003762
Douglas Gregor366809a2009-04-26 03:49:13 +00003763 // Keep writing types and declarations until all types and
3764 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003765 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003766 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003767 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3768 E = DeclsToRewrite.end();
3769 I != E; ++I)
3770 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003771 while (!DeclTypesToEmit.empty()) {
3772 DeclOrType DOT = DeclTypesToEmit.front();
3773 DeclTypesToEmit.pop();
3774 if (DOT.isType())
3775 WriteType(DOT.getType());
3776 else
3777 WriteDecl(Context, DOT.getDecl());
3778 }
3779 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003780
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003781 DoneWritingDeclsAndTypes = true;
3782
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003783 WriteFileDeclIDsMap();
3784 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003785 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003786
3787 if (Chain) {
3788 // Write the mapping information describing our module dependencies and how
3789 // each of those modules were mapped into our own offset/ID space, so that
3790 // the reader can build the appropriate mapping to its own offset/ID space.
3791 // The map consists solely of a blob with the following format:
3792 // *(module-name-len:i16 module-name:len*i8
3793 // source-location-offset:i32
3794 // identifier-id:i32
3795 // preprocessed-entity-id:i32
3796 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003797 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003798 // selector-id:i32
3799 // declaration-id:i32
3800 // c++-base-specifiers-id:i32
3801 // type-id:i32)
3802 //
3803 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3804 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3806 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003807 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003808 {
3809 llvm::raw_svector_ostream Out(Buffer);
3810 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003811 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003812 M != MEnd; ++M) {
3813 StringRef FileName = (*M)->FileName;
3814 io::Emit16(Out, FileName.size());
3815 Out.write(FileName.data(), FileName.size());
3816 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3817 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003818 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003819 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003820 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003821 io::Emit32(Out, (*M)->BaseSelectorID);
3822 io::Emit32(Out, (*M)->BaseDeclID);
3823 io::Emit32(Out, (*M)->BaseTypeIndex);
3824 }
3825 }
3826 Record.clear();
3827 Record.push_back(MODULE_OFFSET_MAP);
3828 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3829 Buffer.data(), Buffer.size());
3830 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003831 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003832 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003833 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003834 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003835 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003836 WriteFPPragmaOptions(SemaRef.getFPOptions());
3837 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003838
Sebastian Redl1476ed42010-07-16 16:36:56 +00003839 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003840 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003841
Anders Carlssonc8505782011-03-06 18:41:18 +00003842 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003843
Douglas Gregore209e502011-12-06 01:10:29 +00003844 // If we're emitting a module, write out the submodule information.
3845 if (WritingModule)
3846 WriteSubmodules(WritingModule);
3847
Douglas Gregora119da02011-08-02 16:26:37 +00003848 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3849
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003850 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003851 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003852 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003853
3854 // Write the record containing tentative definitions.
3855 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003856 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003857
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003858 // Write the record containing unused file scoped decls.
3859 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003860 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003861
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003862 // Write the record containing weak undeclared identifiers.
3863 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003864 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003865 WeakUndeclaredIdentifiers);
3866
Richard Smith5ea6ef42013-01-10 23:43:47 +00003867 // Write the record containing locally-scoped extern "C" definitions.
3868 if (!LocallyScopedExternCDecls.empty())
3869 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
3870 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003871
3872 // Write the record containing ext_vector type names.
3873 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003874 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003875
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003876 // Write the record containing VTable uses information.
3877 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003878 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003879
3880 // Write the record containing dynamic classes declarations.
3881 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003882 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003883
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003884 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003885 if (!PendingInstantiations.empty())
3886 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003887
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003888 // Write the record containing declaration references of Sema.
3889 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003890 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003891
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003892 // Write the record containing CUDA-specific declaration references.
3893 if (!CUDASpecialDeclRefs.empty())
3894 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003895
3896 // Write the delegating constructors.
3897 if (!DelegatingCtorDecls.empty())
3898 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003899
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003900 // Write the known namespaces.
3901 if (!KnownNamespaces.empty())
3902 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00003903
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003904 // Write the undefined internal functions and variables, and inline functions.
3905 if (!UndefinedButUsed.empty())
3906 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003907
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003908 // Write the visible updates to DeclContexts.
3909 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3910 I = UpdatedDeclContexts.begin(),
3911 E = UpdatedDeclContexts.end();
3912 I != E; ++I)
3913 WriteDeclContextVisibleUpdate(*I);
3914
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003915 if (!WritingModule) {
3916 // Write the submodules that were imported, if any.
3917 RecordData ImportedModules;
3918 for (ASTContext::import_iterator I = Context.local_import_begin(),
3919 IEnd = Context.local_import_end();
3920 I != IEnd; ++I) {
3921 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3922 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3923 }
3924 if (!ImportedModules.empty()) {
3925 // Sort module IDs.
3926 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3927
3928 // Unique module IDs.
3929 ImportedModules.erase(std::unique(ImportedModules.begin(),
3930 ImportedModules.end()),
3931 ImportedModules.end());
3932
3933 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3934 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003935 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003936
3937 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003938 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003939 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003940 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00003941 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003942 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003943
Douglas Gregor3e1af842009-04-17 22:13:46 +00003944 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003945 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003946 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003947 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003948 Record.push_back(NumLexicalDeclContexts);
3949 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003950 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003951 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003952}
3953
Douglas Gregora8235d62012-10-09 23:05:51 +00003954void ASTWriter::WriteMacroUpdates() {
3955 if (MacroUpdates.empty())
3956 return;
3957
3958 RecordData Record;
3959 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3960 E = MacroUpdates.end();
3961 I != E; ++I) {
3962 addMacroRef(I->first, Record);
3963 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003964 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003965 }
3966 Stream.EmitRecord(MACRO_UPDATES, Record);
3967}
3968
Douglas Gregor61c5e342011-09-17 00:05:03 +00003969/// \brief Go through the declaration update blocks and resolve declaration
3970/// pointers into declaration IDs.
3971void ASTWriter::ResolveDeclUpdatesBlocks() {
3972 for (DeclUpdateMap::iterator
3973 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3974 const Decl *D = I->first;
3975 UpdateRecord &URec = I->second;
3976
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003977 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003978 continue; // The decl will be written completely
3979
3980 unsigned Idx = 0, N = URec.size();
3981 while (Idx < N) {
3982 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003983 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3984 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3985 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3986 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3987 ++Idx;
3988 break;
3989
3990 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3991 ++Idx;
3992 break;
3993 }
3994 }
3995 }
3996}
3997
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003998void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003999 if (DeclUpdates.empty())
4000 return;
4001
4002 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004003 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004004 for (DeclUpdateMap::iterator
4005 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4006 const Decl *D = I->first;
4007 UpdateRecord &URec = I->second;
4008
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004009 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004010 continue; // The decl will be written completely,no need to store updates.
4011
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004012 uint64_t Offset = Stream.GetCurrentBitNo();
4013 Stream.EmitRecord(DECL_UPDATES, URec);
4014
4015 OffsetsRecord.push_back(GetDeclRef(D));
4016 OffsetsRecord.push_back(Offset);
4017 }
4018 Stream.ExitBlock();
4019 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4020}
4021
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004022void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004023 if (ReplacedDecls.empty())
4024 return;
4025
4026 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004027 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004028 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004029 Record.push_back(I->ID);
4030 Record.push_back(I->Offset);
4031 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004032 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004033 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004034}
4035
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004036void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004037 Record.push_back(Loc.getRawEncoding());
4038}
4039
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004040void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004041 AddSourceLocation(Range.getBegin(), Record);
4042 AddSourceLocation(Range.getEnd(), Record);
4043}
4044
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004045void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004046 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004047 const uint64_t *Words = Value.getRawData();
4048 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004049}
4050
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004051void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004052 Record.push_back(Value.isUnsigned());
4053 AddAPInt(Value, Record);
4054}
4055
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004056void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004057 AddAPInt(Value.bitcastToAPInt(), Record);
4058}
4059
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004060void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004061 Record.push_back(getIdentifierRef(II));
4062}
4063
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004064void ASTWriter::addMacroRef(MacroDirective *MD, RecordDataImpl &Record) {
4065 Record.push_back(getMacroRef(MD));
Douglas Gregora8235d62012-10-09 23:05:51 +00004066}
4067
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004068IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004069 if (II == 0)
4070 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004071
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004072 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004073 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004074 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004075 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004076}
4077
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004078MacroID ASTWriter::getMacroRef(MacroDirective *MD) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004079 // Don't emit builtin macros like __LINE__ to the AST file unless they
4080 // have been redefined by the header (in which case they are not
4081 // isBuiltinMacro).
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004082 if (MD == 0 || MD->getInfo()->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004083 return 0;
4084
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004085 MacroID &ID = MacroIDs[MD];
Douglas Gregora8235d62012-10-09 23:05:51 +00004086 if (ID == 0)
4087 ID = NextMacroID++;
4088 return ID;
4089}
4090
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004091void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004092 Record.push_back(getSelectorRef(SelRef));
4093}
4094
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004095SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004096 if (Sel.getAsOpaquePtr() == 0) {
4097 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004098 }
4099
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004100 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004101 if (SID == 0 && Chain) {
4102 // This might trigger a ReadSelector callback, which will set the ID for
4103 // this selector.
4104 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004105 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004106 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004107 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004108 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004109 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004110 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004111 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004112}
4113
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004114void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004115 AddDeclRef(Temp->getDestructor(), Record);
4116}
4117
Douglas Gregor7c789c12010-10-29 22:39:52 +00004118void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4119 CXXBaseSpecifier const *BasesEnd,
4120 RecordDataImpl &Record) {
4121 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4122 CXXBaseSpecifiersToWrite.push_back(
4123 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4124 Bases, BasesEnd));
4125 Record.push_back(NextCXXBaseSpecifiersID++);
4126}
4127
Sebastian Redla4232eb2010-08-18 23:56:21 +00004128void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004129 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004130 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004131 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004132 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004133 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004134 break;
4135 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004136 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004137 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004138 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004139 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004140 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004141 break;
4142 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004143 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004144 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004145 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004146 break;
John McCall833ca992009-10-29 08:12:44 +00004147 case TemplateArgument::Null:
4148 case TemplateArgument::Integral:
4149 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004150 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004151 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004152 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004153 break;
4154 }
4155}
4156
Sebastian Redla4232eb2010-08-18 23:56:21 +00004157void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004158 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004159 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004160
4161 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4162 bool InfoHasSameExpr
4163 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4164 Record.push_back(InfoHasSameExpr);
4165 if (InfoHasSameExpr)
4166 return; // Avoid storing the same expr twice.
4167 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004168 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4169 Record);
4170}
4171
Douglas Gregordc355712011-02-25 00:36:19 +00004172void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4173 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004174 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004175 AddTypeRef(QualType(), Record);
4176 return;
4177 }
4178
Douglas Gregordc355712011-02-25 00:36:19 +00004179 AddTypeLoc(TInfo->getTypeLoc(), Record);
4180}
4181
4182void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4183 AddTypeRef(TL.getType(), Record);
4184
John McCalla1ee0c52009-10-16 21:56:05 +00004185 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004186 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004187 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004188}
4189
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004190void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004191 Record.push_back(GetOrCreateTypeID(T));
4192}
4193
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004194TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4195 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004196 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4197}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004198
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004199TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004200 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004201 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004202}
4203
4204TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4205 if (T.isNull())
4206 return TypeIdx();
4207 assert(!T.getLocalFastQualifiers());
4208
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004209 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004210 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004211 if (DoneWritingDeclsAndTypes) {
4212 assert(0 && "New type seen after serializing all the types to emit!");
4213 return TypeIdx();
4214 }
4215
Douglas Gregor366809a2009-04-26 03:49:13 +00004216 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004217 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004218 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004219 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004220 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004221 return Idx;
4222}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004223
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004224TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004225 if (T.isNull())
4226 return TypeIdx();
4227 assert(!T.getLocalFastQualifiers());
4228
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004229 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4230 assert(I != TypeIdxs.end() && "Type not emitted!");
4231 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004232}
4233
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004234void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004235 Record.push_back(GetDeclRef(D));
4236}
4237
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004238DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004239 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4240
Douglas Gregor2cf26342009-04-09 22:27:44 +00004241 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004242 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004243 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004244
4245 // If D comes from an AST file, its declaration ID is already known and
4246 // fixed.
4247 if (D->isFromASTFile())
4248 return D->getGlobalID();
4249
Douglas Gregor97475832010-10-05 18:37:06 +00004250 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004251 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004252 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004253 if (DoneWritingDeclsAndTypes) {
4254 assert(0 && "New decl seen after serializing all the decls to emit!");
4255 return 0;
4256 }
4257
Douglas Gregor2cf26342009-04-09 22:27:44 +00004258 // We haven't seen this declaration before. Give it a new ID and
4259 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004260 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004261 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004262 }
4263
Sebastian Redl681d7232010-07-27 00:17:23 +00004264 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004265}
4266
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004267DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004268 if (D == 0)
4269 return 0;
4270
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004271 // If D comes from an AST file, its declaration ID is already known and
4272 // fixed.
4273 if (D->isFromASTFile())
4274 return D->getGlobalID();
4275
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004276 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4277 return DeclIDs[D];
4278}
4279
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004280static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4281 std::pair<unsigned, serialization::DeclID> R) {
4282 return L.first < R.first;
4283}
4284
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004285void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004286 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004287 assert(D);
4288
4289 SourceLocation Loc = D->getLocation();
4290 if (Loc.isInvalid())
4291 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004292
4293 // We only keep track of the file-level declarations of each file.
4294 if (!D->getLexicalDeclContext()->isFileContext())
4295 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004296 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4297 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004298 if (isa<ParmVarDecl>(D))
4299 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004300
4301 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004302 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004303 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004304 FileID FID;
4305 unsigned Offset;
4306 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004307 if (FID.isInvalid())
4308 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004309 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004310
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004311 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004312 if (!Info)
4313 Info = new DeclIDInFileInfo();
4314
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004315 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004316 LocDeclIDsTy &Decls = Info->DeclIDs;
4317
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004318 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004319 Decls.push_back(LocDecl);
4320 return;
4321 }
4322
4323 LocDeclIDsTy::iterator
4324 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4325
4326 Decls.insert(I, LocDecl);
4327}
4328
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004329void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004330 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004331 Record.push_back(Name.getNameKind());
4332 switch (Name.getNameKind()) {
4333 case DeclarationName::Identifier:
4334 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4335 break;
4336
4337 case DeclarationName::ObjCZeroArgSelector:
4338 case DeclarationName::ObjCOneArgSelector:
4339 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004340 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004341 break;
4342
4343 case DeclarationName::CXXConstructorName:
4344 case DeclarationName::CXXDestructorName:
4345 case DeclarationName::CXXConversionFunctionName:
4346 AddTypeRef(Name.getCXXNameType(), Record);
4347 break;
4348
4349 case DeclarationName::CXXOperatorName:
4350 Record.push_back(Name.getCXXOverloadedOperator());
4351 break;
4352
Sean Hunt3e518bd2009-11-29 07:34:05 +00004353 case DeclarationName::CXXLiteralOperatorName:
4354 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4355 break;
4356
Douglas Gregor2cf26342009-04-09 22:27:44 +00004357 case DeclarationName::CXXUsingDirective:
4358 // No extra data to emit
4359 break;
4360 }
4361}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004362
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004363void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004364 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004365 switch (Name.getNameKind()) {
4366 case DeclarationName::CXXConstructorName:
4367 case DeclarationName::CXXDestructorName:
4368 case DeclarationName::CXXConversionFunctionName:
4369 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4370 break;
4371
4372 case DeclarationName::CXXOperatorName:
4373 AddSourceLocation(
4374 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4375 Record);
4376 AddSourceLocation(
4377 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4378 Record);
4379 break;
4380
4381 case DeclarationName::CXXLiteralOperatorName:
4382 AddSourceLocation(
4383 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4384 Record);
4385 break;
4386
4387 case DeclarationName::Identifier:
4388 case DeclarationName::ObjCZeroArgSelector:
4389 case DeclarationName::ObjCOneArgSelector:
4390 case DeclarationName::ObjCMultiArgSelector:
4391 case DeclarationName::CXXUsingDirective:
4392 break;
4393 }
4394}
4395
4396void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004397 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004398 AddDeclarationName(NameInfo.getName(), Record);
4399 AddSourceLocation(NameInfo.getLoc(), Record);
4400 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4401}
4402
4403void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004404 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004405 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004406 Record.push_back(Info.NumTemplParamLists);
4407 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4408 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4409}
4410
Sebastian Redla4232eb2010-08-18 23:56:21 +00004411void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004412 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004413 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004414 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004415 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004416
4417 // Push each of the NNS's onto a stack for serialization in reverse order.
4418 while (NNS) {
4419 NestedNames.push_back(NNS);
4420 NNS = NNS->getPrefix();
4421 }
4422
4423 Record.push_back(NestedNames.size());
4424 while(!NestedNames.empty()) {
4425 NNS = NestedNames.pop_back_val();
4426 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4427 Record.push_back(Kind);
4428 switch (Kind) {
4429 case NestedNameSpecifier::Identifier:
4430 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4431 break;
4432
4433 case NestedNameSpecifier::Namespace:
4434 AddDeclRef(NNS->getAsNamespace(), Record);
4435 break;
4436
Douglas Gregor14aba762011-02-24 02:36:08 +00004437 case NestedNameSpecifier::NamespaceAlias:
4438 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4439 break;
4440
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004441 case NestedNameSpecifier::TypeSpec:
4442 case NestedNameSpecifier::TypeSpecWithTemplate:
4443 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4444 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4445 break;
4446
4447 case NestedNameSpecifier::Global:
4448 // Don't need to write an associated value.
4449 break;
4450 }
4451 }
4452}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004453
Douglas Gregordc355712011-02-25 00:36:19 +00004454void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4455 RecordDataImpl &Record) {
4456 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004457 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004458 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004459
4460 // Push each of the nested-name-specifiers's onto a stack for
4461 // serialization in reverse order.
4462 while (NNS) {
4463 NestedNames.push_back(NNS);
4464 NNS = NNS.getPrefix();
4465 }
4466
4467 Record.push_back(NestedNames.size());
4468 while(!NestedNames.empty()) {
4469 NNS = NestedNames.pop_back_val();
4470 NestedNameSpecifier::SpecifierKind Kind
4471 = NNS.getNestedNameSpecifier()->getKind();
4472 Record.push_back(Kind);
4473 switch (Kind) {
4474 case NestedNameSpecifier::Identifier:
4475 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4476 AddSourceRange(NNS.getLocalSourceRange(), Record);
4477 break;
4478
4479 case NestedNameSpecifier::Namespace:
4480 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4481 AddSourceRange(NNS.getLocalSourceRange(), Record);
4482 break;
4483
4484 case NestedNameSpecifier::NamespaceAlias:
4485 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4486 AddSourceRange(NNS.getLocalSourceRange(), Record);
4487 break;
4488
4489 case NestedNameSpecifier::TypeSpec:
4490 case NestedNameSpecifier::TypeSpecWithTemplate:
4491 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4492 AddTypeLoc(NNS.getTypeLoc(), Record);
4493 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4494 break;
4495
4496 case NestedNameSpecifier::Global:
4497 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4498 break;
4499 }
4500 }
4501}
4502
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004503void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004504 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004505 Record.push_back(Kind);
4506 switch (Kind) {
4507 case TemplateName::Template:
4508 AddDeclRef(Name.getAsTemplateDecl(), Record);
4509 break;
4510
4511 case TemplateName::OverloadedTemplate: {
4512 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4513 Record.push_back(OvT->size());
4514 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4515 I != E; ++I)
4516 AddDeclRef(*I, Record);
4517 break;
4518 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004519
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004520 case TemplateName::QualifiedTemplate: {
4521 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4522 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4523 Record.push_back(QualT->hasTemplateKeyword());
4524 AddDeclRef(QualT->getTemplateDecl(), Record);
4525 break;
4526 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004527
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004528 case TemplateName::DependentTemplate: {
4529 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4530 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4531 Record.push_back(DepT->isIdentifier());
4532 if (DepT->isIdentifier())
4533 AddIdentifierRef(DepT->getIdentifier(), Record);
4534 else
4535 Record.push_back(DepT->getOperator());
4536 break;
4537 }
John McCall14606042011-06-30 08:33:18 +00004538
4539 case TemplateName::SubstTemplateTemplateParm: {
4540 SubstTemplateTemplateParmStorage *subst
4541 = Name.getAsSubstTemplateTemplateParm();
4542 AddDeclRef(subst->getParameter(), Record);
4543 AddTemplateName(subst->getReplacement(), Record);
4544 break;
4545 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004546
4547 case TemplateName::SubstTemplateTemplateParmPack: {
4548 SubstTemplateTemplateParmPackStorage *SubstPack
4549 = Name.getAsSubstTemplateTemplateParmPack();
4550 AddDeclRef(SubstPack->getParameterPack(), Record);
4551 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4552 break;
4553 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004554 }
4555}
4556
Michael J. Spencer20249a12010-10-21 03:16:25 +00004557void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004558 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004559 Record.push_back(Arg.getKind());
4560 switch (Arg.getKind()) {
4561 case TemplateArgument::Null:
4562 break;
4563 case TemplateArgument::Type:
4564 AddTypeRef(Arg.getAsType(), Record);
4565 break;
4566 case TemplateArgument::Declaration:
4567 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004568 Record.push_back(Arg.isDeclForReferenceParam());
4569 break;
4570 case TemplateArgument::NullPtr:
4571 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004572 break;
4573 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004574 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004575 AddTypeRef(Arg.getIntegralType(), Record);
4576 break;
4577 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004578 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4579 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004580 case TemplateArgument::TemplateExpansion:
4581 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004582 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004583 Record.push_back(*NumExpansions + 1);
4584 else
4585 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004586 break;
4587 case TemplateArgument::Expression:
4588 AddStmt(Arg.getAsExpr());
4589 break;
4590 case TemplateArgument::Pack:
4591 Record.push_back(Arg.pack_size());
4592 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4593 I != E; ++I)
4594 AddTemplateArgument(*I, Record);
4595 break;
4596 }
4597}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004598
4599void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004600ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004601 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004602 assert(TemplateParams && "No TemplateParams!");
4603 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4604 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4605 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4606 Record.push_back(TemplateParams->size());
4607 for (TemplateParameterList::const_iterator
4608 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4609 P != PEnd; ++P)
4610 AddDeclRef(*P, Record);
4611}
4612
4613/// \brief Emit a template argument list.
4614void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004615ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004616 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004617 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004618 Record.push_back(TemplateArgs->size());
4619 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004620 AddTemplateArgument(TemplateArgs->get(i), Record);
4621}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004622
4623
4624void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004625ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004626 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004627 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004628 I = Set.begin(), E = Set.end(); I != E; ++I) {
4629 AddDeclRef(I.getDecl(), Record);
4630 Record.push_back(I.getAccess());
4631 }
4632}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004633
Sebastian Redla4232eb2010-08-18 23:56:21 +00004634void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004635 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004636 Record.push_back(Base.isVirtual());
4637 Record.push_back(Base.isBaseOfClass());
4638 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004639 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004640 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004641 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004642 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4643 : SourceLocation(),
4644 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004645}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004646
Douglas Gregor7c789c12010-10-29 22:39:52 +00004647void ASTWriter::FlushCXXBaseSpecifiers() {
4648 RecordData Record;
4649 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4650 Record.clear();
4651
4652 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004653 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004654 if (Index == CXXBaseSpecifiersOffsets.size())
4655 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4656 else {
4657 if (Index > CXXBaseSpecifiersOffsets.size())
4658 CXXBaseSpecifiersOffsets.resize(Index + 1);
4659 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4660 }
4661
4662 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4663 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4664 Record.push_back(BEnd - B);
4665 for (; B != BEnd; ++B)
4666 AddCXXBaseSpecifier(*B, Record);
4667 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004668
4669 // Flush any expressions that were written as part of the base specifiers.
4670 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004671 }
4672
4673 CXXBaseSpecifiersToWrite.clear();
4674}
4675
Sean Huntcbb67482011-01-08 20:30:50 +00004676void ASTWriter::AddCXXCtorInitializers(
4677 const CXXCtorInitializer * const *CtorInitializers,
4678 unsigned NumCtorInitializers,
4679 RecordDataImpl &Record) {
4680 Record.push_back(NumCtorInitializers);
4681 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4682 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004683
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004684 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004685 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004686 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004687 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004688 } else if (Init->isDelegatingInitializer()) {
4689 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004690 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004691 } else if (Init->isMemberInitializer()){
4692 Record.push_back(CTOR_INITIALIZER_MEMBER);
4693 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004694 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004695 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4696 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004697 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004698
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004699 AddSourceLocation(Init->getMemberLocation(), Record);
4700 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004701 AddSourceLocation(Init->getLParenLoc(), Record);
4702 AddSourceLocation(Init->getRParenLoc(), Record);
4703 Record.push_back(Init->isWritten());
4704 if (Init->isWritten()) {
4705 Record.push_back(Init->getSourceOrder());
4706 } else {
4707 Record.push_back(Init->getNumArrayIndices());
4708 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4709 AddDeclRef(Init->getArrayIndex(i), Record);
4710 }
4711 }
4712}
4713
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004714void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4715 assert(D->DefinitionData);
4716 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004717 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004718 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004719 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004720 Record.push_back(Data.Aggregate);
4721 Record.push_back(Data.PlainOldData);
4722 Record.push_back(Data.Empty);
4723 Record.push_back(Data.Polymorphic);
4724 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004725 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004726 Record.push_back(Data.HasNoNonEmptyBases);
4727 Record.push_back(Data.HasPrivateFields);
4728 Record.push_back(Data.HasProtectedFields);
4729 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004730 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004731 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004732 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004733 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004734 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4735 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4736 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4737 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4738 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4739 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004740 Record.push_back(Data.HasTrivialSpecialMembers);
4741 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004742 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004743 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004744 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004745 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004746 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004747 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004748 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00004749 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
4750 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
4751 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
4752 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00004753 Record.push_back(Data.FailedImplicitMoveConstructor);
4754 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004755 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004756
4757 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004758 if (Data.NumBases > 0)
4759 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4760 Record);
4761
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004762 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4763 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004764 if (Data.NumVBases > 0)
4765 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4766 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004767
4768 AddUnresolvedSet(Data.Conversions, Record);
4769 AddUnresolvedSet(Data.VisibleConversions, Record);
4770 // Data.Definition is the owning decl, no need to write it.
4771 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004772
4773 // Add lambda-specific data.
4774 if (Data.IsLambda) {
4775 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004776 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004777 Record.push_back(Lambda.NumCaptures);
4778 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004779 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004780 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004781 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004782 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4783 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4784 AddSourceLocation(Capture.getLocation(), Record);
4785 Record.push_back(Capture.isImplicit());
4786 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4787 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4788 AddDeclRef(Var, Record);
4789 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4790 : SourceLocation(),
4791 Record);
4792 }
4793 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004794}
4795
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004796void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004797 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004798 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004799 assert(FirstDeclID == NextDeclID &&
4800 FirstTypeID == NextTypeID &&
4801 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004802 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004803 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004804 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004805 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004806
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004807 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004808
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004809 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4810 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4811 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004812 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004813 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004814 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004815 NextDeclID = FirstDeclID;
4816 NextTypeID = FirstTypeID;
4817 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004818 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004819 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004820 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004821}
4822
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004823void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004824 // Always keep the highest ID. See \p TypeRead() for more information.
4825 IdentID &StoredID = IdentifierIDs[II];
4826 if (ID > StoredID)
4827 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004828}
4829
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004830void ASTWriter::MacroRead(serialization::MacroID ID, MacroDirective *MD) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004831 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004832 MacroID &StoredID = MacroIDs[MD];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004833 if (ID > StoredID)
4834 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004835}
4836
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004837void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004838 // Always take the highest-numbered type index. This copes with an interesting
4839 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004840 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004841 // keep the higher-numbered entry so that we can properly write it out to
4842 // the AST file.
4843 TypeIdx &StoredIdx = TypeIdxs[T];
4844 if (Idx.getIndex() >= StoredIdx.getIndex())
4845 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004846}
4847
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004848void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004849 // Always keep the highest ID. See \p TypeRead() for more information.
4850 SelectorID &StoredID = SelectorIDs[S];
4851 if (ID > StoredID)
4852 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00004853}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004854
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004855void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004856 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004857 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004858 MacroDefinitions[MD] = ID;
4859}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004860
Douglas Gregora015cab2011-12-02 17:30:13 +00004861void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4862 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4863 SubmoduleIDs[Mod] = ID;
4864}
4865
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004866void ASTWriter::UndefinedMacro(MacroDirective *MD) {
4867 MacroUpdates[MD].UndefLoc = MD->getUndefLoc();
Douglas Gregora8235d62012-10-09 23:05:51 +00004868}
4869
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004870void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004871 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004872 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004873 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4874 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004875 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004876 // A forward reference was mutated into a definition. Rewrite it.
4877 // FIXME: This happens during template instantiation, should we
4878 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004879 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004880 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004881 }
4882}
Douglas Gregora8235d62012-10-09 23:05:51 +00004883
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004884void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004885 assert(!WritingAST && "Already writing the AST!");
4886
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004887 // TU and namespaces are handled elsewhere.
4888 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4889 return;
4890
Douglas Gregor919814d2011-09-09 23:01:35 +00004891 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004892 return; // Not a source decl added to a DeclContext from PCH.
4893
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00004894 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004895 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004896 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004897}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004898
4899void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004900 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004901 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004902 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004903 return; // Not a source member added to a class from PCH.
4904 if (!isa<CXXMethodDecl>(D))
4905 return; // We are interested in lazily declared implicit methods.
4906
4907 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004908 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004909 UpdateRecord &Record = DeclUpdates[RD];
4910 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004911 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004912}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004913
4914void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4915 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004916 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004917 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004918 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004919 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004920 return; // Not a source specialization added to a template from PCH.
4921
4922 UpdateRecord &Record = DeclUpdates[TD];
4923 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004924 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004925}
Douglas Gregor89d99802010-11-30 06:16:57 +00004926
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004927void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4928 const FunctionDecl *D) {
4929 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004930 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004931 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004932 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004933 return; // Not a source specialization added to a template from PCH.
4934
4935 UpdateRecord &Record = DeclUpdates[TD];
4936 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004937 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004938}
4939
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004940void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004941 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004942 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004943 return; // Declaration not imported from PCH.
4944
4945 // Implicit decl from a PCH was defined.
4946 // FIXME: Should implicit definition be a separate FunctionDecl?
4947 RewriteDecl(D);
4948}
4949
Sebastian Redlf79a7192011-04-29 08:19:30 +00004950void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004951 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004952 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004953 return;
4954
4955 // Since the actual instantiation is delayed, this really means that we need
4956 // to update the instantiation location.
4957 UpdateRecord &Record = DeclUpdates[D];
4958 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4959 AddSourceLocation(
4960 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4961}
4962
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004963void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4964 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004965 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004966 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004967 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004968
4969 assert(IFD->getDefinition() && "Category on a class without a definition?");
4970 ObjCClassesWithCategories.insert(
4971 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004972}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004973
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004974
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004975void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4976 const ObjCPropertyDecl *OrigProp,
4977 const ObjCCategoryDecl *ClassExt) {
4978 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4979 if (!D)
4980 return;
4981
4982 assert(!WritingAST && "Already writing the AST!");
4983 if (!D->isFromASTFile())
4984 return; // Declaration not imported from PCH.
4985
4986 RewriteDecl(D);
4987}