blob: 95e32a37fe6fcd185ba9925c8d9ec153a42a1054 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
Sebastian Redlade50002010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000059static StringRef data(const std::vector<T, Allocator> &v) {
60 if (v.empty()) return StringRef();
61 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000063}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000069}
70
Douglas Gregor2cf26342009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000074
Douglas Gregor2cf26342009-04-09 22:27:44 +000075namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000086
87 void VisitArrayType(const ArrayType *T);
88 void VisitFunctionType(const FunctionType *T);
89 void VisitTagType(const TagType *T);
90
91#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
92#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000113 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114}
115
Sebastian Redl3397c552010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000117 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
118 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000123 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000124 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125}
126
Sebastian Redl3397c552010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 Writer.AddTypeRef(T->getPointeeType(), Record);
129 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000136 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137}
138
Sebastian Redl3397c552010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140 VisitArrayType(T);
141 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000147 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
Sebastian Redl3397c552010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000152 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
153 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000154 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000155 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156}
157
Sebastian Redl3397c552010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getElementType(), Record);
160 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000161 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000162 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163}
164
Sebastian Redl3397c552010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000167 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168}
169
Sebastian Redl3397c552010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000172 FunctionType::ExtInfo C = T->getExtInfo();
173 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000174 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000175 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000176 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000177 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000178 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179}
180
Sebastian Redl3397c552010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000183 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184}
185
Sebastian Redl3397c552010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187 VisitFunctionType(T);
188 Record.push_back(T->getNumArgs());
189 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
190 Writer.AddTypeRef(T->getArgType(I), Record);
191 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000192 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000194 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000195 Record.push_back(T->getExceptionSpecType());
196 if (T->getExceptionSpecType() == EST_Dynamic) {
197 Record.push_back(T->getNumExceptions());
198 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
199 Writer.AddTypeRef(T->getExceptionType(I), Record);
200 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
201 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000202 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
204 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000205 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
206 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000207 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000208 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209}
210
Sebastian Redl3397c552010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000212 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000214}
John McCalled976492009-12-04 22:46:56 +0000215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000218 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
219 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226}
227
Sebastian Redl3397c552010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000230 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231}
232
Sebastian Redl3397c552010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000234 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000235 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000236 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000237}
238
Sean Huntca63c202011-05-24 22:41:36 +0000239void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
240 Writer.AddTypeRef(T->getBaseType(), Record);
241 Writer.AddTypeRef(T->getUnderlyingType(), Record);
242 Record.push_back(T->getUTTKind());
243 Code = TYPE_UNARY_TRANSFORM;
244}
245
Richard Smith34b41d92011-02-20 03:19:35 +0000246void ASTTypeWriter::VisitAutoType(const AutoType *T) {
247 Writer.AddTypeRef(T->getDeducedType(), Record);
248 Code = TYPE_AUTO;
249}
250
Sebastian Redl3397c552010-08-18 23:56:27 +0000251void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000252 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000253 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000254 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255 "Cannot serialize in the middle of a type definition");
256}
257
Sebastian Redl3397c552010-08-18 23:56:27 +0000258void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000259 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000260 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261}
262
Sebastian Redl3397c552010-08-18 23:56:27 +0000263void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000265 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000266}
267
John McCall9d156a72011-01-06 01:58:22 +0000268void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
269 Writer.AddTypeRef(T->getModifiedType(), Record);
270 Writer.AddTypeRef(T->getEquivalentType(), Record);
271 Record.push_back(T->getAttrKind());
272 Code = TYPE_ATTRIBUTED;
273}
274
Mike Stump1eb44332009-09-09 15:08:12 +0000275void
Sebastian Redl3397c552010-08-18 23:56:27 +0000276ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000277 const SubstTemplateTypeParmType *T) {
278 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
279 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000280 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000281}
282
283void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000284ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
285 const SubstTemplateTypeParmPackType *T) {
286 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
287 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
288 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
289}
290
291void
Sebastian Redl3397c552010-08-18 23:56:27 +0000292ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000293 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000294 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000295 Writer.AddTemplateName(T->getTemplateName(), Record);
296 Record.push_back(T->getNumArgs());
297 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
298 ArgI != ArgE; ++ArgI)
299 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000300 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
301 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000302 : T->getCanonicalTypeInternal(),
303 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000304 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000305}
306
307void
Sebastian Redl3397c552010-08-18 23:56:27 +0000308ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000309 VisitArrayType(T);
310 Writer.AddStmt(T->getSizeExpr());
311 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000312 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313}
314
315void
Sebastian Redl3397c552010-08-18 23:56:27 +0000316ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317 const DependentSizedExtVectorType *T) {
318 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000319 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320}
321
322void
Sebastian Redl3397c552010-08-18 23:56:27 +0000323ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000324 Record.push_back(T->getDepth());
325 Record.push_back(T->getIndex());
326 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000327 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000333 Record.push_back(T->getKeyword());
334 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
335 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000336 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
337 : T->getCanonicalTypeInternal(),
338 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000339 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl3397c552010-08-18 23:56:27 +0000343ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000344 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000345 Record.push_back(T->getKeyword());
346 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
347 Writer.AddIdentifierRef(T->getIdentifier(), Record);
348 Record.push_back(T->getNumArgs());
349 for (DependentTemplateSpecializationType::iterator
350 I = T->begin(), E = T->end(); I != E; ++I)
351 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000352 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000353}
354
Douglas Gregor7536dd52010-12-20 02:24:11 +0000355void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
356 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000357 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000358 Record.push_back(*NumExpansions + 1);
359 else
360 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000361 Code = TYPE_PACK_EXPANSION;
362}
363
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000364void ASTTypeWriter::VisitParenType(const ParenType *T) {
365 Writer.AddTypeRef(T->getInnerType(), Record);
366 Code = TYPE_PAREN;
367}
368
Sebastian Redl3397c552010-08-18 23:56:27 +0000369void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000370 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000371 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
372 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000374}
375
Sebastian Redl3397c552010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000377 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000378 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000379 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000380}
381
Sebastian Redl3397c552010-08-18 23:56:27 +0000382void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000383 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000384 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000385}
386
Sebastian Redl3397c552010-08-18 23:56:27 +0000387void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000388 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000389 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000390 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000391 E = T->qual_end(); I != E; ++I)
392 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000393 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000394}
395
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000396void
Sebastian Redl3397c552010-08-18 23:56:27 +0000397ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000398 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000399 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000400}
401
Eli Friedmanb001de72011-10-06 23:00:33 +0000402void
403ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
404 Writer.AddTypeRef(T->getValueType(), Record);
405 Code = TYPE_ATOMIC;
406}
407
John McCalla1ee0c52009-10-16 21:56:05 +0000408namespace {
409
410class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000411 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000412 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000413
414public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000415 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000416 : Writer(Writer), Record(Record) { }
417
John McCall51bd8032009-10-18 01:05:36 +0000418#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000419#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000420 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000421#include "clang/AST/TypeLocNodes.def"
422
John McCall51bd8032009-10-18 01:05:36 +0000423 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
424 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000425};
426
427}
428
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
430 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000431}
John McCall51bd8032009-10-18 01:05:36 +0000432void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000433 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
434 if (TL.needsExtraLocalData()) {
435 Record.push_back(TL.getWrittenTypeSpec());
436 Record.push_back(TL.getWrittenSignSpec());
437 Record.push_back(TL.getWrittenWidthSpec());
438 Record.push_back(TL.hasModeAttr());
439 }
John McCalla1ee0c52009-10-16 21:56:05 +0000440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000446}
John McCall51bd8032009-10-18 01:05:36 +0000447void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000458 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000459}
John McCall51bd8032009-10-18 01:05:36 +0000460void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
462 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
463 Record.push_back(TL.getSizeExpr() ? 1 : 0);
464 if (TL.getSizeExpr())
465 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000466}
John McCall51bd8032009-10-18 01:05:36 +0000467void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
468 VisitArrayTypeLoc(TL);
469}
470void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
471 VisitArrayTypeLoc(TL);
472}
473void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
477 DependentSizedArrayTypeLoc TL) {
478 VisitArrayTypeLoc(TL);
479}
480void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
481 DependentSizedExtVectorTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
484void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
487void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
490void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000491 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000492 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
493 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000494 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000495 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
496 Writer.AddDeclRef(TL.getArg(i), Record);
497}
498void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
499 VisitFunctionTypeLoc(TL);
500}
501void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
502 VisitFunctionTypeLoc(TL);
503}
John McCalled976492009-12-04 22:46:56 +0000504void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
John McCall51bd8032009-10-18 01:05:36 +0000507void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000511 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
512 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
513 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000514}
515void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000516 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
517 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
518 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
519 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000520}
521void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
Sean Huntca63c202011-05-24 22:41:36 +0000524void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getKWLoc(), Record);
526 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
527 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
528 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
529}
Richard Smith34b41d92011-02-20 03:19:35 +0000530void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
John McCall51bd8032009-10-18 01:05:36 +0000533void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
536void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getNameLoc(), Record);
538}
John McCall9d156a72011-01-06 01:58:22 +0000539void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
540 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
541 if (TL.hasAttrOperand()) {
542 SourceRange range = TL.getAttrOperandParensRange();
543 Writer.AddSourceLocation(range.getBegin(), Record);
544 Writer.AddSourceLocation(range.getEnd(), Record);
545 }
546 if (TL.hasAttrExprOperand()) {
547 Expr *operand = TL.getAttrExprOperand();
548 Record.push_back(operand ? 1 : 0);
549 if (operand) Writer.AddStmt(operand);
550 } else if (TL.hasAttrEnumOperand()) {
551 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
552 }
553}
John McCall51bd8032009-10-18 01:05:36 +0000554void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
John McCall49a832b2009-10-18 09:09:24 +0000557void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
558 SubstTemplateTypeParmTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getNameLoc(), Record);
560}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000561void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
562 SubstTemplateTypeParmPackTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getNameLoc(), Record);
564}
John McCall51bd8032009-10-18 01:05:36 +0000565void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
566 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000567 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000568 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
569 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
570 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
571 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000572 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
573 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000574}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000575void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
576 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
577 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
578}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000579void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000580 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000582}
John McCall3cb0ebd2010-03-10 03:28:59 +0000583void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getNameLoc(), Record);
585}
Douglas Gregor4714c122010-03-31 17:34:00 +0000586void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000587 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000588 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000589 Writer.AddSourceLocation(TL.getNameLoc(), Record);
590}
John McCall33500952010-06-11 00:33:02 +0000591void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
592 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000594 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000595 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000596 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000600 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
601 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000602}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000603void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
605}
John McCall51bd8032009-10-18 01:05:36 +0000606void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
607 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000608}
609void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
610 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000611 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
612 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
613 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
614 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000615}
John McCall54e14c42009-10-22 22:37:11 +0000616void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
617 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000618}
Eli Friedmanb001de72011-10-06 23:00:33 +0000619void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
620 Writer.AddSourceLocation(TL.getKWLoc(), Record);
621 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
622 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
623}
John McCalla1ee0c52009-10-16 21:56:05 +0000624
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000625//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000626// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000627//===----------------------------------------------------------------------===//
628
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000629static void EmitBlockID(unsigned ID, const char *Name,
630 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 Record.clear();
633 Record.push_back(ID);
634 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
635
636 // Emit the block name if present.
637 if (Name == 0 || Name[0] == 0) return;
638 Record.clear();
639 while (*Name)
640 Record.push_back(*Name++);
641 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
642}
643
644static void EmitRecordID(unsigned ID, const char *Name,
645 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000646 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000647 Record.clear();
648 Record.push_back(ID);
649 while (*Name)
650 Record.push_back(*Name++);
651 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000652}
653
654static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000655 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000656#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000657 RECORD(STMT_STOP);
658 RECORD(STMT_NULL_PTR);
659 RECORD(STMT_NULL);
660 RECORD(STMT_COMPOUND);
661 RECORD(STMT_CASE);
662 RECORD(STMT_DEFAULT);
663 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000664 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000665 RECORD(STMT_IF);
666 RECORD(STMT_SWITCH);
667 RECORD(STMT_WHILE);
668 RECORD(STMT_DO);
669 RECORD(STMT_FOR);
670 RECORD(STMT_GOTO);
671 RECORD(STMT_INDIRECT_GOTO);
672 RECORD(STMT_CONTINUE);
673 RECORD(STMT_BREAK);
674 RECORD(STMT_RETURN);
675 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000676 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000677 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000678 RECORD(EXPR_PREDEFINED);
679 RECORD(EXPR_DECL_REF);
680 RECORD(EXPR_INTEGER_LITERAL);
681 RECORD(EXPR_FLOATING_LITERAL);
682 RECORD(EXPR_IMAGINARY_LITERAL);
683 RECORD(EXPR_STRING_LITERAL);
684 RECORD(EXPR_CHARACTER_LITERAL);
685 RECORD(EXPR_PAREN);
686 RECORD(EXPR_UNARY_OPERATOR);
687 RECORD(EXPR_SIZEOF_ALIGN_OF);
688 RECORD(EXPR_ARRAY_SUBSCRIPT);
689 RECORD(EXPR_CALL);
690 RECORD(EXPR_MEMBER);
691 RECORD(EXPR_BINARY_OPERATOR);
692 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
693 RECORD(EXPR_CONDITIONAL_OPERATOR);
694 RECORD(EXPR_IMPLICIT_CAST);
695 RECORD(EXPR_CSTYLE_CAST);
696 RECORD(EXPR_COMPOUND_LITERAL);
697 RECORD(EXPR_EXT_VECTOR_ELEMENT);
698 RECORD(EXPR_INIT_LIST);
699 RECORD(EXPR_DESIGNATED_INIT);
700 RECORD(EXPR_IMPLICIT_VALUE_INIT);
701 RECORD(EXPR_VA_ARG);
702 RECORD(EXPR_ADDR_LABEL);
703 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000704 RECORD(EXPR_CHOOSE);
705 RECORD(EXPR_GNU_NULL);
706 RECORD(EXPR_SHUFFLE_VECTOR);
707 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000708 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000709 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000710 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000711 RECORD(EXPR_OBJC_ARRAY_LITERAL);
712 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000713 RECORD(EXPR_OBJC_ENCODE);
714 RECORD(EXPR_OBJC_SELECTOR_EXPR);
715 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
716 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
717 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
718 RECORD(EXPR_OBJC_KVC_REF_EXPR);
719 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000720 RECORD(STMT_OBJC_FOR_COLLECTION);
721 RECORD(STMT_OBJC_CATCH);
722 RECORD(STMT_OBJC_FINALLY);
723 RECORD(STMT_OBJC_AT_TRY);
724 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
725 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000726 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000727 RECORD(EXPR_CXX_OPERATOR_CALL);
728 RECORD(EXPR_CXX_CONSTRUCT);
729 RECORD(EXPR_CXX_STATIC_CAST);
730 RECORD(EXPR_CXX_DYNAMIC_CAST);
731 RECORD(EXPR_CXX_REINTERPRET_CAST);
732 RECORD(EXPR_CXX_CONST_CAST);
733 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000734 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000735 RECORD(EXPR_CXX_BOOL_LITERAL);
736 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000737 RECORD(EXPR_CXX_TYPEID_EXPR);
738 RECORD(EXPR_CXX_TYPEID_TYPE);
739 RECORD(EXPR_CXX_UUIDOF_EXPR);
740 RECORD(EXPR_CXX_UUIDOF_TYPE);
741 RECORD(EXPR_CXX_THIS);
742 RECORD(EXPR_CXX_THROW);
743 RECORD(EXPR_CXX_DEFAULT_ARG);
744 RECORD(EXPR_CXX_BIND_TEMPORARY);
745 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
746 RECORD(EXPR_CXX_NEW);
747 RECORD(EXPR_CXX_DELETE);
748 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
749 RECORD(EXPR_EXPR_WITH_CLEANUPS);
750 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
751 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
752 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
753 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
754 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
755 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
756 RECORD(EXPR_CXX_NOEXCEPT);
757 RECORD(EXPR_OPAQUE_VALUE);
758 RECORD(EXPR_BINARY_TYPE_TRAIT);
759 RECORD(EXPR_PACK_EXPANSION);
760 RECORD(EXPR_SIZEOF_PACK);
761 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000762 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000763#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764}
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Sebastian Redla4232eb2010-08-18 23:56:21 +0000766void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000767 RecordData Record;
768 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000770#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
771#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000773 // Control Block.
774 BLOCK(CONTROL_BLOCK);
775 RECORD(METADATA);
776 RECORD(IMPORTS);
777 RECORD(LANGUAGE_OPTIONS);
778 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000779 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000780 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000781 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000782 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000783 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000784 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000785 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000786 RECORD(PREPROCESSOR_OPTIONS);
787
Douglas Gregorc337fef2012-10-19 00:45:00 +0000788 BLOCK(INPUT_FILES_BLOCK);
789 RECORD(INPUT_FILE);
790
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000791 // AST Top-Level Block.
792 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000793 RECORD(TYPE_OFFSET);
794 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000795 RECORD(IDENTIFIER_OFFSET);
796 RECORD(IDENTIFIER_TABLE);
797 RECORD(EXTERNAL_DEFINITIONS);
798 RECORD(SPECIAL_TYPES);
799 RECORD(STATISTICS);
800 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000801 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000802 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000803 RECORD(SELECTOR_OFFSETS);
804 RECORD(METHOD_POOL);
805 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000806 RECORD(SOURCE_LOCATION_OFFSETS);
807 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000808 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000809 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000810 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000811 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000812 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000813 RECORD(SEMA_DECL_REFS);
814 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
815 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
816 RECORD(DECL_REPLACEMENTS);
817 RECORD(UPDATE_VISIBLE);
818 RECORD(DECL_UPDATE_OFFSETS);
819 RECORD(DECL_UPDATES);
820 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
821 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000822 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000823 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000824 RECORD(FP_PRAGMA_OPTIONS);
825 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000826 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000827 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000828 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000829 RECORD(MODULE_OFFSET_MAP);
830 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000831 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000832 RECORD(FILE_SORTED_DECLS);
833 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000834 RECORD(MERGED_DECLARATIONS);
835 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000836 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000837 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000838 RECORD(MACRO_TABLE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000839
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000841 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000842 RECORD(SM_SLOC_FILE_ENTRY);
843 RECORD(SM_SLOC_BUFFER_ENTRY);
844 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000845 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000847 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000848 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000849 RECORD(PP_MACRO_OBJECT_LIKE);
850 RECORD(PP_MACRO_FUNCTION_LIKE);
851 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000852
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000853 // Decls and Types block.
854 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000855 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000856 RECORD(TYPE_COMPLEX);
857 RECORD(TYPE_POINTER);
858 RECORD(TYPE_BLOCK_POINTER);
859 RECORD(TYPE_LVALUE_REFERENCE);
860 RECORD(TYPE_RVALUE_REFERENCE);
861 RECORD(TYPE_MEMBER_POINTER);
862 RECORD(TYPE_CONSTANT_ARRAY);
863 RECORD(TYPE_INCOMPLETE_ARRAY);
864 RECORD(TYPE_VARIABLE_ARRAY);
865 RECORD(TYPE_VECTOR);
866 RECORD(TYPE_EXT_VECTOR);
867 RECORD(TYPE_FUNCTION_PROTO);
868 RECORD(TYPE_FUNCTION_NO_PROTO);
869 RECORD(TYPE_TYPEDEF);
870 RECORD(TYPE_TYPEOF_EXPR);
871 RECORD(TYPE_TYPEOF);
872 RECORD(TYPE_RECORD);
873 RECORD(TYPE_ENUM);
874 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000875 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000876 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000877 RECORD(TYPE_DECLTYPE);
878 RECORD(TYPE_ELABORATED);
879 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
880 RECORD(TYPE_UNRESOLVED_USING);
881 RECORD(TYPE_INJECTED_CLASS_NAME);
882 RECORD(TYPE_OBJC_OBJECT);
883 RECORD(TYPE_TEMPLATE_TYPE_PARM);
884 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
885 RECORD(TYPE_DEPENDENT_NAME);
886 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
887 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
888 RECORD(TYPE_PAREN);
889 RECORD(TYPE_PACK_EXPANSION);
890 RECORD(TYPE_ATTRIBUTED);
891 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000892 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000893 RECORD(DECL_TYPEDEF);
894 RECORD(DECL_ENUM);
895 RECORD(DECL_RECORD);
896 RECORD(DECL_ENUM_CONSTANT);
897 RECORD(DECL_FUNCTION);
898 RECORD(DECL_OBJC_METHOD);
899 RECORD(DECL_OBJC_INTERFACE);
900 RECORD(DECL_OBJC_PROTOCOL);
901 RECORD(DECL_OBJC_IVAR);
902 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000903 RECORD(DECL_OBJC_CATEGORY);
904 RECORD(DECL_OBJC_CATEGORY_IMPL);
905 RECORD(DECL_OBJC_IMPLEMENTATION);
906 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
907 RECORD(DECL_OBJC_PROPERTY);
908 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000909 RECORD(DECL_FIELD);
910 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000911 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000912 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000913 RECORD(DECL_FILE_SCOPE_ASM);
914 RECORD(DECL_BLOCK);
915 RECORD(DECL_CONTEXT_LEXICAL);
916 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000917 RECORD(DECL_NAMESPACE);
918 RECORD(DECL_NAMESPACE_ALIAS);
919 RECORD(DECL_USING);
920 RECORD(DECL_USING_SHADOW);
921 RECORD(DECL_USING_DIRECTIVE);
922 RECORD(DECL_UNRESOLVED_USING_VALUE);
923 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
924 RECORD(DECL_LINKAGE_SPEC);
925 RECORD(DECL_CXX_RECORD);
926 RECORD(DECL_CXX_METHOD);
927 RECORD(DECL_CXX_CONSTRUCTOR);
928 RECORD(DECL_CXX_DESTRUCTOR);
929 RECORD(DECL_CXX_CONVERSION);
930 RECORD(DECL_ACCESS_SPEC);
931 RECORD(DECL_FRIEND);
932 RECORD(DECL_FRIEND_TEMPLATE);
933 RECORD(DECL_CLASS_TEMPLATE);
934 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
935 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
936 RECORD(DECL_FUNCTION_TEMPLATE);
937 RECORD(DECL_TEMPLATE_TYPE_PARM);
938 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
939 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
940 RECORD(DECL_STATIC_ASSERT);
941 RECORD(DECL_CXX_BASE_SPECIFIERS);
942 RECORD(DECL_INDIRECTFIELD);
943 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
944
Douglas Gregora72d8c42011-06-03 02:27:19 +0000945 // Statements and Exprs can occur in the Decls and Types block.
946 AddStmtsExprs(Stream, Record);
947
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000948 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000949 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000950 RECORD(PPD_MACRO_DEFINITION);
951 RECORD(PPD_INCLUSION_DIRECTIVE);
952
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000953#undef RECORD
954#undef BLOCK
955 Stream.ExitBlock();
956}
957
Douglas Gregore650c8c2009-07-07 00:12:59 +0000958/// \brief Adjusts the given filename to only write out the portion of the
959/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000960///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961/// \param Filename the file name to adjust.
962///
963/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
964/// the returned filename will be adjusted by this system root.
965///
966/// \returns either the original filename (if it needs no adjustment) or the
967/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000968static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000969adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregor832d6202011-07-22 16:35:34 +0000972 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 // Verify that the filename and the system root have the same prefix.
976 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000977 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978 if (Filename[Pos] != isysroot[Pos])
979 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 // We hit the end of the filename before we hit the end of the system root.
982 if (!Filename[Pos])
983 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 // If the file name has a '/' at the current position, skip over the '/'.
986 // We distinguish sysroot-based includes from absolute includes by the
987 // absence of '/' at the beginning of sysroot-based includes.
988 if (Filename[Pos] == '/')
989 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991 return Filename + Pos;
992}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000993
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000994/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000995void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
996 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000997 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000998 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000999 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1000 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001001
Douglas Gregore650c8c2009-07-07 00:12:59 +00001002 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001003 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1011 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1012 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1013 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001014 Record.push_back(VERSION_MAJOR);
1015 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001016 Record.push_back(CLANG_VERSION_MAJOR);
1017 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001018 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001019 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001020 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1021 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001022
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001023 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001024 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001025 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001026 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001027 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001028
1029 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1030 M != MEnd; ++M) {
1031 // Skip modules that weren't directly imported.
1032 if (!(*M)->isDirectlyImported())
1033 continue;
1034
1035 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001036 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001037 Record.push_back((*M)->File->getSize());
1038 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001039 // FIXME: This writes the absolute path for AST files we depend on.
1040 const std::string &FileName = (*M)->FileName;
1041 Record.push_back(FileName.size());
1042 Record.append(FileName.begin(), FileName.end());
1043 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001044 Stream.EmitRecord(IMPORTS, Record);
1045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001047 // Language options.
1048 Record.clear();
1049 const LangOptions &LangOpts = Context.getLangOpts();
1050#define LANGOPT(Name, Bits, Default, Description) \
1051 Record.push_back(LangOpts.Name);
1052#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1053 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1054#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001055#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1056#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001057
1058 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1059 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1060
1061 Record.push_back(LangOpts.CurrentModule.size());
1062 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001063
1064 // Comment options.
1065 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1066 for (CommentOptions::BlockCommandNamesTy::const_iterator
1067 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1068 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1069 I != IEnd; ++I) {
1070 AddString(*I, Record);
1071 }
1072
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001073 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1074
Douglas Gregoree097c12012-10-18 17:58:09 +00001075 // Target options.
1076 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001077 const TargetInfo &Target = Context.getTargetInfo();
1078 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001079 AddString(TargetOpts.Triple, Record);
1080 AddString(TargetOpts.CPU, Record);
1081 AddString(TargetOpts.ABI, Record);
1082 AddString(TargetOpts.CXXABI, Record);
1083 AddString(TargetOpts.LinkerVersion, Record);
1084 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1085 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1086 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1087 }
1088 Record.push_back(TargetOpts.Features.size());
1089 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1090 AddString(TargetOpts.Features[I], Record);
1091 }
1092 Stream.EmitRecord(TARGET_OPTIONS, Record);
1093
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001094 // Diagnostic options.
1095 Record.clear();
1096 const DiagnosticOptions &DiagOpts
1097 = Context.getDiagnostics().getDiagnosticOptions();
1098#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1099#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1100 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1101#include "clang/Basic/DiagnosticOptions.def"
1102 Record.push_back(DiagOpts.Warnings.size());
1103 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1104 AddString(DiagOpts.Warnings[I], Record);
1105 // Note: we don't serialize the log or serialization file names, because they
1106 // are generally transient files and will almost always be overridden.
1107 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1108
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001109 // File system options.
1110 Record.clear();
1111 const FileSystemOptions &FSOpts
1112 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1113 AddString(FSOpts.WorkingDir, Record);
1114 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1115
Douglas Gregorbbf38312012-10-24 16:50:34 +00001116 // Header search options.
1117 Record.clear();
1118 const HeaderSearchOptions &HSOpts
1119 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1120 AddString(HSOpts.Sysroot, Record);
1121
1122 // Include entries.
1123 Record.push_back(HSOpts.UserEntries.size());
1124 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1125 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1126 AddString(Entry.Path, Record);
1127 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001128 Record.push_back(Entry.IsFramework);
1129 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001130 }
1131
1132 // System header prefixes.
1133 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1134 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1135 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1136 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1137 }
1138
1139 AddString(HSOpts.ResourceDir, Record);
1140 AddString(HSOpts.ModuleCachePath, Record);
1141 Record.push_back(HSOpts.DisableModuleHash);
1142 Record.push_back(HSOpts.UseBuiltinIncludes);
1143 Record.push_back(HSOpts.UseStandardSystemIncludes);
1144 Record.push_back(HSOpts.UseStandardCXXIncludes);
1145 Record.push_back(HSOpts.UseLibcxx);
1146 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1147
Douglas Gregora71a7d82012-10-24 20:05:57 +00001148 // Preprocessor options.
1149 Record.clear();
1150 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1151
1152 // Macro definitions.
1153 Record.push_back(PPOpts.Macros.size());
1154 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1155 AddString(PPOpts.Macros[I].first, Record);
1156 Record.push_back(PPOpts.Macros[I].second);
1157 }
1158
1159 // Includes
1160 Record.push_back(PPOpts.Includes.size());
1161 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1162 AddString(PPOpts.Includes[I], Record);
1163
1164 // Macro includes
1165 Record.push_back(PPOpts.MacroIncludes.size());
1166 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1167 AddString(PPOpts.MacroIncludes[I], Record);
1168
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001169 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001170 AddString(PPOpts.ImplicitPCHInclude, Record);
1171 AddString(PPOpts.ImplicitPTHInclude, Record);
1172 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1173 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1174
Douglas Gregor31d375f2011-05-06 21:43:30 +00001175 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001176 SourceManager &SM = Context.getSourceManager();
1177 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1178 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001179 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1180 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001181 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1182 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1183
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001184 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001186 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001187
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001188 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001189 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001190 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001191 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001192 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001193 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001194 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001195 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001196
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001197 Record.clear();
1198 Record.push_back(SM.getMainFileID().getOpaqueValue());
1199 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1200
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001201 // Original PCH directory
1202 if (!OutputFile.empty() && OutputFile != "-") {
1203 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1204 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1206 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1207
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001208 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001209
1210 llvm::sys::fs::make_absolute(OutputPath);
1211 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1212
1213 RecordData Record;
1214 Record.push_back(ORIGINAL_PCH_DIR);
1215 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1216 }
1217
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001218 WriteInputFiles(Context.SourceMgr,
1219 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1220 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001221 Stream.ExitBlock();
1222}
1223
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001224namespace {
1225 /// \brief An input file.
1226 struct InputFileEntry {
1227 const FileEntry *File;
1228 bool IsSystemFile;
1229 bool BufferOverridden;
1230 };
1231}
1232
1233void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1234 HeaderSearchOptions &HSOpts,
1235 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001236 using namespace llvm;
1237 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1238 RecordData Record;
1239
1240 // Create input-file abbreviation.
1241 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1242 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001243 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001244 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1245 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001246 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001247 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1248 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1249
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001250 // Get all ContentCache objects for files, sorted by whether the file is a
1251 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001252 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001253 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1254 // Get this source location entry.
1255 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001256 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001257
1258 // We only care about file entries that were not overridden.
1259 if (!SLoc->isFile())
1260 continue;
1261 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001262 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001263 continue;
1264
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001265 InputFileEntry Entry;
1266 Entry.File = Cache->OrigEntry;
1267 Entry.IsSystemFile = Cache->IsSystemFile;
1268 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001269 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001270 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001271 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001272 SortedFiles.push_front(Entry);
1273 }
1274
1275 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1276 // the set of (non-system) input files. This is simple heuristic for
1277 // detecting whether the system headers may have changed, because it is too
1278 // expensive to stat() all of the system headers.
1279 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001280 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001281 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1282 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1283 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1284 InputFileEntry Entry = { SDKSettingsFile, false, false };
1285 SortedFiles.push_front(Entry);
1286 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001287 }
1288
1289 unsigned UserFilesNum = 0;
1290 // Write out all of the input files.
1291 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001292 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001293 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001294 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001295
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001296 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001297 if (InputFileID != 0)
1298 continue; // already recorded this file.
1299
Douglas Gregora930dc92012-10-22 18:42:04 +00001300 // Record this entry's offset.
1301 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001302
1303 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001304
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001305 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001306 ++UserFilesNum;
1307
Douglas Gregor745e6f12012-10-19 00:38:02 +00001308 Record.clear();
1309 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001310 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001311
1312 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001313 Record.push_back(Entry.File->getSize());
1314 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001315
Douglas Gregora930dc92012-10-22 18:42:04 +00001316 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001317 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001318
Douglas Gregor745e6f12012-10-19 00:38:02 +00001319 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001320 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001321 SmallString<128> FilePath(Filename);
1322
1323 // Ask the file manager to fixup the relative path for us. This will
1324 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001325 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001326
1327 // FIXME: This call to make_absolute shouldn't be necessary, the
1328 // call to FixupRelativePath should always return an absolute path.
1329 llvm::sys::fs::make_absolute(FilePath);
1330 Filename = FilePath.c_str();
1331
1332 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1333
1334 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1335 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001336
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001337 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001338
1339 // Create input file offsets abbreviation.
1340 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1341 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1342 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001343 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1344 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001345 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1346 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1347
1348 // Write input file offsets.
1349 Record.clear();
1350 Record.push_back(INPUT_FILE_OFFSETS);
1351 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001352 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001353 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001354}
1355
Douglas Gregor14f79002009-04-10 03:52:48 +00001356//===----------------------------------------------------------------------===//
1357// Source Manager Serialization
1358//===----------------------------------------------------------------------===//
1359
1360/// \brief Create an abbreviation for the SLocEntry that refers to a
1361/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001362static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001363 using namespace llvm;
1364 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001365 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001370 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001375 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001376}
1377
1378/// \brief Create an abbreviation for the SLocEntry that refers to a
1379/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001380static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001381 using namespace llvm;
1382 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001383 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001389 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001390}
1391
1392/// \brief Create an abbreviation for the SLocEntry that refers to a
1393/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001394static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001395 using namespace llvm;
1396 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001397 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001399 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001400}
1401
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001402/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1403/// expansion.
1404static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001405 using namespace llvm;
1406 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001407 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001413 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001414}
1415
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001416namespace {
1417 // Trait used for the on-disk hash table of header search information.
1418 class HeaderFileInfoTrait {
1419 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001420 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001421
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001422 // Keep track of the framework names we've used during serialization.
1423 SmallVector<char, 128> FrameworkStringData;
1424 llvm::StringMap<unsigned> FrameworkNameOffset;
1425
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001426 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001427 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1428 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001429
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001430 struct key_type {
1431 const FileEntry *FE;
1432 const char *Filename;
1433 };
1434 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001435
1436 typedef HeaderFileInfo data_type;
1437 typedef const data_type &data_type_ref;
1438
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001439 static unsigned ComputeHash(key_type_ref key) {
1440 // The hash is based only on size/time of the file, so that the reader can
1441 // match even when symlinking or excess path elements ("foo/../", "../")
1442 // change the form of the name. However, complete path is still the key.
1443 return llvm::hash_combine(key.FE->getSize(),
1444 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001445 }
1446
1447 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001448 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1449 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1450 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001451 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001452 if (Data.isModuleHeader)
1453 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001454 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001455 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001456 }
1457
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001458 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1459 clang::io::Emit64(Out, key.FE->getSize());
1460 KeyLen -= 8;
1461 clang::io::Emit64(Out, key.FE->getModificationTime());
1462 KeyLen -= 8;
1463 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001464 }
1465
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001466 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001467 data_type_ref Data, unsigned DataLen) {
1468 using namespace clang::io;
1469 uint64_t Start = Out.tell(); (void)Start;
1470
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001471 unsigned char Flags = (Data.isImport << 5)
1472 | (Data.isPragmaOnce << 4)
1473 | (Data.DirInfo << 2)
1474 | (Data.Resolved << 1)
1475 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001476 Emit8(Out, (uint8_t)Flags);
1477 Emit16(Out, (uint16_t) Data.NumIncludes);
1478
1479 if (!Data.ControllingMacro)
1480 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1481 else
1482 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001483
1484 unsigned Offset = 0;
1485 if (!Data.Framework.empty()) {
1486 // If this header refers into a framework, save the framework name.
1487 llvm::StringMap<unsigned>::iterator Pos
1488 = FrameworkNameOffset.find(Data.Framework);
1489 if (Pos == FrameworkNameOffset.end()) {
1490 Offset = FrameworkStringData.size() + 1;
1491 FrameworkStringData.append(Data.Framework.begin(),
1492 Data.Framework.end());
1493 FrameworkStringData.push_back(0);
1494
1495 FrameworkNameOffset[Data.Framework] = Offset;
1496 } else
1497 Offset = Pos->second;
1498 }
1499 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001500
1501 if (Data.isModuleHeader) {
1502 Module *Mod = HS.findModuleForHeader(key.FE);
1503 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1504 }
1505
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001506 assert(Out.tell() - Start == DataLen && "Wrong data length");
1507 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001508
1509 const char *strings_begin() const { return FrameworkStringData.begin(); }
1510 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001511 };
1512} // end anonymous namespace
1513
1514/// \brief Write the header search block for the list of files that
1515///
1516/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001517void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001518 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001519 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1520
1521 if (FilesByUID.size() > HS.header_file_size())
1522 FilesByUID.resize(HS.header_file_size());
1523
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001524 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001525 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001526 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001527 unsigned NumHeaderSearchEntries = 0;
1528 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1529 const FileEntry *File = FilesByUID[UID];
1530 if (!File)
1531 continue;
1532
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001533 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1534 // from the external source if it was not provided already.
1535 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001536 if (HFI.External && Chain)
1537 continue;
1538
1539 // Turn the file name into an absolute path, if it isn't already.
1540 const char *Filename = File->getName();
1541 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1542
1543 // If we performed any translation on the file name at all, we need to
1544 // save this string, since the generator will refer to it later.
1545 if (Filename != File->getName()) {
1546 Filename = strdup(Filename);
1547 SavedStrings.push_back(Filename);
1548 }
1549
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001550 HeaderFileInfoTrait::key_type key = { File, Filename };
1551 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001552 ++NumHeaderSearchEntries;
1553 }
1554
1555 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001556 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001557 uint32_t BucketOffset;
1558 {
1559 llvm::raw_svector_ostream Out(TableData);
1560 // Make sure that no bucket is at offset 0
1561 clang::io::Emit32(Out, 0);
1562 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1563 }
1564
1565 // Create a blob abbreviation
1566 using namespace llvm;
1567 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1568 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1573 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1574
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001575 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001576 RecordData Record;
1577 Record.push_back(HEADER_SEARCH_TABLE);
1578 Record.push_back(BucketOffset);
1579 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001580 Record.push_back(TableData.size());
1581 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001582 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1583
1584 // Free all of the strings we had to duplicate.
1585 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001586 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001587}
1588
Douglas Gregor14f79002009-04-10 03:52:48 +00001589/// \brief Writes the block containing the serialized form of the
1590/// source manager.
1591///
1592/// TODO: We should probably use an on-disk hash table (stored in a
1593/// blob), indexed based on the file name, so that we only create
1594/// entries for files that we actually need. In the common case (no
1595/// errors), we probably won't have to create file entries for any of
1596/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001597void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001598 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001599 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001600 RecordData Record;
1601
Chris Lattnerf04ad692009-04-10 17:16:57 +00001602 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001603 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001604
1605 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001606 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1607 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1608 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001609 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001610
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001611 // Write out the source location entry table. We skip the first
1612 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001613 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001614 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001615 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1616 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001617 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001618 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001619 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001620 FileID FID = FileID::get(I);
1621 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001622
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001623 // Record the offset of this source-location entry.
1624 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1625
1626 // Figure out which record code to use.
1627 unsigned Code;
1628 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001629 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1630 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001631 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001632 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001633 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001634 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001635 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001636 Record.clear();
1637 Record.push_back(Code);
1638
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001639 // Starting offset of this entry within this module, so skip the dummy.
1640 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001641 if (SLoc->isFile()) {
1642 const SrcMgr::FileInfo &File = SLoc->getFile();
1643 Record.push_back(File.getIncludeLoc().getRawEncoding());
1644 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1645 Record.push_back(File.hasLineDirectives());
1646
1647 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001648 if (Content->OrigEntry) {
1649 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001650 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001651
Douglas Gregora930dc92012-10-22 18:42:04 +00001652 // The source location entry is a file. Emit input file ID.
1653 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1654 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001656 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001657
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001658 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001659 if (FDI != FileDeclIDs.end()) {
1660 Record.push_back(FDI->second->FirstDeclIndex);
1661 Record.push_back(FDI->second->DeclIDs.size());
1662 } else {
1663 Record.push_back(0);
1664 Record.push_back(0);
1665 }
Douglas Gregora081da52011-11-16 20:05:18 +00001666
Douglas Gregora930dc92012-10-22 18:42:04 +00001667 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001668
1669 if (Content->BufferOverridden) {
1670 Record.clear();
1671 Record.push_back(SM_SLOC_BUFFER_BLOB);
1672 const llvm::MemoryBuffer *Buffer
1673 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1674 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1675 StringRef(Buffer->getBufferStart(),
1676 Buffer->getBufferSize() + 1));
1677 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001678 } else {
1679 // The source location entry is a buffer. The blob associated
1680 // with this entry contains the contents of the buffer.
1681
1682 // We add one to the size so that we capture the trailing NULL
1683 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1684 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001685 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001686 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001687 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001688 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001689 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001690 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001691 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001692 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001693 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001694 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001695
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001696 if (strcmp(Name, "<built-in>") == 0) {
1697 PreloadSLocs.push_back(SLocEntryOffsets.size());
1698 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001699 }
1700 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001701 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001702 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001703 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1704 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001705 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1706 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001707
1708 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001709 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001710 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001711 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001712 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001713 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001714 }
1715 }
1716
Douglas Gregorc9490c02009-04-16 22:23:12 +00001717 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001718
1719 if (SLocEntryOffsets.empty())
1720 return;
1721
Sebastian Redl3397c552010-08-18 23:56:27 +00001722 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001723 // table is used for lazily loading source-location information.
1724 using namespace llvm;
1725 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001726 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1730 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001732 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001733 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001734 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001735 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001736 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001737
Sebastian Redl3397c552010-08-18 23:56:27 +00001738 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001739 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001740 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001741
1742 // Write the line table. It depends on remapping working, so it must come
1743 // after the source location offsets.
1744 if (SourceMgr.hasLineTable()) {
1745 LineTableInfo &LineTable = SourceMgr.getLineTable();
1746
1747 Record.clear();
1748 // Emit the file names
1749 Record.push_back(LineTable.getNumFilenames());
1750 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1751 // Emit the file name
1752 const char *Filename = LineTable.getFilename(I);
1753 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1754 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1755 Record.push_back(FilenameLen);
1756 if (FilenameLen)
1757 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1758 }
1759
1760 // Emit the line entries
1761 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1762 L != LEnd; ++L) {
1763 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001764 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001765 continue;
1766
1767 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001768 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001769
1770 // Emit the line entries
1771 Record.push_back(L->second.size());
1772 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1773 LEEnd = L->second.end();
1774 LE != LEEnd; ++LE) {
1775 Record.push_back(LE->FileOffset);
1776 Record.push_back(LE->LineNo);
1777 Record.push_back(LE->FilenameID);
1778 Record.push_back((unsigned)LE->FileKind);
1779 Record.push_back(LE->IncludeOffset);
1780 }
1781 }
1782 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1783 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001784}
1785
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001786//===----------------------------------------------------------------------===//
1787// Preprocessor Serialization
1788//===----------------------------------------------------------------------===//
1789
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001790namespace {
1791class ASTMacroTableTrait {
1792public:
1793 typedef IdentID key_type;
1794 typedef key_type key_type_ref;
1795
1796 struct Data {
1797 uint32_t MacroDirectivesOffset;
1798 };
1799
1800 typedef Data data_type;
1801 typedef const data_type &data_type_ref;
1802
1803 static unsigned ComputeHash(IdentID IdID) {
1804 return llvm::hash_value(IdID);
1805 }
1806
1807 std::pair<unsigned,unsigned>
1808 static EmitKeyDataLength(raw_ostream& Out,
1809 key_type_ref Key, data_type_ref Data) {
1810 unsigned KeyLen = 4; // IdentID.
1811 unsigned DataLen = 4; // MacroDirectivesOffset.
1812 return std::make_pair(KeyLen, DataLen);
1813 }
1814
1815 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1816 clang::io::Emit32(Out, Key);
1817 }
1818
1819 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1820 unsigned) {
1821 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1822 }
1823};
1824} // end anonymous namespace
1825
1826static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1827 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1828 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1829 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1830 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001831 return X.first->getName().compare(Y.first->getName());
1832}
1833
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001834static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1835 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001836 if (MacroInfo *MI = MD->getMacroInfo())
1837 if (MI->isBuiltinMacro())
1838 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001839
1840 if (IsModule) {
1841 SourceLocation Loc = MD->getLocation();
1842 if (Loc.isInvalid())
1843 return true;
1844 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1845 return true;
1846 }
1847
1848 return false;
1849}
1850
Chris Lattner0b1fb982009-04-10 17:15:23 +00001851/// \brief Writes the block containing the serialized form of the
1852/// preprocessor.
1853///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001854void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001855 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1856 if (PPRec)
1857 WritePreprocessorDetail(*PPRec);
1858
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001859 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001860
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001861 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1862 if (PP.getCounterValue() != 0) {
1863 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001864 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001865 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001866 }
1867
1868 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001869 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Sebastian Redl3397c552010-08-18 23:56:27 +00001871 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001872 // FIXME: use diagnostics subsystem for localization etc.
1873 if (PP.SawDateOrTime())
1874 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Douglas Gregorecdcb882010-10-20 22:00:55 +00001876
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001877 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001878 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001879
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001880 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001881 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001882 MacroDirectives;
1883 for (Preprocessor::macro_iterator
1884 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1885 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001886 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001887 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001888 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001889
Douglas Gregor9c736102011-02-10 18:20:09 +00001890 // Sort the set of macro definitions that need to be serialized by the
1891 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001892 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1893 &compareMacroDirectives);
1894
1895 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1896
1897 // Emit the macro directives as a list and associate the offset with the
1898 // identifier they belong to.
1899 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1900 const IdentifierInfo *Name = MacroDirectives[I].first;
1901 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1902 MacroDirective *MD = MacroDirectives[I].second;
1903
1904 // If the macro or identifier need no updates, don't write the macro history
1905 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001906 // FIXME: Chain the macro history instead of re-writing it.
1907 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001908 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1909 continue;
1910
1911 // Emit the macro directives in reverse source order.
1912 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001913 if (MD->isHidden())
1914 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001915 if (shouldIgnoreMacro(MD, IsModule, PP))
1916 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001917
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001918 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001919 Record.push_back(MD->getKind());
1920 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1921 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1922 Record.push_back(InfoID);
1923 Record.push_back(DefMD->isImported());
1924 Record.push_back(DefMD->isAmbiguous());
1925
1926 } else if (VisibilityMacroDirective *
1927 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1928 Record.push_back(VisMD->isPublic());
1929 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001930 }
1931 if (Record.empty())
1932 continue;
1933
1934 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1935 Record.clear();
1936
1937 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1938
1939 IdentID NameID = getIdentifierRef(Name);
1940 ASTMacroTableTrait::Data data;
1941 data.MacroDirectivesOffset = MacroDirectiveOffset;
1942 Generator.insert(NameID, data);
1943 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001944
Douglas Gregora8235d62012-10-09 23:05:51 +00001945 /// \brief Offsets of each of the macros into the bitstream, indexed by
1946 /// the local macro ID
1947 ///
1948 /// For each identifier that is associated with a macro, this map
1949 /// provides the offset into the bitstream where that macro is
1950 /// defined.
1951 std::vector<uint32_t> MacroOffsets;
1952
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001953 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1954 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1955 MacroInfo *MI = MacroInfosToEmit[I].MI;
1956 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001957
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001958 if (ID < FirstMacroID) {
1959 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1960 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001961 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001962
1963 // Record the local offset of this macro.
1964 unsigned Index = ID - FirstMacroID;
1965 if (Index == MacroOffsets.size())
1966 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1967 else {
1968 if (Index > MacroOffsets.size())
1969 MacroOffsets.resize(Index + 1);
1970
1971 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1972 }
1973
1974 AddIdentifierRef(Name, Record);
1975 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1976 AddSourceLocation(MI->getDefinitionLoc(), Record);
1977 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1978 Record.push_back(MI->isUsed());
1979 unsigned Code;
1980 if (MI->isObjectLike()) {
1981 Code = PP_MACRO_OBJECT_LIKE;
1982 } else {
1983 Code = PP_MACRO_FUNCTION_LIKE;
1984
1985 Record.push_back(MI->isC99Varargs());
1986 Record.push_back(MI->isGNUVarargs());
1987 Record.push_back(MI->hasCommaPasting());
1988 Record.push_back(MI->getNumArgs());
1989 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1990 I != E; ++I)
1991 AddIdentifierRef(*I, Record);
1992 }
1993
1994 // If we have a detailed preprocessing record, record the macro definition
1995 // ID that corresponds to this macro.
1996 if (PPRec)
1997 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1998
1999 Stream.EmitRecord(Code, Record);
2000 Record.clear();
2001
2002 // Emit the tokens array.
2003 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2004 // Note that we know that the preprocessor does not have any annotation
2005 // tokens in it because they are created by the parser, and thus can't
2006 // be in a macro definition.
2007 const Token &Tok = MI->getReplacementToken(TokNo);
2008
2009 Record.push_back(Tok.getLocation().getRawEncoding());
2010 Record.push_back(Tok.getLength());
2011
2012 // FIXME: When reading literal tokens, reconstruct the literal pointer
2013 // if it is needed.
2014 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
2015 // FIXME: Should translate token kind to a stable encoding.
2016 Record.push_back(Tok.getKind());
2017 // FIXME: Should translate token flags to a stable encoding.
2018 Record.push_back(Tok.getFlags());
2019
2020 Stream.EmitRecord(PP_TOKEN, Record);
2021 Record.clear();
2022 }
2023 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002024 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002025
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002026 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002027
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002028 // Create the on-disk hash table in a buffer.
2029 SmallString<4096> MacroTable;
2030 uint32_t BucketOffset;
2031 {
2032 llvm::raw_svector_ostream Out(MacroTable);
2033 // Make sure that no bucket is at offset 0
2034 clang::io::Emit32(Out, 0);
2035 BucketOffset = Generator.Emit(Out);
2036 }
2037
2038 // Write the macro table
2039 using namespace llvm;
2040 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2041 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2044 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2045
2046 Record.push_back(MACRO_TABLE);
2047 Record.push_back(BucketOffset);
2048 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2049 Record.clear();
2050
Douglas Gregora8235d62012-10-09 23:05:51 +00002051 // Write the offsets table for macro IDs.
2052 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002053 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002054 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2058
2059 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2060 Record.clear();
2061 Record.push_back(MACRO_OFFSET);
2062 Record.push_back(MacroOffsets.size());
2063 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2064 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2065 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002066}
2067
2068void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002069 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002070 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002071
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002072 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002073
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002074 // Enter the preprocessor block.
2075 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002076
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002077 // If the preprocessor has a preprocessing record, emit it.
2078 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002079 using namespace llvm;
2080
2081 // Set up the abbreviation for
2082 unsigned InclusionAbbrev = 0;
2083 {
2084 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2085 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2091 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2092 }
2093
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002094 unsigned FirstPreprocessorEntityID
2095 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2096 + NUM_PREDEF_PP_ENTITY_IDS;
2097 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002098 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002099 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2100 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002101 E != EEnd;
2102 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002103 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002104
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002105 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2106 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002107
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002108 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002109 // Record this macro definition's ID.
2110 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002111
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002112 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002113 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2114 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002115 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002116
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002117 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002118 Record.push_back(ME->isBuiltinMacro());
2119 if (ME->isBuiltinMacro())
2120 AddIdentifierRef(ME->getName(), Record);
2121 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002122 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002123 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002124 continue;
2125 }
2126
2127 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2128 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002129 Record.push_back(ID->getFileName().size());
2130 Record.push_back(ID->wasInQuotes());
2131 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002132 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002133 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002134 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002135 // Check that the FileEntry is not null because it was not resolved and
2136 // we create a PCH even with compiler errors.
2137 if (ID->getFile())
2138 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002139 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2140 continue;
2141 }
2142
2143 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2144 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002145 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002146
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002147 // Write the offsets table for the preprocessing record.
2148 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002149 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2150
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002151 // Write the offsets table for identifier IDs.
2152 using namespace llvm;
2153 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002154 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002157 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002158
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002159 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002160 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002161 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002162 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2163 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002164 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002165}
2166
Douglas Gregore209e502011-12-06 01:10:29 +00002167unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2168 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2169 if (Known != SubmoduleIDs.end())
2170 return Known->second;
2171
2172 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2173}
2174
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002175unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2176 if (!Mod)
2177 return 0;
2178
2179 llvm::DenseMap<Module *, unsigned>::const_iterator
2180 Known = SubmoduleIDs.find(Mod);
2181 if (Known != SubmoduleIDs.end())
2182 return Known->second;
2183
2184 return 0;
2185}
2186
Douglas Gregor26ced122011-12-01 00:59:36 +00002187/// \brief Compute the number of modules within the given tree (including the
2188/// given module).
2189static unsigned getNumberOfModules(Module *Mod) {
2190 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002191 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2192 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002193 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002194 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002195
2196 return ChildModules + 1;
2197}
2198
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002199void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002200 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002201 // FIXME: This feels like it belongs somewhere else, but there are no
2202 // other consumers of this information.
2203 SourceManager &SrcMgr = PP->getSourceManager();
2204 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2205 for (ASTContext::import_iterator I = Context->local_import_begin(),
2206 IEnd = Context->local_import_end();
2207 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002208 if (Module *ImportedFrom
2209 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2210 SrcMgr))) {
2211 ImportedFrom->Imports.push_back(I->getImportedModule());
2212 }
2213 }
2214
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002215 // Enter the submodule description block.
2216 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2217
2218 // Write the abbreviations needed for the submodules block.
2219 using namespace llvm;
2220 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2221 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2232 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2233
2234 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002235 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2237 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2238
2239 Abbrev = new BitCodeAbbrev();
2240 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2242 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002243
2244 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002245 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2247 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2248
2249 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002250 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2252 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2253
Douglas Gregor51f564f2011-12-31 04:05:44 +00002254 Abbrev = new BitCodeAbbrev();
2255 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2257 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2258
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002259 Abbrev = new BitCodeAbbrev();
2260 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2262 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2263
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002264 Abbrev = new BitCodeAbbrev();
2265 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2268 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2269
Douglas Gregor63a72682013-03-20 00:22:05 +00002270 Abbrev = new BitCodeAbbrev();
2271 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2273 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2274
Douglas Gregor906d66a2013-03-20 21:10:35 +00002275 Abbrev = new BitCodeAbbrev();
2276 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2279 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2280
Douglas Gregor26ced122011-12-01 00:59:36 +00002281 // Write the submodule metadata block.
2282 RecordData Record;
2283 Record.push_back(getNumberOfModules(WritingModule));
2284 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2285 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2286
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002287 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002288 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002289 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002290 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002291 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002292 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002293 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002294
2295 // Emit the definition of the block.
2296 Record.clear();
2297 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002298 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002299 if (Mod->Parent) {
2300 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2301 Record.push_back(SubmoduleIDs[Mod->Parent]);
2302 } else {
2303 Record.push_back(0);
2304 }
2305 Record.push_back(Mod->IsFramework);
2306 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002307 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002308 Record.push_back(Mod->InferSubmodules);
2309 Record.push_back(Mod->InferExplicitSubmodules);
2310 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002311 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002312 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2313
Douglas Gregor51f564f2011-12-31 04:05:44 +00002314 // Emit the requirements.
2315 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2316 Record.clear();
2317 Record.push_back(SUBMODULE_REQUIRES);
2318 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2319 Mod->Requires[I].data(),
2320 Mod->Requires[I].size());
2321 }
2322
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002323 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002324 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002325 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002326 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002327 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002328 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002329 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2330 Record.clear();
2331 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2332 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2333 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002334 }
2335
2336 // Emit the headers.
2337 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2338 Record.clear();
2339 Record.push_back(SUBMODULE_HEADER);
2340 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2341 Mod->Headers[I]->getName());
2342 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002343 // Emit the excluded headers.
2344 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2345 Record.clear();
2346 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2347 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2348 Mod->ExcludedHeaders[I]->getName());
2349 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002350 ArrayRef<const FileEntry *>
2351 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2352 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002353 Record.clear();
2354 Record.push_back(SUBMODULE_TOPHEADER);
2355 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002356 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002357 }
Douglas Gregor55988682011-12-05 16:33:54 +00002358
2359 // Emit the imports.
2360 if (!Mod->Imports.empty()) {
2361 Record.clear();
2362 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002363 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002364 assert(ImportedID && "Unknown submodule!");
2365 Record.push_back(ImportedID);
2366 }
2367 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2368 }
2369
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002370 // Emit the exports.
2371 if (!Mod->Exports.empty()) {
2372 Record.clear();
2373 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002374 if (Module *Exported = Mod->Exports[I].getPointer()) {
2375 unsigned ExportedID = SubmoduleIDs[Exported];
2376 assert(ExportedID > 0 && "Unknown submodule ID?");
2377 Record.push_back(ExportedID);
2378 } else {
2379 Record.push_back(0);
2380 }
2381
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002382 Record.push_back(Mod->Exports[I].getInt());
2383 }
2384 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2385 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002386
2387 // Emit the link libraries.
2388 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2389 Record.clear();
2390 Record.push_back(SUBMODULE_LINK_LIBRARY);
2391 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2392 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2393 Mod->LinkLibraries[I].Library);
2394 }
2395
Douglas Gregor906d66a2013-03-20 21:10:35 +00002396 // Emit the conflicts.
2397 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2398 Record.clear();
2399 Record.push_back(SUBMODULE_CONFLICT);
2400 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2401 assert(OtherID && "Unknown submodule!");
2402 Record.push_back(OtherID);
2403 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2404 Mod->Conflicts[I].Message);
2405 }
2406
Douglas Gregor63a72682013-03-20 00:22:05 +00002407 // Emit the configuration macros.
2408 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2409 Record.clear();
2410 Record.push_back(SUBMODULE_CONFIG_MACRO);
2411 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2412 Mod->ConfigMacros[I]);
2413 }
2414
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002415 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002416 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2417 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002418 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002419 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002420 }
2421
2422 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002423
2424 assert((NextSubmoduleID - FirstSubmoduleID
2425 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002426}
2427
Douglas Gregor185dbd72011-12-01 02:07:58 +00002428serialization::SubmoduleID
2429ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002430 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002431 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002432
2433 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002434 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002435 Module *OwningMod
2436 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002437 if (!OwningMod)
2438 return 0;
2439
Douglas Gregore209e502011-12-06 01:10:29 +00002440 // Check whether this submodule is part of our own module.
2441 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002442 return 0;
2443
Douglas Gregore209e502011-12-06 01:10:29 +00002444 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002445}
2446
David Blaikied6471f72011-09-25 23:23:43 +00002447void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002448 // FIXME: Make it work properly with modules.
2449 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2450 DiagStateIDMap;
2451 unsigned CurrID = 0;
2452 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002453 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002454 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002455 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2456 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002457 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002458 if (point.Loc.isInvalid())
2459 continue;
2460
2461 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002462 unsigned &DiagStateID = DiagStateIDMap[point.State];
2463 Record.push_back(DiagStateID);
2464
2465 if (DiagStateID == 0) {
2466 DiagStateID = ++CurrID;
2467 for (DiagnosticsEngine::DiagState::const_iterator
2468 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2469 if (I->second.isPragma()) {
2470 Record.push_back(I->first);
2471 Record.push_back(I->second.getMapping());
2472 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002473 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002474 Record.push_back(-1); // mark the end of the diag/map pairs for this
2475 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002476 }
2477 }
2478
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002479 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002480 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002481}
2482
Anders Carlssonc8505782011-03-06 18:41:18 +00002483void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2484 if (CXXBaseSpecifiersOffsets.empty())
2485 return;
2486
2487 RecordData Record;
2488
2489 // Create a blob abbreviation for the C++ base specifiers offsets.
2490 using namespace llvm;
2491
2492 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2493 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2496 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2497
Douglas Gregore92b8a12011-08-04 00:01:48 +00002498 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002499 Record.clear();
2500 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2501 Record.push_back(CXXBaseSpecifiersOffsets.size());
2502 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002503 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002504}
2505
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002506//===----------------------------------------------------------------------===//
2507// Type Serialization
2508//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002509
Sebastian Redl3397c552010-08-18 23:56:27 +00002510/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002511void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002512 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002513 if (Idx.getIndex() == 0) // we haven't seen this type before.
2514 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002515
Douglas Gregor97475832010-10-05 18:37:06 +00002516 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002517
Douglas Gregor2cf26342009-04-09 22:27:44 +00002518 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002519 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002520 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002521 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002522 else if (TypeOffsets.size() < Index) {
2523 TypeOffsets.resize(Index + 1);
2524 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002525 }
2526
2527 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002528
Douglas Gregor2cf26342009-04-09 22:27:44 +00002529 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002530 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002531
Douglas Gregora4923eb2009-11-16 21:35:15 +00002532 if (T.hasLocalNonFastQualifiers()) {
2533 Qualifiers Qs = T.getLocalQualifiers();
2534 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002535 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002536 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002537 } else {
2538 switch (T->getTypeClass()) {
2539 // For all of the concrete, non-dependent types, call the
2540 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002541#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002542 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002543#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002544#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002545 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002546 }
2547
2548 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002549 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002550
2551 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002552 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002553}
2554
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002555//===----------------------------------------------------------------------===//
2556// Declaration Serialization
2557//===----------------------------------------------------------------------===//
2558
Douglas Gregor2cf26342009-04-09 22:27:44 +00002559/// \brief Write the block containing all of the declaration IDs
2560/// lexically declared within the given DeclContext.
2561///
2562/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2563/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002564uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002565 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002566 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002567 return 0;
2568
Douglas Gregorc9490c02009-04-16 22:23:12 +00002569 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002570 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002571 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002572 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002573 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2574 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002575 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002576
Douglas Gregor25123082009-04-22 22:34:57 +00002577 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002578 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002579 return Offset;
2580}
2581
Sebastian Redla4232eb2010-08-18 23:56:21 +00002582void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002583 using namespace llvm;
2584 RecordData Record;
2585
2586 // Write the type offsets array
2587 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002588 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2592 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2593 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002594 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002595 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002596 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002597 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002598
2599 // Write the declaration offsets array
2600 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002601 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2605 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2606 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002607 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002608 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002609 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002610 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002611}
2612
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002613void ASTWriter::WriteFileDeclIDsMap() {
2614 using namespace llvm;
2615 RecordData Record;
2616
2617 // Join the vectors of DeclIDs from all files.
2618 SmallVector<DeclID, 256> FileSortedIDs;
2619 for (FileDeclIDsTy::iterator
2620 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2621 DeclIDInFileInfo &Info = *FI->second;
2622 Info.FirstDeclIndex = FileSortedIDs.size();
2623 for (LocDeclIDsTy::iterator
2624 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2625 FileSortedIDs.push_back(DI->second);
2626 }
2627
2628 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2629 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2632 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2633 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002634 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002635 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2636}
2637
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002638void ASTWriter::WriteComments() {
2639 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002640 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002641 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002642 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2643 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002644 I != E; ++I) {
2645 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002646 AddSourceRange((*I)->getSourceRange(), Record);
2647 Record.push_back((*I)->getKind());
2648 Record.push_back((*I)->isTrailingComment());
2649 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002650 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2651 }
2652 Stream.ExitBlock();
2653}
2654
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002655//===----------------------------------------------------------------------===//
2656// Global Method Pool and Selector Serialization
2657//===----------------------------------------------------------------------===//
2658
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002659namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002660// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002661class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002662 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002663
2664public:
2665 typedef Selector key_type;
2666 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002667
Sebastian Redl5d050072010-08-04 17:20:04 +00002668 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002669 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002670 ObjCMethodList Instance, Factory;
2671 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002672 typedef const data_type& data_type_ref;
2673
Sebastian Redl3397c552010-08-18 23:56:27 +00002674 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002675
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002676 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002677 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002678 }
Mike Stump1eb44332009-09-09 15:08:12 +00002679
2680 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002681 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002682 data_type_ref Methods) {
2683 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2684 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002685 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2686 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002687 Method = Method->Next)
2688 if (Method->Method)
2689 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002690 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002691 Method = Method->Next)
2692 if (Method->Method)
2693 DataLen += 4;
2694 clang::io::Emit16(Out, DataLen);
2695 return std::make_pair(KeyLen, DataLen);
2696 }
Mike Stump1eb44332009-09-09 15:08:12 +00002697
Chris Lattner5f9e2722011-07-23 10:55:15 +00002698 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002699 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002700 assert((Start >> 32) == 0 && "Selector key offset too large");
2701 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002702 unsigned N = Sel.getNumArgs();
2703 clang::io::Emit16(Out, N);
2704 if (N == 0)
2705 N = 1;
2706 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002707 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002708 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2709 }
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Chris Lattner5f9e2722011-07-23 10:55:15 +00002711 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002712 data_type_ref Methods, unsigned DataLen) {
2713 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002714 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002715 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002716 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002717 Method = Method->Next)
2718 if (Method->Method)
2719 ++NumInstanceMethods;
2720
2721 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002722 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002723 Method = Method->Next)
2724 if (Method->Method)
2725 ++NumFactoryMethods;
2726
2727 clang::io::Emit16(Out, NumInstanceMethods);
2728 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002729 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002730 Method = Method->Next)
2731 if (Method->Method)
2732 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002733 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002734 Method = Method->Next)
2735 if (Method->Method)
2736 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002737
2738 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002739 }
2740};
2741} // end anonymous namespace
2742
Sebastian Redl059612d2010-08-03 21:58:15 +00002743/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002744///
2745/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002746/// in an on-disk hash table indexed by the selector. The hash table also
2747/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002748void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002749 using namespace llvm;
2750
Sebastian Redl059612d2010-08-03 21:58:15 +00002751 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002752 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002753 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002754 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002755 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002756 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002757 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002758 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002759
Sebastian Redl059612d2010-08-03 21:58:15 +00002760 // Create the on-disk hash table representation. We walk through every
2761 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002762 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002763 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002764 I = SelectorIDs.begin(), E = SelectorIDs.end();
2765 I != E; ++I) {
2766 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002767 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002768 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002769 I->second,
2770 ObjCMethodList(),
2771 ObjCMethodList()
2772 };
2773 if (F != SemaRef.MethodPool.end()) {
2774 Data.Instance = F->second.first;
2775 Data.Factory = F->second.second;
2776 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002777 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002778 // changed.
2779 if (Chain && I->second < FirstSelectorID) {
2780 // Selector already exists. Did it change?
2781 bool changed = false;
2782 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2783 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002784 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002785 changed = true;
2786 }
2787 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2788 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002789 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002790 changed = true;
2791 }
2792 if (!changed)
2793 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002794 } else if (Data.Instance.Method || Data.Factory.Method) {
2795 // A new method pool entry.
2796 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002797 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002798 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002799 }
2800
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002801 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002802 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002803 uint32_t BucketOffset;
2804 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002805 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002806 llvm::raw_svector_ostream Out(MethodPool);
2807 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002808 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002809 BucketOffset = Generator.Emit(Out, Trait);
2810 }
2811
2812 // Create a blob abbreviation
2813 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002814 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2818 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2819
Douglas Gregor83941df2009-04-25 17:48:32 +00002820 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002821 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002822 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002823 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002824 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002825 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002826
2827 // Create a blob abbreviation for the selector table offsets.
2828 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002829 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2833 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2834
2835 // Write the selector offsets table.
2836 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002837 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002838 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002839 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002840 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002841 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002842 }
2843}
2844
Sebastian Redl3397c552010-08-18 23:56:27 +00002845/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002846void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002847 using namespace llvm;
2848 if (SemaRef.ReferencedSelectors.empty())
2849 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002850
Fariborz Jahanian32019832010-07-23 19:11:11 +00002851 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002852
Sebastian Redl3397c552010-08-18 23:56:27 +00002853 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002854 // very tricky to fix, and given that @selector shouldn't really appear in
2855 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002856 for (DenseMap<Selector, SourceLocation>::iterator S =
2857 SemaRef.ReferencedSelectors.begin(),
2858 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2859 Selector Sel = (*S).first;
2860 SourceLocation Loc = (*S).second;
2861 AddSelectorRef(Sel, Record);
2862 AddSourceLocation(Loc, Record);
2863 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002864 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002865}
2866
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002867//===----------------------------------------------------------------------===//
2868// Identifier Table Serialization
2869//===----------------------------------------------------------------------===//
2870
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002871namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002872class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002873 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002874 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002875 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002876 bool IsModule;
2877
Douglas Gregora92193e2009-04-28 21:18:29 +00002878 /// \brief Determines whether this is an "interesting" identifier
2879 /// that needs a full IdentifierInfo structure written into the hash
2880 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002881 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002882 if (II->isPoisoned() ||
2883 II->isExtensionToken() ||
2884 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002885 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002886 II->getFETokenInfo<void>())
2887 return true;
2888
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002889 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002890 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002891
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002892 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002893 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002894 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002895
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002896 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2897 if (!IsModule)
2898 return !shouldIgnoreMacro(Macro, IsModule, PP);
2899 SubmoduleID ModID;
2900 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2901 return true;
2902 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002903
2904 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002905 }
2906
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002907 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2908 SubmoduleID &ModID) {
2909 ModID = 0;
2910 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2911 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2912 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002913 return 0;
2914 }
2915
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002916 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2917 SubmoduleID &ModID) {
2918 if (DefMacroDirective *
2919 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2920 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2921 return DefMD;
2922 return 0;
2923 }
2924
2925 /// \brief Traverses the macro directives history and returns the latest
2926 /// macro that is public and not undefined in the same submodule.
2927 /// A macro that is defined in submodule A and undefined in submodule B,
2928 /// will still be considered as defined/exported from submodule A.
2929 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2930 SubmoduleID &ModID) {
2931 if (!MD)
2932 return 0;
2933
2934 bool isUndefined = false;
2935 Optional<bool> isPublic;
2936 for (; MD; MD = MD->getPrevious()) {
2937 if (MD->isHidden())
2938 continue;
2939
2940 SubmoduleID ThisModID = getSubmoduleID(MD);
2941 if (ThisModID == 0) {
2942 isUndefined = false;
2943 isPublic = Optional<bool>();
2944 continue;
2945 }
2946 if (ThisModID != ModID){
2947 ModID = ThisModID;
2948 isUndefined = false;
2949 isPublic = Optional<bool>();
2950 }
2951
2952 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2953 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
2954 return DefMD;
2955 continue;
2956 }
2957
2958 if (isa<UndefMacroDirective>(MD)) {
2959 isUndefined = true;
2960 continue;
2961 }
2962
2963 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
2964 if (!isPublic.hasValue())
2965 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002966 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002967
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002968 return 0;
2969 }
2970
2971 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002972 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2973 MacroInfo *MI = DefMD->getInfo();
2974 if (unsigned ID = MI->getOwningModuleID())
2975 return ID;
2976 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
2977 }
2978 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002979 }
2980
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002981public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002982 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002983 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002984
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002985 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002986 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002987
Douglas Gregoreee242f2011-10-27 09:33:13 +00002988 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2989 IdentifierResolver &IdResolver, bool IsModule)
2990 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002991
2992 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002993 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002994 }
Mike Stump1eb44332009-09-09 15:08:12 +00002995
2996 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002997 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002998 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002999 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003000 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003001 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003002 DataLen += 2; // 2 bytes for builtin ID
3003 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003004 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003005 DataLen += 4; // MacroDirectives offset.
3006 if (IsModule) {
3007 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003008 for (DefMacroDirective *
3009 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3010 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003011 DataLen += 4; // MacroInfo ID.
3012 }
3013 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003014 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003015 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003016
Douglas Gregoreee242f2011-10-27 09:33:13 +00003017 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3018 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003019 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003020 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003021 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003022 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003023 // We emit the key length after the data length so that every
3024 // string is preceded by a 16-bit length. This matches the PTH
3025 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003026 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003027 return std::make_pair(KeyLen, DataLen);
3028 }
Mike Stump1eb44332009-09-09 15:08:12 +00003029
Chris Lattner5f9e2722011-07-23 10:55:15 +00003030 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003031 unsigned KeyLen) {
3032 // Record the location of the key data. This is used when generating
3033 // the mapping from persistent IDs to strings.
3034 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003035 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003036 }
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Douglas Gregor7143aab2011-09-01 17:04:32 +00003038 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003039 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003040 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003041 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003042 clang::io::Emit32(Out, ID << 1);
3043 return;
3044 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003045
Douglas Gregora92193e2009-04-28 21:18:29 +00003046 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003047 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3048 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3049 clang::io::Emit16(Out, Bits);
3050 Bits = 0;
3051 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003052 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003053 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003054 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3055 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003056 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003057 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003058 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003059
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003060 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003061 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3062 if (IsModule) {
3063 // Write the IDs of macros coming from different submodules.
3064 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003065 for (DefMacroDirective *
3066 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3067 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3068 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003069 assert(InfoID);
3070 clang::io::Emit32(Out, InfoID);
3071 }
3072 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003073 }
Douglas Gregor13292642011-12-02 15:45:10 +00003074 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003075
Douglas Gregor668c1a42009-04-21 22:25:48 +00003076 // Emit the declaration IDs in reverse order, because the
3077 // IdentifierResolver provides the declarations as they would be
3078 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003079 // "stat"), but the ASTReader adds declarations to the end of the list
3080 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003081 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003082 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3083 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00003084 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003085 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003086 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003087 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003088 }
3089};
3090} // end anonymous namespace
3091
Sebastian Redl3397c552010-08-18 23:56:27 +00003092/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003093///
3094/// The identifier table consists of a blob containing string data
3095/// (the actual identifiers themselves) and a separate "offsets" index
3096/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003097void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3098 IdentifierResolver &IdResolver,
3099 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003100 using namespace llvm;
3101
3102 // Create and write out the blob that contains the identifier
3103 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003104 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003105 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003106 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003107
Douglas Gregor92b059e2009-04-28 20:33:11 +00003108 // Look for any identifiers that were named while processing the
3109 // headers, but are otherwise not needed. We add these to the hash
3110 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003111 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003112 // file.
3113 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3114 IDEnd = PP.getIdentifierTable().end();
3115 ID != IDEnd; ++ID)
3116 getIdentifierRef(ID->second);
3117
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003118 // Create the on-disk hash table representation. We only store offsets
3119 // for identifiers that appear here for the first time.
3120 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003121 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003122 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3123 ID != IDEnd; ++ID) {
3124 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003125 if (!Chain || !ID->first->isFromAST() ||
3126 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003127 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003128 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003129 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003130
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003131 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003132 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003133 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003134 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003135 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003136 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003137 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003138 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003139 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003140 }
3141
3142 // Create a blob abbreviation
3143 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003144 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003147 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003148
3149 // Write the identifier table
3150 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003151 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003152 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003153 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003154 }
3155
3156 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003157 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003158 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3162 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3163
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003164#ifndef NDEBUG
3165 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3166 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3167#endif
3168
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003169 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003170 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003171 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003172 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003173 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003174 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003175}
3176
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003177//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003178// DeclContext's Name Lookup Table Serialization
3179//===----------------------------------------------------------------------===//
3180
3181namespace {
3182// Trait used for the on-disk hash table used in the method pool.
3183class ASTDeclContextNameLookupTrait {
3184 ASTWriter &Writer;
3185
3186public:
3187 typedef DeclarationName key_type;
3188 typedef key_type key_type_ref;
3189
3190 typedef DeclContext::lookup_result data_type;
3191 typedef const data_type& data_type_ref;
3192
3193 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3194
3195 unsigned ComputeHash(DeclarationName Name) {
3196 llvm::FoldingSetNodeID ID;
3197 ID.AddInteger(Name.getNameKind());
3198
3199 switch (Name.getNameKind()) {
3200 case DeclarationName::Identifier:
3201 ID.AddString(Name.getAsIdentifierInfo()->getName());
3202 break;
3203 case DeclarationName::ObjCZeroArgSelector:
3204 case DeclarationName::ObjCOneArgSelector:
3205 case DeclarationName::ObjCMultiArgSelector:
3206 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3207 break;
3208 case DeclarationName::CXXConstructorName:
3209 case DeclarationName::CXXDestructorName:
3210 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003211 break;
3212 case DeclarationName::CXXOperatorName:
3213 ID.AddInteger(Name.getCXXOverloadedOperator());
3214 break;
3215 case DeclarationName::CXXLiteralOperatorName:
3216 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3217 case DeclarationName::CXXUsingDirective:
3218 break;
3219 }
3220
3221 return ID.ComputeHash();
3222 }
3223
3224 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003225 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003226 data_type_ref Lookup) {
3227 unsigned KeyLen = 1;
3228 switch (Name.getNameKind()) {
3229 case DeclarationName::Identifier:
3230 case DeclarationName::ObjCZeroArgSelector:
3231 case DeclarationName::ObjCOneArgSelector:
3232 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003233 case DeclarationName::CXXLiteralOperatorName:
3234 KeyLen += 4;
3235 break;
3236 case DeclarationName::CXXOperatorName:
3237 KeyLen += 1;
3238 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003239 case DeclarationName::CXXConstructorName:
3240 case DeclarationName::CXXDestructorName:
3241 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003242 case DeclarationName::CXXUsingDirective:
3243 break;
3244 }
3245 clang::io::Emit16(Out, KeyLen);
3246
3247 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003248 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003249 clang::io::Emit16(Out, DataLen);
3250
3251 return std::make_pair(KeyLen, DataLen);
3252 }
3253
Chris Lattner5f9e2722011-07-23 10:55:15 +00003254 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003255 using namespace clang::io;
3256
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003257 Emit8(Out, Name.getNameKind());
3258 switch (Name.getNameKind()) {
3259 case DeclarationName::Identifier:
3260 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003261 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003262 case DeclarationName::ObjCZeroArgSelector:
3263 case DeclarationName::ObjCOneArgSelector:
3264 case DeclarationName::ObjCMultiArgSelector:
3265 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003266 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003267 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003268 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3269 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003270 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003271 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003272 case DeclarationName::CXXLiteralOperatorName:
3273 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003274 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003275 case DeclarationName::CXXConstructorName:
3276 case DeclarationName::CXXDestructorName:
3277 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003278 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003279 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003280 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003281
3282 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003283 }
3284
Chris Lattner5f9e2722011-07-23 10:55:15 +00003285 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003286 data_type Lookup, unsigned DataLen) {
3287 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003288 clang::io::Emit16(Out, Lookup.size());
3289 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3290 I != E; ++I)
3291 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003292
3293 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3294 }
3295};
3296} // end anonymous namespace
3297
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003298/// \brief Write the block containing all of the declaration IDs
3299/// visible from the given DeclContext.
3300///
3301/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003302/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003303uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3304 DeclContext *DC) {
3305 if (DC->getPrimaryContext() != DC)
3306 return 0;
3307
3308 // Since there is no name lookup into functions or methods, don't bother to
3309 // build a visible-declarations table for these entities.
3310 if (DC->isFunctionOrMethod())
3311 return 0;
3312
3313 // If not in C++, we perform name lookup for the translation unit via the
3314 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3315 // FIXME: In C++ we need the visible declarations in order to "see" the
3316 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00003317 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003318 return 0;
3319
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003320 // Serialize the contents of the mapping used for lookup. Note that,
3321 // although we have two very different code paths, the serialized
3322 // representation is the same for both cases: a declaration name,
3323 // followed by a size, followed by references to the visible
3324 // declarations that have that name.
3325 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003326 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003327 if (!Map || Map->empty())
3328 return 0;
3329
3330 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3331 ASTDeclContextNameLookupTrait Trait(*this);
3332
3333 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003334 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003335 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003336 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3337 D != DEnd; ++D) {
3338 DeclarationName Name = D->first;
3339 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003340 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003341 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3342 // Hash all conversion function names to the same name. The actual
3343 // type information in conversion function name is not used in the
3344 // key (since such type information is not stable across different
3345 // modules), so the intended effect is to coalesce all of the conversion
3346 // functions under a single key.
3347 if (!ConversionName)
3348 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003349 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003350 continue;
3351 }
3352
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003353 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003354 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003355 }
3356
Douglas Gregore5a54b62011-08-30 20:49:19 +00003357 // Add the conversion functions
3358 if (!ConversionDecls.empty()) {
3359 Generator.insert(ConversionName,
3360 DeclContext::lookup_result(ConversionDecls.begin(),
3361 ConversionDecls.end()),
3362 Trait);
3363 }
3364
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003365 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003366 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003367 uint32_t BucketOffset;
3368 {
3369 llvm::raw_svector_ostream Out(LookupTable);
3370 // Make sure that no bucket is at offset 0
3371 clang::io::Emit32(Out, 0);
3372 BucketOffset = Generator.Emit(Out, Trait);
3373 }
3374
3375 // Write the lookup table
3376 RecordData Record;
3377 Record.push_back(DECL_CONTEXT_VISIBLE);
3378 Record.push_back(BucketOffset);
3379 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3380 LookupTable.str());
3381
3382 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3383 ++NumVisibleDeclContexts;
3384 return Offset;
3385}
3386
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003387/// \brief Write an UPDATE_VISIBLE block for the given context.
3388///
3389/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3390/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003391/// (in C++), for namespaces, and for classes with forward-declared unscoped
3392/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003393void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003394 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3395 if (!Map || Map->empty())
3396 return;
3397
3398 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3399 ASTDeclContextNameLookupTrait Trait(*this);
3400
3401 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003402 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3403 D != DEnd; ++D) {
3404 DeclarationName Name = D->first;
3405 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003406 // For any name that appears in this table, the results are complete, i.e.
3407 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003408 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003409 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003410 }
3411
3412 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003413 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003414 uint32_t BucketOffset;
3415 {
3416 llvm::raw_svector_ostream Out(LookupTable);
3417 // Make sure that no bucket is at offset 0
3418 clang::io::Emit32(Out, 0);
3419 BucketOffset = Generator.Emit(Out, Trait);
3420 }
3421
3422 // Write the lookup table
3423 RecordData Record;
3424 Record.push_back(UPDATE_VISIBLE);
3425 Record.push_back(getDeclID(cast<Decl>(DC)));
3426 Record.push_back(BucketOffset);
3427 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3428}
3429
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003430/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3431void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3432 RecordData Record;
3433 Record.push_back(Opts.fp_contract);
3434 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3435}
3436
3437/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3438void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003439 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003440 return;
3441
3442 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3443 RecordData Record;
3444#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3445#include "clang/Basic/OpenCLExtensions.def"
3446 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3447}
3448
Douglas Gregor2171bf12012-01-15 16:58:34 +00003449void ASTWriter::WriteRedeclarations() {
3450 RecordData LocalRedeclChains;
3451 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3452
3453 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3454 Decl *First = Redeclarations[I];
3455 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3456
3457 Decl *MostRecent = First->getMostRecentDecl();
3458
3459 // If we only have a single declaration, there is no point in storing
3460 // a redeclaration chain.
3461 if (First == MostRecent)
3462 continue;
3463
3464 unsigned Offset = LocalRedeclChains.size();
3465 unsigned Size = 0;
3466 LocalRedeclChains.push_back(0); // Placeholder for the size.
3467
3468 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003469 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003470 Prev = Prev->getPreviousDecl()) {
3471 if (!Prev->isFromASTFile()) {
3472 AddDeclRef(Prev, LocalRedeclChains);
3473 ++Size;
3474 }
3475 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003476
3477 if (!First->isFromASTFile() && Chain) {
3478 Decl *FirstFromAST = MostRecent;
3479 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3480 if (Prev->isFromASTFile())
3481 FirstFromAST = Prev;
3482 }
3483
3484 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3485 }
3486
Douglas Gregor2171bf12012-01-15 16:58:34 +00003487 LocalRedeclChains[Offset] = Size;
3488
3489 // Reverse the set of local redeclarations, so that we store them in
3490 // order (since we found them in reverse order).
3491 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3492
Douglas Gregoraa945902013-02-18 15:53:43 +00003493 // Add the mapping from the first ID from the AST to the set of local
3494 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003495 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3496 LocalRedeclsMap.push_back(Info);
3497
3498 assert(N == Redeclarations.size() &&
3499 "Deserialized a declaration we shouldn't have");
3500 }
3501
3502 if (LocalRedeclChains.empty())
3503 return;
3504
3505 // Sort the local redeclarations map by the first declaration ID,
3506 // since the reader will be performing binary searches on this information.
3507 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3508
3509 // Emit the local redeclarations map.
3510 using namespace llvm;
3511 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3512 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3515 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3516
3517 RecordData Record;
3518 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3519 Record.push_back(LocalRedeclsMap.size());
3520 Stream.EmitRecordWithBlob(AbbrevID, Record,
3521 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3522 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3523
3524 // Emit the redeclaration chains.
3525 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3526}
3527
Douglas Gregorcff9f262012-01-27 01:47:08 +00003528void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003529 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003530 RecordData Categories;
3531
3532 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3533 unsigned Size = 0;
3534 unsigned StartIndex = Categories.size();
3535
3536 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3537
3538 // Allocate space for the size.
3539 Categories.push_back(0);
3540
3541 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003542 for (ObjCInterfaceDecl::known_categories_iterator
3543 Cat = Class->known_categories_begin(),
3544 CatEnd = Class->known_categories_end();
3545 Cat != CatEnd; ++Cat, ++Size) {
3546 assert(getDeclID(*Cat) != 0 && "Bogus category");
3547 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003548 }
3549
3550 // Update the size.
3551 Categories[StartIndex] = Size;
3552
3553 // Record this interface -> category map.
3554 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3555 CategoriesMap.push_back(CatInfo);
3556 }
3557
3558 // Sort the categories map by the definition ID, since the reader will be
3559 // performing binary searches on this information.
3560 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3561
3562 // Emit the categories map.
3563 using namespace llvm;
3564 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3565 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3568 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3569
3570 RecordData Record;
3571 Record.push_back(OBJC_CATEGORIES_MAP);
3572 Record.push_back(CategoriesMap.size());
3573 Stream.EmitRecordWithBlob(AbbrevID, Record,
3574 reinterpret_cast<char*>(CategoriesMap.data()),
3575 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3576
3577 // Emit the category lists.
3578 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3579}
3580
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003581void ASTWriter::WriteMergedDecls() {
3582 if (!Chain || Chain->MergedDecls.empty())
3583 return;
3584
3585 RecordData Record;
3586 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3587 IEnd = Chain->MergedDecls.end();
3588 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003589 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003590 : getDeclID(I->first);
3591 assert(CanonID && "Merged declaration not known?");
3592
3593 Record.push_back(CanonID);
3594 Record.push_back(I->second.size());
3595 Record.append(I->second.begin(), I->second.end());
3596 }
3597 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3598}
3599
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003600//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003601// General Serialization Routines
3602//===----------------------------------------------------------------------===//
3603
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003604/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003605void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3606 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003607 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003608 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3609 e = Attrs.end(); i != e; ++i){
3610 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003611 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003612 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003613
Sean Huntcf807c42010-08-18 23:23:40 +00003614#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003615
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003616 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003617}
3618
Chris Lattner5f9e2722011-07-23 10:55:15 +00003619void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003620 Record.push_back(Str.size());
3621 Record.insert(Record.end(), Str.begin(), Str.end());
3622}
3623
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003624void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3625 RecordDataImpl &Record) {
3626 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003627 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003628 Record.push_back(*Minor + 1);
3629 else
3630 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003631 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003632 Record.push_back(*Subminor + 1);
3633 else
3634 Record.push_back(0);
3635}
3636
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003637/// \brief Note that the identifier II occurs at the given offset
3638/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003639void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003640 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003641 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003642 // up earlier in the chain and thus don't need an offset.
3643 if (ID >= FirstIdentID)
3644 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003645}
3646
Douglas Gregor83941df2009-04-25 17:48:32 +00003647/// \brief Note that the selector Sel occurs at the given offset
3648/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003649void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003650 unsigned ID = SelectorIDs[Sel];
3651 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003652 // Don't record offsets for selectors that are also available in a different
3653 // file.
3654 if (ID < FirstSelectorID)
3655 return;
3656 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003657}
3658
Sebastian Redla4232eb2010-08-18 23:56:21 +00003659ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003660 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003661 WritingAST(false), DoneWritingDeclsAndTypes(false),
3662 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003663 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003664 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003665 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3666 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003667 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3668 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003669 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003670 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003671 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003672 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003673 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003674 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003675 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3676 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3677 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003678 DeclTypedefAbbrev(0),
3679 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3680 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003681{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003682}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003683
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003684ASTWriter::~ASTWriter() {
3685 for (FileDeclIDsTy::iterator
3686 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3687 delete I->second;
3688}
3689
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003690void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003691 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003692 Module *WritingModule, StringRef isysroot,
3693 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003694 WritingAST = true;
3695
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003696 ASTHasCompilerErrors = hasErrors;
3697
Douglas Gregor2cf26342009-04-09 22:27:44 +00003698 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003699 Stream.Emit((unsigned)'C', 8);
3700 Stream.Emit((unsigned)'P', 8);
3701 Stream.Emit((unsigned)'C', 8);
3702 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003703
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003704 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003705
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003706 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003707 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003708 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003709 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003710 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003711 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003712 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003713
3714 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003715}
3716
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003717template<typename Vector>
3718static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3719 ASTWriter::RecordData &Record) {
3720 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3721 I != E; ++I) {
3722 Writer.AddDeclRef(*I, Record);
3723 }
3724}
3725
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003726void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003727 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003728 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003729 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003730 using namespace llvm;
3731
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003732 bool isModule = WritingModule != 0;
3733
Douglas Gregorecc2c092011-12-01 22:20:10 +00003734 // Make sure that the AST reader knows to finalize itself.
3735 if (Chain)
3736 Chain->finalizeForWriting();
3737
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003738 ASTContext &Context = SemaRef.Context;
3739 Preprocessor &PP = SemaRef.PP;
3740
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003741 // Set up predefined declaration IDs.
3742 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003743 if (Context.ObjCIdDecl)
3744 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003745 if (Context.ObjCSelDecl)
3746 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003747 if (Context.ObjCClassDecl)
3748 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003749 if (Context.ObjCProtocolClassDecl)
3750 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003751 if (Context.Int128Decl)
3752 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3753 if (Context.UInt128Decl)
3754 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003755 if (Context.ObjCInstanceTypeDecl)
3756 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003757 if (Context.BuiltinVaListDecl)
3758 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3759
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003760 if (!Chain) {
3761 // Make sure that we emit IdentifierInfos (and any attached
3762 // declarations) for builtins. We don't need to do this when we're
3763 // emitting chained PCH files, because all of the builtins will be
3764 // in the original PCH file.
3765 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003766 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003767 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003768 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003769 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003770 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3771 getIdentifierRef(&Table.get(BuiltinNames[I]));
3772 }
3773
Douglas Gregoreee242f2011-10-27 09:33:13 +00003774 // If there are any out-of-date identifiers, bring them up to date.
3775 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003776 // Find out-of-date identifiers.
3777 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003778 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3779 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003780 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003781 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003782 OutOfDate.push_back(ID->second);
3783 }
3784
3785 // Update the out-of-date identifiers.
3786 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3787 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3788 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003789 }
3790
Chris Lattner63d65f82009-09-08 18:19:27 +00003791 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003792 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003793 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003794 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003795 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003796
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003797 // Build a record containing all of the file scoped decls in this file.
3798 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003799 if (!isModule)
3800 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3801 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003802
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003803 // Build a record containing all of the delegating constructors we still need
3804 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003805 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003806 if (!isModule)
3807 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003808
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003809 // Write the set of weak, undeclared identifiers. We always write the
3810 // entire table, since later PCH files in a PCH chain are only interested in
3811 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003812 RecordData WeakUndeclaredIdentifiers;
3813 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003814 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003815 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3816 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3817 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3818 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3819 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3820 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3821 }
3822 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003823
Richard Smith5ea6ef42013-01-10 23:43:47 +00003824 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003825 // declarations in this header file. Generally, this record will be
3826 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003827 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003828 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003829 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003830 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003831 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3832 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003833 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003834 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003835 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003836 }
3837
Douglas Gregorb81c1702009-04-27 20:06:05 +00003838 // Build a record containing all of the ext_vector declarations.
3839 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003840 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003841
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003842 // Build a record containing all of the VTable uses information.
3843 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003844 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003845 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3846 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3847 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3848 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3849 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003850 }
3851
3852 // Build a record containing all of dynamic classes declarations.
3853 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003854 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003855
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003856 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003857 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003858 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003859 I = SemaRef.PendingInstantiations.begin(),
3860 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3861 AddDeclRef(I->first, PendingInstantiations);
3862 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003863 }
3864 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3865 "There are local ones at end of translation unit!");
3866
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003867 // Build a record containing some declaration references.
3868 RecordData SemaDeclRefs;
3869 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3870 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3871 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3872 }
3873
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003874 RecordData CUDASpecialDeclRefs;
3875 if (Context.getcudaConfigureCallDecl()) {
3876 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3877 }
3878
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003879 // Build a record containing all of the known namespaces.
3880 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003881 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003882 I = SemaRef.KnownNamespaces.begin(),
3883 IEnd = SemaRef.KnownNamespaces.end();
3884 I != IEnd; ++I) {
3885 if (!I->second)
3886 AddDeclRef(I->first, KnownNamespaces);
3887 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003888
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003889 // Build a record of all used, undefined objects that require definitions.
3890 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003891
3892 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003893 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003894 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3895 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003896 AddDeclRef(I->first, UndefinedButUsed);
3897 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003898 }
3899
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003900 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003901 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003902
Sebastian Redl3397c552010-08-18 23:56:27 +00003903 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003904 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003905 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003906
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003907 // This is so that older clang versions, before the introduction
3908 // of the control block, can read and reject the newer PCH format.
3909 Record.clear();
3910 Record.push_back(VERSION_MAJOR);
3911 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3912
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003913 // Create a lexical update block containing all of the declarations in the
3914 // translation unit that do not come from other AST files.
3915 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3916 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3917 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3918 E = TU->noload_decls_end();
3919 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003920 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003921 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003922 }
3923
3924 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3925 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3926 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3927 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3928 Record.clear();
3929 Record.push_back(TU_UPDATE_LEXICAL);
3930 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3931 data(NewGlobalDecls));
3932
3933 // And a visible updates block for the translation unit.
3934 Abv = new llvm::BitCodeAbbrev();
3935 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3936 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3937 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3938 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3939 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3940 WriteDeclContextVisibleUpdate(TU);
3941
3942 // If the translation unit has an anonymous namespace, and we don't already
3943 // have an update block for it, write it as an update block.
3944 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3945 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3946 if (Record.empty()) {
3947 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003948 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003949 }
3950 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003951
3952 // Make sure visible decls, added to DeclContexts previously loaded from
3953 // an AST file, are registered for serialization.
3954 for (SmallVector<const Decl *, 16>::iterator
3955 I = UpdatingVisibleDecls.begin(),
3956 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3957 GetDeclRef(*I);
3958 }
3959
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003960 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003961 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003962
Douglas Gregora119da02011-08-02 16:26:37 +00003963 // Form the record of special types.
3964 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003965 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003966 AddTypeRef(Context.getFILEType(), SpecialTypes);
3967 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3968 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3969 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3970 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003971 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003972 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003973
Douglas Gregor366809a2009-04-26 03:49:13 +00003974 // Keep writing types and declarations until all types and
3975 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003976 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003977 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003978 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3979 E = DeclsToRewrite.end();
3980 I != E; ++I)
3981 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003982 while (!DeclTypesToEmit.empty()) {
3983 DeclOrType DOT = DeclTypesToEmit.front();
3984 DeclTypesToEmit.pop();
3985 if (DOT.isType())
3986 WriteType(DOT.getType());
3987 else
3988 WriteDecl(Context, DOT.getDecl());
3989 }
3990 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003991
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003992 DoneWritingDeclsAndTypes = true;
3993
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003994 WriteFileDeclIDsMap();
3995 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003996 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003997
3998 if (Chain) {
3999 // Write the mapping information describing our module dependencies and how
4000 // each of those modules were mapped into our own offset/ID space, so that
4001 // the reader can build the appropriate mapping to its own offset/ID space.
4002 // The map consists solely of a blob with the following format:
4003 // *(module-name-len:i16 module-name:len*i8
4004 // source-location-offset:i32
4005 // identifier-id:i32
4006 // preprocessed-entity-id:i32
4007 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004008 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004009 // selector-id:i32
4010 // declaration-id:i32
4011 // c++-base-specifiers-id:i32
4012 // type-id:i32)
4013 //
4014 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4015 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4017 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004018 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004019 {
4020 llvm::raw_svector_ostream Out(Buffer);
4021 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004022 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004023 M != MEnd; ++M) {
4024 StringRef FileName = (*M)->FileName;
4025 io::Emit16(Out, FileName.size());
4026 Out.write(FileName.data(), FileName.size());
4027 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4028 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004029 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004030 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004031 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004032 io::Emit32(Out, (*M)->BaseSelectorID);
4033 io::Emit32(Out, (*M)->BaseDeclID);
4034 io::Emit32(Out, (*M)->BaseTypeIndex);
4035 }
4036 }
4037 Record.clear();
4038 Record.push_back(MODULE_OFFSET_MAP);
4039 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4040 Buffer.data(), Buffer.size());
4041 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004042 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004043 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004044 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004045 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004046 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004047 WriteFPPragmaOptions(SemaRef.getFPOptions());
4048 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004049
Sebastian Redl1476ed42010-07-16 16:36:56 +00004050 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00004051 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00004052
Anders Carlssonc8505782011-03-06 18:41:18 +00004053 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004054
Douglas Gregore209e502011-12-06 01:10:29 +00004055 // If we're emitting a module, write out the submodule information.
4056 if (WritingModule)
4057 WriteSubmodules(WritingModule);
4058
Douglas Gregora119da02011-08-02 16:26:37 +00004059 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4060
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004061 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004062 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004063 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004064
4065 // Write the record containing tentative definitions.
4066 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004067 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004068
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004069 // Write the record containing unused file scoped decls.
4070 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004071 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004072
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004073 // Write the record containing weak undeclared identifiers.
4074 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004075 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004076 WeakUndeclaredIdentifiers);
4077
Richard Smith5ea6ef42013-01-10 23:43:47 +00004078 // Write the record containing locally-scoped extern "C" definitions.
4079 if (!LocallyScopedExternCDecls.empty())
4080 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4081 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004082
4083 // Write the record containing ext_vector type names.
4084 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004085 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004086
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004087 // Write the record containing VTable uses information.
4088 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004089 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004090
4091 // Write the record containing dynamic classes declarations.
4092 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004093 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004094
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004095 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004096 if (!PendingInstantiations.empty())
4097 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004098
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004099 // Write the record containing declaration references of Sema.
4100 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004101 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004102
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004103 // Write the record containing CUDA-specific declaration references.
4104 if (!CUDASpecialDeclRefs.empty())
4105 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004106
4107 // Write the delegating constructors.
4108 if (!DelegatingCtorDecls.empty())
4109 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004110
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004111 // Write the known namespaces.
4112 if (!KnownNamespaces.empty())
4113 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004114
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004115 // Write the undefined internal functions and variables, and inline functions.
4116 if (!UndefinedButUsed.empty())
4117 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004118
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004119 // Write the visible updates to DeclContexts.
4120 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4121 I = UpdatedDeclContexts.begin(),
4122 E = UpdatedDeclContexts.end();
4123 I != E; ++I)
4124 WriteDeclContextVisibleUpdate(*I);
4125
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004126 if (!WritingModule) {
4127 // Write the submodules that were imported, if any.
4128 RecordData ImportedModules;
4129 for (ASTContext::import_iterator I = Context.local_import_begin(),
4130 IEnd = Context.local_import_end();
4131 I != IEnd; ++I) {
4132 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4133 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4134 }
4135 if (!ImportedModules.empty()) {
4136 // Sort module IDs.
4137 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4138
4139 // Unique module IDs.
4140 ImportedModules.erase(std::unique(ImportedModules.begin(),
4141 ImportedModules.end()),
4142 ImportedModules.end());
4143
4144 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4145 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004146 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004147
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004148 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004149 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004150 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004151 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004152 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00004153
Douglas Gregor3e1af842009-04-17 22:13:46 +00004154 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004155 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004156 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004157 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004158 Record.push_back(NumLexicalDeclContexts);
4159 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004160 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004161 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004162}
4163
Douglas Gregor61c5e342011-09-17 00:05:03 +00004164/// \brief Go through the declaration update blocks and resolve declaration
4165/// pointers into declaration IDs.
4166void ASTWriter::ResolveDeclUpdatesBlocks() {
4167 for (DeclUpdateMap::iterator
4168 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4169 const Decl *D = I->first;
4170 UpdateRecord &URec = I->second;
4171
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004172 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004173 continue; // The decl will be written completely
4174
4175 unsigned Idx = 0, N = URec.size();
4176 while (Idx < N) {
4177 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004178 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4179 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4180 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4181 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4182 ++Idx;
4183 break;
4184
4185 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4186 ++Idx;
4187 break;
4188 }
4189 }
4190 }
4191}
4192
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004193void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004194 if (DeclUpdates.empty())
4195 return;
4196
4197 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004198 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004199 for (DeclUpdateMap::iterator
4200 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4201 const Decl *D = I->first;
4202 UpdateRecord &URec = I->second;
4203
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004204 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004205 continue; // The decl will be written completely,no need to store updates.
4206
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004207 uint64_t Offset = Stream.GetCurrentBitNo();
4208 Stream.EmitRecord(DECL_UPDATES, URec);
4209
4210 OffsetsRecord.push_back(GetDeclRef(D));
4211 OffsetsRecord.push_back(Offset);
4212 }
4213 Stream.ExitBlock();
4214 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4215}
4216
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004217void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004218 if (ReplacedDecls.empty())
4219 return;
4220
4221 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004222 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004223 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004224 Record.push_back(I->ID);
4225 Record.push_back(I->Offset);
4226 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004227 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004228 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004229}
4230
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004231void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004232 Record.push_back(Loc.getRawEncoding());
4233}
4234
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004235void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004236 AddSourceLocation(Range.getBegin(), Record);
4237 AddSourceLocation(Range.getEnd(), Record);
4238}
4239
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004240void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004241 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004242 const uint64_t *Words = Value.getRawData();
4243 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004244}
4245
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004246void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004247 Record.push_back(Value.isUnsigned());
4248 AddAPInt(Value, Record);
4249}
4250
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004251void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004252 AddAPInt(Value.bitcastToAPInt(), Record);
4253}
4254
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004255void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004256 Record.push_back(getIdentifierRef(II));
4257}
4258
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004259IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004260 if (II == 0)
4261 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004262
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004263 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004264 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004265 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004266 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004267}
4268
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004269MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004270 // Don't emit builtin macros like __LINE__ to the AST file unless they
4271 // have been redefined by the header (in which case they are not
4272 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004273 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004274 return 0;
4275
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004276 MacroID &ID = MacroIDs[MI];
4277 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004278 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004279 MacroInfoToEmitData Info = { Name, MI, ID };
4280 MacroInfosToEmit.push_back(Info);
4281 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004282 return ID;
4283}
4284
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004285MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4286 if (MI == 0 || MI->isBuiltinMacro())
4287 return 0;
4288
4289 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4290 return MacroIDs[MI];
4291}
4292
4293uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4294 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4295 return IdentMacroDirectivesOffsetMap[Name];
4296}
4297
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004298void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004299 Record.push_back(getSelectorRef(SelRef));
4300}
4301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004302SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004303 if (Sel.getAsOpaquePtr() == 0) {
4304 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004305 }
4306
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004307 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004308 if (SID == 0 && Chain) {
4309 // This might trigger a ReadSelector callback, which will set the ID for
4310 // this selector.
4311 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004312 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004313 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004314 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004315 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004316 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004317 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004318 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004319}
4320
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004321void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004322 AddDeclRef(Temp->getDestructor(), Record);
4323}
4324
Douglas Gregor7c789c12010-10-29 22:39:52 +00004325void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4326 CXXBaseSpecifier const *BasesEnd,
4327 RecordDataImpl &Record) {
4328 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4329 CXXBaseSpecifiersToWrite.push_back(
4330 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4331 Bases, BasesEnd));
4332 Record.push_back(NextCXXBaseSpecifiersID++);
4333}
4334
Sebastian Redla4232eb2010-08-18 23:56:21 +00004335void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004336 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004337 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004338 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004339 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004340 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004341 break;
4342 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004343 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004344 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004345 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004346 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004347 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004348 break;
4349 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004350 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004351 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004352 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004353 break;
John McCall833ca992009-10-29 08:12:44 +00004354 case TemplateArgument::Null:
4355 case TemplateArgument::Integral:
4356 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004357 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004358 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004359 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004360 break;
4361 }
4362}
4363
Sebastian Redla4232eb2010-08-18 23:56:21 +00004364void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004365 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004366 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004367
4368 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4369 bool InfoHasSameExpr
4370 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4371 Record.push_back(InfoHasSameExpr);
4372 if (InfoHasSameExpr)
4373 return; // Avoid storing the same expr twice.
4374 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004375 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4376 Record);
4377}
4378
Douglas Gregordc355712011-02-25 00:36:19 +00004379void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4380 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004381 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004382 AddTypeRef(QualType(), Record);
4383 return;
4384 }
4385
Douglas Gregordc355712011-02-25 00:36:19 +00004386 AddTypeLoc(TInfo->getTypeLoc(), Record);
4387}
4388
4389void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4390 AddTypeRef(TL.getType(), Record);
4391
John McCalla1ee0c52009-10-16 21:56:05 +00004392 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004393 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004394 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004395}
4396
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004397void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004398 Record.push_back(GetOrCreateTypeID(T));
4399}
4400
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004401TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4402 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004403 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4404}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004405
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004406TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004407 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004408 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004409}
4410
4411TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4412 if (T.isNull())
4413 return TypeIdx();
4414 assert(!T.getLocalFastQualifiers());
4415
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004416 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004417 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004418 if (DoneWritingDeclsAndTypes) {
4419 assert(0 && "New type seen after serializing all the types to emit!");
4420 return TypeIdx();
4421 }
4422
Douglas Gregor366809a2009-04-26 03:49:13 +00004423 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004424 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004425 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004426 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004427 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004428 return Idx;
4429}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004430
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004431TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004432 if (T.isNull())
4433 return TypeIdx();
4434 assert(!T.getLocalFastQualifiers());
4435
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004436 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4437 assert(I != TypeIdxs.end() && "Type not emitted!");
4438 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004439}
4440
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004441void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004442 Record.push_back(GetDeclRef(D));
4443}
4444
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004445DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004446 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4447
Douglas Gregor2cf26342009-04-09 22:27:44 +00004448 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004449 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004450 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004451
4452 // If D comes from an AST file, its declaration ID is already known and
4453 // fixed.
4454 if (D->isFromASTFile())
4455 return D->getGlobalID();
4456
Douglas Gregor97475832010-10-05 18:37:06 +00004457 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004458 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004459 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004460 if (DoneWritingDeclsAndTypes) {
4461 assert(0 && "New decl seen after serializing all the decls to emit!");
4462 return 0;
4463 }
4464
Douglas Gregor2cf26342009-04-09 22:27:44 +00004465 // We haven't seen this declaration before. Give it a new ID and
4466 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004467 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004468 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004469 }
4470
Sebastian Redl681d7232010-07-27 00:17:23 +00004471 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004472}
4473
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004474DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004475 if (D == 0)
4476 return 0;
4477
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004478 // If D comes from an AST file, its declaration ID is already known and
4479 // fixed.
4480 if (D->isFromASTFile())
4481 return D->getGlobalID();
4482
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004483 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4484 return DeclIDs[D];
4485}
4486
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004487static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4488 std::pair<unsigned, serialization::DeclID> R) {
4489 return L.first < R.first;
4490}
4491
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004492void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004493 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004494 assert(D);
4495
4496 SourceLocation Loc = D->getLocation();
4497 if (Loc.isInvalid())
4498 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004499
4500 // We only keep track of the file-level declarations of each file.
4501 if (!D->getLexicalDeclContext()->isFileContext())
4502 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004503 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4504 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004505 if (isa<ParmVarDecl>(D))
4506 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004507
4508 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004509 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004510 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004511 FileID FID;
4512 unsigned Offset;
4513 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004514 if (FID.isInvalid())
4515 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004516 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004517
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004518 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004519 if (!Info)
4520 Info = new DeclIDInFileInfo();
4521
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004522 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004523 LocDeclIDsTy &Decls = Info->DeclIDs;
4524
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004525 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004526 Decls.push_back(LocDecl);
4527 return;
4528 }
4529
4530 LocDeclIDsTy::iterator
4531 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4532
4533 Decls.insert(I, LocDecl);
4534}
4535
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004536void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004537 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004538 Record.push_back(Name.getNameKind());
4539 switch (Name.getNameKind()) {
4540 case DeclarationName::Identifier:
4541 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4542 break;
4543
4544 case DeclarationName::ObjCZeroArgSelector:
4545 case DeclarationName::ObjCOneArgSelector:
4546 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004547 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004548 break;
4549
4550 case DeclarationName::CXXConstructorName:
4551 case DeclarationName::CXXDestructorName:
4552 case DeclarationName::CXXConversionFunctionName:
4553 AddTypeRef(Name.getCXXNameType(), Record);
4554 break;
4555
4556 case DeclarationName::CXXOperatorName:
4557 Record.push_back(Name.getCXXOverloadedOperator());
4558 break;
4559
Sean Hunt3e518bd2009-11-29 07:34:05 +00004560 case DeclarationName::CXXLiteralOperatorName:
4561 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4562 break;
4563
Douglas Gregor2cf26342009-04-09 22:27:44 +00004564 case DeclarationName::CXXUsingDirective:
4565 // No extra data to emit
4566 break;
4567 }
4568}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004569
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004570void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004571 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004572 switch (Name.getNameKind()) {
4573 case DeclarationName::CXXConstructorName:
4574 case DeclarationName::CXXDestructorName:
4575 case DeclarationName::CXXConversionFunctionName:
4576 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4577 break;
4578
4579 case DeclarationName::CXXOperatorName:
4580 AddSourceLocation(
4581 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4582 Record);
4583 AddSourceLocation(
4584 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4585 Record);
4586 break;
4587
4588 case DeclarationName::CXXLiteralOperatorName:
4589 AddSourceLocation(
4590 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4591 Record);
4592 break;
4593
4594 case DeclarationName::Identifier:
4595 case DeclarationName::ObjCZeroArgSelector:
4596 case DeclarationName::ObjCOneArgSelector:
4597 case DeclarationName::ObjCMultiArgSelector:
4598 case DeclarationName::CXXUsingDirective:
4599 break;
4600 }
4601}
4602
4603void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004604 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004605 AddDeclarationName(NameInfo.getName(), Record);
4606 AddSourceLocation(NameInfo.getLoc(), Record);
4607 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4608}
4609
4610void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004611 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004612 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004613 Record.push_back(Info.NumTemplParamLists);
4614 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4615 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4616}
4617
Sebastian Redla4232eb2010-08-18 23:56:21 +00004618void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004619 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004620 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004621 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004622 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004623
4624 // Push each of the NNS's onto a stack for serialization in reverse order.
4625 while (NNS) {
4626 NestedNames.push_back(NNS);
4627 NNS = NNS->getPrefix();
4628 }
4629
4630 Record.push_back(NestedNames.size());
4631 while(!NestedNames.empty()) {
4632 NNS = NestedNames.pop_back_val();
4633 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4634 Record.push_back(Kind);
4635 switch (Kind) {
4636 case NestedNameSpecifier::Identifier:
4637 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4638 break;
4639
4640 case NestedNameSpecifier::Namespace:
4641 AddDeclRef(NNS->getAsNamespace(), Record);
4642 break;
4643
Douglas Gregor14aba762011-02-24 02:36:08 +00004644 case NestedNameSpecifier::NamespaceAlias:
4645 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4646 break;
4647
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004648 case NestedNameSpecifier::TypeSpec:
4649 case NestedNameSpecifier::TypeSpecWithTemplate:
4650 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4651 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4652 break;
4653
4654 case NestedNameSpecifier::Global:
4655 // Don't need to write an associated value.
4656 break;
4657 }
4658 }
4659}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004660
Douglas Gregordc355712011-02-25 00:36:19 +00004661void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4662 RecordDataImpl &Record) {
4663 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004664 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004665 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004666
4667 // Push each of the nested-name-specifiers's onto a stack for
4668 // serialization in reverse order.
4669 while (NNS) {
4670 NestedNames.push_back(NNS);
4671 NNS = NNS.getPrefix();
4672 }
4673
4674 Record.push_back(NestedNames.size());
4675 while(!NestedNames.empty()) {
4676 NNS = NestedNames.pop_back_val();
4677 NestedNameSpecifier::SpecifierKind Kind
4678 = NNS.getNestedNameSpecifier()->getKind();
4679 Record.push_back(Kind);
4680 switch (Kind) {
4681 case NestedNameSpecifier::Identifier:
4682 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4683 AddSourceRange(NNS.getLocalSourceRange(), Record);
4684 break;
4685
4686 case NestedNameSpecifier::Namespace:
4687 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4688 AddSourceRange(NNS.getLocalSourceRange(), Record);
4689 break;
4690
4691 case NestedNameSpecifier::NamespaceAlias:
4692 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4693 AddSourceRange(NNS.getLocalSourceRange(), Record);
4694 break;
4695
4696 case NestedNameSpecifier::TypeSpec:
4697 case NestedNameSpecifier::TypeSpecWithTemplate:
4698 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4699 AddTypeLoc(NNS.getTypeLoc(), Record);
4700 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4701 break;
4702
4703 case NestedNameSpecifier::Global:
4704 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4705 break;
4706 }
4707 }
4708}
4709
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004710void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004711 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004712 Record.push_back(Kind);
4713 switch (Kind) {
4714 case TemplateName::Template:
4715 AddDeclRef(Name.getAsTemplateDecl(), Record);
4716 break;
4717
4718 case TemplateName::OverloadedTemplate: {
4719 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4720 Record.push_back(OvT->size());
4721 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4722 I != E; ++I)
4723 AddDeclRef(*I, Record);
4724 break;
4725 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004726
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004727 case TemplateName::QualifiedTemplate: {
4728 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4729 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4730 Record.push_back(QualT->hasTemplateKeyword());
4731 AddDeclRef(QualT->getTemplateDecl(), Record);
4732 break;
4733 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004734
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004735 case TemplateName::DependentTemplate: {
4736 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4737 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4738 Record.push_back(DepT->isIdentifier());
4739 if (DepT->isIdentifier())
4740 AddIdentifierRef(DepT->getIdentifier(), Record);
4741 else
4742 Record.push_back(DepT->getOperator());
4743 break;
4744 }
John McCall14606042011-06-30 08:33:18 +00004745
4746 case TemplateName::SubstTemplateTemplateParm: {
4747 SubstTemplateTemplateParmStorage *subst
4748 = Name.getAsSubstTemplateTemplateParm();
4749 AddDeclRef(subst->getParameter(), Record);
4750 AddTemplateName(subst->getReplacement(), Record);
4751 break;
4752 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004753
4754 case TemplateName::SubstTemplateTemplateParmPack: {
4755 SubstTemplateTemplateParmPackStorage *SubstPack
4756 = Name.getAsSubstTemplateTemplateParmPack();
4757 AddDeclRef(SubstPack->getParameterPack(), Record);
4758 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4759 break;
4760 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004761 }
4762}
4763
Michael J. Spencer20249a12010-10-21 03:16:25 +00004764void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004765 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004766 Record.push_back(Arg.getKind());
4767 switch (Arg.getKind()) {
4768 case TemplateArgument::Null:
4769 break;
4770 case TemplateArgument::Type:
4771 AddTypeRef(Arg.getAsType(), Record);
4772 break;
4773 case TemplateArgument::Declaration:
4774 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004775 Record.push_back(Arg.isDeclForReferenceParam());
4776 break;
4777 case TemplateArgument::NullPtr:
4778 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004779 break;
4780 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004781 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004782 AddTypeRef(Arg.getIntegralType(), Record);
4783 break;
4784 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004785 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4786 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004787 case TemplateArgument::TemplateExpansion:
4788 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004789 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004790 Record.push_back(*NumExpansions + 1);
4791 else
4792 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004793 break;
4794 case TemplateArgument::Expression:
4795 AddStmt(Arg.getAsExpr());
4796 break;
4797 case TemplateArgument::Pack:
4798 Record.push_back(Arg.pack_size());
4799 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4800 I != E; ++I)
4801 AddTemplateArgument(*I, Record);
4802 break;
4803 }
4804}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004805
4806void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004807ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004808 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004809 assert(TemplateParams && "No TemplateParams!");
4810 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4811 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4812 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4813 Record.push_back(TemplateParams->size());
4814 for (TemplateParameterList::const_iterator
4815 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4816 P != PEnd; ++P)
4817 AddDeclRef(*P, Record);
4818}
4819
4820/// \brief Emit a template argument list.
4821void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004822ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004823 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004824 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004825 Record.push_back(TemplateArgs->size());
4826 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004827 AddTemplateArgument(TemplateArgs->get(i), Record);
4828}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004829
4830
4831void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004832ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004833 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004834 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004835 I = Set.begin(), E = Set.end(); I != E; ++I) {
4836 AddDeclRef(I.getDecl(), Record);
4837 Record.push_back(I.getAccess());
4838 }
4839}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004840
Sebastian Redla4232eb2010-08-18 23:56:21 +00004841void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004842 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004843 Record.push_back(Base.isVirtual());
4844 Record.push_back(Base.isBaseOfClass());
4845 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004846 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004847 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004848 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004849 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4850 : SourceLocation(),
4851 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004852}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004853
Douglas Gregor7c789c12010-10-29 22:39:52 +00004854void ASTWriter::FlushCXXBaseSpecifiers() {
4855 RecordData Record;
4856 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4857 Record.clear();
4858
4859 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004860 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004861 if (Index == CXXBaseSpecifiersOffsets.size())
4862 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4863 else {
4864 if (Index > CXXBaseSpecifiersOffsets.size())
4865 CXXBaseSpecifiersOffsets.resize(Index + 1);
4866 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4867 }
4868
4869 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4870 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4871 Record.push_back(BEnd - B);
4872 for (; B != BEnd; ++B)
4873 AddCXXBaseSpecifier(*B, Record);
4874 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004875
4876 // Flush any expressions that were written as part of the base specifiers.
4877 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004878 }
4879
4880 CXXBaseSpecifiersToWrite.clear();
4881}
4882
Sean Huntcbb67482011-01-08 20:30:50 +00004883void ASTWriter::AddCXXCtorInitializers(
4884 const CXXCtorInitializer * const *CtorInitializers,
4885 unsigned NumCtorInitializers,
4886 RecordDataImpl &Record) {
4887 Record.push_back(NumCtorInitializers);
4888 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4889 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004890
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004891 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004892 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004893 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004894 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004895 } else if (Init->isDelegatingInitializer()) {
4896 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004897 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004898 } else if (Init->isMemberInitializer()){
4899 Record.push_back(CTOR_INITIALIZER_MEMBER);
4900 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004901 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004902 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4903 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004904 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004905
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004906 AddSourceLocation(Init->getMemberLocation(), Record);
4907 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004908 AddSourceLocation(Init->getLParenLoc(), Record);
4909 AddSourceLocation(Init->getRParenLoc(), Record);
4910 Record.push_back(Init->isWritten());
4911 if (Init->isWritten()) {
4912 Record.push_back(Init->getSourceOrder());
4913 } else {
4914 Record.push_back(Init->getNumArrayIndices());
4915 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4916 AddDeclRef(Init->getArrayIndex(i), Record);
4917 }
4918 }
4919}
4920
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004921void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4922 assert(D->DefinitionData);
4923 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004924 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004925 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004926 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004927 Record.push_back(Data.Aggregate);
4928 Record.push_back(Data.PlainOldData);
4929 Record.push_back(Data.Empty);
4930 Record.push_back(Data.Polymorphic);
4931 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004932 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004933 Record.push_back(Data.HasNoNonEmptyBases);
4934 Record.push_back(Data.HasPrivateFields);
4935 Record.push_back(Data.HasProtectedFields);
4936 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004937 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004938 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004939 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004940 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004941 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4942 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4943 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4944 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4945 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4946 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004947 Record.push_back(Data.HasTrivialSpecialMembers);
4948 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004949 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004950 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004951 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004952 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004953 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004954 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004955 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00004956 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
4957 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
4958 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
4959 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00004960 Record.push_back(Data.FailedImplicitMoveConstructor);
4961 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004962 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004963
4964 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004965 if (Data.NumBases > 0)
4966 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4967 Record);
4968
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004969 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4970 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004971 if (Data.NumVBases > 0)
4972 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4973 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004974
4975 AddUnresolvedSet(Data.Conversions, Record);
4976 AddUnresolvedSet(Data.VisibleConversions, Record);
4977 // Data.Definition is the owning decl, no need to write it.
4978 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004979
4980 // Add lambda-specific data.
4981 if (Data.IsLambda) {
4982 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004983 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004984 Record.push_back(Lambda.NumCaptures);
4985 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004986 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004987 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004988 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004989 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4990 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4991 AddSourceLocation(Capture.getLocation(), Record);
4992 Record.push_back(Capture.isImplicit());
4993 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4994 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4995 AddDeclRef(Var, Record);
4996 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4997 : SourceLocation(),
4998 Record);
4999 }
5000 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005001}
5002
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005003void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005004 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005005 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005006 assert(FirstDeclID == NextDeclID &&
5007 FirstTypeID == NextTypeID &&
5008 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005009 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005010 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005011 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005012 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005013
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005014 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005015
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005016 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5017 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5018 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005019 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005020 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005021 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005022 NextDeclID = FirstDeclID;
5023 NextTypeID = FirstTypeID;
5024 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005025 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005026 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005027 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005028}
5029
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005030void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005031 // Always keep the highest ID. See \p TypeRead() for more information.
5032 IdentID &StoredID = IdentifierIDs[II];
5033 if (ID > StoredID)
5034 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005035}
5036
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005037void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005038 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005039 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005040 if (ID > StoredID)
5041 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005042}
5043
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005044void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005045 // Always take the highest-numbered type index. This copes with an interesting
5046 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005047 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005048 // keep the higher-numbered entry so that we can properly write it out to
5049 // the AST file.
5050 TypeIdx &StoredIdx = TypeIdxs[T];
5051 if (Idx.getIndex() >= StoredIdx.getIndex())
5052 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005053}
5054
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005055void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005056 // Always keep the highest ID. See \p TypeRead() for more information.
5057 SelectorID &StoredID = SelectorIDs[S];
5058 if (ID > StoredID)
5059 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005060}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005061
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005062void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005063 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005064 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005065 MacroDefinitions[MD] = ID;
5066}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005067
Douglas Gregora015cab2011-12-02 17:30:13 +00005068void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5069 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5070 SubmoduleIDs[Mod] = ID;
5071}
5072
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005073void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005074 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005075 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005076 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5077 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005078 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005079 // A forward reference was mutated into a definition. Rewrite it.
5080 // FIXME: This happens during template instantiation, should we
5081 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005082 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005083 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005084 }
5085}
Douglas Gregora8235d62012-10-09 23:05:51 +00005086
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005087void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005088 assert(!WritingAST && "Already writing the AST!");
5089
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005090 // TU and namespaces are handled elsewhere.
5091 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5092 return;
5093
Douglas Gregor919814d2011-09-09 23:01:35 +00005094 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005095 return; // Not a source decl added to a DeclContext from PCH.
5096
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005097 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005098 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005099 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005100}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005101
5102void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005103 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005104 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005105 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005106 return; // Not a source member added to a class from PCH.
5107 if (!isa<CXXMethodDecl>(D))
5108 return; // We are interested in lazily declared implicit methods.
5109
5110 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005111 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005112 UpdateRecord &Record = DeclUpdates[RD];
5113 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005114 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005115}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005116
5117void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5118 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005119 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005120 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005121 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005122 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005123 return; // Not a source specialization added to a template from PCH.
5124
5125 UpdateRecord &Record = DeclUpdates[TD];
5126 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005127 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005128}
Douglas Gregor89d99802010-11-30 06:16:57 +00005129
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005130void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5131 const FunctionDecl *D) {
5132 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005133 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005134 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005135 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005136 return; // Not a source specialization added to a template from PCH.
5137
5138 UpdateRecord &Record = DeclUpdates[TD];
5139 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005140 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005141}
5142
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005143void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005144 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005145 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005146 return; // Declaration not imported from PCH.
5147
5148 // Implicit decl from a PCH was defined.
5149 // FIXME: Should implicit definition be a separate FunctionDecl?
5150 RewriteDecl(D);
5151}
5152
Sebastian Redlf79a7192011-04-29 08:19:30 +00005153void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005154 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005155 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005156 return;
5157
5158 // Since the actual instantiation is delayed, this really means that we need
5159 // to update the instantiation location.
5160 UpdateRecord &Record = DeclUpdates[D];
5161 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5162 AddSourceLocation(
5163 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5164}
5165
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005166void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5167 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005168 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005169 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005170 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005171
5172 assert(IFD->getDefinition() && "Category on a class without a definition?");
5173 ObjCClassesWithCategories.insert(
5174 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005175}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005176
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005177
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005178void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5179 const ObjCPropertyDecl *OrigProp,
5180 const ObjCCategoryDecl *ClassExt) {
5181 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5182 if (!D)
5183 return;
5184
5185 assert(!WritingAST && "Already writing the AST!");
5186 if (!D->isFromASTFile())
5187 return; // Declaration not imported from PCH.
5188
5189 RewriteDecl(D);
5190}