blob: 4e66a2fabbbb2f5b97c54f200591801bcdbce1fb [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 Gregorcb177f12012-10-16 23:40:58 +000038#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000054
Sebastian Redl3df5a082010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056static StringRef data(const std::vector<T, Allocator> &v) {
57 if (v.empty()) return StringRef();
58 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000059 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000060}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000061
62template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000063static StringRef data(const SmallVectorImpl<T> &v) {
64 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000066}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000071
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000080
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
84 void VisitArrayType(const ArrayType *T);
85 void VisitFunctionType(const FunctionType *T);
86 void VisitTagType(const TagType *T);
87
88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
89#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000095 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096}
97
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101}
102
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106}
107
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111}
112
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000117}
118
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122}
123
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128}
129
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134}
135
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140}
141
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153}
154
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160}
161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176}
177
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181}
182
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184 VisitFunctionType(T);
185 Record.push_back(T->getNumArgs());
186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
187 Writer.AddTypeRef(T->getArgType(I), Record);
188 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000189 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000191 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000192 Record.push_back(T->getExceptionSpecType());
193 if (T->getExceptionSpecType() == EST_Dynamic) {
194 Record.push_back(T->getNumExceptions());
195 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
196 Writer.AddTypeRef(T->getExceptionType(I), Record);
197 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
198 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000199 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
200 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
201 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000202 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000204 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000205 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000206}
207
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000208void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000209 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000210 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000211}
John McCallb96ec562009-12-04 22:46:56 +0000212
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000215 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
216 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000217 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218}
219
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000220void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000221 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000222 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223}
224
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000225void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000227 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228}
229
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000230void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000232 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000233 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000234}
235
Alexis Hunte852b102011-05-24 22:41:36 +0000236void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
237 Writer.AddTypeRef(T->getBaseType(), Record);
238 Writer.AddTypeRef(T->getUnderlyingType(), Record);
239 Record.push_back(T->getUTTKind());
240 Code = TYPE_UNARY_TRANSFORM;
241}
242
Richard Smith30482bc2011-02-20 03:19:35 +0000243void ASTTypeWriter::VisitAutoType(const AutoType *T) {
244 Writer.AddTypeRef(T->getDeducedType(), Record);
245 Code = TYPE_AUTO;
246}
247
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000249 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000250 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000251 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000252 "Cannot serialize in the middle of a type definition");
253}
254
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000255void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000257 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258}
259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
John McCall81904512011-01-06 01:58:22 +0000265void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
266 Writer.AddTypeRef(T->getModifiedType(), Record);
267 Writer.AddTypeRef(T->getEquivalentType(), Record);
268 Record.push_back(T->getAttrKind());
269 Code = TYPE_ATTRIBUTED;
270}
271
Mike Stump11289f42009-09-09 15:08:12 +0000272void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000274 const SubstTemplateTypeParmType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000278}
279
280void
Douglas Gregorada4b792011-01-14 02:55:32 +0000281ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
282 const SubstTemplateTypeParmPackType *T) {
283 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
284 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
285 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
286}
287
288void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000289ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000290 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000291 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000292 Writer.AddTemplateName(T->getTemplateName(), Record);
293 Record.push_back(T->getNumArgs());
294 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
295 ArgI != ArgE; ++ArgI)
296 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000297 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
298 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000299 : T->getCanonicalTypeInternal(),
300 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000301 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000306 VisitArrayType(T);
307 Writer.AddStmt(T->getSizeExpr());
308 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000309 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000310}
311
312void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000313ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000314 const DependentSizedExtVectorType *T) {
315 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000316 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000317}
318
319void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000320ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000321 Record.push_back(T->getDepth());
322 Record.push_back(T->getIndex());
323 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000324 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000325 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000326}
327
328void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000329ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000330 Record.push_back(T->getKeyword());
331 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
332 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000333 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
334 : T->getCanonicalTypeInternal(),
335 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000336 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000337}
338
339void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000340ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000341 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000342 Record.push_back(T->getKeyword());
343 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
344 Writer.AddIdentifierRef(T->getIdentifier(), Record);
345 Record.push_back(T->getNumArgs());
346 for (DependentTemplateSpecializationType::iterator
347 I = T->begin(), E = T->end(); I != E; ++I)
348 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000349 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000350}
351
Douglas Gregord2fa7662010-12-20 02:24:11 +0000352void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
353 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000354 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
355 Record.push_back(*NumExpansions + 1);
356 else
357 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000358 Code = TYPE_PACK_EXPANSION;
359}
360
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000361void ASTTypeWriter::VisitParenType(const ParenType *T) {
362 Writer.AddTypeRef(T->getInnerType(), Record);
363 Code = TYPE_PAREN;
364}
365
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000367 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000368 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
369 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000370 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000371}
372
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000373void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000374 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000375 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000376 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000377}
378
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000379void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000380 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000381 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000382}
383
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000384void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000385 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000386 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000387 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000388 E = T->qual_end(); I != E; ++I)
389 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000390 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000391}
392
Steve Narofffb4330f2009-06-17 22:40:22 +0000393void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000394ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000395 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000396 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000397}
398
Eli Friedman0dfb8892011-10-06 23:00:33 +0000399void
400ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
401 Writer.AddTypeRef(T->getValueType(), Record);
402 Code = TYPE_ATOMIC;
403}
404
John McCall8f115c62009-10-16 21:56:05 +0000405namespace {
406
407class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000408 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000409 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000410
411public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000412 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000413 : Writer(Writer), Record(Record) { }
414
John McCall17001972009-10-18 01:05:36 +0000415#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000416#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000417 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000418#include "clang/AST/TypeLocNodes.def"
419
John McCall17001972009-10-18 01:05:36 +0000420 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
421 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000422};
423
424}
425
John McCall17001972009-10-18 01:05:36 +0000426void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
427 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000428}
John McCall17001972009-10-18 01:05:36 +0000429void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000430 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
431 if (TL.needsExtraLocalData()) {
432 Record.push_back(TL.getWrittenTypeSpec());
433 Record.push_back(TL.getWrittenSignSpec());
434 Record.push_back(TL.getWrittenWidthSpec());
435 Record.push_back(TL.hasModeAttr());
436 }
John McCall8f115c62009-10-16 21:56:05 +0000437}
John McCall17001972009-10-18 01:05:36 +0000438void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000440}
John McCall17001972009-10-18 01:05:36 +0000441void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000443}
John McCall17001972009-10-18 01:05:36 +0000444void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000446}
John McCall17001972009-10-18 01:05:36 +0000447void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000449}
John McCall17001972009-10-18 01:05:36 +0000450void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000452}
John McCall17001972009-10-18 01:05:36 +0000453void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000455 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000456}
John McCall17001972009-10-18 01:05:36 +0000457void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
459 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
460 Record.push_back(TL.getSizeExpr() ? 1 : 0);
461 if (TL.getSizeExpr())
462 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000463}
John McCall17001972009-10-18 01:05:36 +0000464void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
465 VisitArrayTypeLoc(TL);
466}
467void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
468 VisitArrayTypeLoc(TL);
469}
470void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
471 VisitArrayTypeLoc(TL);
472}
473void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
474 DependentSizedArrayTypeLoc TL) {
475 VisitArrayTypeLoc(TL);
476}
477void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
478 DependentSizedExtVectorTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
481void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
484void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
487void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000488 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000489 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
490 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000491 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall17001972009-10-18 01:05:36 +0000492 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
493 Writer.AddDeclRef(TL.getArg(i), Record);
494}
495void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
496 VisitFunctionTypeLoc(TL);
497}
498void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
499 VisitFunctionTypeLoc(TL);
500}
John McCallb96ec562009-12-04 22:46:56 +0000501void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
John McCall17001972009-10-18 01:05:36 +0000504void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
507void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000508 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000511}
512void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000513 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
514 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
515 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
516 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000517}
518void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
Alexis Hunte852b102011-05-24 22:41:36 +0000521void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getKWLoc(), Record);
523 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
524 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
525 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
526}
Richard Smith30482bc2011-02-20 03:19:35 +0000527void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
John McCall17001972009-10-18 01:05:36 +0000530void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
533void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
John McCall81904512011-01-06 01:58:22 +0000536void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
538 if (TL.hasAttrOperand()) {
539 SourceRange range = TL.getAttrOperandParensRange();
540 Writer.AddSourceLocation(range.getBegin(), Record);
541 Writer.AddSourceLocation(range.getEnd(), Record);
542 }
543 if (TL.hasAttrExprOperand()) {
544 Expr *operand = TL.getAttrExprOperand();
545 Record.push_back(operand ? 1 : 0);
546 if (operand) Writer.AddStmt(operand);
547 } else if (TL.hasAttrEnumOperand()) {
548 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
549 }
550}
John McCall17001972009-10-18 01:05:36 +0000551void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
553}
John McCallcebee162009-10-18 09:09:24 +0000554void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
555 SubstTemplateTypeParmTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557}
Douglas Gregorada4b792011-01-14 02:55:32 +0000558void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
559 SubstTemplateTypeParmPackTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561}
John McCall17001972009-10-18 01:05:36 +0000562void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
563 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000564 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000565 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
566 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
567 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
568 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000569 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
570 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000571}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000572void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
573 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
574 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
575}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000576void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000577 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000578 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000579}
John McCalle78aac42010-03-10 03:28:59 +0000580void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000583void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000584 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000585 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000586 Writer.AddSourceLocation(TL.getNameLoc(), Record);
587}
John McCallc392f372010-06-11 00:33:02 +0000588void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
589 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000590 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000591 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000592 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000594 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
595 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
596 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000597 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
598 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000599}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000600void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
601 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
602}
John McCall17001972009-10-18 01:05:36 +0000603void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000605}
606void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
607 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000608 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
609 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
610 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
611 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000612}
John McCallfc93cf92009-10-22 22:37:11 +0000613void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
614 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000615}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000616void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
617 Writer.AddSourceLocation(TL.getKWLoc(), Record);
618 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
619 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
620}
John McCall8f115c62009-10-16 21:56:05 +0000621
Chris Lattner19cea4e2009-04-22 05:57:30 +0000622//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000623// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000624//===----------------------------------------------------------------------===//
625
Chris Lattner28fa4e62009-04-26 22:26:21 +0000626static void EmitBlockID(unsigned ID, const char *Name,
627 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000628 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000629 Record.clear();
630 Record.push_back(ID);
631 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
632
633 // Emit the block name if present.
634 if (Name == 0 || Name[0] == 0) return;
635 Record.clear();
636 while (*Name)
637 Record.push_back(*Name++);
638 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
639}
640
641static void EmitRecordID(unsigned ID, const char *Name,
642 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000643 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000644 Record.clear();
645 Record.push_back(ID);
646 while (*Name)
647 Record.push_back(*Name++);
648 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000649}
650
651static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000652 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000653#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000654 RECORD(STMT_STOP);
655 RECORD(STMT_NULL_PTR);
656 RECORD(STMT_NULL);
657 RECORD(STMT_COMPOUND);
658 RECORD(STMT_CASE);
659 RECORD(STMT_DEFAULT);
660 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000661 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000662 RECORD(STMT_IF);
663 RECORD(STMT_SWITCH);
664 RECORD(STMT_WHILE);
665 RECORD(STMT_DO);
666 RECORD(STMT_FOR);
667 RECORD(STMT_GOTO);
668 RECORD(STMT_INDIRECT_GOTO);
669 RECORD(STMT_CONTINUE);
670 RECORD(STMT_BREAK);
671 RECORD(STMT_RETURN);
672 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000673 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000674 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000675 RECORD(EXPR_PREDEFINED);
676 RECORD(EXPR_DECL_REF);
677 RECORD(EXPR_INTEGER_LITERAL);
678 RECORD(EXPR_FLOATING_LITERAL);
679 RECORD(EXPR_IMAGINARY_LITERAL);
680 RECORD(EXPR_STRING_LITERAL);
681 RECORD(EXPR_CHARACTER_LITERAL);
682 RECORD(EXPR_PAREN);
683 RECORD(EXPR_UNARY_OPERATOR);
684 RECORD(EXPR_SIZEOF_ALIGN_OF);
685 RECORD(EXPR_ARRAY_SUBSCRIPT);
686 RECORD(EXPR_CALL);
687 RECORD(EXPR_MEMBER);
688 RECORD(EXPR_BINARY_OPERATOR);
689 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
690 RECORD(EXPR_CONDITIONAL_OPERATOR);
691 RECORD(EXPR_IMPLICIT_CAST);
692 RECORD(EXPR_CSTYLE_CAST);
693 RECORD(EXPR_COMPOUND_LITERAL);
694 RECORD(EXPR_EXT_VECTOR_ELEMENT);
695 RECORD(EXPR_INIT_LIST);
696 RECORD(EXPR_DESIGNATED_INIT);
697 RECORD(EXPR_IMPLICIT_VALUE_INIT);
698 RECORD(EXPR_VA_ARG);
699 RECORD(EXPR_ADDR_LABEL);
700 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000701 RECORD(EXPR_CHOOSE);
702 RECORD(EXPR_GNU_NULL);
703 RECORD(EXPR_SHUFFLE_VECTOR);
704 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000705 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000706 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000707 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000708 RECORD(EXPR_OBJC_ARRAY_LITERAL);
709 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000710 RECORD(EXPR_OBJC_ENCODE);
711 RECORD(EXPR_OBJC_SELECTOR_EXPR);
712 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
713 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
714 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
715 RECORD(EXPR_OBJC_KVC_REF_EXPR);
716 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000717 RECORD(STMT_OBJC_FOR_COLLECTION);
718 RECORD(STMT_OBJC_CATCH);
719 RECORD(STMT_OBJC_FINALLY);
720 RECORD(STMT_OBJC_AT_TRY);
721 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
722 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000723 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000724 RECORD(EXPR_CXX_OPERATOR_CALL);
725 RECORD(EXPR_CXX_CONSTRUCT);
726 RECORD(EXPR_CXX_STATIC_CAST);
727 RECORD(EXPR_CXX_DYNAMIC_CAST);
728 RECORD(EXPR_CXX_REINTERPRET_CAST);
729 RECORD(EXPR_CXX_CONST_CAST);
730 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000731 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000732 RECORD(EXPR_CXX_BOOL_LITERAL);
733 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000734 RECORD(EXPR_CXX_TYPEID_EXPR);
735 RECORD(EXPR_CXX_TYPEID_TYPE);
736 RECORD(EXPR_CXX_UUIDOF_EXPR);
737 RECORD(EXPR_CXX_UUIDOF_TYPE);
738 RECORD(EXPR_CXX_THIS);
739 RECORD(EXPR_CXX_THROW);
740 RECORD(EXPR_CXX_DEFAULT_ARG);
741 RECORD(EXPR_CXX_BIND_TEMPORARY);
742 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
743 RECORD(EXPR_CXX_NEW);
744 RECORD(EXPR_CXX_DELETE);
745 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
746 RECORD(EXPR_EXPR_WITH_CLEANUPS);
747 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
748 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
749 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
750 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
751 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
752 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
753 RECORD(EXPR_CXX_NOEXCEPT);
754 RECORD(EXPR_OPAQUE_VALUE);
755 RECORD(EXPR_BINARY_TYPE_TRAIT);
756 RECORD(EXPR_PACK_EXPANSION);
757 RECORD(EXPR_SIZEOF_PACK);
758 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000759 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000760#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000761}
Mike Stump11289f42009-09-09 15:08:12 +0000762
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000763void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000764 RecordData Record;
765 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000766
Sebastian Redl539c5062010-08-18 23:57:32 +0000767#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
768#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000769
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000770 // Control Block.
771 BLOCK(CONTROL_BLOCK);
772 RECORD(METADATA);
773 RECORD(IMPORTS);
774 RECORD(LANGUAGE_OPTIONS);
775 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000776 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000777 RECORD(ORIGINAL_PCH_DIR);
778
Douglas Gregor108cb222012-10-19 00:45:00 +0000779 BLOCK(INPUT_FILES_BLOCK);
780 RECORD(INPUT_FILE);
781
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000782 // AST Top-Level Block.
783 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000784 RECORD(TYPE_OFFSET);
785 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000786 RECORD(IDENTIFIER_OFFSET);
787 RECORD(IDENTIFIER_TABLE);
788 RECORD(EXTERNAL_DEFINITIONS);
789 RECORD(SPECIAL_TYPES);
790 RECORD(STATISTICS);
791 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000792 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000793 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
794 RECORD(SELECTOR_OFFSETS);
795 RECORD(METHOD_POOL);
796 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000797 RECORD(SOURCE_LOCATION_OFFSETS);
798 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000799 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000800 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000801 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000802 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000803 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000804 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000805 RECORD(SEMA_DECL_REFS);
806 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
807 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
808 RECORD(DECL_REPLACEMENTS);
809 RECORD(UPDATE_VISIBLE);
810 RECORD(DECL_UPDATE_OFFSETS);
811 RECORD(DECL_UPDATES);
812 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
813 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000814 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000815 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000816 RECORD(FP_PRAGMA_OPTIONS);
817 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000818 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000819 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000820 RECORD(MODULE_OFFSET_MAP);
821 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000822 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000823 RECORD(FILE_SORTED_DECLS);
824 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000825 RECORD(MERGED_DECLARATIONS);
826 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000827 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000828 RECORD(MACRO_OFFSET);
829 RECORD(MACRO_UPDATES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000830
Chris Lattner28fa4e62009-04-26 22:26:21 +0000831 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000832 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000833 RECORD(SM_SLOC_FILE_ENTRY);
834 RECORD(SM_SLOC_BUFFER_ENTRY);
835 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000836 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattner28fa4e62009-04-26 22:26:21 +0000838 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000839 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000840 RECORD(PP_MACRO_OBJECT_LIKE);
841 RECORD(PP_MACRO_FUNCTION_LIKE);
842 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000843
Douglas Gregor12bfa382009-10-17 00:13:19 +0000844 // Decls and Types block.
845 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000846 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000847 RECORD(TYPE_COMPLEX);
848 RECORD(TYPE_POINTER);
849 RECORD(TYPE_BLOCK_POINTER);
850 RECORD(TYPE_LVALUE_REFERENCE);
851 RECORD(TYPE_RVALUE_REFERENCE);
852 RECORD(TYPE_MEMBER_POINTER);
853 RECORD(TYPE_CONSTANT_ARRAY);
854 RECORD(TYPE_INCOMPLETE_ARRAY);
855 RECORD(TYPE_VARIABLE_ARRAY);
856 RECORD(TYPE_VECTOR);
857 RECORD(TYPE_EXT_VECTOR);
858 RECORD(TYPE_FUNCTION_PROTO);
859 RECORD(TYPE_FUNCTION_NO_PROTO);
860 RECORD(TYPE_TYPEDEF);
861 RECORD(TYPE_TYPEOF_EXPR);
862 RECORD(TYPE_TYPEOF);
863 RECORD(TYPE_RECORD);
864 RECORD(TYPE_ENUM);
865 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000866 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000867 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000868 RECORD(TYPE_DECLTYPE);
869 RECORD(TYPE_ELABORATED);
870 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
871 RECORD(TYPE_UNRESOLVED_USING);
872 RECORD(TYPE_INJECTED_CLASS_NAME);
873 RECORD(TYPE_OBJC_OBJECT);
874 RECORD(TYPE_TEMPLATE_TYPE_PARM);
875 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
876 RECORD(TYPE_DEPENDENT_NAME);
877 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
878 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
879 RECORD(TYPE_PAREN);
880 RECORD(TYPE_PACK_EXPANSION);
881 RECORD(TYPE_ATTRIBUTED);
882 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000883 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000884 RECORD(DECL_TYPEDEF);
885 RECORD(DECL_ENUM);
886 RECORD(DECL_RECORD);
887 RECORD(DECL_ENUM_CONSTANT);
888 RECORD(DECL_FUNCTION);
889 RECORD(DECL_OBJC_METHOD);
890 RECORD(DECL_OBJC_INTERFACE);
891 RECORD(DECL_OBJC_PROTOCOL);
892 RECORD(DECL_OBJC_IVAR);
893 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000894 RECORD(DECL_OBJC_CATEGORY);
895 RECORD(DECL_OBJC_CATEGORY_IMPL);
896 RECORD(DECL_OBJC_IMPLEMENTATION);
897 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
898 RECORD(DECL_OBJC_PROPERTY);
899 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000900 RECORD(DECL_FIELD);
901 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000902 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000903 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000904 RECORD(DECL_FILE_SCOPE_ASM);
905 RECORD(DECL_BLOCK);
906 RECORD(DECL_CONTEXT_LEXICAL);
907 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000908 RECORD(DECL_NAMESPACE);
909 RECORD(DECL_NAMESPACE_ALIAS);
910 RECORD(DECL_USING);
911 RECORD(DECL_USING_SHADOW);
912 RECORD(DECL_USING_DIRECTIVE);
913 RECORD(DECL_UNRESOLVED_USING_VALUE);
914 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
915 RECORD(DECL_LINKAGE_SPEC);
916 RECORD(DECL_CXX_RECORD);
917 RECORD(DECL_CXX_METHOD);
918 RECORD(DECL_CXX_CONSTRUCTOR);
919 RECORD(DECL_CXX_DESTRUCTOR);
920 RECORD(DECL_CXX_CONVERSION);
921 RECORD(DECL_ACCESS_SPEC);
922 RECORD(DECL_FRIEND);
923 RECORD(DECL_FRIEND_TEMPLATE);
924 RECORD(DECL_CLASS_TEMPLATE);
925 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
926 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
927 RECORD(DECL_FUNCTION_TEMPLATE);
928 RECORD(DECL_TEMPLATE_TYPE_PARM);
929 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
930 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
931 RECORD(DECL_STATIC_ASSERT);
932 RECORD(DECL_CXX_BASE_SPECIFIERS);
933 RECORD(DECL_INDIRECTFIELD);
934 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
935
Douglas Gregor03412ba2011-06-03 02:27:19 +0000936 // Statements and Exprs can occur in the Decls and Types block.
937 AddStmtsExprs(Stream, Record);
938
Douglas Gregor92a96f52011-02-08 21:58:10 +0000939 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000940 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000941 RECORD(PPD_MACRO_DEFINITION);
942 RECORD(PPD_INCLUSION_DIRECTIVE);
943
Chris Lattner28fa4e62009-04-26 22:26:21 +0000944#undef RECORD
945#undef BLOCK
946 Stream.ExitBlock();
947}
948
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000949/// \brief Adjusts the given filename to only write out the portion of the
950/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000951///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000952/// \param Filename the file name to adjust.
953///
954/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
955/// the returned filename will be adjusted by this system root.
956///
957/// \returns either the original filename (if it needs no adjustment) or the
958/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000959static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000960adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000961 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000962
Douglas Gregorc567ba22011-07-22 16:35:34 +0000963 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000964 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000965
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000966 // Verify that the filename and the system root have the same prefix.
967 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000968 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000969 if (Filename[Pos] != isysroot[Pos])
970 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000971
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000972 // We hit the end of the filename before we hit the end of the system root.
973 if (!Filename[Pos])
974 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000975
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000976 // If the file name has a '/' at the current position, skip over the '/'.
977 // We distinguish sysroot-based includes from absolute includes by the
978 // absence of '/' at the beginning of sysroot-based includes.
979 if (Filename[Pos] == '/')
980 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000981
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000982 return Filename + Pos;
983}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000984
Douglas Gregor112b9072012-10-18 05:31:06 +0000985/// \brief Write the control block.
986void ASTWriter::WriteControlBlock(ASTContext &Context, StringRef isysroot,
987 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000988 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000989 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
990 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +0000991
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000992 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000993 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
994 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
995 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
996 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
997 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
998 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
999 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1000 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1001 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1002 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1003 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001004 Record.push_back(VERSION_MAJOR);
1005 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001006 Record.push_back(CLANG_VERSION_MAJOR);
1007 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001008 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001009 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001010 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1011 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001012
Douglas Gregor112b9072012-10-18 05:31:06 +00001013 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001014 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001015 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1016 llvm::SmallVector<char, 128> ModulePaths;
1017 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001018
1019 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1020 M != MEnd; ++M) {
1021 // Skip modules that weren't directly imported.
1022 if (!(*M)->isDirectlyImported())
1023 continue;
1024
1025 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1026 // FIXME: Write import location, once it matters.
1027 // FIXME: This writes the absolute path for AST files we depend on.
1028 const std::string &FileName = (*M)->FileName;
1029 Record.push_back(FileName.size());
1030 Record.append(FileName.begin(), FileName.end());
1031 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001032 Stream.EmitRecord(IMPORTS, Record);
1033 }
Mike Stump11289f42009-09-09 15:08:12 +00001034
Douglas Gregor112b9072012-10-18 05:31:06 +00001035 // Language options.
1036 Record.clear();
1037 const LangOptions &LangOpts = Context.getLangOpts();
1038#define LANGOPT(Name, Bits, Default, Description) \
1039 Record.push_back(LangOpts.Name);
1040#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1041 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1042#include "clang/Basic/LangOptions.def"
1043
1044 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1045 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1046
1047 Record.push_back(LangOpts.CurrentModule.size());
1048 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1049 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1050
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001051 // Target options.
1052 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001053 const TargetInfo &Target = Context.getTargetInfo();
1054 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001055 AddString(TargetOpts.Triple, Record);
1056 AddString(TargetOpts.CPU, Record);
1057 AddString(TargetOpts.ABI, Record);
1058 AddString(TargetOpts.CXXABI, Record);
1059 AddString(TargetOpts.LinkerVersion, Record);
1060 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1061 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1062 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1063 }
1064 Record.push_back(TargetOpts.Features.size());
1065 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1066 AddString(TargetOpts.Features[I], Record);
1067 }
1068 Stream.EmitRecord(TARGET_OPTIONS, Record);
1069
Douglas Gregora3b20262011-05-06 21:43:30 +00001070 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001071 SourceManager &SM = Context.getSourceManager();
1072 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1073 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001074 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1075 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001076 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1077 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1078
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001079 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001080
Michael J. Spencer740857f2010-12-21 16:45:57 +00001081 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001082
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001083 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001084 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001085 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001086 RecordData Record;
Douglas Gregorfad10d82012-10-18 18:36:53 +00001087 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001088 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001089 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
1090 Record.clear();
Douglas Gregor45fe0362009-05-12 01:31:05 +00001091 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001092
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001093 // Original PCH directory
1094 if (!OutputFile.empty() && OutputFile != "-") {
1095 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1096 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1098 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1099
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001100 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001101
1102 llvm::sys::fs::make_absolute(OutputPath);
1103 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1104
1105 RecordData Record;
1106 Record.push_back(ORIGINAL_PCH_DIR);
1107 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1108 }
1109
Douglas Gregor72be3902012-10-19 00:38:02 +00001110 WriteInputFiles(Context.SourceMgr, isysroot);
1111 Stream.ExitBlock();
1112}
1113
1114void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1115 using namespace llvm;
1116 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1117 RecordData Record;
1118
1119 // Create input-file abbreviation.
1120 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1121 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1122 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1123 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1124 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1125 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1126
1127 // Write out all of the input files.
1128 std::vector<uint32_t> InputFileOffsets;
1129 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1130 // Get this source location entry.
1131 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1132 FileID FID = FileID::get(I);
1133 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
1134
1135 // We only care about file entries that were not overridden.
1136 if (!SLoc->isFile())
1137 continue;
1138 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1139 if (!Cache->OrigEntry || Cache->BufferOverridden)
1140 continue;
1141
1142 Record.clear();
1143 Record.push_back(INPUT_FILE);
1144
1145 // Emit size/modification time for this file.
1146 Record.push_back(Cache->OrigEntry->getSize());
1147 Record.push_back(Cache->OrigEntry->getModificationTime());
1148
1149 // Turn the file name into an absolute path, if it isn't already.
1150 const char *Filename = Cache->OrigEntry->getName();
1151 SmallString<128> FilePath(Filename);
1152
1153 // Ask the file manager to fixup the relative path for us. This will
1154 // honor the working directory.
1155 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1156
1157 // FIXME: This call to make_absolute shouldn't be necessary, the
1158 // call to FixupRelativePath should always return an absolute path.
1159 llvm::sys::fs::make_absolute(FilePath);
1160 Filename = FilePath.c_str();
1161
1162 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1163
1164 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1165 }
1166
Douglas Gregor112b9072012-10-18 05:31:06 +00001167 Stream.ExitBlock();
Douglas Gregor55abb232009-04-10 20:39:37 +00001168}
1169
Douglas Gregora7f71a92009-04-10 03:52:48 +00001170//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001171// stat cache Serialization
1172//===----------------------------------------------------------------------===//
1173
1174namespace {
1175// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001176class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001177public:
1178 typedef const char * key_type;
1179 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001180
Chris Lattner2a6fa472010-11-23 19:28:12 +00001181 typedef struct stat data_type;
1182 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001183
1184 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001185 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001186 }
Mike Stump11289f42009-09-09 15:08:12 +00001187
1188 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001189 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001190 data_type_ref Data) {
1191 unsigned StrLen = strlen(path);
1192 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001193 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001194 clang::io::Emit8(Out, DataLen);
1195 return std::make_pair(StrLen + 1, DataLen);
1196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001198 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001199 Out.write(path, KeyLen);
1200 }
Mike Stump11289f42009-09-09 15:08:12 +00001201
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001202 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001203 data_type_ref Data, unsigned DataLen) {
1204 using namespace clang::io;
1205 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001206
Chris Lattner2a6fa472010-11-23 19:28:12 +00001207 Emit32(Out, (uint32_t) Data.st_ino);
1208 Emit32(Out, (uint32_t) Data.st_dev);
1209 Emit16(Out, (uint16_t) Data.st_mode);
1210 Emit64(Out, (uint64_t) Data.st_mtime);
1211 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001212
1213 assert(Out.tell() - Start == DataLen && "Wrong data length");
1214 }
1215};
1216} // end anonymous namespace
1217
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001218/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001219void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001220 // Build the on-disk hash table containing information about every
1221 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001222 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001223 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001224 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001225 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001226 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001227 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001228 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Douglas Gregorc5046832009-04-27 18:38:38 +00001231 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001232 SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001233 uint32_t BucketOffset;
1234 {
1235 llvm::raw_svector_ostream Out(StatCacheData);
1236 // Make sure that no bucket is at offset 0
1237 clang::io::Emit32(Out, 0);
1238 BucketOffset = Generator.Emit(Out);
1239 }
1240
1241 // Create a blob abbreviation
1242 using namespace llvm;
1243 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001244 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1248 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1249
1250 // Write the stat cache
1251 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001252 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001253 Record.push_back(BucketOffset);
1254 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001255 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001256}
1257
1258//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001259// Source Manager Serialization
1260//===----------------------------------------------------------------------===//
1261
1262/// \brief Create an abbreviation for the SLocEntry that refers to a
1263/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001264static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001265 using namespace llvm;
1266 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001267 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001272 // FileEntry fields.
1273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor9dc32122011-11-16 20:05:18 +00001275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregora7f71a92009-04-10 03:52:48 +00001279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001280 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001281}
1282
1283/// \brief Create an abbreviation for the SLocEntry that refers to a
1284/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001285static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001286 using namespace llvm;
1287 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001288 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001294 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001295}
1296
1297/// \brief Create an abbreviation for the SLocEntry that refers to a
1298/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001299static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001300 using namespace llvm;
1301 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001302 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001304 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001305}
1306
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001307/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1308/// expansion.
1309static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001310 using namespace llvm;
1311 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001312 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001318 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001319}
1320
Douglas Gregor09b69892011-02-10 17:09:37 +00001321namespace {
1322 // Trait used for the on-disk hash table of header search information.
1323 class HeaderFileInfoTrait {
1324 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001325
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001326 // Keep track of the framework names we've used during serialization.
1327 SmallVector<char, 128> FrameworkStringData;
1328 llvm::StringMap<unsigned> FrameworkNameOffset;
1329
Douglas Gregor09b69892011-02-10 17:09:37 +00001330 public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001331 HeaderFileInfoTrait(ASTWriter &Writer)
1332 : Writer(Writer) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001333
1334 typedef const char *key_type;
1335 typedef key_type key_type_ref;
1336
1337 typedef HeaderFileInfo data_type;
1338 typedef const data_type &data_type_ref;
1339
1340 static unsigned ComputeHash(const char *path) {
1341 // The hash is based only on the filename portion of the key, so that the
1342 // reader can match based on filenames when symlinking or excess path
1343 // elements ("foo/../", "../") change the form of the name. However,
1344 // complete path is still the key.
1345 return llvm::HashString(llvm::sys::path::filename(path));
1346 }
1347
1348 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001349 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001350 data_type_ref Data) {
1351 unsigned StrLen = strlen(path);
1352 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001353 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001354 clang::io::Emit8(Out, DataLen);
1355 return std::make_pair(StrLen + 1, DataLen);
1356 }
1357
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001358 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001359 Out.write(path, KeyLen);
1360 }
1361
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001362 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001363 data_type_ref Data, unsigned DataLen) {
1364 using namespace clang::io;
1365 uint64_t Start = Out.tell(); (void)Start;
1366
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001367 unsigned char Flags = (Data.isImport << 5)
1368 | (Data.isPragmaOnce << 4)
1369 | (Data.DirInfo << 2)
1370 | (Data.Resolved << 1)
1371 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001372 Emit8(Out, (uint8_t)Flags);
1373 Emit16(Out, (uint16_t) Data.NumIncludes);
1374
1375 if (!Data.ControllingMacro)
1376 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1377 else
1378 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001379
1380 unsigned Offset = 0;
1381 if (!Data.Framework.empty()) {
1382 // If this header refers into a framework, save the framework name.
1383 llvm::StringMap<unsigned>::iterator Pos
1384 = FrameworkNameOffset.find(Data.Framework);
1385 if (Pos == FrameworkNameOffset.end()) {
1386 Offset = FrameworkStringData.size() + 1;
1387 FrameworkStringData.append(Data.Framework.begin(),
1388 Data.Framework.end());
1389 FrameworkStringData.push_back(0);
1390
1391 FrameworkNameOffset[Data.Framework] = Offset;
1392 } else
1393 Offset = Pos->second;
1394 }
1395 Emit32(Out, Offset);
1396
Douglas Gregor09b69892011-02-10 17:09:37 +00001397 assert(Out.tell() - Start == DataLen && "Wrong data length");
1398 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001399
1400 const char *strings_begin() const { return FrameworkStringData.begin(); }
1401 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001402 };
1403} // end anonymous namespace
1404
1405/// \brief Write the header search block for the list of files that
1406///
1407/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001408void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001409 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001410 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1411
1412 if (FilesByUID.size() > HS.header_file_size())
1413 FilesByUID.resize(HS.header_file_size());
1414
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001415 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregor09b69892011-02-10 17:09:37 +00001416 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001417 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001418 unsigned NumHeaderSearchEntries = 0;
1419 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1420 const FileEntry *File = FilesByUID[UID];
1421 if (!File)
1422 continue;
1423
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001424 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1425 // from the external source if it was not provided already.
1426 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001427 if (HFI.External && Chain)
1428 continue;
1429
1430 // Turn the file name into an absolute path, if it isn't already.
1431 const char *Filename = File->getName();
1432 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1433
1434 // If we performed any translation on the file name at all, we need to
1435 // save this string, since the generator will refer to it later.
1436 if (Filename != File->getName()) {
1437 Filename = strdup(Filename);
1438 SavedStrings.push_back(Filename);
1439 }
1440
1441 Generator.insert(Filename, HFI, GeneratorTrait);
1442 ++NumHeaderSearchEntries;
1443 }
1444
1445 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001446 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001447 uint32_t BucketOffset;
1448 {
1449 llvm::raw_svector_ostream Out(TableData);
1450 // Make sure that no bucket is at offset 0
1451 clang::io::Emit32(Out, 0);
1452 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1453 }
1454
1455 // Create a blob abbreviation
1456 using namespace llvm;
1457 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1458 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1463 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1464
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001465 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001466 RecordData Record;
1467 Record.push_back(HEADER_SEARCH_TABLE);
1468 Record.push_back(BucketOffset);
1469 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001470 Record.push_back(TableData.size());
1471 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001472 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1473
1474 // Free all of the strings we had to duplicate.
1475 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1476 free((void*)SavedStrings[I]);
1477}
1478
Douglas Gregora7f71a92009-04-10 03:52:48 +00001479/// \brief Writes the block containing the serialized form of the
1480/// source manager.
1481///
1482/// TODO: We should probably use an on-disk hash table (stored in a
1483/// blob), indexed based on the file name, so that we only create
1484/// entries for files that we actually need. In the common case (no
1485/// errors), we probably won't have to create file entries for any of
1486/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001487void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001488 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001489 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001490 RecordData Record;
1491
Chris Lattner0910e3b2009-04-10 17:16:57 +00001492 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001493 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001494
1495 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001496 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1497 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1498 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001499 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001500
Douglas Gregor258ae542009-04-27 06:38:32 +00001501 // Write out the source location entry table. We skip the first
1502 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001503 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001504 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001505 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1506 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001507 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001508 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001509 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001510 FileID FID = FileID::get(I);
1511 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001512
Douglas Gregor258ae542009-04-27 06:38:32 +00001513 // Record the offset of this source-location entry.
1514 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1515
1516 // Figure out which record code to use.
1517 unsigned Code;
1518 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001519 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1520 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001521 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001522 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001523 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001524 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001525 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001526 Record.clear();
1527 Record.push_back(Code);
1528
Douglas Gregor925296b2011-07-19 16:10:42 +00001529 // Starting offset of this entry within this module, so skip the dummy.
1530 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001531 if (SLoc->isFile()) {
1532 const SrcMgr::FileInfo &File = SLoc->getFile();
1533 Record.push_back(File.getIncludeLoc().getRawEncoding());
1534 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1535 Record.push_back(File.hasLineDirectives());
1536
1537 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001538 if (Content->OrigEntry) {
1539 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001540 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001541
Douglas Gregor258ae542009-04-27 06:38:32 +00001542 // The source location entry is a file. The blob associated
1543 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001544
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001545 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001546 Record.push_back(Content->OrigEntry->getSize());
1547 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor9dc32122011-11-16 20:05:18 +00001548 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001549 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001550
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001551 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001552 if (FDI != FileDeclIDs.end()) {
1553 Record.push_back(FDI->second->FirstDeclIndex);
1554 Record.push_back(FDI->second->DeclIDs.size());
1555 } else {
1556 Record.push_back(0);
1557 Record.push_back(0);
1558 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001559
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001560 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001561 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001562 SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001563
1564 // Ask the file manager to fixup the relative path for us. This will
1565 // honor the working directory.
1566 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1567
1568 // FIXME: This call to make_absolute shouldn't be necessary, the
1569 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001570 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001571 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001572
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001573 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001574 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001575
1576 if (Content->BufferOverridden) {
1577 Record.clear();
1578 Record.push_back(SM_SLOC_BUFFER_BLOB);
1579 const llvm::MemoryBuffer *Buffer
1580 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1581 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1582 StringRef(Buffer->getBufferStart(),
1583 Buffer->getBufferSize() + 1));
1584 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001585 } else {
1586 // The source location entry is a buffer. The blob associated
1587 // with this entry contains the contents of the buffer.
1588
1589 // We add one to the size so that we capture the trailing NULL
1590 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1591 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001592 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001593 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001594 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001595 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001596 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001597 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001598 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001599 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001600 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001601 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001602
Douglas Gregor925296b2011-07-19 16:10:42 +00001603 if (strcmp(Name, "<built-in>") == 0) {
1604 PreloadSLocs.push_back(SLocEntryOffsets.size());
1605 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001606 }
1607 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001608 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001609 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001610 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1611 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001612 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1613 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001614
1615 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001616 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001617 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001618 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001619 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001620 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001621 }
1622 }
1623
Douglas Gregor8f45df52009-04-16 22:23:12 +00001624 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001625
1626 if (SLocEntryOffsets.empty())
1627 return;
1628
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001629 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001630 // table is used for lazily loading source-location information.
1631 using namespace llvm;
1632 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001633 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001636 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1637 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001638
Douglas Gregor258ae542009-04-27 06:38:32 +00001639 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001640 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001641 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001642 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001643 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001644
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001645 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001646 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001647 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001648
1649 // Write the line table. It depends on remapping working, so it must come
1650 // after the source location offsets.
1651 if (SourceMgr.hasLineTable()) {
1652 LineTableInfo &LineTable = SourceMgr.getLineTable();
1653
1654 Record.clear();
1655 // Emit the file names
1656 Record.push_back(LineTable.getNumFilenames());
1657 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1658 // Emit the file name
1659 const char *Filename = LineTable.getFilename(I);
1660 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1661 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1662 Record.push_back(FilenameLen);
1663 if (FilenameLen)
1664 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1665 }
1666
1667 // Emit the line entries
1668 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1669 L != LEnd; ++L) {
1670 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001671 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001672 continue;
1673
1674 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001675 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001676
1677 // Emit the line entries
1678 Record.push_back(L->second.size());
1679 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1680 LEEnd = L->second.end();
1681 LE != LEEnd; ++LE) {
1682 Record.push_back(LE->FileOffset);
1683 Record.push_back(LE->LineNo);
1684 Record.push_back(LE->FilenameID);
1685 Record.push_back((unsigned)LE->FileKind);
1686 Record.push_back(LE->IncludeOffset);
1687 }
1688 }
1689 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1690 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001691}
1692
Douglas Gregorc5046832009-04-27 18:38:38 +00001693//===----------------------------------------------------------------------===//
1694// Preprocessor Serialization
1695//===----------------------------------------------------------------------===//
1696
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001697static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1698 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1699 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1700 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1701 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1702 return X.first->getName().compare(Y.first->getName());
1703}
1704
Chris Lattnereeffaef2009-04-10 17:15:23 +00001705/// \brief Writes the block containing the serialized form of the
1706/// preprocessor.
1707///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001708void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001709 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1710 if (PPRec)
1711 WritePreprocessorDetail(*PPRec);
1712
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001713 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001714
Chris Lattner0af3ba12009-04-13 01:29:17 +00001715 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1716 if (PP.getCounterValue() != 0) {
1717 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001718 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001719 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001720 }
1721
1722 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001723 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001724
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001725 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001726 // FIXME: use diagnostics subsystem for localization etc.
1727 if (PP.SawDateOrTime())
1728 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001729
Douglas Gregor796d76a2010-10-20 22:00:55 +00001730
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001731 // Loop over all the macro definitions that are live at the end of the file,
1732 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001733
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001734 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001735 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001736 MacrosToEmit;
1737 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001738 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor68051a72011-02-11 00:26:14 +00001739 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001740 I != E; ++I) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001741 if (!IsModule || I->second->isPublic()) {
1742 MacroDefinitionsSeen.insert(I->first);
1743 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001744 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001745 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001746
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001747 // Sort the set of macro definitions that need to be serialized by the
1748 // name of the macro, to provide a stable ordering.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001749 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001750 &compareMacroDefinitions);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001751
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001752 /// \brief Offsets of each of the macros into the bitstream, indexed by
1753 /// the local macro ID
1754 ///
1755 /// For each identifier that is associated with a macro, this map
1756 /// provides the offset into the bitstream where that macro is
1757 /// defined.
1758 std::vector<uint32_t> MacroOffsets;
1759
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001760 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1761 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001762
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001763 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1764 MI = MI->getPreviousDefinition()) {
1765 MacroID ID = getMacroRef(MI);
1766 if (!ID)
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001767 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001768
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001769 // Skip macros from a AST file if we're chaining.
1770 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1771 continue;
1772
1773 if (ID < FirstMacroID) {
1774 // This will have been dealt with via an update record.
1775 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1776 continue;
1777 }
1778
1779 // Record the local offset of this macro.
1780 unsigned Index = ID - FirstMacroID;
1781 if (Index == MacroOffsets.size())
1782 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1783 else {
1784 if (Index > MacroOffsets.size())
1785 MacroOffsets.resize(Index + 1);
1786
1787 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1788 }
1789
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001790 AddIdentifierRef(Name, Record);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001791 addMacroRef(MI, Record);
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001792 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001793 AddSourceLocation(MI->getDefinitionLoc(), Record);
1794 AddSourceLocation(MI->getUndefLoc(), Record);
1795 Record.push_back(MI->isUsed());
1796 Record.push_back(MI->isPublic());
1797 AddSourceLocation(MI->getVisibilityLocation(), Record);
1798 unsigned Code;
1799 if (MI->isObjectLike()) {
1800 Code = PP_MACRO_OBJECT_LIKE;
1801 } else {
1802 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001803
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001804 Record.push_back(MI->isC99Varargs());
1805 Record.push_back(MI->isGNUVarargs());
1806 Record.push_back(MI->getNumArgs());
1807 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1808 I != E; ++I)
1809 AddIdentifierRef(*I, Record);
1810 }
Mike Stump11289f42009-09-09 15:08:12 +00001811
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001812 // If we have a detailed preprocessing record, record the macro definition
1813 // ID that corresponds to this macro.
1814 if (PPRec)
1815 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1816
1817 Stream.EmitRecord(Code, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001818 Record.clear();
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001819
1820 // Emit the tokens array.
1821 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1822 // Note that we know that the preprocessor does not have any annotation
1823 // tokens in it because they are created by the parser, and thus can't
1824 // be in a macro definition.
1825 const Token &Tok = MI->getReplacementToken(TokNo);
1826
1827 Record.push_back(Tok.getLocation().getRawEncoding());
1828 Record.push_back(Tok.getLength());
1829
1830 // FIXME: When reading literal tokens, reconstruct the literal pointer
1831 // if it is needed.
1832 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1833 // FIXME: Should translate token kind to a stable encoding.
1834 Record.push_back(Tok.getKind());
1835 // FIXME: Should translate token flags to a stable encoding.
1836 Record.push_back(Tok.getFlags());
1837
1838 Stream.EmitRecord(PP_TOKEN, Record);
1839 Record.clear();
1840 }
1841 ++NumMacros;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001842 }
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001843 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001844 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001845
1846 // Write the offsets table for macro IDs.
1847 using namespace llvm;
1848 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1849 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1852 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1853
1854 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1855 Record.clear();
1856 Record.push_back(MACRO_OFFSET);
1857 Record.push_back(MacroOffsets.size());
1858 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1859 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1860 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001861}
1862
1863void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001864 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001865 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001866
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001867 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001868
Douglas Gregor92a96f52011-02-08 21:58:10 +00001869 // Enter the preprocessor block.
1870 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001871
Douglas Gregoraae92242010-03-19 21:51:54 +00001872 // If the preprocessor has a preprocessing record, emit it.
1873 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001874 using namespace llvm;
1875
1876 // Set up the abbreviation for
1877 unsigned InclusionAbbrev = 0;
1878 {
1879 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1880 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00001885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1886 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1887 }
1888
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001889 unsigned FirstPreprocessorEntityID
1890 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1891 + NUM_PREDEF_PP_ENTITY_IDS;
1892 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001893 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001894 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1895 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001896 E != EEnd;
1897 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001898 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001899
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001900 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1901 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001902
Douglas Gregor92a96f52011-02-08 21:58:10 +00001903 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001904 // Record this macro definition's ID.
1905 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001906
Douglas Gregor92a96f52011-02-08 21:58:10 +00001907 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001908 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1909 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001910 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001911
Chandler Carrutha88a22182011-07-14 08:20:46 +00001912 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001913 Record.push_back(ME->isBuiltinMacro());
1914 if (ME->isBuiltinMacro())
1915 AddIdentifierRef(ME->getName(), Record);
1916 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001917 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001918 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001919 continue;
1920 }
1921
1922 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1923 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001924 Record.push_back(ID->getFileName().size());
1925 Record.push_back(ID->wasInQuotes());
1926 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001927 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001928 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001929 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00001930 // Check that the FileEntry is not null because it was not resolved and
1931 // we create a PCH even with compiler errors.
1932 if (ID->getFile())
1933 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001934 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1935 continue;
1936 }
1937
1938 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1939 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001940 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001941
Douglas Gregoraae92242010-03-19 21:51:54 +00001942 // Write the offsets table for the preprocessing record.
1943 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001944 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1945
Douglas Gregoraae92242010-03-19 21:51:54 +00001946 // Write the offsets table for identifier IDs.
1947 using namespace llvm;
1948 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001949 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001952 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001953
Douglas Gregoraae92242010-03-19 21:51:54 +00001954 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001955 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001956 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001957 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1958 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001959 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001960}
1961
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001962unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1963 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1964 if (Known != SubmoduleIDs.end())
1965 return Known->second;
1966
1967 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1968}
1969
Douglas Gregor253eefe2011-12-01 00:59:36 +00001970/// \brief Compute the number of modules within the given tree (including the
1971/// given module).
1972static unsigned getNumberOfModules(Module *Mod) {
1973 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00001974 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1975 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00001976 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00001977 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00001978
1979 return ChildModules + 1;
1980}
1981
Douglas Gregorde3ef502011-11-30 23:21:26 +00001982void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00001983 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001984 // FIXME: This feels like it belongs somewhere else, but there are no
1985 // other consumers of this information.
1986 SourceManager &SrcMgr = PP->getSourceManager();
1987 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1988 for (ASTContext::import_iterator I = Context->local_import_begin(),
1989 IEnd = Context->local_import_end();
1990 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001991 if (Module *ImportedFrom
1992 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1993 SrcMgr))) {
1994 ImportedFrom->Imports.push_back(I->getImportedModule());
1995 }
1996 }
1997
Douglas Gregor69021972011-11-30 17:33:56 +00001998 // Enter the submodule description block.
1999 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2000
2001 // Write the abbreviations needed for the submodules block.
2002 using namespace llvm;
2003 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2004 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00002013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2014 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2015
2016 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002017 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2019 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2020
2021 Abbrev = new BitCodeAbbrev();
2022 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2024 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002025
2026 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002027 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2029 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2030
2031 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002032 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2034 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2035
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002036 Abbrev = new BitCodeAbbrev();
2037 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2039 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2040
Douglas Gregor59527662012-10-15 06:28:11 +00002041 Abbrev = new BitCodeAbbrev();
2042 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2044 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2045
Douglas Gregor253eefe2011-12-01 00:59:36 +00002046 // Write the submodule metadata block.
2047 RecordData Record;
2048 Record.push_back(getNumberOfModules(WritingModule));
2049 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2050 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2051
Douglas Gregor69021972011-11-30 17:33:56 +00002052 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002053 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002054 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002055 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002056 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002057 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002058 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002059
2060 // Emit the definition of the block.
2061 Record.clear();
2062 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002063 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002064 if (Mod->Parent) {
2065 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2066 Record.push_back(SubmoduleIDs[Mod->Parent]);
2067 } else {
2068 Record.push_back(0);
2069 }
2070 Record.push_back(Mod->IsFramework);
2071 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002072 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002073 Record.push_back(Mod->InferSubmodules);
2074 Record.push_back(Mod->InferExplicitSubmodules);
2075 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00002076 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2077
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002078 // Emit the requirements.
2079 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2080 Record.clear();
2081 Record.push_back(SUBMODULE_REQUIRES);
2082 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2083 Mod->Requires[I].data(),
2084 Mod->Requires[I].size());
2085 }
2086
Douglas Gregor69021972011-11-30 17:33:56 +00002087 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002088 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002089 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002090 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002091 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002092 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002093 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2094 Record.clear();
2095 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2096 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2097 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002098 }
2099
2100 // Emit the headers.
2101 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2102 Record.clear();
2103 Record.push_back(SUBMODULE_HEADER);
2104 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2105 Mod->Headers[I]->getName());
2106 }
Douglas Gregor59527662012-10-15 06:28:11 +00002107 // Emit the excluded headers.
2108 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2109 Record.clear();
2110 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2111 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2112 Mod->ExcludedHeaders[I]->getName());
2113 }
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002114 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2115 Record.clear();
2116 Record.push_back(SUBMODULE_TOPHEADER);
2117 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2118 Mod->TopHeaders[I]->getName());
2119 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002120
2121 // Emit the imports.
2122 if (!Mod->Imports.empty()) {
2123 Record.clear();
2124 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002125 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002126 assert(ImportedID && "Unknown submodule!");
2127 Record.push_back(ImportedID);
2128 }
2129 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2130 }
2131
Douglas Gregor24bb9232011-12-02 18:58:38 +00002132 // Emit the exports.
2133 if (!Mod->Exports.empty()) {
2134 Record.clear();
2135 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002136 if (Module *Exported = Mod->Exports[I].getPointer()) {
2137 unsigned ExportedID = SubmoduleIDs[Exported];
2138 assert(ExportedID > 0 && "Unknown submodule ID?");
2139 Record.push_back(ExportedID);
2140 } else {
2141 Record.push_back(0);
2142 }
2143
Douglas Gregor24bb9232011-12-02 18:58:38 +00002144 Record.push_back(Mod->Exports[I].getInt());
2145 }
2146 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2147 }
2148
Douglas Gregor69021972011-11-30 17:33:56 +00002149 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002150 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2151 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002152 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002153 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002154 }
2155
2156 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002157
2158 assert((NextSubmoduleID - FirstSubmoduleID
2159 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002160}
2161
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002162serialization::SubmoduleID
2163ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002164 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002165 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002166
2167 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002168 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002169 Module *OwningMod
2170 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002171 if (!OwningMod)
2172 return 0;
2173
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002174 // Check whether this submodule is part of our own module.
2175 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002176 return 0;
2177
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002178 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002179}
2180
David Blaikie9c902b52011-09-25 23:23:43 +00002181void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002182 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002183 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002184 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2185 I != E; ++I) {
David Blaikie9c902b52011-09-25 23:23:43 +00002186 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002187 if (point.Loc.isInvalid())
2188 continue;
2189
2190 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbare8c12a22011-09-29 01:42:25 +00002191 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002192 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbara3637e62011-09-29 01:30:00 +00002193 if (I->second.isPragma()) {
2194 Record.push_back(I->first);
2195 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002196 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002197 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002198 Record.push_back(-1); // mark the end of the diag/map pairs for this
2199 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002200 }
2201
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002202 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002203 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002204}
2205
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002206void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2207 if (CXXBaseSpecifiersOffsets.empty())
2208 return;
2209
2210 RecordData Record;
2211
2212 // Create a blob abbreviation for the C++ base specifiers offsets.
2213 using namespace llvm;
2214
2215 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2216 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2219 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2220
Douglas Gregorc27b2872011-08-04 00:01:48 +00002221 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002222 Record.clear();
2223 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2224 Record.push_back(CXXBaseSpecifiersOffsets.size());
2225 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002226 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002227}
2228
Douglas Gregorc5046832009-04-27 18:38:38 +00002229//===----------------------------------------------------------------------===//
2230// Type Serialization
2231//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002232
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002233/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002234void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002235 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002236 if (Idx.getIndex() == 0) // we haven't seen this type before.
2237 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002238
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002239 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002240
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002241 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002242 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002243 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002244 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002245 else if (TypeOffsets.size() < Index) {
2246 TypeOffsets.resize(Index + 1);
2247 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002248 }
2249
2250 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002251
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002252 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002253 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002254
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002255 if (T.hasLocalNonFastQualifiers()) {
2256 Qualifiers Qs = T.getLocalQualifiers();
2257 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002258 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002259 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002260 } else {
2261 switch (T->getTypeClass()) {
2262 // For all of the concrete, non-dependent types, call the
2263 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002264#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002265 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002266#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002267#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002268 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002269 }
2270
2271 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002272 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002273
2274 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002275 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002276}
2277
Douglas Gregorc5046832009-04-27 18:38:38 +00002278//===----------------------------------------------------------------------===//
2279// Declaration Serialization
2280//===----------------------------------------------------------------------===//
2281
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002282/// \brief Write the block containing all of the declaration IDs
2283/// lexically declared within the given DeclContext.
2284///
2285/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2286/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002287uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002288 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002289 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002290 return 0;
2291
Douglas Gregor8f45df52009-04-16 22:23:12 +00002292 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002293 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002294 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002295 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002296 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2297 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002298 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002299
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002300 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002301 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002302 return Offset;
2303}
2304
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002305void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002306 using namespace llvm;
2307 RecordData Record;
2308
2309 // Write the type offsets array
2310 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002311 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2315 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2316 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002317 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002318 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002319 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002320 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002321
2322 // Write the declaration offsets array
2323 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002324 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2328 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2329 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002330 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002331 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002332 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002333 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002334}
2335
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002336void ASTWriter::WriteFileDeclIDsMap() {
2337 using namespace llvm;
2338 RecordData Record;
2339
2340 // Join the vectors of DeclIDs from all files.
2341 SmallVector<DeclID, 256> FileSortedIDs;
2342 for (FileDeclIDsTy::iterator
2343 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2344 DeclIDInFileInfo &Info = *FI->second;
2345 Info.FirstDeclIndex = FileSortedIDs.size();
2346 for (LocDeclIDsTy::iterator
2347 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2348 FileSortedIDs.push_back(DI->second);
2349 }
2350
2351 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2352 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2355 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2356 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002357 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002358 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2359}
2360
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002361void ASTWriter::WriteComments() {
2362 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002363 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002364 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002365 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2366 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002367 I != E; ++I) {
2368 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002369 AddSourceRange((*I)->getSourceRange(), Record);
2370 Record.push_back((*I)->getKind());
2371 Record.push_back((*I)->isTrailingComment());
2372 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002373 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2374 }
2375 Stream.ExitBlock();
2376}
2377
Douglas Gregorc5046832009-04-27 18:38:38 +00002378//===----------------------------------------------------------------------===//
2379// Global Method Pool and Selector Serialization
2380//===----------------------------------------------------------------------===//
2381
Douglas Gregore84a9da2009-04-20 20:36:09 +00002382namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002383// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002384class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002385 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002386
2387public:
2388 typedef Selector key_type;
2389 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002390
Sebastian Redl834bb972010-08-04 17:20:04 +00002391 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002392 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002393 ObjCMethodList Instance, Factory;
2394 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002395 typedef const data_type& data_type_ref;
2396
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002397 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002398
Douglas Gregorc78d3462009-04-24 21:10:55 +00002399 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002400 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002401 }
Mike Stump11289f42009-09-09 15:08:12 +00002402
2403 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002404 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002405 data_type_ref Methods) {
2406 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2407 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002408 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2409 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002410 Method = Method->Next)
2411 if (Method->Method)
2412 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002413 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002414 Method = Method->Next)
2415 if (Method->Method)
2416 DataLen += 4;
2417 clang::io::Emit16(Out, DataLen);
2418 return std::make_pair(KeyLen, DataLen);
2419 }
Mike Stump11289f42009-09-09 15:08:12 +00002420
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002421 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002422 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002423 assert((Start >> 32) == 0 && "Selector key offset too large");
2424 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002425 unsigned N = Sel.getNumArgs();
2426 clang::io::Emit16(Out, N);
2427 if (N == 0)
2428 N = 1;
2429 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002430 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002431 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2432 }
Mike Stump11289f42009-09-09 15:08:12 +00002433
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002434 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002435 data_type_ref Methods, unsigned DataLen) {
2436 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002437 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002438 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002439 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002440 Method = Method->Next)
2441 if (Method->Method)
2442 ++NumInstanceMethods;
2443
2444 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002445 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002446 Method = Method->Next)
2447 if (Method->Method)
2448 ++NumFactoryMethods;
2449
2450 clang::io::Emit16(Out, NumInstanceMethods);
2451 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002452 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002453 Method = Method->Next)
2454 if (Method->Method)
2455 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002456 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002457 Method = Method->Next)
2458 if (Method->Method)
2459 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002460
2461 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002462 }
2463};
2464} // end anonymous namespace
2465
Sebastian Redla19a67f2010-08-03 21:58:15 +00002466/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002467///
2468/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002469/// in an on-disk hash table indexed by the selector. The hash table also
2470/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002471void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002472 using namespace llvm;
2473
Sebastian Redla19a67f2010-08-03 21:58:15 +00002474 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002475 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002476 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002477 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002478 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002479 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002480 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002481 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002482
Sebastian Redla19a67f2010-08-03 21:58:15 +00002483 // Create the on-disk hash table representation. We walk through every
2484 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002485 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002486 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002487 I = SelectorIDs.begin(), E = SelectorIDs.end();
2488 I != E; ++I) {
2489 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002490 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002491 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002492 I->second,
2493 ObjCMethodList(),
2494 ObjCMethodList()
2495 };
2496 if (F != SemaRef.MethodPool.end()) {
2497 Data.Instance = F->second.first;
2498 Data.Factory = F->second.second;
2499 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002500 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002501 // changed.
2502 if (Chain && I->second < FirstSelectorID) {
2503 // Selector already exists. Did it change?
2504 bool changed = false;
2505 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2506 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002507 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002508 changed = true;
2509 }
2510 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2511 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002512 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002513 changed = true;
2514 }
2515 if (!changed)
2516 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002517 } else if (Data.Instance.Method || Data.Factory.Method) {
2518 // A new method pool entry.
2519 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002520 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002521 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002522 }
2523
Douglas Gregorc78d3462009-04-24 21:10:55 +00002524 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002525 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002526 uint32_t BucketOffset;
2527 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002528 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002529 llvm::raw_svector_ostream Out(MethodPool);
2530 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002531 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002532 BucketOffset = Generator.Emit(Out, Trait);
2533 }
2534
2535 // Create a blob abbreviation
2536 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002537 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2541 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2542
Douglas Gregor95c13f52009-04-25 17:48:32 +00002543 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002544 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002545 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002546 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002547 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002548 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002549
2550 // Create a blob abbreviation for the selector table offsets.
2551 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002552 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2556 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2557
2558 // Write the selector offsets table.
2559 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002560 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002561 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002562 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002563 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002564 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002565 }
2566}
2567
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002568/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002569void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002570 using namespace llvm;
2571 if (SemaRef.ReferencedSelectors.empty())
2572 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002573
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002574 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002575
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002576 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002577 // very tricky to fix, and given that @selector shouldn't really appear in
2578 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002579 for (DenseMap<Selector, SourceLocation>::iterator S =
2580 SemaRef.ReferencedSelectors.begin(),
2581 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2582 Selector Sel = (*S).first;
2583 SourceLocation Loc = (*S).second;
2584 AddSelectorRef(Sel, Record);
2585 AddSourceLocation(Loc, Record);
2586 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002587 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002588}
2589
Douglas Gregorc5046832009-04-27 18:38:38 +00002590//===----------------------------------------------------------------------===//
2591// Identifier Table Serialization
2592//===----------------------------------------------------------------------===//
2593
Douglas Gregorc78d3462009-04-24 21:10:55 +00002594namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002595class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002596 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002597 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002598 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002599 bool IsModule;
2600
Douglas Gregor1d583f22009-04-28 21:18:29 +00002601 /// \brief Determines whether this is an "interesting" identifier
2602 /// that needs a full IdentifierInfo structure written into the hash
2603 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002604 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002605 if (II->isPoisoned() ||
2606 II->isExtensionToken() ||
2607 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002608 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002609 II->getFETokenInfo<void>())
2610 return true;
2611
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002612 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002613 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002614
2615 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2616 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002617 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002618
2619 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002620 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002621
2622 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002623 }
2624
Douglas Gregore84a9da2009-04-20 20:36:09 +00002625public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002626 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002627 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002628
Sebastian Redl539c5062010-08-18 23:57:32 +00002629 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002630 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002631
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002632 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2633 IdentifierResolver &IdResolver, bool IsModule)
2634 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002635
2636 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002637 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002638 }
Mike Stump11289f42009-09-09 15:08:12 +00002639
2640 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002641 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002642 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002643 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002644 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002645 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002646 DataLen += 2; // 2 bytes for builtin ID
2647 DataLen += 2; // 2 bytes for flags
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002648 if (hadMacroDefinition(II, Macro)) {
2649 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2650 if (Writer.getMacroRef(M) != 0)
2651 DataLen += 4;
2652 }
2653
2654 DataLen += 4;
2655 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002656
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002657 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2658 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002659 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002660 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002661 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002662 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002663 // We emit the key length after the data length so that every
2664 // string is preceded by a 16-bit length. This matches the PTH
2665 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002666 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002667 return std::make_pair(KeyLen, DataLen);
2668 }
Mike Stump11289f42009-09-09 15:08:12 +00002669
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002670 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002671 unsigned KeyLen) {
2672 // Record the location of the key data. This is used when generating
2673 // the mapping from persistent IDs to strings.
2674 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002675 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002676 }
Mike Stump11289f42009-09-09 15:08:12 +00002677
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002678 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002679 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002680 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002681 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002682 clang::io::Emit32(Out, ID << 1);
2683 return;
2684 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002685
Douglas Gregor1d583f22009-04-28 21:18:29 +00002686 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002687 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2688 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2689 clang::io::Emit16(Out, Bits);
2690 Bits = 0;
2691 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002692 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002693 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2694 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002695 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002696 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002697 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002698
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002699 if (HadMacroDefinition) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002700 // Write all of the macro IDs associated with this identifier.
2701 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2702 if (MacroID ID = Writer.getMacroRef(M))
2703 clang::io::Emit32(Out, ID);
2704 }
2705
2706 clang::io::Emit32(Out, 0);
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002707 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002708
Douglas Gregora868bbd2009-04-21 22:25:48 +00002709 // Emit the declaration IDs in reverse order, because the
2710 // IdentifierResolver provides the declarations as they would be
2711 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002712 // "stat"), but the ASTReader adds declarations to the end of the list
2713 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002714 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002715 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2716 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002717 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002718 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002719 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002720 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002721 }
2722};
2723} // end anonymous namespace
2724
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002725/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002726///
2727/// The identifier table consists of a blob containing string data
2728/// (the actual identifiers themselves) and a separate "offsets" index
2729/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002730void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2731 IdentifierResolver &IdResolver,
2732 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002733 using namespace llvm;
2734
2735 // Create and write out the blob that contains the identifier
2736 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002737 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002738 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002739 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002740
Douglas Gregore6648fb2009-04-28 20:33:11 +00002741 // Look for any identifiers that were named while processing the
2742 // headers, but are otherwise not needed. We add these to the hash
2743 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002744 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002745 // file.
2746 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2747 IDEnd = PP.getIdentifierTable().end();
2748 ID != IDEnd; ++ID)
2749 getIdentifierRef(ID->second);
2750
Sebastian Redlff4a2952010-07-23 23:49:55 +00002751 // Create the on-disk hash table representation. We only store offsets
2752 // for identifiers that appear here for the first time.
2753 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002754 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002755 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2756 ID != IDEnd; ++ID) {
2757 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002758 if (!Chain || !ID->first->isFromAST() ||
2759 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002760 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2761 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002762 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002763
Douglas Gregore84a9da2009-04-20 20:36:09 +00002764 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002765 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002766 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002767 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002768 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002769 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002770 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002771 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002772 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002773 }
2774
2775 // Create a blob abbreviation
2776 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002777 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002780 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002781
2782 // Write the identifier table
2783 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002784 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002785 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002786 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002787 }
2788
2789 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002790 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002791 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2795 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2796
2797 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002798 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002799 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002800 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002801 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002802 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002803}
2804
Douglas Gregorc5046832009-04-27 18:38:38 +00002805//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002806// DeclContext's Name Lookup Table Serialization
2807//===----------------------------------------------------------------------===//
2808
2809namespace {
2810// Trait used for the on-disk hash table used in the method pool.
2811class ASTDeclContextNameLookupTrait {
2812 ASTWriter &Writer;
2813
2814public:
2815 typedef DeclarationName key_type;
2816 typedef key_type key_type_ref;
2817
2818 typedef DeclContext::lookup_result data_type;
2819 typedef const data_type& data_type_ref;
2820
2821 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2822
2823 unsigned ComputeHash(DeclarationName Name) {
2824 llvm::FoldingSetNodeID ID;
2825 ID.AddInteger(Name.getNameKind());
2826
2827 switch (Name.getNameKind()) {
2828 case DeclarationName::Identifier:
2829 ID.AddString(Name.getAsIdentifierInfo()->getName());
2830 break;
2831 case DeclarationName::ObjCZeroArgSelector:
2832 case DeclarationName::ObjCOneArgSelector:
2833 case DeclarationName::ObjCMultiArgSelector:
2834 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2835 break;
2836 case DeclarationName::CXXConstructorName:
2837 case DeclarationName::CXXDestructorName:
2838 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002839 break;
2840 case DeclarationName::CXXOperatorName:
2841 ID.AddInteger(Name.getCXXOverloadedOperator());
2842 break;
2843 case DeclarationName::CXXLiteralOperatorName:
2844 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2845 case DeclarationName::CXXUsingDirective:
2846 break;
2847 }
2848
2849 return ID.ComputeHash();
2850 }
2851
2852 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002853 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002854 data_type_ref Lookup) {
2855 unsigned KeyLen = 1;
2856 switch (Name.getNameKind()) {
2857 case DeclarationName::Identifier:
2858 case DeclarationName::ObjCZeroArgSelector:
2859 case DeclarationName::ObjCOneArgSelector:
2860 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002861 case DeclarationName::CXXLiteralOperatorName:
2862 KeyLen += 4;
2863 break;
2864 case DeclarationName::CXXOperatorName:
2865 KeyLen += 1;
2866 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002867 case DeclarationName::CXXConstructorName:
2868 case DeclarationName::CXXDestructorName:
2869 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002870 case DeclarationName::CXXUsingDirective:
2871 break;
2872 }
2873 clang::io::Emit16(Out, KeyLen);
2874
2875 // 2 bytes for num of decls and 4 for each DeclID.
2876 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2877 clang::io::Emit16(Out, DataLen);
2878
2879 return std::make_pair(KeyLen, DataLen);
2880 }
2881
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002882 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002883 using namespace clang::io;
2884
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002885 Emit8(Out, Name.getNameKind());
2886 switch (Name.getNameKind()) {
2887 case DeclarationName::Identifier:
2888 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002889 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002890 case DeclarationName::ObjCZeroArgSelector:
2891 case DeclarationName::ObjCOneArgSelector:
2892 case DeclarationName::ObjCMultiArgSelector:
2893 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002894 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002895 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002896 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2897 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002898 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00002899 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002900 case DeclarationName::CXXLiteralOperatorName:
2901 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002902 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002903 case DeclarationName::CXXConstructorName:
2904 case DeclarationName::CXXDestructorName:
2905 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002906 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002907 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002908 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00002909
2910 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002911 }
2912
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002913 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002914 data_type Lookup, unsigned DataLen) {
2915 uint64_t Start = Out.tell(); (void)Start;
2916 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2917 for (; Lookup.first != Lookup.second; ++Lookup.first)
2918 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2919
2920 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2921 }
2922};
2923} // end anonymous namespace
2924
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002925/// \brief Write the block containing all of the declaration IDs
2926/// visible from the given DeclContext.
2927///
2928/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002929/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002930uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2931 DeclContext *DC) {
2932 if (DC->getPrimaryContext() != DC)
2933 return 0;
2934
2935 // Since there is no name lookup into functions or methods, don't bother to
2936 // build a visible-declarations table for these entities.
2937 if (DC->isFunctionOrMethod())
2938 return 0;
2939
2940 // If not in C++, we perform name lookup for the translation unit via the
2941 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2942 // FIXME: In C++ we need the visible declarations in order to "see" the
2943 // friend declarations, is there a way to do this without writing the table ?
David Blaikiebbafb8a2012-03-11 07:00:24 +00002944 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002945 return 0;
2946
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002947 // Serialize the contents of the mapping used for lookup. Note that,
2948 // although we have two very different code paths, the serialized
2949 // representation is the same for both cases: a declaration name,
2950 // followed by a size, followed by references to the visible
2951 // declarations that have that name.
2952 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00002953 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002954 if (!Map || Map->empty())
2955 return 0;
2956
2957 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2958 ASTDeclContextNameLookupTrait Trait(*this);
2959
2960 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002961 DeclarationName ConversionName;
2962 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002963 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2964 D != DEnd; ++D) {
2965 DeclarationName Name = D->first;
2966 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002967 if (Result.first != Result.second) {
2968 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2969 // Hash all conversion function names to the same name. The actual
2970 // type information in conversion function name is not used in the
2971 // key (since such type information is not stable across different
2972 // modules), so the intended effect is to coalesce all of the conversion
2973 // functions under a single key.
2974 if (!ConversionName)
2975 ConversionName = Name;
2976 ConversionDecls.append(Result.first, Result.second);
2977 continue;
2978 }
2979
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002980 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002981 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002982 }
2983
Douglas Gregor05ef9312011-08-30 20:49:19 +00002984 // Add the conversion functions
2985 if (!ConversionDecls.empty()) {
2986 Generator.insert(ConversionName,
2987 DeclContext::lookup_result(ConversionDecls.begin(),
2988 ConversionDecls.end()),
2989 Trait);
2990 }
2991
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002992 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002993 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002994 uint32_t BucketOffset;
2995 {
2996 llvm::raw_svector_ostream Out(LookupTable);
2997 // Make sure that no bucket is at offset 0
2998 clang::io::Emit32(Out, 0);
2999 BucketOffset = Generator.Emit(Out, Trait);
3000 }
3001
3002 // Write the lookup table
3003 RecordData Record;
3004 Record.push_back(DECL_CONTEXT_VISIBLE);
3005 Record.push_back(BucketOffset);
3006 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3007 LookupTable.str());
3008
3009 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3010 ++NumVisibleDeclContexts;
3011 return Offset;
3012}
3013
Sebastian Redla4071b42010-08-24 00:50:09 +00003014/// \brief Write an UPDATE_VISIBLE block for the given context.
3015///
3016/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3017/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003018/// (in C++), for namespaces, and for classes with forward-declared unscoped
3019/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003020void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003021 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3022 if (!Map || Map->empty())
3023 return;
3024
3025 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3026 ASTDeclContextNameLookupTrait Trait(*this);
3027
3028 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003029 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3030 D != DEnd; ++D) {
3031 DeclarationName Name = D->first;
3032 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003033 // For any name that appears in this table, the results are complete, i.e.
3034 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003035 if (Result.first != Result.second)
3036 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003037 }
3038
3039 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003040 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003041 uint32_t BucketOffset;
3042 {
3043 llvm::raw_svector_ostream Out(LookupTable);
3044 // Make sure that no bucket is at offset 0
3045 clang::io::Emit32(Out, 0);
3046 BucketOffset = Generator.Emit(Out, Trait);
3047 }
3048
3049 // Write the lookup table
3050 RecordData Record;
3051 Record.push_back(UPDATE_VISIBLE);
3052 Record.push_back(getDeclID(cast<Decl>(DC)));
3053 Record.push_back(BucketOffset);
3054 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3055}
3056
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003057/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3058void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3059 RecordData Record;
3060 Record.push_back(Opts.fp_contract);
3061 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3062}
3063
3064/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3065void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003066 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003067 return;
3068
3069 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3070 RecordData Record;
3071#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3072#include "clang/Basic/OpenCLExtensions.def"
3073 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3074}
3075
Douglas Gregor358cd442012-01-15 16:58:34 +00003076void ASTWriter::WriteRedeclarations() {
3077 RecordData LocalRedeclChains;
3078 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3079
3080 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3081 Decl *First = Redeclarations[I];
3082 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3083
3084 Decl *MostRecent = First->getMostRecentDecl();
3085
3086 // If we only have a single declaration, there is no point in storing
3087 // a redeclaration chain.
3088 if (First == MostRecent)
3089 continue;
3090
3091 unsigned Offset = LocalRedeclChains.size();
3092 unsigned Size = 0;
3093 LocalRedeclChains.push_back(0); // Placeholder for the size.
3094
3095 // Collect the set of local redeclarations of this declaration.
3096 for (Decl *Prev = MostRecent; Prev != First;
3097 Prev = Prev->getPreviousDecl()) {
3098 if (!Prev->isFromASTFile()) {
3099 AddDeclRef(Prev, LocalRedeclChains);
3100 ++Size;
3101 }
3102 }
3103 LocalRedeclChains[Offset] = Size;
3104
3105 // Reverse the set of local redeclarations, so that we store them in
3106 // order (since we found them in reverse order).
3107 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3108
3109 // Add the mapping from the first ID to the set of local declarations.
3110 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3111 LocalRedeclsMap.push_back(Info);
3112
3113 assert(N == Redeclarations.size() &&
3114 "Deserialized a declaration we shouldn't have");
3115 }
3116
3117 if (LocalRedeclChains.empty())
3118 return;
3119
3120 // Sort the local redeclarations map by the first declaration ID,
3121 // since the reader will be performing binary searches on this information.
3122 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3123
3124 // Emit the local redeclarations map.
3125 using namespace llvm;
3126 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3127 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3129 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3130 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3131
3132 RecordData Record;
3133 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3134 Record.push_back(LocalRedeclsMap.size());
3135 Stream.EmitRecordWithBlob(AbbrevID, Record,
3136 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3137 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3138
3139 // Emit the redeclaration chains.
3140 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3141}
3142
Douglas Gregor404cdde2012-01-27 01:47:08 +00003143void ASTWriter::WriteObjCCategories() {
3144 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3145 RecordData Categories;
3146
3147 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3148 unsigned Size = 0;
3149 unsigned StartIndex = Categories.size();
3150
3151 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3152
3153 // Allocate space for the size.
3154 Categories.push_back(0);
3155
3156 // Add the categories.
3157 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3158 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3159 assert(getDeclID(Cat) != 0 && "Bogus category");
3160 AddDeclRef(Cat, Categories);
3161 }
3162
3163 // Update the size.
3164 Categories[StartIndex] = Size;
3165
3166 // Record this interface -> category map.
3167 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3168 CategoriesMap.push_back(CatInfo);
3169 }
3170
3171 // Sort the categories map by the definition ID, since the reader will be
3172 // performing binary searches on this information.
3173 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3174
3175 // Emit the categories map.
3176 using namespace llvm;
3177 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3178 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3181 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3182
3183 RecordData Record;
3184 Record.push_back(OBJC_CATEGORIES_MAP);
3185 Record.push_back(CategoriesMap.size());
3186 Stream.EmitRecordWithBlob(AbbrevID, Record,
3187 reinterpret_cast<char*>(CategoriesMap.data()),
3188 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3189
3190 // Emit the category lists.
3191 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3192}
3193
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003194void ASTWriter::WriteMergedDecls() {
3195 if (!Chain || Chain->MergedDecls.empty())
3196 return;
3197
3198 RecordData Record;
3199 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3200 IEnd = Chain->MergedDecls.end();
3201 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003202 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003203 : getDeclID(I->first);
3204 assert(CanonID && "Merged declaration not known?");
3205
3206 Record.push_back(CanonID);
3207 Record.push_back(I->second.size());
3208 Record.append(I->second.begin(), I->second.end());
3209 }
3210 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3211}
3212
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003213//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003214// General Serialization Routines
3215//===----------------------------------------------------------------------===//
3216
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003217/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003218void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3219 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003220 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003221 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3222 e = Attrs.end(); i != e; ++i){
3223 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003224 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003225 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003226
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003227#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003228
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003229 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003230}
3231
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003232void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003233 Record.push_back(Str.size());
3234 Record.insert(Record.end(), Str.begin(), Str.end());
3235}
3236
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003237void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3238 RecordDataImpl &Record) {
3239 Record.push_back(Version.getMajor());
3240 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3241 Record.push_back(*Minor + 1);
3242 else
3243 Record.push_back(0);
3244 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3245 Record.push_back(*Subminor + 1);
3246 else
3247 Record.push_back(0);
3248}
3249
Douglas Gregore84a9da2009-04-20 20:36:09 +00003250/// \brief Note that the identifier II occurs at the given offset
3251/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003252void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003253 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003254 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003255 // up earlier in the chain and thus don't need an offset.
3256 if (ID >= FirstIdentID)
3257 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003258}
3259
Douglas Gregor95c13f52009-04-25 17:48:32 +00003260/// \brief Note that the selector Sel occurs at the given offset
3261/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003262void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003263 unsigned ID = SelectorIDs[Sel];
3264 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003265 // Don't record offsets for selectors that are also available in a different
3266 // file.
3267 if (ID < FirstSelectorID)
3268 return;
3269 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003270}
3271
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003272ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003273 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003274 WritingAST(false), DoneWritingDeclsAndTypes(false),
3275 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003276 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003277 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003278 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3279 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003280 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3281 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003282 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003283 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003284 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003285 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003286 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003287 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003288 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3289 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3290 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003291 DeclTypedefAbbrev(0),
3292 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3293 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003294{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003295}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003296
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003297ASTWriter::~ASTWriter() {
3298 for (FileDeclIDsTy::iterator
3299 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3300 delete I->second;
3301}
3302
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003303void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003304 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003305 Module *WritingModule, StringRef isysroot,
3306 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003307 WritingAST = true;
3308
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003309 ASTHasCompilerErrors = hasErrors;
3310
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003311 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003312 Stream.Emit((unsigned)'C', 8);
3313 Stream.Emit((unsigned)'P', 8);
3314 Stream.Emit((unsigned)'C', 8);
3315 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003316
Chris Lattner28fa4e62009-04-26 22:26:21 +00003317 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003318
Douglas Gregoreda8e122011-08-09 15:13:55 +00003319 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003320 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003321 this->WritingModule = WritingModule;
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003322 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003323 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003324 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003325 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003326
3327 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003328}
3329
Douglas Gregora94a1542011-07-27 21:45:57 +00003330template<typename Vector>
3331static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3332 ASTWriter::RecordData &Record) {
3333 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3334 I != E; ++I) {
3335 Writer.AddDeclRef(*I, Record);
3336 }
3337}
3338
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003339void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003340 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003341 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003342 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003343 using namespace llvm;
3344
Douglas Gregorcf68c582011-12-01 22:20:10 +00003345 // Make sure that the AST reader knows to finalize itself.
3346 if (Chain)
3347 Chain->finalizeForWriting();
3348
Sebastian Redl143413f2010-07-12 22:02:52 +00003349 ASTContext &Context = SemaRef.Context;
3350 Preprocessor &PP = SemaRef.PP;
3351
Douglas Gregordab42432011-08-12 00:15:20 +00003352 // Set up predefined declaration IDs.
3353 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003354 if (Context.ObjCIdDecl)
3355 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003356 if (Context.ObjCSelDecl)
3357 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003358 if (Context.ObjCClassDecl)
3359 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003360 if (Context.ObjCProtocolClassDecl)
3361 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003362 if (Context.Int128Decl)
3363 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3364 if (Context.UInt128Decl)
3365 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003366 if (Context.ObjCInstanceTypeDecl)
3367 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003368 if (Context.BuiltinVaListDecl)
3369 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3370
Douglas Gregor851443c2011-08-12 01:39:19 +00003371 if (!Chain) {
3372 // Make sure that we emit IdentifierInfos (and any attached
3373 // declarations) for builtins. We don't need to do this when we're
3374 // emitting chained PCH files, because all of the builtins will be
3375 // in the original PCH file.
3376 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003377 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003378 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003379 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003380 Context.getLangOpts().NoBuiltin);
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003381 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3382 getIdentifierRef(&Table.get(BuiltinNames[I]));
3383 }
3384
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003385 // If there are any out-of-date identifiers, bring them up to date.
3386 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3387 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3388 IDEnd = PP.getIdentifierTable().end();
3389 ID != IDEnd; ++ID)
3390 if (ID->second->isOutOfDate())
3391 ExtSource->updateOutOfDateIdentifier(*ID->second);
3392 }
3393
Chris Lattner0c797362009-09-08 18:19:27 +00003394 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003395 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003396 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003397 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003398 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003399
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003400 // Build a record containing all of the file scoped decls in this file.
3401 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003402 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3403 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003404
Douglas Gregor851443c2011-08-12 01:39:19 +00003405 // Build a record containing all of the delegating constructors we still need
3406 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003407 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003408 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003409
Douglas Gregor851443c2011-08-12 01:39:19 +00003410 // Write the set of weak, undeclared identifiers. We always write the
3411 // entire table, since later PCH files in a PCH chain are only interested in
3412 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003413 RecordData WeakUndeclaredIdentifiers;
3414 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003415 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003416 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3417 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3418 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3419 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3420 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3421 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3422 }
3423 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003424
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003425 // Build a record containing all of the locally-scoped external
3426 // declarations in this header file. Generally, this record will be
3427 // empty.
3428 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003429 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003430 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003431 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003432 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3433 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003434 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003435 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003436 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3437 }
3438
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003439 // Build a record containing all of the ext_vector declarations.
3440 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003441 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003442
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003443 // Build a record containing all of the VTable uses information.
3444 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003445 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003446 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3447 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3448 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3449 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3450 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003451 }
3452
3453 // Build a record containing all of dynamic classes declarations.
3454 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003455 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003456
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003457 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003458 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003459 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003460 I = SemaRef.PendingInstantiations.begin(),
3461 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3462 AddDeclRef(I->first, PendingInstantiations);
3463 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003464 }
3465 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3466 "There are local ones at end of translation unit!");
3467
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003468 // Build a record containing some declaration references.
3469 RecordData SemaDeclRefs;
3470 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3471 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3472 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3473 }
3474
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003475 RecordData CUDASpecialDeclRefs;
3476 if (Context.getcudaConfigureCallDecl()) {
3477 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3478 }
3479
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003480 // Build a record containing all of the known namespaces.
3481 RecordData KnownNamespaces;
3482 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3483 I = SemaRef.KnownNamespaces.begin(),
3484 IEnd = SemaRef.KnownNamespaces.end();
3485 I != IEnd; ++I) {
3486 if (!I->second)
3487 AddDeclRef(I->first, KnownNamespaces);
3488 }
Douglas Gregor112b9072012-10-18 05:31:06 +00003489
3490 // Write the control block
3491 WriteControlBlock(Context, isysroot, OutputFile);
3492
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003493 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003494 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003495 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorc567ba22011-07-22 16:35:34 +00003496 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00003497 WriteStatCache(*StatCalls);
Douglas Gregor851443c2011-08-12 01:39:19 +00003498
3499 // Create a lexical update block containing all of the declarations in the
3500 // translation unit that do not come from other AST files.
3501 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3502 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3503 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3504 E = TU->noload_decls_end();
3505 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003506 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003507 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003508 }
3509
3510 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3511 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3512 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3513 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3514 Record.clear();
3515 Record.push_back(TU_UPDATE_LEXICAL);
3516 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3517 data(NewGlobalDecls));
3518
3519 // And a visible updates block for the translation unit.
3520 Abv = new llvm::BitCodeAbbrev();
3521 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3522 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3523 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3524 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3525 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3526 WriteDeclContextVisibleUpdate(TU);
3527
3528 // If the translation unit has an anonymous namespace, and we don't already
3529 // have an update block for it, write it as an update block.
3530 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3531 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3532 if (Record.empty()) {
3533 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003534 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003535 }
3536 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003537
3538 // Make sure visible decls, added to DeclContexts previously loaded from
3539 // an AST file, are registered for serialization.
3540 for (SmallVector<const Decl *, 16>::iterator
3541 I = UpdatingVisibleDecls.begin(),
3542 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3543 GetDeclRef(*I);
3544 }
3545
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003546 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003547 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003548
Douglas Gregor5204bde2011-08-02 16:26:37 +00003549 // Form the record of special types.
3550 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00003551 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003552 AddTypeRef(Context.getFILEType(), SpecialTypes);
3553 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3554 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3555 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3556 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003557 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003558 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003559
Douglas Gregor1970d882009-04-26 03:49:13 +00003560 // Keep writing types and declarations until all types and
3561 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003562 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003563 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003564 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3565 E = DeclsToRewrite.end();
3566 I != E; ++I)
3567 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003568 while (!DeclTypesToEmit.empty()) {
3569 DeclOrType DOT = DeclTypesToEmit.front();
3570 DeclTypesToEmit.pop();
3571 if (DOT.isType())
3572 WriteType(DOT.getType());
3573 else
3574 WriteDecl(Context, DOT.getDecl());
3575 }
3576 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003577
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003578 DoneWritingDeclsAndTypes = true;
3579
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003580 WriteFileDeclIDsMap();
3581 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003582 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003583
3584 if (Chain) {
3585 // Write the mapping information describing our module dependencies and how
3586 // each of those modules were mapped into our own offset/ID space, so that
3587 // the reader can build the appropriate mapping to its own offset/ID space.
3588 // The map consists solely of a blob with the following format:
3589 // *(module-name-len:i16 module-name:len*i8
3590 // source-location-offset:i32
3591 // identifier-id:i32
3592 // preprocessed-entity-id:i32
3593 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003594 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003595 // selector-id:i32
3596 // declaration-id:i32
3597 // c++-base-specifiers-id:i32
3598 // type-id:i32)
3599 //
3600 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3601 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3603 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003604 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003605 {
3606 llvm::raw_svector_ostream Out(Buffer);
3607 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003608 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003609 M != MEnd; ++M) {
3610 StringRef FileName = (*M)->FileName;
3611 io::Emit16(Out, FileName.size());
3612 Out.write(FileName.data(), FileName.size());
3613 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3614 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003615 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003616 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003617 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003618 io::Emit32(Out, (*M)->BaseSelectorID);
3619 io::Emit32(Out, (*M)->BaseDeclID);
3620 io::Emit32(Out, (*M)->BaseTypeIndex);
3621 }
3622 }
3623 Record.clear();
3624 Record.push_back(MODULE_OFFSET_MAP);
3625 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3626 Buffer.data(), Buffer.size());
3627 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003628 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003629 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003630 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003631 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003632 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003633 WriteFPPragmaOptions(SemaRef.getFPOptions());
3634 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003635
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003636 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003637 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003638
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003639 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003640
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003641 // If we're emitting a module, write out the submodule information.
3642 if (WritingModule)
3643 WriteSubmodules(WritingModule);
3644
Douglas Gregor5204bde2011-08-02 16:26:37 +00003645 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3646
Douglas Gregord4df8652009-04-22 22:02:47 +00003647 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003648 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003649 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003650
3651 // Write the record containing tentative definitions.
3652 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003653 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003654
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003655 // Write the record containing unused file scoped decls.
3656 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003657 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003658
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003659 // Write the record containing weak undeclared identifiers.
3660 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003661 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003662 WeakUndeclaredIdentifiers);
3663
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003664 // Write the record containing locally-scoped external definitions.
3665 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003666 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003667 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003668
3669 // Write the record containing ext_vector type names.
3670 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003671 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003672
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003673 // Write the record containing VTable uses information.
3674 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003675 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003676
3677 // Write the record containing dynamic classes declarations.
3678 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003679 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003680
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003681 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003682 if (!PendingInstantiations.empty())
3683 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003684
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003685 // Write the record containing declaration references of Sema.
3686 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003687 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003688
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003689 // Write the record containing CUDA-specific declaration references.
3690 if (!CUDASpecialDeclRefs.empty())
3691 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003692
3693 // Write the delegating constructors.
3694 if (!DelegatingCtorDecls.empty())
3695 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003696
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003697 // Write the known namespaces.
3698 if (!KnownNamespaces.empty())
3699 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3700
Douglas Gregor851443c2011-08-12 01:39:19 +00003701 // Write the visible updates to DeclContexts.
3702 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3703 I = UpdatedDeclContexts.begin(),
3704 E = UpdatedDeclContexts.end();
3705 I != E; ++I)
3706 WriteDeclContextVisibleUpdate(*I);
3707
Douglas Gregor959bb062011-12-03 01:15:29 +00003708 if (!WritingModule) {
3709 // Write the submodules that were imported, if any.
3710 RecordData ImportedModules;
3711 for (ASTContext::import_iterator I = Context.local_import_begin(),
3712 IEnd = Context.local_import_end();
3713 I != IEnd; ++I) {
3714 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3715 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3716 }
3717 if (!ImportedModules.empty()) {
3718 // Sort module IDs.
3719 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3720
3721 // Unique module IDs.
3722 ImportedModules.erase(std::unique(ImportedModules.begin(),
3723 ImportedModules.end()),
3724 ImportedModules.end());
3725
3726 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3727 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003728 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003729
3730 WriteMacroUpdates();
Douglas Gregordab42432011-08-12 00:15:20 +00003731 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003732 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003733 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003734 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003735 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003736
Douglas Gregor08f01292009-04-17 22:13:46 +00003737 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003738 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003739 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003740 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003741 Record.push_back(NumLexicalDeclContexts);
3742 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003743 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003744 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003745}
3746
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003747void ASTWriter::WriteMacroUpdates() {
3748 if (MacroUpdates.empty())
3749 return;
3750
3751 RecordData Record;
3752 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3753 E = MacroUpdates.end();
3754 I != E; ++I) {
3755 addMacroRef(I->first, Record);
3756 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregorcfa46a82012-10-12 00:16:50 +00003757 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003758 }
3759 Stream.EmitRecord(MACRO_UPDATES, Record);
3760}
3761
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003762/// \brief Go through the declaration update blocks and resolve declaration
3763/// pointers into declaration IDs.
3764void ASTWriter::ResolveDeclUpdatesBlocks() {
3765 for (DeclUpdateMap::iterator
3766 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3767 const Decl *D = I->first;
3768 UpdateRecord &URec = I->second;
3769
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003770 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003771 continue; // The decl will be written completely
3772
3773 unsigned Idx = 0, N = URec.size();
3774 while (Idx < N) {
3775 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003776 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3777 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3778 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3779 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3780 ++Idx;
3781 break;
3782
3783 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3784 ++Idx;
3785 break;
3786 }
3787 }
3788 }
3789}
3790
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003791void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003792 if (DeclUpdates.empty())
3793 return;
3794
3795 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003796 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003797 for (DeclUpdateMap::iterator
3798 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3799 const Decl *D = I->first;
3800 UpdateRecord &URec = I->second;
3801
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003802 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003803 continue; // The decl will be written completely,no need to store updates.
3804
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003805 uint64_t Offset = Stream.GetCurrentBitNo();
3806 Stream.EmitRecord(DECL_UPDATES, URec);
3807
3808 OffsetsRecord.push_back(GetDeclRef(D));
3809 OffsetsRecord.push_back(Offset);
3810 }
3811 Stream.ExitBlock();
3812 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3813}
3814
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003815void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003816 if (ReplacedDecls.empty())
3817 return;
3818
3819 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003820 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003821 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003822 Record.push_back(I->ID);
3823 Record.push_back(I->Offset);
3824 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003825 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003826 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003827}
3828
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003829void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003830 Record.push_back(Loc.getRawEncoding());
3831}
3832
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003833void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003834 AddSourceLocation(Range.getBegin(), Record);
3835 AddSourceLocation(Range.getEnd(), Record);
3836}
3837
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003838void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003839 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003840 const uint64_t *Words = Value.getRawData();
3841 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003842}
3843
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003844void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003845 Record.push_back(Value.isUnsigned());
3846 AddAPInt(Value, Record);
3847}
3848
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003849void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003850 AddAPInt(Value.bitcastToAPInt(), Record);
3851}
3852
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003853void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003854 Record.push_back(getIdentifierRef(II));
3855}
3856
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003857void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3858 Record.push_back(getMacroRef(MI));
3859}
3860
Sebastian Redl539c5062010-08-18 23:57:32 +00003861IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003862 if (II == 0)
3863 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003864
Sebastian Redl539c5062010-08-18 23:57:32 +00003865 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003866 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003867 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003868 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003869}
3870
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003871MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3872 // Don't emit builtin macros like __LINE__ to the AST file unless they
3873 // have been redefined by the header (in which case they are not
3874 // isBuiltinMacro).
3875 if (MI == 0 || MI->isBuiltinMacro())
3876 return 0;
3877
3878 MacroID &ID = MacroIDs[MI];
3879 if (ID == 0)
3880 ID = NextMacroID++;
3881 return ID;
3882}
3883
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003884void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003885 Record.push_back(getSelectorRef(SelRef));
3886}
3887
Sebastian Redl539c5062010-08-18 23:57:32 +00003888SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003889 if (Sel.getAsOpaquePtr() == 0) {
3890 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003891 }
3892
Sebastian Redl539c5062010-08-18 23:57:32 +00003893 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003894 if (SID == 0 && Chain) {
3895 // This might trigger a ReadSelector callback, which will set the ID for
3896 // this selector.
3897 Chain->LoadSelector(Sel);
3898 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003899 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003900 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003901 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003902 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003903}
3904
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003905void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003906 AddDeclRef(Temp->getDestructor(), Record);
3907}
3908
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003909void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3910 CXXBaseSpecifier const *BasesEnd,
3911 RecordDataImpl &Record) {
3912 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3913 CXXBaseSpecifiersToWrite.push_back(
3914 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3915 Bases, BasesEnd));
3916 Record.push_back(NextCXXBaseSpecifiersID++);
3917}
3918
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003919void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003920 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003921 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003922 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003923 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003924 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003925 break;
3926 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003927 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003928 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003929 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003930 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003931 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003932 break;
3933 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003934 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003935 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003936 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003937 break;
John McCall0ad16662009-10-29 08:12:44 +00003938 case TemplateArgument::Null:
3939 case TemplateArgument::Integral:
3940 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00003941 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00003942 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00003943 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00003944 break;
3945 }
3946}
3947
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003948void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003949 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003950 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003951
3952 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3953 bool InfoHasSameExpr
3954 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3955 Record.push_back(InfoHasSameExpr);
3956 if (InfoHasSameExpr)
3957 return; // Avoid storing the same expr twice.
3958 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003959 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3960 Record);
3961}
3962
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003963void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3964 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003965 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003966 AddTypeRef(QualType(), Record);
3967 return;
3968 }
3969
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003970 AddTypeLoc(TInfo->getTypeLoc(), Record);
3971}
3972
3973void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3974 AddTypeRef(TL.getType(), Record);
3975
John McCall8f115c62009-10-16 21:56:05 +00003976 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003977 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003978 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003979}
3980
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003981void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003982 Record.push_back(GetOrCreateTypeID(T));
3983}
3984
Douglas Gregoreda8e122011-08-09 15:13:55 +00003985TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3986 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003987 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3988}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003989
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003990TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003991 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003992 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003993}
3994
3995TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3996 if (T.isNull())
3997 return TypeIdx();
3998 assert(!T.getLocalFastQualifiers());
3999
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004000 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004001 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004002 if (DoneWritingDeclsAndTypes) {
4003 assert(0 && "New type seen after serializing all the types to emit!");
4004 return TypeIdx();
4005 }
4006
Douglas Gregor1970d882009-04-26 03:49:13 +00004007 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004008 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004009 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004010 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004011 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004012 return Idx;
4013}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004014
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004015TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004016 if (T.isNull())
4017 return TypeIdx();
4018 assert(!T.getLocalFastQualifiers());
4019
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004020 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4021 assert(I != TypeIdxs.end() && "Type not emitted!");
4022 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004023}
4024
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004025void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004026 Record.push_back(GetDeclRef(D));
4027}
4028
Sebastian Redl539c5062010-08-18 23:57:32 +00004029DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004030 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4031
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004032 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004033 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004034 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004035
4036 // If D comes from an AST file, its declaration ID is already known and
4037 // fixed.
4038 if (D->isFromASTFile())
4039 return D->getGlobalID();
4040
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004041 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004042 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004043 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004044 if (DoneWritingDeclsAndTypes) {
4045 assert(0 && "New decl seen after serializing all the decls to emit!");
4046 return 0;
4047 }
4048
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004049 // We haven't seen this declaration before. Give it a new ID and
4050 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004051 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004052 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004053 }
4054
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004055 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004056}
4057
Sebastian Redl539c5062010-08-18 23:57:32 +00004058DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004059 if (D == 0)
4060 return 0;
4061
Douglas Gregorb3163e52012-01-05 22:33:30 +00004062 // If D comes from an AST file, its declaration ID is already known and
4063 // fixed.
4064 if (D->isFromASTFile())
4065 return D->getGlobalID();
4066
Douglas Gregore84a9da2009-04-20 20:36:09 +00004067 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4068 return DeclIDs[D];
4069}
4070
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004071static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4072 std::pair<unsigned, serialization::DeclID> R) {
4073 return L.first < R.first;
4074}
4075
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004076void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004077 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004078 assert(D);
4079
4080 SourceLocation Loc = D->getLocation();
4081 if (Loc.isInvalid())
4082 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004083
4084 // We only keep track of the file-level declarations of each file.
4085 if (!D->getLexicalDeclContext()->isFileContext())
4086 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004087 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4088 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004089 if (isa<ParmVarDecl>(D))
4090 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004091
4092 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004093 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004094 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004095 FileID FID;
4096 unsigned Offset;
4097 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004098 if (FID.isInvalid())
4099 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004100 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004101
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004102 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004103 if (!Info)
4104 Info = new DeclIDInFileInfo();
4105
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004106 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004107 LocDeclIDsTy &Decls = Info->DeclIDs;
4108
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004109 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004110 Decls.push_back(LocDecl);
4111 return;
4112 }
4113
4114 LocDeclIDsTy::iterator
4115 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4116
4117 Decls.insert(I, LocDecl);
4118}
4119
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004120void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004121 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004122 Record.push_back(Name.getNameKind());
4123 switch (Name.getNameKind()) {
4124 case DeclarationName::Identifier:
4125 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4126 break;
4127
4128 case DeclarationName::ObjCZeroArgSelector:
4129 case DeclarationName::ObjCOneArgSelector:
4130 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004131 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004132 break;
4133
4134 case DeclarationName::CXXConstructorName:
4135 case DeclarationName::CXXDestructorName:
4136 case DeclarationName::CXXConversionFunctionName:
4137 AddTypeRef(Name.getCXXNameType(), Record);
4138 break;
4139
4140 case DeclarationName::CXXOperatorName:
4141 Record.push_back(Name.getCXXOverloadedOperator());
4142 break;
4143
Alexis Hunt3d221f22009-11-29 07:34:05 +00004144 case DeclarationName::CXXLiteralOperatorName:
4145 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4146 break;
4147
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004148 case DeclarationName::CXXUsingDirective:
4149 // No extra data to emit
4150 break;
4151 }
4152}
Chris Lattnerca025db2010-05-07 21:43:38 +00004153
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004154void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004155 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004156 switch (Name.getNameKind()) {
4157 case DeclarationName::CXXConstructorName:
4158 case DeclarationName::CXXDestructorName:
4159 case DeclarationName::CXXConversionFunctionName:
4160 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4161 break;
4162
4163 case DeclarationName::CXXOperatorName:
4164 AddSourceLocation(
4165 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4166 Record);
4167 AddSourceLocation(
4168 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4169 Record);
4170 break;
4171
4172 case DeclarationName::CXXLiteralOperatorName:
4173 AddSourceLocation(
4174 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4175 Record);
4176 break;
4177
4178 case DeclarationName::Identifier:
4179 case DeclarationName::ObjCZeroArgSelector:
4180 case DeclarationName::ObjCOneArgSelector:
4181 case DeclarationName::ObjCMultiArgSelector:
4182 case DeclarationName::CXXUsingDirective:
4183 break;
4184 }
4185}
4186
4187void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004188 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004189 AddDeclarationName(NameInfo.getName(), Record);
4190 AddSourceLocation(NameInfo.getLoc(), Record);
4191 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4192}
4193
4194void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004195 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004196 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004197 Record.push_back(Info.NumTemplParamLists);
4198 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4199 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4200}
4201
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004202void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004203 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004204 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004205 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004206 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004207
4208 // Push each of the NNS's onto a stack for serialization in reverse order.
4209 while (NNS) {
4210 NestedNames.push_back(NNS);
4211 NNS = NNS->getPrefix();
4212 }
4213
4214 Record.push_back(NestedNames.size());
4215 while(!NestedNames.empty()) {
4216 NNS = NestedNames.pop_back_val();
4217 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4218 Record.push_back(Kind);
4219 switch (Kind) {
4220 case NestedNameSpecifier::Identifier:
4221 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4222 break;
4223
4224 case NestedNameSpecifier::Namespace:
4225 AddDeclRef(NNS->getAsNamespace(), Record);
4226 break;
4227
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004228 case NestedNameSpecifier::NamespaceAlias:
4229 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4230 break;
4231
Chris Lattnerca025db2010-05-07 21:43:38 +00004232 case NestedNameSpecifier::TypeSpec:
4233 case NestedNameSpecifier::TypeSpecWithTemplate:
4234 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4235 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4236 break;
4237
4238 case NestedNameSpecifier::Global:
4239 // Don't need to write an associated value.
4240 break;
4241 }
4242 }
4243}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004244
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004245void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4246 RecordDataImpl &Record) {
4247 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004248 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004249 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004250
4251 // Push each of the nested-name-specifiers's onto a stack for
4252 // serialization in reverse order.
4253 while (NNS) {
4254 NestedNames.push_back(NNS);
4255 NNS = NNS.getPrefix();
4256 }
4257
4258 Record.push_back(NestedNames.size());
4259 while(!NestedNames.empty()) {
4260 NNS = NestedNames.pop_back_val();
4261 NestedNameSpecifier::SpecifierKind Kind
4262 = NNS.getNestedNameSpecifier()->getKind();
4263 Record.push_back(Kind);
4264 switch (Kind) {
4265 case NestedNameSpecifier::Identifier:
4266 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4267 AddSourceRange(NNS.getLocalSourceRange(), Record);
4268 break;
4269
4270 case NestedNameSpecifier::Namespace:
4271 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4272 AddSourceRange(NNS.getLocalSourceRange(), Record);
4273 break;
4274
4275 case NestedNameSpecifier::NamespaceAlias:
4276 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4277 AddSourceRange(NNS.getLocalSourceRange(), Record);
4278 break;
4279
4280 case NestedNameSpecifier::TypeSpec:
4281 case NestedNameSpecifier::TypeSpecWithTemplate:
4282 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4283 AddTypeLoc(NNS.getTypeLoc(), Record);
4284 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4285 break;
4286
4287 case NestedNameSpecifier::Global:
4288 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4289 break;
4290 }
4291 }
4292}
4293
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004294void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004295 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004296 Record.push_back(Kind);
4297 switch (Kind) {
4298 case TemplateName::Template:
4299 AddDeclRef(Name.getAsTemplateDecl(), Record);
4300 break;
4301
4302 case TemplateName::OverloadedTemplate: {
4303 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4304 Record.push_back(OvT->size());
4305 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4306 I != E; ++I)
4307 AddDeclRef(*I, Record);
4308 break;
4309 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004310
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004311 case TemplateName::QualifiedTemplate: {
4312 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4313 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4314 Record.push_back(QualT->hasTemplateKeyword());
4315 AddDeclRef(QualT->getTemplateDecl(), Record);
4316 break;
4317 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004318
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004319 case TemplateName::DependentTemplate: {
4320 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4321 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4322 Record.push_back(DepT->isIdentifier());
4323 if (DepT->isIdentifier())
4324 AddIdentifierRef(DepT->getIdentifier(), Record);
4325 else
4326 Record.push_back(DepT->getOperator());
4327 break;
4328 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004329
4330 case TemplateName::SubstTemplateTemplateParm: {
4331 SubstTemplateTemplateParmStorage *subst
4332 = Name.getAsSubstTemplateTemplateParm();
4333 AddDeclRef(subst->getParameter(), Record);
4334 AddTemplateName(subst->getReplacement(), Record);
4335 break;
4336 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004337
4338 case TemplateName::SubstTemplateTemplateParmPack: {
4339 SubstTemplateTemplateParmPackStorage *SubstPack
4340 = Name.getAsSubstTemplateTemplateParmPack();
4341 AddDeclRef(SubstPack->getParameterPack(), Record);
4342 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4343 break;
4344 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004345 }
4346}
4347
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004348void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004349 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004350 Record.push_back(Arg.getKind());
4351 switch (Arg.getKind()) {
4352 case TemplateArgument::Null:
4353 break;
4354 case TemplateArgument::Type:
4355 AddTypeRef(Arg.getAsType(), Record);
4356 break;
4357 case TemplateArgument::Declaration:
4358 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004359 Record.push_back(Arg.isDeclForReferenceParam());
4360 break;
4361 case TemplateArgument::NullPtr:
4362 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004363 break;
4364 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004365 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004366 AddTypeRef(Arg.getIntegralType(), Record);
4367 break;
4368 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004369 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4370 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004371 case TemplateArgument::TemplateExpansion:
4372 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004373 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4374 Record.push_back(*NumExpansions + 1);
4375 else
4376 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004377 break;
4378 case TemplateArgument::Expression:
4379 AddStmt(Arg.getAsExpr());
4380 break;
4381 case TemplateArgument::Pack:
4382 Record.push_back(Arg.pack_size());
4383 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4384 I != E; ++I)
4385 AddTemplateArgument(*I, Record);
4386 break;
4387 }
4388}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004389
4390void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004391ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004392 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004393 assert(TemplateParams && "No TemplateParams!");
4394 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4395 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4396 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4397 Record.push_back(TemplateParams->size());
4398 for (TemplateParameterList::const_iterator
4399 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4400 P != PEnd; ++P)
4401 AddDeclRef(*P, Record);
4402}
4403
4404/// \brief Emit a template argument list.
4405void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004406ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004407 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004408 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004409 Record.push_back(TemplateArgs->size());
4410 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004411 AddTemplateArgument(TemplateArgs->get(i), Record);
4412}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004413
4414
4415void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004416ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004417 Record.push_back(Set.size());
4418 for (UnresolvedSetImpl::const_iterator
4419 I = Set.begin(), E = Set.end(); I != E; ++I) {
4420 AddDeclRef(I.getDecl(), Record);
4421 Record.push_back(I.getAccess());
4422 }
4423}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004424
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004425void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004426 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004427 Record.push_back(Base.isVirtual());
4428 Record.push_back(Base.isBaseOfClass());
4429 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004430 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004431 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004432 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004433 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4434 : SourceLocation(),
4435 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004436}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004437
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004438void ASTWriter::FlushCXXBaseSpecifiers() {
4439 RecordData Record;
4440 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4441 Record.clear();
4442
4443 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004444 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004445 if (Index == CXXBaseSpecifiersOffsets.size())
4446 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4447 else {
4448 if (Index > CXXBaseSpecifiersOffsets.size())
4449 CXXBaseSpecifiersOffsets.resize(Index + 1);
4450 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4451 }
4452
4453 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4454 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4455 Record.push_back(BEnd - B);
4456 for (; B != BEnd; ++B)
4457 AddCXXBaseSpecifier(*B, Record);
4458 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004459
4460 // Flush any expressions that were written as part of the base specifiers.
4461 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004462 }
4463
4464 CXXBaseSpecifiersToWrite.clear();
4465}
4466
Alexis Hunt1d792652011-01-08 20:30:50 +00004467void ASTWriter::AddCXXCtorInitializers(
4468 const CXXCtorInitializer * const *CtorInitializers,
4469 unsigned NumCtorInitializers,
4470 RecordDataImpl &Record) {
4471 Record.push_back(NumCtorInitializers);
4472 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4473 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004474
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004475 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004476 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004477 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004478 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004479 } else if (Init->isDelegatingInitializer()) {
4480 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004481 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004482 } else if (Init->isMemberInitializer()){
4483 Record.push_back(CTOR_INITIALIZER_MEMBER);
4484 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004485 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004486 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4487 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004488 }
Francois Pichetd583da02010-12-04 09:14:42 +00004489
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004490 AddSourceLocation(Init->getMemberLocation(), Record);
4491 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004492 AddSourceLocation(Init->getLParenLoc(), Record);
4493 AddSourceLocation(Init->getRParenLoc(), Record);
4494 Record.push_back(Init->isWritten());
4495 if (Init->isWritten()) {
4496 Record.push_back(Init->getSourceOrder());
4497 } else {
4498 Record.push_back(Init->getNumArrayIndices());
4499 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4500 AddDeclRef(Init->getArrayIndex(i), Record);
4501 }
4502 }
4503}
4504
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004505void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4506 assert(D->DefinitionData);
4507 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004508 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004509 Record.push_back(Data.UserDeclaredConstructor);
4510 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004511 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004512 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004513 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004514 Record.push_back(Data.UserDeclaredDestructor);
4515 Record.push_back(Data.Aggregate);
4516 Record.push_back(Data.PlainOldData);
4517 Record.push_back(Data.Empty);
4518 Record.push_back(Data.Polymorphic);
4519 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004520 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004521 Record.push_back(Data.HasNoNonEmptyBases);
4522 Record.push_back(Data.HasPrivateFields);
4523 Record.push_back(Data.HasProtectedFields);
4524 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004525 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004526 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00004527 Record.push_back(Data.HasInClassInitializer);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004528 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004529 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004530 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00004531 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004532 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004533 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004534 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004535 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004536 Record.push_back(Data.HasTrivialDestructor);
Richard Smith561fb152012-02-25 07:33:38 +00004537 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004538 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004539 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004540 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004541 Record.push_back(Data.DeclaredDefaultConstructor);
4542 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004543 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004544 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004545 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004546 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004547 Record.push_back(Data.FailedImplicitMoveConstructor);
4548 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004549 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004550
4551 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004552 if (Data.NumBases > 0)
4553 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4554 Record);
4555
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004556 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4557 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004558 if (Data.NumVBases > 0)
4559 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4560 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004561
4562 AddUnresolvedSet(Data.Conversions, Record);
4563 AddUnresolvedSet(Data.VisibleConversions, Record);
4564 // Data.Definition is the owning decl, no need to write it.
4565 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004566
4567 // Add lambda-specific data.
4568 if (Data.IsLambda) {
4569 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004570 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004571 Record.push_back(Lambda.NumCaptures);
4572 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004573 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004574 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00004575 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004576 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4577 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4578 AddSourceLocation(Capture.getLocation(), Record);
4579 Record.push_back(Capture.isImplicit());
4580 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4581 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4582 AddDeclRef(Var, Record);
4583 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4584 : SourceLocation(),
4585 Record);
4586 }
4587 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004588}
4589
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004590void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004591 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004592 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004593 assert(FirstDeclID == NextDeclID &&
4594 FirstTypeID == NextTypeID &&
4595 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004596 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004597 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004598 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004599 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004600
Sebastian Redl07a89a82010-07-30 00:29:29 +00004601 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004602
Douglas Gregordf0c1512011-08-18 04:12:04 +00004603 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4604 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4605 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004606 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004607 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004608 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004609 NextDeclID = FirstDeclID;
4610 NextTypeID = FirstTypeID;
4611 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004612 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004613 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004614 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004615}
4616
Sebastian Redl539c5062010-08-18 23:57:32 +00004617void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004618 IdentifierIDs[II] = ID;
4619}
4620
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004621void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4622 MacroIDs[MI] = ID;
4623}
4624
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004625void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004626 // Always take the highest-numbered type index. This copes with an interesting
4627 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004628 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004629 // keep the higher-numbered entry so that we can properly write it out to
4630 // the AST file.
4631 TypeIdx &StoredIdx = TypeIdxs[T];
4632 if (Idx.getIndex() >= StoredIdx.getIndex())
4633 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004634}
4635
Sebastian Redl539c5062010-08-18 23:57:32 +00004636void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004637 SelectorIDs[S] = ID;
4638}
Douglas Gregor91096292010-10-02 19:29:26 +00004639
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004640void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004641 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004642 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004643 MacroDefinitions[MD] = ID;
4644}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004645
Douglas Gregore37a85a2011-12-02 17:30:13 +00004646void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4647 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4648 SubmoduleIDs[Mod] = ID;
4649}
4650
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004651void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4652 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4653}
4654
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004655void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004656 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004657 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004658 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4659 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004660 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004661 // A forward reference was mutated into a definition. Rewrite it.
4662 // FIXME: This happens during template instantiation, should we
4663 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004664 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004665 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004666 }
4667}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004668
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004669void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004670 assert(!WritingAST && "Already writing the AST!");
4671
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004672 // TU and namespaces are handled elsewhere.
4673 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4674 return;
4675
Douglas Gregorb3722e22011-09-09 23:01:35 +00004676 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004677 return; // Not a source decl added to a DeclContext from PCH.
4678
4679 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004680 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004681}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004682
4683void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004684 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004685 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004686 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004687 return; // Not a source member added to a class from PCH.
4688 if (!isa<CXXMethodDecl>(D))
4689 return; // We are interested in lazily declared implicit methods.
4690
4691 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004692 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004693 UpdateRecord &Record = DeclUpdates[RD];
4694 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004695 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004696}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004697
4698void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4699 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004700 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004701 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004702 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004703 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004704 return; // Not a source specialization added to a template from PCH.
4705
4706 UpdateRecord &Record = DeclUpdates[TD];
4707 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004708 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004709}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004710
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004711void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4712 const FunctionDecl *D) {
4713 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004714 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004715 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004716 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004717 return; // Not a source specialization added to a template from PCH.
4718
4719 UpdateRecord &Record = DeclUpdates[TD];
4720 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004721 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004722}
4723
Sebastian Redlab238a72011-04-24 16:28:06 +00004724void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004725 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004726 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004727 return; // Declaration not imported from PCH.
4728
4729 // Implicit decl from a PCH was defined.
4730 // FIXME: Should implicit definition be a separate FunctionDecl?
4731 RewriteDecl(D);
4732}
4733
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004734void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004735 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004736 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004737 return;
4738
4739 // Since the actual instantiation is delayed, this really means that we need
4740 // to update the instantiation location.
4741 UpdateRecord &Record = DeclUpdates[D];
4742 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4743 AddSourceLocation(
4744 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4745}
4746
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004747void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4748 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004749 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004750 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004751 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004752
4753 assert(IFD->getDefinition() && "Category on a class without a definition?");
4754 ObjCClassesWithCategories.insert(
4755 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004756}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004757
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004758
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004759void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4760 const ObjCPropertyDecl *OrigProp,
4761 const ObjCCategoryDecl *ClassExt) {
4762 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4763 if (!D)
4764 return;
4765
4766 assert(!WritingAST && "Already writing the AST!");
4767 if (!D->isFromASTFile())
4768 return; // Declaration not imported from PCH.
4769
4770 RewriteDecl(D);
4771}