blob: 59061792af1ac5fe1b8cfa0029046a43cb06785b [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 Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053
Sebastian Redl3df5a082010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000059}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000065}
66
Douglas Gregoref84c4b2009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000070
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100}
101
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116}
117
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121}
122
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000127}
128
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133}
134
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139}
140
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175}
176
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180}
181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000188 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000189 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000198 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
199 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
200 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000201 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000203 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000204 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205}
206
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000209 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000210}
John McCallb96ec562009-12-04 22:46:56 +0000211
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000214 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
215 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000216 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217}
218
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000220 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222}
223
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000226 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227}
228
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000231 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000232 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000233}
234
Alexis Hunte852b102011-05-24 22:41:36 +0000235void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
236 Writer.AddTypeRef(T->getBaseType(), Record);
237 Writer.AddTypeRef(T->getUnderlyingType(), Record);
238 Record.push_back(T->getUTTKind());
239 Code = TYPE_UNARY_TRANSFORM;
240}
241
Richard Smith30482bc2011-02-20 03:19:35 +0000242void ASTTypeWriter::VisitAutoType(const AutoType *T) {
243 Writer.AddTypeRef(T->getDeducedType(), Record);
244 Code = TYPE_AUTO;
245}
246
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000248 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000249 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000250 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251 "Cannot serialize in the middle of a type definition");
252}
253
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000256 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000257}
258
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000259void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000261 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262}
263
John McCall81904512011-01-06 01:58:22 +0000264void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
265 Writer.AddTypeRef(T->getModifiedType(), Record);
266 Writer.AddTypeRef(T->getEquivalentType(), Record);
267 Record.push_back(T->getAttrKind());
268 Code = TYPE_ATTRIBUTED;
269}
270
Mike Stump11289f42009-09-09 15:08:12 +0000271void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000273 const SubstTemplateTypeParmType *T) {
274 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
275 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000276 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000277}
278
279void
Douglas Gregorada4b792011-01-14 02:55:32 +0000280ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
281 const SubstTemplateTypeParmPackType *T) {
282 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
283 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
284 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
285}
286
287void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000288ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000289 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000290 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000291 Writer.AddTemplateName(T->getTemplateName(), Record);
292 Record.push_back(T->getNumArgs());
293 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
294 ArgI != ArgE; ++ArgI)
295 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000296 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
297 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000298 : T->getCanonicalTypeInternal(),
299 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000300 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000301}
302
303void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000304ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000305 VisitArrayType(T);
306 Writer.AddStmt(T->getSizeExpr());
307 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000308 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000313 const DependentSizedExtVectorType *T) {
314 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000315 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000316}
317
318void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000319ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000320 Record.push_back(T->getDepth());
321 Record.push_back(T->getIndex());
322 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000323 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000324 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000325}
326
327void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000328ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000329 Record.push_back(T->getKeyword());
330 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
331 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000332 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
333 : T->getCanonicalTypeInternal(),
334 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000335 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000336}
337
338void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000339ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000340 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000341 Record.push_back(T->getKeyword());
342 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
343 Writer.AddIdentifierRef(T->getIdentifier(), Record);
344 Record.push_back(T->getNumArgs());
345 for (DependentTemplateSpecializationType::iterator
346 I = T->begin(), E = T->end(); I != E; ++I)
347 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000349}
350
Douglas Gregord2fa7662010-12-20 02:24:11 +0000351void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
352 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000353 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
354 Record.push_back(*NumExpansions + 1);
355 else
356 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000357 Code = TYPE_PACK_EXPANSION;
358}
359
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000360void ASTTypeWriter::VisitParenType(const ParenType *T) {
361 Writer.AddTypeRef(T->getInnerType(), Record);
362 Code = TYPE_PAREN;
363}
364
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000366 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000367 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
368 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000369 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000370}
371
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000373 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000374 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000375 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000376}
377
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000378void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000379 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000380 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000381}
382
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000384 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000385 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000386 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000387 E = T->qual_end(); I != E; ++I)
388 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000390}
391
Steve Narofffb4330f2009-06-17 22:40:22 +0000392void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000393ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000394 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000395 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000396}
397
Eli Friedman0dfb8892011-10-06 23:00:33 +0000398void
399ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
400 Writer.AddTypeRef(T->getValueType(), Record);
401 Code = TYPE_ATOMIC;
402}
403
John McCall8f115c62009-10-16 21:56:05 +0000404namespace {
405
406class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000407 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000408 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000409
410public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000411 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000412 : Writer(Writer), Record(Record) { }
413
John McCall17001972009-10-18 01:05:36 +0000414#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000415#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000416 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000417#include "clang/AST/TypeLocNodes.def"
418
John McCall17001972009-10-18 01:05:36 +0000419 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
420 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000421};
422
423}
424
John McCall17001972009-10-18 01:05:36 +0000425void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
426 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000427}
John McCall17001972009-10-18 01:05:36 +0000428void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000429 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
430 if (TL.needsExtraLocalData()) {
431 Record.push_back(TL.getWrittenTypeSpec());
432 Record.push_back(TL.getWrittenSignSpec());
433 Record.push_back(TL.getWrittenWidthSpec());
434 Record.push_back(TL.hasModeAttr());
435 }
John McCall8f115c62009-10-16 21:56:05 +0000436}
John McCall17001972009-10-18 01:05:36 +0000437void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000439}
John McCall17001972009-10-18 01:05:36 +0000440void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000442}
John McCall17001972009-10-18 01:05:36 +0000443void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000445}
John McCall17001972009-10-18 01:05:36 +0000446void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000448}
John McCall17001972009-10-18 01:05:36 +0000449void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000451}
John McCall17001972009-10-18 01:05:36 +0000452void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000454 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000455}
John McCall17001972009-10-18 01:05:36 +0000456void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
458 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
459 Record.push_back(TL.getSizeExpr() ? 1 : 0);
460 if (TL.getSizeExpr())
461 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000462}
John McCall17001972009-10-18 01:05:36 +0000463void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
464 VisitArrayTypeLoc(TL);
465}
466void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
473 DependentSizedArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
477 DependentSizedExtVectorTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
480void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000487 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000488 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
489 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000490 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall17001972009-10-18 01:05:36 +0000491 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
492 Writer.AddDeclRef(TL.getArg(i), Record);
493}
494void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
495 VisitFunctionTypeLoc(TL);
496}
497void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
498 VisitFunctionTypeLoc(TL);
499}
John McCallb96ec562009-12-04 22:46:56 +0000500void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502}
John McCall17001972009-10-18 01:05:36 +0000503void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
506void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000507 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
508 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
509 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000510}
511void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000512 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
513 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
514 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
515 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000516}
517void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
518 Writer.AddSourceLocation(TL.getNameLoc(), Record);
519}
Alexis Hunte852b102011-05-24 22:41:36 +0000520void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getKWLoc(), Record);
522 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
523 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
524 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
525}
Richard Smith30482bc2011-02-20 03:19:35 +0000526void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
John McCall17001972009-10-18 01:05:36 +0000529void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
532void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
John McCall81904512011-01-06 01:58:22 +0000535void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
537 if (TL.hasAttrOperand()) {
538 SourceRange range = TL.getAttrOperandParensRange();
539 Writer.AddSourceLocation(range.getBegin(), Record);
540 Writer.AddSourceLocation(range.getEnd(), Record);
541 }
542 if (TL.hasAttrExprOperand()) {
543 Expr *operand = TL.getAttrExprOperand();
544 Record.push_back(operand ? 1 : 0);
545 if (operand) Writer.AddStmt(operand);
546 } else if (TL.hasAttrEnumOperand()) {
547 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
548 }
549}
John McCall17001972009-10-18 01:05:36 +0000550void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCallcebee162009-10-18 09:09:24 +0000553void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
554 SubstTemplateTypeParmTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
Douglas Gregorada4b792011-01-14 02:55:32 +0000557void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
558 SubstTemplateTypeParmPackTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getNameLoc(), Record);
560}
John McCall17001972009-10-18 01:05:36 +0000561void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
562 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000563 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000564 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
565 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
566 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
567 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000568 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
569 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000570}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000571void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
572 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
573 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
574}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000575void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000576 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000577 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000578}
John McCalle78aac42010-03-10 03:28:59 +0000579void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getNameLoc(), Record);
581}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000582void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000583 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000584 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000585 Writer.AddSourceLocation(TL.getNameLoc(), Record);
586}
John McCallc392f372010-06-11 00:33:02 +0000587void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
588 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000589 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000590 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000591 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000592 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000593 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
594 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
595 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000596 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
597 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000598}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000599void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
600 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
601}
John McCall17001972009-10-18 01:05:36 +0000602void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000604}
605void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
606 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000607 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
608 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
609 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
610 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000611}
John McCallfc93cf92009-10-22 22:37:11 +0000612void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
613 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000614}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000615void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getKWLoc(), Record);
617 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
618 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
619}
John McCall8f115c62009-10-16 21:56:05 +0000620
Chris Lattner19cea4e2009-04-22 05:57:30 +0000621//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000622// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000623//===----------------------------------------------------------------------===//
624
Chris Lattner28fa4e62009-04-26 22:26:21 +0000625static void EmitBlockID(unsigned ID, const char *Name,
626 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000627 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000628 Record.clear();
629 Record.push_back(ID);
630 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
631
632 // Emit the block name if present.
633 if (Name == 0 || Name[0] == 0) return;
634 Record.clear();
635 while (*Name)
636 Record.push_back(*Name++);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
638}
639
640static void EmitRecordID(unsigned ID, const char *Name,
641 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000642 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000643 Record.clear();
644 Record.push_back(ID);
645 while (*Name)
646 Record.push_back(*Name++);
647 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000648}
649
650static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000651 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000652#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000653 RECORD(STMT_STOP);
654 RECORD(STMT_NULL_PTR);
655 RECORD(STMT_NULL);
656 RECORD(STMT_COMPOUND);
657 RECORD(STMT_CASE);
658 RECORD(STMT_DEFAULT);
659 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000660 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000661 RECORD(STMT_IF);
662 RECORD(STMT_SWITCH);
663 RECORD(STMT_WHILE);
664 RECORD(STMT_DO);
665 RECORD(STMT_FOR);
666 RECORD(STMT_GOTO);
667 RECORD(STMT_INDIRECT_GOTO);
668 RECORD(STMT_CONTINUE);
669 RECORD(STMT_BREAK);
670 RECORD(STMT_RETURN);
671 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000672 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000673 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000674 RECORD(EXPR_PREDEFINED);
675 RECORD(EXPR_DECL_REF);
676 RECORD(EXPR_INTEGER_LITERAL);
677 RECORD(EXPR_FLOATING_LITERAL);
678 RECORD(EXPR_IMAGINARY_LITERAL);
679 RECORD(EXPR_STRING_LITERAL);
680 RECORD(EXPR_CHARACTER_LITERAL);
681 RECORD(EXPR_PAREN);
682 RECORD(EXPR_UNARY_OPERATOR);
683 RECORD(EXPR_SIZEOF_ALIGN_OF);
684 RECORD(EXPR_ARRAY_SUBSCRIPT);
685 RECORD(EXPR_CALL);
686 RECORD(EXPR_MEMBER);
687 RECORD(EXPR_BINARY_OPERATOR);
688 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
689 RECORD(EXPR_CONDITIONAL_OPERATOR);
690 RECORD(EXPR_IMPLICIT_CAST);
691 RECORD(EXPR_CSTYLE_CAST);
692 RECORD(EXPR_COMPOUND_LITERAL);
693 RECORD(EXPR_EXT_VECTOR_ELEMENT);
694 RECORD(EXPR_INIT_LIST);
695 RECORD(EXPR_DESIGNATED_INIT);
696 RECORD(EXPR_IMPLICIT_VALUE_INIT);
697 RECORD(EXPR_VA_ARG);
698 RECORD(EXPR_ADDR_LABEL);
699 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000700 RECORD(EXPR_CHOOSE);
701 RECORD(EXPR_GNU_NULL);
702 RECORD(EXPR_SHUFFLE_VECTOR);
703 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000704 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000705 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000706 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000707 RECORD(EXPR_OBJC_ARRAY_LITERAL);
708 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000709 RECORD(EXPR_OBJC_ENCODE);
710 RECORD(EXPR_OBJC_SELECTOR_EXPR);
711 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
712 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
713 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
714 RECORD(EXPR_OBJC_KVC_REF_EXPR);
715 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000716 RECORD(STMT_OBJC_FOR_COLLECTION);
717 RECORD(STMT_OBJC_CATCH);
718 RECORD(STMT_OBJC_FINALLY);
719 RECORD(STMT_OBJC_AT_TRY);
720 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
721 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000722 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000723 RECORD(EXPR_CXX_OPERATOR_CALL);
724 RECORD(EXPR_CXX_CONSTRUCT);
725 RECORD(EXPR_CXX_STATIC_CAST);
726 RECORD(EXPR_CXX_DYNAMIC_CAST);
727 RECORD(EXPR_CXX_REINTERPRET_CAST);
728 RECORD(EXPR_CXX_CONST_CAST);
729 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000730 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000731 RECORD(EXPR_CXX_BOOL_LITERAL);
732 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000733 RECORD(EXPR_CXX_TYPEID_EXPR);
734 RECORD(EXPR_CXX_TYPEID_TYPE);
735 RECORD(EXPR_CXX_UUIDOF_EXPR);
736 RECORD(EXPR_CXX_UUIDOF_TYPE);
737 RECORD(EXPR_CXX_THIS);
738 RECORD(EXPR_CXX_THROW);
739 RECORD(EXPR_CXX_DEFAULT_ARG);
740 RECORD(EXPR_CXX_BIND_TEMPORARY);
741 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
742 RECORD(EXPR_CXX_NEW);
743 RECORD(EXPR_CXX_DELETE);
744 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
745 RECORD(EXPR_EXPR_WITH_CLEANUPS);
746 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
747 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
748 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
749 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
750 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
751 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
752 RECORD(EXPR_CXX_NOEXCEPT);
753 RECORD(EXPR_OPAQUE_VALUE);
754 RECORD(EXPR_BINARY_TYPE_TRAIT);
755 RECORD(EXPR_PACK_EXPANSION);
756 RECORD(EXPR_SIZEOF_PACK);
757 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000758 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000759#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000760}
Mike Stump11289f42009-09-09 15:08:12 +0000761
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000762void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000763 RecordData Record;
764 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000765
Sebastian Redl539c5062010-08-18 23:57:32 +0000766#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
767#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000768
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000769 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000770 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000771 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000772 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000773 RECORD(TYPE_OFFSET);
774 RECORD(DECL_OFFSET);
775 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000776 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000777 RECORD(IDENTIFIER_OFFSET);
778 RECORD(IDENTIFIER_TABLE);
779 RECORD(EXTERNAL_DEFINITIONS);
780 RECORD(SPECIAL_TYPES);
781 RECORD(STATISTICS);
782 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000783 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000784 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
785 RECORD(SELECTOR_OFFSETS);
786 RECORD(METHOD_POOL);
787 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000788 RECORD(SOURCE_LOCATION_OFFSETS);
789 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000790 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000791 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000792 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000793 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregor29cc6422011-08-17 21:07:30 +0000794 RECORD(IMPORTS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000795 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000796 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000797 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000798 RECORD(SEMA_DECL_REFS);
799 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
800 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
801 RECORD(DECL_REPLACEMENTS);
802 RECORD(UPDATE_VISIBLE);
803 RECORD(DECL_UPDATE_OFFSETS);
804 RECORD(DECL_UPDATES);
805 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
806 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000807 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000808 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000809 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000810 RECORD(FP_PRAGMA_OPTIONS);
811 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000812 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000813 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
814 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000815 RECORD(MODULE_OFFSET_MAP);
816 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000817 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000818 RECORD(FILE_SORTED_DECLS);
819 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000820 RECORD(MERGED_DECLARATIONS);
821 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000822 RECORD(OBJC_CATEGORIES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000823
Chris Lattner28fa4e62009-04-26 22:26:21 +0000824 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000825 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000826 RECORD(SM_SLOC_FILE_ENTRY);
827 RECORD(SM_SLOC_BUFFER_ENTRY);
828 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000829 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner28fa4e62009-04-26 22:26:21 +0000831 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000832 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000833 RECORD(PP_MACRO_OBJECT_LIKE);
834 RECORD(PP_MACRO_FUNCTION_LIKE);
835 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000836
Douglas Gregor12bfa382009-10-17 00:13:19 +0000837 // Decls and Types block.
838 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000839 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000840 RECORD(TYPE_COMPLEX);
841 RECORD(TYPE_POINTER);
842 RECORD(TYPE_BLOCK_POINTER);
843 RECORD(TYPE_LVALUE_REFERENCE);
844 RECORD(TYPE_RVALUE_REFERENCE);
845 RECORD(TYPE_MEMBER_POINTER);
846 RECORD(TYPE_CONSTANT_ARRAY);
847 RECORD(TYPE_INCOMPLETE_ARRAY);
848 RECORD(TYPE_VARIABLE_ARRAY);
849 RECORD(TYPE_VECTOR);
850 RECORD(TYPE_EXT_VECTOR);
851 RECORD(TYPE_FUNCTION_PROTO);
852 RECORD(TYPE_FUNCTION_NO_PROTO);
853 RECORD(TYPE_TYPEDEF);
854 RECORD(TYPE_TYPEOF_EXPR);
855 RECORD(TYPE_TYPEOF);
856 RECORD(TYPE_RECORD);
857 RECORD(TYPE_ENUM);
858 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000859 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000860 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000861 RECORD(TYPE_DECLTYPE);
862 RECORD(TYPE_ELABORATED);
863 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
864 RECORD(TYPE_UNRESOLVED_USING);
865 RECORD(TYPE_INJECTED_CLASS_NAME);
866 RECORD(TYPE_OBJC_OBJECT);
867 RECORD(TYPE_TEMPLATE_TYPE_PARM);
868 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
869 RECORD(TYPE_DEPENDENT_NAME);
870 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
871 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
872 RECORD(TYPE_PAREN);
873 RECORD(TYPE_PACK_EXPANSION);
874 RECORD(TYPE_ATTRIBUTED);
875 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000876 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000877 RECORD(DECL_TYPEDEF);
878 RECORD(DECL_ENUM);
879 RECORD(DECL_RECORD);
880 RECORD(DECL_ENUM_CONSTANT);
881 RECORD(DECL_FUNCTION);
882 RECORD(DECL_OBJC_METHOD);
883 RECORD(DECL_OBJC_INTERFACE);
884 RECORD(DECL_OBJC_PROTOCOL);
885 RECORD(DECL_OBJC_IVAR);
886 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000887 RECORD(DECL_OBJC_CATEGORY);
888 RECORD(DECL_OBJC_CATEGORY_IMPL);
889 RECORD(DECL_OBJC_IMPLEMENTATION);
890 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
891 RECORD(DECL_OBJC_PROPERTY);
892 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000893 RECORD(DECL_FIELD);
894 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000895 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000896 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000897 RECORD(DECL_FILE_SCOPE_ASM);
898 RECORD(DECL_BLOCK);
899 RECORD(DECL_CONTEXT_LEXICAL);
900 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000901 RECORD(DECL_NAMESPACE);
902 RECORD(DECL_NAMESPACE_ALIAS);
903 RECORD(DECL_USING);
904 RECORD(DECL_USING_SHADOW);
905 RECORD(DECL_USING_DIRECTIVE);
906 RECORD(DECL_UNRESOLVED_USING_VALUE);
907 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
908 RECORD(DECL_LINKAGE_SPEC);
909 RECORD(DECL_CXX_RECORD);
910 RECORD(DECL_CXX_METHOD);
911 RECORD(DECL_CXX_CONSTRUCTOR);
912 RECORD(DECL_CXX_DESTRUCTOR);
913 RECORD(DECL_CXX_CONVERSION);
914 RECORD(DECL_ACCESS_SPEC);
915 RECORD(DECL_FRIEND);
916 RECORD(DECL_FRIEND_TEMPLATE);
917 RECORD(DECL_CLASS_TEMPLATE);
918 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
919 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
920 RECORD(DECL_FUNCTION_TEMPLATE);
921 RECORD(DECL_TEMPLATE_TYPE_PARM);
922 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
923 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
924 RECORD(DECL_STATIC_ASSERT);
925 RECORD(DECL_CXX_BASE_SPECIFIERS);
926 RECORD(DECL_INDIRECTFIELD);
927 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
928
Douglas Gregor03412ba2011-06-03 02:27:19 +0000929 // Statements and Exprs can occur in the Decls and Types block.
930 AddStmtsExprs(Stream, Record);
931
Douglas Gregor92a96f52011-02-08 21:58:10 +0000932 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000933 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000934 RECORD(PPD_MACRO_DEFINITION);
935 RECORD(PPD_INCLUSION_DIRECTIVE);
936
Chris Lattner28fa4e62009-04-26 22:26:21 +0000937#undef RECORD
938#undef BLOCK
939 Stream.ExitBlock();
940}
941
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000942/// \brief Adjusts the given filename to only write out the portion of the
943/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000944///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000945/// \param Filename the file name to adjust.
946///
947/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
948/// the returned filename will be adjusted by this system root.
949///
950/// \returns either the original filename (if it needs no adjustment) or the
951/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000952static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000953adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000954 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000955
Douglas Gregorc567ba22011-07-22 16:35:34 +0000956 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000957 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000958
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000959 // Verify that the filename and the system root have the same prefix.
960 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000961 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000962 if (Filename[Pos] != isysroot[Pos])
963 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000964
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000965 // We hit the end of the filename before we hit the end of the system root.
966 if (!Filename[Pos])
967 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000968
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000969 // If the file name has a '/' at the current position, skip over the '/'.
970 // We distinguish sysroot-based includes from absolute includes by the
971 // absence of '/' at the beginning of sysroot-based includes.
972 if (Filename[Pos] == '/')
973 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000974
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000975 return Filename + Pos;
976}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000977
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000978/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregorc567ba22011-07-22 16:35:34 +0000979void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000980 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000981 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000982
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000983 // Metadata
Douglas Gregore8bbc122011-09-02 00:18:52 +0000984 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000985 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregor29cc6422011-08-17 21:07:30 +0000986 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000987 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
988 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000989 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
990 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
991 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +0000992 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors
Douglas Gregor29cc6422011-08-17 21:07:30 +0000993 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000994 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000995
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000996 RecordData Record;
Douglas Gregor29cc6422011-08-17 21:07:30 +0000997 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +0000998 Record.push_back(VERSION_MAJOR);
999 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001000 Record.push_back(CLANG_VERSION_MAJOR);
1001 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001002 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001003 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor29cc6422011-08-17 21:07:30 +00001004 const std::string &Triple = Target.getTriple().getTriple();
1005 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
1006
1007 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001008 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1009 llvm::SmallVector<char, 128> ModulePaths;
1010 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001011
1012 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1013 M != MEnd; ++M) {
1014 // Skip modules that weren't directly imported.
1015 if (!(*M)->isDirectlyImported())
1016 continue;
1017
1018 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1019 // FIXME: Write import location, once it matters.
1020 // FIXME: This writes the absolute path for AST files we depend on.
1021 const std::string &FileName = (*M)->FileName;
1022 Record.push_back(FileName.size());
1023 Record.append(FileName.begin(), FileName.end());
1024 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001025 Stream.EmitRecord(IMPORTS, Record);
1026 }
Mike Stump11289f42009-09-09 15:08:12 +00001027
Douglas Gregora3b20262011-05-06 21:43:30 +00001028 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001029 SourceManager &SM = Context.getSourceManager();
1030 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1031 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001032 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001033 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1034 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1035
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001036 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001037
Michael J. Spencer740857f2010-12-21 16:45:57 +00001038 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001039
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001040 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001041 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001042 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001043 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001044 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001045 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +00001046
1047 Record.clear();
1048 Record.push_back(SM.getMainFileID().getOpaqueValue());
1049 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001050 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001051
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001052 // Original PCH directory
1053 if (!OutputFile.empty() && OutputFile != "-") {
1054 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1055 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1057 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1058
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001059 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001060
1061 llvm::sys::fs::make_absolute(OutputPath);
1062 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1063
1064 RecordData Record;
1065 Record.push_back(ORIGINAL_PCH_DIR);
1066 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1067 }
1068
Ted Kremenek18e066f2010-01-22 22:12:47 +00001069 // Repository branch/version information.
1070 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001071 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001072 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1073 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001074 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001075 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001076 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1077 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001078}
1079
1080/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001081void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001082 RecordData Record;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00001083#define LANGOPT(Name, Bits, Default, Description) \
1084 Record.push_back(LangOpts.Name);
1085#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1086 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1087#include "clang/Basic/LangOptions.def"
John McCall5fb5df92012-06-20 06:18:46 +00001088
1089 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1090 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Douglas Gregor7d106e42011-11-15 19:35:01 +00001091
1092 Record.push_back(LangOpts.CurrentModule.size());
1093 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl539c5062010-08-18 23:57:32 +00001094 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001095}
1096
Douglas Gregora7f71a92009-04-10 03:52:48 +00001097//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001098// stat cache Serialization
1099//===----------------------------------------------------------------------===//
1100
1101namespace {
1102// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001103class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001104public:
1105 typedef const char * key_type;
1106 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001107
Chris Lattner2a6fa472010-11-23 19:28:12 +00001108 typedef struct stat data_type;
1109 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001110
1111 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001112 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001113 }
Mike Stump11289f42009-09-09 15:08:12 +00001114
1115 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001116 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001117 data_type_ref Data) {
1118 unsigned StrLen = strlen(path);
1119 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001120 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001121 clang::io::Emit8(Out, DataLen);
1122 return std::make_pair(StrLen + 1, DataLen);
1123 }
Mike Stump11289f42009-09-09 15:08:12 +00001124
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001125 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001126 Out.write(path, KeyLen);
1127 }
Mike Stump11289f42009-09-09 15:08:12 +00001128
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001129 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001130 data_type_ref Data, unsigned DataLen) {
1131 using namespace clang::io;
1132 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001133
Chris Lattner2a6fa472010-11-23 19:28:12 +00001134 Emit32(Out, (uint32_t) Data.st_ino);
1135 Emit32(Out, (uint32_t) Data.st_dev);
1136 Emit16(Out, (uint16_t) Data.st_mode);
1137 Emit64(Out, (uint64_t) Data.st_mtime);
1138 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001139
1140 assert(Out.tell() - Start == DataLen && "Wrong data length");
1141 }
1142};
1143} // end anonymous namespace
1144
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001145/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001146void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001147 // Build the on-disk hash table containing information about every
1148 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001149 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001150 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001151 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001152 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001153 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001154 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001155 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Douglas Gregorc5046832009-04-27 18:38:38 +00001158 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001159 SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001160 uint32_t BucketOffset;
1161 {
1162 llvm::raw_svector_ostream Out(StatCacheData);
1163 // Make sure that no bucket is at offset 0
1164 clang::io::Emit32(Out, 0);
1165 BucketOffset = Generator.Emit(Out);
1166 }
1167
1168 // Create a blob abbreviation
1169 using namespace llvm;
1170 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001171 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1175 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1176
1177 // Write the stat cache
1178 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001179 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001180 Record.push_back(BucketOffset);
1181 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001182 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001183}
1184
1185//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001186// Source Manager Serialization
1187//===----------------------------------------------------------------------===//
1188
1189/// \brief Create an abbreviation for the SLocEntry that refers to a
1190/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001191static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001192 using namespace llvm;
1193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001194 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001199 // FileEntry fields.
1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor9dc32122011-11-16 20:05:18 +00001202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregora7f71a92009-04-10 03:52:48 +00001206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001207 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001208}
1209
1210/// \brief Create an abbreviation for the SLocEntry that refers to a
1211/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001212static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001213 using namespace llvm;
1214 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001215 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001221 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001222}
1223
1224/// \brief Create an abbreviation for the SLocEntry that refers to a
1225/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001226static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001227 using namespace llvm;
1228 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001229 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001231 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001232}
1233
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001234/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1235/// expansion.
1236static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001237 using namespace llvm;
1238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001239 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001245 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001246}
1247
Douglas Gregor09b69892011-02-10 17:09:37 +00001248namespace {
1249 // Trait used for the on-disk hash table of header search information.
1250 class HeaderFileInfoTrait {
1251 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001252
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001253 // Keep track of the framework names we've used during serialization.
1254 SmallVector<char, 128> FrameworkStringData;
1255 llvm::StringMap<unsigned> FrameworkNameOffset;
1256
Douglas Gregor09b69892011-02-10 17:09:37 +00001257 public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001258 HeaderFileInfoTrait(ASTWriter &Writer)
1259 : Writer(Writer) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001260
1261 typedef const char *key_type;
1262 typedef key_type key_type_ref;
1263
1264 typedef HeaderFileInfo data_type;
1265 typedef const data_type &data_type_ref;
1266
1267 static unsigned ComputeHash(const char *path) {
1268 // The hash is based only on the filename portion of the key, so that the
1269 // reader can match based on filenames when symlinking or excess path
1270 // elements ("foo/../", "../") change the form of the name. However,
1271 // complete path is still the key.
1272 return llvm::HashString(llvm::sys::path::filename(path));
1273 }
1274
1275 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001276 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001277 data_type_ref Data) {
1278 unsigned StrLen = strlen(path);
1279 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001280 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001281 clang::io::Emit8(Out, DataLen);
1282 return std::make_pair(StrLen + 1, DataLen);
1283 }
1284
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001285 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001286 Out.write(path, KeyLen);
1287 }
1288
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001289 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001290 data_type_ref Data, unsigned DataLen) {
1291 using namespace clang::io;
1292 uint64_t Start = Out.tell(); (void)Start;
1293
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001294 unsigned char Flags = (Data.isImport << 5)
1295 | (Data.isPragmaOnce << 4)
1296 | (Data.DirInfo << 2)
1297 | (Data.Resolved << 1)
1298 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001299 Emit8(Out, (uint8_t)Flags);
1300 Emit16(Out, (uint16_t) Data.NumIncludes);
1301
1302 if (!Data.ControllingMacro)
1303 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1304 else
1305 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001306
1307 unsigned Offset = 0;
1308 if (!Data.Framework.empty()) {
1309 // If this header refers into a framework, save the framework name.
1310 llvm::StringMap<unsigned>::iterator Pos
1311 = FrameworkNameOffset.find(Data.Framework);
1312 if (Pos == FrameworkNameOffset.end()) {
1313 Offset = FrameworkStringData.size() + 1;
1314 FrameworkStringData.append(Data.Framework.begin(),
1315 Data.Framework.end());
1316 FrameworkStringData.push_back(0);
1317
1318 FrameworkNameOffset[Data.Framework] = Offset;
1319 } else
1320 Offset = Pos->second;
1321 }
1322 Emit32(Out, Offset);
1323
Douglas Gregor09b69892011-02-10 17:09:37 +00001324 assert(Out.tell() - Start == DataLen && "Wrong data length");
1325 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001326
1327 const char *strings_begin() const { return FrameworkStringData.begin(); }
1328 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001329 };
1330} // end anonymous namespace
1331
1332/// \brief Write the header search block for the list of files that
1333///
1334/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001335void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001336 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001337 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1338
1339 if (FilesByUID.size() > HS.header_file_size())
1340 FilesByUID.resize(HS.header_file_size());
1341
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001342 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregor09b69892011-02-10 17:09:37 +00001343 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001344 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001345 unsigned NumHeaderSearchEntries = 0;
1346 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1347 const FileEntry *File = FilesByUID[UID];
1348 if (!File)
1349 continue;
1350
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001351 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1352 // from the external source if it was not provided already.
1353 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001354 if (HFI.External && Chain)
1355 continue;
1356
1357 // Turn the file name into an absolute path, if it isn't already.
1358 const char *Filename = File->getName();
1359 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1360
1361 // If we performed any translation on the file name at all, we need to
1362 // save this string, since the generator will refer to it later.
1363 if (Filename != File->getName()) {
1364 Filename = strdup(Filename);
1365 SavedStrings.push_back(Filename);
1366 }
1367
1368 Generator.insert(Filename, HFI, GeneratorTrait);
1369 ++NumHeaderSearchEntries;
1370 }
1371
1372 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001373 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001374 uint32_t BucketOffset;
1375 {
1376 llvm::raw_svector_ostream Out(TableData);
1377 // Make sure that no bucket is at offset 0
1378 clang::io::Emit32(Out, 0);
1379 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1380 }
1381
1382 // Create a blob abbreviation
1383 using namespace llvm;
1384 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1385 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1390 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1391
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001392 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001393 RecordData Record;
1394 Record.push_back(HEADER_SEARCH_TABLE);
1395 Record.push_back(BucketOffset);
1396 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001397 Record.push_back(TableData.size());
1398 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001399 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1400
1401 // Free all of the strings we had to duplicate.
1402 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1403 free((void*)SavedStrings[I]);
1404}
1405
Douglas Gregora7f71a92009-04-10 03:52:48 +00001406/// \brief Writes the block containing the serialized form of the
1407/// source manager.
1408///
1409/// TODO: We should probably use an on-disk hash table (stored in a
1410/// blob), indexed based on the file name, so that we only create
1411/// entries for files that we actually need. In the common case (no
1412/// errors), we probably won't have to create file entries for any of
1413/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001414void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001415 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001416 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001417 RecordData Record;
1418
Chris Lattner0910e3b2009-04-10 17:16:57 +00001419 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001420 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001421
1422 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001423 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1424 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1425 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001426 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001427
Douglas Gregor258ae542009-04-27 06:38:32 +00001428 // Write out the source location entry table. We skip the first
1429 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001430 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001431 // Write out the offsets of only source location file entries.
1432 // We will go through them in ASTReader::validateFileEntries().
1433 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001434 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001435 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1436 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001437 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001438 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001439 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001440 FileID FID = FileID::get(I);
1441 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001442
Douglas Gregor258ae542009-04-27 06:38:32 +00001443 // Record the offset of this source-location entry.
1444 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1445
1446 // Figure out which record code to use.
1447 unsigned Code;
1448 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001449 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1450 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001451 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001452 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1453 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001454 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001455 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001456 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001457 Record.clear();
1458 Record.push_back(Code);
1459
Douglas Gregor925296b2011-07-19 16:10:42 +00001460 // Starting offset of this entry within this module, so skip the dummy.
1461 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001462 if (SLoc->isFile()) {
1463 const SrcMgr::FileInfo &File = SLoc->getFile();
1464 Record.push_back(File.getIncludeLoc().getRawEncoding());
1465 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1466 Record.push_back(File.hasLineDirectives());
1467
1468 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001469 if (Content->OrigEntry) {
1470 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001471 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001472
Douglas Gregor258ae542009-04-27 06:38:32 +00001473 // The source location entry is a file. The blob associated
1474 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001475
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001476 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001477 Record.push_back(Content->OrigEntry->getSize());
1478 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor9dc32122011-11-16 20:05:18 +00001479 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001480 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001481
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001482 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001483 if (FDI != FileDeclIDs.end()) {
1484 Record.push_back(FDI->second->FirstDeclIndex);
1485 Record.push_back(FDI->second->DeclIDs.size());
1486 } else {
1487 Record.push_back(0);
1488 Record.push_back(0);
1489 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001490
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001491 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001492 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001493 SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001494
1495 // Ask the file manager to fixup the relative path for us. This will
1496 // honor the working directory.
1497 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1498
1499 // FIXME: This call to make_absolute shouldn't be necessary, the
1500 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001501 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001502 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001503
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001504 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001505 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001506
1507 if (Content->BufferOverridden) {
1508 Record.clear();
1509 Record.push_back(SM_SLOC_BUFFER_BLOB);
1510 const llvm::MemoryBuffer *Buffer
1511 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1512 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1513 StringRef(Buffer->getBufferStart(),
1514 Buffer->getBufferSize() + 1));
1515 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001516 } else {
1517 // The source location entry is a buffer. The blob associated
1518 // with this entry contains the contents of the buffer.
1519
1520 // We add one to the size so that we capture the trailing NULL
1521 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1522 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001523 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001524 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001525 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001526 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001527 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001528 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001529 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001530 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001531 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001532 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001533
Douglas Gregor925296b2011-07-19 16:10:42 +00001534 if (strcmp(Name, "<built-in>") == 0) {
1535 PreloadSLocs.push_back(SLocEntryOffsets.size());
1536 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001537 }
1538 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001539 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001540 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001541 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1542 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001543 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1544 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001545
1546 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001547 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001548 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001549 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001550 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001551 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001552 }
1553 }
1554
Douglas Gregor8f45df52009-04-16 22:23:12 +00001555 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001556
1557 if (SLocEntryOffsets.empty())
1558 return;
1559
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001560 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001561 // table is used for lazily loading source-location information.
1562 using namespace llvm;
1563 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001564 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1568 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001569
Douglas Gregor258ae542009-04-27 06:38:32 +00001570 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001571 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001572 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001573 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001574 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001575
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001576 Abbrev = new BitCodeAbbrev();
1577 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1580 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1581
1582 Record.clear();
1583 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1584 Record.push_back(SLocFileEntryOffsets.size());
1585 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1586 data(SLocFileEntryOffsets));
1587
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001588 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001589 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001590 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001591
1592 // Write the line table. It depends on remapping working, so it must come
1593 // after the source location offsets.
1594 if (SourceMgr.hasLineTable()) {
1595 LineTableInfo &LineTable = SourceMgr.getLineTable();
1596
1597 Record.clear();
1598 // Emit the file names
1599 Record.push_back(LineTable.getNumFilenames());
1600 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1601 // Emit the file name
1602 const char *Filename = LineTable.getFilename(I);
1603 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1604 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1605 Record.push_back(FilenameLen);
1606 if (FilenameLen)
1607 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1608 }
1609
1610 // Emit the line entries
1611 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1612 L != LEnd; ++L) {
1613 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001614 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001615 continue;
1616
1617 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001618 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001619
1620 // Emit the line entries
1621 Record.push_back(L->second.size());
1622 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1623 LEEnd = L->second.end();
1624 LE != LEEnd; ++LE) {
1625 Record.push_back(LE->FileOffset);
1626 Record.push_back(LE->LineNo);
1627 Record.push_back(LE->FilenameID);
1628 Record.push_back((unsigned)LE->FileKind);
1629 Record.push_back(LE->IncludeOffset);
1630 }
1631 }
1632 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1633 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001634}
1635
Douglas Gregorc5046832009-04-27 18:38:38 +00001636//===----------------------------------------------------------------------===//
1637// Preprocessor Serialization
1638//===----------------------------------------------------------------------===//
1639
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001640static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1641 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1642 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1643 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1644 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1645 return X.first->getName().compare(Y.first->getName());
1646}
1647
Chris Lattnereeffaef2009-04-10 17:15:23 +00001648/// \brief Writes the block containing the serialized form of the
1649/// preprocessor.
1650///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001651void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001652 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1653 if (PPRec)
1654 WritePreprocessorDetail(*PPRec);
1655
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001656 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001657
Chris Lattner0af3ba12009-04-13 01:29:17 +00001658 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1659 if (PP.getCounterValue() != 0) {
1660 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001661 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001662 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001663 }
1664
1665 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001666 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001667
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001668 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001669 // FIXME: use diagnostics subsystem for localization etc.
1670 if (PP.SawDateOrTime())
1671 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001672
Douglas Gregor796d76a2010-10-20 22:00:55 +00001673
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001674 // Loop over all the macro definitions that are live at the end of the file,
1675 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001676
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001677 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001678 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001679 MacrosToEmit;
1680 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001681 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor68051a72011-02-11 00:26:14 +00001682 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001683 I != E; ++I) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001684 if (!IsModule || I->second->isPublic()) {
1685 MacroDefinitionsSeen.insert(I->first);
1686 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001687 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001688 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001689
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001690 // Sort the set of macro definitions that need to be serialized by the
1691 // name of the macro, to provide a stable ordering.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001692 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001693 &compareMacroDefinitions);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001694
Douglas Gregor68051a72011-02-11 00:26:14 +00001695 // Resolve any identifiers that defined macros at the time they were
1696 // deserialized, adding them to the list of macros to emit (if appropriate).
1697 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1698 IdentifierInfo *Name
1699 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001700 if (Name->hadMacroDefinition() && MacroDefinitionsSeen.insert(Name))
Douglas Gregor68051a72011-02-11 00:26:14 +00001701 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1702 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001703
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001704 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1705 const IdentifierInfo *Name = MacrosToEmit[I].first;
1706 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001707 if (!MI)
1708 continue;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001709
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001710 // History of macro definitions for this identifier in chronological order.
1711 SmallVector<MacroInfo*, 8> MacroHistory;
1712 while (MI) {
1713 MacroHistory.push_back(MI);
1714 MI = MI->getPreviousDefinition();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001715 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001716
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001717 while (!MacroHistory.empty()) {
1718 MI = MacroHistory.pop_back_val();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001719
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001720 // Don't emit builtin macros like __LINE__ to the AST file unless they
1721 // have been redefined by the header (in which case they are not
1722 // isBuiltinMacro).
1723 // Also skip macros from a AST file if we're chaining.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001724
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001725 // FIXME: There is a (probably minor) optimization we could do here, if
1726 // the macro comes from the original PCH but the identifier comes from a
1727 // chained PCH, by storing the offset into the original PCH rather than
1728 // writing the macro definition a second time.
1729 if (MI->isBuiltinMacro() ||
1730 (Chain &&
1731 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1732 MI->isFromAST() && !MI->hasChangedAfterLoad()))
1733 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001734
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001735 AddIdentifierRef(Name, Record);
1736 MacroOffsets[Name] = Stream.GetCurrentBitNo();
1737 AddSourceLocation(MI->getDefinitionLoc(), Record);
1738 AddSourceLocation(MI->getUndefLoc(), Record);
1739 Record.push_back(MI->isUsed());
1740 Record.push_back(MI->isPublic());
1741 AddSourceLocation(MI->getVisibilityLocation(), Record);
1742 unsigned Code;
1743 if (MI->isObjectLike()) {
1744 Code = PP_MACRO_OBJECT_LIKE;
1745 } else {
1746 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001747
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001748 Record.push_back(MI->isC99Varargs());
1749 Record.push_back(MI->isGNUVarargs());
1750 Record.push_back(MI->getNumArgs());
1751 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1752 I != E; ++I)
1753 AddIdentifierRef(*I, Record);
1754 }
Mike Stump11289f42009-09-09 15:08:12 +00001755
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001756 // If we have a detailed preprocessing record, record the macro definition
1757 // ID that corresponds to this macro.
1758 if (PPRec)
1759 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1760
1761 Stream.EmitRecord(Code, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001762 Record.clear();
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001763
1764 // Emit the tokens array.
1765 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1766 // Note that we know that the preprocessor does not have any annotation
1767 // tokens in it because they are created by the parser, and thus can't
1768 // be in a macro definition.
1769 const Token &Tok = MI->getReplacementToken(TokNo);
1770
1771 Record.push_back(Tok.getLocation().getRawEncoding());
1772 Record.push_back(Tok.getLength());
1773
1774 // FIXME: When reading literal tokens, reconstruct the literal pointer
1775 // if it is needed.
1776 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1777 // FIXME: Should translate token kind to a stable encoding.
1778 Record.push_back(Tok.getKind());
1779 // FIXME: Should translate token flags to a stable encoding.
1780 Record.push_back(Tok.getFlags());
1781
1782 Stream.EmitRecord(PP_TOKEN, Record);
1783 Record.clear();
1784 }
1785 ++NumMacros;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001786 }
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001787 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001788 Stream.ExitBlock();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001789}
1790
1791void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001792 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001793 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001794
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001795 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001796
Douglas Gregor92a96f52011-02-08 21:58:10 +00001797 // Enter the preprocessor block.
1798 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001799
Douglas Gregoraae92242010-03-19 21:51:54 +00001800 // If the preprocessor has a preprocessing record, emit it.
1801 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001802 using namespace llvm;
1803
1804 // Set up the abbreviation for
1805 unsigned InclusionAbbrev = 0;
1806 {
1807 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1808 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00001813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1814 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1815 }
1816
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001817 unsigned FirstPreprocessorEntityID
1818 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1819 + NUM_PREDEF_PP_ENTITY_IDS;
1820 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001821 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001822 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1823 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001824 E != EEnd;
1825 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001826 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001827
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001828 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1829 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001830
Douglas Gregor92a96f52011-02-08 21:58:10 +00001831 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001832 // Record this macro definition's ID.
1833 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001834
Douglas Gregor92a96f52011-02-08 21:58:10 +00001835 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001836 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1837 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001838 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001839
Chandler Carrutha88a22182011-07-14 08:20:46 +00001840 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001841 Record.push_back(ME->isBuiltinMacro());
1842 if (ME->isBuiltinMacro())
1843 AddIdentifierRef(ME->getName(), Record);
1844 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001845 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001846 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001847 continue;
1848 }
1849
1850 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1851 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001852 Record.push_back(ID->getFileName().size());
1853 Record.push_back(ID->wasInQuotes());
1854 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001855 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001856 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001857 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00001858 // Check that the FileEntry is not null because it was not resolved and
1859 // we create a PCH even with compiler errors.
1860 if (ID->getFile())
1861 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001862 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1863 continue;
1864 }
1865
1866 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1867 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001868 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001869
Douglas Gregoraae92242010-03-19 21:51:54 +00001870 // Write the offsets table for the preprocessing record.
1871 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001872 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1873
Douglas Gregoraae92242010-03-19 21:51:54 +00001874 // Write the offsets table for identifier IDs.
1875 using namespace llvm;
1876 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001877 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001880 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001881
Douglas Gregoraae92242010-03-19 21:51:54 +00001882 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001883 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001884 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001885 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1886 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001887 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001888}
1889
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001890unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1891 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1892 if (Known != SubmoduleIDs.end())
1893 return Known->second;
1894
1895 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1896}
1897
Douglas Gregor253eefe2011-12-01 00:59:36 +00001898/// \brief Compute the number of modules within the given tree (including the
1899/// given module).
1900static unsigned getNumberOfModules(Module *Mod) {
1901 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00001902 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1903 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00001904 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00001905 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00001906
1907 return ChildModules + 1;
1908}
1909
Douglas Gregorde3ef502011-11-30 23:21:26 +00001910void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00001911 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001912 // FIXME: This feels like it belongs somewhere else, but there are no
1913 // other consumers of this information.
1914 SourceManager &SrcMgr = PP->getSourceManager();
1915 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1916 for (ASTContext::import_iterator I = Context->local_import_begin(),
1917 IEnd = Context->local_import_end();
1918 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001919 if (Module *ImportedFrom
1920 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1921 SrcMgr))) {
1922 ImportedFrom->Imports.push_back(I->getImportedModule());
1923 }
1924 }
1925
Douglas Gregor69021972011-11-30 17:33:56 +00001926 // Enter the submodule description block.
1927 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1928
1929 // Write the abbreviations needed for the submodules block.
1930 using namespace llvm;
1931 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1932 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00001934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00001937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00001939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00001940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00001941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1942 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1943
1944 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001945 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00001946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1947 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1948
1949 Abbrev = new BitCodeAbbrev();
1950 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1952 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00001953
1954 Abbrev = new BitCodeAbbrev();
1955 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1957 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1958
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001959 Abbrev = new BitCodeAbbrev();
1960 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1962 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1963
Douglas Gregor253eefe2011-12-01 00:59:36 +00001964 // Write the submodule metadata block.
1965 RecordData Record;
1966 Record.push_back(getNumberOfModules(WritingModule));
1967 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1968 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1969
Douglas Gregor69021972011-11-30 17:33:56 +00001970 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001971 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00001972 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00001973 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001974 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00001975 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001976 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00001977
1978 // Emit the definition of the block.
1979 Record.clear();
1980 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001981 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00001982 if (Mod->Parent) {
1983 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1984 Record.push_back(SubmoduleIDs[Mod->Parent]);
1985 } else {
1986 Record.push_back(0);
1987 }
1988 Record.push_back(Mod->IsFramework);
1989 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00001990 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00001991 Record.push_back(Mod->InferSubmodules);
1992 Record.push_back(Mod->InferExplicitSubmodules);
1993 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00001994 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1995
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001996 // Emit the requirements.
1997 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1998 Record.clear();
1999 Record.push_back(SUBMODULE_REQUIRES);
2000 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2001 Mod->Requires[I].data(),
2002 Mod->Requires[I].size());
2003 }
2004
Douglas Gregor69021972011-11-30 17:33:56 +00002005 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002006 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002007 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002008 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002009 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002010 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002011 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2012 Record.clear();
2013 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2014 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2015 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002016 }
2017
2018 // Emit the headers.
2019 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2020 Record.clear();
2021 Record.push_back(SUBMODULE_HEADER);
2022 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2023 Mod->Headers[I]->getName());
2024 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002025
2026 // Emit the imports.
2027 if (!Mod->Imports.empty()) {
2028 Record.clear();
2029 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002030 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002031 assert(ImportedID && "Unknown submodule!");
2032 Record.push_back(ImportedID);
2033 }
2034 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2035 }
2036
Douglas Gregor24bb9232011-12-02 18:58:38 +00002037 // Emit the exports.
2038 if (!Mod->Exports.empty()) {
2039 Record.clear();
2040 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002041 if (Module *Exported = Mod->Exports[I].getPointer()) {
2042 unsigned ExportedID = SubmoduleIDs[Exported];
2043 assert(ExportedID > 0 && "Unknown submodule ID?");
2044 Record.push_back(ExportedID);
2045 } else {
2046 Record.push_back(0);
2047 }
2048
Douglas Gregor24bb9232011-12-02 18:58:38 +00002049 Record.push_back(Mod->Exports[I].getInt());
2050 }
2051 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2052 }
2053
Douglas Gregor69021972011-11-30 17:33:56 +00002054 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002055 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2056 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002057 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002058 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002059 }
2060
2061 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002062
2063 assert((NextSubmoduleID - FirstSubmoduleID
2064 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002065}
2066
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002067serialization::SubmoduleID
2068ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002069 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002070 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002071
2072 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002073 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002074 Module *OwningMod
2075 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002076 if (!OwningMod)
2077 return 0;
2078
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002079 // Check whether this submodule is part of our own module.
2080 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002081 return 0;
2082
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002083 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002084}
2085
David Blaikie9c902b52011-09-25 23:23:43 +00002086void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002087 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002088 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002089 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2090 I != E; ++I) {
David Blaikie9c902b52011-09-25 23:23:43 +00002091 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002092 if (point.Loc.isInvalid())
2093 continue;
2094
2095 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbare8c12a22011-09-29 01:42:25 +00002096 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002097 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbara3637e62011-09-29 01:30:00 +00002098 if (I->second.isPragma()) {
2099 Record.push_back(I->first);
2100 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002101 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002102 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002103 Record.push_back(-1); // mark the end of the diag/map pairs for this
2104 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002105 }
2106
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002107 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002108 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002109}
2110
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002111void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2112 if (CXXBaseSpecifiersOffsets.empty())
2113 return;
2114
2115 RecordData Record;
2116
2117 // Create a blob abbreviation for the C++ base specifiers offsets.
2118 using namespace llvm;
2119
2120 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2121 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2124 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2125
Douglas Gregorc27b2872011-08-04 00:01:48 +00002126 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002127 Record.clear();
2128 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2129 Record.push_back(CXXBaseSpecifiersOffsets.size());
2130 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002131 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002132}
2133
Douglas Gregorc5046832009-04-27 18:38:38 +00002134//===----------------------------------------------------------------------===//
2135// Type Serialization
2136//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002138/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002139void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002140 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002141 if (Idx.getIndex() == 0) // we haven't seen this type before.
2142 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002143
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002144 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002145
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002146 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002147 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002148 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002149 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002150 else if (TypeOffsets.size() < Index) {
2151 TypeOffsets.resize(Index + 1);
2152 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002153 }
2154
2155 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002156
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002157 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002158 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002159
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002160 if (T.hasLocalNonFastQualifiers()) {
2161 Qualifiers Qs = T.getLocalQualifiers();
2162 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002163 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002164 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002165 } else {
2166 switch (T->getTypeClass()) {
2167 // For all of the concrete, non-dependent types, call the
2168 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002169#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002170 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002171#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002172#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002173 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002174 }
2175
2176 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002177 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002178
2179 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002180 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002181}
2182
Douglas Gregorc5046832009-04-27 18:38:38 +00002183//===----------------------------------------------------------------------===//
2184// Declaration Serialization
2185//===----------------------------------------------------------------------===//
2186
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002187/// \brief Write the block containing all of the declaration IDs
2188/// lexically declared within the given DeclContext.
2189///
2190/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2191/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002192uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002193 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002194 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002195 return 0;
2196
Douglas Gregor8f45df52009-04-16 22:23:12 +00002197 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002198 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002199 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002200 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002201 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2202 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002203 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002204
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002205 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002206 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002207 return Offset;
2208}
2209
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002210void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002211 using namespace llvm;
2212 RecordData Record;
2213
2214 // Write the type offsets array
2215 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002216 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2220 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2221 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002222 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002223 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002224 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002225 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002226
2227 // Write the declaration offsets array
2228 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002229 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2233 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2234 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002235 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002236 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002237 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002238 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002239}
2240
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002241void ASTWriter::WriteFileDeclIDsMap() {
2242 using namespace llvm;
2243 RecordData Record;
2244
2245 // Join the vectors of DeclIDs from all files.
2246 SmallVector<DeclID, 256> FileSortedIDs;
2247 for (FileDeclIDsTy::iterator
2248 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2249 DeclIDInFileInfo &Info = *FI->second;
2250 Info.FirstDeclIndex = FileSortedIDs.size();
2251 for (LocDeclIDsTy::iterator
2252 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2253 FileSortedIDs.push_back(DI->second);
2254 }
2255
2256 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2257 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2260 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2261 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002262 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002263 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2264}
2265
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002266void ASTWriter::WriteComments() {
2267 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002268 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002269 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002270 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2271 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002272 I != E; ++I) {
2273 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002274 AddSourceRange((*I)->getSourceRange(), Record);
2275 Record.push_back((*I)->getKind());
2276 Record.push_back((*I)->isTrailingComment());
2277 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002278 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2279 }
2280 Stream.ExitBlock();
2281}
2282
Douglas Gregorc5046832009-04-27 18:38:38 +00002283//===----------------------------------------------------------------------===//
2284// Global Method Pool and Selector Serialization
2285//===----------------------------------------------------------------------===//
2286
Douglas Gregore84a9da2009-04-20 20:36:09 +00002287namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002288// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002289class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002290 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002291
2292public:
2293 typedef Selector key_type;
2294 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002295
Sebastian Redl834bb972010-08-04 17:20:04 +00002296 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002297 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002298 ObjCMethodList Instance, Factory;
2299 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002300 typedef const data_type& data_type_ref;
2301
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002302 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002303
Douglas Gregorc78d3462009-04-24 21:10:55 +00002304 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002305 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002306 }
Mike Stump11289f42009-09-09 15:08:12 +00002307
2308 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002309 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002310 data_type_ref Methods) {
2311 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2312 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002313 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2314 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002315 Method = Method->Next)
2316 if (Method->Method)
2317 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002318 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002319 Method = Method->Next)
2320 if (Method->Method)
2321 DataLen += 4;
2322 clang::io::Emit16(Out, DataLen);
2323 return std::make_pair(KeyLen, DataLen);
2324 }
Mike Stump11289f42009-09-09 15:08:12 +00002325
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002326 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002327 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002328 assert((Start >> 32) == 0 && "Selector key offset too large");
2329 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002330 unsigned N = Sel.getNumArgs();
2331 clang::io::Emit16(Out, N);
2332 if (N == 0)
2333 N = 1;
2334 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002335 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002336 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2337 }
Mike Stump11289f42009-09-09 15:08:12 +00002338
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002339 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002340 data_type_ref Methods, unsigned DataLen) {
2341 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002342 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002343 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002344 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002345 Method = Method->Next)
2346 if (Method->Method)
2347 ++NumInstanceMethods;
2348
2349 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002350 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002351 Method = Method->Next)
2352 if (Method->Method)
2353 ++NumFactoryMethods;
2354
2355 clang::io::Emit16(Out, NumInstanceMethods);
2356 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002357 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002358 Method = Method->Next)
2359 if (Method->Method)
2360 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002361 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002362 Method = Method->Next)
2363 if (Method->Method)
2364 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002365
2366 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002367 }
2368};
2369} // end anonymous namespace
2370
Sebastian Redla19a67f2010-08-03 21:58:15 +00002371/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002372///
2373/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002374/// in an on-disk hash table indexed by the selector. The hash table also
2375/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002376void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002377 using namespace llvm;
2378
Sebastian Redla19a67f2010-08-03 21:58:15 +00002379 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002380 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002381 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002382 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002383 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002384 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002385 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002386 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002387
Sebastian Redla19a67f2010-08-03 21:58:15 +00002388 // Create the on-disk hash table representation. We walk through every
2389 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002390 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002391 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002392 I = SelectorIDs.begin(), E = SelectorIDs.end();
2393 I != E; ++I) {
2394 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002395 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002396 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002397 I->second,
2398 ObjCMethodList(),
2399 ObjCMethodList()
2400 };
2401 if (F != SemaRef.MethodPool.end()) {
2402 Data.Instance = F->second.first;
2403 Data.Factory = F->second.second;
2404 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002405 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002406 // changed.
2407 if (Chain && I->second < FirstSelectorID) {
2408 // Selector already exists. Did it change?
2409 bool changed = false;
2410 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2411 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002412 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002413 changed = true;
2414 }
2415 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2416 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002417 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002418 changed = true;
2419 }
2420 if (!changed)
2421 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002422 } else if (Data.Instance.Method || Data.Factory.Method) {
2423 // A new method pool entry.
2424 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002425 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002426 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002427 }
2428
Douglas Gregorc78d3462009-04-24 21:10:55 +00002429 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002430 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002431 uint32_t BucketOffset;
2432 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002433 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002434 llvm::raw_svector_ostream Out(MethodPool);
2435 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002436 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002437 BucketOffset = Generator.Emit(Out, Trait);
2438 }
2439
2440 // Create a blob abbreviation
2441 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002442 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2446 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2447
Douglas Gregor95c13f52009-04-25 17:48:32 +00002448 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002449 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002450 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002451 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002452 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002453 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002454
2455 // Create a blob abbreviation for the selector table offsets.
2456 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002457 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2461 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2462
2463 // Write the selector offsets table.
2464 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002465 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002466 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002467 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002468 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002469 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002470 }
2471}
2472
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002473/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002474void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002475 using namespace llvm;
2476 if (SemaRef.ReferencedSelectors.empty())
2477 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002478
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002479 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002480
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002481 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002482 // very tricky to fix, and given that @selector shouldn't really appear in
2483 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002484 for (DenseMap<Selector, SourceLocation>::iterator S =
2485 SemaRef.ReferencedSelectors.begin(),
2486 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2487 Selector Sel = (*S).first;
2488 SourceLocation Loc = (*S).second;
2489 AddSelectorRef(Sel, Record);
2490 AddSourceLocation(Loc, Record);
2491 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002492 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002493}
2494
Douglas Gregorc5046832009-04-27 18:38:38 +00002495//===----------------------------------------------------------------------===//
2496// Identifier Table Serialization
2497//===----------------------------------------------------------------------===//
2498
Douglas Gregorc78d3462009-04-24 21:10:55 +00002499namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002500class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002501 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002502 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002503 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002504 bool IsModule;
2505
Douglas Gregor1d583f22009-04-28 21:18:29 +00002506 /// \brief Determines whether this is an "interesting" identifier
2507 /// that needs a full IdentifierInfo structure written into the hash
2508 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002509 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002510 if (II->isPoisoned() ||
2511 II->isExtensionToken() ||
2512 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002513 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002514 II->getFETokenInfo<void>())
2515 return true;
2516
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002517 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002518 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002519
2520 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2521 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002522 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002523
2524 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002525 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002526
2527 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002528 }
2529
Douglas Gregore84a9da2009-04-20 20:36:09 +00002530public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002531 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002532 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002533
Sebastian Redl539c5062010-08-18 23:57:32 +00002534 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002535 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002536
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002537 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2538 IdentifierResolver &IdResolver, bool IsModule)
2539 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002540
2541 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002542 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002543 }
Mike Stump11289f42009-09-09 15:08:12 +00002544
2545 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002546 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002547 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002548 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002549 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002550 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002551 DataLen += 2; // 2 bytes for builtin ID
2552 DataLen += 2; // 2 bytes for flags
2553 if (hadMacroDefinition(II, Macro))
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002554 DataLen += 8;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002555
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002556 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2557 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002558 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002559 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002560 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002561 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002562 // We emit the key length after the data length so that every
2563 // string is preceded by a 16-bit length. This matches the PTH
2564 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002565 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002566 return std::make_pair(KeyLen, DataLen);
2567 }
Mike Stump11289f42009-09-09 15:08:12 +00002568
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002569 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002570 unsigned KeyLen) {
2571 // Record the location of the key data. This is used when generating
2572 // the mapping from persistent IDs to strings.
2573 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002574 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002575 }
Mike Stump11289f42009-09-09 15:08:12 +00002576
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002577 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002578 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002579 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002580 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002581 clang::io::Emit32(Out, ID << 1);
2582 return;
2583 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002584
Douglas Gregor1d583f22009-04-28 21:18:29 +00002585 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002586 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2587 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2588 clang::io::Emit16(Out, Bits);
2589 Bits = 0;
2590 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
2591 bool HasMacroDefinition = HadMacroDefinition && II->hasMacroDefinition();
Douglas Gregord7910e92011-09-14 22:14:14 +00002592 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002593 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002594 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2595 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002596 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002597 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002598 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002599
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002600 if (HadMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002601 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002602 clang::io::Emit32(Out,
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002603 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2604 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002605
Douglas Gregora868bbd2009-04-21 22:25:48 +00002606 // Emit the declaration IDs in reverse order, because the
2607 // IdentifierResolver provides the declarations as they would be
2608 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002609 // "stat"), but the ASTReader adds declarations to the end of the list
2610 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002611 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002612 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2613 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002614 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002615 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002616 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002617 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002618 }
2619};
2620} // end anonymous namespace
2621
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002622/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002623///
2624/// The identifier table consists of a blob containing string data
2625/// (the actual identifiers themselves) and a separate "offsets" index
2626/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002627void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2628 IdentifierResolver &IdResolver,
2629 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002630 using namespace llvm;
2631
2632 // Create and write out the blob that contains the identifier
2633 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002634 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002635 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002636 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002637
Douglas Gregore6648fb2009-04-28 20:33:11 +00002638 // Look for any identifiers that were named while processing the
2639 // headers, but are otherwise not needed. We add these to the hash
2640 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002641 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002642 // file.
2643 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2644 IDEnd = PP.getIdentifierTable().end();
2645 ID != IDEnd; ++ID)
2646 getIdentifierRef(ID->second);
2647
Sebastian Redlff4a2952010-07-23 23:49:55 +00002648 // Create the on-disk hash table representation. We only store offsets
2649 // for identifiers that appear here for the first time.
2650 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002651 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002652 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2653 ID != IDEnd; ++ID) {
2654 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002655 if (!Chain || !ID->first->isFromAST() ||
2656 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002657 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2658 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002659 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002660
Douglas Gregore84a9da2009-04-20 20:36:09 +00002661 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002662 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002663 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002664 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002665 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002666 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002667 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002668 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002669 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002670 }
2671
2672 // Create a blob abbreviation
2673 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002674 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002675 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002677 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002678
2679 // Write the identifier table
2680 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002681 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002682 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002683 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002684 }
2685
2686 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002687 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002688 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002690 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002691 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2692 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2693
2694 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002695 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002696 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002697 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002698 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002699 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002700}
2701
Douglas Gregorc5046832009-04-27 18:38:38 +00002702//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002703// DeclContext's Name Lookup Table Serialization
2704//===----------------------------------------------------------------------===//
2705
2706namespace {
2707// Trait used for the on-disk hash table used in the method pool.
2708class ASTDeclContextNameLookupTrait {
2709 ASTWriter &Writer;
2710
2711public:
2712 typedef DeclarationName key_type;
2713 typedef key_type key_type_ref;
2714
2715 typedef DeclContext::lookup_result data_type;
2716 typedef const data_type& data_type_ref;
2717
2718 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2719
2720 unsigned ComputeHash(DeclarationName Name) {
2721 llvm::FoldingSetNodeID ID;
2722 ID.AddInteger(Name.getNameKind());
2723
2724 switch (Name.getNameKind()) {
2725 case DeclarationName::Identifier:
2726 ID.AddString(Name.getAsIdentifierInfo()->getName());
2727 break;
2728 case DeclarationName::ObjCZeroArgSelector:
2729 case DeclarationName::ObjCOneArgSelector:
2730 case DeclarationName::ObjCMultiArgSelector:
2731 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2732 break;
2733 case DeclarationName::CXXConstructorName:
2734 case DeclarationName::CXXDestructorName:
2735 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002736 break;
2737 case DeclarationName::CXXOperatorName:
2738 ID.AddInteger(Name.getCXXOverloadedOperator());
2739 break;
2740 case DeclarationName::CXXLiteralOperatorName:
2741 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2742 case DeclarationName::CXXUsingDirective:
2743 break;
2744 }
2745
2746 return ID.ComputeHash();
2747 }
2748
2749 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002750 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002751 data_type_ref Lookup) {
2752 unsigned KeyLen = 1;
2753 switch (Name.getNameKind()) {
2754 case DeclarationName::Identifier:
2755 case DeclarationName::ObjCZeroArgSelector:
2756 case DeclarationName::ObjCOneArgSelector:
2757 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002758 case DeclarationName::CXXLiteralOperatorName:
2759 KeyLen += 4;
2760 break;
2761 case DeclarationName::CXXOperatorName:
2762 KeyLen += 1;
2763 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002764 case DeclarationName::CXXConstructorName:
2765 case DeclarationName::CXXDestructorName:
2766 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002767 case DeclarationName::CXXUsingDirective:
2768 break;
2769 }
2770 clang::io::Emit16(Out, KeyLen);
2771
2772 // 2 bytes for num of decls and 4 for each DeclID.
2773 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2774 clang::io::Emit16(Out, DataLen);
2775
2776 return std::make_pair(KeyLen, DataLen);
2777 }
2778
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002779 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002780 using namespace clang::io;
2781
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002782 Emit8(Out, Name.getNameKind());
2783 switch (Name.getNameKind()) {
2784 case DeclarationName::Identifier:
2785 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002786 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002787 case DeclarationName::ObjCZeroArgSelector:
2788 case DeclarationName::ObjCOneArgSelector:
2789 case DeclarationName::ObjCMultiArgSelector:
2790 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002791 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002792 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002793 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2794 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002795 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00002796 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002797 case DeclarationName::CXXLiteralOperatorName:
2798 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002799 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002800 case DeclarationName::CXXConstructorName:
2801 case DeclarationName::CXXDestructorName:
2802 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002803 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002804 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002805 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00002806
2807 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002808 }
2809
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002810 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002811 data_type Lookup, unsigned DataLen) {
2812 uint64_t Start = Out.tell(); (void)Start;
2813 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2814 for (; Lookup.first != Lookup.second; ++Lookup.first)
2815 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2816
2817 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2818 }
2819};
2820} // end anonymous namespace
2821
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002822/// \brief Write the block containing all of the declaration IDs
2823/// visible from the given DeclContext.
2824///
2825/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002826/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002827uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2828 DeclContext *DC) {
2829 if (DC->getPrimaryContext() != DC)
2830 return 0;
2831
2832 // Since there is no name lookup into functions or methods, don't bother to
2833 // build a visible-declarations table for these entities.
2834 if (DC->isFunctionOrMethod())
2835 return 0;
2836
2837 // If not in C++, we perform name lookup for the translation unit via the
2838 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2839 // FIXME: In C++ we need the visible declarations in order to "see" the
2840 // friend declarations, is there a way to do this without writing the table ?
David Blaikiebbafb8a2012-03-11 07:00:24 +00002841 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002842 return 0;
2843
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002844 // Serialize the contents of the mapping used for lookup. Note that,
2845 // although we have two very different code paths, the serialized
2846 // representation is the same for both cases: a declaration name,
2847 // followed by a size, followed by references to the visible
2848 // declarations that have that name.
2849 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00002850 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002851 if (!Map || Map->empty())
2852 return 0;
2853
2854 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2855 ASTDeclContextNameLookupTrait Trait(*this);
2856
2857 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002858 DeclarationName ConversionName;
2859 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002860 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2861 D != DEnd; ++D) {
2862 DeclarationName Name = D->first;
2863 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002864 if (Result.first != Result.second) {
2865 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2866 // Hash all conversion function names to the same name. The actual
2867 // type information in conversion function name is not used in the
2868 // key (since such type information is not stable across different
2869 // modules), so the intended effect is to coalesce all of the conversion
2870 // functions under a single key.
2871 if (!ConversionName)
2872 ConversionName = Name;
2873 ConversionDecls.append(Result.first, Result.second);
2874 continue;
2875 }
2876
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002877 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002878 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002879 }
2880
Douglas Gregor05ef9312011-08-30 20:49:19 +00002881 // Add the conversion functions
2882 if (!ConversionDecls.empty()) {
2883 Generator.insert(ConversionName,
2884 DeclContext::lookup_result(ConversionDecls.begin(),
2885 ConversionDecls.end()),
2886 Trait);
2887 }
2888
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002889 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002890 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002891 uint32_t BucketOffset;
2892 {
2893 llvm::raw_svector_ostream Out(LookupTable);
2894 // Make sure that no bucket is at offset 0
2895 clang::io::Emit32(Out, 0);
2896 BucketOffset = Generator.Emit(Out, Trait);
2897 }
2898
2899 // Write the lookup table
2900 RecordData Record;
2901 Record.push_back(DECL_CONTEXT_VISIBLE);
2902 Record.push_back(BucketOffset);
2903 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2904 LookupTable.str());
2905
2906 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2907 ++NumVisibleDeclContexts;
2908 return Offset;
2909}
2910
Sebastian Redla4071b42010-08-24 00:50:09 +00002911/// \brief Write an UPDATE_VISIBLE block for the given context.
2912///
2913/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2914/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00002915/// (in C++), for namespaces, and for classes with forward-declared unscoped
2916/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00002917void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002918 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2919 if (!Map || Map->empty())
2920 return;
2921
2922 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2923 ASTDeclContextNameLookupTrait Trait(*this);
2924
2925 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002926 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2927 D != DEnd; ++D) {
2928 DeclarationName Name = D->first;
2929 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002930 // For any name that appears in this table, the results are complete, i.e.
2931 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002932 if (Result.first != Result.second)
2933 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002934 }
2935
2936 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002937 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00002938 uint32_t BucketOffset;
2939 {
2940 llvm::raw_svector_ostream Out(LookupTable);
2941 // Make sure that no bucket is at offset 0
2942 clang::io::Emit32(Out, 0);
2943 BucketOffset = Generator.Emit(Out, Trait);
2944 }
2945
2946 // Write the lookup table
2947 RecordData Record;
2948 Record.push_back(UPDATE_VISIBLE);
2949 Record.push_back(getDeclID(cast<Decl>(DC)));
2950 Record.push_back(BucketOffset);
2951 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2952}
2953
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002954/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2955void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2956 RecordData Record;
2957 Record.push_back(Opts.fp_contract);
2958 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2959}
2960
2961/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2962void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002963 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002964 return;
2965
2966 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2967 RecordData Record;
2968#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2969#include "clang/Basic/OpenCLExtensions.def"
2970 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2971}
2972
Douglas Gregor358cd442012-01-15 16:58:34 +00002973void ASTWriter::WriteRedeclarations() {
2974 RecordData LocalRedeclChains;
2975 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2976
2977 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2978 Decl *First = Redeclarations[I];
2979 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2980
2981 Decl *MostRecent = First->getMostRecentDecl();
2982
2983 // If we only have a single declaration, there is no point in storing
2984 // a redeclaration chain.
2985 if (First == MostRecent)
2986 continue;
2987
2988 unsigned Offset = LocalRedeclChains.size();
2989 unsigned Size = 0;
2990 LocalRedeclChains.push_back(0); // Placeholder for the size.
2991
2992 // Collect the set of local redeclarations of this declaration.
2993 for (Decl *Prev = MostRecent; Prev != First;
2994 Prev = Prev->getPreviousDecl()) {
2995 if (!Prev->isFromASTFile()) {
2996 AddDeclRef(Prev, LocalRedeclChains);
2997 ++Size;
2998 }
2999 }
3000 LocalRedeclChains[Offset] = Size;
3001
3002 // Reverse the set of local redeclarations, so that we store them in
3003 // order (since we found them in reverse order).
3004 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3005
3006 // Add the mapping from the first ID to the set of local declarations.
3007 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3008 LocalRedeclsMap.push_back(Info);
3009
3010 assert(N == Redeclarations.size() &&
3011 "Deserialized a declaration we shouldn't have");
3012 }
3013
3014 if (LocalRedeclChains.empty())
3015 return;
3016
3017 // Sort the local redeclarations map by the first declaration ID,
3018 // since the reader will be performing binary searches on this information.
3019 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3020
3021 // Emit the local redeclarations map.
3022 using namespace llvm;
3023 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3024 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3027 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3028
3029 RecordData Record;
3030 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3031 Record.push_back(LocalRedeclsMap.size());
3032 Stream.EmitRecordWithBlob(AbbrevID, Record,
3033 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3034 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3035
3036 // Emit the redeclaration chains.
3037 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3038}
3039
Douglas Gregor404cdde2012-01-27 01:47:08 +00003040void ASTWriter::WriteObjCCategories() {
3041 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3042 RecordData Categories;
3043
3044 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3045 unsigned Size = 0;
3046 unsigned StartIndex = Categories.size();
3047
3048 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3049
3050 // Allocate space for the size.
3051 Categories.push_back(0);
3052
3053 // Add the categories.
3054 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3055 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3056 assert(getDeclID(Cat) != 0 && "Bogus category");
3057 AddDeclRef(Cat, Categories);
3058 }
3059
3060 // Update the size.
3061 Categories[StartIndex] = Size;
3062
3063 // Record this interface -> category map.
3064 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3065 CategoriesMap.push_back(CatInfo);
3066 }
3067
3068 // Sort the categories map by the definition ID, since the reader will be
3069 // performing binary searches on this information.
3070 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3071
3072 // Emit the categories map.
3073 using namespace llvm;
3074 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3075 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3077 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3078 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3079
3080 RecordData Record;
3081 Record.push_back(OBJC_CATEGORIES_MAP);
3082 Record.push_back(CategoriesMap.size());
3083 Stream.EmitRecordWithBlob(AbbrevID, Record,
3084 reinterpret_cast<char*>(CategoriesMap.data()),
3085 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3086
3087 // Emit the category lists.
3088 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3089}
3090
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003091void ASTWriter::WriteMergedDecls() {
3092 if (!Chain || Chain->MergedDecls.empty())
3093 return;
3094
3095 RecordData Record;
3096 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3097 IEnd = Chain->MergedDecls.end();
3098 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003099 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003100 : getDeclID(I->first);
3101 assert(CanonID && "Merged declaration not known?");
3102
3103 Record.push_back(CanonID);
3104 Record.push_back(I->second.size());
3105 Record.append(I->second.begin(), I->second.end());
3106 }
3107 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3108}
3109
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003110//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003111// General Serialization Routines
3112//===----------------------------------------------------------------------===//
3113
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003114/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003115void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3116 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003117 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003118 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3119 e = Attrs.end(); i != e; ++i){
3120 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003121 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003122 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003123
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003124#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003125
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003126 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003127}
3128
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003129void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003130 Record.push_back(Str.size());
3131 Record.insert(Record.end(), Str.begin(), Str.end());
3132}
3133
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003134void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3135 RecordDataImpl &Record) {
3136 Record.push_back(Version.getMajor());
3137 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3138 Record.push_back(*Minor + 1);
3139 else
3140 Record.push_back(0);
3141 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3142 Record.push_back(*Subminor + 1);
3143 else
3144 Record.push_back(0);
3145}
3146
Douglas Gregore84a9da2009-04-20 20:36:09 +00003147/// \brief Note that the identifier II occurs at the given offset
3148/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003149void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003150 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003151 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003152 // up earlier in the chain and thus don't need an offset.
3153 if (ID >= FirstIdentID)
3154 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003155}
3156
Douglas Gregor95c13f52009-04-25 17:48:32 +00003157/// \brief Note that the selector Sel occurs at the given offset
3158/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003159void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003160 unsigned ID = SelectorIDs[Sel];
3161 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003162 // Don't record offsets for selectors that are also available in a different
3163 // file.
3164 if (ID < FirstSelectorID)
3165 return;
3166 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003167}
3168
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003169ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003170 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003171 WritingAST(false), DoneWritingDeclsAndTypes(false),
3172 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003173 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003174 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003175 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003176 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3177 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003178 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003179 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003180 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003181 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003182 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003183 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003184 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3185 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3186 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003187 DeclTypedefAbbrev(0),
3188 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3189 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003190{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003191}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003192
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003193ASTWriter::~ASTWriter() {
3194 for (FileDeclIDsTy::iterator
3195 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3196 delete I->second;
3197}
3198
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003199void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003200 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003201 Module *WritingModule, StringRef isysroot,
3202 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003203 WritingAST = true;
3204
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003205 ASTHasCompilerErrors = hasErrors;
3206
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003207 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003208 Stream.Emit((unsigned)'C', 8);
3209 Stream.Emit((unsigned)'P', 8);
3210 Stream.Emit((unsigned)'C', 8);
3211 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003212
Chris Lattner28fa4e62009-04-26 22:26:21 +00003213 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003214
Douglas Gregoreda8e122011-08-09 15:13:55 +00003215 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003216 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003217 this->WritingModule = WritingModule;
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003218 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003219 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003220 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003221 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003222
3223 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003224}
3225
Douglas Gregora94a1542011-07-27 21:45:57 +00003226template<typename Vector>
3227static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3228 ASTWriter::RecordData &Record) {
3229 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3230 I != E; ++I) {
3231 Writer.AddDeclRef(*I, Record);
3232 }
3233}
3234
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003235void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003236 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003237 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003238 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003239 using namespace llvm;
3240
Douglas Gregorcf68c582011-12-01 22:20:10 +00003241 // Make sure that the AST reader knows to finalize itself.
3242 if (Chain)
3243 Chain->finalizeForWriting();
3244
Sebastian Redl143413f2010-07-12 22:02:52 +00003245 ASTContext &Context = SemaRef.Context;
3246 Preprocessor &PP = SemaRef.PP;
3247
Douglas Gregordab42432011-08-12 00:15:20 +00003248 // Set up predefined declaration IDs.
3249 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003250 if (Context.ObjCIdDecl)
3251 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003252 if (Context.ObjCSelDecl)
3253 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003254 if (Context.ObjCClassDecl)
3255 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003256 if (Context.ObjCProtocolClassDecl)
3257 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003258 if (Context.Int128Decl)
3259 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3260 if (Context.UInt128Decl)
3261 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003262 if (Context.ObjCInstanceTypeDecl)
3263 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003264 if (Context.BuiltinVaListDecl)
3265 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3266
Douglas Gregor851443c2011-08-12 01:39:19 +00003267 if (!Chain) {
3268 // Make sure that we emit IdentifierInfos (and any attached
3269 // declarations) for builtins. We don't need to do this when we're
3270 // emitting chained PCH files, because all of the builtins will be
3271 // in the original PCH file.
3272 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003273 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003274 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003275 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003276 Context.getLangOpts().NoBuiltin);
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003277 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3278 getIdentifierRef(&Table.get(BuiltinNames[I]));
3279 }
3280
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003281 // If there are any out-of-date identifiers, bring them up to date.
3282 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3283 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3284 IDEnd = PP.getIdentifierTable().end();
3285 ID != IDEnd; ++ID)
3286 if (ID->second->isOutOfDate())
3287 ExtSource->updateOutOfDateIdentifier(*ID->second);
3288 }
3289
Chris Lattner0c797362009-09-08 18:19:27 +00003290 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003291 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003292 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003293 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003294 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003295
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003296 // Build a record containing all of the file scoped decls in this file.
3297 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003298 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3299 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003300
Douglas Gregor851443c2011-08-12 01:39:19 +00003301 // Build a record containing all of the delegating constructors we still need
3302 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003303 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003304 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003305
Douglas Gregor851443c2011-08-12 01:39:19 +00003306 // Write the set of weak, undeclared identifiers. We always write the
3307 // entire table, since later PCH files in a PCH chain are only interested in
3308 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003309 RecordData WeakUndeclaredIdentifiers;
3310 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003311 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003312 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3313 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3314 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3315 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3316 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3317 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3318 }
3319 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003320
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003321 // Build a record containing all of the locally-scoped external
3322 // declarations in this header file. Generally, this record will be
3323 // empty.
3324 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003325 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003326 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003327 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003328 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3329 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003330 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003331 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003332 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3333 }
3334
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003335 // Build a record containing all of the ext_vector declarations.
3336 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003337 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003338
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003339 // Build a record containing all of the VTable uses information.
3340 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003341 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003342 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3343 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3344 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3345 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3346 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003347 }
3348
3349 // Build a record containing all of dynamic classes declarations.
3350 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003351 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003352
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003353 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003354 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003355 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003356 I = SemaRef.PendingInstantiations.begin(),
3357 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3358 AddDeclRef(I->first, PendingInstantiations);
3359 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003360 }
3361 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3362 "There are local ones at end of translation unit!");
3363
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003364 // Build a record containing some declaration references.
3365 RecordData SemaDeclRefs;
3366 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3367 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3368 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3369 }
3370
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003371 RecordData CUDASpecialDeclRefs;
3372 if (Context.getcudaConfigureCallDecl()) {
3373 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3374 }
3375
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003376 // Build a record containing all of the known namespaces.
3377 RecordData KnownNamespaces;
3378 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3379 I = SemaRef.KnownNamespaces.begin(),
3380 IEnd = SemaRef.KnownNamespaces.end();
3381 I != IEnd; ++I) {
3382 if (!I->second)
3383 AddDeclRef(I->first, KnownNamespaces);
3384 }
3385
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003386 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003387 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003388 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003389 WriteMetadata(Context, isysroot, OutputFile);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003390 WriteLanguageOptions(Context.getLangOpts());
Douglas Gregorc567ba22011-07-22 16:35:34 +00003391 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00003392 WriteStatCache(*StatCalls);
Douglas Gregor851443c2011-08-12 01:39:19 +00003393
3394 // Create a lexical update block containing all of the declarations in the
3395 // translation unit that do not come from other AST files.
3396 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3397 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3398 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3399 E = TU->noload_decls_end();
3400 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003401 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003402 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003403 }
3404
3405 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3406 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3407 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3408 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3409 Record.clear();
3410 Record.push_back(TU_UPDATE_LEXICAL);
3411 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3412 data(NewGlobalDecls));
3413
3414 // And a visible updates block for the translation unit.
3415 Abv = new llvm::BitCodeAbbrev();
3416 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3417 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3418 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3419 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3420 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3421 WriteDeclContextVisibleUpdate(TU);
3422
3423 // If the translation unit has an anonymous namespace, and we don't already
3424 // have an update block for it, write it as an update block.
3425 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3426 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3427 if (Record.empty()) {
3428 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003429 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003430 }
3431 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003432
3433 // Make sure visible decls, added to DeclContexts previously loaded from
3434 // an AST file, are registered for serialization.
3435 for (SmallVector<const Decl *, 16>::iterator
3436 I = UpdatingVisibleDecls.begin(),
3437 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3438 GetDeclRef(*I);
3439 }
3440
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003441 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003442 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003443
Douglas Gregor5204bde2011-08-02 16:26:37 +00003444 // Form the record of special types.
3445 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00003446 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003447 AddTypeRef(Context.getFILEType(), SpecialTypes);
3448 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3449 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3450 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3451 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003452 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003453 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003454
Douglas Gregor1970d882009-04-26 03:49:13 +00003455 // Keep writing types and declarations until all types and
3456 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003457 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003458 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003459 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3460 E = DeclsToRewrite.end();
3461 I != E; ++I)
3462 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003463 while (!DeclTypesToEmit.empty()) {
3464 DeclOrType DOT = DeclTypesToEmit.front();
3465 DeclTypesToEmit.pop();
3466 if (DOT.isType())
3467 WriteType(DOT.getType());
3468 else
3469 WriteDecl(Context, DOT.getDecl());
3470 }
3471 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003472
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003473 DoneWritingDeclsAndTypes = true;
3474
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003475 WriteFileDeclIDsMap();
3476 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003477 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003478
3479 if (Chain) {
3480 // Write the mapping information describing our module dependencies and how
3481 // each of those modules were mapped into our own offset/ID space, so that
3482 // the reader can build the appropriate mapping to its own offset/ID space.
3483 // The map consists solely of a blob with the following format:
3484 // *(module-name-len:i16 module-name:len*i8
3485 // source-location-offset:i32
3486 // identifier-id:i32
3487 // preprocessed-entity-id:i32
3488 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003489 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003490 // selector-id:i32
3491 // declaration-id:i32
3492 // c++-base-specifiers-id:i32
3493 // type-id:i32)
3494 //
3495 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3496 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3497 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3498 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003499 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003500 {
3501 llvm::raw_svector_ostream Out(Buffer);
3502 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003503 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003504 M != MEnd; ++M) {
3505 StringRef FileName = (*M)->FileName;
3506 io::Emit16(Out, FileName.size());
3507 Out.write(FileName.data(), FileName.size());
3508 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3509 io::Emit32(Out, (*M)->BaseIdentifierID);
3510 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003511 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003512 io::Emit32(Out, (*M)->BaseSelectorID);
3513 io::Emit32(Out, (*M)->BaseDeclID);
3514 io::Emit32(Out, (*M)->BaseTypeIndex);
3515 }
3516 }
3517 Record.clear();
3518 Record.push_back(MODULE_OFFSET_MAP);
3519 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3520 Buffer.data(), Buffer.size());
3521 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003522 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003523 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003524 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003525 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003526 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003527 WriteFPPragmaOptions(SemaRef.getFPOptions());
3528 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003529
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003530 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003531 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003532
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003533 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003534
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003535 // If we're emitting a module, write out the submodule information.
3536 if (WritingModule)
3537 WriteSubmodules(WritingModule);
3538
Douglas Gregor5204bde2011-08-02 16:26:37 +00003539 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3540
Douglas Gregord4df8652009-04-22 22:02:47 +00003541 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003542 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003543 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003544
3545 // Write the record containing tentative definitions.
3546 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003547 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003548
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003549 // Write the record containing unused file scoped decls.
3550 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003551 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003552
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003553 // Write the record containing weak undeclared identifiers.
3554 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003555 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003556 WeakUndeclaredIdentifiers);
3557
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003558 // Write the record containing locally-scoped external definitions.
3559 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003560 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003561 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003562
3563 // Write the record containing ext_vector type names.
3564 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003565 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003566
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003567 // Write the record containing VTable uses information.
3568 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003569 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003570
3571 // Write the record containing dynamic classes declarations.
3572 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003573 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003574
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003575 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003576 if (!PendingInstantiations.empty())
3577 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003578
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003579 // Write the record containing declaration references of Sema.
3580 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003581 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003582
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003583 // Write the record containing CUDA-specific declaration references.
3584 if (!CUDASpecialDeclRefs.empty())
3585 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003586
3587 // Write the delegating constructors.
3588 if (!DelegatingCtorDecls.empty())
3589 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003590
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003591 // Write the known namespaces.
3592 if (!KnownNamespaces.empty())
3593 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3594
Douglas Gregor851443c2011-08-12 01:39:19 +00003595 // Write the visible updates to DeclContexts.
3596 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3597 I = UpdatedDeclContexts.begin(),
3598 E = UpdatedDeclContexts.end();
3599 I != E; ++I)
3600 WriteDeclContextVisibleUpdate(*I);
3601
Douglas Gregor959bb062011-12-03 01:15:29 +00003602 if (!WritingModule) {
3603 // Write the submodules that were imported, if any.
3604 RecordData ImportedModules;
3605 for (ASTContext::import_iterator I = Context.local_import_begin(),
3606 IEnd = Context.local_import_end();
3607 I != IEnd; ++I) {
3608 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3609 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3610 }
3611 if (!ImportedModules.empty()) {
3612 // Sort module IDs.
3613 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3614
3615 // Unique module IDs.
3616 ImportedModules.erase(std::unique(ImportedModules.begin(),
3617 ImportedModules.end()),
3618 ImportedModules.end());
3619
3620 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3621 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003622 }
3623
Douglas Gregordab42432011-08-12 00:15:20 +00003624 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003625 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003626 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003627 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003628 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003629
Douglas Gregor08f01292009-04-17 22:13:46 +00003630 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003631 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003632 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003633 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003634 Record.push_back(NumLexicalDeclContexts);
3635 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003636 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003637 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003638}
3639
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003640/// \brief Go through the declaration update blocks and resolve declaration
3641/// pointers into declaration IDs.
3642void ASTWriter::ResolveDeclUpdatesBlocks() {
3643 for (DeclUpdateMap::iterator
3644 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3645 const Decl *D = I->first;
3646 UpdateRecord &URec = I->second;
3647
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003648 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003649 continue; // The decl will be written completely
3650
3651 unsigned Idx = 0, N = URec.size();
3652 while (Idx < N) {
3653 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003654 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3655 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3656 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3657 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3658 ++Idx;
3659 break;
3660
3661 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3662 ++Idx;
3663 break;
3664 }
3665 }
3666 }
3667}
3668
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003669void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003670 if (DeclUpdates.empty())
3671 return;
3672
3673 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003674 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003675 for (DeclUpdateMap::iterator
3676 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3677 const Decl *D = I->first;
3678 UpdateRecord &URec = I->second;
3679
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003680 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003681 continue; // The decl will be written completely,no need to store updates.
3682
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003683 uint64_t Offset = Stream.GetCurrentBitNo();
3684 Stream.EmitRecord(DECL_UPDATES, URec);
3685
3686 OffsetsRecord.push_back(GetDeclRef(D));
3687 OffsetsRecord.push_back(Offset);
3688 }
3689 Stream.ExitBlock();
3690 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3691}
3692
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003693void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003694 if (ReplacedDecls.empty())
3695 return;
3696
3697 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003698 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003699 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003700 Record.push_back(I->ID);
3701 Record.push_back(I->Offset);
3702 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003703 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003704 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003705}
3706
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003707void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003708 Record.push_back(Loc.getRawEncoding());
3709}
3710
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003711void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003712 AddSourceLocation(Range.getBegin(), Record);
3713 AddSourceLocation(Range.getEnd(), Record);
3714}
3715
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003716void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003717 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003718 const uint64_t *Words = Value.getRawData();
3719 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003720}
3721
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003722void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003723 Record.push_back(Value.isUnsigned());
3724 AddAPInt(Value, Record);
3725}
3726
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003727void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003728 AddAPInt(Value.bitcastToAPInt(), Record);
3729}
3730
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003731void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003732 Record.push_back(getIdentifierRef(II));
3733}
3734
Sebastian Redl539c5062010-08-18 23:57:32 +00003735IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003736 if (II == 0)
3737 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003738
Sebastian Redl539c5062010-08-18 23:57:32 +00003739 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003740 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003741 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003742 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003743}
3744
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003745void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003746 Record.push_back(getSelectorRef(SelRef));
3747}
3748
Sebastian Redl539c5062010-08-18 23:57:32 +00003749SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003750 if (Sel.getAsOpaquePtr() == 0) {
3751 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003752 }
3753
Sebastian Redl539c5062010-08-18 23:57:32 +00003754 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003755 if (SID == 0 && Chain) {
3756 // This might trigger a ReadSelector callback, which will set the ID for
3757 // this selector.
3758 Chain->LoadSelector(Sel);
3759 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003760 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003761 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003762 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003763 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003764}
3765
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003766void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003767 AddDeclRef(Temp->getDestructor(), Record);
3768}
3769
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003770void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3771 CXXBaseSpecifier const *BasesEnd,
3772 RecordDataImpl &Record) {
3773 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3774 CXXBaseSpecifiersToWrite.push_back(
3775 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3776 Bases, BasesEnd));
3777 Record.push_back(NextCXXBaseSpecifiersID++);
3778}
3779
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003780void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003781 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003782 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003783 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003784 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003785 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003786 break;
3787 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003788 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003789 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003790 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003791 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003792 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003793 break;
3794 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003795 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003796 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003797 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003798 break;
John McCall0ad16662009-10-29 08:12:44 +00003799 case TemplateArgument::Null:
3800 case TemplateArgument::Integral:
3801 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00003802 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00003803 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00003804 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00003805 break;
3806 }
3807}
3808
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003809void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003810 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003811 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003812
3813 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3814 bool InfoHasSameExpr
3815 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3816 Record.push_back(InfoHasSameExpr);
3817 if (InfoHasSameExpr)
3818 return; // Avoid storing the same expr twice.
3819 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003820 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3821 Record);
3822}
3823
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003824void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3825 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003826 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003827 AddTypeRef(QualType(), Record);
3828 return;
3829 }
3830
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003831 AddTypeLoc(TInfo->getTypeLoc(), Record);
3832}
3833
3834void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3835 AddTypeRef(TL.getType(), Record);
3836
John McCall8f115c62009-10-16 21:56:05 +00003837 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003838 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003839 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003840}
3841
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003842void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003843 Record.push_back(GetOrCreateTypeID(T));
3844}
3845
Douglas Gregoreda8e122011-08-09 15:13:55 +00003846TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3847 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003848 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3849}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003850
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003851TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003852 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003853 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003854}
3855
3856TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3857 if (T.isNull())
3858 return TypeIdx();
3859 assert(!T.getLocalFastQualifiers());
3860
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003861 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003862 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003863 if (DoneWritingDeclsAndTypes) {
3864 assert(0 && "New type seen after serializing all the types to emit!");
3865 return TypeIdx();
3866 }
3867
Douglas Gregor1970d882009-04-26 03:49:13 +00003868 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003869 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003870 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003871 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003872 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003873 return Idx;
3874}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003875
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003876TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003877 if (T.isNull())
3878 return TypeIdx();
3879 assert(!T.getLocalFastQualifiers());
3880
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003881 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3882 assert(I != TypeIdxs.end() && "Type not emitted!");
3883 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003884}
3885
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003886void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003887 Record.push_back(GetDeclRef(D));
3888}
3889
Sebastian Redl539c5062010-08-18 23:57:32 +00003890DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003891 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3892
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003893 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003894 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003895 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00003896
3897 // If D comes from an AST file, its declaration ID is already known and
3898 // fixed.
3899 if (D->isFromASTFile())
3900 return D->getGlobalID();
3901
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003902 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003903 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003904 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003905 if (DoneWritingDeclsAndTypes) {
3906 assert(0 && "New decl seen after serializing all the decls to emit!");
3907 return 0;
3908 }
3909
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003910 // We haven't seen this declaration before. Give it a new ID and
3911 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003912 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003913 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003914 }
3915
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003916 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003917}
3918
Sebastian Redl539c5062010-08-18 23:57:32 +00003919DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003920 if (D == 0)
3921 return 0;
3922
Douglas Gregorb3163e52012-01-05 22:33:30 +00003923 // If D comes from an AST file, its declaration ID is already known and
3924 // fixed.
3925 if (D->isFromASTFile())
3926 return D->getGlobalID();
3927
Douglas Gregore84a9da2009-04-20 20:36:09 +00003928 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3929 return DeclIDs[D];
3930}
3931
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003932static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3933 std::pair<unsigned, serialization::DeclID> R) {
3934 return L.first < R.first;
3935}
3936
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003937void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003938 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003939 assert(D);
3940
3941 SourceLocation Loc = D->getLocation();
3942 if (Loc.isInvalid())
3943 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003944
3945 // We only keep track of the file-level declarations of each file.
3946 if (!D->getLexicalDeclContext()->isFileContext())
3947 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00003948 // FIXME: ParmVarDecls that are part of a function type of a parameter of
3949 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00003950 if (isa<ParmVarDecl>(D))
3951 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003952
3953 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003954 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003955 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003956 FileID FID;
3957 unsigned Offset;
3958 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003959 if (FID.isInvalid())
3960 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00003961 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003962
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00003963 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003964 if (!Info)
3965 Info = new DeclIDInFileInfo();
3966
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003967 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003968 LocDeclIDsTy &Decls = Info->DeclIDs;
3969
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003970 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003971 Decls.push_back(LocDecl);
3972 return;
3973 }
3974
3975 LocDeclIDsTy::iterator
3976 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3977
3978 Decls.insert(I, LocDecl);
3979}
3980
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003981void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003982 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003983 Record.push_back(Name.getNameKind());
3984 switch (Name.getNameKind()) {
3985 case DeclarationName::Identifier:
3986 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3987 break;
3988
3989 case DeclarationName::ObjCZeroArgSelector:
3990 case DeclarationName::ObjCOneArgSelector:
3991 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003992 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003993 break;
3994
3995 case DeclarationName::CXXConstructorName:
3996 case DeclarationName::CXXDestructorName:
3997 case DeclarationName::CXXConversionFunctionName:
3998 AddTypeRef(Name.getCXXNameType(), Record);
3999 break;
4000
4001 case DeclarationName::CXXOperatorName:
4002 Record.push_back(Name.getCXXOverloadedOperator());
4003 break;
4004
Alexis Hunt3d221f22009-11-29 07:34:05 +00004005 case DeclarationName::CXXLiteralOperatorName:
4006 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4007 break;
4008
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004009 case DeclarationName::CXXUsingDirective:
4010 // No extra data to emit
4011 break;
4012 }
4013}
Chris Lattnerca025db2010-05-07 21:43:38 +00004014
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004015void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004016 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004017 switch (Name.getNameKind()) {
4018 case DeclarationName::CXXConstructorName:
4019 case DeclarationName::CXXDestructorName:
4020 case DeclarationName::CXXConversionFunctionName:
4021 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4022 break;
4023
4024 case DeclarationName::CXXOperatorName:
4025 AddSourceLocation(
4026 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4027 Record);
4028 AddSourceLocation(
4029 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4030 Record);
4031 break;
4032
4033 case DeclarationName::CXXLiteralOperatorName:
4034 AddSourceLocation(
4035 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4036 Record);
4037 break;
4038
4039 case DeclarationName::Identifier:
4040 case DeclarationName::ObjCZeroArgSelector:
4041 case DeclarationName::ObjCOneArgSelector:
4042 case DeclarationName::ObjCMultiArgSelector:
4043 case DeclarationName::CXXUsingDirective:
4044 break;
4045 }
4046}
4047
4048void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004049 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004050 AddDeclarationName(NameInfo.getName(), Record);
4051 AddSourceLocation(NameInfo.getLoc(), Record);
4052 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4053}
4054
4055void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004056 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004057 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004058 Record.push_back(Info.NumTemplParamLists);
4059 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4060 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4061}
4062
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004063void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004064 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004065 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004066 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004067 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004068
4069 // Push each of the NNS's onto a stack for serialization in reverse order.
4070 while (NNS) {
4071 NestedNames.push_back(NNS);
4072 NNS = NNS->getPrefix();
4073 }
4074
4075 Record.push_back(NestedNames.size());
4076 while(!NestedNames.empty()) {
4077 NNS = NestedNames.pop_back_val();
4078 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4079 Record.push_back(Kind);
4080 switch (Kind) {
4081 case NestedNameSpecifier::Identifier:
4082 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4083 break;
4084
4085 case NestedNameSpecifier::Namespace:
4086 AddDeclRef(NNS->getAsNamespace(), Record);
4087 break;
4088
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004089 case NestedNameSpecifier::NamespaceAlias:
4090 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4091 break;
4092
Chris Lattnerca025db2010-05-07 21:43:38 +00004093 case NestedNameSpecifier::TypeSpec:
4094 case NestedNameSpecifier::TypeSpecWithTemplate:
4095 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4096 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4097 break;
4098
4099 case NestedNameSpecifier::Global:
4100 // Don't need to write an associated value.
4101 break;
4102 }
4103 }
4104}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004105
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004106void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4107 RecordDataImpl &Record) {
4108 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004109 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004110 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004111
4112 // Push each of the nested-name-specifiers's onto a stack for
4113 // serialization in reverse order.
4114 while (NNS) {
4115 NestedNames.push_back(NNS);
4116 NNS = NNS.getPrefix();
4117 }
4118
4119 Record.push_back(NestedNames.size());
4120 while(!NestedNames.empty()) {
4121 NNS = NestedNames.pop_back_val();
4122 NestedNameSpecifier::SpecifierKind Kind
4123 = NNS.getNestedNameSpecifier()->getKind();
4124 Record.push_back(Kind);
4125 switch (Kind) {
4126 case NestedNameSpecifier::Identifier:
4127 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4128 AddSourceRange(NNS.getLocalSourceRange(), Record);
4129 break;
4130
4131 case NestedNameSpecifier::Namespace:
4132 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4133 AddSourceRange(NNS.getLocalSourceRange(), Record);
4134 break;
4135
4136 case NestedNameSpecifier::NamespaceAlias:
4137 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4138 AddSourceRange(NNS.getLocalSourceRange(), Record);
4139 break;
4140
4141 case NestedNameSpecifier::TypeSpec:
4142 case NestedNameSpecifier::TypeSpecWithTemplate:
4143 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4144 AddTypeLoc(NNS.getTypeLoc(), Record);
4145 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4146 break;
4147
4148 case NestedNameSpecifier::Global:
4149 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4150 break;
4151 }
4152 }
4153}
4154
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004155void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004156 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004157 Record.push_back(Kind);
4158 switch (Kind) {
4159 case TemplateName::Template:
4160 AddDeclRef(Name.getAsTemplateDecl(), Record);
4161 break;
4162
4163 case TemplateName::OverloadedTemplate: {
4164 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4165 Record.push_back(OvT->size());
4166 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4167 I != E; ++I)
4168 AddDeclRef(*I, Record);
4169 break;
4170 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004171
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004172 case TemplateName::QualifiedTemplate: {
4173 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4174 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4175 Record.push_back(QualT->hasTemplateKeyword());
4176 AddDeclRef(QualT->getTemplateDecl(), Record);
4177 break;
4178 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004179
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004180 case TemplateName::DependentTemplate: {
4181 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4182 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4183 Record.push_back(DepT->isIdentifier());
4184 if (DepT->isIdentifier())
4185 AddIdentifierRef(DepT->getIdentifier(), Record);
4186 else
4187 Record.push_back(DepT->getOperator());
4188 break;
4189 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004190
4191 case TemplateName::SubstTemplateTemplateParm: {
4192 SubstTemplateTemplateParmStorage *subst
4193 = Name.getAsSubstTemplateTemplateParm();
4194 AddDeclRef(subst->getParameter(), Record);
4195 AddTemplateName(subst->getReplacement(), Record);
4196 break;
4197 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004198
4199 case TemplateName::SubstTemplateTemplateParmPack: {
4200 SubstTemplateTemplateParmPackStorage *SubstPack
4201 = Name.getAsSubstTemplateTemplateParmPack();
4202 AddDeclRef(SubstPack->getParameterPack(), Record);
4203 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4204 break;
4205 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004206 }
4207}
4208
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004209void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004210 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004211 Record.push_back(Arg.getKind());
4212 switch (Arg.getKind()) {
4213 case TemplateArgument::Null:
4214 break;
4215 case TemplateArgument::Type:
4216 AddTypeRef(Arg.getAsType(), Record);
4217 break;
4218 case TemplateArgument::Declaration:
4219 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004220 Record.push_back(Arg.isDeclForReferenceParam());
4221 break;
4222 case TemplateArgument::NullPtr:
4223 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004224 break;
4225 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004226 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004227 AddTypeRef(Arg.getIntegralType(), Record);
4228 break;
4229 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004230 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4231 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004232 case TemplateArgument::TemplateExpansion:
4233 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004234 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4235 Record.push_back(*NumExpansions + 1);
4236 else
4237 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004238 break;
4239 case TemplateArgument::Expression:
4240 AddStmt(Arg.getAsExpr());
4241 break;
4242 case TemplateArgument::Pack:
4243 Record.push_back(Arg.pack_size());
4244 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4245 I != E; ++I)
4246 AddTemplateArgument(*I, Record);
4247 break;
4248 }
4249}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004250
4251void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004252ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004253 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004254 assert(TemplateParams && "No TemplateParams!");
4255 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4256 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4257 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4258 Record.push_back(TemplateParams->size());
4259 for (TemplateParameterList::const_iterator
4260 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4261 P != PEnd; ++P)
4262 AddDeclRef(*P, Record);
4263}
4264
4265/// \brief Emit a template argument list.
4266void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004267ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004268 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004269 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004270 Record.push_back(TemplateArgs->size());
4271 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004272 AddTemplateArgument(TemplateArgs->get(i), Record);
4273}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004274
4275
4276void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004277ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004278 Record.push_back(Set.size());
4279 for (UnresolvedSetImpl::const_iterator
4280 I = Set.begin(), E = Set.end(); I != E; ++I) {
4281 AddDeclRef(I.getDecl(), Record);
4282 Record.push_back(I.getAccess());
4283 }
4284}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004285
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004286void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004287 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004288 Record.push_back(Base.isVirtual());
4289 Record.push_back(Base.isBaseOfClass());
4290 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004291 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004292 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004293 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004294 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4295 : SourceLocation(),
4296 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004297}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004298
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004299void ASTWriter::FlushCXXBaseSpecifiers() {
4300 RecordData Record;
4301 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4302 Record.clear();
4303
4304 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004305 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004306 if (Index == CXXBaseSpecifiersOffsets.size())
4307 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4308 else {
4309 if (Index > CXXBaseSpecifiersOffsets.size())
4310 CXXBaseSpecifiersOffsets.resize(Index + 1);
4311 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4312 }
4313
4314 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4315 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4316 Record.push_back(BEnd - B);
4317 for (; B != BEnd; ++B)
4318 AddCXXBaseSpecifier(*B, Record);
4319 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004320
4321 // Flush any expressions that were written as part of the base specifiers.
4322 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004323 }
4324
4325 CXXBaseSpecifiersToWrite.clear();
4326}
4327
Alexis Hunt1d792652011-01-08 20:30:50 +00004328void ASTWriter::AddCXXCtorInitializers(
4329 const CXXCtorInitializer * const *CtorInitializers,
4330 unsigned NumCtorInitializers,
4331 RecordDataImpl &Record) {
4332 Record.push_back(NumCtorInitializers);
4333 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4334 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004335
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004336 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004337 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004338 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004339 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004340 } else if (Init->isDelegatingInitializer()) {
4341 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004342 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004343 } else if (Init->isMemberInitializer()){
4344 Record.push_back(CTOR_INITIALIZER_MEMBER);
4345 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004346 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004347 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4348 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004349 }
Francois Pichetd583da02010-12-04 09:14:42 +00004350
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004351 AddSourceLocation(Init->getMemberLocation(), Record);
4352 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004353 AddSourceLocation(Init->getLParenLoc(), Record);
4354 AddSourceLocation(Init->getRParenLoc(), Record);
4355 Record.push_back(Init->isWritten());
4356 if (Init->isWritten()) {
4357 Record.push_back(Init->getSourceOrder());
4358 } else {
4359 Record.push_back(Init->getNumArrayIndices());
4360 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4361 AddDeclRef(Init->getArrayIndex(i), Record);
4362 }
4363 }
4364}
4365
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004366void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4367 assert(D->DefinitionData);
4368 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004369 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004370 Record.push_back(Data.UserDeclaredConstructor);
4371 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004372 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004373 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004374 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004375 Record.push_back(Data.UserDeclaredDestructor);
4376 Record.push_back(Data.Aggregate);
4377 Record.push_back(Data.PlainOldData);
4378 Record.push_back(Data.Empty);
4379 Record.push_back(Data.Polymorphic);
4380 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004381 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004382 Record.push_back(Data.HasNoNonEmptyBases);
4383 Record.push_back(Data.HasPrivateFields);
4384 Record.push_back(Data.HasProtectedFields);
4385 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004386 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004387 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00004388 Record.push_back(Data.HasInClassInitializer);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004389 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004390 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004391 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00004392 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004393 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004394 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004395 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004396 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004397 Record.push_back(Data.HasTrivialDestructor);
Richard Smith561fb152012-02-25 07:33:38 +00004398 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004399 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004400 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004401 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004402 Record.push_back(Data.DeclaredDefaultConstructor);
4403 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004404 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004405 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004406 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004407 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004408 Record.push_back(Data.FailedImplicitMoveConstructor);
4409 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004410 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004411
4412 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004413 if (Data.NumBases > 0)
4414 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4415 Record);
4416
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004417 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4418 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004419 if (Data.NumVBases > 0)
4420 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4421 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004422
4423 AddUnresolvedSet(Data.Conversions, Record);
4424 AddUnresolvedSet(Data.VisibleConversions, Record);
4425 // Data.Definition is the owning decl, no need to write it.
4426 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004427
4428 // Add lambda-specific data.
4429 if (Data.IsLambda) {
4430 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004431 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004432 Record.push_back(Lambda.NumCaptures);
4433 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004434 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004435 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00004436 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004437 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4438 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4439 AddSourceLocation(Capture.getLocation(), Record);
4440 Record.push_back(Capture.isImplicit());
4441 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4442 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4443 AddDeclRef(Var, Record);
4444 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4445 : SourceLocation(),
4446 Record);
4447 }
4448 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004449}
4450
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004451void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004452 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004453 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004454 assert(FirstDeclID == NextDeclID &&
4455 FirstTypeID == NextTypeID &&
4456 FirstIdentID == NextIdentID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004457 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004458 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004459 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004460
Sebastian Redl07a89a82010-07-30 00:29:29 +00004461 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004462
Douglas Gregordf0c1512011-08-18 04:12:04 +00004463 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4464 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4465 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004466 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004467 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004468 NextDeclID = FirstDeclID;
4469 NextTypeID = FirstTypeID;
4470 NextIdentID = FirstIdentID;
4471 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004472 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004473}
4474
Sebastian Redl539c5062010-08-18 23:57:32 +00004475void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004476 IdentifierIDs[II] = ID;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00004477 if (II->hadMacroDefinition())
Douglas Gregor68051a72011-02-11 00:26:14 +00004478 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004479}
4480
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004481void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004482 // Always take the highest-numbered type index. This copes with an interesting
4483 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004484 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004485 // keep the higher-numbered entry so that we can properly write it out to
4486 // the AST file.
4487 TypeIdx &StoredIdx = TypeIdxs[T];
4488 if (Idx.getIndex() >= StoredIdx.getIndex())
4489 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004490}
4491
Sebastian Redl539c5062010-08-18 23:57:32 +00004492void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004493 SelectorIDs[S] = ID;
4494}
Douglas Gregor91096292010-10-02 19:29:26 +00004495
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004496void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004497 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004498 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004499 MacroDefinitions[MD] = ID;
4500}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004501
Douglas Gregor0abc2622011-12-20 22:06:13 +00004502void ASTWriter::MacroVisible(IdentifierInfo *II) {
4503 DeserializedMacroNames.push_back(II);
4504}
4505
Douglas Gregore37a85a2011-12-02 17:30:13 +00004506void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4507 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4508 SubmoduleIDs[Mod] = ID;
4509}
4510
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004511void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004512 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004513 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004514 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4515 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004516 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004517 // A forward reference was mutated into a definition. Rewrite it.
4518 // FIXME: This happens during template instantiation, should we
4519 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004520 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004521 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004522 }
4523}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004524void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004525 assert(!WritingAST && "Already writing the AST!");
4526
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004527 // TU and namespaces are handled elsewhere.
4528 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4529 return;
4530
Douglas Gregorb3722e22011-09-09 23:01:35 +00004531 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004532 return; // Not a source decl added to a DeclContext from PCH.
4533
4534 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004535 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004536}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004537
4538void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004539 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004540 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004541 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004542 return; // Not a source member added to a class from PCH.
4543 if (!isa<CXXMethodDecl>(D))
4544 return; // We are interested in lazily declared implicit methods.
4545
4546 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004547 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004548 UpdateRecord &Record = DeclUpdates[RD];
4549 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004550 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004551}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004552
4553void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4554 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004555 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004556 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004557 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004558 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004559 return; // Not a source specialization added to a template from PCH.
4560
4561 UpdateRecord &Record = DeclUpdates[TD];
4562 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004563 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004564}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004565
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004566void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4567 const FunctionDecl *D) {
4568 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004569 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004570 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004571 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004572 return; // Not a source specialization added to a template from PCH.
4573
4574 UpdateRecord &Record = DeclUpdates[TD];
4575 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004576 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004577}
4578
Sebastian Redlab238a72011-04-24 16:28:06 +00004579void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004580 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004581 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004582 return; // Declaration not imported from PCH.
4583
4584 // Implicit decl from a PCH was defined.
4585 // FIXME: Should implicit definition be a separate FunctionDecl?
4586 RewriteDecl(D);
4587}
4588
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004589void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004590 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004591 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004592 return;
4593
4594 // Since the actual instantiation is delayed, this really means that we need
4595 // to update the instantiation location.
4596 UpdateRecord &Record = DeclUpdates[D];
4597 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4598 AddSourceLocation(
4599 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4600}
4601
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004602void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4603 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004604 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004605 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004606 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004607
4608 assert(IFD->getDefinition() && "Category on a class without a definition?");
4609 ObjCClassesWithCategories.insert(
4610 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004611}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004612
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004613
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004614void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4615 const ObjCPropertyDecl *OrigProp,
4616 const ObjCCategoryDecl *ClassExt) {
4617 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4618 if (!D)
4619 return;
4620
4621 assert(!WritingAST && "Already writing the AST!");
4622 if (!D->isFromASTFile())
4623 return; // Declaration not imported from PCH.
4624
4625 RewriteDecl(D);
4626}