blob: d8f170fd517c95af3dd38cc8b03b8eab9babca32 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-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 Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-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 Gregore0a3a512009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057
Sebastian Redl3df5a082010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-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 Kramerd47a12a2011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000063}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000069}
70
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000074
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-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 Gregoref84c4b2009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Reid Kleckner8a365022013-06-24 17:51:48 +0000111void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
112 Writer.AddTypeRef(T->getOriginalType(), Record);
113 Code = TYPE_DECAYED;
114}
115
Reid Kleckner0503a872013-12-05 01:23:43 +0000116void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
117 Writer.AddTypeRef(T->getOriginalType(), Record);
118 Writer.AddTypeRef(T->getAdjustedType(), Record);
119 Code = TYPE_ADJUSTED;
120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000124 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000128 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
129 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000134 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000139 Writer.AddTypeRef(T->getPointeeType(), Record);
140 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145 Writer.AddTypeRef(T->getElementType(), Record);
146 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000147 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148}
149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151 VisitArrayType(T);
152 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000153 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154}
155
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000163 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
164 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000165 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000166 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167}
168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170 Writer.AddTypeRef(T->getElementType(), Record);
171 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000172 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000173 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174}
175
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000176void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000178 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179}
180
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000182 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000183 FunctionType::ExtInfo C = T->getExtInfo();
184 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000185 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000186 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000187 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000188 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000189 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190}
191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000194 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195}
196
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000197void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198 VisitFunctionType(T);
199 Record.push_back(T->getNumArgs());
200 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
201 Writer.AddTypeRef(T->getArgType(I), Record);
202 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000203 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000205 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000206 Record.push_back(T->getExceptionSpecType());
207 if (T->getExceptionSpecType() == EST_Dynamic) {
208 Record.push_back(T->getNumExceptions());
209 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
210 Writer.AddTypeRef(T->getExceptionType(I), Record);
211 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
212 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000213 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
214 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
215 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000216 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
217 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000218 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000223 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000225}
John McCallb96ec562009-12-04 22:46:56 +0000226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000229 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
230 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000231 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000232}
233
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000235 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000236 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237}
238
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000245 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000246 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000248}
249
Alexis Hunte852b102011-05-24 22:41:36 +0000250void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
251 Writer.AddTypeRef(T->getBaseType(), Record);
252 Writer.AddTypeRef(T->getUnderlyingType(), Record);
253 Record.push_back(T->getUTTKind());
254 Code = TYPE_UNARY_TRANSFORM;
255}
256
Richard Smith30482bc2011-02-20 03:19:35 +0000257void ASTTypeWriter::VisitAutoType(const AutoType *T) {
258 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000259 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000260 if (T->getDeducedType().isNull())
261 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000262 Code = TYPE_AUTO;
263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000266 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000267 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000268 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000269 "Cannot serialize in the middle of a type definition");
270}
271
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000273 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000274 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000275}
276
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000277void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000278 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000279 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000280}
281
John McCall81904512011-01-06 01:58:22 +0000282void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
283 Writer.AddTypeRef(T->getModifiedType(), Record);
284 Writer.AddTypeRef(T->getEquivalentType(), Record);
285 Record.push_back(T->getAttrKind());
286 Code = TYPE_ATTRIBUTED;
287}
288
Mike Stump11289f42009-09-09 15:08:12 +0000289void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000290ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000291 const SubstTemplateTypeParmType *T) {
292 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
293 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000294 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000295}
296
297void
Douglas Gregorada4b792011-01-14 02:55:32 +0000298ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
299 const SubstTemplateTypeParmPackType *T) {
300 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
301 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
302 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
303}
304
305void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000306ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000307 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000308 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309 Writer.AddTemplateName(T->getTemplateName(), Record);
310 Record.push_back(T->getNumArgs());
311 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
312 ArgI != ArgE; ++ArgI)
313 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000314 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
315 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000316 : T->getCanonicalTypeInternal(),
317 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000318 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000323 VisitArrayType(T);
324 Writer.AddStmt(T->getSizeExpr());
325 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000326 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000327}
328
329void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000331 const DependentSizedExtVectorType *T) {
332 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000333 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000334}
335
336void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000337ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000338 Record.push_back(T->getDepth());
339 Record.push_back(T->getIndex());
340 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000341 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000343}
344
345void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000346ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000347 Record.push_back(T->getKeyword());
348 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
349 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000350 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
351 : T->getCanonicalTypeInternal(),
352 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000353 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000354}
355
356void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000357ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000358 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000359 Record.push_back(T->getKeyword());
360 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
361 Writer.AddIdentifierRef(T->getIdentifier(), Record);
362 Record.push_back(T->getNumArgs());
363 for (DependentTemplateSpecializationType::iterator
364 I = T->begin(), E = T->end(); I != E; ++I)
365 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000366 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000367}
368
Douglas Gregord2fa7662010-12-20 02:24:11 +0000369void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
370 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000371 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000372 Record.push_back(*NumExpansions + 1);
373 else
374 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000375 Code = TYPE_PACK_EXPANSION;
376}
377
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000378void ASTTypeWriter::VisitParenType(const ParenType *T) {
379 Writer.AddTypeRef(T->getInnerType(), Record);
380 Code = TYPE_PAREN;
381}
382
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000384 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000385 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
386 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000387 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388}
389
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000390void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000391 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000392 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000393 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000394}
395
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000396void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000397 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000398 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000399}
400
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000401void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000402 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000403 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000404 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000405 E = T->qual_end(); I != E; ++I)
406 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000407 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000408}
409
Steve Narofffb4330f2009-06-17 22:40:22 +0000410void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000411ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000412 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000413 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000414}
415
Eli Friedman0dfb8892011-10-06 23:00:33 +0000416void
417ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
418 Writer.AddTypeRef(T->getValueType(), Record);
419 Code = TYPE_ATOMIC;
420}
421
John McCall8f115c62009-10-16 21:56:05 +0000422namespace {
423
424class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000425 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000426 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000427
428public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000429 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000430 : Writer(Writer), Record(Record) { }
431
John McCall17001972009-10-18 01:05:36 +0000432#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000433#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000434 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000435#include "clang/AST/TypeLocNodes.def"
436
John McCall17001972009-10-18 01:05:36 +0000437 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
438 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000439};
440
441}
442
John McCall17001972009-10-18 01:05:36 +0000443void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
444 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000445}
John McCall17001972009-10-18 01:05:36 +0000446void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000447 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
448 if (TL.needsExtraLocalData()) {
449 Record.push_back(TL.getWrittenTypeSpec());
450 Record.push_back(TL.getWrittenSignSpec());
451 Record.push_back(TL.getWrittenWidthSpec());
452 Record.push_back(TL.hasModeAttr());
453 }
John McCall8f115c62009-10-16 21:56:05 +0000454}
John McCall17001972009-10-18 01:05:36 +0000455void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000457}
John McCall17001972009-10-18 01:05:36 +0000458void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000460}
Reid Kleckner8a365022013-06-24 17:51:48 +0000461void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
462 // nothing to do
463}
Reid Kleckner0503a872013-12-05 01:23:43 +0000464void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
465 // nothing to do
466}
John McCall17001972009-10-18 01:05:36 +0000467void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
468 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000469}
John McCall17001972009-10-18 01:05:36 +0000470void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000472}
John McCall17001972009-10-18 01:05:36 +0000473void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000475}
John McCall17001972009-10-18 01:05:36 +0000476void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000478 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000479}
John McCall17001972009-10-18 01:05:36 +0000480void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
482 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
483 Record.push_back(TL.getSizeExpr() ? 1 : 0);
484 if (TL.getSizeExpr())
485 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
488 VisitArrayTypeLoc(TL);
489}
490void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
491 VisitArrayTypeLoc(TL);
492}
493void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
494 VisitArrayTypeLoc(TL);
495}
496void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
497 DependentSizedArrayTypeLoc TL) {
498 VisitArrayTypeLoc(TL);
499}
500void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
501 DependentSizedExtVectorTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
504void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
507void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000511 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000512 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
513 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000514 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall17001972009-10-18 01:05:36 +0000515 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
516 Writer.AddDeclRef(TL.getArg(i), Record);
517}
518void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
519 VisitFunctionTypeLoc(TL);
520}
521void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
522 VisitFunctionTypeLoc(TL);
523}
John McCallb96ec562009-12-04 22:46:56 +0000524void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall17001972009-10-18 01:05:36 +0000527void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000531 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
532 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
533 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000534}
535void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000536 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
537 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
538 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
539 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000540}
541void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
Alexis Hunte852b102011-05-24 22:41:36 +0000544void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getKWLoc(), Record);
546 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
547 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
548 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
549}
Richard Smith30482bc2011-02-20 03:19:35 +0000550void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall17001972009-10-18 01:05:36 +0000553void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
556void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getNameLoc(), Record);
558}
John McCall81904512011-01-06 01:58:22 +0000559void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
561 if (TL.hasAttrOperand()) {
562 SourceRange range = TL.getAttrOperandParensRange();
563 Writer.AddSourceLocation(range.getBegin(), Record);
564 Writer.AddSourceLocation(range.getEnd(), Record);
565 }
566 if (TL.hasAttrExprOperand()) {
567 Expr *operand = TL.getAttrExprOperand();
568 Record.push_back(operand ? 1 : 0);
569 if (operand) Writer.AddStmt(operand);
570 } else if (TL.hasAttrEnumOperand()) {
571 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
572 }
573}
John McCall17001972009-10-18 01:05:36 +0000574void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getNameLoc(), Record);
576}
John McCallcebee162009-10-18 09:09:24 +0000577void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
578 SubstTemplateTypeParmTypeLoc TL) {
579 Writer.AddSourceLocation(TL.getNameLoc(), Record);
580}
Douglas Gregorada4b792011-01-14 02:55:32 +0000581void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
582 SubstTemplateTypeParmPackTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
John McCall17001972009-10-18 01:05:36 +0000585void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
586 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000587 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000588 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
589 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
590 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
591 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000592 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
593 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000594}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000595void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
596 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
597 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
598}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000599void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000600 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000601 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000602}
John McCalle78aac42010-03-10 03:28:59 +0000603void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getNameLoc(), Record);
605}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000606void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000607 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000608 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000609 Writer.AddSourceLocation(TL.getNameLoc(), Record);
610}
John McCallc392f372010-06-11 00:33:02 +0000611void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
612 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000613 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000614 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000615 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000616 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000617 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
618 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
619 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000620 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
621 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000622}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000623void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
624 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
625}
John McCall17001972009-10-18 01:05:36 +0000626void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
627 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000628}
629void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
630 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000631 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
632 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
633 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
634 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000635}
John McCallfc93cf92009-10-22 22:37:11 +0000636void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
637 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000638}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000639void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
640 Writer.AddSourceLocation(TL.getKWLoc(), Record);
641 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
642 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
643}
John McCall8f115c62009-10-16 21:56:05 +0000644
Chris Lattner19cea4e2009-04-22 05:57:30 +0000645//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000646// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000647//===----------------------------------------------------------------------===//
648
Chris Lattner28fa4e62009-04-26 22:26:21 +0000649static void EmitBlockID(unsigned ID, const char *Name,
650 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000651 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000652 Record.clear();
653 Record.push_back(ID);
654 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
655
656 // Emit the block name if present.
657 if (Name == 0 || Name[0] == 0) return;
658 Record.clear();
659 while (*Name)
660 Record.push_back(*Name++);
661 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
662}
663
664static void EmitRecordID(unsigned ID, const char *Name,
665 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000666 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667 Record.clear();
668 Record.push_back(ID);
669 while (*Name)
670 Record.push_back(*Name++);
671 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000672}
673
674static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000675 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000676#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000677 RECORD(STMT_STOP);
678 RECORD(STMT_NULL_PTR);
679 RECORD(STMT_NULL);
680 RECORD(STMT_COMPOUND);
681 RECORD(STMT_CASE);
682 RECORD(STMT_DEFAULT);
683 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000684 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000685 RECORD(STMT_IF);
686 RECORD(STMT_SWITCH);
687 RECORD(STMT_WHILE);
688 RECORD(STMT_DO);
689 RECORD(STMT_FOR);
690 RECORD(STMT_GOTO);
691 RECORD(STMT_INDIRECT_GOTO);
692 RECORD(STMT_CONTINUE);
693 RECORD(STMT_BREAK);
694 RECORD(STMT_RETURN);
695 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000696 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000697 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000698 RECORD(EXPR_PREDEFINED);
699 RECORD(EXPR_DECL_REF);
700 RECORD(EXPR_INTEGER_LITERAL);
701 RECORD(EXPR_FLOATING_LITERAL);
702 RECORD(EXPR_IMAGINARY_LITERAL);
703 RECORD(EXPR_STRING_LITERAL);
704 RECORD(EXPR_CHARACTER_LITERAL);
705 RECORD(EXPR_PAREN);
706 RECORD(EXPR_UNARY_OPERATOR);
707 RECORD(EXPR_SIZEOF_ALIGN_OF);
708 RECORD(EXPR_ARRAY_SUBSCRIPT);
709 RECORD(EXPR_CALL);
710 RECORD(EXPR_MEMBER);
711 RECORD(EXPR_BINARY_OPERATOR);
712 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
713 RECORD(EXPR_CONDITIONAL_OPERATOR);
714 RECORD(EXPR_IMPLICIT_CAST);
715 RECORD(EXPR_CSTYLE_CAST);
716 RECORD(EXPR_COMPOUND_LITERAL);
717 RECORD(EXPR_EXT_VECTOR_ELEMENT);
718 RECORD(EXPR_INIT_LIST);
719 RECORD(EXPR_DESIGNATED_INIT);
720 RECORD(EXPR_IMPLICIT_VALUE_INIT);
721 RECORD(EXPR_VA_ARG);
722 RECORD(EXPR_ADDR_LABEL);
723 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000724 RECORD(EXPR_CHOOSE);
725 RECORD(EXPR_GNU_NULL);
726 RECORD(EXPR_SHUFFLE_VECTOR);
727 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000728 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000729 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000730 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000731 RECORD(EXPR_OBJC_ARRAY_LITERAL);
732 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000733 RECORD(EXPR_OBJC_ENCODE);
734 RECORD(EXPR_OBJC_SELECTOR_EXPR);
735 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
736 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
737 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
738 RECORD(EXPR_OBJC_KVC_REF_EXPR);
739 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000740 RECORD(STMT_OBJC_FOR_COLLECTION);
741 RECORD(STMT_OBJC_CATCH);
742 RECORD(STMT_OBJC_FINALLY);
743 RECORD(STMT_OBJC_AT_TRY);
744 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
745 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000746 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000747 RECORD(EXPR_CXX_OPERATOR_CALL);
748 RECORD(EXPR_CXX_CONSTRUCT);
749 RECORD(EXPR_CXX_STATIC_CAST);
750 RECORD(EXPR_CXX_DYNAMIC_CAST);
751 RECORD(EXPR_CXX_REINTERPRET_CAST);
752 RECORD(EXPR_CXX_CONST_CAST);
753 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000754 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000755 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000756 RECORD(EXPR_CXX_BOOL_LITERAL);
757 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000758 RECORD(EXPR_CXX_TYPEID_EXPR);
759 RECORD(EXPR_CXX_TYPEID_TYPE);
760 RECORD(EXPR_CXX_UUIDOF_EXPR);
761 RECORD(EXPR_CXX_UUIDOF_TYPE);
762 RECORD(EXPR_CXX_THIS);
763 RECORD(EXPR_CXX_THROW);
764 RECORD(EXPR_CXX_DEFAULT_ARG);
765 RECORD(EXPR_CXX_BIND_TEMPORARY);
766 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
767 RECORD(EXPR_CXX_NEW);
768 RECORD(EXPR_CXX_DELETE);
769 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
770 RECORD(EXPR_EXPR_WITH_CLEANUPS);
771 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
772 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
773 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
774 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
775 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
776 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
777 RECORD(EXPR_CXX_NOEXCEPT);
778 RECORD(EXPR_OPAQUE_VALUE);
779 RECORD(EXPR_BINARY_TYPE_TRAIT);
780 RECORD(EXPR_PACK_EXPANSION);
781 RECORD(EXPR_SIZEOF_PACK);
782 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000783 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000784#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000785}
Mike Stump11289f42009-09-09 15:08:12 +0000786
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000787void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000788 RecordData Record;
789 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000790
Sebastian Redl539c5062010-08-18 23:57:32 +0000791#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
792#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000793
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000794 // Control Block.
795 BLOCK(CONTROL_BLOCK);
796 RECORD(METADATA);
797 RECORD(IMPORTS);
798 RECORD(LANGUAGE_OPTIONS);
799 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000800 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000801 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000802 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000803 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000804 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000805 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000806 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000807 RECORD(PREPROCESSOR_OPTIONS);
808
Douglas Gregor108cb222012-10-19 00:45:00 +0000809 BLOCK(INPUT_FILES_BLOCK);
810 RECORD(INPUT_FILE);
811
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000812 // AST Top-Level Block.
813 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000814 RECORD(TYPE_OFFSET);
815 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000816 RECORD(IDENTIFIER_OFFSET);
817 RECORD(IDENTIFIER_TABLE);
818 RECORD(EXTERNAL_DEFINITIONS);
819 RECORD(SPECIAL_TYPES);
820 RECORD(STATISTICS);
821 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000822 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000823 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000824 RECORD(SELECTOR_OFFSETS);
825 RECORD(METHOD_POOL);
826 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000827 RECORD(SOURCE_LOCATION_OFFSETS);
828 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000829 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000830 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000831 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000832 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000833 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000834 RECORD(SEMA_DECL_REFS);
835 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
836 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
837 RECORD(DECL_REPLACEMENTS);
838 RECORD(UPDATE_VISIBLE);
839 RECORD(DECL_UPDATE_OFFSETS);
840 RECORD(DECL_UPDATES);
841 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
842 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000843 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000844 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000845 RECORD(FP_PRAGMA_OPTIONS);
846 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000847 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000848 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000849 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000850 RECORD(MODULE_OFFSET_MAP);
851 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000852 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000853 RECORD(FILE_SORTED_DECLS);
854 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000855 RECORD(MERGED_DECLARATIONS);
856 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000857 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000858 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000859 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000860 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000861
Chris Lattner28fa4e62009-04-26 22:26:21 +0000862 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000863 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000864 RECORD(SM_SLOC_FILE_ENTRY);
865 RECORD(SM_SLOC_BUFFER_ENTRY);
866 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000867 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000868
Chris Lattner28fa4e62009-04-26 22:26:21 +0000869 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000870 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000871 RECORD(PP_MACRO_OBJECT_LIKE);
872 RECORD(PP_MACRO_FUNCTION_LIKE);
873 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000874
Douglas Gregor12bfa382009-10-17 00:13:19 +0000875 // Decls and Types block.
876 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000877 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000878 RECORD(TYPE_COMPLEX);
879 RECORD(TYPE_POINTER);
880 RECORD(TYPE_BLOCK_POINTER);
881 RECORD(TYPE_LVALUE_REFERENCE);
882 RECORD(TYPE_RVALUE_REFERENCE);
883 RECORD(TYPE_MEMBER_POINTER);
884 RECORD(TYPE_CONSTANT_ARRAY);
885 RECORD(TYPE_INCOMPLETE_ARRAY);
886 RECORD(TYPE_VARIABLE_ARRAY);
887 RECORD(TYPE_VECTOR);
888 RECORD(TYPE_EXT_VECTOR);
889 RECORD(TYPE_FUNCTION_PROTO);
890 RECORD(TYPE_FUNCTION_NO_PROTO);
891 RECORD(TYPE_TYPEDEF);
892 RECORD(TYPE_TYPEOF_EXPR);
893 RECORD(TYPE_TYPEOF);
894 RECORD(TYPE_RECORD);
895 RECORD(TYPE_ENUM);
896 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000897 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000898 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000899 RECORD(TYPE_DECLTYPE);
900 RECORD(TYPE_ELABORATED);
901 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
902 RECORD(TYPE_UNRESOLVED_USING);
903 RECORD(TYPE_INJECTED_CLASS_NAME);
904 RECORD(TYPE_OBJC_OBJECT);
905 RECORD(TYPE_TEMPLATE_TYPE_PARM);
906 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
907 RECORD(TYPE_DEPENDENT_NAME);
908 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
909 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
910 RECORD(TYPE_PAREN);
911 RECORD(TYPE_PACK_EXPANSION);
912 RECORD(TYPE_ATTRIBUTED);
913 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000914 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000915 RECORD(DECL_TYPEDEF);
916 RECORD(DECL_ENUM);
917 RECORD(DECL_RECORD);
918 RECORD(DECL_ENUM_CONSTANT);
919 RECORD(DECL_FUNCTION);
920 RECORD(DECL_OBJC_METHOD);
921 RECORD(DECL_OBJC_INTERFACE);
922 RECORD(DECL_OBJC_PROTOCOL);
923 RECORD(DECL_OBJC_IVAR);
924 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000925 RECORD(DECL_OBJC_CATEGORY);
926 RECORD(DECL_OBJC_CATEGORY_IMPL);
927 RECORD(DECL_OBJC_IMPLEMENTATION);
928 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
929 RECORD(DECL_OBJC_PROPERTY);
930 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000931 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000932 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000933 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000934 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000935 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000936 RECORD(DECL_FILE_SCOPE_ASM);
937 RECORD(DECL_BLOCK);
938 RECORD(DECL_CONTEXT_LEXICAL);
939 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000940 RECORD(DECL_NAMESPACE);
941 RECORD(DECL_NAMESPACE_ALIAS);
942 RECORD(DECL_USING);
943 RECORD(DECL_USING_SHADOW);
944 RECORD(DECL_USING_DIRECTIVE);
945 RECORD(DECL_UNRESOLVED_USING_VALUE);
946 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
947 RECORD(DECL_LINKAGE_SPEC);
948 RECORD(DECL_CXX_RECORD);
949 RECORD(DECL_CXX_METHOD);
950 RECORD(DECL_CXX_CONSTRUCTOR);
951 RECORD(DECL_CXX_DESTRUCTOR);
952 RECORD(DECL_CXX_CONVERSION);
953 RECORD(DECL_ACCESS_SPEC);
954 RECORD(DECL_FRIEND);
955 RECORD(DECL_FRIEND_TEMPLATE);
956 RECORD(DECL_CLASS_TEMPLATE);
957 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
958 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000959 RECORD(DECL_VAR_TEMPLATE);
960 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
961 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000962 RECORD(DECL_FUNCTION_TEMPLATE);
963 RECORD(DECL_TEMPLATE_TYPE_PARM);
964 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
965 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
966 RECORD(DECL_STATIC_ASSERT);
967 RECORD(DECL_CXX_BASE_SPECIFIERS);
968 RECORD(DECL_INDIRECTFIELD);
969 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
970
Douglas Gregor03412ba2011-06-03 02:27:19 +0000971 // Statements and Exprs can occur in the Decls and Types block.
972 AddStmtsExprs(Stream, Record);
973
Douglas Gregor92a96f52011-02-08 21:58:10 +0000974 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000975 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000976 RECORD(PPD_MACRO_DEFINITION);
977 RECORD(PPD_INCLUSION_DIRECTIVE);
978
Chris Lattner28fa4e62009-04-26 22:26:21 +0000979#undef RECORD
980#undef BLOCK
981 Stream.ExitBlock();
982}
983
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000984/// \brief Adjusts the given filename to only write out the portion of the
985/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000986///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000987/// \param Filename the file name to adjust.
988///
989/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
990/// the returned filename will be adjusted by this system root.
991///
992/// \returns either the original filename (if it needs no adjustment) or the
993/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000994static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000995adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000996 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000997
Douglas Gregorc567ba22011-07-22 16:35:34 +0000998 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000999 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001000
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001001 // Verify that the filename and the system root have the same prefix.
1002 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001003 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001004 if (Filename[Pos] != isysroot[Pos])
1005 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001006
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001007 // We hit the end of the filename before we hit the end of the system root.
1008 if (!Filename[Pos])
1009 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001010
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001011 // If the file name has a '/' at the current position, skip over the '/'.
1012 // We distinguish sysroot-based includes from absolute includes by the
1013 // absence of '/' at the beginning of sysroot-based includes.
1014 if (Filename[Pos] == '/')
1015 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001016
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001017 return Filename + Pos;
1018}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001019
Douglas Gregor112b9072012-10-18 05:31:06 +00001020/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001021void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1022 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001023 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001024 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001025 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1026 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001027
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001028 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001029 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1030 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1031 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1032 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1033 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1034 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1035 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1036 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1037 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1038 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1039 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001040 Record.push_back(VERSION_MAJOR);
1041 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001042 Record.push_back(CLANG_VERSION_MAJOR);
1043 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001044 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001045 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001046 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1047 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001048
Douglas Gregor112b9072012-10-18 05:31:06 +00001049 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001050 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001051 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001052 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001053
1054 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1055 M != MEnd; ++M) {
1056 // Skip modules that weren't directly imported.
1057 if (!(*M)->isDirectlyImported())
1058 continue;
1059
1060 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001061 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001062 Record.push_back((*M)->File->getSize());
1063 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001064 // FIXME: This writes the absolute path for AST files we depend on.
1065 const std::string &FileName = (*M)->FileName;
1066 Record.push_back(FileName.size());
1067 Record.append(FileName.begin(), FileName.end());
1068 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001069 Stream.EmitRecord(IMPORTS, Record);
1070 }
Mike Stump11289f42009-09-09 15:08:12 +00001071
Douglas Gregor112b9072012-10-18 05:31:06 +00001072 // Language options.
1073 Record.clear();
1074 const LangOptions &LangOpts = Context.getLangOpts();
1075#define LANGOPT(Name, Bits, Default, Description) \
1076 Record.push_back(LangOpts.Name);
1077#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1078 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1079#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001080#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1081#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001082
1083 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1084 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1085
1086 Record.push_back(LangOpts.CurrentModule.size());
1087 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001088
1089 // Comment options.
1090 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1091 for (CommentOptions::BlockCommandNamesTy::const_iterator
1092 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1093 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1094 I != IEnd; ++I) {
1095 AddString(*I, Record);
1096 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001097 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001098
Douglas Gregor112b9072012-10-18 05:31:06 +00001099 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1100
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001101 // Target options.
1102 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001103 const TargetInfo &Target = Context.getTargetInfo();
1104 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001105 AddString(TargetOpts.Triple, Record);
1106 AddString(TargetOpts.CPU, Record);
1107 AddString(TargetOpts.ABI, Record);
1108 AddString(TargetOpts.CXXABI, Record);
1109 AddString(TargetOpts.LinkerVersion, Record);
1110 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1111 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1112 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1113 }
1114 Record.push_back(TargetOpts.Features.size());
1115 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1116 AddString(TargetOpts.Features[I], Record);
1117 }
1118 Stream.EmitRecord(TARGET_OPTIONS, Record);
1119
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001120 // Diagnostic options.
1121 Record.clear();
1122 const DiagnosticOptions &DiagOpts
1123 = Context.getDiagnostics().getDiagnosticOptions();
1124#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1125#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1126 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1127#include "clang/Basic/DiagnosticOptions.def"
1128 Record.push_back(DiagOpts.Warnings.size());
1129 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1130 AddString(DiagOpts.Warnings[I], Record);
1131 // Note: we don't serialize the log or serialization file names, because they
1132 // are generally transient files and will almost always be overridden.
1133 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1134
Douglas Gregorc6317db2012-10-24 15:49:58 +00001135 // File system options.
1136 Record.clear();
1137 const FileSystemOptions &FSOpts
1138 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1139 AddString(FSOpts.WorkingDir, Record);
1140 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1141
Douglas Gregor2d302362012-10-24 16:50:34 +00001142 // Header search options.
1143 Record.clear();
1144 const HeaderSearchOptions &HSOpts
1145 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1146 AddString(HSOpts.Sysroot, Record);
1147
1148 // Include entries.
1149 Record.push_back(HSOpts.UserEntries.size());
1150 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1151 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1152 AddString(Entry.Path, Record);
1153 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001154 Record.push_back(Entry.IsFramework);
1155 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001156 }
1157
1158 // System header prefixes.
1159 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1160 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1161 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1162 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1163 }
1164
1165 AddString(HSOpts.ResourceDir, Record);
1166 AddString(HSOpts.ModuleCachePath, Record);
1167 Record.push_back(HSOpts.DisableModuleHash);
1168 Record.push_back(HSOpts.UseBuiltinIncludes);
1169 Record.push_back(HSOpts.UseStandardSystemIncludes);
1170 Record.push_back(HSOpts.UseStandardCXXIncludes);
1171 Record.push_back(HSOpts.UseLibcxx);
1172 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1173
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001174 // Preprocessor options.
1175 Record.clear();
1176 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1177
1178 // Macro definitions.
1179 Record.push_back(PPOpts.Macros.size());
1180 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1181 AddString(PPOpts.Macros[I].first, Record);
1182 Record.push_back(PPOpts.Macros[I].second);
1183 }
1184
1185 // Includes
1186 Record.push_back(PPOpts.Includes.size());
1187 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1188 AddString(PPOpts.Includes[I], Record);
1189
1190 // Macro includes
1191 Record.push_back(PPOpts.MacroIncludes.size());
1192 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1193 AddString(PPOpts.MacroIncludes[I], Record);
1194
Douglas Gregorb6368752012-10-24 23:41:50 +00001195 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001196 // Detailed record is important since it is used for the module cache hash.
1197 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001198 AddString(PPOpts.ImplicitPCHInclude, Record);
1199 AddString(PPOpts.ImplicitPTHInclude, Record);
1200 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1201 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1202
Douglas Gregora3b20262011-05-06 21:43:30 +00001203 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001204 SourceManager &SM = Context.getSourceManager();
1205 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1206 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001207 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1208 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001209 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1210 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1211
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001212 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001213
Michael J. Spencer740857f2010-12-21 16:45:57 +00001214 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001215
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001216 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001217 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001218 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001219 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001220 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001221 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001222 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001223 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001224
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001225 Record.clear();
1226 Record.push_back(SM.getMainFileID().getOpaqueValue());
1227 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1228
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001229 // Original PCH directory
1230 if (!OutputFile.empty() && OutputFile != "-") {
1231 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1232 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1234 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1235
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001236 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001237
1238 llvm::sys::fs::make_absolute(OutputPath);
1239 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1240
1241 RecordData Record;
1242 Record.push_back(ORIGINAL_PCH_DIR);
1243 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1244 }
1245
Douglas Gregor49491f72013-03-15 22:15:07 +00001246 WriteInputFiles(Context.SourceMgr,
1247 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001248 isysroot,
1249 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001250 Stream.ExitBlock();
1251}
1252
Douglas Gregor49491f72013-03-15 22:15:07 +00001253namespace {
1254 /// \brief An input file.
1255 struct InputFileEntry {
1256 const FileEntry *File;
1257 bool IsSystemFile;
1258 bool BufferOverridden;
1259 };
1260}
1261
1262void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1263 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001264 StringRef isysroot,
1265 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001266 using namespace llvm;
1267 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1268 RecordData Record;
1269
1270 // Create input-file abbreviation.
1271 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1272 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001273 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001274 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1275 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001276 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001277 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1278 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1279
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001280 // Get all ContentCache objects for files, sorted by whether the file is a
1281 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001282 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001283 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1284 // Get this source location entry.
1285 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001286 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001287
1288 // We only care about file entries that were not overridden.
1289 if (!SLoc->isFile())
1290 continue;
1291 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001292 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001293 continue;
1294
Douglas Gregor49491f72013-03-15 22:15:07 +00001295 InputFileEntry Entry;
1296 Entry.File = Cache->OrigEntry;
1297 Entry.IsSystemFile = Cache->IsSystemFile;
1298 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001299 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001300 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001301 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001302 SortedFiles.push_front(Entry);
1303 }
1304
1305 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1306 // the set of (non-system) input files. This is simple heuristic for
1307 // detecting whether the system headers may have changed, because it is too
1308 // expensive to stat() all of the system headers.
Richard Smith0b1f4762013-05-20 23:40:27 +00001309 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bb719f2013-03-20 16:59:53 +00001310 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001311 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1312 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1313 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1314 InputFileEntry Entry = { SDKSettingsFile, false, false };
1315 SortedFiles.push_front(Entry);
1316 }
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001317 }
1318
Douglas Gregora3dd9002013-07-22 20:48:33 +00001319 // Add the compiler's own module.map in the set of (non-system) input files.
1320 // This is a simple heuristic for detecting whether the compiler's headers
1321 // have changed, because we don't want to stat() all of them.
1322 if (Modules && !Chain) {
1323 SmallString<128> P = StringRef(HSOpts.ResourceDir);
1324 llvm::sys::path::append(P, "include");
1325 llvm::sys::path::append(P, "module.map");
1326 if (const FileEntry *ModuleMapFile = FileMgr.getFile(P)) {
1327 InputFileEntry Entry = { ModuleMapFile, false, false };
1328 SortedFiles.push_front(Entry);
1329 }
1330 }
1331
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001332 unsigned UserFilesNum = 0;
1333 // Write out all of the input files.
1334 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001335 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001336 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001337 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001338
Douglas Gregor49491f72013-03-15 22:15:07 +00001339 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001340 if (InputFileID != 0)
1341 continue; // already recorded this file.
1342
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001343 // Record this entry's offset.
1344 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001345
1346 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001347
Douglas Gregor49491f72013-03-15 22:15:07 +00001348 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001349 ++UserFilesNum;
1350
Douglas Gregor72be3902012-10-19 00:38:02 +00001351 Record.clear();
1352 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001353 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001354
1355 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001356 Record.push_back(Entry.File->getSize());
1357 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001358
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001359 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001360 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001361
Douglas Gregor72be3902012-10-19 00:38:02 +00001362 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001363 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001364 SmallString<128> FilePath(Filename);
1365
1366 // Ask the file manager to fixup the relative path for us. This will
1367 // honor the working directory.
Douglas Gregor49491f72013-03-15 22:15:07 +00001368 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001369
1370 // FIXME: This call to make_absolute shouldn't be necessary, the
1371 // call to FixupRelativePath should always return an absolute path.
1372 llvm::sys::fs::make_absolute(FilePath);
1373 Filename = FilePath.c_str();
1374
1375 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1376
1377 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1378 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001379
Douglas Gregor112b9072012-10-18 05:31:06 +00001380 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001381
1382 // Create input file offsets abbreviation.
1383 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1384 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1385 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001386 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1387 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001388 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1389 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1390
1391 // Write input file offsets.
1392 Record.clear();
1393 Record.push_back(INPUT_FILE_OFFSETS);
1394 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001395 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001396 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001397}
1398
Douglas Gregora7f71a92009-04-10 03:52:48 +00001399//===----------------------------------------------------------------------===//
1400// Source Manager Serialization
1401//===----------------------------------------------------------------------===//
1402
1403/// \brief Create an abbreviation for the SLocEntry that refers to a
1404/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001405static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001406 using namespace llvm;
1407 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001408 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001413 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001418 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001419}
1420
1421/// \brief Create an abbreviation for the SLocEntry that refers to a
1422/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001423static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001424 using namespace llvm;
1425 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001426 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001432 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001433}
1434
1435/// \brief Create an abbreviation for the SLocEntry that refers to a
1436/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001437static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001438 using namespace llvm;
1439 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001440 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001442 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001443}
1444
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001445/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1446/// expansion.
1447static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001448 using namespace llvm;
1449 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001450 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1452 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001456 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001457}
1458
Douglas Gregor09b69892011-02-10 17:09:37 +00001459namespace {
1460 // Trait used for the on-disk hash table of header search information.
1461 class HeaderFileInfoTrait {
1462 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001463 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001464
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001465 // Keep track of the framework names we've used during serialization.
1466 SmallVector<char, 128> FrameworkStringData;
1467 llvm::StringMap<unsigned> FrameworkNameOffset;
1468
Douglas Gregor09b69892011-02-10 17:09:37 +00001469 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001470 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1471 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001472
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001473 struct key_type {
1474 const FileEntry *FE;
1475 const char *Filename;
1476 };
1477 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001478
1479 typedef HeaderFileInfo data_type;
1480 typedef const data_type &data_type_ref;
1481
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001482 static unsigned ComputeHash(key_type_ref key) {
1483 // The hash is based only on size/time of the file, so that the reader can
1484 // match even when symlinking or excess path elements ("foo/../", "../")
1485 // change the form of the name. However, complete path is still the key.
1486 return llvm::hash_combine(key.FE->getSize(),
1487 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001488 }
1489
1490 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001491 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1492 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1493 clang::io::Emit16(Out, KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001494 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001495 if (Data.isModuleHeader)
1496 DataLen += 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001497 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001498 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001499 }
1500
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001501 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1502 clang::io::Emit64(Out, key.FE->getSize());
1503 KeyLen -= 8;
1504 clang::io::Emit64(Out, key.FE->getModificationTime());
1505 KeyLen -= 8;
1506 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001507 }
1508
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001509 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001510 data_type_ref Data, unsigned DataLen) {
1511 using namespace clang::io;
1512 uint64_t Start = Out.tell(); (void)Start;
1513
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001514 unsigned char Flags = (Data.HeaderRole << 6)
1515 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001516 | (Data.isPragmaOnce << 4)
1517 | (Data.DirInfo << 2)
1518 | (Data.Resolved << 1)
1519 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001520 Emit8(Out, (uint8_t)Flags);
1521 Emit16(Out, (uint16_t) Data.NumIncludes);
1522
1523 if (!Data.ControllingMacro)
1524 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1525 else
1526 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001527
1528 unsigned Offset = 0;
1529 if (!Data.Framework.empty()) {
1530 // If this header refers into a framework, save the framework name.
1531 llvm::StringMap<unsigned>::iterator Pos
1532 = FrameworkNameOffset.find(Data.Framework);
1533 if (Pos == FrameworkNameOffset.end()) {
1534 Offset = FrameworkStringData.size() + 1;
1535 FrameworkStringData.append(Data.Framework.begin(),
1536 Data.Framework.end());
1537 FrameworkStringData.push_back(0);
1538
1539 FrameworkNameOffset[Data.Framework] = Offset;
1540 } else
1541 Offset = Pos->second;
1542 }
1543 Emit32(Out, Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001544
1545 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001546 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001547 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1548 }
1549
Douglas Gregor09b69892011-02-10 17:09:37 +00001550 assert(Out.tell() - Start == DataLen && "Wrong data length");
1551 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001552
1553 const char *strings_begin() const { return FrameworkStringData.begin(); }
1554 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001555 };
1556} // end anonymous namespace
1557
1558/// \brief Write the header search block for the list of files that
1559///
1560/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001561void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001562 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001563 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1564
1565 if (FilesByUID.size() > HS.header_file_size())
1566 FilesByUID.resize(HS.header_file_size());
1567
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001568 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001569 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001570 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001571 unsigned NumHeaderSearchEntries = 0;
1572 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1573 const FileEntry *File = FilesByUID[UID];
1574 if (!File)
1575 continue;
1576
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001577 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1578 // from the external source if it was not provided already.
1579 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001580 if (HFI.External && Chain)
1581 continue;
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001582 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1583 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001584
1585 // Turn the file name into an absolute path, if it isn't already.
1586 const char *Filename = File->getName();
1587 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1588
1589 // If we performed any translation on the file name at all, we need to
1590 // save this string, since the generator will refer to it later.
1591 if (Filename != File->getName()) {
1592 Filename = strdup(Filename);
1593 SavedStrings.push_back(Filename);
1594 }
1595
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001596 HeaderFileInfoTrait::key_type key = { File, Filename };
1597 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001598 ++NumHeaderSearchEntries;
1599 }
1600
1601 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001602 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001603 uint32_t BucketOffset;
1604 {
1605 llvm::raw_svector_ostream Out(TableData);
1606 // Make sure that no bucket is at offset 0
1607 clang::io::Emit32(Out, 0);
1608 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1609 }
1610
1611 // Create a blob abbreviation
1612 using namespace llvm;
1613 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1614 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1619 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1620
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001621 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001622 RecordData Record;
1623 Record.push_back(HEADER_SEARCH_TABLE);
1624 Record.push_back(BucketOffset);
1625 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001626 Record.push_back(TableData.size());
1627 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001628 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1629
1630 // Free all of the strings we had to duplicate.
1631 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001632 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001633}
1634
Douglas Gregora7f71a92009-04-10 03:52:48 +00001635/// \brief Writes the block containing the serialized form of the
1636/// source manager.
1637///
1638/// TODO: We should probably use an on-disk hash table (stored in a
1639/// blob), indexed based on the file name, so that we only create
1640/// entries for files that we actually need. In the common case (no
1641/// errors), we probably won't have to create file entries for any of
1642/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001643void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001644 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001645 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001646 RecordData Record;
1647
Chris Lattner0910e3b2009-04-10 17:16:57 +00001648 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001649 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001650
1651 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001652 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1653 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1654 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001655 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001656
Douglas Gregor258ae542009-04-27 06:38:32 +00001657 // Write out the source location entry table. We skip the first
1658 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001659 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001660 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001661 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1662 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001663 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001664 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001665 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001666 FileID FID = FileID::get(I);
1667 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001668
Douglas Gregor258ae542009-04-27 06:38:32 +00001669 // Record the offset of this source-location entry.
1670 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1671
1672 // Figure out which record code to use.
1673 unsigned Code;
1674 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001675 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1676 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001677 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001678 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001679 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001680 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001681 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001682 Record.clear();
1683 Record.push_back(Code);
1684
Douglas Gregor925296b2011-07-19 16:10:42 +00001685 // Starting offset of this entry within this module, so skip the dummy.
1686 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001687 if (SLoc->isFile()) {
1688 const SrcMgr::FileInfo &File = SLoc->getFile();
1689 Record.push_back(File.getIncludeLoc().getRawEncoding());
1690 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1691 Record.push_back(File.hasLineDirectives());
1692
1693 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001694 if (Content->OrigEntry) {
1695 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001696 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001697
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001698 // The source location entry is a file. Emit input file ID.
1699 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1700 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001701
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001702 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001703
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001704 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001705 if (FDI != FileDeclIDs.end()) {
1706 Record.push_back(FDI->second->FirstDeclIndex);
1707 Record.push_back(FDI->second->DeclIDs.size());
1708 } else {
1709 Record.push_back(0);
1710 Record.push_back(0);
1711 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001712
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001713 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001714
1715 if (Content->BufferOverridden) {
1716 Record.clear();
1717 Record.push_back(SM_SLOC_BUFFER_BLOB);
1718 const llvm::MemoryBuffer *Buffer
1719 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1720 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1721 StringRef(Buffer->getBufferStart(),
1722 Buffer->getBufferSize() + 1));
1723 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001724 } else {
1725 // The source location entry is a buffer. The blob associated
1726 // with this entry contains the contents of the buffer.
1727
1728 // We add one to the size so that we capture the trailing NULL
1729 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1730 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001731 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001732 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001733 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001734 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001735 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001736 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001737 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001738 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001739 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001740 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001741
Douglas Gregor925296b2011-07-19 16:10:42 +00001742 if (strcmp(Name, "<built-in>") == 0) {
1743 PreloadSLocs.push_back(SLocEntryOffsets.size());
1744 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001745 }
1746 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001747 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001748 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001749 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1750 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001751 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1752 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001753
1754 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001755 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001756 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001757 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001758 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001759 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001760 }
1761 }
1762
Douglas Gregor8f45df52009-04-16 22:23:12 +00001763 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001764
1765 if (SLocEntryOffsets.empty())
1766 return;
1767
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001768 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001769 // table is used for lazily loading source-location information.
1770 using namespace llvm;
1771 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001772 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1776 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001777
Douglas Gregor258ae542009-04-27 06:38:32 +00001778 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001779 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001780 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001781 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001782 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001783
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001784 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001785 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001786 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001787
1788 // Write the line table. It depends on remapping working, so it must come
1789 // after the source location offsets.
1790 if (SourceMgr.hasLineTable()) {
1791 LineTableInfo &LineTable = SourceMgr.getLineTable();
1792
1793 Record.clear();
1794 // Emit the file names
1795 Record.push_back(LineTable.getNumFilenames());
1796 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1797 // Emit the file name
1798 const char *Filename = LineTable.getFilename(I);
1799 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1800 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1801 Record.push_back(FilenameLen);
1802 if (FilenameLen)
1803 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1804 }
1805
1806 // Emit the line entries
1807 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1808 L != LEnd; ++L) {
1809 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001810 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001811 continue;
1812
1813 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001814 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001815
1816 // Emit the line entries
1817 Record.push_back(L->second.size());
1818 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1819 LEEnd = L->second.end();
1820 LE != LEEnd; ++LE) {
1821 Record.push_back(LE->FileOffset);
1822 Record.push_back(LE->LineNo);
1823 Record.push_back(LE->FilenameID);
1824 Record.push_back((unsigned)LE->FileKind);
1825 Record.push_back(LE->IncludeOffset);
1826 }
1827 }
1828 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1829 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001830}
1831
Douglas Gregorc5046832009-04-27 18:38:38 +00001832//===----------------------------------------------------------------------===//
1833// Preprocessor Serialization
1834//===----------------------------------------------------------------------===//
1835
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001836namespace {
1837class ASTMacroTableTrait {
1838public:
1839 typedef IdentID key_type;
1840 typedef key_type key_type_ref;
1841
1842 struct Data {
1843 uint32_t MacroDirectivesOffset;
1844 };
1845
1846 typedef Data data_type;
1847 typedef const data_type &data_type_ref;
1848
1849 static unsigned ComputeHash(IdentID IdID) {
1850 return llvm::hash_value(IdID);
1851 }
1852
1853 std::pair<unsigned,unsigned>
1854 static EmitKeyDataLength(raw_ostream& Out,
1855 key_type_ref Key, data_type_ref Data) {
1856 unsigned KeyLen = 4; // IdentID.
1857 unsigned DataLen = 4; // MacroDirectivesOffset.
1858 return std::make_pair(KeyLen, DataLen);
1859 }
1860
1861 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1862 clang::io::Emit32(Out, Key);
1863 }
1864
1865 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1866 unsigned) {
1867 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1868 }
1869};
1870} // end anonymous namespace
1871
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001872static int compareMacroDirectives(
1873 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1874 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1875 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001876}
1877
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001878static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1879 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001880 if (MacroInfo *MI = MD->getMacroInfo())
1881 if (MI->isBuiltinMacro())
1882 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001883
1884 if (IsModule) {
1885 SourceLocation Loc = MD->getLocation();
1886 if (Loc.isInvalid())
1887 return true;
1888 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1889 return true;
1890 }
1891
1892 return false;
1893}
1894
Chris Lattnereeffaef2009-04-10 17:15:23 +00001895/// \brief Writes the block containing the serialized form of the
1896/// preprocessor.
1897///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001898void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001899 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1900 if (PPRec)
1901 WritePreprocessorDetail(*PPRec);
1902
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001903 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001904
Chris Lattner0af3ba12009-04-13 01:29:17 +00001905 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1906 if (PP.getCounterValue() != 0) {
1907 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001908 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001909 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001910 }
1911
1912 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001913 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001914
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001915 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001916 // FIXME: use diagnostics subsystem for localization etc.
1917 if (PP.SawDateOrTime())
1918 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001919
Douglas Gregor796d76a2010-10-20 22:00:55 +00001920
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001921 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001922 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001923
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001924 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001925 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001926 MacroDirectives;
1927 for (Preprocessor::macro_iterator
1928 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1929 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001930 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001931 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001932 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001933
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001934 // Sort the set of macro definitions that need to be serialized by the
1935 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001936 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1937 &compareMacroDirectives);
1938
1939 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1940
1941 // Emit the macro directives as a list and associate the offset with the
1942 // identifier they belong to.
1943 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1944 const IdentifierInfo *Name = MacroDirectives[I].first;
1945 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1946 MacroDirective *MD = MacroDirectives[I].second;
1947
1948 // If the macro or identifier need no updates, don't write the macro history
1949 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001950 // FIXME: Chain the macro history instead of re-writing it.
1951 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001952 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1953 continue;
1954
1955 // Emit the macro directives in reverse source order.
1956 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001957 if (MD->isHidden())
1958 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001959 if (shouldIgnoreMacro(MD, IsModule, PP))
1960 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001961
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001962 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001963 Record.push_back(MD->getKind());
1964 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1965 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1966 Record.push_back(InfoID);
1967 Record.push_back(DefMD->isImported());
1968 Record.push_back(DefMD->isAmbiguous());
1969
1970 } else if (VisibilityMacroDirective *
1971 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1972 Record.push_back(VisMD->isPublic());
1973 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001974 }
1975 if (Record.empty())
1976 continue;
1977
1978 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1979 Record.clear();
1980
1981 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1982
1983 IdentID NameID = getIdentifierRef(Name);
1984 ASTMacroTableTrait::Data data;
1985 data.MacroDirectivesOffset = MacroDirectiveOffset;
1986 Generator.insert(NameID, data);
1987 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001988
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001989 /// \brief Offsets of each of the macros into the bitstream, indexed by
1990 /// the local macro ID
1991 ///
1992 /// For each identifier that is associated with a macro, this map
1993 /// provides the offset into the bitstream where that macro is
1994 /// defined.
1995 std::vector<uint32_t> MacroOffsets;
1996
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001997 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1998 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1999 MacroInfo *MI = MacroInfosToEmit[I].MI;
2000 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002001
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002002 if (ID < FirstMacroID) {
2003 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2004 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002005 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002006
2007 // Record the local offset of this macro.
2008 unsigned Index = ID - FirstMacroID;
2009 if (Index == MacroOffsets.size())
2010 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2011 else {
2012 if (Index > MacroOffsets.size())
2013 MacroOffsets.resize(Index + 1);
2014
2015 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2016 }
2017
2018 AddIdentifierRef(Name, Record);
2019 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2020 AddSourceLocation(MI->getDefinitionLoc(), Record);
2021 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2022 Record.push_back(MI->isUsed());
2023 unsigned Code;
2024 if (MI->isObjectLike()) {
2025 Code = PP_MACRO_OBJECT_LIKE;
2026 } else {
2027 Code = PP_MACRO_FUNCTION_LIKE;
2028
2029 Record.push_back(MI->isC99Varargs());
2030 Record.push_back(MI->isGNUVarargs());
2031 Record.push_back(MI->hasCommaPasting());
2032 Record.push_back(MI->getNumArgs());
2033 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2034 I != E; ++I)
2035 AddIdentifierRef(*I, Record);
2036 }
2037
2038 // If we have a detailed preprocessing record, record the macro definition
2039 // ID that corresponds to this macro.
2040 if (PPRec)
2041 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2042
2043 Stream.EmitRecord(Code, Record);
2044 Record.clear();
2045
2046 // Emit the tokens array.
2047 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2048 // Note that we know that the preprocessor does not have any annotation
2049 // tokens in it because they are created by the parser, and thus can't
2050 // be in a macro definition.
2051 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002052 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002053 Stream.EmitRecord(PP_TOKEN, Record);
2054 Record.clear();
2055 }
2056 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002057 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002058
Douglas Gregor92a96f52011-02-08 21:58:10 +00002059 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002060
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002061 // Create the on-disk hash table in a buffer.
2062 SmallString<4096> MacroTable;
2063 uint32_t BucketOffset;
2064 {
2065 llvm::raw_svector_ostream Out(MacroTable);
2066 // Make sure that no bucket is at offset 0
2067 clang::io::Emit32(Out, 0);
2068 BucketOffset = Generator.Emit(Out);
2069 }
2070
2071 // Write the macro table
2072 using namespace llvm;
2073 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2074 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2077 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2078
2079 Record.push_back(MACRO_TABLE);
2080 Record.push_back(BucketOffset);
2081 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2082 Record.clear();
2083
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002084 // Write the offsets table for macro IDs.
2085 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002086 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002087 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2091
2092 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2093 Record.clear();
2094 Record.push_back(MACRO_OFFSET);
2095 Record.push_back(MacroOffsets.size());
2096 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2097 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2098 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002099}
2100
2101void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002102 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002103 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002104
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002105 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002106
Douglas Gregor92a96f52011-02-08 21:58:10 +00002107 // Enter the preprocessor block.
2108 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002109
Douglas Gregoraae92242010-03-19 21:51:54 +00002110 // If the preprocessor has a preprocessing record, emit it.
2111 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002112 using namespace llvm;
2113
2114 // Set up the abbreviation for
2115 unsigned InclusionAbbrev = 0;
2116 {
2117 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2118 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002119 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2120 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2124 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2125 }
2126
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002127 unsigned FirstPreprocessorEntityID
2128 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2129 + NUM_PREDEF_PP_ENTITY_IDS;
2130 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002131 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002132 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2133 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002134 E != EEnd;
2135 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002136 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002137
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002138 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2139 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002140
Douglas Gregor92a96f52011-02-08 21:58:10 +00002141 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002142 // Record this macro definition's ID.
2143 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002144
Douglas Gregor92a96f52011-02-08 21:58:10 +00002145 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002146 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2147 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002148 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002149
Chandler Carrutha88a22182011-07-14 08:20:46 +00002150 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002151 Record.push_back(ME->isBuiltinMacro());
2152 if (ME->isBuiltinMacro())
2153 AddIdentifierRef(ME->getName(), Record);
2154 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002155 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002156 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002157 continue;
2158 }
2159
2160 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2161 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002162 Record.push_back(ID->getFileName().size());
2163 Record.push_back(ID->wasInQuotes());
2164 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002165 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002166 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002167 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002168 // Check that the FileEntry is not null because it was not resolved and
2169 // we create a PCH even with compiler errors.
2170 if (ID->getFile())
2171 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002172 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2173 continue;
2174 }
2175
2176 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2177 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002178 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002179
Douglas Gregoraae92242010-03-19 21:51:54 +00002180 // Write the offsets table for the preprocessing record.
2181 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002182 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2183
Douglas Gregoraae92242010-03-19 21:51:54 +00002184 // Write the offsets table for identifier IDs.
2185 using namespace llvm;
2186 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002187 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002190 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002191
Douglas Gregoraae92242010-03-19 21:51:54 +00002192 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002193 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002194 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002195 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2196 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002197 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002198}
2199
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002200unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2201 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2202 if (Known != SubmoduleIDs.end())
2203 return Known->second;
2204
2205 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2206}
2207
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002208unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2209 if (!Mod)
2210 return 0;
2211
2212 llvm::DenseMap<Module *, unsigned>::const_iterator
2213 Known = SubmoduleIDs.find(Mod);
2214 if (Known != SubmoduleIDs.end())
2215 return Known->second;
2216
2217 return 0;
2218}
2219
Douglas Gregor253eefe2011-12-01 00:59:36 +00002220/// \brief Compute the number of modules within the given tree (including the
2221/// given module).
2222static unsigned getNumberOfModules(Module *Mod) {
2223 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002224 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2225 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002226 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002227 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002228
2229 return ChildModules + 1;
2230}
2231
Douglas Gregorde3ef502011-11-30 23:21:26 +00002232void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002233 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002234 // FIXME: This feels like it belongs somewhere else, but there are no
2235 // other consumers of this information.
2236 SourceManager &SrcMgr = PP->getSourceManager();
2237 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2238 for (ASTContext::import_iterator I = Context->local_import_begin(),
2239 IEnd = Context->local_import_end();
2240 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002241 if (Module *ImportedFrom
2242 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2243 SrcMgr))) {
2244 ImportedFrom->Imports.push_back(I->getImportedModule());
2245 }
2246 }
2247
Douglas Gregor69021972011-11-30 17:33:56 +00002248 // Enter the submodule description block.
2249 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2250
2251 // Write the abbreviations needed for the submodules block.
2252 using namespace llvm;
2253 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2254 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2265 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2266
2267 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002268 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2270 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2271
2272 Abbrev = new BitCodeAbbrev();
2273 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2275 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002276
2277 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002278 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2280 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2281
2282 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002283 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2285 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2286
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002287 Abbrev = new BitCodeAbbrev();
2288 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002291 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2292
Douglas Gregor59527662012-10-15 06:28:11 +00002293 Abbrev = new BitCodeAbbrev();
2294 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2296 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2297
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002298 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002299 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2301 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2302
2303 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002304 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2307 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2308
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002309 Abbrev = new BitCodeAbbrev();
2310 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2312 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2313
Douglas Gregorfb912652013-03-20 21:10:35 +00002314 Abbrev = new BitCodeAbbrev();
2315 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2318 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2319
Douglas Gregor253eefe2011-12-01 00:59:36 +00002320 // Write the submodule metadata block.
2321 RecordData Record;
2322 Record.push_back(getNumberOfModules(WritingModule));
2323 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2324 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2325
Douglas Gregor69021972011-11-30 17:33:56 +00002326 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002327 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002328 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002329 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002330 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002331 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002332 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002333
2334 // Emit the definition of the block.
2335 Record.clear();
2336 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002337 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002338 if (Mod->Parent) {
2339 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2340 Record.push_back(SubmoduleIDs[Mod->Parent]);
2341 } else {
2342 Record.push_back(0);
2343 }
2344 Record.push_back(Mod->IsFramework);
2345 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002346 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002347 Record.push_back(Mod->InferSubmodules);
2348 Record.push_back(Mod->InferExplicitSubmodules);
2349 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002350 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002351 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2352
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002353 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002354 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002355 Record.clear();
2356 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002357 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002358 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002359 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002360 }
2361
Douglas Gregor69021972011-11-30 17:33:56 +00002362 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002363 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002364 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002365 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002366 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002367 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002368 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2369 Record.clear();
2370 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2371 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2372 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002373 }
2374
2375 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002376 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002377 Record.clear();
2378 Record.push_back(SUBMODULE_HEADER);
2379 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002380 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002381 }
Douglas Gregor59527662012-10-15 06:28:11 +00002382 // Emit the excluded headers.
2383 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2384 Record.clear();
2385 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2386 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2387 Mod->ExcludedHeaders[I]->getName());
2388 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002389 // Emit the private headers.
2390 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2391 Record.clear();
2392 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2393 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2394 Mod->PrivateHeaders[I]->getName());
2395 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002396 ArrayRef<const FileEntry *>
2397 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2398 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002399 Record.clear();
2400 Record.push_back(SUBMODULE_TOPHEADER);
2401 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002402 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002403 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002404
2405 // Emit the imports.
2406 if (!Mod->Imports.empty()) {
2407 Record.clear();
2408 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002409 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002410 assert(ImportedID && "Unknown submodule!");
2411 Record.push_back(ImportedID);
2412 }
2413 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2414 }
2415
Douglas Gregor24bb9232011-12-02 18:58:38 +00002416 // Emit the exports.
2417 if (!Mod->Exports.empty()) {
2418 Record.clear();
2419 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002420 if (Module *Exported = Mod->Exports[I].getPointer()) {
2421 unsigned ExportedID = SubmoduleIDs[Exported];
2422 assert(ExportedID > 0 && "Unknown submodule ID?");
2423 Record.push_back(ExportedID);
2424 } else {
2425 Record.push_back(0);
2426 }
2427
Douglas Gregor24bb9232011-12-02 18:58:38 +00002428 Record.push_back(Mod->Exports[I].getInt());
2429 }
2430 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2431 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002432
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002433 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2434 // Might be unnecessary as use declarations are only used to build the
2435 // module itself.
2436
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002437 // Emit the link libraries.
2438 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2439 Record.clear();
2440 Record.push_back(SUBMODULE_LINK_LIBRARY);
2441 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2442 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2443 Mod->LinkLibraries[I].Library);
2444 }
2445
Douglas Gregorfb912652013-03-20 21:10:35 +00002446 // Emit the conflicts.
2447 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2448 Record.clear();
2449 Record.push_back(SUBMODULE_CONFLICT);
2450 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2451 assert(OtherID && "Unknown submodule!");
2452 Record.push_back(OtherID);
2453 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2454 Mod->Conflicts[I].Message);
2455 }
2456
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002457 // Emit the configuration macros.
2458 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2459 Record.clear();
2460 Record.push_back(SUBMODULE_CONFIG_MACRO);
2461 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2462 Mod->ConfigMacros[I]);
2463 }
2464
Douglas Gregor69021972011-11-30 17:33:56 +00002465 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002466 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2467 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002468 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002469 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002470 }
2471
2472 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002473
2474 assert((NextSubmoduleID - FirstSubmoduleID
2475 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002476}
2477
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002478serialization::SubmoduleID
2479ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002480 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002481 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002482
2483 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002484 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002485 Module *OwningMod
2486 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002487 if (!OwningMod)
2488 return 0;
2489
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002490 // Check whether this submodule is part of our own module.
2491 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002492 return 0;
2493
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002494 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002495}
2496
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002497void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2498 bool isModule) {
2499 // Make sure set diagnostic pragmas don't affect the translation unit that
2500 // imports the module.
2501 // FIXME: Make diagnostic pragma sections work properly with modules.
2502 if (isModule)
2503 return;
2504
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002505 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2506 DiagStateIDMap;
2507 unsigned CurrID = 0;
2508 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002509 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002510 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002511 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2512 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002513 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002514 if (point.Loc.isInvalid())
2515 continue;
2516
2517 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002518 unsigned &DiagStateID = DiagStateIDMap[point.State];
2519 Record.push_back(DiagStateID);
2520
2521 if (DiagStateID == 0) {
2522 DiagStateID = ++CurrID;
2523 for (DiagnosticsEngine::DiagState::const_iterator
2524 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2525 if (I->second.isPragma()) {
2526 Record.push_back(I->first);
2527 Record.push_back(I->second.getMapping());
2528 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002529 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002530 Record.push_back(-1); // mark the end of the diag/map pairs for this
2531 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002532 }
2533 }
2534
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002535 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002536 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002537}
2538
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002539void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2540 if (CXXBaseSpecifiersOffsets.empty())
2541 return;
2542
2543 RecordData Record;
2544
2545 // Create a blob abbreviation for the C++ base specifiers offsets.
2546 using namespace llvm;
2547
2548 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2549 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2552 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2553
Douglas Gregorc27b2872011-08-04 00:01:48 +00002554 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002555 Record.clear();
2556 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2557 Record.push_back(CXXBaseSpecifiersOffsets.size());
2558 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002559 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002560}
2561
Douglas Gregorc5046832009-04-27 18:38:38 +00002562//===----------------------------------------------------------------------===//
2563// Type Serialization
2564//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002565
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002566/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002567void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002568 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002569 if (Idx.getIndex() == 0) // we haven't seen this type before.
2570 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002571
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002572 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002573
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002574 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002575 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002576 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002577 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002578 else if (TypeOffsets.size() < Index) {
2579 TypeOffsets.resize(Index + 1);
2580 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002581 }
2582
2583 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002584
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002585 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002586 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002587
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002588 if (T.hasLocalNonFastQualifiers()) {
2589 Qualifiers Qs = T.getLocalQualifiers();
2590 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002591 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002592 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002593 } else {
2594 switch (T->getTypeClass()) {
2595 // For all of the concrete, non-dependent types, call the
2596 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002597#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002598 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002599#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002600#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002601 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002602 }
2603
2604 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002605 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002606
2607 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002608 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002609}
2610
Douglas Gregorc5046832009-04-27 18:38:38 +00002611//===----------------------------------------------------------------------===//
2612// Declaration Serialization
2613//===----------------------------------------------------------------------===//
2614
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002615/// \brief Write the block containing all of the declaration IDs
2616/// lexically declared within the given DeclContext.
2617///
2618/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2619/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002620uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002621 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002622 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002623 return 0;
2624
Douglas Gregor8f45df52009-04-16 22:23:12 +00002625 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002626 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002627 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002628 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002629 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2630 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002631 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002632
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002633 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002634 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002635 return Offset;
2636}
2637
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002638void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002639 using namespace llvm;
2640 RecordData Record;
2641
2642 // Write the type offsets array
2643 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002644 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002647 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2648 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2649 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002650 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002651 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002652 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002653 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002654
2655 // Write the declaration offsets array
2656 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002657 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002658 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002659 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002660 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2661 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2662 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002663 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002664 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002665 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002666 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002667}
2668
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002669void ASTWriter::WriteFileDeclIDsMap() {
2670 using namespace llvm;
2671 RecordData Record;
2672
2673 // Join the vectors of DeclIDs from all files.
2674 SmallVector<DeclID, 256> FileSortedIDs;
2675 for (FileDeclIDsTy::iterator
2676 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2677 DeclIDInFileInfo &Info = *FI->second;
2678 Info.FirstDeclIndex = FileSortedIDs.size();
2679 for (LocDeclIDsTy::iterator
2680 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2681 FileSortedIDs.push_back(DI->second);
2682 }
2683
2684 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2685 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2688 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2689 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002690 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002691 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2692}
2693
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002694void ASTWriter::WriteComments() {
2695 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002696 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002697 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002698 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2699 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002700 I != E; ++I) {
2701 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002702 AddSourceRange((*I)->getSourceRange(), Record);
2703 Record.push_back((*I)->getKind());
2704 Record.push_back((*I)->isTrailingComment());
2705 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002706 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2707 }
2708 Stream.ExitBlock();
2709}
2710
Douglas Gregorc5046832009-04-27 18:38:38 +00002711//===----------------------------------------------------------------------===//
2712// Global Method Pool and Selector Serialization
2713//===----------------------------------------------------------------------===//
2714
Douglas Gregore84a9da2009-04-20 20:36:09 +00002715namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002716// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002717class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002718 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002719
2720public:
2721 typedef Selector key_type;
2722 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002723
Sebastian Redl834bb972010-08-04 17:20:04 +00002724 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002725 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002726 ObjCMethodList Instance, Factory;
2727 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002728 typedef const data_type& data_type_ref;
2729
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002730 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002731
Douglas Gregorc78d3462009-04-24 21:10:55 +00002732 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002733 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002734 }
Mike Stump11289f42009-09-09 15:08:12 +00002735
2736 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002737 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002738 data_type_ref Methods) {
2739 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2740 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002741 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2742 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002743 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002744 if (Method->Method)
2745 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002746 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002747 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002748 if (Method->Method)
2749 DataLen += 4;
2750 clang::io::Emit16(Out, DataLen);
2751 return std::make_pair(KeyLen, DataLen);
2752 }
Mike Stump11289f42009-09-09 15:08:12 +00002753
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002754 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002755 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002756 assert((Start >> 32) == 0 && "Selector key offset too large");
2757 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002758 unsigned N = Sel.getNumArgs();
2759 clang::io::Emit16(Out, N);
2760 if (N == 0)
2761 N = 1;
2762 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002763 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002764 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2765 }
Mike Stump11289f42009-09-09 15:08:12 +00002766
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002767 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002768 data_type_ref Methods, unsigned DataLen) {
2769 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002770 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002771 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002772 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002773 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002774 if (Method->Method)
2775 ++NumInstanceMethods;
2776
2777 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002778 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002779 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002780 if (Method->Method)
2781 ++NumFactoryMethods;
2782
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002783 unsigned InstanceBits = Methods.Instance.getBits();
2784 assert(InstanceBits < 4);
2785 unsigned NumInstanceMethodsAndBits =
2786 (NumInstanceMethods << 2) | InstanceBits;
2787 unsigned FactoryBits = Methods.Factory.getBits();
2788 assert(FactoryBits < 4);
2789 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2790 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2791 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002792 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002793 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002794 if (Method->Method)
2795 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002796 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002797 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002798 if (Method->Method)
2799 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002800
2801 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002802 }
2803};
2804} // end anonymous namespace
2805
Sebastian Redla19a67f2010-08-03 21:58:15 +00002806/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002807///
2808/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002809/// in an on-disk hash table indexed by the selector. The hash table also
2810/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002811void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002812 using namespace llvm;
2813
Sebastian Redla19a67f2010-08-03 21:58:15 +00002814 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002815 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002816 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002817 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002818 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002819 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002820 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002821 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002822
Sebastian Redla19a67f2010-08-03 21:58:15 +00002823 // Create the on-disk hash table representation. We walk through every
2824 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002825 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002826 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002827 I = SelectorIDs.begin(), E = SelectorIDs.end();
2828 I != E; ++I) {
2829 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002830 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002831 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002832 I->second,
2833 ObjCMethodList(),
2834 ObjCMethodList()
2835 };
2836 if (F != SemaRef.MethodPool.end()) {
2837 Data.Instance = F->second.first;
2838 Data.Factory = F->second.second;
2839 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002840 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002841 // changed.
2842 if (Chain && I->second < FirstSelectorID) {
2843 // Selector already exists. Did it change?
2844 bool changed = false;
2845 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002846 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002847 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002848 changed = true;
2849 }
2850 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002851 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002852 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002853 changed = true;
2854 }
2855 if (!changed)
2856 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002857 } else if (Data.Instance.Method || Data.Factory.Method) {
2858 // A new method pool entry.
2859 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002860 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002861 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002862 }
2863
Douglas Gregorc78d3462009-04-24 21:10:55 +00002864 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002865 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002866 uint32_t BucketOffset;
2867 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002868 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002869 llvm::raw_svector_ostream Out(MethodPool);
2870 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002871 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002872 BucketOffset = Generator.Emit(Out, Trait);
2873 }
2874
2875 // Create a blob abbreviation
2876 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002877 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2881 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2882
Douglas Gregor95c13f52009-04-25 17:48:32 +00002883 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002884 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002885 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002886 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002887 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002888 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002889
2890 // Create a blob abbreviation for the selector table offsets.
2891 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002892 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002893 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002894 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2896 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2897
2898 // Write the selector offsets table.
2899 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002900 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002901 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002902 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002903 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002904 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002905 }
2906}
2907
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002908/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002909void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002910 using namespace llvm;
2911 if (SemaRef.ReferencedSelectors.empty())
2912 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002913
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002914 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002915
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002916 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002917 // very tricky to fix, and given that @selector shouldn't really appear in
2918 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002919 for (DenseMap<Selector, SourceLocation>::iterator S =
2920 SemaRef.ReferencedSelectors.begin(),
2921 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2922 Selector Sel = (*S).first;
2923 SourceLocation Loc = (*S).second;
2924 AddSelectorRef(Sel, Record);
2925 AddSourceLocation(Loc, Record);
2926 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002927 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002928}
2929
Douglas Gregorc5046832009-04-27 18:38:38 +00002930//===----------------------------------------------------------------------===//
2931// Identifier Table Serialization
2932//===----------------------------------------------------------------------===//
2933
Douglas Gregorc78d3462009-04-24 21:10:55 +00002934namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002935class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002936 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002937 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002938 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002939 bool IsModule;
2940
Douglas Gregor1d583f22009-04-28 21:18:29 +00002941 /// \brief Determines whether this is an "interesting" identifier
2942 /// that needs a full IdentifierInfo structure written into the hash
2943 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002944 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002945 if (II->isPoisoned() ||
2946 II->isExtensionToken() ||
2947 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002948 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002949 II->getFETokenInfo<void>())
2950 return true;
2951
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002952 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002953 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002954
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002955 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002956 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002957 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002958
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002959 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2960 if (!IsModule)
2961 return !shouldIgnoreMacro(Macro, IsModule, PP);
2962 SubmoduleID ModID;
2963 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2964 return true;
2965 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002966
2967 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002968 }
2969
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002970 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2971 SubmoduleID &ModID) {
2972 ModID = 0;
2973 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2974 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2975 return DefMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002976 return 0;
2977 }
2978
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002979 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2980 SubmoduleID &ModID) {
2981 if (DefMacroDirective *
2982 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2983 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2984 return DefMD;
2985 return 0;
2986 }
2987
2988 /// \brief Traverses the macro directives history and returns the latest
2989 /// macro that is public and not undefined in the same submodule.
2990 /// A macro that is defined in submodule A and undefined in submodule B,
2991 /// will still be considered as defined/exported from submodule A.
2992 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2993 SubmoduleID &ModID) {
2994 if (!MD)
2995 return 0;
2996
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00002997 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002998 bool isUndefined = false;
2999 Optional<bool> isPublic;
3000 for (; MD; MD = MD->getPrevious()) {
3001 if (MD->isHidden())
3002 continue;
3003
3004 SubmoduleID ThisModID = getSubmoduleID(MD);
3005 if (ThisModID == 0) {
3006 isUndefined = false;
3007 isPublic = Optional<bool>();
3008 continue;
3009 }
3010 if (ThisModID != ModID){
3011 ModID = ThisModID;
3012 isUndefined = false;
3013 isPublic = Optional<bool>();
3014 }
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003015 // We are looking for a definition in a different submodule than the one
3016 // that we started with. If a submodule has re-definitions of the same
3017 // macro, only the last definition will be used as the "exported" one.
3018 if (ModID == OrigModID)
3019 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003020
3021 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3022 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
3023 return DefMD;
3024 continue;
3025 }
3026
3027 if (isa<UndefMacroDirective>(MD)) {
3028 isUndefined = true;
3029 continue;
3030 }
3031
3032 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
3033 if (!isPublic.hasValue())
3034 isPublic = VisMD->isPublic();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003035 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003036
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003037 return 0;
3038 }
3039
3040 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003041 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3042 MacroInfo *MI = DefMD->getInfo();
3043 if (unsigned ID = MI->getOwningModuleID())
3044 return ID;
3045 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
3046 }
3047 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003048 }
3049
Douglas Gregore84a9da2009-04-20 20:36:09 +00003050public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003051 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003052 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003053
Sebastian Redl539c5062010-08-18 23:57:32 +00003054 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003055 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003056
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003057 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3058 IdentifierResolver &IdResolver, bool IsModule)
3059 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003060
3061 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003062 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003063 }
Mike Stump11289f42009-09-09 15:08:12 +00003064
3065 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003066 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003067 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003068 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003069 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003070 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003071 DataLen += 2; // 2 bytes for builtin ID
3072 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003073 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003074 DataLen += 4; // MacroDirectives offset.
3075 if (IsModule) {
3076 SubmoduleID ModID;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003077 for (DefMacroDirective *
3078 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3079 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003080 DataLen += 4; // MacroInfo ID.
3081 }
3082 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003083 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003084 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003085
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003086 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3087 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003088 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003089 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003090 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003091 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003092 // We emit the key length after the data length so that every
3093 // string is preceded by a 16-bit length. This matches the PTH
3094 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003095 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003096 return std::make_pair(KeyLen, DataLen);
3097 }
Mike Stump11289f42009-09-09 15:08:12 +00003098
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003099 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003100 unsigned KeyLen) {
3101 // Record the location of the key data. This is used when generating
3102 // the mapping from persistent IDs to strings.
3103 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003104 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003105 }
Mike Stump11289f42009-09-09 15:08:12 +00003106
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003107 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003108 IdentID ID, unsigned) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003109 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003110 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00003111 clang::io::Emit32(Out, ID << 1);
3112 return;
3113 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003114
Douglas Gregor1d583f22009-04-28 21:18:29 +00003115 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003116 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3117 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3118 clang::io::Emit16(Out, Bits);
3119 Bits = 0;
3120 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003121 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003122 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003123 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3124 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003125 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003126 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00003127 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003128
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003129 if (HadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003130 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3131 if (IsModule) {
3132 // Write the IDs of macros coming from different submodules.
3133 SubmoduleID ModID;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003134 for (DefMacroDirective *
3135 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3136 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3137 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003138 assert(InfoID);
3139 clang::io::Emit32(Out, InfoID);
3140 }
3141 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003142 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003143 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003144
Douglas Gregora868bbd2009-04-21 22:25:48 +00003145 // Emit the declaration IDs in reverse order, because the
3146 // IdentifierResolver provides the declarations as they would be
3147 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003148 // "stat"), but the ASTReader adds declarations to the end of the list
3149 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003150 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003151 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3152 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003153 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003154 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003155 D != DEnd; ++D)
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003156 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3157 }
3158
3159 /// \brief Returns the most recent local decl or the given decl if there are
3160 /// no local ones. The given decl is assumed to be the most recent one.
3161 Decl *getMostRecentLocalDecl(Decl *Orig) {
3162 // The only way a "from AST file" decl would be more recent from a local one
3163 // is if it came from a module.
3164 if (!PP.getLangOpts().Modules)
3165 return Orig;
3166
3167 // Look for a local in the decl chain.
3168 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3169 if (!D->isFromASTFile())
3170 return D;
3171 // If we come up a decl from a (chained-)PCH stop since we won't find a
3172 // local one.
3173 if (D->getOwningModuleID() == 0)
3174 break;
3175 }
3176
3177 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003178 }
3179};
3180} // end anonymous namespace
3181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003182/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003183///
3184/// The identifier table consists of a blob containing string data
3185/// (the actual identifiers themselves) and a separate "offsets" index
3186/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003187void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3188 IdentifierResolver &IdResolver,
3189 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003190 using namespace llvm;
3191
3192 // Create and write out the blob that contains the identifier
3193 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003194 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003195 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003196 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003197
Douglas Gregore6648fb2009-04-28 20:33:11 +00003198 // Look for any identifiers that were named while processing the
3199 // headers, but are otherwise not needed. We add these to the hash
3200 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003201 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003202 // file.
3203 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3204 IDEnd = PP.getIdentifierTable().end();
3205 ID != IDEnd; ++ID)
3206 getIdentifierRef(ID->second);
3207
Sebastian Redlff4a2952010-07-23 23:49:55 +00003208 // Create the on-disk hash table representation. We only store offsets
3209 // for identifiers that appear here for the first time.
3210 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003211 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003212 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3213 ID != IDEnd; ++ID) {
3214 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003215 if (!Chain || !ID->first->isFromAST() ||
3216 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003217 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003218 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003219 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003220
Douglas Gregore84a9da2009-04-20 20:36:09 +00003221 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003222 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003223 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003224 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003225 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003226 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003227 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003228 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003229 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003230 }
3231
3232 // Create a blob abbreviation
3233 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003234 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003237 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003238
3239 // Write the identifier table
3240 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003241 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003242 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003243 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003244 }
3245
3246 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003247 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003248 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003250 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3252 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3253
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003254#ifndef NDEBUG
3255 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3256 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3257#endif
3258
Douglas Gregor0e149972009-04-25 19:10:14 +00003259 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003260 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003261 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003262 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003263 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003264 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003265}
3266
Douglas Gregorc5046832009-04-27 18:38:38 +00003267//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003268// DeclContext's Name Lookup Table Serialization
3269//===----------------------------------------------------------------------===//
3270
3271namespace {
3272// Trait used for the on-disk hash table used in the method pool.
3273class ASTDeclContextNameLookupTrait {
3274 ASTWriter &Writer;
3275
3276public:
3277 typedef DeclarationName key_type;
3278 typedef key_type key_type_ref;
3279
3280 typedef DeclContext::lookup_result data_type;
3281 typedef const data_type& data_type_ref;
3282
3283 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3284
3285 unsigned ComputeHash(DeclarationName Name) {
3286 llvm::FoldingSetNodeID ID;
3287 ID.AddInteger(Name.getNameKind());
3288
3289 switch (Name.getNameKind()) {
3290 case DeclarationName::Identifier:
3291 ID.AddString(Name.getAsIdentifierInfo()->getName());
3292 break;
3293 case DeclarationName::ObjCZeroArgSelector:
3294 case DeclarationName::ObjCOneArgSelector:
3295 case DeclarationName::ObjCMultiArgSelector:
3296 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3297 break;
3298 case DeclarationName::CXXConstructorName:
3299 case DeclarationName::CXXDestructorName:
3300 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003301 break;
3302 case DeclarationName::CXXOperatorName:
3303 ID.AddInteger(Name.getCXXOverloadedOperator());
3304 break;
3305 case DeclarationName::CXXLiteralOperatorName:
3306 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3307 case DeclarationName::CXXUsingDirective:
3308 break;
3309 }
3310
3311 return ID.ComputeHash();
3312 }
3313
3314 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003315 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003316 data_type_ref Lookup) {
3317 unsigned KeyLen = 1;
3318 switch (Name.getNameKind()) {
3319 case DeclarationName::Identifier:
3320 case DeclarationName::ObjCZeroArgSelector:
3321 case DeclarationName::ObjCOneArgSelector:
3322 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003323 case DeclarationName::CXXLiteralOperatorName:
3324 KeyLen += 4;
3325 break;
3326 case DeclarationName::CXXOperatorName:
3327 KeyLen += 1;
3328 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003329 case DeclarationName::CXXConstructorName:
3330 case DeclarationName::CXXDestructorName:
3331 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003332 case DeclarationName::CXXUsingDirective:
3333 break;
3334 }
3335 clang::io::Emit16(Out, KeyLen);
3336
3337 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003338 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003339 clang::io::Emit16(Out, DataLen);
3340
3341 return std::make_pair(KeyLen, DataLen);
3342 }
3343
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003344 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003345 using namespace clang::io;
3346
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003347 Emit8(Out, Name.getNameKind());
3348 switch (Name.getNameKind()) {
3349 case DeclarationName::Identifier:
3350 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003351 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003352 case DeclarationName::ObjCZeroArgSelector:
3353 case DeclarationName::ObjCOneArgSelector:
3354 case DeclarationName::ObjCMultiArgSelector:
3355 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003356 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003357 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003358 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3359 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003360 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003361 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003362 case DeclarationName::CXXLiteralOperatorName:
3363 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003364 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003365 case DeclarationName::CXXConstructorName:
3366 case DeclarationName::CXXDestructorName:
3367 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003368 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003369 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003370 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003371
3372 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003373 }
3374
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003375 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003376 data_type Lookup, unsigned DataLen) {
3377 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003378 clang::io::Emit16(Out, Lookup.size());
3379 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3380 I != E; ++I)
3381 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003382
3383 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3384 }
3385};
3386} // end anonymous namespace
3387
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003388/// \brief Write the block containing all of the declaration IDs
3389/// visible from the given DeclContext.
3390///
3391/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003392/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003393uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3394 DeclContext *DC) {
3395 if (DC->getPrimaryContext() != DC)
3396 return 0;
3397
3398 // Since there is no name lookup into functions or methods, don't bother to
3399 // build a visible-declarations table for these entities.
3400 if (DC->isFunctionOrMethod())
3401 return 0;
3402
3403 // If not in C++, we perform name lookup for the translation unit via the
3404 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003405 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003406 return 0;
3407
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003408 // Serialize the contents of the mapping used for lookup. Note that,
3409 // although we have two very different code paths, the serialized
3410 // representation is the same for both cases: a declaration name,
3411 // followed by a size, followed by references to the visible
3412 // declarations that have that name.
3413 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003414 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003415 if (!Map || Map->empty())
3416 return 0;
3417
3418 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3419 ASTDeclContextNameLookupTrait Trait(*this);
3420
3421 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003422 DeclarationName ConversionName;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003423 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003424 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3425 D != DEnd; ++D) {
3426 DeclarationName Name = D->first;
3427 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikieff7d47a2012-12-19 00:45:41 +00003428 if (!Result.empty()) {
Douglas Gregor05ef9312011-08-30 20:49:19 +00003429 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3430 // Hash all conversion function names to the same name. The actual
3431 // type information in conversion function name is not used in the
3432 // key (since such type information is not stable across different
3433 // modules), so the intended effect is to coalesce all of the conversion
3434 // functions under a single key.
3435 if (!ConversionName)
3436 ConversionName = Name;
David Blaikieff7d47a2012-12-19 00:45:41 +00003437 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregor05ef9312011-08-30 20:49:19 +00003438 continue;
3439 }
3440
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003441 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003442 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003443 }
3444
Douglas Gregor05ef9312011-08-30 20:49:19 +00003445 // Add the conversion functions
3446 if (!ConversionDecls.empty()) {
3447 Generator.insert(ConversionName,
3448 DeclContext::lookup_result(ConversionDecls.begin(),
3449 ConversionDecls.end()),
3450 Trait);
3451 }
3452
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003453 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003454 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003455 uint32_t BucketOffset;
3456 {
3457 llvm::raw_svector_ostream Out(LookupTable);
3458 // Make sure that no bucket is at offset 0
3459 clang::io::Emit32(Out, 0);
3460 BucketOffset = Generator.Emit(Out, Trait);
3461 }
3462
3463 // Write the lookup table
3464 RecordData Record;
3465 Record.push_back(DECL_CONTEXT_VISIBLE);
3466 Record.push_back(BucketOffset);
3467 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3468 LookupTable.str());
3469
3470 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3471 ++NumVisibleDeclContexts;
3472 return Offset;
3473}
3474
Sebastian Redla4071b42010-08-24 00:50:09 +00003475/// \brief Write an UPDATE_VISIBLE block for the given context.
3476///
3477/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3478/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003479/// (in C++), for namespaces, and for classes with forward-declared unscoped
3480/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003481void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003482 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3483 if (!Map || Map->empty())
3484 return;
3485
3486 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3487 ASTDeclContextNameLookupTrait Trait(*this);
3488
3489 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003490 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3491 D != DEnd; ++D) {
3492 DeclarationName Name = D->first;
3493 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003494 // For any name that appears in this table, the results are complete, i.e.
3495 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikieff7d47a2012-12-19 00:45:41 +00003496 if (!Result.empty())
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003497 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003498 }
3499
3500 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003501 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003502 uint32_t BucketOffset;
3503 {
3504 llvm::raw_svector_ostream Out(LookupTable);
3505 // Make sure that no bucket is at offset 0
3506 clang::io::Emit32(Out, 0);
3507 BucketOffset = Generator.Emit(Out, Trait);
3508 }
3509
3510 // Write the lookup table
3511 RecordData Record;
3512 Record.push_back(UPDATE_VISIBLE);
3513 Record.push_back(getDeclID(cast<Decl>(DC)));
3514 Record.push_back(BucketOffset);
3515 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3516}
3517
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003518/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3519void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3520 RecordData Record;
3521 Record.push_back(Opts.fp_contract);
3522 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3523}
3524
3525/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3526void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003527 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003528 return;
3529
3530 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3531 RecordData Record;
3532#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3533#include "clang/Basic/OpenCLExtensions.def"
3534 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3535}
3536
Douglas Gregor358cd442012-01-15 16:58:34 +00003537void ASTWriter::WriteRedeclarations() {
3538 RecordData LocalRedeclChains;
3539 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3540
3541 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3542 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003543 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003544
3545 Decl *MostRecent = First->getMostRecentDecl();
3546
3547 // If we only have a single declaration, there is no point in storing
3548 // a redeclaration chain.
3549 if (First == MostRecent)
3550 continue;
3551
3552 unsigned Offset = LocalRedeclChains.size();
3553 unsigned Size = 0;
3554 LocalRedeclChains.push_back(0); // Placeholder for the size.
3555
3556 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003557 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003558 Prev = Prev->getPreviousDecl()) {
3559 if (!Prev->isFromASTFile()) {
3560 AddDeclRef(Prev, LocalRedeclChains);
3561 ++Size;
3562 }
3563 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003564
3565 if (!First->isFromASTFile() && Chain) {
3566 Decl *FirstFromAST = MostRecent;
3567 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3568 if (Prev->isFromASTFile())
3569 FirstFromAST = Prev;
3570 }
3571
3572 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3573 }
3574
Douglas Gregor358cd442012-01-15 16:58:34 +00003575 LocalRedeclChains[Offset] = Size;
3576
3577 // Reverse the set of local redeclarations, so that we store them in
3578 // order (since we found them in reverse order).
3579 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3580
Douglas Gregor6168bd22013-02-18 15:53:43 +00003581 // Add the mapping from the first ID from the AST to the set of local
3582 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003583 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3584 LocalRedeclsMap.push_back(Info);
3585
3586 assert(N == Redeclarations.size() &&
3587 "Deserialized a declaration we shouldn't have");
3588 }
3589
3590 if (LocalRedeclChains.empty())
3591 return;
3592
3593 // Sort the local redeclarations map by the first declaration ID,
3594 // since the reader will be performing binary searches on this information.
3595 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3596
3597 // Emit the local redeclarations map.
3598 using namespace llvm;
3599 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3600 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3603 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3604
3605 RecordData Record;
3606 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3607 Record.push_back(LocalRedeclsMap.size());
3608 Stream.EmitRecordWithBlob(AbbrevID, Record,
3609 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3610 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3611
3612 // Emit the redeclaration chains.
3613 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3614}
3615
Douglas Gregor404cdde2012-01-27 01:47:08 +00003616void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003617 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003618 RecordData Categories;
3619
3620 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3621 unsigned Size = 0;
3622 unsigned StartIndex = Categories.size();
3623
3624 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3625
3626 // Allocate space for the size.
3627 Categories.push_back(0);
3628
3629 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003630 for (ObjCInterfaceDecl::known_categories_iterator
3631 Cat = Class->known_categories_begin(),
3632 CatEnd = Class->known_categories_end();
3633 Cat != CatEnd; ++Cat, ++Size) {
3634 assert(getDeclID(*Cat) != 0 && "Bogus category");
3635 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003636 }
3637
3638 // Update the size.
3639 Categories[StartIndex] = Size;
3640
3641 // Record this interface -> category map.
3642 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3643 CategoriesMap.push_back(CatInfo);
3644 }
3645
3646 // Sort the categories map by the definition ID, since the reader will be
3647 // performing binary searches on this information.
3648 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3649
3650 // Emit the categories map.
3651 using namespace llvm;
3652 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3653 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3656 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3657
3658 RecordData Record;
3659 Record.push_back(OBJC_CATEGORIES_MAP);
3660 Record.push_back(CategoriesMap.size());
3661 Stream.EmitRecordWithBlob(AbbrevID, Record,
3662 reinterpret_cast<char*>(CategoriesMap.data()),
3663 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3664
3665 // Emit the category lists.
3666 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3667}
3668
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003669void ASTWriter::WriteMergedDecls() {
3670 if (!Chain || Chain->MergedDecls.empty())
3671 return;
3672
3673 RecordData Record;
3674 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3675 IEnd = Chain->MergedDecls.end();
3676 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003677 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003678 : getDeclID(I->first);
3679 assert(CanonID && "Merged declaration not known?");
3680
3681 Record.push_back(CanonID);
3682 Record.push_back(I->second.size());
3683 Record.append(I->second.begin(), I->second.end());
3684 }
3685 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3686}
3687
Richard Smithe40f2ba2013-08-07 21:41:30 +00003688void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3689 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3690
3691 if (LPTMap.empty())
3692 return;
3693
3694 RecordData Record;
3695 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3696 ItEnd = LPTMap.end();
3697 It != ItEnd; ++It) {
3698 LateParsedTemplate *LPT = It->second;
3699 AddDeclRef(It->first, Record);
3700 AddDeclRef(LPT->D, Record);
3701 Record.push_back(LPT->Toks.size());
3702
3703 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3704 TokEnd = LPT->Toks.end();
3705 TokIt != TokEnd; ++TokIt) {
3706 AddToken(*TokIt, Record);
3707 }
3708 }
3709 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3710}
3711
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003712//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003713// General Serialization Routines
3714//===----------------------------------------------------------------------===//
3715
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003716/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003717void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3718 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003719 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003720 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3721 e = Attrs.end(); i != e; ++i){
3722 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003723 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003724 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003725
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003726#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003727
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003728 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003729}
3730
John McCallf413f5e2013-05-03 00:10:13 +00003731void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3732 AddSourceLocation(Tok.getLocation(), Record);
3733 Record.push_back(Tok.getLength());
3734
3735 // FIXME: When reading literal tokens, reconstruct the literal pointer
3736 // if it is needed.
3737 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3738 // FIXME: Should translate token kind to a stable encoding.
3739 Record.push_back(Tok.getKind());
3740 // FIXME: Should translate token flags to a stable encoding.
3741 Record.push_back(Tok.getFlags());
3742}
3743
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003744void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003745 Record.push_back(Str.size());
3746 Record.insert(Record.end(), Str.begin(), Str.end());
3747}
3748
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003749void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3750 RecordDataImpl &Record) {
3751 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003752 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003753 Record.push_back(*Minor + 1);
3754 else
3755 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003756 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003757 Record.push_back(*Subminor + 1);
3758 else
3759 Record.push_back(0);
3760}
3761
Douglas Gregore84a9da2009-04-20 20:36:09 +00003762/// \brief Note that the identifier II occurs at the given offset
3763/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003764void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003765 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003766 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003767 // up earlier in the chain and thus don't need an offset.
3768 if (ID >= FirstIdentID)
3769 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003770}
3771
Douglas Gregor95c13f52009-04-25 17:48:32 +00003772/// \brief Note that the selector Sel occurs at the given offset
3773/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003774void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003775 unsigned ID = SelectorIDs[Sel];
3776 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003777 // Don't record offsets for selectors that are also available in a different
3778 // file.
3779 if (ID < FirstSelectorID)
3780 return;
3781 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003782}
3783
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003784ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003785 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003786 WritingAST(false), DoneWritingDeclsAndTypes(false),
3787 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003788 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003789 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003790 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3791 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003792 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3793 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003794 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003795 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003796 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003797 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003798 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003799 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003800 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3801 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3802 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003803 DeclTypedefAbbrev(0),
3804 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3805 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003806{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003807}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003808
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003809ASTWriter::~ASTWriter() {
3810 for (FileDeclIDsTy::iterator
3811 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3812 delete I->second;
3813}
3814
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003815void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003816 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003817 Module *WritingModule, StringRef isysroot,
3818 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003819 WritingAST = true;
3820
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003821 ASTHasCompilerErrors = hasErrors;
3822
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003823 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003824 Stream.Emit((unsigned)'C', 8);
3825 Stream.Emit((unsigned)'P', 8);
3826 Stream.Emit((unsigned)'C', 8);
3827 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003828
Chris Lattner28fa4e62009-04-26 22:26:21 +00003829 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003830
Douglas Gregoreda8e122011-08-09 15:13:55 +00003831 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003832 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003833 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003834 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003835 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003836 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003837 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003838
3839 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003840}
3841
Douglas Gregora94a1542011-07-27 21:45:57 +00003842template<typename Vector>
3843static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3844 ASTWriter::RecordData &Record) {
3845 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3846 I != E; ++I) {
3847 Writer.AddDeclRef(*I, Record);
3848 }
3849}
3850
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003851void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003852 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003853 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003854 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003855 using namespace llvm;
3856
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003857 bool isModule = WritingModule != 0;
3858
Douglas Gregorcf68c582011-12-01 22:20:10 +00003859 // Make sure that the AST reader knows to finalize itself.
3860 if (Chain)
3861 Chain->finalizeForWriting();
3862
Sebastian Redl143413f2010-07-12 22:02:52 +00003863 ASTContext &Context = SemaRef.Context;
3864 Preprocessor &PP = SemaRef.PP;
3865
Douglas Gregordab42432011-08-12 00:15:20 +00003866 // Set up predefined declaration IDs.
3867 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003868 if (Context.ObjCIdDecl)
3869 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003870 if (Context.ObjCSelDecl)
3871 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003872 if (Context.ObjCClassDecl)
3873 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003874 if (Context.ObjCProtocolClassDecl)
3875 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003876 if (Context.Int128Decl)
3877 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3878 if (Context.UInt128Decl)
3879 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003880 if (Context.ObjCInstanceTypeDecl)
3881 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003882 if (Context.BuiltinVaListDecl)
3883 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3884
Douglas Gregor851443c2011-08-12 01:39:19 +00003885 if (!Chain) {
3886 // Make sure that we emit IdentifierInfos (and any attached
3887 // declarations) for builtins. We don't need to do this when we're
3888 // emitting chained PCH files, because all of the builtins will be
3889 // in the original PCH file.
3890 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003891 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003892 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003893 if (!Context.getLangOpts().NoBuiltin) {
3894 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3895 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003896 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3897 getIdentifierRef(&Table.get(BuiltinNames[I]));
3898 }
3899
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003900 // If there are any out-of-date identifiers, bring them up to date.
3901 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003902 // Find out-of-date identifiers.
3903 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003904 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3905 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003906 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003907 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003908 OutOfDate.push_back(ID->second);
3909 }
3910
3911 // Update the out-of-date identifiers.
3912 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3913 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3914 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003915 }
3916
Chris Lattner0c797362009-09-08 18:19:27 +00003917 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003918 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003919 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003920 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003921 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003922
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003923 // Build a record containing all of the file scoped decls in this file.
3924 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00003925 if (!isModule)
3926 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3927 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003928
Douglas Gregor851443c2011-08-12 01:39:19 +00003929 // Build a record containing all of the delegating constructors we still need
3930 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003931 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003932 if (!isModule)
3933 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003934
Douglas Gregor851443c2011-08-12 01:39:19 +00003935 // Write the set of weak, undeclared identifiers. We always write the
3936 // entire table, since later PCH files in a PCH chain are only interested in
3937 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003938 RecordData WeakUndeclaredIdentifiers;
3939 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003940 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003941 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3942 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3943 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3944 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3945 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3946 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3947 }
3948 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003949
Richard Smith78165b52013-01-10 23:43:47 +00003950 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003951 // declarations in this header file. Generally, this record will be
3952 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00003953 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003954 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003955 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003956 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00003957 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3958 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003959 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003960 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00003961 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00003962 }
3963
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003964 // Build a record containing all of the ext_vector declarations.
3965 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003966 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003967
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003968 // Build a record containing all of the VTable uses information.
3969 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003970 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003971 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3972 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3973 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3974 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3975 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003976 }
3977
3978 // Build a record containing all of dynamic classes declarations.
3979 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003980 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003981
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003982 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003983 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003984 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003985 I = SemaRef.PendingInstantiations.begin(),
3986 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3987 AddDeclRef(I->first, PendingInstantiations);
3988 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003989 }
3990 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3991 "There are local ones at end of translation unit!");
3992
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003993 // Build a record containing some declaration references.
3994 RecordData SemaDeclRefs;
3995 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3996 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3997 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3998 }
3999
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004000 RecordData CUDASpecialDeclRefs;
4001 if (Context.getcudaConfigureCallDecl()) {
4002 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4003 }
4004
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004005 // Build a record containing all of the known namespaces.
4006 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004007 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004008 I = SemaRef.KnownNamespaces.begin(),
4009 IEnd = SemaRef.KnownNamespaces.end();
4010 I != IEnd; ++I) {
4011 if (!I->second)
4012 AddDeclRef(I->first, KnownNamespaces);
4013 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004014
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004015 // Build a record of all used, undefined objects that require definitions.
4016 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004017
4018 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004019 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004020 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4021 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004022 AddDeclRef(I->first, UndefinedButUsed);
4023 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004024 }
4025
Douglas Gregor112b9072012-10-18 05:31:06 +00004026 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004027 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004028
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004029 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004030 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004031 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004032
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004033 // This is so that older clang versions, before the introduction
4034 // of the control block, can read and reject the newer PCH format.
4035 Record.clear();
4036 Record.push_back(VERSION_MAJOR);
4037 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4038
Douglas Gregor851443c2011-08-12 01:39:19 +00004039 // Create a lexical update block containing all of the declarations in the
4040 // translation unit that do not come from other AST files.
4041 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4042 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
4043 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
4044 E = TU->noload_decls_end();
4045 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004046 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00004047 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004048 }
4049
4050 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4051 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4052 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4053 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4054 Record.clear();
4055 Record.push_back(TU_UPDATE_LEXICAL);
4056 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4057 data(NewGlobalDecls));
4058
4059 // And a visible updates block for the translation unit.
4060 Abv = new llvm::BitCodeAbbrev();
4061 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4062 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4063 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4064 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4065 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4066 WriteDeclContextVisibleUpdate(TU);
4067
4068 // If the translation unit has an anonymous namespace, and we don't already
4069 // have an update block for it, write it as an update block.
4070 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4071 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4072 if (Record.empty()) {
4073 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004074 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004075 }
4076 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004077
4078 // Make sure visible decls, added to DeclContexts previously loaded from
4079 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004080 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004081 I = UpdatingVisibleDecls.begin(),
4082 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4083 GetDeclRef(*I);
4084 }
4085
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004086 // Make sure all decls associated with an identifier are registered for
4087 // serialization.
4088 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4089 IDEnd = PP.getIdentifierTable().end();
4090 ID != IDEnd; ++ID) {
4091 const IdentifierInfo *II = ID->second;
4092 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4093 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4094 DEnd = SemaRef.IdResolver.end();
4095 D != DEnd; ++D) {
4096 GetDeclRef(*D);
4097 }
4098 }
4099 }
4100
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00004101 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004102 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004103
Douglas Gregor5204bde2011-08-02 16:26:37 +00004104 // Form the record of special types.
4105 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004106 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004107 AddTypeRef(Context.getFILEType(), SpecialTypes);
4108 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4109 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4110 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4111 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004112 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004113 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004114
Douglas Gregor1970d882009-04-26 03:49:13 +00004115 // Keep writing types and declarations until all types and
4116 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00004117 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004118 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00004119 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4120 E = DeclsToRewrite.end();
4121 I != E; ++I)
4122 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00004123 while (!DeclTypesToEmit.empty()) {
4124 DeclOrType DOT = DeclTypesToEmit.front();
4125 DeclTypesToEmit.pop();
4126 if (DOT.isType())
4127 WriteType(DOT.getType());
4128 else
4129 WriteDecl(Context, DOT.getDecl());
4130 }
4131 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004132
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004133 DoneWritingDeclsAndTypes = true;
4134
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004135 WriteFileDeclIDsMap();
4136 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00004137 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004138
4139 if (Chain) {
4140 // Write the mapping information describing our module dependencies and how
4141 // each of those modules were mapped into our own offset/ID space, so that
4142 // the reader can build the appropriate mapping to its own offset/ID space.
4143 // The map consists solely of a blob with the following format:
4144 // *(module-name-len:i16 module-name:len*i8
4145 // source-location-offset:i32
4146 // identifier-id:i32
4147 // preprocessed-entity-id:i32
4148 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004149 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004150 // selector-id:i32
4151 // declaration-id:i32
4152 // c++-base-specifiers-id:i32
4153 // type-id:i32)
4154 //
4155 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4156 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4158 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004159 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004160 {
4161 llvm::raw_svector_ostream Out(Buffer);
4162 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004163 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004164 M != MEnd; ++M) {
4165 StringRef FileName = (*M)->FileName;
4166 io::Emit16(Out, FileName.size());
4167 Out.write(FileName.data(), FileName.size());
4168 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4169 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004170 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004171 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00004172 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004173 io::Emit32(Out, (*M)->BaseSelectorID);
4174 io::Emit32(Out, (*M)->BaseDeclID);
4175 io::Emit32(Out, (*M)->BaseTypeIndex);
4176 }
4177 }
4178 Record.clear();
4179 Record.push_back(MODULE_OFFSET_MAP);
4180 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4181 Buffer.data(), Buffer.size());
4182 }
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004183 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004184 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004185 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004186 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004187 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004188 WriteFPPragmaOptions(SemaRef.getFPOptions());
4189 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00004190
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004191 WriteTypeDeclOffsets();
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004192 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004193
Anders Carlsson9bb83e82011-03-06 18:41:18 +00004194 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004195
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004196 // If we're emitting a module, write out the submodule information.
4197 if (WritingModule)
4198 WriteSubmodules(WritingModule);
4199
Douglas Gregor5204bde2011-08-02 16:26:37 +00004200 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4201
Douglas Gregord4df8652009-04-22 22:02:47 +00004202 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004203 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004204 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00004205
4206 // Write the record containing tentative definitions.
4207 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004208 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004209
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004210 // Write the record containing unused file scoped decls.
4211 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004212 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004213
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004214 // Write the record containing weak undeclared identifiers.
4215 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004216 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004217 WeakUndeclaredIdentifiers);
4218
Richard Smith78165b52013-01-10 23:43:47 +00004219 // Write the record containing locally-scoped extern "C" definitions.
4220 if (!LocallyScopedExternCDecls.empty())
4221 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4222 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004223
4224 // Write the record containing ext_vector type names.
4225 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004226 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004227
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004228 // Write the record containing VTable uses information.
4229 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004230 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004231
4232 // Write the record containing dynamic classes declarations.
4233 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004234 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004235
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004236 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004237 if (!PendingInstantiations.empty())
4238 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004239
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004240 // Write the record containing declaration references of Sema.
4241 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004242 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004243
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004244 // Write the record containing CUDA-specific declaration references.
4245 if (!CUDASpecialDeclRefs.empty())
4246 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004247
4248 // Write the delegating constructors.
4249 if (!DelegatingCtorDecls.empty())
4250 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004251
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004252 // Write the known namespaces.
4253 if (!KnownNamespaces.empty())
4254 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004255
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004256 // Write the undefined internal functions and variables, and inline functions.
4257 if (!UndefinedButUsed.empty())
4258 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004259
Douglas Gregor851443c2011-08-12 01:39:19 +00004260 // Write the visible updates to DeclContexts.
4261 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4262 I = UpdatedDeclContexts.begin(),
4263 E = UpdatedDeclContexts.end();
4264 I != E; ++I)
4265 WriteDeclContextVisibleUpdate(*I);
4266
Douglas Gregor959bb062011-12-03 01:15:29 +00004267 if (!WritingModule) {
4268 // Write the submodules that were imported, if any.
4269 RecordData ImportedModules;
4270 for (ASTContext::import_iterator I = Context.local_import_begin(),
4271 IEnd = Context.local_import_end();
4272 I != IEnd; ++I) {
4273 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4274 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4275 }
4276 if (!ImportedModules.empty()) {
4277 // Sort module IDs.
4278 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4279
4280 // Unique module IDs.
4281 ImportedModules.erase(std::unique(ImportedModules.begin(),
4282 ImportedModules.end()),
4283 ImportedModules.end());
4284
4285 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4286 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004287 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004288
Douglas Gregordab42432011-08-12 00:15:20 +00004289 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00004290 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004291 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004292 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004293 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004294 WriteLateParsedTemplates(SemaRef);
4295
Douglas Gregor08f01292009-04-17 22:13:46 +00004296 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004297 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004298 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004299 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004300 Record.push_back(NumLexicalDeclContexts);
4301 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004302 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004303 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004304}
4305
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004306/// \brief Go through the declaration update blocks and resolve declaration
4307/// pointers into declaration IDs.
4308void ASTWriter::ResolveDeclUpdatesBlocks() {
4309 for (DeclUpdateMap::iterator
4310 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4311 const Decl *D = I->first;
4312 UpdateRecord &URec = I->second;
4313
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004314 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004315 continue; // The decl will be written completely
4316
4317 unsigned Idx = 0, N = URec.size();
4318 while (Idx < N) {
4319 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004320 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4321 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4322 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4323 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4324 ++Idx;
4325 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004326
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004327 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Eli Friedman276dd182013-09-05 00:02:25 +00004328 case UPD_DECL_MARKED_USED:
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004329 ++Idx;
4330 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004331
4332 case UPD_CXX_DEDUCED_RETURN_TYPE:
4333 URec[Idx] = GetOrCreateTypeID(
4334 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4335 ++Idx;
4336 break;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004337 }
4338 }
4339 }
4340}
4341
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004342void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004343 if (DeclUpdates.empty())
4344 return;
4345
4346 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00004347 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004348 for (DeclUpdateMap::iterator
4349 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4350 const Decl *D = I->first;
4351 UpdateRecord &URec = I->second;
4352
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004353 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004354 continue; // The decl will be written completely,no need to store updates.
4355
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004356 uint64_t Offset = Stream.GetCurrentBitNo();
4357 Stream.EmitRecord(DECL_UPDATES, URec);
4358
4359 OffsetsRecord.push_back(GetDeclRef(D));
4360 OffsetsRecord.push_back(Offset);
4361 }
4362 Stream.ExitBlock();
4363 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4364}
4365
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004366void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004367 if (ReplacedDecls.empty())
4368 return;
4369
4370 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004371 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4372 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004373 Record.push_back(I->ID);
4374 Record.push_back(I->Offset);
4375 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004376 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004377 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004378}
4379
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004380void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004381 Record.push_back(Loc.getRawEncoding());
4382}
4383
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004384void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004385 AddSourceLocation(Range.getBegin(), Record);
4386 AddSourceLocation(Range.getEnd(), Record);
4387}
4388
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004389void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004390 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004391 const uint64_t *Words = Value.getRawData();
4392 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004393}
4394
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004395void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004396 Record.push_back(Value.isUnsigned());
4397 AddAPInt(Value, Record);
4398}
4399
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004400void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004401 AddAPInt(Value.bitcastToAPInt(), Record);
4402}
4403
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004404void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004405 Record.push_back(getIdentifierRef(II));
4406}
4407
Sebastian Redl539c5062010-08-18 23:57:32 +00004408IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004409 if (II == 0)
4410 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004411
Sebastian Redl539c5062010-08-18 23:57:32 +00004412 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004413 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004414 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004415 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004416}
4417
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004418MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004419 // Don't emit builtin macros like __LINE__ to the AST file unless they
4420 // have been redefined by the header (in which case they are not
4421 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004422 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004423 return 0;
4424
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004425 MacroID &ID = MacroIDs[MI];
4426 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004427 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004428 MacroInfoToEmitData Info = { Name, MI, ID };
4429 MacroInfosToEmit.push_back(Info);
4430 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004431 return ID;
4432}
4433
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004434MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4435 if (MI == 0 || MI->isBuiltinMacro())
4436 return 0;
4437
4438 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4439 return MacroIDs[MI];
4440}
4441
4442uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4443 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4444 return IdentMacroDirectivesOffsetMap[Name];
4445}
4446
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004447void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004448 Record.push_back(getSelectorRef(SelRef));
4449}
4450
Sebastian Redl539c5062010-08-18 23:57:32 +00004451SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004452 if (Sel.getAsOpaquePtr() == 0) {
4453 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004454 }
4455
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004456 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004457 if (SID == 0 && Chain) {
4458 // This might trigger a ReadSelector callback, which will set the ID for
4459 // this selector.
4460 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004461 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004462 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004463 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004464 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004465 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004466 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004467 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004468}
4469
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004470void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004471 AddDeclRef(Temp->getDestructor(), Record);
4472}
4473
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004474void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4475 CXXBaseSpecifier const *BasesEnd,
4476 RecordDataImpl &Record) {
4477 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4478 CXXBaseSpecifiersToWrite.push_back(
4479 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4480 Bases, BasesEnd));
4481 Record.push_back(NextCXXBaseSpecifiersID++);
4482}
4483
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004484void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004485 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004486 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004487 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004488 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004489 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004490 break;
4491 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004492 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004493 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004494 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004495 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004496 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004497 break;
4498 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004499 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004500 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004501 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004502 break;
John McCall0ad16662009-10-29 08:12:44 +00004503 case TemplateArgument::Null:
4504 case TemplateArgument::Integral:
4505 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004506 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004507 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004508 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004509 break;
4510 }
4511}
4512
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004513void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004514 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004515 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004516
4517 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4518 bool InfoHasSameExpr
4519 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4520 Record.push_back(InfoHasSameExpr);
4521 if (InfoHasSameExpr)
4522 return; // Avoid storing the same expr twice.
4523 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004524 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4525 Record);
4526}
4527
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004528void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4529 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004530 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004531 AddTypeRef(QualType(), Record);
4532 return;
4533 }
4534
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004535 AddTypeLoc(TInfo->getTypeLoc(), Record);
4536}
4537
4538void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4539 AddTypeRef(TL.getType(), Record);
4540
John McCall8f115c62009-10-16 21:56:05 +00004541 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004542 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004543 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004544}
4545
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004546void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004547 Record.push_back(GetOrCreateTypeID(T));
4548}
4549
Douglas Gregoreda8e122011-08-09 15:13:55 +00004550TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004551 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004552 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004553 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4554}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004555
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004556TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004557 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004558 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004559 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004560}
4561
4562TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4563 if (T.isNull())
4564 return TypeIdx();
4565 assert(!T.getLocalFastQualifiers());
4566
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004567 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004568 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004569 if (DoneWritingDeclsAndTypes) {
4570 assert(0 && "New type seen after serializing all the types to emit!");
4571 return TypeIdx();
4572 }
4573
Douglas Gregor1970d882009-04-26 03:49:13 +00004574 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004575 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004576 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004577 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004578 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004579 return Idx;
4580}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004581
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004582TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004583 if (T.isNull())
4584 return TypeIdx();
4585 assert(!T.getLocalFastQualifiers());
4586
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004587 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4588 assert(I != TypeIdxs.end() && "Type not emitted!");
4589 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004590}
4591
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004592void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004593 Record.push_back(GetDeclRef(D));
4594}
4595
Sebastian Redl539c5062010-08-18 23:57:32 +00004596DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004597 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4598
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004599 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004600 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004601 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004602
4603 // If D comes from an AST file, its declaration ID is already known and
4604 // fixed.
4605 if (D->isFromASTFile())
4606 return D->getGlobalID();
4607
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004608 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004609 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004610 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004611 if (DoneWritingDeclsAndTypes) {
4612 assert(0 && "New decl seen after serializing all the decls to emit!");
4613 return 0;
4614 }
4615
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004616 // We haven't seen this declaration before. Give it a new ID and
4617 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004618 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004619 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004620 }
4621
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004622 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004623}
4624
Sebastian Redl539c5062010-08-18 23:57:32 +00004625DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004626 if (D == 0)
4627 return 0;
4628
Douglas Gregorb3163e52012-01-05 22:33:30 +00004629 // If D comes from an AST file, its declaration ID is already known and
4630 // fixed.
4631 if (D->isFromASTFile())
4632 return D->getGlobalID();
4633
Douglas Gregore84a9da2009-04-20 20:36:09 +00004634 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4635 return DeclIDs[D];
4636}
4637
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004638void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004639 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004640 assert(D);
4641
4642 SourceLocation Loc = D->getLocation();
4643 if (Loc.isInvalid())
4644 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004645
4646 // We only keep track of the file-level declarations of each file.
4647 if (!D->getLexicalDeclContext()->isFileContext())
4648 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004649 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4650 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004651 if (isa<ParmVarDecl>(D))
4652 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004653
4654 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004655 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004656 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004657 FileID FID;
4658 unsigned Offset;
4659 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004660 if (FID.isInvalid())
4661 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004662 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004663
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004664 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004665 if (!Info)
4666 Info = new DeclIDInFileInfo();
4667
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004668 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004669 LocDeclIDsTy &Decls = Info->DeclIDs;
4670
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004671 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004672 Decls.push_back(LocDecl);
4673 return;
4674 }
4675
Benjamin Kramer45025c02013-08-24 13:22:59 +00004676 LocDeclIDsTy::iterator I =
4677 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004678
4679 Decls.insert(I, LocDecl);
4680}
4681
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004682void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004683 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004684 Record.push_back(Name.getNameKind());
4685 switch (Name.getNameKind()) {
4686 case DeclarationName::Identifier:
4687 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4688 break;
4689
4690 case DeclarationName::ObjCZeroArgSelector:
4691 case DeclarationName::ObjCOneArgSelector:
4692 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004693 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004694 break;
4695
4696 case DeclarationName::CXXConstructorName:
4697 case DeclarationName::CXXDestructorName:
4698 case DeclarationName::CXXConversionFunctionName:
4699 AddTypeRef(Name.getCXXNameType(), Record);
4700 break;
4701
4702 case DeclarationName::CXXOperatorName:
4703 Record.push_back(Name.getCXXOverloadedOperator());
4704 break;
4705
Alexis Hunt3d221f22009-11-29 07:34:05 +00004706 case DeclarationName::CXXLiteralOperatorName:
4707 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4708 break;
4709
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004710 case DeclarationName::CXXUsingDirective:
4711 // No extra data to emit
4712 break;
4713 }
4714}
Chris Lattnerca025db2010-05-07 21:43:38 +00004715
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004716void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004717 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004718 switch (Name.getNameKind()) {
4719 case DeclarationName::CXXConstructorName:
4720 case DeclarationName::CXXDestructorName:
4721 case DeclarationName::CXXConversionFunctionName:
4722 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4723 break;
4724
4725 case DeclarationName::CXXOperatorName:
4726 AddSourceLocation(
4727 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4728 Record);
4729 AddSourceLocation(
4730 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4731 Record);
4732 break;
4733
4734 case DeclarationName::CXXLiteralOperatorName:
4735 AddSourceLocation(
4736 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4737 Record);
4738 break;
4739
4740 case DeclarationName::Identifier:
4741 case DeclarationName::ObjCZeroArgSelector:
4742 case DeclarationName::ObjCOneArgSelector:
4743 case DeclarationName::ObjCMultiArgSelector:
4744 case DeclarationName::CXXUsingDirective:
4745 break;
4746 }
4747}
4748
4749void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004750 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004751 AddDeclarationName(NameInfo.getName(), Record);
4752 AddSourceLocation(NameInfo.getLoc(), Record);
4753 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4754}
4755
4756void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004757 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004758 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004759 Record.push_back(Info.NumTemplParamLists);
4760 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4761 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4762}
4763
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004764void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004765 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004766 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004767 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004768 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004769
4770 // Push each of the NNS's onto a stack for serialization in reverse order.
4771 while (NNS) {
4772 NestedNames.push_back(NNS);
4773 NNS = NNS->getPrefix();
4774 }
4775
4776 Record.push_back(NestedNames.size());
4777 while(!NestedNames.empty()) {
4778 NNS = NestedNames.pop_back_val();
4779 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4780 Record.push_back(Kind);
4781 switch (Kind) {
4782 case NestedNameSpecifier::Identifier:
4783 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4784 break;
4785
4786 case NestedNameSpecifier::Namespace:
4787 AddDeclRef(NNS->getAsNamespace(), Record);
4788 break;
4789
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004790 case NestedNameSpecifier::NamespaceAlias:
4791 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4792 break;
4793
Chris Lattnerca025db2010-05-07 21:43:38 +00004794 case NestedNameSpecifier::TypeSpec:
4795 case NestedNameSpecifier::TypeSpecWithTemplate:
4796 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4797 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4798 break;
4799
4800 case NestedNameSpecifier::Global:
4801 // Don't need to write an associated value.
4802 break;
4803 }
4804 }
4805}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004806
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004807void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4808 RecordDataImpl &Record) {
4809 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004810 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004811 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004812
4813 // Push each of the nested-name-specifiers's onto a stack for
4814 // serialization in reverse order.
4815 while (NNS) {
4816 NestedNames.push_back(NNS);
4817 NNS = NNS.getPrefix();
4818 }
4819
4820 Record.push_back(NestedNames.size());
4821 while(!NestedNames.empty()) {
4822 NNS = NestedNames.pop_back_val();
4823 NestedNameSpecifier::SpecifierKind Kind
4824 = NNS.getNestedNameSpecifier()->getKind();
4825 Record.push_back(Kind);
4826 switch (Kind) {
4827 case NestedNameSpecifier::Identifier:
4828 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4829 AddSourceRange(NNS.getLocalSourceRange(), Record);
4830 break;
4831
4832 case NestedNameSpecifier::Namespace:
4833 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4834 AddSourceRange(NNS.getLocalSourceRange(), Record);
4835 break;
4836
4837 case NestedNameSpecifier::NamespaceAlias:
4838 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4839 AddSourceRange(NNS.getLocalSourceRange(), Record);
4840 break;
4841
4842 case NestedNameSpecifier::TypeSpec:
4843 case NestedNameSpecifier::TypeSpecWithTemplate:
4844 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4845 AddTypeLoc(NNS.getTypeLoc(), Record);
4846 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4847 break;
4848
4849 case NestedNameSpecifier::Global:
4850 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4851 break;
4852 }
4853 }
4854}
4855
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004856void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004857 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004858 Record.push_back(Kind);
4859 switch (Kind) {
4860 case TemplateName::Template:
4861 AddDeclRef(Name.getAsTemplateDecl(), Record);
4862 break;
4863
4864 case TemplateName::OverloadedTemplate: {
4865 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4866 Record.push_back(OvT->size());
4867 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4868 I != E; ++I)
4869 AddDeclRef(*I, Record);
4870 break;
4871 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004872
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004873 case TemplateName::QualifiedTemplate: {
4874 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4875 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4876 Record.push_back(QualT->hasTemplateKeyword());
4877 AddDeclRef(QualT->getTemplateDecl(), Record);
4878 break;
4879 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004880
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004881 case TemplateName::DependentTemplate: {
4882 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4883 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4884 Record.push_back(DepT->isIdentifier());
4885 if (DepT->isIdentifier())
4886 AddIdentifierRef(DepT->getIdentifier(), Record);
4887 else
4888 Record.push_back(DepT->getOperator());
4889 break;
4890 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004891
4892 case TemplateName::SubstTemplateTemplateParm: {
4893 SubstTemplateTemplateParmStorage *subst
4894 = Name.getAsSubstTemplateTemplateParm();
4895 AddDeclRef(subst->getParameter(), Record);
4896 AddTemplateName(subst->getReplacement(), Record);
4897 break;
4898 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004899
4900 case TemplateName::SubstTemplateTemplateParmPack: {
4901 SubstTemplateTemplateParmPackStorage *SubstPack
4902 = Name.getAsSubstTemplateTemplateParmPack();
4903 AddDeclRef(SubstPack->getParameterPack(), Record);
4904 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4905 break;
4906 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004907 }
4908}
4909
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004910void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004911 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004912 Record.push_back(Arg.getKind());
4913 switch (Arg.getKind()) {
4914 case TemplateArgument::Null:
4915 break;
4916 case TemplateArgument::Type:
4917 AddTypeRef(Arg.getAsType(), Record);
4918 break;
4919 case TemplateArgument::Declaration:
4920 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004921 Record.push_back(Arg.isDeclForReferenceParam());
4922 break;
4923 case TemplateArgument::NullPtr:
4924 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004925 break;
4926 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004927 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004928 AddTypeRef(Arg.getIntegralType(), Record);
4929 break;
4930 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004931 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4932 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004933 case TemplateArgument::TemplateExpansion:
4934 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00004935 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00004936 Record.push_back(*NumExpansions + 1);
4937 else
4938 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004939 break;
4940 case TemplateArgument::Expression:
4941 AddStmt(Arg.getAsExpr());
4942 break;
4943 case TemplateArgument::Pack:
4944 Record.push_back(Arg.pack_size());
4945 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4946 I != E; ++I)
4947 AddTemplateArgument(*I, Record);
4948 break;
4949 }
4950}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004951
4952void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004953ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004954 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004955 assert(TemplateParams && "No TemplateParams!");
4956 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4957 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4958 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4959 Record.push_back(TemplateParams->size());
4960 for (TemplateParameterList::const_iterator
4961 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4962 P != PEnd; ++P)
4963 AddDeclRef(*P, Record);
4964}
4965
4966/// \brief Emit a template argument list.
4967void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004968ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004969 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004970 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004971 Record.push_back(TemplateArgs->size());
4972 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004973 AddTemplateArgument(TemplateArgs->get(i), Record);
4974}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004975
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00004976void
4977ASTWriter::AddASTTemplateArgumentListInfo
4978(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
4979 assert(ASTTemplArgList && "No ASTTemplArgList!");
4980 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
4981 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
4982 Record.push_back(ASTTemplArgList->NumTemplateArgs);
4983 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
4984 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
4985 AddTemplateArgumentLoc(TemplArgs[i], Record);
4986}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004987
4988void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004989ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004990 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004991 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004992 I = Set.begin(), E = Set.end(); I != E; ++I) {
4993 AddDeclRef(I.getDecl(), Record);
4994 Record.push_back(I.getAccess());
4995 }
4996}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004997
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004998void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004999 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005000 Record.push_back(Base.isVirtual());
5001 Record.push_back(Base.isBaseOfClass());
5002 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005003 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005004 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005005 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005006 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5007 : SourceLocation(),
5008 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005009}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005010
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005011void ASTWriter::FlushCXXBaseSpecifiers() {
5012 RecordData Record;
5013 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5014 Record.clear();
5015
5016 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005017 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005018 if (Index == CXXBaseSpecifiersOffsets.size())
5019 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5020 else {
5021 if (Index > CXXBaseSpecifiersOffsets.size())
5022 CXXBaseSpecifiersOffsets.resize(Index + 1);
5023 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5024 }
5025
5026 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5027 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5028 Record.push_back(BEnd - B);
5029 for (; B != BEnd; ++B)
5030 AddCXXBaseSpecifier(*B, Record);
5031 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005032
5033 // Flush any expressions that were written as part of the base specifiers.
5034 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005035 }
5036
5037 CXXBaseSpecifiersToWrite.clear();
5038}
5039
Alexis Hunt1d792652011-01-08 20:30:50 +00005040void ASTWriter::AddCXXCtorInitializers(
5041 const CXXCtorInitializer * const *CtorInitializers,
5042 unsigned NumCtorInitializers,
5043 RecordDataImpl &Record) {
5044 Record.push_back(NumCtorInitializers);
5045 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5046 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005047
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005048 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005049 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005050 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005051 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005052 } else if (Init->isDelegatingInitializer()) {
5053 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005054 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005055 } else if (Init->isMemberInitializer()){
5056 Record.push_back(CTOR_INITIALIZER_MEMBER);
5057 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005058 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005059 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5060 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005061 }
Francois Pichetd583da02010-12-04 09:14:42 +00005062
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005063 AddSourceLocation(Init->getMemberLocation(), Record);
5064 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005065 AddSourceLocation(Init->getLParenLoc(), Record);
5066 AddSourceLocation(Init->getRParenLoc(), Record);
5067 Record.push_back(Init->isWritten());
5068 if (Init->isWritten()) {
5069 Record.push_back(Init->getSourceOrder());
5070 } else {
5071 Record.push_back(Init->getNumArrayIndices());
5072 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5073 AddDeclRef(Init->getArrayIndex(i), Record);
5074 }
5075 }
5076}
5077
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005078void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5079 assert(D->DefinitionData);
5080 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005081 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005082 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005083 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005084 Record.push_back(Data.Aggregate);
5085 Record.push_back(Data.PlainOldData);
5086 Record.push_back(Data.Empty);
5087 Record.push_back(Data.Polymorphic);
5088 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005089 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005090 Record.push_back(Data.HasNoNonEmptyBases);
5091 Record.push_back(Data.HasPrivateFields);
5092 Record.push_back(Data.HasProtectedFields);
5093 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005094 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00005095 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005096 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005097 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005098 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5099 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5100 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5101 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5102 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5103 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005104 Record.push_back(Data.HasTrivialSpecialMembers);
5105 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005106 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005107 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005108 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005109 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005110 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005111 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005112 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005113 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5114 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5115 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5116 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005117 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005118
5119 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005120 if (Data.NumBases > 0)
5121 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5122 Record);
5123
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005124 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5125 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005126 if (Data.NumVBases > 0)
5127 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5128 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005129
Richard Smitha4ba74c2013-08-30 04:46:40 +00005130 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5131 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005132 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005133 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005134
5135 // Add lambda-specific data.
5136 if (Data.IsLambda) {
5137 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005138 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005139 Record.push_back(Lambda.IsGenericLambda);
5140 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005141 Record.push_back(Lambda.NumCaptures);
5142 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005143 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005144 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005145 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005146 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5147 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5148 AddSourceLocation(Capture.getLocation(), Record);
5149 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005150 Record.push_back(Capture.getCaptureKind());
5151 switch (Capture.getCaptureKind()) {
5152 case LCK_This:
5153 break;
5154 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005155 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005156 VarDecl *Var =
5157 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5158 AddDeclRef(Var, Record);
5159 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5160 : SourceLocation(),
5161 Record);
5162 break;
5163 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005164 }
5165 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005166}
5167
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005168void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005169 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005170 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005171 assert(FirstDeclID == NextDeclID &&
5172 FirstTypeID == NextTypeID &&
5173 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005174 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005175 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005176 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005177 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005178
Sebastian Redl07a89a82010-07-30 00:29:29 +00005179 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005180
Douglas Gregordf0c1512011-08-18 04:12:04 +00005181 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5182 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5183 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005184 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005185 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005186 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005187 NextDeclID = FirstDeclID;
5188 NextTypeID = FirstTypeID;
5189 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005190 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005191 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005192 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005193}
5194
Sebastian Redl539c5062010-08-18 23:57:32 +00005195void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005196 // Always keep the highest ID. See \p TypeRead() for more information.
5197 IdentID &StoredID = IdentifierIDs[II];
5198 if (ID > StoredID)
5199 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005200}
5201
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005202void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005203 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005204 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005205 if (ID > StoredID)
5206 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005207}
5208
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005209void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005210 // Always take the highest-numbered type index. This copes with an interesting
5211 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005212 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005213 // keep the higher-numbered entry so that we can properly write it out to
5214 // the AST file.
5215 TypeIdx &StoredIdx = TypeIdxs[T];
5216 if (Idx.getIndex() >= StoredIdx.getIndex())
5217 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005218}
5219
Sebastian Redl539c5062010-08-18 23:57:32 +00005220void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005221 // Always keep the highest ID. See \p TypeRead() for more information.
5222 SelectorID &StoredID = SelectorIDs[S];
5223 if (ID > StoredID)
5224 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005225}
Douglas Gregor91096292010-10-02 19:29:26 +00005226
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005227void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005228 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005229 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005230 MacroDefinitions[MD] = ID;
5231}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005232
Douglas Gregore37a85a2011-12-02 17:30:13 +00005233void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5234 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5235 SubmoduleIDs[Mod] = ID;
5236}
5237
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005238void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005239 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005240 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005241 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5242 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005243 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005244 // A forward reference was mutated into a definition. Rewrite it.
5245 // FIXME: This happens during template instantiation, should we
5246 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005247 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005248 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005249 }
5250}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005251
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005252void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005253 assert(!WritingAST && "Already writing the AST!");
5254
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005255 // TU and namespaces are handled elsewhere.
5256 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5257 return;
5258
Douglas Gregorb3722e22011-09-09 23:01:35 +00005259 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005260 return; // Not a source decl added to a DeclContext from PCH.
5261
Douglas Gregor9f782892013-01-21 15:25:38 +00005262 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005263 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005264 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005265}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005266
5267void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005268 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005269 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005270 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005271 return; // Not a source member added to a class from PCH.
5272 if (!isa<CXXMethodDecl>(D))
5273 return; // We are interested in lazily declared implicit methods.
5274
5275 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005276 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005277 UpdateRecord &Record = DeclUpdates[RD];
5278 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005279 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005280}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005281
5282void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5283 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005284 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005285 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005286 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005287 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005288 return; // Not a source specialization added to a template from PCH.
5289
5290 UpdateRecord &Record = DeclUpdates[TD];
5291 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005292 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005293}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005294
Larisse Voufo39a1e502013-08-06 01:03:05 +00005295void ASTWriter::AddedCXXTemplateSpecialization(
5296 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5297 // The specializations set is kept in the canonical template.
5298 assert(!WritingAST && "Already writing the AST!");
5299 TD = TD->getCanonicalDecl();
5300 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5301 return; // Not a source specialization added to a template from PCH.
5302
5303 UpdateRecord &Record = DeclUpdates[TD];
5304 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
5305 Record.push_back(reinterpret_cast<uint64_t>(D));
5306}
5307
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005308void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5309 const FunctionDecl *D) {
5310 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005311 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005312 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005313 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005314 return; // Not a source specialization added to a template from PCH.
5315
5316 UpdateRecord &Record = DeclUpdates[TD];
5317 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005318 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005319}
5320
Richard Smith1fa5d642013-05-11 05:45:24 +00005321void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5322 assert(!WritingAST && "Already writing the AST!");
5323 FD = FD->getCanonicalDecl();
5324 if (!FD->isFromASTFile())
5325 return; // Not a function declared in PCH and defined outside.
5326
5327 UpdateRecord &Record = DeclUpdates[FD];
5328 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5329 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5330}
5331
Sebastian Redlab238a72011-04-24 16:28:06 +00005332void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005333 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005334 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005335 return; // Declaration not imported from PCH.
5336
5337 // Implicit decl from a PCH was defined.
5338 // FIXME: Should implicit definition be a separate FunctionDecl?
5339 RewriteDecl(D);
5340}
5341
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005342void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005343 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005344 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005345 return;
5346
5347 // Since the actual instantiation is delayed, this really means that we need
5348 // to update the instantiation location.
5349 UpdateRecord &Record = DeclUpdates[D];
5350 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5351 AddSourceLocation(
5352 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5353}
5354
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005355void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5356 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005357 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005358 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005359 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005360
5361 assert(IFD->getDefinition() && "Category on a class without a definition?");
5362 ObjCClassesWithCategories.insert(
5363 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005364}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005365
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005366
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005367void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5368 const ObjCPropertyDecl *OrigProp,
5369 const ObjCCategoryDecl *ClassExt) {
5370 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5371 if (!D)
5372 return;
5373
5374 assert(!WritingAST && "Already writing the AST!");
5375 if (!D->isFromASTFile())
5376 return; // Declaration not imported from PCH.
5377
5378 RewriteDecl(D);
5379}
Eli Friedman276dd182013-09-05 00:02:25 +00005380
5381void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5382 assert(!WritingAST && "Already writing the AST!");
5383 if (!D->isFromASTFile())
5384 return;
5385
5386 UpdateRecord &Record = DeclUpdates[D];
5387 Record.push_back(UPD_DECL_MARKED_USED);
5388}