blob: b113c76f44c80499ef3027c7071b5397c35efb82 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
Sebastian Redlade50002010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000059static StringRef data(const std::vector<T, Allocator> &v) {
60 if (v.empty()) return StringRef();
61 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000063}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000069}
70
Douglas Gregor2cf26342009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000074
Douglas Gregor2cf26342009-04-09 22:27:44 +000075namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000086
87 void VisitArrayType(const ArrayType *T);
88 void VisitFunctionType(const FunctionType *T);
89 void VisitTagType(const TagType *T);
90
91#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
92#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000113 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114}
115
Sebastian Redl3397c552010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000117 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
118 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000123 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000124 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125}
126
Sebastian Redl3397c552010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 Writer.AddTypeRef(T->getPointeeType(), Record);
129 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000136 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137}
138
Sebastian Redl3397c552010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140 VisitArrayType(T);
141 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000147 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
Sebastian Redl3397c552010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000152 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
153 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000154 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000155 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156}
157
Sebastian Redl3397c552010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getElementType(), Record);
160 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000161 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000162 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163}
164
Sebastian Redl3397c552010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000167 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168}
169
Sebastian Redl3397c552010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000172 FunctionType::ExtInfo C = T->getExtInfo();
173 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000174 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000175 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000176 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000177 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000178 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179}
180
Sebastian Redl3397c552010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000183 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184}
185
Sebastian Redl3397c552010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187 VisitFunctionType(T);
188 Record.push_back(T->getNumArgs());
189 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
190 Writer.AddTypeRef(T->getArgType(I), Record);
191 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000192 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000194 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000195 Record.push_back(T->getExceptionSpecType());
196 if (T->getExceptionSpecType() == EST_Dynamic) {
197 Record.push_back(T->getNumExceptions());
198 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
199 Writer.AddTypeRef(T->getExceptionType(I), Record);
200 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
201 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000202 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
204 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000205 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
206 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000207 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000208 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209}
210
Sebastian Redl3397c552010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000212 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000214}
John McCalled976492009-12-04 22:46:56 +0000215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000218 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
219 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226}
227
Sebastian Redl3397c552010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000230 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231}
232
Sebastian Redl3397c552010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000234 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000235 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000236 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000237}
238
Sean Huntca63c202011-05-24 22:41:36 +0000239void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
240 Writer.AddTypeRef(T->getBaseType(), Record);
241 Writer.AddTypeRef(T->getUnderlyingType(), Record);
242 Record.push_back(T->getUTTKind());
243 Code = TYPE_UNARY_TRANSFORM;
244}
245
Richard Smith34b41d92011-02-20 03:19:35 +0000246void ASTTypeWriter::VisitAutoType(const AutoType *T) {
247 Writer.AddTypeRef(T->getDeducedType(), Record);
248 Code = TYPE_AUTO;
249}
250
Sebastian Redl3397c552010-08-18 23:56:27 +0000251void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000252 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000253 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000254 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255 "Cannot serialize in the middle of a type definition");
256}
257
Sebastian Redl3397c552010-08-18 23:56:27 +0000258void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000259 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000260 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261}
262
Sebastian Redl3397c552010-08-18 23:56:27 +0000263void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000265 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000266}
267
John McCall9d156a72011-01-06 01:58:22 +0000268void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
269 Writer.AddTypeRef(T->getModifiedType(), Record);
270 Writer.AddTypeRef(T->getEquivalentType(), Record);
271 Record.push_back(T->getAttrKind());
272 Code = TYPE_ATTRIBUTED;
273}
274
Mike Stump1eb44332009-09-09 15:08:12 +0000275void
Sebastian Redl3397c552010-08-18 23:56:27 +0000276ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000277 const SubstTemplateTypeParmType *T) {
278 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
279 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000280 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000281}
282
283void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000284ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
285 const SubstTemplateTypeParmPackType *T) {
286 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
287 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
288 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
289}
290
291void
Sebastian Redl3397c552010-08-18 23:56:27 +0000292ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000293 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000294 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000295 Writer.AddTemplateName(T->getTemplateName(), Record);
296 Record.push_back(T->getNumArgs());
297 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
298 ArgI != ArgE; ++ArgI)
299 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000300 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
301 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000302 : T->getCanonicalTypeInternal(),
303 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000304 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000305}
306
307void
Sebastian Redl3397c552010-08-18 23:56:27 +0000308ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000309 VisitArrayType(T);
310 Writer.AddStmt(T->getSizeExpr());
311 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000312 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313}
314
315void
Sebastian Redl3397c552010-08-18 23:56:27 +0000316ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317 const DependentSizedExtVectorType *T) {
318 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000319 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320}
321
322void
Sebastian Redl3397c552010-08-18 23:56:27 +0000323ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000324 Record.push_back(T->getDepth());
325 Record.push_back(T->getIndex());
326 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000327 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000333 Record.push_back(T->getKeyword());
334 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
335 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000336 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
337 : T->getCanonicalTypeInternal(),
338 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000339 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl3397c552010-08-18 23:56:27 +0000343ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000344 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000345 Record.push_back(T->getKeyword());
346 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
347 Writer.AddIdentifierRef(T->getIdentifier(), Record);
348 Record.push_back(T->getNumArgs());
349 for (DependentTemplateSpecializationType::iterator
350 I = T->begin(), E = T->end(); I != E; ++I)
351 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000352 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000353}
354
Douglas Gregor7536dd52010-12-20 02:24:11 +0000355void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
356 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000357 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000358 Record.push_back(*NumExpansions + 1);
359 else
360 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000361 Code = TYPE_PACK_EXPANSION;
362}
363
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000364void ASTTypeWriter::VisitParenType(const ParenType *T) {
365 Writer.AddTypeRef(T->getInnerType(), Record);
366 Code = TYPE_PAREN;
367}
368
Sebastian Redl3397c552010-08-18 23:56:27 +0000369void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000370 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000371 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
372 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000374}
375
Sebastian Redl3397c552010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000377 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000378 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000379 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000380}
381
Sebastian Redl3397c552010-08-18 23:56:27 +0000382void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000383 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000384 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000385}
386
Sebastian Redl3397c552010-08-18 23:56:27 +0000387void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000388 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000389 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000390 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000391 E = T->qual_end(); I != E; ++I)
392 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000393 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000394}
395
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000396void
Sebastian Redl3397c552010-08-18 23:56:27 +0000397ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000398 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000399 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000400}
401
Eli Friedmanb001de72011-10-06 23:00:33 +0000402void
403ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
404 Writer.AddTypeRef(T->getValueType(), Record);
405 Code = TYPE_ATOMIC;
406}
407
John McCalla1ee0c52009-10-16 21:56:05 +0000408namespace {
409
410class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000411 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000412 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000413
414public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000415 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000416 : Writer(Writer), Record(Record) { }
417
John McCall51bd8032009-10-18 01:05:36 +0000418#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000419#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000420 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000421#include "clang/AST/TypeLocNodes.def"
422
John McCall51bd8032009-10-18 01:05:36 +0000423 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
424 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000425};
426
427}
428
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
430 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000431}
John McCall51bd8032009-10-18 01:05:36 +0000432void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000433 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
434 if (TL.needsExtraLocalData()) {
435 Record.push_back(TL.getWrittenTypeSpec());
436 Record.push_back(TL.getWrittenSignSpec());
437 Record.push_back(TL.getWrittenWidthSpec());
438 Record.push_back(TL.hasModeAttr());
439 }
John McCalla1ee0c52009-10-16 21:56:05 +0000440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000446}
John McCall51bd8032009-10-18 01:05:36 +0000447void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000458 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000459}
John McCall51bd8032009-10-18 01:05:36 +0000460void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
462 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
463 Record.push_back(TL.getSizeExpr() ? 1 : 0);
464 if (TL.getSizeExpr())
465 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000466}
John McCall51bd8032009-10-18 01:05:36 +0000467void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
468 VisitArrayTypeLoc(TL);
469}
470void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
471 VisitArrayTypeLoc(TL);
472}
473void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
477 DependentSizedArrayTypeLoc TL) {
478 VisitArrayTypeLoc(TL);
479}
480void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
481 DependentSizedExtVectorTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
484void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
487void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
490void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000491 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000492 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
493 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000494 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000495 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
496 Writer.AddDeclRef(TL.getArg(i), Record);
497}
498void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
499 VisitFunctionTypeLoc(TL);
500}
501void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
502 VisitFunctionTypeLoc(TL);
503}
John McCalled976492009-12-04 22:46:56 +0000504void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
John McCall51bd8032009-10-18 01:05:36 +0000507void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000511 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
512 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
513 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000514}
515void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000516 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
517 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
518 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
519 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000520}
521void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
Sean Huntca63c202011-05-24 22:41:36 +0000524void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getKWLoc(), Record);
526 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
527 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
528 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
529}
Richard Smith34b41d92011-02-20 03:19:35 +0000530void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
John McCall51bd8032009-10-18 01:05:36 +0000533void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
536void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getNameLoc(), Record);
538}
John McCall9d156a72011-01-06 01:58:22 +0000539void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
540 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
541 if (TL.hasAttrOperand()) {
542 SourceRange range = TL.getAttrOperandParensRange();
543 Writer.AddSourceLocation(range.getBegin(), Record);
544 Writer.AddSourceLocation(range.getEnd(), Record);
545 }
546 if (TL.hasAttrExprOperand()) {
547 Expr *operand = TL.getAttrExprOperand();
548 Record.push_back(operand ? 1 : 0);
549 if (operand) Writer.AddStmt(operand);
550 } else if (TL.hasAttrEnumOperand()) {
551 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
552 }
553}
John McCall51bd8032009-10-18 01:05:36 +0000554void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
John McCall49a832b2009-10-18 09:09:24 +0000557void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
558 SubstTemplateTypeParmTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getNameLoc(), Record);
560}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000561void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
562 SubstTemplateTypeParmPackTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getNameLoc(), Record);
564}
John McCall51bd8032009-10-18 01:05:36 +0000565void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
566 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000567 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000568 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
569 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
570 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
571 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000572 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
573 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000574}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000575void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
576 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
577 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
578}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000579void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000580 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000582}
John McCall3cb0ebd2010-03-10 03:28:59 +0000583void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getNameLoc(), Record);
585}
Douglas Gregor4714c122010-03-31 17:34:00 +0000586void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000587 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000588 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000589 Writer.AddSourceLocation(TL.getNameLoc(), Record);
590}
John McCall33500952010-06-11 00:33:02 +0000591void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
592 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000594 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000595 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000596 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000600 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
601 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000602}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000603void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
605}
John McCall51bd8032009-10-18 01:05:36 +0000606void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
607 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000608}
609void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
610 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000611 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
612 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
613 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
614 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000615}
John McCall54e14c42009-10-22 22:37:11 +0000616void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
617 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000618}
Eli Friedmanb001de72011-10-06 23:00:33 +0000619void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
620 Writer.AddSourceLocation(TL.getKWLoc(), Record);
621 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
622 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
623}
John McCalla1ee0c52009-10-16 21:56:05 +0000624
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000625//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000626// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000627//===----------------------------------------------------------------------===//
628
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000629static void EmitBlockID(unsigned ID, const char *Name,
630 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632 Record.clear();
633 Record.push_back(ID);
634 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
635
636 // Emit the block name if present.
637 if (Name == 0 || Name[0] == 0) return;
638 Record.clear();
639 while (*Name)
640 Record.push_back(*Name++);
641 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
642}
643
644static void EmitRecordID(unsigned ID, const char *Name,
645 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000646 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000647 Record.clear();
648 Record.push_back(ID);
649 while (*Name)
650 Record.push_back(*Name++);
651 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000652}
653
654static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000655 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000656#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000657 RECORD(STMT_STOP);
658 RECORD(STMT_NULL_PTR);
659 RECORD(STMT_NULL);
660 RECORD(STMT_COMPOUND);
661 RECORD(STMT_CASE);
662 RECORD(STMT_DEFAULT);
663 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000664 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000665 RECORD(STMT_IF);
666 RECORD(STMT_SWITCH);
667 RECORD(STMT_WHILE);
668 RECORD(STMT_DO);
669 RECORD(STMT_FOR);
670 RECORD(STMT_GOTO);
671 RECORD(STMT_INDIRECT_GOTO);
672 RECORD(STMT_CONTINUE);
673 RECORD(STMT_BREAK);
674 RECORD(STMT_RETURN);
675 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000676 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000677 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000678 RECORD(EXPR_PREDEFINED);
679 RECORD(EXPR_DECL_REF);
680 RECORD(EXPR_INTEGER_LITERAL);
681 RECORD(EXPR_FLOATING_LITERAL);
682 RECORD(EXPR_IMAGINARY_LITERAL);
683 RECORD(EXPR_STRING_LITERAL);
684 RECORD(EXPR_CHARACTER_LITERAL);
685 RECORD(EXPR_PAREN);
686 RECORD(EXPR_UNARY_OPERATOR);
687 RECORD(EXPR_SIZEOF_ALIGN_OF);
688 RECORD(EXPR_ARRAY_SUBSCRIPT);
689 RECORD(EXPR_CALL);
690 RECORD(EXPR_MEMBER);
691 RECORD(EXPR_BINARY_OPERATOR);
692 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
693 RECORD(EXPR_CONDITIONAL_OPERATOR);
694 RECORD(EXPR_IMPLICIT_CAST);
695 RECORD(EXPR_CSTYLE_CAST);
696 RECORD(EXPR_COMPOUND_LITERAL);
697 RECORD(EXPR_EXT_VECTOR_ELEMENT);
698 RECORD(EXPR_INIT_LIST);
699 RECORD(EXPR_DESIGNATED_INIT);
700 RECORD(EXPR_IMPLICIT_VALUE_INIT);
701 RECORD(EXPR_VA_ARG);
702 RECORD(EXPR_ADDR_LABEL);
703 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000704 RECORD(EXPR_CHOOSE);
705 RECORD(EXPR_GNU_NULL);
706 RECORD(EXPR_SHUFFLE_VECTOR);
707 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000708 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000709 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000710 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000711 RECORD(EXPR_OBJC_ARRAY_LITERAL);
712 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000713 RECORD(EXPR_OBJC_ENCODE);
714 RECORD(EXPR_OBJC_SELECTOR_EXPR);
715 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
716 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
717 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
718 RECORD(EXPR_OBJC_KVC_REF_EXPR);
719 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000720 RECORD(STMT_OBJC_FOR_COLLECTION);
721 RECORD(STMT_OBJC_CATCH);
722 RECORD(STMT_OBJC_FINALLY);
723 RECORD(STMT_OBJC_AT_TRY);
724 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
725 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000726 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000727 RECORD(EXPR_CXX_OPERATOR_CALL);
728 RECORD(EXPR_CXX_CONSTRUCT);
729 RECORD(EXPR_CXX_STATIC_CAST);
730 RECORD(EXPR_CXX_DYNAMIC_CAST);
731 RECORD(EXPR_CXX_REINTERPRET_CAST);
732 RECORD(EXPR_CXX_CONST_CAST);
733 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000734 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000735 RECORD(EXPR_CXX_BOOL_LITERAL);
736 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000737 RECORD(EXPR_CXX_TYPEID_EXPR);
738 RECORD(EXPR_CXX_TYPEID_TYPE);
739 RECORD(EXPR_CXX_UUIDOF_EXPR);
740 RECORD(EXPR_CXX_UUIDOF_TYPE);
741 RECORD(EXPR_CXX_THIS);
742 RECORD(EXPR_CXX_THROW);
743 RECORD(EXPR_CXX_DEFAULT_ARG);
744 RECORD(EXPR_CXX_BIND_TEMPORARY);
745 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
746 RECORD(EXPR_CXX_NEW);
747 RECORD(EXPR_CXX_DELETE);
748 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
749 RECORD(EXPR_EXPR_WITH_CLEANUPS);
750 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
751 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
752 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
753 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
754 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
755 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
756 RECORD(EXPR_CXX_NOEXCEPT);
757 RECORD(EXPR_OPAQUE_VALUE);
758 RECORD(EXPR_BINARY_TYPE_TRAIT);
759 RECORD(EXPR_PACK_EXPANSION);
760 RECORD(EXPR_SIZEOF_PACK);
761 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000762 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000763#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764}
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Sebastian Redla4232eb2010-08-18 23:56:21 +0000766void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000767 RecordData Record;
768 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000770#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
771#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000773 // Control Block.
774 BLOCK(CONTROL_BLOCK);
775 RECORD(METADATA);
776 RECORD(IMPORTS);
777 RECORD(LANGUAGE_OPTIONS);
778 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000779 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000780 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000781 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000782 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000783 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000784 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000785 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000786 RECORD(PREPROCESSOR_OPTIONS);
787
Douglas Gregorc337fef2012-10-19 00:45:00 +0000788 BLOCK(INPUT_FILES_BLOCK);
789 RECORD(INPUT_FILE);
790
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000791 // AST Top-Level Block.
792 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000793 RECORD(TYPE_OFFSET);
794 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000795 RECORD(IDENTIFIER_OFFSET);
796 RECORD(IDENTIFIER_TABLE);
797 RECORD(EXTERNAL_DEFINITIONS);
798 RECORD(SPECIAL_TYPES);
799 RECORD(STATISTICS);
800 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000801 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000802 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000803 RECORD(SELECTOR_OFFSETS);
804 RECORD(METHOD_POOL);
805 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000806 RECORD(SOURCE_LOCATION_OFFSETS);
807 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000808 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000809 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000810 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000811 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000812 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000813 RECORD(SEMA_DECL_REFS);
814 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
815 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
816 RECORD(DECL_REPLACEMENTS);
817 RECORD(UPDATE_VISIBLE);
818 RECORD(DECL_UPDATE_OFFSETS);
819 RECORD(DECL_UPDATES);
820 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
821 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000822 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000823 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000824 RECORD(FP_PRAGMA_OPTIONS);
825 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000826 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000827 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000828 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000829 RECORD(MODULE_OFFSET_MAP);
830 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000831 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000832 RECORD(FILE_SORTED_DECLS);
833 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000834 RECORD(MERGED_DECLARATIONS);
835 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000836 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000837 RECORD(MACRO_OFFSET);
838 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000839
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000841 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000842 RECORD(SM_SLOC_FILE_ENTRY);
843 RECORD(SM_SLOC_BUFFER_ENTRY);
844 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000845 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000847 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000848 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000849 RECORD(PP_MACRO_OBJECT_LIKE);
850 RECORD(PP_MACRO_FUNCTION_LIKE);
851 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000852
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000853 // Decls and Types block.
854 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000855 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000856 RECORD(TYPE_COMPLEX);
857 RECORD(TYPE_POINTER);
858 RECORD(TYPE_BLOCK_POINTER);
859 RECORD(TYPE_LVALUE_REFERENCE);
860 RECORD(TYPE_RVALUE_REFERENCE);
861 RECORD(TYPE_MEMBER_POINTER);
862 RECORD(TYPE_CONSTANT_ARRAY);
863 RECORD(TYPE_INCOMPLETE_ARRAY);
864 RECORD(TYPE_VARIABLE_ARRAY);
865 RECORD(TYPE_VECTOR);
866 RECORD(TYPE_EXT_VECTOR);
867 RECORD(TYPE_FUNCTION_PROTO);
868 RECORD(TYPE_FUNCTION_NO_PROTO);
869 RECORD(TYPE_TYPEDEF);
870 RECORD(TYPE_TYPEOF_EXPR);
871 RECORD(TYPE_TYPEOF);
872 RECORD(TYPE_RECORD);
873 RECORD(TYPE_ENUM);
874 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000875 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000876 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000877 RECORD(TYPE_DECLTYPE);
878 RECORD(TYPE_ELABORATED);
879 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
880 RECORD(TYPE_UNRESOLVED_USING);
881 RECORD(TYPE_INJECTED_CLASS_NAME);
882 RECORD(TYPE_OBJC_OBJECT);
883 RECORD(TYPE_TEMPLATE_TYPE_PARM);
884 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
885 RECORD(TYPE_DEPENDENT_NAME);
886 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
887 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
888 RECORD(TYPE_PAREN);
889 RECORD(TYPE_PACK_EXPANSION);
890 RECORD(TYPE_ATTRIBUTED);
891 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000892 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000893 RECORD(DECL_TYPEDEF);
894 RECORD(DECL_ENUM);
895 RECORD(DECL_RECORD);
896 RECORD(DECL_ENUM_CONSTANT);
897 RECORD(DECL_FUNCTION);
898 RECORD(DECL_OBJC_METHOD);
899 RECORD(DECL_OBJC_INTERFACE);
900 RECORD(DECL_OBJC_PROTOCOL);
901 RECORD(DECL_OBJC_IVAR);
902 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000903 RECORD(DECL_OBJC_CATEGORY);
904 RECORD(DECL_OBJC_CATEGORY_IMPL);
905 RECORD(DECL_OBJC_IMPLEMENTATION);
906 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
907 RECORD(DECL_OBJC_PROPERTY);
908 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000909 RECORD(DECL_FIELD);
910 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000911 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000912 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000913 RECORD(DECL_FILE_SCOPE_ASM);
914 RECORD(DECL_BLOCK);
915 RECORD(DECL_CONTEXT_LEXICAL);
916 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000917 RECORD(DECL_NAMESPACE);
918 RECORD(DECL_NAMESPACE_ALIAS);
919 RECORD(DECL_USING);
920 RECORD(DECL_USING_SHADOW);
921 RECORD(DECL_USING_DIRECTIVE);
922 RECORD(DECL_UNRESOLVED_USING_VALUE);
923 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
924 RECORD(DECL_LINKAGE_SPEC);
925 RECORD(DECL_CXX_RECORD);
926 RECORD(DECL_CXX_METHOD);
927 RECORD(DECL_CXX_CONSTRUCTOR);
928 RECORD(DECL_CXX_DESTRUCTOR);
929 RECORD(DECL_CXX_CONVERSION);
930 RECORD(DECL_ACCESS_SPEC);
931 RECORD(DECL_FRIEND);
932 RECORD(DECL_FRIEND_TEMPLATE);
933 RECORD(DECL_CLASS_TEMPLATE);
934 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
935 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
936 RECORD(DECL_FUNCTION_TEMPLATE);
937 RECORD(DECL_TEMPLATE_TYPE_PARM);
938 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
939 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
940 RECORD(DECL_STATIC_ASSERT);
941 RECORD(DECL_CXX_BASE_SPECIFIERS);
942 RECORD(DECL_INDIRECTFIELD);
943 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
944
Douglas Gregora72d8c42011-06-03 02:27:19 +0000945 // Statements and Exprs can occur in the Decls and Types block.
946 AddStmtsExprs(Stream, Record);
947
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000948 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000949 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000950 RECORD(PPD_MACRO_DEFINITION);
951 RECORD(PPD_INCLUSION_DIRECTIVE);
952
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000953#undef RECORD
954#undef BLOCK
955 Stream.ExitBlock();
956}
957
Douglas Gregore650c8c2009-07-07 00:12:59 +0000958/// \brief Adjusts the given filename to only write out the portion of the
959/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000960///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961/// \param Filename the file name to adjust.
962///
963/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
964/// the returned filename will be adjusted by this system root.
965///
966/// \returns either the original filename (if it needs no adjustment) or the
967/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000968static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000969adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregor832d6202011-07-22 16:35:34 +0000972 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 // Verify that the filename and the system root have the same prefix.
976 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000977 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978 if (Filename[Pos] != isysroot[Pos])
979 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 // We hit the end of the filename before we hit the end of the system root.
982 if (!Filename[Pos])
983 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 // If the file name has a '/' at the current position, skip over the '/'.
986 // We distinguish sysroot-based includes from absolute includes by the
987 // absence of '/' at the beginning of sysroot-based includes.
988 if (Filename[Pos] == '/')
989 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991 return Filename + Pos;
992}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000993
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000994/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000995void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
996 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000997 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000998 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000999 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1000 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001001
Douglas Gregore650c8c2009-07-07 00:12:59 +00001002 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001003 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1011 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1012 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1013 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001014 Record.push_back(VERSION_MAJOR);
1015 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001016 Record.push_back(CLANG_VERSION_MAJOR);
1017 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001018 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001019 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001020 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1021 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001022
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001023 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001024 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001025 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001026 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001027 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001028
1029 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1030 M != MEnd; ++M) {
1031 // Skip modules that weren't directly imported.
1032 if (!(*M)->isDirectlyImported())
1033 continue;
1034
1035 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001036 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001037 // FIXME: This writes the absolute path for AST files we depend on.
1038 const std::string &FileName = (*M)->FileName;
1039 Record.push_back(FileName.size());
1040 Record.append(FileName.begin(), FileName.end());
1041 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001042 Stream.EmitRecord(IMPORTS, Record);
1043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001045 // Language options.
1046 Record.clear();
1047 const LangOptions &LangOpts = Context.getLangOpts();
1048#define LANGOPT(Name, Bits, Default, Description) \
1049 Record.push_back(LangOpts.Name);
1050#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1051 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1052#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001053#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1054#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001055
1056 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1057 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1058
1059 Record.push_back(LangOpts.CurrentModule.size());
1060 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001061
1062 // Comment options.
1063 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1064 for (CommentOptions::BlockCommandNamesTy::const_iterator
1065 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1066 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1067 I != IEnd; ++I) {
1068 AddString(*I, Record);
1069 }
1070
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001071 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1072
Douglas Gregoree097c12012-10-18 17:58:09 +00001073 // Target options.
1074 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001075 const TargetInfo &Target = Context.getTargetInfo();
1076 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001077 AddString(TargetOpts.Triple, Record);
1078 AddString(TargetOpts.CPU, Record);
1079 AddString(TargetOpts.ABI, Record);
1080 AddString(TargetOpts.CXXABI, Record);
1081 AddString(TargetOpts.LinkerVersion, Record);
1082 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1083 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1084 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1085 }
1086 Record.push_back(TargetOpts.Features.size());
1087 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1088 AddString(TargetOpts.Features[I], Record);
1089 }
1090 Stream.EmitRecord(TARGET_OPTIONS, Record);
1091
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001092 // Diagnostic options.
1093 Record.clear();
1094 const DiagnosticOptions &DiagOpts
1095 = Context.getDiagnostics().getDiagnosticOptions();
1096#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1097#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1098 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1099#include "clang/Basic/DiagnosticOptions.def"
1100 Record.push_back(DiagOpts.Warnings.size());
1101 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1102 AddString(DiagOpts.Warnings[I], Record);
1103 // Note: we don't serialize the log or serialization file names, because they
1104 // are generally transient files and will almost always be overridden.
1105 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1106
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001107 // File system options.
1108 Record.clear();
1109 const FileSystemOptions &FSOpts
1110 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1111 AddString(FSOpts.WorkingDir, Record);
1112 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1113
Douglas Gregorbbf38312012-10-24 16:50:34 +00001114 // Header search options.
1115 Record.clear();
1116 const HeaderSearchOptions &HSOpts
1117 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1118 AddString(HSOpts.Sysroot, Record);
1119
1120 // Include entries.
1121 Record.push_back(HSOpts.UserEntries.size());
1122 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1123 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1124 AddString(Entry.Path, Record);
1125 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001126 Record.push_back(Entry.IsFramework);
1127 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001128 }
1129
1130 // System header prefixes.
1131 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1132 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1133 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1134 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1135 }
1136
1137 AddString(HSOpts.ResourceDir, Record);
1138 AddString(HSOpts.ModuleCachePath, Record);
1139 Record.push_back(HSOpts.DisableModuleHash);
1140 Record.push_back(HSOpts.UseBuiltinIncludes);
1141 Record.push_back(HSOpts.UseStandardSystemIncludes);
1142 Record.push_back(HSOpts.UseStandardCXXIncludes);
1143 Record.push_back(HSOpts.UseLibcxx);
1144 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1145
Douglas Gregora71a7d82012-10-24 20:05:57 +00001146 // Preprocessor options.
1147 Record.clear();
1148 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1149
1150 // Macro definitions.
1151 Record.push_back(PPOpts.Macros.size());
1152 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1153 AddString(PPOpts.Macros[I].first, Record);
1154 Record.push_back(PPOpts.Macros[I].second);
1155 }
1156
1157 // Includes
1158 Record.push_back(PPOpts.Includes.size());
1159 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1160 AddString(PPOpts.Includes[I], Record);
1161
1162 // Macro includes
1163 Record.push_back(PPOpts.MacroIncludes.size());
1164 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1165 AddString(PPOpts.MacroIncludes[I], Record);
1166
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001167 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001168 AddString(PPOpts.ImplicitPCHInclude, Record);
1169 AddString(PPOpts.ImplicitPTHInclude, Record);
1170 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1171 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1172
Douglas Gregor31d375f2011-05-06 21:43:30 +00001173 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001174 SourceManager &SM = Context.getSourceManager();
1175 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1176 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001177 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1178 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001179 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1180 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1181
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001182 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001184 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001185
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001186 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001187 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001188 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001189 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001190 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001191 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001192 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001193 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001194
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001195 Record.clear();
1196 Record.push_back(SM.getMainFileID().getOpaqueValue());
1197 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1198
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001199 // Original PCH directory
1200 if (!OutputFile.empty() && OutputFile != "-") {
1201 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1202 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1204 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1205
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001206 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001207
1208 llvm::sys::fs::make_absolute(OutputPath);
1209 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1210
1211 RecordData Record;
1212 Record.push_back(ORIGINAL_PCH_DIR);
1213 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1214 }
1215
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001216 WriteInputFiles(Context.SourceMgr,
1217 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1218 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001219 Stream.ExitBlock();
1220}
1221
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001222namespace {
1223 /// \brief An input file.
1224 struct InputFileEntry {
1225 const FileEntry *File;
1226 bool IsSystemFile;
1227 bool BufferOverridden;
1228 };
1229}
1230
1231void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1232 HeaderSearchOptions &HSOpts,
1233 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001234 using namespace llvm;
1235 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1236 RecordData Record;
1237
1238 // Create input-file abbreviation.
1239 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1240 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001241 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001242 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1243 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001244 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001245 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1246 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1247
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001248 // Get all ContentCache objects for files, sorted by whether the file is a
1249 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001250 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001251 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1252 // Get this source location entry.
1253 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001254 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001255
1256 // We only care about file entries that were not overridden.
1257 if (!SLoc->isFile())
1258 continue;
1259 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001260 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001261 continue;
1262
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001263 InputFileEntry Entry;
1264 Entry.File = Cache->OrigEntry;
1265 Entry.IsSystemFile = Cache->IsSystemFile;
1266 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001267 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001268 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001269 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001270 SortedFiles.push_front(Entry);
1271 }
1272
1273 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1274 // the set of (non-system) input files. This is simple heuristic for
1275 // detecting whether the system headers may have changed, because it is too
1276 // expensive to stat() all of the system headers.
1277 FileManager &FileMgr = SourceMgr.getFileManager();
1278 if (!HSOpts.Sysroot.empty()) {
1279 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1280 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1281 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1282 InputFileEntry Entry = { SDKSettingsFile, false, false };
1283 SortedFiles.push_front(Entry);
1284 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001285 }
1286
1287 unsigned UserFilesNum = 0;
1288 // Write out all of the input files.
1289 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001290 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001291 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001292 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001293
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001294 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001295 if (InputFileID != 0)
1296 continue; // already recorded this file.
1297
Douglas Gregora930dc92012-10-22 18:42:04 +00001298 // Record this entry's offset.
1299 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001300
1301 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001302
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001303 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001304 ++UserFilesNum;
1305
Douglas Gregor745e6f12012-10-19 00:38:02 +00001306 Record.clear();
1307 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001308 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001309
1310 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001311 Record.push_back(Entry.File->getSize());
1312 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001313
Douglas Gregora930dc92012-10-22 18:42:04 +00001314 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001315 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001316
Douglas Gregor745e6f12012-10-19 00:38:02 +00001317 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001318 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001319 SmallString<128> FilePath(Filename);
1320
1321 // Ask the file manager to fixup the relative path for us. This will
1322 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001323 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001324
1325 // FIXME: This call to make_absolute shouldn't be necessary, the
1326 // call to FixupRelativePath should always return an absolute path.
1327 llvm::sys::fs::make_absolute(FilePath);
1328 Filename = FilePath.c_str();
1329
1330 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1331
1332 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1333 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001334
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001335 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001336
1337 // Create input file offsets abbreviation.
1338 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1339 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1340 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001341 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1342 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001343 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1344 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1345
1346 // Write input file offsets.
1347 Record.clear();
1348 Record.push_back(INPUT_FILE_OFFSETS);
1349 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001350 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001351 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001352}
1353
Douglas Gregor14f79002009-04-10 03:52:48 +00001354//===----------------------------------------------------------------------===//
1355// Source Manager Serialization
1356//===----------------------------------------------------------------------===//
1357
1358/// \brief Create an abbreviation for the SLocEntry that refers to a
1359/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001360static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001361 using namespace llvm;
1362 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001363 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001368 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001373 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001374}
1375
1376/// \brief Create an abbreviation for the SLocEntry that refers to a
1377/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001378static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001379 using namespace llvm;
1380 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001381 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001387 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001388}
1389
1390/// \brief Create an abbreviation for the SLocEntry that refers to a
1391/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001392static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001393 using namespace llvm;
1394 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001395 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001397 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001398}
1399
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001400/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1401/// expansion.
1402static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001403 using namespace llvm;
1404 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001405 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001411 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001412}
1413
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001414namespace {
1415 // Trait used for the on-disk hash table of header search information.
1416 class HeaderFileInfoTrait {
1417 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001418 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001419
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001420 // Keep track of the framework names we've used during serialization.
1421 SmallVector<char, 128> FrameworkStringData;
1422 llvm::StringMap<unsigned> FrameworkNameOffset;
1423
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001424 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001425 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1426 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001427
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001428 struct key_type {
1429 const FileEntry *FE;
1430 const char *Filename;
1431 };
1432 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001433
1434 typedef HeaderFileInfo data_type;
1435 typedef const data_type &data_type_ref;
1436
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001437 static unsigned ComputeHash(key_type_ref key) {
1438 // The hash is based only on size/time of the file, so that the reader can
1439 // match even when symlinking or excess path elements ("foo/../", "../")
1440 // change the form of the name. However, complete path is still the key.
1441 return llvm::hash_combine(key.FE->getSize(),
1442 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001443 }
1444
1445 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001446 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1447 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1448 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001449 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001450 if (Data.isModuleHeader)
1451 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001452 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001453 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001454 }
1455
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001456 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1457 clang::io::Emit64(Out, key.FE->getSize());
1458 KeyLen -= 8;
1459 clang::io::Emit64(Out, key.FE->getModificationTime());
1460 KeyLen -= 8;
1461 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001462 }
1463
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001464 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001465 data_type_ref Data, unsigned DataLen) {
1466 using namespace clang::io;
1467 uint64_t Start = Out.tell(); (void)Start;
1468
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001469 unsigned char Flags = (Data.isImport << 5)
1470 | (Data.isPragmaOnce << 4)
1471 | (Data.DirInfo << 2)
1472 | (Data.Resolved << 1)
1473 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001474 Emit8(Out, (uint8_t)Flags);
1475 Emit16(Out, (uint16_t) Data.NumIncludes);
1476
1477 if (!Data.ControllingMacro)
1478 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1479 else
1480 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001481
1482 unsigned Offset = 0;
1483 if (!Data.Framework.empty()) {
1484 // If this header refers into a framework, save the framework name.
1485 llvm::StringMap<unsigned>::iterator Pos
1486 = FrameworkNameOffset.find(Data.Framework);
1487 if (Pos == FrameworkNameOffset.end()) {
1488 Offset = FrameworkStringData.size() + 1;
1489 FrameworkStringData.append(Data.Framework.begin(),
1490 Data.Framework.end());
1491 FrameworkStringData.push_back(0);
1492
1493 FrameworkNameOffset[Data.Framework] = Offset;
1494 } else
1495 Offset = Pos->second;
1496 }
1497 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001498
1499 if (Data.isModuleHeader) {
1500 Module *Mod = HS.findModuleForHeader(key.FE);
1501 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1502 }
1503
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001504 assert(Out.tell() - Start == DataLen && "Wrong data length");
1505 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001506
1507 const char *strings_begin() const { return FrameworkStringData.begin(); }
1508 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001509 };
1510} // end anonymous namespace
1511
1512/// \brief Write the header search block for the list of files that
1513///
1514/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001515void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001516 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001517 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1518
1519 if (FilesByUID.size() > HS.header_file_size())
1520 FilesByUID.resize(HS.header_file_size());
1521
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001522 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001523 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001524 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001525 unsigned NumHeaderSearchEntries = 0;
1526 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1527 const FileEntry *File = FilesByUID[UID];
1528 if (!File)
1529 continue;
1530
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001531 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1532 // from the external source if it was not provided already.
1533 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001534 if (HFI.External && Chain)
1535 continue;
1536
1537 // Turn the file name into an absolute path, if it isn't already.
1538 const char *Filename = File->getName();
1539 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1540
1541 // If we performed any translation on the file name at all, we need to
1542 // save this string, since the generator will refer to it later.
1543 if (Filename != File->getName()) {
1544 Filename = strdup(Filename);
1545 SavedStrings.push_back(Filename);
1546 }
1547
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001548 HeaderFileInfoTrait::key_type key = { File, Filename };
1549 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001550 ++NumHeaderSearchEntries;
1551 }
1552
1553 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001554 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001555 uint32_t BucketOffset;
1556 {
1557 llvm::raw_svector_ostream Out(TableData);
1558 // Make sure that no bucket is at offset 0
1559 clang::io::Emit32(Out, 0);
1560 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1561 }
1562
1563 // Create a blob abbreviation
1564 using namespace llvm;
1565 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1566 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1571 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1572
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001573 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001574 RecordData Record;
1575 Record.push_back(HEADER_SEARCH_TABLE);
1576 Record.push_back(BucketOffset);
1577 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001578 Record.push_back(TableData.size());
1579 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001580 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1581
1582 // Free all of the strings we had to duplicate.
1583 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001584 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001585}
1586
Douglas Gregor14f79002009-04-10 03:52:48 +00001587/// \brief Writes the block containing the serialized form of the
1588/// source manager.
1589///
1590/// TODO: We should probably use an on-disk hash table (stored in a
1591/// blob), indexed based on the file name, so that we only create
1592/// entries for files that we actually need. In the common case (no
1593/// errors), we probably won't have to create file entries for any of
1594/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001595void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001596 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001597 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001598 RecordData Record;
1599
Chris Lattnerf04ad692009-04-10 17:16:57 +00001600 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001601 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001602
1603 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001604 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1605 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1606 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001607 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001608
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001609 // Write out the source location entry table. We skip the first
1610 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001611 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001612 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001613 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1614 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001615 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001616 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001617 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001618 FileID FID = FileID::get(I);
1619 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001620
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001621 // Record the offset of this source-location entry.
1622 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1623
1624 // Figure out which record code to use.
1625 unsigned Code;
1626 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001627 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1628 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001629 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001630 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001631 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001632 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001633 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001634 Record.clear();
1635 Record.push_back(Code);
1636
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001637 // Starting offset of this entry within this module, so skip the dummy.
1638 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001639 if (SLoc->isFile()) {
1640 const SrcMgr::FileInfo &File = SLoc->getFile();
1641 Record.push_back(File.getIncludeLoc().getRawEncoding());
1642 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1643 Record.push_back(File.hasLineDirectives());
1644
1645 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001646 if (Content->OrigEntry) {
1647 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001648 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001649
Douglas Gregora930dc92012-10-22 18:42:04 +00001650 // The source location entry is a file. Emit input file ID.
1651 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1652 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001654 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001655
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001656 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001657 if (FDI != FileDeclIDs.end()) {
1658 Record.push_back(FDI->second->FirstDeclIndex);
1659 Record.push_back(FDI->second->DeclIDs.size());
1660 } else {
1661 Record.push_back(0);
1662 Record.push_back(0);
1663 }
Douglas Gregora081da52011-11-16 20:05:18 +00001664
Douglas Gregora930dc92012-10-22 18:42:04 +00001665 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001666
1667 if (Content->BufferOverridden) {
1668 Record.clear();
1669 Record.push_back(SM_SLOC_BUFFER_BLOB);
1670 const llvm::MemoryBuffer *Buffer
1671 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1672 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1673 StringRef(Buffer->getBufferStart(),
1674 Buffer->getBufferSize() + 1));
1675 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001676 } else {
1677 // The source location entry is a buffer. The blob associated
1678 // with this entry contains the contents of the buffer.
1679
1680 // We add one to the size so that we capture the trailing NULL
1681 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1682 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001683 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001684 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001685 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001686 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001687 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001688 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001689 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001690 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001691 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001692 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001693
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001694 if (strcmp(Name, "<built-in>") == 0) {
1695 PreloadSLocs.push_back(SLocEntryOffsets.size());
1696 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001697 }
1698 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001699 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001700 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001701 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1702 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001703 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1704 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001705
1706 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001707 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001708 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001709 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001710 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001711 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001712 }
1713 }
1714
Douglas Gregorc9490c02009-04-16 22:23:12 +00001715 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001716
1717 if (SLocEntryOffsets.empty())
1718 return;
1719
Sebastian Redl3397c552010-08-18 23:56:27 +00001720 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001721 // table is used for lazily loading source-location information.
1722 using namespace llvm;
1723 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001724 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1728 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001730 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001731 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001732 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001733 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001734 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001735
Sebastian Redl3397c552010-08-18 23:56:27 +00001736 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001737 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001738 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001739
1740 // Write the line table. It depends on remapping working, so it must come
1741 // after the source location offsets.
1742 if (SourceMgr.hasLineTable()) {
1743 LineTableInfo &LineTable = SourceMgr.getLineTable();
1744
1745 Record.clear();
1746 // Emit the file names
1747 Record.push_back(LineTable.getNumFilenames());
1748 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1749 // Emit the file name
1750 const char *Filename = LineTable.getFilename(I);
1751 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1752 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1753 Record.push_back(FilenameLen);
1754 if (FilenameLen)
1755 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1756 }
1757
1758 // Emit the line entries
1759 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1760 L != LEnd; ++L) {
1761 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001762 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001763 continue;
1764
1765 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001766 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001767
1768 // Emit the line entries
1769 Record.push_back(L->second.size());
1770 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1771 LEEnd = L->second.end();
1772 LE != LEEnd; ++LE) {
1773 Record.push_back(LE->FileOffset);
1774 Record.push_back(LE->LineNo);
1775 Record.push_back(LE->FilenameID);
1776 Record.push_back((unsigned)LE->FileKind);
1777 Record.push_back(LE->IncludeOffset);
1778 }
1779 }
1780 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1781 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001782}
1783
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001784//===----------------------------------------------------------------------===//
1785// Preprocessor Serialization
1786//===----------------------------------------------------------------------===//
1787
Douglas Gregor9c736102011-02-10 18:20:09 +00001788static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1789 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1790 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1791 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1792 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1793 return X.first->getName().compare(Y.first->getName());
1794}
1795
Chris Lattner0b1fb982009-04-10 17:15:23 +00001796/// \brief Writes the block containing the serialized form of the
1797/// preprocessor.
1798///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001799void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001800 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1801 if (PPRec)
1802 WritePreprocessorDetail(*PPRec);
1803
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001804 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001805
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001806 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1807 if (PP.getCounterValue() != 0) {
1808 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001809 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001810 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001811 }
1812
1813 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001814 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Sebastian Redl3397c552010-08-18 23:56:27 +00001816 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001817 // FIXME: use diagnostics subsystem for localization etc.
1818 if (PP.SawDateOrTime())
1819 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Douglas Gregorecdcb882010-10-20 22:00:55 +00001821
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001822 // Loop over all the macro definitions that are live at the end of the file,
1823 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001824
Douglas Gregor9c736102011-02-10 18:20:09 +00001825 // Construct the list of macro definitions that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001826 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001827 MacrosToEmit;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001828 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001829 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001830 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001831 if (!IsModule || I->second->isPublic()) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001832 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001833 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001834 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001835
Douglas Gregor9c736102011-02-10 18:20:09 +00001836 // Sort the set of macro definitions that need to be serialized by the
1837 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001838 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001839 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001840
Douglas Gregora8235d62012-10-09 23:05:51 +00001841 /// \brief Offsets of each of the macros into the bitstream, indexed by
1842 /// the local macro ID
1843 ///
1844 /// For each identifier that is associated with a macro, this map
1845 /// provides the offset into the bitstream where that macro is
1846 /// defined.
1847 std::vector<uint32_t> MacroOffsets;
1848
Douglas Gregor9c736102011-02-10 18:20:09 +00001849 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1850 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001851
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001852 for (MacroDirective *MD = MacrosToEmit[I].second; MD;
1853 MD = MD->getPrevious()) {
1854 MacroID ID = getMacroRef(MD);
Douglas Gregora8235d62012-10-09 23:05:51 +00001855 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001856 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Douglas Gregora8235d62012-10-09 23:05:51 +00001858 // Skip macros from a AST file if we're chaining.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001859 if (Chain && MD->isImported() && !MD->hasChangedAfterLoad())
Douglas Gregora8235d62012-10-09 23:05:51 +00001860 continue;
1861
1862 if (ID < FirstMacroID) {
1863 // This will have been dealt with via an update record.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001864 assert(MacroUpdates.count(MD) > 0 && "Missing macro update");
Douglas Gregora8235d62012-10-09 23:05:51 +00001865 continue;
1866 }
1867
1868 // Record the local offset of this macro.
1869 unsigned Index = ID - FirstMacroID;
1870 if (Index == MacroOffsets.size())
1871 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1872 else {
1873 if (Index > MacroOffsets.size())
1874 MacroOffsets.resize(Index + 1);
1875
1876 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1877 }
1878
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001879 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001880 addMacroRef(MD, Record);
1881 const MacroInfo *MI = MD->getInfo();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001882 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001883 AddSourceLocation(MI->getDefinitionLoc(), Record);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001884 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001885 AddSourceLocation(MD->getUndefLoc(), Record);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001886 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001887 Record.push_back(MD->isPublic());
1888 AddSourceLocation(MD->getVisibilityLocation(), Record);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001889 unsigned Code;
1890 if (MI->isObjectLike()) {
1891 Code = PP_MACRO_OBJECT_LIKE;
1892 } else {
1893 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001894
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001895 Record.push_back(MI->isC99Varargs());
1896 Record.push_back(MI->isGNUVarargs());
Eli Friedman4fa4b482012-11-14 02:18:46 +00001897 Record.push_back(MI->hasCommaPasting());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001898 Record.push_back(MI->getNumArgs());
1899 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1900 I != E; ++I)
1901 AddIdentifierRef(*I, Record);
1902 }
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001904 // If we have a detailed preprocessing record, record the macro definition
1905 // ID that corresponds to this macro.
1906 if (PPRec)
1907 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1908
1909 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001910 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001911
1912 // Emit the tokens array.
1913 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1914 // Note that we know that the preprocessor does not have any annotation
1915 // tokens in it because they are created by the parser, and thus can't
1916 // be in a macro definition.
1917 const Token &Tok = MI->getReplacementToken(TokNo);
1918
1919 Record.push_back(Tok.getLocation().getRawEncoding());
1920 Record.push_back(Tok.getLength());
1921
1922 // FIXME: When reading literal tokens, reconstruct the literal pointer
1923 // if it is needed.
1924 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1925 // FIXME: Should translate token kind to a stable encoding.
1926 Record.push_back(Tok.getKind());
1927 // FIXME: Should translate token flags to a stable encoding.
1928 Record.push_back(Tok.getFlags());
1929
1930 Stream.EmitRecord(PP_TOKEN, Record);
1931 Record.clear();
1932 }
1933 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001934 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001935 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001936 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001937
1938 // Write the offsets table for macro IDs.
1939 using namespace llvm;
1940 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1941 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1945
1946 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1947 Record.clear();
1948 Record.push_back(MACRO_OFFSET);
1949 Record.push_back(MacroOffsets.size());
1950 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1951 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1952 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001953}
1954
1955void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001956 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001957 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001958
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001959 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001960
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001961 // Enter the preprocessor block.
1962 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001963
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001964 // If the preprocessor has a preprocessing record, emit it.
1965 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001966 using namespace llvm;
1967
1968 // Set up the abbreviation for
1969 unsigned InclusionAbbrev = 0;
1970 {
1971 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1972 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1978 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1979 }
1980
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001981 unsigned FirstPreprocessorEntityID
1982 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1983 + NUM_PREDEF_PP_ENTITY_IDS;
1984 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001985 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001986 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1987 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001988 E != EEnd;
1989 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001990 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001991
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001992 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1993 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001994
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001995 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001996 // Record this macro definition's ID.
1997 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001998
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001999 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002000 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2001 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002002 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002003
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002004 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002005 Record.push_back(ME->isBuiltinMacro());
2006 if (ME->isBuiltinMacro())
2007 AddIdentifierRef(ME->getName(), Record);
2008 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002009 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002010 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002011 continue;
2012 }
2013
2014 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2015 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002016 Record.push_back(ID->getFileName().size());
2017 Record.push_back(ID->wasInQuotes());
2018 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002019 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002020 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002021 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002022 // Check that the FileEntry is not null because it was not resolved and
2023 // we create a PCH even with compiler errors.
2024 if (ID->getFile())
2025 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002026 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2027 continue;
2028 }
2029
2030 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2031 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002032 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002033
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002034 // Write the offsets table for the preprocessing record.
2035 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002036 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2037
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002038 // Write the offsets table for identifier IDs.
2039 using namespace llvm;
2040 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002041 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002044 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002045
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002046 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002047 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002048 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002049 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2050 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002051 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002052}
2053
Douglas Gregore209e502011-12-06 01:10:29 +00002054unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2055 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2056 if (Known != SubmoduleIDs.end())
2057 return Known->second;
2058
2059 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2060}
2061
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002062unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2063 if (!Mod)
2064 return 0;
2065
2066 llvm::DenseMap<Module *, unsigned>::const_iterator
2067 Known = SubmoduleIDs.find(Mod);
2068 if (Known != SubmoduleIDs.end())
2069 return Known->second;
2070
2071 return 0;
2072}
2073
Douglas Gregor26ced122011-12-01 00:59:36 +00002074/// \brief Compute the number of modules within the given tree (including the
2075/// given module).
2076static unsigned getNumberOfModules(Module *Mod) {
2077 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002078 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2079 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002080 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002081 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002082
2083 return ChildModules + 1;
2084}
2085
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002086void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002087 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002088 // FIXME: This feels like it belongs somewhere else, but there are no
2089 // other consumers of this information.
2090 SourceManager &SrcMgr = PP->getSourceManager();
2091 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2092 for (ASTContext::import_iterator I = Context->local_import_begin(),
2093 IEnd = Context->local_import_end();
2094 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002095 if (Module *ImportedFrom
2096 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2097 SrcMgr))) {
2098 ImportedFrom->Imports.push_back(I->getImportedModule());
2099 }
2100 }
2101
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002102 // Enter the submodule description block.
2103 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2104
2105 // Write the abbreviations needed for the submodules block.
2106 using namespace llvm;
2107 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2108 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002109 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002110 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2111 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002117 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2118 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2119
2120 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002121 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2123 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2124
2125 Abbrev = new BitCodeAbbrev();
2126 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2128 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002129
2130 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002131 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2133 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2134
2135 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002136 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2138 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2139
Douglas Gregor51f564f2011-12-31 04:05:44 +00002140 Abbrev = new BitCodeAbbrev();
2141 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2142 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2143 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2144
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002145 Abbrev = new BitCodeAbbrev();
2146 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2148 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2149
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002150 Abbrev = new BitCodeAbbrev();
2151 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2154 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2155
Douglas Gregor26ced122011-12-01 00:59:36 +00002156 // Write the submodule metadata block.
2157 RecordData Record;
2158 Record.push_back(getNumberOfModules(WritingModule));
2159 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2160 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2161
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002162 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002163 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002164 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002165 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002166 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002167 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002168 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002169
2170 // Emit the definition of the block.
2171 Record.clear();
2172 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002173 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002174 if (Mod->Parent) {
2175 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2176 Record.push_back(SubmoduleIDs[Mod->Parent]);
2177 } else {
2178 Record.push_back(0);
2179 }
2180 Record.push_back(Mod->IsFramework);
2181 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002182 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002183 Record.push_back(Mod->InferSubmodules);
2184 Record.push_back(Mod->InferExplicitSubmodules);
2185 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002186 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2187
Douglas Gregor51f564f2011-12-31 04:05:44 +00002188 // Emit the requirements.
2189 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2190 Record.clear();
2191 Record.push_back(SUBMODULE_REQUIRES);
2192 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2193 Mod->Requires[I].data(),
2194 Mod->Requires[I].size());
2195 }
2196
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002197 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002198 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002199 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002200 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002201 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002202 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002203 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2204 Record.clear();
2205 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2206 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2207 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002208 }
2209
2210 // Emit the headers.
2211 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2212 Record.clear();
2213 Record.push_back(SUBMODULE_HEADER);
2214 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2215 Mod->Headers[I]->getName());
2216 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002217 // Emit the excluded headers.
2218 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2219 Record.clear();
2220 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2221 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2222 Mod->ExcludedHeaders[I]->getName());
2223 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002224 ArrayRef<const FileEntry *>
2225 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2226 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002227 Record.clear();
2228 Record.push_back(SUBMODULE_TOPHEADER);
2229 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002230 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002231 }
Douglas Gregor55988682011-12-05 16:33:54 +00002232
2233 // Emit the imports.
2234 if (!Mod->Imports.empty()) {
2235 Record.clear();
2236 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002237 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002238 assert(ImportedID && "Unknown submodule!");
2239 Record.push_back(ImportedID);
2240 }
2241 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2242 }
2243
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002244 // Emit the exports.
2245 if (!Mod->Exports.empty()) {
2246 Record.clear();
2247 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002248 if (Module *Exported = Mod->Exports[I].getPointer()) {
2249 unsigned ExportedID = SubmoduleIDs[Exported];
2250 assert(ExportedID > 0 && "Unknown submodule ID?");
2251 Record.push_back(ExportedID);
2252 } else {
2253 Record.push_back(0);
2254 }
2255
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002256 Record.push_back(Mod->Exports[I].getInt());
2257 }
2258 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2259 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002260
2261 // Emit the link libraries.
2262 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2263 Record.clear();
2264 Record.push_back(SUBMODULE_LINK_LIBRARY);
2265 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2266 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2267 Mod->LinkLibraries[I].Library);
2268 }
2269
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002270 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002271 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2272 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002273 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002274 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002275 }
2276
2277 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002278
2279 assert((NextSubmoduleID - FirstSubmoduleID
2280 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002281}
2282
Douglas Gregor185dbd72011-12-01 02:07:58 +00002283serialization::SubmoduleID
2284ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002285 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002286 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002287
2288 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002289 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002290 Module *OwningMod
2291 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002292 if (!OwningMod)
2293 return 0;
2294
Douglas Gregore209e502011-12-06 01:10:29 +00002295 // Check whether this submodule is part of our own module.
2296 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002297 return 0;
2298
Douglas Gregore209e502011-12-06 01:10:29 +00002299 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002300}
2301
David Blaikied6471f72011-09-25 23:23:43 +00002302void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002303 // FIXME: Make it work properly with modules.
2304 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2305 DiagStateIDMap;
2306 unsigned CurrID = 0;
2307 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002308 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002309 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002310 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2311 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002312 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002313 if (point.Loc.isInvalid())
2314 continue;
2315
2316 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002317 unsigned &DiagStateID = DiagStateIDMap[point.State];
2318 Record.push_back(DiagStateID);
2319
2320 if (DiagStateID == 0) {
2321 DiagStateID = ++CurrID;
2322 for (DiagnosticsEngine::DiagState::const_iterator
2323 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2324 if (I->second.isPragma()) {
2325 Record.push_back(I->first);
2326 Record.push_back(I->second.getMapping());
2327 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002328 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002329 Record.push_back(-1); // mark the end of the diag/map pairs for this
2330 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002331 }
2332 }
2333
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002334 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002335 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002336}
2337
Anders Carlssonc8505782011-03-06 18:41:18 +00002338void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2339 if (CXXBaseSpecifiersOffsets.empty())
2340 return;
2341
2342 RecordData Record;
2343
2344 // Create a blob abbreviation for the C++ base specifiers offsets.
2345 using namespace llvm;
2346
2347 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2348 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2349 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2351 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2352
Douglas Gregore92b8a12011-08-04 00:01:48 +00002353 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002354 Record.clear();
2355 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2356 Record.push_back(CXXBaseSpecifiersOffsets.size());
2357 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002358 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002359}
2360
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002361//===----------------------------------------------------------------------===//
2362// Type Serialization
2363//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002364
Sebastian Redl3397c552010-08-18 23:56:27 +00002365/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002366void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002367 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002368 if (Idx.getIndex() == 0) // we haven't seen this type before.
2369 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Douglas Gregor97475832010-10-05 18:37:06 +00002371 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002372
Douglas Gregor2cf26342009-04-09 22:27:44 +00002373 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002374 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002375 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002376 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002377 else if (TypeOffsets.size() < Index) {
2378 TypeOffsets.resize(Index + 1);
2379 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002380 }
2381
2382 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Douglas Gregor2cf26342009-04-09 22:27:44 +00002384 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002385 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002386
Douglas Gregora4923eb2009-11-16 21:35:15 +00002387 if (T.hasLocalNonFastQualifiers()) {
2388 Qualifiers Qs = T.getLocalQualifiers();
2389 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002390 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002391 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002392 } else {
2393 switch (T->getTypeClass()) {
2394 // For all of the concrete, non-dependent types, call the
2395 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002396#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002397 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002398#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002399#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002400 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002401 }
2402
2403 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002404 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002405
2406 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002407 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002408}
2409
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002410//===----------------------------------------------------------------------===//
2411// Declaration Serialization
2412//===----------------------------------------------------------------------===//
2413
Douglas Gregor2cf26342009-04-09 22:27:44 +00002414/// \brief Write the block containing all of the declaration IDs
2415/// lexically declared within the given DeclContext.
2416///
2417/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2418/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002419uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002420 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002421 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002422 return 0;
2423
Douglas Gregorc9490c02009-04-16 22:23:12 +00002424 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002425 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002426 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002427 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002428 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2429 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002430 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002431
Douglas Gregor25123082009-04-22 22:34:57 +00002432 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002433 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002434 return Offset;
2435}
2436
Sebastian Redla4232eb2010-08-18 23:56:21 +00002437void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002438 using namespace llvm;
2439 RecordData Record;
2440
2441 // Write the type offsets array
2442 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002443 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2447 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2448 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002449 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002450 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002451 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002452 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002453
2454 // Write the declaration offsets array
2455 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002456 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2460 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2461 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002462 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002463 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002464 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002465 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002466}
2467
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002468void ASTWriter::WriteFileDeclIDsMap() {
2469 using namespace llvm;
2470 RecordData Record;
2471
2472 // Join the vectors of DeclIDs from all files.
2473 SmallVector<DeclID, 256> FileSortedIDs;
2474 for (FileDeclIDsTy::iterator
2475 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2476 DeclIDInFileInfo &Info = *FI->second;
2477 Info.FirstDeclIndex = FileSortedIDs.size();
2478 for (LocDeclIDsTy::iterator
2479 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2480 FileSortedIDs.push_back(DI->second);
2481 }
2482
2483 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2484 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002486 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2487 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2488 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002489 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002490 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2491}
2492
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002493void ASTWriter::WriteComments() {
2494 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002495 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002496 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002497 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2498 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002499 I != E; ++I) {
2500 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002501 AddSourceRange((*I)->getSourceRange(), Record);
2502 Record.push_back((*I)->getKind());
2503 Record.push_back((*I)->isTrailingComment());
2504 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002505 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2506 }
2507 Stream.ExitBlock();
2508}
2509
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002510//===----------------------------------------------------------------------===//
2511// Global Method Pool and Selector Serialization
2512//===----------------------------------------------------------------------===//
2513
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002514namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002515// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002516class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002517 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002518
2519public:
2520 typedef Selector key_type;
2521 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002522
Sebastian Redl5d050072010-08-04 17:20:04 +00002523 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002524 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002525 ObjCMethodList Instance, Factory;
2526 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002527 typedef const data_type& data_type_ref;
2528
Sebastian Redl3397c552010-08-18 23:56:27 +00002529 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002530
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002531 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002532 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002533 }
Mike Stump1eb44332009-09-09 15:08:12 +00002534
2535 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002536 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002537 data_type_ref Methods) {
2538 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2539 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002540 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2541 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002542 Method = Method->Next)
2543 if (Method->Method)
2544 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002545 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002546 Method = Method->Next)
2547 if (Method->Method)
2548 DataLen += 4;
2549 clang::io::Emit16(Out, DataLen);
2550 return std::make_pair(KeyLen, DataLen);
2551 }
Mike Stump1eb44332009-09-09 15:08:12 +00002552
Chris Lattner5f9e2722011-07-23 10:55:15 +00002553 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002554 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002555 assert((Start >> 32) == 0 && "Selector key offset too large");
2556 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002557 unsigned N = Sel.getNumArgs();
2558 clang::io::Emit16(Out, N);
2559 if (N == 0)
2560 N = 1;
2561 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002562 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002563 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2564 }
Mike Stump1eb44332009-09-09 15:08:12 +00002565
Chris Lattner5f9e2722011-07-23 10:55:15 +00002566 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002567 data_type_ref Methods, unsigned DataLen) {
2568 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002569 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002570 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002571 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002572 Method = Method->Next)
2573 if (Method->Method)
2574 ++NumInstanceMethods;
2575
2576 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002577 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002578 Method = Method->Next)
2579 if (Method->Method)
2580 ++NumFactoryMethods;
2581
2582 clang::io::Emit16(Out, NumInstanceMethods);
2583 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002584 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002585 Method = Method->Next)
2586 if (Method->Method)
2587 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002588 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002589 Method = Method->Next)
2590 if (Method->Method)
2591 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002592
2593 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002594 }
2595};
2596} // end anonymous namespace
2597
Sebastian Redl059612d2010-08-03 21:58:15 +00002598/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002599///
2600/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002601/// in an on-disk hash table indexed by the selector. The hash table also
2602/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002603void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002604 using namespace llvm;
2605
Sebastian Redl059612d2010-08-03 21:58:15 +00002606 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002607 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002608 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002609 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002610 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002611 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002612 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002613 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Sebastian Redl059612d2010-08-03 21:58:15 +00002615 // Create the on-disk hash table representation. We walk through every
2616 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002617 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002618 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002619 I = SelectorIDs.begin(), E = SelectorIDs.end();
2620 I != E; ++I) {
2621 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002622 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002623 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002624 I->second,
2625 ObjCMethodList(),
2626 ObjCMethodList()
2627 };
2628 if (F != SemaRef.MethodPool.end()) {
2629 Data.Instance = F->second.first;
2630 Data.Factory = F->second.second;
2631 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002632 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002633 // changed.
2634 if (Chain && I->second < FirstSelectorID) {
2635 // Selector already exists. Did it change?
2636 bool changed = false;
2637 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2638 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002639 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002640 changed = true;
2641 }
2642 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2643 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002644 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002645 changed = true;
2646 }
2647 if (!changed)
2648 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002649 } else if (Data.Instance.Method || Data.Factory.Method) {
2650 // A new method pool entry.
2651 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002652 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002653 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002654 }
2655
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002656 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002657 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002658 uint32_t BucketOffset;
2659 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002660 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002661 llvm::raw_svector_ostream Out(MethodPool);
2662 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002663 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002664 BucketOffset = Generator.Emit(Out, Trait);
2665 }
2666
2667 // Create a blob abbreviation
2668 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002669 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002670 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002671 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002672 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2673 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2674
Douglas Gregor83941df2009-04-25 17:48:32 +00002675 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002676 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002677 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002678 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002679 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002680 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002681
2682 // Create a blob abbreviation for the selector table offsets.
2683 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002684 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002685 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2688 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2689
2690 // Write the selector offsets table.
2691 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002692 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002693 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002694 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002695 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002696 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002697 }
2698}
2699
Sebastian Redl3397c552010-08-18 23:56:27 +00002700/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002701void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002702 using namespace llvm;
2703 if (SemaRef.ReferencedSelectors.empty())
2704 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002705
Fariborz Jahanian32019832010-07-23 19:11:11 +00002706 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002707
Sebastian Redl3397c552010-08-18 23:56:27 +00002708 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002709 // very tricky to fix, and given that @selector shouldn't really appear in
2710 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002711 for (DenseMap<Selector, SourceLocation>::iterator S =
2712 SemaRef.ReferencedSelectors.begin(),
2713 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2714 Selector Sel = (*S).first;
2715 SourceLocation Loc = (*S).second;
2716 AddSelectorRef(Sel, Record);
2717 AddSourceLocation(Loc, Record);
2718 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002719 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002720}
2721
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002722//===----------------------------------------------------------------------===//
2723// Identifier Table Serialization
2724//===----------------------------------------------------------------------===//
2725
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002726namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002727class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002728 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002729 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002730 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002731 bool IsModule;
2732
Douglas Gregora92193e2009-04-28 21:18:29 +00002733 /// \brief Determines whether this is an "interesting" identifier
2734 /// that needs a full IdentifierInfo structure written into the hash
2735 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002736 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002737 if (II->isPoisoned() ||
2738 II->isExtensionToken() ||
2739 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002740 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002741 II->getFETokenInfo<void>())
2742 return true;
2743
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002744 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002745 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002746
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002747 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002748 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002749 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002750
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002751 if (Macro || (Macro = PP.getMacroDirectiveHistory(II)))
2752 return !Macro->getInfo()->isBuiltinMacro() &&
2753 (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002754
2755 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002756 }
2757
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002758public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002759 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002760 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002762 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002763 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002764
Douglas Gregoreee242f2011-10-27 09:33:13 +00002765 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2766 IdentifierResolver &IdResolver, bool IsModule)
2767 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002768
2769 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002770 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002771 }
Mike Stump1eb44332009-09-09 15:08:12 +00002772
2773 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002774 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002775 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002776 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002777 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002778 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002779 DataLen += 2; // 2 bytes for builtin ID
2780 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002781 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002782 for (MacroDirective *M = Macro; M; M = M->getPrevious()) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002783 if (Writer.getMacroRef(M) != 0)
2784 DataLen += 4;
2785 }
2786
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002787 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002788 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002789
Douglas Gregoreee242f2011-10-27 09:33:13 +00002790 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2791 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002792 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002793 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002794 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002795 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002796 // We emit the key length after the data length so that every
2797 // string is preceded by a 16-bit length. This matches the PTH
2798 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002799 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002800 return std::make_pair(KeyLen, DataLen);
2801 }
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Chris Lattner5f9e2722011-07-23 10:55:15 +00002803 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002804 unsigned KeyLen) {
2805 // Record the location of the key data. This is used when generating
2806 // the mapping from persistent IDs to strings.
2807 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002808 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002809 }
Mike Stump1eb44332009-09-09 15:08:12 +00002810
Douglas Gregor7143aab2011-09-01 17:04:32 +00002811 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002812 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002813 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002814 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002815 clang::io::Emit32(Out, ID << 1);
2816 return;
2817 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002818
Douglas Gregora92193e2009-04-28 21:18:29 +00002819 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002820 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2821 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2822 clang::io::Emit16(Out, Bits);
2823 Bits = 0;
2824 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002825 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002826 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2827 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002828 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002829 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002830 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002831
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002832 if (HadMacroDefinition) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002833 // Write all of the macro IDs associated with this identifier.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002834 for (MacroDirective *M = Macro; M; M = M->getPrevious()) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002835 if (MacroID ID = Writer.getMacroRef(M))
2836 clang::io::Emit32(Out, ID);
2837 }
2838
2839 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002840 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002841
Douglas Gregor668c1a42009-04-21 22:25:48 +00002842 // Emit the declaration IDs in reverse order, because the
2843 // IdentifierResolver provides the declarations as they would be
2844 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002845 // "stat"), but the ASTReader adds declarations to the end of the list
2846 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002847 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002848 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2849 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002850 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002851 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002852 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002853 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002854 }
2855};
2856} // end anonymous namespace
2857
Sebastian Redl3397c552010-08-18 23:56:27 +00002858/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002859///
2860/// The identifier table consists of a blob containing string data
2861/// (the actual identifiers themselves) and a separate "offsets" index
2862/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002863void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2864 IdentifierResolver &IdResolver,
2865 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002866 using namespace llvm;
2867
2868 // Create and write out the blob that contains the identifier
2869 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002870 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002871 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002872 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002873
Douglas Gregor92b059e2009-04-28 20:33:11 +00002874 // Look for any identifiers that were named while processing the
2875 // headers, but are otherwise not needed. We add these to the hash
2876 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002877 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002878 // file.
2879 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2880 IDEnd = PP.getIdentifierTable().end();
2881 ID != IDEnd; ++ID)
2882 getIdentifierRef(ID->second);
2883
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002884 // Create the on-disk hash table representation. We only store offsets
2885 // for identifiers that appear here for the first time.
2886 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002887 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002888 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2889 ID != IDEnd; ++ID) {
2890 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002891 if (!Chain || !ID->first->isFromAST() ||
2892 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00002893 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002894 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002895 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002896
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002897 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002898 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002899 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002900 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002901 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002902 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002903 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002904 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002905 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002906 }
2907
2908 // Create a blob abbreviation
2909 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002910 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002913 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002914
2915 // Write the identifier table
2916 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002917 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002918 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002919 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002920 }
2921
2922 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002923 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002924 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2928 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2929
Douglas Gregor2d1ece82013-02-08 21:30:59 +00002930#ifndef NDEBUG
2931 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
2932 assert(IdentifierOffsets[I] && "Missing identifier offset?");
2933#endif
2934
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002935 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002936 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002937 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002938 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002939 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002940 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002941}
2942
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002943//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002944// DeclContext's Name Lookup Table Serialization
2945//===----------------------------------------------------------------------===//
2946
2947namespace {
2948// Trait used for the on-disk hash table used in the method pool.
2949class ASTDeclContextNameLookupTrait {
2950 ASTWriter &Writer;
2951
2952public:
2953 typedef DeclarationName key_type;
2954 typedef key_type key_type_ref;
2955
2956 typedef DeclContext::lookup_result data_type;
2957 typedef const data_type& data_type_ref;
2958
2959 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2960
2961 unsigned ComputeHash(DeclarationName Name) {
2962 llvm::FoldingSetNodeID ID;
2963 ID.AddInteger(Name.getNameKind());
2964
2965 switch (Name.getNameKind()) {
2966 case DeclarationName::Identifier:
2967 ID.AddString(Name.getAsIdentifierInfo()->getName());
2968 break;
2969 case DeclarationName::ObjCZeroArgSelector:
2970 case DeclarationName::ObjCOneArgSelector:
2971 case DeclarationName::ObjCMultiArgSelector:
2972 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2973 break;
2974 case DeclarationName::CXXConstructorName:
2975 case DeclarationName::CXXDestructorName:
2976 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002977 break;
2978 case DeclarationName::CXXOperatorName:
2979 ID.AddInteger(Name.getCXXOverloadedOperator());
2980 break;
2981 case DeclarationName::CXXLiteralOperatorName:
2982 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2983 case DeclarationName::CXXUsingDirective:
2984 break;
2985 }
2986
2987 return ID.ComputeHash();
2988 }
2989
2990 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002991 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002992 data_type_ref Lookup) {
2993 unsigned KeyLen = 1;
2994 switch (Name.getNameKind()) {
2995 case DeclarationName::Identifier:
2996 case DeclarationName::ObjCZeroArgSelector:
2997 case DeclarationName::ObjCOneArgSelector:
2998 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002999 case DeclarationName::CXXLiteralOperatorName:
3000 KeyLen += 4;
3001 break;
3002 case DeclarationName::CXXOperatorName:
3003 KeyLen += 1;
3004 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003005 case DeclarationName::CXXConstructorName:
3006 case DeclarationName::CXXDestructorName:
3007 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003008 case DeclarationName::CXXUsingDirective:
3009 break;
3010 }
3011 clang::io::Emit16(Out, KeyLen);
3012
3013 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003014 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003015 clang::io::Emit16(Out, DataLen);
3016
3017 return std::make_pair(KeyLen, DataLen);
3018 }
3019
Chris Lattner5f9e2722011-07-23 10:55:15 +00003020 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003021 using namespace clang::io;
3022
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003023 Emit8(Out, Name.getNameKind());
3024 switch (Name.getNameKind()) {
3025 case DeclarationName::Identifier:
3026 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003027 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003028 case DeclarationName::ObjCZeroArgSelector:
3029 case DeclarationName::ObjCOneArgSelector:
3030 case DeclarationName::ObjCMultiArgSelector:
3031 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003032 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003033 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003034 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3035 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003036 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003037 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003038 case DeclarationName::CXXLiteralOperatorName:
3039 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003040 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003041 case DeclarationName::CXXConstructorName:
3042 case DeclarationName::CXXDestructorName:
3043 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003044 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003045 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003046 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003047
3048 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003049 }
3050
Chris Lattner5f9e2722011-07-23 10:55:15 +00003051 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003052 data_type Lookup, unsigned DataLen) {
3053 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003054 clang::io::Emit16(Out, Lookup.size());
3055 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3056 I != E; ++I)
3057 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003058
3059 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3060 }
3061};
3062} // end anonymous namespace
3063
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003064/// \brief Write the block containing all of the declaration IDs
3065/// visible from the given DeclContext.
3066///
3067/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003068/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003069uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3070 DeclContext *DC) {
3071 if (DC->getPrimaryContext() != DC)
3072 return 0;
3073
3074 // Since there is no name lookup into functions or methods, don't bother to
3075 // build a visible-declarations table for these entities.
3076 if (DC->isFunctionOrMethod())
3077 return 0;
3078
3079 // If not in C++, we perform name lookup for the translation unit via the
3080 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3081 // FIXME: In C++ we need the visible declarations in order to "see" the
3082 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00003083 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003084 return 0;
3085
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003086 // Serialize the contents of the mapping used for lookup. Note that,
3087 // although we have two very different code paths, the serialized
3088 // representation is the same for both cases: a declaration name,
3089 // followed by a size, followed by references to the visible
3090 // declarations that have that name.
3091 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003092 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003093 if (!Map || Map->empty())
3094 return 0;
3095
3096 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3097 ASTDeclContextNameLookupTrait Trait(*this);
3098
3099 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003100 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003101 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003102 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3103 D != DEnd; ++D) {
3104 DeclarationName Name = D->first;
3105 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003106 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003107 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3108 // Hash all conversion function names to the same name. The actual
3109 // type information in conversion function name is not used in the
3110 // key (since such type information is not stable across different
3111 // modules), so the intended effect is to coalesce all of the conversion
3112 // functions under a single key.
3113 if (!ConversionName)
3114 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003115 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003116 continue;
3117 }
3118
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003119 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003120 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003121 }
3122
Douglas Gregore5a54b62011-08-30 20:49:19 +00003123 // Add the conversion functions
3124 if (!ConversionDecls.empty()) {
3125 Generator.insert(ConversionName,
3126 DeclContext::lookup_result(ConversionDecls.begin(),
3127 ConversionDecls.end()),
3128 Trait);
3129 }
3130
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003131 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003132 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003133 uint32_t BucketOffset;
3134 {
3135 llvm::raw_svector_ostream Out(LookupTable);
3136 // Make sure that no bucket is at offset 0
3137 clang::io::Emit32(Out, 0);
3138 BucketOffset = Generator.Emit(Out, Trait);
3139 }
3140
3141 // Write the lookup table
3142 RecordData Record;
3143 Record.push_back(DECL_CONTEXT_VISIBLE);
3144 Record.push_back(BucketOffset);
3145 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3146 LookupTable.str());
3147
3148 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3149 ++NumVisibleDeclContexts;
3150 return Offset;
3151}
3152
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003153/// \brief Write an UPDATE_VISIBLE block for the given context.
3154///
3155/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3156/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003157/// (in C++), for namespaces, and for classes with forward-declared unscoped
3158/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003159void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003160 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3161 if (!Map || Map->empty())
3162 return;
3163
3164 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3165 ASTDeclContextNameLookupTrait Trait(*this);
3166
3167 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003168 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3169 D != DEnd; ++D) {
3170 DeclarationName Name = D->first;
3171 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003172 // For any name that appears in this table, the results are complete, i.e.
3173 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003174 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003175 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003176 }
3177
3178 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003179 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003180 uint32_t BucketOffset;
3181 {
3182 llvm::raw_svector_ostream Out(LookupTable);
3183 // Make sure that no bucket is at offset 0
3184 clang::io::Emit32(Out, 0);
3185 BucketOffset = Generator.Emit(Out, Trait);
3186 }
3187
3188 // Write the lookup table
3189 RecordData Record;
3190 Record.push_back(UPDATE_VISIBLE);
3191 Record.push_back(getDeclID(cast<Decl>(DC)));
3192 Record.push_back(BucketOffset);
3193 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3194}
3195
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003196/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3197void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3198 RecordData Record;
3199 Record.push_back(Opts.fp_contract);
3200 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3201}
3202
3203/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3204void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003205 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003206 return;
3207
3208 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3209 RecordData Record;
3210#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3211#include "clang/Basic/OpenCLExtensions.def"
3212 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3213}
3214
Douglas Gregor2171bf12012-01-15 16:58:34 +00003215void ASTWriter::WriteRedeclarations() {
3216 RecordData LocalRedeclChains;
3217 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3218
3219 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3220 Decl *First = Redeclarations[I];
3221 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3222
3223 Decl *MostRecent = First->getMostRecentDecl();
3224
3225 // If we only have a single declaration, there is no point in storing
3226 // a redeclaration chain.
3227 if (First == MostRecent)
3228 continue;
3229
3230 unsigned Offset = LocalRedeclChains.size();
3231 unsigned Size = 0;
3232 LocalRedeclChains.push_back(0); // Placeholder for the size.
3233
3234 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003235 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003236 Prev = Prev->getPreviousDecl()) {
3237 if (!Prev->isFromASTFile()) {
3238 AddDeclRef(Prev, LocalRedeclChains);
3239 ++Size;
3240 }
3241 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003242
3243 if (!First->isFromASTFile() && Chain) {
3244 Decl *FirstFromAST = MostRecent;
3245 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3246 if (Prev->isFromASTFile())
3247 FirstFromAST = Prev;
3248 }
3249
3250 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3251 }
3252
Douglas Gregor2171bf12012-01-15 16:58:34 +00003253 LocalRedeclChains[Offset] = Size;
3254
3255 // Reverse the set of local redeclarations, so that we store them in
3256 // order (since we found them in reverse order).
3257 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3258
Douglas Gregoraa945902013-02-18 15:53:43 +00003259 // Add the mapping from the first ID from the AST to the set of local
3260 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003261 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3262 LocalRedeclsMap.push_back(Info);
3263
3264 assert(N == Redeclarations.size() &&
3265 "Deserialized a declaration we shouldn't have");
3266 }
3267
3268 if (LocalRedeclChains.empty())
3269 return;
3270
3271 // Sort the local redeclarations map by the first declaration ID,
3272 // since the reader will be performing binary searches on this information.
3273 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3274
3275 // Emit the local redeclarations map.
3276 using namespace llvm;
3277 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3278 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3281 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3282
3283 RecordData Record;
3284 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3285 Record.push_back(LocalRedeclsMap.size());
3286 Stream.EmitRecordWithBlob(AbbrevID, Record,
3287 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3288 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3289
3290 // Emit the redeclaration chains.
3291 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3292}
3293
Douglas Gregorcff9f262012-01-27 01:47:08 +00003294void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003295 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003296 RecordData Categories;
3297
3298 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3299 unsigned Size = 0;
3300 unsigned StartIndex = Categories.size();
3301
3302 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3303
3304 // Allocate space for the size.
3305 Categories.push_back(0);
3306
3307 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003308 for (ObjCInterfaceDecl::known_categories_iterator
3309 Cat = Class->known_categories_begin(),
3310 CatEnd = Class->known_categories_end();
3311 Cat != CatEnd; ++Cat, ++Size) {
3312 assert(getDeclID(*Cat) != 0 && "Bogus category");
3313 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003314 }
3315
3316 // Update the size.
3317 Categories[StartIndex] = Size;
3318
3319 // Record this interface -> category map.
3320 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3321 CategoriesMap.push_back(CatInfo);
3322 }
3323
3324 // Sort the categories map by the definition ID, since the reader will be
3325 // performing binary searches on this information.
3326 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3327
3328 // Emit the categories map.
3329 using namespace llvm;
3330 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3331 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3334 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3335
3336 RecordData Record;
3337 Record.push_back(OBJC_CATEGORIES_MAP);
3338 Record.push_back(CategoriesMap.size());
3339 Stream.EmitRecordWithBlob(AbbrevID, Record,
3340 reinterpret_cast<char*>(CategoriesMap.data()),
3341 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3342
3343 // Emit the category lists.
3344 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3345}
3346
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003347void ASTWriter::WriteMergedDecls() {
3348 if (!Chain || Chain->MergedDecls.empty())
3349 return;
3350
3351 RecordData Record;
3352 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3353 IEnd = Chain->MergedDecls.end();
3354 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003355 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003356 : getDeclID(I->first);
3357 assert(CanonID && "Merged declaration not known?");
3358
3359 Record.push_back(CanonID);
3360 Record.push_back(I->second.size());
3361 Record.append(I->second.begin(), I->second.end());
3362 }
3363 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3364}
3365
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003366//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003367// General Serialization Routines
3368//===----------------------------------------------------------------------===//
3369
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003370/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003371void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3372 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003373 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003374 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3375 e = Attrs.end(); i != e; ++i){
3376 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003377 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003378 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003379
Sean Huntcf807c42010-08-18 23:23:40 +00003380#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003381
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003382 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003383}
3384
Chris Lattner5f9e2722011-07-23 10:55:15 +00003385void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003386 Record.push_back(Str.size());
3387 Record.insert(Record.end(), Str.begin(), Str.end());
3388}
3389
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003390void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3391 RecordDataImpl &Record) {
3392 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003393 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003394 Record.push_back(*Minor + 1);
3395 else
3396 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003397 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003398 Record.push_back(*Subminor + 1);
3399 else
3400 Record.push_back(0);
3401}
3402
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003403/// \brief Note that the identifier II occurs at the given offset
3404/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003405void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003406 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003407 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003408 // up earlier in the chain and thus don't need an offset.
3409 if (ID >= FirstIdentID)
3410 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003411}
3412
Douglas Gregor83941df2009-04-25 17:48:32 +00003413/// \brief Note that the selector Sel occurs at the given offset
3414/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003415void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003416 unsigned ID = SelectorIDs[Sel];
3417 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003418 // Don't record offsets for selectors that are also available in a different
3419 // file.
3420 if (ID < FirstSelectorID)
3421 return;
3422 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003423}
3424
Sebastian Redla4232eb2010-08-18 23:56:21 +00003425ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003426 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003427 WritingAST(false), DoneWritingDeclsAndTypes(false),
3428 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003429 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003430 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003431 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3432 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003433 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3434 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003435 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003436 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003437 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003438 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003439 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003440 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003441 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3442 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3443 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003444 DeclTypedefAbbrev(0),
3445 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3446 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003447{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003448}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003449
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003450ASTWriter::~ASTWriter() {
3451 for (FileDeclIDsTy::iterator
3452 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3453 delete I->second;
3454}
3455
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003456void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003457 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003458 Module *WritingModule, StringRef isysroot,
3459 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003460 WritingAST = true;
3461
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003462 ASTHasCompilerErrors = hasErrors;
3463
Douglas Gregor2cf26342009-04-09 22:27:44 +00003464 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003465 Stream.Emit((unsigned)'C', 8);
3466 Stream.Emit((unsigned)'P', 8);
3467 Stream.Emit((unsigned)'C', 8);
3468 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003470 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003471
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003472 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003473 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003474 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003475 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003476 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003477 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003478 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003479
3480 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003481}
3482
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003483template<typename Vector>
3484static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3485 ASTWriter::RecordData &Record) {
3486 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3487 I != E; ++I) {
3488 Writer.AddDeclRef(*I, Record);
3489 }
3490}
3491
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003492void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003493 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003494 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003495 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003496 using namespace llvm;
3497
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003498 bool isModule = WritingModule != 0;
3499
Douglas Gregorecc2c092011-12-01 22:20:10 +00003500 // Make sure that the AST reader knows to finalize itself.
3501 if (Chain)
3502 Chain->finalizeForWriting();
3503
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003504 ASTContext &Context = SemaRef.Context;
3505 Preprocessor &PP = SemaRef.PP;
3506
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003507 // Set up predefined declaration IDs.
3508 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003509 if (Context.ObjCIdDecl)
3510 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003511 if (Context.ObjCSelDecl)
3512 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003513 if (Context.ObjCClassDecl)
3514 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003515 if (Context.ObjCProtocolClassDecl)
3516 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003517 if (Context.Int128Decl)
3518 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3519 if (Context.UInt128Decl)
3520 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003521 if (Context.ObjCInstanceTypeDecl)
3522 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003523 if (Context.BuiltinVaListDecl)
3524 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3525
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003526 if (!Chain) {
3527 // Make sure that we emit IdentifierInfos (and any attached
3528 // declarations) for builtins. We don't need to do this when we're
3529 // emitting chained PCH files, because all of the builtins will be
3530 // in the original PCH file.
3531 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003532 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003533 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003534 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003535 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003536 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3537 getIdentifierRef(&Table.get(BuiltinNames[I]));
3538 }
3539
Douglas Gregoreee242f2011-10-27 09:33:13 +00003540 // If there are any out-of-date identifiers, bring them up to date.
3541 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003542 // Find out-of-date identifiers.
3543 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003544 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3545 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003546 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003547 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003548 OutOfDate.push_back(ID->second);
3549 }
3550
3551 // Update the out-of-date identifiers.
3552 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3553 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3554 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003555 }
3556
Chris Lattner63d65f82009-09-08 18:19:27 +00003557 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003558 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003559 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003560 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003561 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003562
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003563 // Build a record containing all of the file scoped decls in this file.
3564 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003565 if (!isModule)
3566 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3567 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003568
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003569 // Build a record containing all of the delegating constructors we still need
3570 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003571 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003572 if (!isModule)
3573 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003574
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003575 // Write the set of weak, undeclared identifiers. We always write the
3576 // entire table, since later PCH files in a PCH chain are only interested in
3577 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003578 RecordData WeakUndeclaredIdentifiers;
3579 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003580 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003581 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3582 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3583 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3584 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3585 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3586 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3587 }
3588 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003589
Richard Smith5ea6ef42013-01-10 23:43:47 +00003590 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003591 // declarations in this header file. Generally, this record will be
3592 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003593 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003594 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003595 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003596 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003597 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3598 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003599 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003600 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003601 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003602 }
3603
Douglas Gregorb81c1702009-04-27 20:06:05 +00003604 // Build a record containing all of the ext_vector declarations.
3605 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003606 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003607
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003608 // Build a record containing all of the VTable uses information.
3609 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003610 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003611 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3612 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3613 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3614 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3615 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003616 }
3617
3618 // Build a record containing all of dynamic classes declarations.
3619 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003620 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003621
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003622 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003623 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003624 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003625 I = SemaRef.PendingInstantiations.begin(),
3626 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3627 AddDeclRef(I->first, PendingInstantiations);
3628 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003629 }
3630 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3631 "There are local ones at end of translation unit!");
3632
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003633 // Build a record containing some declaration references.
3634 RecordData SemaDeclRefs;
3635 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3636 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3637 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3638 }
3639
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003640 RecordData CUDASpecialDeclRefs;
3641 if (Context.getcudaConfigureCallDecl()) {
3642 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3643 }
3644
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003645 // Build a record containing all of the known namespaces.
3646 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003647 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003648 I = SemaRef.KnownNamespaces.begin(),
3649 IEnd = SemaRef.KnownNamespaces.end();
3650 I != IEnd; ++I) {
3651 if (!I->second)
3652 AddDeclRef(I->first, KnownNamespaces);
3653 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003654
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003655 // Build a record of all used, undefined objects that require definitions.
3656 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003657
3658 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003659 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003660 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3661 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003662 AddDeclRef(I->first, UndefinedButUsed);
3663 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003664 }
3665
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003666 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003667 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003668
Sebastian Redl3397c552010-08-18 23:56:27 +00003669 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003670 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003671 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003672
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003673 // This is so that older clang versions, before the introduction
3674 // of the control block, can read and reject the newer PCH format.
3675 Record.clear();
3676 Record.push_back(VERSION_MAJOR);
3677 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3678
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003679 // Create a lexical update block containing all of the declarations in the
3680 // translation unit that do not come from other AST files.
3681 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3682 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3683 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3684 E = TU->noload_decls_end();
3685 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003686 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003687 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003688 }
3689
3690 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3691 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3692 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3693 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3694 Record.clear();
3695 Record.push_back(TU_UPDATE_LEXICAL);
3696 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3697 data(NewGlobalDecls));
3698
3699 // And a visible updates block for the translation unit.
3700 Abv = new llvm::BitCodeAbbrev();
3701 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3702 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3703 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3704 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3705 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3706 WriteDeclContextVisibleUpdate(TU);
3707
3708 // If the translation unit has an anonymous namespace, and we don't already
3709 // have an update block for it, write it as an update block.
3710 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3711 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3712 if (Record.empty()) {
3713 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003714 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003715 }
3716 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003717
3718 // Make sure visible decls, added to DeclContexts previously loaded from
3719 // an AST file, are registered for serialization.
3720 for (SmallVector<const Decl *, 16>::iterator
3721 I = UpdatingVisibleDecls.begin(),
3722 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3723 GetDeclRef(*I);
3724 }
3725
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003726 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003727 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003728
Douglas Gregora119da02011-08-02 16:26:37 +00003729 // Form the record of special types.
3730 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003731 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003732 AddTypeRef(Context.getFILEType(), SpecialTypes);
3733 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3734 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3735 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3736 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003737 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003738 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003739
Douglas Gregor366809a2009-04-26 03:49:13 +00003740 // Keep writing types and declarations until all types and
3741 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003742 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003743 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003744 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3745 E = DeclsToRewrite.end();
3746 I != E; ++I)
3747 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003748 while (!DeclTypesToEmit.empty()) {
3749 DeclOrType DOT = DeclTypesToEmit.front();
3750 DeclTypesToEmit.pop();
3751 if (DOT.isType())
3752 WriteType(DOT.getType());
3753 else
3754 WriteDecl(Context, DOT.getDecl());
3755 }
3756 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003757
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003758 DoneWritingDeclsAndTypes = true;
3759
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003760 WriteFileDeclIDsMap();
3761 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003762 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003763
3764 if (Chain) {
3765 // Write the mapping information describing our module dependencies and how
3766 // each of those modules were mapped into our own offset/ID space, so that
3767 // the reader can build the appropriate mapping to its own offset/ID space.
3768 // The map consists solely of a blob with the following format:
3769 // *(module-name-len:i16 module-name:len*i8
3770 // source-location-offset:i32
3771 // identifier-id:i32
3772 // preprocessed-entity-id:i32
3773 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003774 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003775 // selector-id:i32
3776 // declaration-id:i32
3777 // c++-base-specifiers-id:i32
3778 // type-id:i32)
3779 //
3780 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3781 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3783 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003784 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003785 {
3786 llvm::raw_svector_ostream Out(Buffer);
3787 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003788 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003789 M != MEnd; ++M) {
3790 StringRef FileName = (*M)->FileName;
3791 io::Emit16(Out, FileName.size());
3792 Out.write(FileName.data(), FileName.size());
3793 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3794 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003795 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003796 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003797 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003798 io::Emit32(Out, (*M)->BaseSelectorID);
3799 io::Emit32(Out, (*M)->BaseDeclID);
3800 io::Emit32(Out, (*M)->BaseTypeIndex);
3801 }
3802 }
3803 Record.clear();
3804 Record.push_back(MODULE_OFFSET_MAP);
3805 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3806 Buffer.data(), Buffer.size());
3807 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003808 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003809 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003810 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003811 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003812 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003813 WriteFPPragmaOptions(SemaRef.getFPOptions());
3814 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003815
Sebastian Redl1476ed42010-07-16 16:36:56 +00003816 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003817 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003818
Anders Carlssonc8505782011-03-06 18:41:18 +00003819 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003820
Douglas Gregore209e502011-12-06 01:10:29 +00003821 // If we're emitting a module, write out the submodule information.
3822 if (WritingModule)
3823 WriteSubmodules(WritingModule);
3824
Douglas Gregora119da02011-08-02 16:26:37 +00003825 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3826
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003827 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003828 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003829 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003830
3831 // Write the record containing tentative definitions.
3832 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003833 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003834
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003835 // Write the record containing unused file scoped decls.
3836 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003837 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003838
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003839 // Write the record containing weak undeclared identifiers.
3840 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003841 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003842 WeakUndeclaredIdentifiers);
3843
Richard Smith5ea6ef42013-01-10 23:43:47 +00003844 // Write the record containing locally-scoped extern "C" definitions.
3845 if (!LocallyScopedExternCDecls.empty())
3846 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
3847 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003848
3849 // Write the record containing ext_vector type names.
3850 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003851 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003852
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003853 // Write the record containing VTable uses information.
3854 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003855 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003856
3857 // Write the record containing dynamic classes declarations.
3858 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003859 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003860
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003861 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003862 if (!PendingInstantiations.empty())
3863 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003864
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003865 // Write the record containing declaration references of Sema.
3866 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003867 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003868
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003869 // Write the record containing CUDA-specific declaration references.
3870 if (!CUDASpecialDeclRefs.empty())
3871 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003872
3873 // Write the delegating constructors.
3874 if (!DelegatingCtorDecls.empty())
3875 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003876
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003877 // Write the known namespaces.
3878 if (!KnownNamespaces.empty())
3879 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00003880
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003881 // Write the undefined internal functions and variables, and inline functions.
3882 if (!UndefinedButUsed.empty())
3883 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003884
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003885 // Write the visible updates to DeclContexts.
3886 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3887 I = UpdatedDeclContexts.begin(),
3888 E = UpdatedDeclContexts.end();
3889 I != E; ++I)
3890 WriteDeclContextVisibleUpdate(*I);
3891
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003892 if (!WritingModule) {
3893 // Write the submodules that were imported, if any.
3894 RecordData ImportedModules;
3895 for (ASTContext::import_iterator I = Context.local_import_begin(),
3896 IEnd = Context.local_import_end();
3897 I != IEnd; ++I) {
3898 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3899 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3900 }
3901 if (!ImportedModules.empty()) {
3902 // Sort module IDs.
3903 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3904
3905 // Unique module IDs.
3906 ImportedModules.erase(std::unique(ImportedModules.begin(),
3907 ImportedModules.end()),
3908 ImportedModules.end());
3909
3910 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3911 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003912 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003913
3914 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003915 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003916 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003917 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00003918 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003919 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003920
Douglas Gregor3e1af842009-04-17 22:13:46 +00003921 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003922 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003923 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003924 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003925 Record.push_back(NumLexicalDeclContexts);
3926 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003927 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003928 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003929}
3930
Douglas Gregora8235d62012-10-09 23:05:51 +00003931void ASTWriter::WriteMacroUpdates() {
3932 if (MacroUpdates.empty())
3933 return;
3934
3935 RecordData Record;
3936 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3937 E = MacroUpdates.end();
3938 I != E; ++I) {
3939 addMacroRef(I->first, Record);
3940 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003941 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003942 }
3943 Stream.EmitRecord(MACRO_UPDATES, Record);
3944}
3945
Douglas Gregor61c5e342011-09-17 00:05:03 +00003946/// \brief Go through the declaration update blocks and resolve declaration
3947/// pointers into declaration IDs.
3948void ASTWriter::ResolveDeclUpdatesBlocks() {
3949 for (DeclUpdateMap::iterator
3950 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3951 const Decl *D = I->first;
3952 UpdateRecord &URec = I->second;
3953
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003954 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003955 continue; // The decl will be written completely
3956
3957 unsigned Idx = 0, N = URec.size();
3958 while (Idx < N) {
3959 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003960 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3961 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3962 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3963 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3964 ++Idx;
3965 break;
3966
3967 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3968 ++Idx;
3969 break;
3970 }
3971 }
3972 }
3973}
3974
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003975void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003976 if (DeclUpdates.empty())
3977 return;
3978
3979 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003980 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003981 for (DeclUpdateMap::iterator
3982 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3983 const Decl *D = I->first;
3984 UpdateRecord &URec = I->second;
3985
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003986 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003987 continue; // The decl will be written completely,no need to store updates.
3988
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003989 uint64_t Offset = Stream.GetCurrentBitNo();
3990 Stream.EmitRecord(DECL_UPDATES, URec);
3991
3992 OffsetsRecord.push_back(GetDeclRef(D));
3993 OffsetsRecord.push_back(Offset);
3994 }
3995 Stream.ExitBlock();
3996 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3997}
3998
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003999void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004000 if (ReplacedDecls.empty())
4001 return;
4002
4003 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004004 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004005 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004006 Record.push_back(I->ID);
4007 Record.push_back(I->Offset);
4008 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004009 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004010 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004011}
4012
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004013void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004014 Record.push_back(Loc.getRawEncoding());
4015}
4016
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004017void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004018 AddSourceLocation(Range.getBegin(), Record);
4019 AddSourceLocation(Range.getEnd(), Record);
4020}
4021
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004022void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004023 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004024 const uint64_t *Words = Value.getRawData();
4025 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004026}
4027
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004028void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004029 Record.push_back(Value.isUnsigned());
4030 AddAPInt(Value, Record);
4031}
4032
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004033void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004034 AddAPInt(Value.bitcastToAPInt(), Record);
4035}
4036
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004037void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004038 Record.push_back(getIdentifierRef(II));
4039}
4040
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004041void ASTWriter::addMacroRef(MacroDirective *MD, RecordDataImpl &Record) {
4042 Record.push_back(getMacroRef(MD));
Douglas Gregora8235d62012-10-09 23:05:51 +00004043}
4044
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004045IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004046 if (II == 0)
4047 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004048
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004049 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004050 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004051 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004052 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004053}
4054
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004055MacroID ASTWriter::getMacroRef(MacroDirective *MD) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004056 // Don't emit builtin macros like __LINE__ to the AST file unless they
4057 // have been redefined by the header (in which case they are not
4058 // isBuiltinMacro).
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004059 if (MD == 0 || MD->getInfo()->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004060 return 0;
4061
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004062 MacroID &ID = MacroIDs[MD];
Douglas Gregora8235d62012-10-09 23:05:51 +00004063 if (ID == 0)
4064 ID = NextMacroID++;
4065 return ID;
4066}
4067
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004068void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004069 Record.push_back(getSelectorRef(SelRef));
4070}
4071
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004072SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004073 if (Sel.getAsOpaquePtr() == 0) {
4074 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004075 }
4076
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004077 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004078 if (SID == 0 && Chain) {
4079 // This might trigger a ReadSelector callback, which will set the ID for
4080 // this selector.
4081 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004082 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004083 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004084 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004085 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004086 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004087 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004088 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004089}
4090
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004091void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004092 AddDeclRef(Temp->getDestructor(), Record);
4093}
4094
Douglas Gregor7c789c12010-10-29 22:39:52 +00004095void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4096 CXXBaseSpecifier const *BasesEnd,
4097 RecordDataImpl &Record) {
4098 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4099 CXXBaseSpecifiersToWrite.push_back(
4100 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4101 Bases, BasesEnd));
4102 Record.push_back(NextCXXBaseSpecifiersID++);
4103}
4104
Sebastian Redla4232eb2010-08-18 23:56:21 +00004105void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004106 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004107 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004108 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004109 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004110 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004111 break;
4112 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004113 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004114 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004115 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004116 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004117 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004118 break;
4119 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004120 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004121 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004122 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004123 break;
John McCall833ca992009-10-29 08:12:44 +00004124 case TemplateArgument::Null:
4125 case TemplateArgument::Integral:
4126 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004127 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004128 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004129 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004130 break;
4131 }
4132}
4133
Sebastian Redla4232eb2010-08-18 23:56:21 +00004134void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004135 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004136 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004137
4138 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4139 bool InfoHasSameExpr
4140 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4141 Record.push_back(InfoHasSameExpr);
4142 if (InfoHasSameExpr)
4143 return; // Avoid storing the same expr twice.
4144 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004145 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4146 Record);
4147}
4148
Douglas Gregordc355712011-02-25 00:36:19 +00004149void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4150 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004151 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004152 AddTypeRef(QualType(), Record);
4153 return;
4154 }
4155
Douglas Gregordc355712011-02-25 00:36:19 +00004156 AddTypeLoc(TInfo->getTypeLoc(), Record);
4157}
4158
4159void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4160 AddTypeRef(TL.getType(), Record);
4161
John McCalla1ee0c52009-10-16 21:56:05 +00004162 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004163 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004164 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004165}
4166
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004167void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004168 Record.push_back(GetOrCreateTypeID(T));
4169}
4170
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004171TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4172 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004173 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4174}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004175
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004176TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004177 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004178 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004179}
4180
4181TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4182 if (T.isNull())
4183 return TypeIdx();
4184 assert(!T.getLocalFastQualifiers());
4185
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004186 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004187 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004188 if (DoneWritingDeclsAndTypes) {
4189 assert(0 && "New type seen after serializing all the types to emit!");
4190 return TypeIdx();
4191 }
4192
Douglas Gregor366809a2009-04-26 03:49:13 +00004193 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004194 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004195 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004196 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004197 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004198 return Idx;
4199}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004200
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004201TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004202 if (T.isNull())
4203 return TypeIdx();
4204 assert(!T.getLocalFastQualifiers());
4205
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004206 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4207 assert(I != TypeIdxs.end() && "Type not emitted!");
4208 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004209}
4210
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004211void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004212 Record.push_back(GetDeclRef(D));
4213}
4214
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004215DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004216 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4217
Douglas Gregor2cf26342009-04-09 22:27:44 +00004218 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004219 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004220 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004221
4222 // If D comes from an AST file, its declaration ID is already known and
4223 // fixed.
4224 if (D->isFromASTFile())
4225 return D->getGlobalID();
4226
Douglas Gregor97475832010-10-05 18:37:06 +00004227 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004228 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004229 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004230 if (DoneWritingDeclsAndTypes) {
4231 assert(0 && "New decl seen after serializing all the decls to emit!");
4232 return 0;
4233 }
4234
Douglas Gregor2cf26342009-04-09 22:27:44 +00004235 // We haven't seen this declaration before. Give it a new ID and
4236 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004237 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004238 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004239 }
4240
Sebastian Redl681d7232010-07-27 00:17:23 +00004241 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004242}
4243
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004244DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004245 if (D == 0)
4246 return 0;
4247
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004248 // If D comes from an AST file, its declaration ID is already known and
4249 // fixed.
4250 if (D->isFromASTFile())
4251 return D->getGlobalID();
4252
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004253 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4254 return DeclIDs[D];
4255}
4256
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004257static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4258 std::pair<unsigned, serialization::DeclID> R) {
4259 return L.first < R.first;
4260}
4261
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004262void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004263 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004264 assert(D);
4265
4266 SourceLocation Loc = D->getLocation();
4267 if (Loc.isInvalid())
4268 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004269
4270 // We only keep track of the file-level declarations of each file.
4271 if (!D->getLexicalDeclContext()->isFileContext())
4272 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004273 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4274 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004275 if (isa<ParmVarDecl>(D))
4276 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004277
4278 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004279 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004280 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004281 FileID FID;
4282 unsigned Offset;
4283 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004284 if (FID.isInvalid())
4285 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004286 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004287
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004288 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004289 if (!Info)
4290 Info = new DeclIDInFileInfo();
4291
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004292 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004293 LocDeclIDsTy &Decls = Info->DeclIDs;
4294
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004295 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004296 Decls.push_back(LocDecl);
4297 return;
4298 }
4299
4300 LocDeclIDsTy::iterator
4301 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4302
4303 Decls.insert(I, LocDecl);
4304}
4305
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004306void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004307 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004308 Record.push_back(Name.getNameKind());
4309 switch (Name.getNameKind()) {
4310 case DeclarationName::Identifier:
4311 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4312 break;
4313
4314 case DeclarationName::ObjCZeroArgSelector:
4315 case DeclarationName::ObjCOneArgSelector:
4316 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004317 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004318 break;
4319
4320 case DeclarationName::CXXConstructorName:
4321 case DeclarationName::CXXDestructorName:
4322 case DeclarationName::CXXConversionFunctionName:
4323 AddTypeRef(Name.getCXXNameType(), Record);
4324 break;
4325
4326 case DeclarationName::CXXOperatorName:
4327 Record.push_back(Name.getCXXOverloadedOperator());
4328 break;
4329
Sean Hunt3e518bd2009-11-29 07:34:05 +00004330 case DeclarationName::CXXLiteralOperatorName:
4331 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4332 break;
4333
Douglas Gregor2cf26342009-04-09 22:27:44 +00004334 case DeclarationName::CXXUsingDirective:
4335 // No extra data to emit
4336 break;
4337 }
4338}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004339
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004340void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004341 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004342 switch (Name.getNameKind()) {
4343 case DeclarationName::CXXConstructorName:
4344 case DeclarationName::CXXDestructorName:
4345 case DeclarationName::CXXConversionFunctionName:
4346 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4347 break;
4348
4349 case DeclarationName::CXXOperatorName:
4350 AddSourceLocation(
4351 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4352 Record);
4353 AddSourceLocation(
4354 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4355 Record);
4356 break;
4357
4358 case DeclarationName::CXXLiteralOperatorName:
4359 AddSourceLocation(
4360 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4361 Record);
4362 break;
4363
4364 case DeclarationName::Identifier:
4365 case DeclarationName::ObjCZeroArgSelector:
4366 case DeclarationName::ObjCOneArgSelector:
4367 case DeclarationName::ObjCMultiArgSelector:
4368 case DeclarationName::CXXUsingDirective:
4369 break;
4370 }
4371}
4372
4373void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004374 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004375 AddDeclarationName(NameInfo.getName(), Record);
4376 AddSourceLocation(NameInfo.getLoc(), Record);
4377 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4378}
4379
4380void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004381 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004382 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004383 Record.push_back(Info.NumTemplParamLists);
4384 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4385 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4386}
4387
Sebastian Redla4232eb2010-08-18 23:56:21 +00004388void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004389 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004390 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004391 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004392 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004393
4394 // Push each of the NNS's onto a stack for serialization in reverse order.
4395 while (NNS) {
4396 NestedNames.push_back(NNS);
4397 NNS = NNS->getPrefix();
4398 }
4399
4400 Record.push_back(NestedNames.size());
4401 while(!NestedNames.empty()) {
4402 NNS = NestedNames.pop_back_val();
4403 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4404 Record.push_back(Kind);
4405 switch (Kind) {
4406 case NestedNameSpecifier::Identifier:
4407 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4408 break;
4409
4410 case NestedNameSpecifier::Namespace:
4411 AddDeclRef(NNS->getAsNamespace(), Record);
4412 break;
4413
Douglas Gregor14aba762011-02-24 02:36:08 +00004414 case NestedNameSpecifier::NamespaceAlias:
4415 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4416 break;
4417
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004418 case NestedNameSpecifier::TypeSpec:
4419 case NestedNameSpecifier::TypeSpecWithTemplate:
4420 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4421 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4422 break;
4423
4424 case NestedNameSpecifier::Global:
4425 // Don't need to write an associated value.
4426 break;
4427 }
4428 }
4429}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004430
Douglas Gregordc355712011-02-25 00:36:19 +00004431void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4432 RecordDataImpl &Record) {
4433 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004434 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004435 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004436
4437 // Push each of the nested-name-specifiers's onto a stack for
4438 // serialization in reverse order.
4439 while (NNS) {
4440 NestedNames.push_back(NNS);
4441 NNS = NNS.getPrefix();
4442 }
4443
4444 Record.push_back(NestedNames.size());
4445 while(!NestedNames.empty()) {
4446 NNS = NestedNames.pop_back_val();
4447 NestedNameSpecifier::SpecifierKind Kind
4448 = NNS.getNestedNameSpecifier()->getKind();
4449 Record.push_back(Kind);
4450 switch (Kind) {
4451 case NestedNameSpecifier::Identifier:
4452 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4453 AddSourceRange(NNS.getLocalSourceRange(), Record);
4454 break;
4455
4456 case NestedNameSpecifier::Namespace:
4457 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4458 AddSourceRange(NNS.getLocalSourceRange(), Record);
4459 break;
4460
4461 case NestedNameSpecifier::NamespaceAlias:
4462 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4463 AddSourceRange(NNS.getLocalSourceRange(), Record);
4464 break;
4465
4466 case NestedNameSpecifier::TypeSpec:
4467 case NestedNameSpecifier::TypeSpecWithTemplate:
4468 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4469 AddTypeLoc(NNS.getTypeLoc(), Record);
4470 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4471 break;
4472
4473 case NestedNameSpecifier::Global:
4474 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4475 break;
4476 }
4477 }
4478}
4479
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004480void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004481 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004482 Record.push_back(Kind);
4483 switch (Kind) {
4484 case TemplateName::Template:
4485 AddDeclRef(Name.getAsTemplateDecl(), Record);
4486 break;
4487
4488 case TemplateName::OverloadedTemplate: {
4489 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4490 Record.push_back(OvT->size());
4491 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4492 I != E; ++I)
4493 AddDeclRef(*I, Record);
4494 break;
4495 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004496
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004497 case TemplateName::QualifiedTemplate: {
4498 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4499 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4500 Record.push_back(QualT->hasTemplateKeyword());
4501 AddDeclRef(QualT->getTemplateDecl(), Record);
4502 break;
4503 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004504
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004505 case TemplateName::DependentTemplate: {
4506 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4507 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4508 Record.push_back(DepT->isIdentifier());
4509 if (DepT->isIdentifier())
4510 AddIdentifierRef(DepT->getIdentifier(), Record);
4511 else
4512 Record.push_back(DepT->getOperator());
4513 break;
4514 }
John McCall14606042011-06-30 08:33:18 +00004515
4516 case TemplateName::SubstTemplateTemplateParm: {
4517 SubstTemplateTemplateParmStorage *subst
4518 = Name.getAsSubstTemplateTemplateParm();
4519 AddDeclRef(subst->getParameter(), Record);
4520 AddTemplateName(subst->getReplacement(), Record);
4521 break;
4522 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004523
4524 case TemplateName::SubstTemplateTemplateParmPack: {
4525 SubstTemplateTemplateParmPackStorage *SubstPack
4526 = Name.getAsSubstTemplateTemplateParmPack();
4527 AddDeclRef(SubstPack->getParameterPack(), Record);
4528 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4529 break;
4530 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004531 }
4532}
4533
Michael J. Spencer20249a12010-10-21 03:16:25 +00004534void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004535 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004536 Record.push_back(Arg.getKind());
4537 switch (Arg.getKind()) {
4538 case TemplateArgument::Null:
4539 break;
4540 case TemplateArgument::Type:
4541 AddTypeRef(Arg.getAsType(), Record);
4542 break;
4543 case TemplateArgument::Declaration:
4544 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004545 Record.push_back(Arg.isDeclForReferenceParam());
4546 break;
4547 case TemplateArgument::NullPtr:
4548 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004549 break;
4550 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004551 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004552 AddTypeRef(Arg.getIntegralType(), Record);
4553 break;
4554 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004555 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4556 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004557 case TemplateArgument::TemplateExpansion:
4558 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004559 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004560 Record.push_back(*NumExpansions + 1);
4561 else
4562 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004563 break;
4564 case TemplateArgument::Expression:
4565 AddStmt(Arg.getAsExpr());
4566 break;
4567 case TemplateArgument::Pack:
4568 Record.push_back(Arg.pack_size());
4569 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4570 I != E; ++I)
4571 AddTemplateArgument(*I, Record);
4572 break;
4573 }
4574}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004575
4576void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004577ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004578 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004579 assert(TemplateParams && "No TemplateParams!");
4580 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4581 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4582 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4583 Record.push_back(TemplateParams->size());
4584 for (TemplateParameterList::const_iterator
4585 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4586 P != PEnd; ++P)
4587 AddDeclRef(*P, Record);
4588}
4589
4590/// \brief Emit a template argument list.
4591void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004592ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004593 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004594 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004595 Record.push_back(TemplateArgs->size());
4596 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004597 AddTemplateArgument(TemplateArgs->get(i), Record);
4598}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004599
4600
4601void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004602ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004603 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004604 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004605 I = Set.begin(), E = Set.end(); I != E; ++I) {
4606 AddDeclRef(I.getDecl(), Record);
4607 Record.push_back(I.getAccess());
4608 }
4609}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004610
Sebastian Redla4232eb2010-08-18 23:56:21 +00004611void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004612 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004613 Record.push_back(Base.isVirtual());
4614 Record.push_back(Base.isBaseOfClass());
4615 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004616 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004617 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004618 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004619 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4620 : SourceLocation(),
4621 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004622}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004623
Douglas Gregor7c789c12010-10-29 22:39:52 +00004624void ASTWriter::FlushCXXBaseSpecifiers() {
4625 RecordData Record;
4626 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4627 Record.clear();
4628
4629 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004630 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004631 if (Index == CXXBaseSpecifiersOffsets.size())
4632 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4633 else {
4634 if (Index > CXXBaseSpecifiersOffsets.size())
4635 CXXBaseSpecifiersOffsets.resize(Index + 1);
4636 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4637 }
4638
4639 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4640 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4641 Record.push_back(BEnd - B);
4642 for (; B != BEnd; ++B)
4643 AddCXXBaseSpecifier(*B, Record);
4644 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004645
4646 // Flush any expressions that were written as part of the base specifiers.
4647 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004648 }
4649
4650 CXXBaseSpecifiersToWrite.clear();
4651}
4652
Sean Huntcbb67482011-01-08 20:30:50 +00004653void ASTWriter::AddCXXCtorInitializers(
4654 const CXXCtorInitializer * const *CtorInitializers,
4655 unsigned NumCtorInitializers,
4656 RecordDataImpl &Record) {
4657 Record.push_back(NumCtorInitializers);
4658 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4659 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004660
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004661 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004662 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004663 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004664 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004665 } else if (Init->isDelegatingInitializer()) {
4666 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004667 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004668 } else if (Init->isMemberInitializer()){
4669 Record.push_back(CTOR_INITIALIZER_MEMBER);
4670 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004671 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004672 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4673 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004674 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004675
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004676 AddSourceLocation(Init->getMemberLocation(), Record);
4677 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004678 AddSourceLocation(Init->getLParenLoc(), Record);
4679 AddSourceLocation(Init->getRParenLoc(), Record);
4680 Record.push_back(Init->isWritten());
4681 if (Init->isWritten()) {
4682 Record.push_back(Init->getSourceOrder());
4683 } else {
4684 Record.push_back(Init->getNumArrayIndices());
4685 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4686 AddDeclRef(Init->getArrayIndex(i), Record);
4687 }
4688 }
4689}
4690
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004691void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4692 assert(D->DefinitionData);
4693 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004694 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004695 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004696 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004697 Record.push_back(Data.Aggregate);
4698 Record.push_back(Data.PlainOldData);
4699 Record.push_back(Data.Empty);
4700 Record.push_back(Data.Polymorphic);
4701 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004702 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004703 Record.push_back(Data.HasNoNonEmptyBases);
4704 Record.push_back(Data.HasPrivateFields);
4705 Record.push_back(Data.HasProtectedFields);
4706 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004707 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004708 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004709 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004710 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004711 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4712 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4713 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4714 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4715 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4716 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004717 Record.push_back(Data.HasTrivialSpecialMembers);
4718 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004719 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004720 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004721 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004722 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004723 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004724 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004725 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00004726 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
4727 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
4728 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
4729 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00004730 Record.push_back(Data.FailedImplicitMoveConstructor);
4731 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004732 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004733
4734 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004735 if (Data.NumBases > 0)
4736 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4737 Record);
4738
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004739 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4740 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004741 if (Data.NumVBases > 0)
4742 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4743 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004744
4745 AddUnresolvedSet(Data.Conversions, Record);
4746 AddUnresolvedSet(Data.VisibleConversions, Record);
4747 // Data.Definition is the owning decl, no need to write it.
4748 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004749
4750 // Add lambda-specific data.
4751 if (Data.IsLambda) {
4752 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004753 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004754 Record.push_back(Lambda.NumCaptures);
4755 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004756 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004757 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004758 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004759 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4760 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4761 AddSourceLocation(Capture.getLocation(), Record);
4762 Record.push_back(Capture.isImplicit());
4763 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4764 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4765 AddDeclRef(Var, Record);
4766 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4767 : SourceLocation(),
4768 Record);
4769 }
4770 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004771}
4772
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004773void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004774 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004775 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004776 assert(FirstDeclID == NextDeclID &&
4777 FirstTypeID == NextTypeID &&
4778 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004779 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004780 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004781 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004782 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004783
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004784 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004785
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004786 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4787 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4788 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004789 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004790 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004791 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004792 NextDeclID = FirstDeclID;
4793 NextTypeID = FirstTypeID;
4794 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004795 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004796 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004797 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004798}
4799
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004800void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004801 // Always keep the highest ID. See \p TypeRead() for more information.
4802 IdentID &StoredID = IdentifierIDs[II];
4803 if (ID > StoredID)
4804 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004805}
4806
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004807void ASTWriter::MacroRead(serialization::MacroID ID, MacroDirective *MD) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004808 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004809 MacroID &StoredID = MacroIDs[MD];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004810 if (ID > StoredID)
4811 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004812}
4813
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004814void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004815 // Always take the highest-numbered type index. This copes with an interesting
4816 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004817 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004818 // keep the higher-numbered entry so that we can properly write it out to
4819 // the AST file.
4820 TypeIdx &StoredIdx = TypeIdxs[T];
4821 if (Idx.getIndex() >= StoredIdx.getIndex())
4822 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004823}
4824
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004825void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004826 // Always keep the highest ID. See \p TypeRead() for more information.
4827 SelectorID &StoredID = SelectorIDs[S];
4828 if (ID > StoredID)
4829 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00004830}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004831
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004832void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004833 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004834 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004835 MacroDefinitions[MD] = ID;
4836}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004837
Douglas Gregora015cab2011-12-02 17:30:13 +00004838void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4839 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4840 SubmoduleIDs[Mod] = ID;
4841}
4842
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004843void ASTWriter::UndefinedMacro(MacroDirective *MD) {
4844 MacroUpdates[MD].UndefLoc = MD->getUndefLoc();
Douglas Gregora8235d62012-10-09 23:05:51 +00004845}
4846
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004847void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004848 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004849 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004850 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4851 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004852 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004853 // A forward reference was mutated into a definition. Rewrite it.
4854 // FIXME: This happens during template instantiation, should we
4855 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004856 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004857 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004858 }
4859}
Douglas Gregora8235d62012-10-09 23:05:51 +00004860
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004861void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004862 assert(!WritingAST && "Already writing the AST!");
4863
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004864 // TU and namespaces are handled elsewhere.
4865 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4866 return;
4867
Douglas Gregor919814d2011-09-09 23:01:35 +00004868 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004869 return; // Not a source decl added to a DeclContext from PCH.
4870
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00004871 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004872 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004873 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004874}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004875
4876void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004877 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004878 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004879 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004880 return; // Not a source member added to a class from PCH.
4881 if (!isa<CXXMethodDecl>(D))
4882 return; // We are interested in lazily declared implicit methods.
4883
4884 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004885 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004886 UpdateRecord &Record = DeclUpdates[RD];
4887 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004888 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004889}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004890
4891void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4892 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004893 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004894 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004895 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004896 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004897 return; // Not a source specialization added to a template from PCH.
4898
4899 UpdateRecord &Record = DeclUpdates[TD];
4900 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004901 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004902}
Douglas Gregor89d99802010-11-30 06:16:57 +00004903
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004904void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4905 const FunctionDecl *D) {
4906 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004907 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004908 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004909 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004910 return; // Not a source specialization added to a template from PCH.
4911
4912 UpdateRecord &Record = DeclUpdates[TD];
4913 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004914 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004915}
4916
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004917void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004918 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004919 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004920 return; // Declaration not imported from PCH.
4921
4922 // Implicit decl from a PCH was defined.
4923 // FIXME: Should implicit definition be a separate FunctionDecl?
4924 RewriteDecl(D);
4925}
4926
Sebastian Redlf79a7192011-04-29 08:19:30 +00004927void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004928 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004929 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004930 return;
4931
4932 // Since the actual instantiation is delayed, this really means that we need
4933 // to update the instantiation location.
4934 UpdateRecord &Record = DeclUpdates[D];
4935 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4936 AddSourceLocation(
4937 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4938}
4939
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004940void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4941 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004942 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004943 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004944 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004945
4946 assert(IFD->getDefinition() && "Category on a class without a definition?");
4947 ObjCClassesWithCategories.insert(
4948 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004949}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004950
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004951
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004952void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4953 const ObjCPropertyDecl *OrigProp,
4954 const ObjCCategoryDecl *ClassExt) {
4955 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4956 if (!D)
4957 return;
4958
4959 assert(!WritingAST && "Already writing the AST!");
4960 if (!D->isFromASTFile())
4961 return; // Declaration not imported from PCH.
4962
4963 RewriteDecl(D);
4964}