blob: 85b5326da993b71b6a45d6f7bbe6a3dad432cafd [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"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.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"
Chris Lattner225dd6c2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000051using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052
Sebastian Redl3df5a082010-07-30 17:03:48 +000053template <typename T, typename Allocator>
54T *data(std::vector<T, Allocator> &v) {
55 return v.empty() ? 0 : &v.front();
56}
57template <typename T, typename Allocator>
58const T *data(const std::vector<T, Allocator> &v) {
59 return v.empty() ? 0 : &v.front();
60}
61
Douglas Gregoref84c4b2009-04-09 22:27:44 +000062//===----------------------------------------------------------------------===//
63// Type serialization
64//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000065
Douglas Gregoref84c4b2009-04-09 22:27:44 +000066namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000067 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000068 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000069 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000070
71 public:
72 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000073 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000074
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000075 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000076 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000077
78 void VisitArrayType(const ArrayType *T);
79 void VisitFunctionType(const FunctionType *T);
80 void VisitTagType(const TagType *T);
81
82#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
83#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084#include "clang/AST/TypeNodes.def"
85 };
86}
87
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000088void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089 assert(false && "Built-in types are never serialized");
90}
91
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000092void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000094 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000099 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100}
101
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000104 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000108 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
109 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000110 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111}
112
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000115 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116}
117
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000119 Writer.AddTypeRef(T->getPointeeType(), Record);
120 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000121 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122}
123
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125 Writer.AddTypeRef(T->getElementType(), Record);
126 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000127 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128}
129
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131 VisitArrayType(T);
132 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000133 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134}
135
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000138 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139}
140
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000143 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
144 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000145 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000146 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147}
148
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150 Writer.AddTypeRef(T->getElementType(), Record);
151 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000152 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000153 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154}
155
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000163 FunctionType::ExtInfo C = T->getExtInfo();
164 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000165 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000166 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000167 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000168 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169}
170
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000172 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000173 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174}
175
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000176void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177 VisitFunctionType(T);
178 Record.push_back(T->getNumArgs());
179 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
180 Writer.AddTypeRef(T->getArgType(I), Record);
181 Record.push_back(T->isVariadic());
182 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000183 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000184 Record.push_back(T->getExceptionSpecType());
185 if (T->getExceptionSpecType() == EST_Dynamic) {
186 Record.push_back(T->getNumExceptions());
187 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
188 Writer.AddTypeRef(T->getExceptionType(I), Record);
189 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
190 Writer.AddStmt(T->getNoexceptExpr());
191 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000192 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193}
194
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000195void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000196 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000197 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000198}
John McCallb96ec562009-12-04 22:46:56 +0000199
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000200void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000201 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000202 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
203 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000204 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205}
206
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000208 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000209 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000210}
211
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000214 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000215}
216
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000217void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000218 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000220}
221
Richard Smith30482bc2011-02-20 03:19:35 +0000222void ASTTypeWriter::VisitAutoType(const AutoType *T) {
223 Writer.AddTypeRef(T->getDeducedType(), Record);
224 Code = TYPE_AUTO;
225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000228 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000230 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231 "Cannot serialize in the middle of a type definition");
232}
233
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000236 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237}
238
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
John McCall81904512011-01-06 01:58:22 +0000244void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
245 Writer.AddTypeRef(T->getModifiedType(), Record);
246 Writer.AddTypeRef(T->getEquivalentType(), Record);
247 Record.push_back(T->getAttrKind());
248 Code = TYPE_ATTRIBUTED;
249}
250
Mike Stump11289f42009-09-09 15:08:12 +0000251void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000252ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000253 const SubstTemplateTypeParmType *T) {
254 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
255 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000256 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000257}
258
259void
Douglas Gregorada4b792011-01-14 02:55:32 +0000260ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
261 const SubstTemplateTypeParmPackType *T) {
262 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
263 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
264 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
265}
266
267void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000268ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000269 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000270 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000271 Writer.AddTemplateName(T->getTemplateName(), Record);
272 Record.push_back(T->getNumArgs());
273 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
274 ArgI != ArgE; ++ArgI)
275 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000276 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
277 : T->getCanonicalTypeInternal(),
278 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000279 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000280}
281
282void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000283ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000284 VisitArrayType(T);
285 Writer.AddStmt(T->getSizeExpr());
286 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000287 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000288}
289
290void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000291ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000292 const DependentSizedExtVectorType *T) {
293 // FIXME: Serialize this type (C++ only)
294 assert(false && "Cannot serialize dependent sized extended vector types");
295}
296
297void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000298ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000299 Record.push_back(T->getDepth());
300 Record.push_back(T->getIndex());
301 Record.push_back(T->isParameterPack());
302 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000303 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000308 Record.push_back(T->getKeyword());
309 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
310 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000311 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
312 : T->getCanonicalTypeInternal(),
313 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000314 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000315}
316
317void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000318ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000319 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000320 Record.push_back(T->getKeyword());
321 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
322 Writer.AddIdentifierRef(T->getIdentifier(), Record);
323 Record.push_back(T->getNumArgs());
324 for (DependentTemplateSpecializationType::iterator
325 I = T->begin(), E = T->end(); I != E; ++I)
326 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000327 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000328}
329
Douglas Gregord2fa7662010-12-20 02:24:11 +0000330void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
331 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000332 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
333 Record.push_back(*NumExpansions + 1);
334 else
335 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000336 Code = TYPE_PACK_EXPANSION;
337}
338
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000339void ASTTypeWriter::VisitParenType(const ParenType *T) {
340 Writer.AddTypeRef(T->getInnerType(), Record);
341 Code = TYPE_PAREN;
342}
343
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000344void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000345 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000346 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
347 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000352 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000353 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000354 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000355}
356
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000357void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000358 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000359 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000360}
361
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000362void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000363 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000364 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000365 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000366 E = T->qual_end(); I != E; ++I)
367 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000368 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000369}
370
Steve Narofffb4330f2009-06-17 22:40:22 +0000371void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000372ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000373 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000374 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000375}
376
John McCall8f115c62009-10-16 21:56:05 +0000377namespace {
378
379class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000380 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000381 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000382
383public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000384 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000385 : Writer(Writer), Record(Record) { }
386
John McCall17001972009-10-18 01:05:36 +0000387#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000388#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000389 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000390#include "clang/AST/TypeLocNodes.def"
391
John McCall17001972009-10-18 01:05:36 +0000392 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
393 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000394};
395
396}
397
John McCall17001972009-10-18 01:05:36 +0000398void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
399 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000400}
John McCall17001972009-10-18 01:05:36 +0000401void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000402 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
403 if (TL.needsExtraLocalData()) {
404 Record.push_back(TL.getWrittenTypeSpec());
405 Record.push_back(TL.getWrittenSignSpec());
406 Record.push_back(TL.getWrittenWidthSpec());
407 Record.push_back(TL.hasModeAttr());
408 }
John McCall8f115c62009-10-16 21:56:05 +0000409}
John McCall17001972009-10-18 01:05:36 +0000410void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000412}
John McCall17001972009-10-18 01:05:36 +0000413void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000415}
John McCall17001972009-10-18 01:05:36 +0000416void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000418}
John McCall17001972009-10-18 01:05:36 +0000419void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
420 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000421}
John McCall17001972009-10-18 01:05:36 +0000422void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000424}
John McCall17001972009-10-18 01:05:36 +0000425void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000427 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000428}
John McCall17001972009-10-18 01:05:36 +0000429void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
430 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
431 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
432 Record.push_back(TL.getSizeExpr() ? 1 : 0);
433 if (TL.getSizeExpr())
434 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000435}
John McCall17001972009-10-18 01:05:36 +0000436void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
437 VisitArrayTypeLoc(TL);
438}
439void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
440 VisitArrayTypeLoc(TL);
441}
442void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
443 VisitArrayTypeLoc(TL);
444}
445void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
446 DependentSizedArrayTypeLoc TL) {
447 VisitArrayTypeLoc(TL);
448}
449void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
450 DependentSizedExtVectorTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
453void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getNameLoc(), Record);
455}
456void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getNameLoc(), Record);
458}
459void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000460 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
461 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000462 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000463 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
464 Writer.AddDeclRef(TL.getArg(i), Record);
465}
466void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
467 VisitFunctionTypeLoc(TL);
468}
469void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
470 VisitFunctionTypeLoc(TL);
471}
John McCallb96ec562009-12-04 22:46:56 +0000472void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
473 Writer.AddSourceLocation(TL.getNameLoc(), Record);
474}
John McCall17001972009-10-18 01:05:36 +0000475void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
477}
478void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000479 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
480 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
481 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000482}
483void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000484 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
485 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
486 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
487 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000488}
489void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
491}
Richard Smith30482bc2011-02-20 03:19:35 +0000492void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
John McCall17001972009-10-18 01:05:36 +0000495void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getNameLoc(), Record);
497}
498void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getNameLoc(), Record);
500}
John McCall81904512011-01-06 01:58:22 +0000501void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
503 if (TL.hasAttrOperand()) {
504 SourceRange range = TL.getAttrOperandParensRange();
505 Writer.AddSourceLocation(range.getBegin(), Record);
506 Writer.AddSourceLocation(range.getEnd(), Record);
507 }
508 if (TL.hasAttrExprOperand()) {
509 Expr *operand = TL.getAttrExprOperand();
510 Record.push_back(operand ? 1 : 0);
511 if (operand) Writer.AddStmt(operand);
512 } else if (TL.hasAttrEnumOperand()) {
513 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
514 }
515}
John McCall17001972009-10-18 01:05:36 +0000516void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
John McCallcebee162009-10-18 09:09:24 +0000519void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
520 SubstTemplateTypeParmTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
Douglas Gregorada4b792011-01-14 02:55:32 +0000523void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
524 SubstTemplateTypeParmPackTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall17001972009-10-18 01:05:36 +0000527void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
528 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000529 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
530 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
531 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
532 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000533 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
534 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000535}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000536void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
538 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
539}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000540void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000541 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000542 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000543}
John McCalle78aac42010-03-10 03:28:59 +0000544void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000547void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000548 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000549 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000550 Writer.AddSourceLocation(TL.getNameLoc(), Record);
551}
John McCallc392f372010-06-11 00:33:02 +0000552void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
553 DependentTemplateSpecializationTypeLoc TL) {
554 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000555 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
558 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
559 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000560 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
561 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000562}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000563void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
564 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
565}
John McCall17001972009-10-18 01:05:36 +0000566void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
567 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000568}
569void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
570 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000571 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
572 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
573 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
574 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000575}
John McCallfc93cf92009-10-22 22:37:11 +0000576void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
577 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000578}
John McCall8f115c62009-10-16 21:56:05 +0000579
Chris Lattner19cea4e2009-04-22 05:57:30 +0000580//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000581// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000582//===----------------------------------------------------------------------===//
583
Chris Lattner28fa4e62009-04-26 22:26:21 +0000584static void EmitBlockID(unsigned ID, const char *Name,
585 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000586 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000587 Record.clear();
588 Record.push_back(ID);
589 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
590
591 // Emit the block name if present.
592 if (Name == 0 || Name[0] == 0) return;
593 Record.clear();
594 while (*Name)
595 Record.push_back(*Name++);
596 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
597}
598
599static void EmitRecordID(unsigned ID, const char *Name,
600 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000601 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000602 Record.clear();
603 Record.push_back(ID);
604 while (*Name)
605 Record.push_back(*Name++);
606 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000607}
608
609static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000610 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000611#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000612 RECORD(STMT_STOP);
613 RECORD(STMT_NULL_PTR);
614 RECORD(STMT_NULL);
615 RECORD(STMT_COMPOUND);
616 RECORD(STMT_CASE);
617 RECORD(STMT_DEFAULT);
618 RECORD(STMT_LABEL);
619 RECORD(STMT_IF);
620 RECORD(STMT_SWITCH);
621 RECORD(STMT_WHILE);
622 RECORD(STMT_DO);
623 RECORD(STMT_FOR);
624 RECORD(STMT_GOTO);
625 RECORD(STMT_INDIRECT_GOTO);
626 RECORD(STMT_CONTINUE);
627 RECORD(STMT_BREAK);
628 RECORD(STMT_RETURN);
629 RECORD(STMT_DECL);
630 RECORD(STMT_ASM);
631 RECORD(EXPR_PREDEFINED);
632 RECORD(EXPR_DECL_REF);
633 RECORD(EXPR_INTEGER_LITERAL);
634 RECORD(EXPR_FLOATING_LITERAL);
635 RECORD(EXPR_IMAGINARY_LITERAL);
636 RECORD(EXPR_STRING_LITERAL);
637 RECORD(EXPR_CHARACTER_LITERAL);
638 RECORD(EXPR_PAREN);
639 RECORD(EXPR_UNARY_OPERATOR);
640 RECORD(EXPR_SIZEOF_ALIGN_OF);
641 RECORD(EXPR_ARRAY_SUBSCRIPT);
642 RECORD(EXPR_CALL);
643 RECORD(EXPR_MEMBER);
644 RECORD(EXPR_BINARY_OPERATOR);
645 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
646 RECORD(EXPR_CONDITIONAL_OPERATOR);
647 RECORD(EXPR_IMPLICIT_CAST);
648 RECORD(EXPR_CSTYLE_CAST);
649 RECORD(EXPR_COMPOUND_LITERAL);
650 RECORD(EXPR_EXT_VECTOR_ELEMENT);
651 RECORD(EXPR_INIT_LIST);
652 RECORD(EXPR_DESIGNATED_INIT);
653 RECORD(EXPR_IMPLICIT_VALUE_INIT);
654 RECORD(EXPR_VA_ARG);
655 RECORD(EXPR_ADDR_LABEL);
656 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000657 RECORD(EXPR_CHOOSE);
658 RECORD(EXPR_GNU_NULL);
659 RECORD(EXPR_SHUFFLE_VECTOR);
660 RECORD(EXPR_BLOCK);
661 RECORD(EXPR_BLOCK_DECL_REF);
662 RECORD(EXPR_OBJC_STRING_LITERAL);
663 RECORD(EXPR_OBJC_ENCODE);
664 RECORD(EXPR_OBJC_SELECTOR_EXPR);
665 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
666 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
667 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
668 RECORD(EXPR_OBJC_KVC_REF_EXPR);
669 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000670 RECORD(STMT_OBJC_FOR_COLLECTION);
671 RECORD(STMT_OBJC_CATCH);
672 RECORD(STMT_OBJC_FINALLY);
673 RECORD(STMT_OBJC_AT_TRY);
674 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
675 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000676 RECORD(EXPR_CXX_OPERATOR_CALL);
677 RECORD(EXPR_CXX_CONSTRUCT);
678 RECORD(EXPR_CXX_STATIC_CAST);
679 RECORD(EXPR_CXX_DYNAMIC_CAST);
680 RECORD(EXPR_CXX_REINTERPRET_CAST);
681 RECORD(EXPR_CXX_CONST_CAST);
682 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
683 RECORD(EXPR_CXX_BOOL_LITERAL);
684 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000685 RECORD(EXPR_CXX_TYPEID_EXPR);
686 RECORD(EXPR_CXX_TYPEID_TYPE);
687 RECORD(EXPR_CXX_UUIDOF_EXPR);
688 RECORD(EXPR_CXX_UUIDOF_TYPE);
689 RECORD(EXPR_CXX_THIS);
690 RECORD(EXPR_CXX_THROW);
691 RECORD(EXPR_CXX_DEFAULT_ARG);
692 RECORD(EXPR_CXX_BIND_TEMPORARY);
693 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
694 RECORD(EXPR_CXX_NEW);
695 RECORD(EXPR_CXX_DELETE);
696 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
697 RECORD(EXPR_EXPR_WITH_CLEANUPS);
698 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
699 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
700 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
701 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
702 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
703 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
704 RECORD(EXPR_CXX_NOEXCEPT);
705 RECORD(EXPR_OPAQUE_VALUE);
706 RECORD(EXPR_BINARY_TYPE_TRAIT);
707 RECORD(EXPR_PACK_EXPANSION);
708 RECORD(EXPR_SIZEOF_PACK);
709 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000710 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000711#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000712}
Mike Stump11289f42009-09-09 15:08:12 +0000713
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000714void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000715 RecordData Record;
716 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000717
Sebastian Redl539c5062010-08-18 23:57:32 +0000718#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
719#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000720
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000721 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000722 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000723 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000724 RECORD(TYPE_OFFSET);
725 RECORD(DECL_OFFSET);
726 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000727 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000728 RECORD(IDENTIFIER_OFFSET);
729 RECORD(IDENTIFIER_TABLE);
730 RECORD(EXTERNAL_DEFINITIONS);
731 RECORD(SPECIAL_TYPES);
732 RECORD(STATISTICS);
733 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000734 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000735 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
736 RECORD(SELECTOR_OFFSETS);
737 RECORD(METHOD_POOL);
738 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000739 RECORD(SOURCE_LOCATION_OFFSETS);
740 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000741 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000742 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000743 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000744 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000745 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000746 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000747 RECORD(TU_UPDATE_LEXICAL);
748 RECORD(REDECLS_UPDATE_LATEST);
749 RECORD(SEMA_DECL_REFS);
750 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
751 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
752 RECORD(DECL_REPLACEMENTS);
753 RECORD(UPDATE_VISIBLE);
754 RECORD(DECL_UPDATE_OFFSETS);
755 RECORD(DECL_UPDATES);
756 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
757 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000758 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000759 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000760 RECORD(FP_PRAGMA_OPTIONS);
761 RECORD(OPENCL_EXTENSIONS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000762
Chris Lattner28fa4e62009-04-26 22:26:21 +0000763 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000764 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000765 RECORD(SM_SLOC_FILE_ENTRY);
766 RECORD(SM_SLOC_BUFFER_ENTRY);
767 RECORD(SM_SLOC_BUFFER_BLOB);
768 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
769 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000770
Chris Lattner28fa4e62009-04-26 22:26:21 +0000771 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000772 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000773 RECORD(PP_MACRO_OBJECT_LIKE);
774 RECORD(PP_MACRO_FUNCTION_LIKE);
775 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000776
Douglas Gregor12bfa382009-10-17 00:13:19 +0000777 // Decls and Types block.
778 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000779 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000780 RECORD(TYPE_COMPLEX);
781 RECORD(TYPE_POINTER);
782 RECORD(TYPE_BLOCK_POINTER);
783 RECORD(TYPE_LVALUE_REFERENCE);
784 RECORD(TYPE_RVALUE_REFERENCE);
785 RECORD(TYPE_MEMBER_POINTER);
786 RECORD(TYPE_CONSTANT_ARRAY);
787 RECORD(TYPE_INCOMPLETE_ARRAY);
788 RECORD(TYPE_VARIABLE_ARRAY);
789 RECORD(TYPE_VECTOR);
790 RECORD(TYPE_EXT_VECTOR);
791 RECORD(TYPE_FUNCTION_PROTO);
792 RECORD(TYPE_FUNCTION_NO_PROTO);
793 RECORD(TYPE_TYPEDEF);
794 RECORD(TYPE_TYPEOF_EXPR);
795 RECORD(TYPE_TYPEOF);
796 RECORD(TYPE_RECORD);
797 RECORD(TYPE_ENUM);
798 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000799 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000800 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000801 RECORD(TYPE_DECLTYPE);
802 RECORD(TYPE_ELABORATED);
803 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
804 RECORD(TYPE_UNRESOLVED_USING);
805 RECORD(TYPE_INJECTED_CLASS_NAME);
806 RECORD(TYPE_OBJC_OBJECT);
807 RECORD(TYPE_TEMPLATE_TYPE_PARM);
808 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
809 RECORD(TYPE_DEPENDENT_NAME);
810 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
811 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
812 RECORD(TYPE_PAREN);
813 RECORD(TYPE_PACK_EXPANSION);
814 RECORD(TYPE_ATTRIBUTED);
815 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000816 RECORD(DECL_TRANSLATION_UNIT);
817 RECORD(DECL_TYPEDEF);
818 RECORD(DECL_ENUM);
819 RECORD(DECL_RECORD);
820 RECORD(DECL_ENUM_CONSTANT);
821 RECORD(DECL_FUNCTION);
822 RECORD(DECL_OBJC_METHOD);
823 RECORD(DECL_OBJC_INTERFACE);
824 RECORD(DECL_OBJC_PROTOCOL);
825 RECORD(DECL_OBJC_IVAR);
826 RECORD(DECL_OBJC_AT_DEFS_FIELD);
827 RECORD(DECL_OBJC_CLASS);
828 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
829 RECORD(DECL_OBJC_CATEGORY);
830 RECORD(DECL_OBJC_CATEGORY_IMPL);
831 RECORD(DECL_OBJC_IMPLEMENTATION);
832 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
833 RECORD(DECL_OBJC_PROPERTY);
834 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000835 RECORD(DECL_FIELD);
836 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000837 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000838 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000839 RECORD(DECL_FILE_SCOPE_ASM);
840 RECORD(DECL_BLOCK);
841 RECORD(DECL_CONTEXT_LEXICAL);
842 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000843 RECORD(DECL_NAMESPACE);
844 RECORD(DECL_NAMESPACE_ALIAS);
845 RECORD(DECL_USING);
846 RECORD(DECL_USING_SHADOW);
847 RECORD(DECL_USING_DIRECTIVE);
848 RECORD(DECL_UNRESOLVED_USING_VALUE);
849 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
850 RECORD(DECL_LINKAGE_SPEC);
851 RECORD(DECL_CXX_RECORD);
852 RECORD(DECL_CXX_METHOD);
853 RECORD(DECL_CXX_CONSTRUCTOR);
854 RECORD(DECL_CXX_DESTRUCTOR);
855 RECORD(DECL_CXX_CONVERSION);
856 RECORD(DECL_ACCESS_SPEC);
857 RECORD(DECL_FRIEND);
858 RECORD(DECL_FRIEND_TEMPLATE);
859 RECORD(DECL_CLASS_TEMPLATE);
860 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
861 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
862 RECORD(DECL_FUNCTION_TEMPLATE);
863 RECORD(DECL_TEMPLATE_TYPE_PARM);
864 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
865 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
866 RECORD(DECL_STATIC_ASSERT);
867 RECORD(DECL_CXX_BASE_SPECIFIERS);
868 RECORD(DECL_INDIRECTFIELD);
869 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
870
Douglas Gregor92a96f52011-02-08 21:58:10 +0000871 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
872 RECORD(PPD_MACRO_INSTANTIATION);
873 RECORD(PPD_MACRO_DEFINITION);
874 RECORD(PPD_INCLUSION_DIRECTIVE);
875
Douglas Gregor12bfa382009-10-17 00:13:19 +0000876 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000877 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000878#undef RECORD
879#undef BLOCK
880 Stream.ExitBlock();
881}
882
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000883/// \brief Adjusts the given filename to only write out the portion of the
884/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000885///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000886/// \param Filename the file name to adjust.
887///
888/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
889/// the returned filename will be adjusted by this system root.
890///
891/// \returns either the original filename (if it needs no adjustment) or the
892/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000893static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000894adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
895 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000896
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000897 if (!isysroot)
898 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000899
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000900 // Verify that the filename and the system root have the same prefix.
901 unsigned Pos = 0;
902 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
903 if (Filename[Pos] != isysroot[Pos])
904 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000905
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000906 // We hit the end of the filename before we hit the end of the system root.
907 if (!Filename[Pos])
908 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000910 // If the file name has a '/' at the current position, skip over the '/'.
911 // We distinguish sysroot-based includes from absolute includes by the
912 // absence of '/' at the beginning of sysroot-based includes.
913 if (Filename[Pos] == '/')
914 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000915
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000916 return Filename + Pos;
917}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000918
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000919/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000920void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
921 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000922 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000923
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000924 // Metadata
925 const TargetInfo &Target = Context.Target;
926 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000927 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000928 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000929 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
930 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000931 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
932 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
933 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000934 // Target triple or chained PCH name
935 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000936 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000937
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000938 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000939 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
940 Record.push_back(VERSION_MAJOR);
941 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000942 Record.push_back(CLANG_VERSION_MAJOR);
943 Record.push_back(CLANG_VERSION_MINOR);
944 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000945 // FIXME: This writes the absolute path for chained headers.
946 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
947 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000948
Douglas Gregor45fe0362009-05-12 01:31:05 +0000949 // Original file name
950 SourceManager &SM = Context.getSourceManager();
951 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
952 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000953 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000954 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
955 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
956
Michael J. Spencer740857f2010-12-21 16:45:57 +0000957 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000958
Michael J. Spencer740857f2010-12-21 16:45:57 +0000959 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000960
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000961 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000962 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000963 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000964 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000965 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000966 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000967 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000968
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000969 // Original PCH directory
970 if (!OutputFile.empty() && OutputFile != "-") {
971 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
972 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
974 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
975
976 llvm::SmallString<128> OutputPath(OutputFile);
977
978 llvm::sys::fs::make_absolute(OutputPath);
979 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
980
981 RecordData Record;
982 Record.push_back(ORIGINAL_PCH_DIR);
983 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
984 }
985
Ted Kremenek18e066f2010-01-22 22:12:47 +0000986 // Repository branch/version information.
987 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000988 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000989 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
990 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000991 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000992 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000993 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
994 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000995}
996
997/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000998void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000999 RecordData Record;
1000 Record.push_back(LangOpts.Trigraphs);
1001 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1002 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1003 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1004 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +00001005 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001006 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1007 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1008 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1009 Record.push_back(LangOpts.C99); // C99 Support
1010 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001011 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1012 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001013 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1014 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001015 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001016
Douglas Gregor55abb232009-04-10 20:39:37 +00001017 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1018 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001019 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001020 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001021 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001022 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001023 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001024 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1025 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001026 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001027
Douglas Gregor55abb232009-04-10 20:39:37 +00001028 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001029 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1030 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001031 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001032 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001033 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001034 Record.push_back(LangOpts.CXXExceptions);
1035 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001036
Douglas Gregordbe39272011-02-01 15:15:22 +00001037 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001038 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1039 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1040 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1041
Chris Lattner258172e2009-04-27 07:35:58 +00001042 // Whether static initializers are protected by locks.
1043 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001044 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001045 Record.push_back(LangOpts.Blocks); // block extension to C
1046 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1047 // they are unused.
1048 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1049 // (modulo the platform support).
1050
Chris Lattner51924e512010-06-26 21:25:03 +00001051 Record.push_back(LangOpts.getSignedOverflowBehavior());
1052 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001053
1054 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001055 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001056 // defined.
1057 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1058 // opposed to __DYNAMIC__).
1059 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1060
1061 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1062 // used (instead of C99 semantics).
1063 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001064 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1065 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001066 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1067 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001068 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001069 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1070 // to the smallest integer type with
1071 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001072 Record.push_back(LangOpts.getGCMode());
1073 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001074 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001075 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001076 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001077 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001078 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001079 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001080 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001081 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001082 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001083 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001084}
1085
Douglas Gregora7f71a92009-04-10 03:52:48 +00001086//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001087// stat cache Serialization
1088//===----------------------------------------------------------------------===//
1089
1090namespace {
1091// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001092class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001093public:
1094 typedef const char * key_type;
1095 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001096
Chris Lattner2a6fa472010-11-23 19:28:12 +00001097 typedef struct stat data_type;
1098 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001099
1100 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001101 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001102 }
Mike Stump11289f42009-09-09 15:08:12 +00001103
1104 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001105 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1106 data_type_ref Data) {
1107 unsigned StrLen = strlen(path);
1108 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001109 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001110 clang::io::Emit8(Out, DataLen);
1111 return std::make_pair(StrLen + 1, DataLen);
1112 }
Mike Stump11289f42009-09-09 15:08:12 +00001113
Douglas Gregorc5046832009-04-27 18:38:38 +00001114 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1115 Out.write(path, KeyLen);
1116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner2a6fa472010-11-23 19:28:12 +00001118 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001119 data_type_ref Data, unsigned DataLen) {
1120 using namespace clang::io;
1121 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001122
Chris Lattner2a6fa472010-11-23 19:28:12 +00001123 Emit32(Out, (uint32_t) Data.st_ino);
1124 Emit32(Out, (uint32_t) Data.st_dev);
1125 Emit16(Out, (uint16_t) Data.st_mode);
1126 Emit64(Out, (uint64_t) Data.st_mtime);
1127 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001128
1129 assert(Out.tell() - Start == DataLen && "Wrong data length");
1130 }
1131};
1132} // end anonymous namespace
1133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001134/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001135void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001136 // Build the on-disk hash table containing information about every
1137 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001138 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001139 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001140 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001141 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001142 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1143 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001144 Generator.insert(Filename, Stat->second);
1145 }
Mike Stump11289f42009-09-09 15:08:12 +00001146
Douglas Gregorc5046832009-04-27 18:38:38 +00001147 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001148 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001149 uint32_t BucketOffset;
1150 {
1151 llvm::raw_svector_ostream Out(StatCacheData);
1152 // Make sure that no bucket is at offset 0
1153 clang::io::Emit32(Out, 0);
1154 BucketOffset = Generator.Emit(Out);
1155 }
1156
1157 // Create a blob abbreviation
1158 using namespace llvm;
1159 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001160 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1164 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1165
1166 // Write the stat cache
1167 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001168 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001169 Record.push_back(BucketOffset);
1170 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001171 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001172}
1173
1174//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175// Source Manager Serialization
1176//===----------------------------------------------------------------------===//
1177
1178/// \brief Create an abbreviation for the SLocEntry that refers to a
1179/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001180static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001181 using namespace llvm;
1182 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001183 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001188 // FileEntry fields.
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001192 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001193}
1194
1195/// \brief Create an abbreviation for the SLocEntry that refers to a
1196/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001197static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001198 using namespace llvm;
1199 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001200 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001206 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001207}
1208
1209/// \brief Create an abbreviation for the SLocEntry that refers to a
1210/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001211static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001212 using namespace llvm;
1213 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001214 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001216 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001217}
1218
1219/// \brief Create an abbreviation for the SLocEntry that refers to an
1220/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001221static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001222 using namespace llvm;
1223 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001224 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001230 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001231}
1232
Douglas Gregor09b69892011-02-10 17:09:37 +00001233namespace {
1234 // Trait used for the on-disk hash table of header search information.
1235 class HeaderFileInfoTrait {
1236 ASTWriter &Writer;
1237 HeaderSearch &HS;
1238
1239 public:
1240 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1241 : Writer(Writer), HS(HS) { }
1242
1243 typedef const char *key_type;
1244 typedef key_type key_type_ref;
1245
1246 typedef HeaderFileInfo data_type;
1247 typedef const data_type &data_type_ref;
1248
1249 static unsigned ComputeHash(const char *path) {
1250 // The hash is based only on the filename portion of the key, so that the
1251 // reader can match based on filenames when symlinking or excess path
1252 // elements ("foo/../", "../") change the form of the name. However,
1253 // complete path is still the key.
1254 return llvm::HashString(llvm::sys::path::filename(path));
1255 }
1256
1257 std::pair<unsigned,unsigned>
1258 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1259 data_type_ref Data) {
1260 unsigned StrLen = strlen(path);
1261 clang::io::Emit16(Out, StrLen);
1262 unsigned DataLen = 1 + 2 + 4;
1263 clang::io::Emit8(Out, DataLen);
1264 return std::make_pair(StrLen + 1, DataLen);
1265 }
1266
1267 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1268 Out.write(path, KeyLen);
1269 }
1270
1271 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1272 data_type_ref Data, unsigned DataLen) {
1273 using namespace clang::io;
1274 uint64_t Start = Out.tell(); (void)Start;
1275
1276 unsigned char Flags = (Data.isImport << 3)
1277 | (Data.DirInfo << 1)
1278 | Data.Resolved;
1279 Emit8(Out, (uint8_t)Flags);
1280 Emit16(Out, (uint16_t) Data.NumIncludes);
1281
1282 if (!Data.ControllingMacro)
1283 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1284 else
1285 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1286 assert(Out.tell() - Start == DataLen && "Wrong data length");
1287 }
1288 };
1289} // end anonymous namespace
1290
1291/// \brief Write the header search block for the list of files that
1292///
1293/// \param HS The header search structure to save.
1294///
1295/// \param Chain Whether we're creating a chained AST file.
1296void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1297 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1298 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1299
1300 if (FilesByUID.size() > HS.header_file_size())
1301 FilesByUID.resize(HS.header_file_size());
1302
1303 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1304 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1305 llvm::SmallVector<const char *, 4> SavedStrings;
1306 unsigned NumHeaderSearchEntries = 0;
1307 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1308 const FileEntry *File = FilesByUID[UID];
1309 if (!File)
1310 continue;
1311
1312 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1313 if (HFI.External && Chain)
1314 continue;
1315
1316 // Turn the file name into an absolute path, if it isn't already.
1317 const char *Filename = File->getName();
1318 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1319
1320 // If we performed any translation on the file name at all, we need to
1321 // save this string, since the generator will refer to it later.
1322 if (Filename != File->getName()) {
1323 Filename = strdup(Filename);
1324 SavedStrings.push_back(Filename);
1325 }
1326
1327 Generator.insert(Filename, HFI, GeneratorTrait);
1328 ++NumHeaderSearchEntries;
1329 }
1330
1331 // Create the on-disk hash table in a buffer.
1332 llvm::SmallString<4096> TableData;
1333 uint32_t BucketOffset;
1334 {
1335 llvm::raw_svector_ostream Out(TableData);
1336 // Make sure that no bucket is at offset 0
1337 clang::io::Emit32(Out, 0);
1338 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1339 }
1340
1341 // Create a blob abbreviation
1342 using namespace llvm;
1343 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1344 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1348 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1349
1350 // Write the stat cache
1351 RecordData Record;
1352 Record.push_back(HEADER_SEARCH_TABLE);
1353 Record.push_back(BucketOffset);
1354 Record.push_back(NumHeaderSearchEntries);
1355 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1356
1357 // Free all of the strings we had to duplicate.
1358 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1359 free((void*)SavedStrings[I]);
1360}
1361
Douglas Gregora7f71a92009-04-10 03:52:48 +00001362/// \brief Writes the block containing the serialized form of the
1363/// source manager.
1364///
1365/// TODO: We should probably use an on-disk hash table (stored in a
1366/// blob), indexed based on the file name, so that we only create
1367/// entries for files that we actually need. In the common case (no
1368/// errors), we probably won't have to create file entries for any of
1369/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001370void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001371 const Preprocessor &PP,
1372 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001373 RecordData Record;
1374
Chris Lattner0910e3b2009-04-10 17:16:57 +00001375 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001376 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001377
1378 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001379 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1380 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1381 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1382 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001383
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001384 // Write the line table.
1385 if (SourceMgr.hasLineTable()) {
1386 LineTableInfo &LineTable = SourceMgr.getLineTable();
1387
1388 // Emit the file names
1389 Record.push_back(LineTable.getNumFilenames());
1390 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1391 // Emit the file name
1392 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001393 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001394 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1395 Record.push_back(FilenameLen);
1396 if (FilenameLen)
1397 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1398 }
Mike Stump11289f42009-09-09 15:08:12 +00001399
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001400 // Emit the line entries
1401 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1402 L != LEnd; ++L) {
1403 // Emit the file ID
1404 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001405
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001406 // Emit the line entries
1407 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001408 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001409 LEEnd = L->second.end();
1410 LE != LEEnd; ++LE) {
1411 Record.push_back(LE->FileOffset);
1412 Record.push_back(LE->LineNo);
1413 Record.push_back(LE->FilenameID);
1414 Record.push_back((unsigned)LE->FileKind);
1415 Record.push_back(LE->IncludeOffset);
1416 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001417 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001418 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001419 }
1420
Douglas Gregor258ae542009-04-27 06:38:32 +00001421 // Write out the source location entry table. We skip the first
1422 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001423 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001424 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001425 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1426 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1427 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1428 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001429 // Get this source location entry.
1430 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001431
Douglas Gregor258ae542009-04-27 06:38:32 +00001432 // Record the offset of this source-location entry.
1433 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1434
1435 // Figure out which record code to use.
1436 unsigned Code;
1437 if (SLoc->isFile()) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001438 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001439 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001440 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001441 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001442 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001443 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001444 Record.clear();
1445 Record.push_back(Code);
1446
1447 Record.push_back(SLoc->getOffset());
1448 if (SLoc->isFile()) {
1449 const SrcMgr::FileInfo &File = SLoc->getFile();
1450 Record.push_back(File.getIncludeLoc().getRawEncoding());
1451 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1452 Record.push_back(File.hasLineDirectives());
1453
1454 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001455 if (Content->OrigEntry) {
1456 assert(Content->OrigEntry == Content->ContentsEntry &&
1457 "Writing to AST an overriden file is not supported");
1458
Douglas Gregor258ae542009-04-27 06:38:32 +00001459 // The source location entry is a file. The blob associated
1460 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001461
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001462 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001463 Record.push_back(Content->OrigEntry->getSize());
1464 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001465
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001466 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001467 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001468 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001469
1470 // Ask the file manager to fixup the relative path for us. This will
1471 // honor the working directory.
1472 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1473
1474 // FIXME: This call to make_absolute shouldn't be necessary, the
1475 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001476 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001477 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001478
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001479 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001480 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001481 } else {
1482 // The source location entry is a buffer. The blob associated
1483 // with this entry contains the contents of the buffer.
1484
1485 // We add one to the size so that we capture the trailing NULL
1486 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1487 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001488 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001489 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001490 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001491 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1492 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001493 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001494 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001495 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001496 llvm::StringRef(Buffer->getBufferStart(),
1497 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001498
1499 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001500 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001501 }
1502 } else {
1503 // The source location entry is an instantiation.
1504 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1505 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1506 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1507 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1508
1509 // Compute the token length for this macro expansion.
1510 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001511 if (I + 1 != N)
1512 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001513 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1514 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1515 }
1516 }
1517
Douglas Gregor8f45df52009-04-16 22:23:12 +00001518 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001519
1520 if (SLocEntryOffsets.empty())
1521 return;
1522
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001523 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001524 // table is used for lazily loading source-location information.
1525 using namespace llvm;
1526 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001527 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1531 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001532
Douglas Gregor258ae542009-04-27 06:38:32 +00001533 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001534 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001535 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001536 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1537 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001538 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001539 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001540 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001541
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001542 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001543 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001544 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001545}
1546
Douglas Gregorc5046832009-04-27 18:38:38 +00001547//===----------------------------------------------------------------------===//
1548// Preprocessor Serialization
1549//===----------------------------------------------------------------------===//
1550
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001551static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1552 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1553 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1554 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1555 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1556 return X.first->getName().compare(Y.first->getName());
1557}
1558
Chris Lattnereeffaef2009-04-10 17:15:23 +00001559/// \brief Writes the block containing the serialized form of the
1560/// preprocessor.
1561///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001562void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001563 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001564
Chris Lattner0af3ba12009-04-13 01:29:17 +00001565 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1566 if (PP.getCounterValue() != 0) {
1567 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001568 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001569 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001570 }
1571
1572 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001573 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001574
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001575 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001576 // FIXME: use diagnostics subsystem for localization etc.
1577 if (PP.SawDateOrTime())
1578 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001579
Douglas Gregor796d76a2010-10-20 22:00:55 +00001580
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001581 // Loop over all the macro definitions that are live at the end of the file,
1582 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001583 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001584
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001585 // Construct the list of macro definitions that need to be serialized.
1586 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1587 MacrosToEmit;
1588 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001589 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1590 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001591 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001592 MacroDefinitionsSeen.insert(I->first);
1593 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1594 }
1595
1596 // Sort the set of macro definitions that need to be serialized by the
1597 // name of the macro, to provide a stable ordering.
1598 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1599 &compareMacroDefinitions);
1600
Douglas Gregor68051a72011-02-11 00:26:14 +00001601 // Resolve any identifiers that defined macros at the time they were
1602 // deserialized, adding them to the list of macros to emit (if appropriate).
1603 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1604 IdentifierInfo *Name
1605 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1606 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1607 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1608 }
1609
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001610 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1611 const IdentifierInfo *Name = MacrosToEmit[I].first;
1612 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001613 if (!MI)
1614 continue;
1615
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001616 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001617 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001618 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001619
1620 // FIXME: There is a (probably minor) optimization we could do here, if
1621 // the macro comes from the original PCH but the identifier comes from a
1622 // chained PCH, by storing the offset into the original PCH rather than
1623 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001624 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001625 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001626 continue;
1627
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001628 AddIdentifierRef(Name, Record);
1629 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001630 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1631 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001632
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001633 unsigned Code;
1634 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001635 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001636 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001637 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001638
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001639 Record.push_back(MI->isC99Varargs());
1640 Record.push_back(MI->isGNUVarargs());
1641 Record.push_back(MI->getNumArgs());
1642 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1643 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001644 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001645 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001646
Douglas Gregoraae92242010-03-19 21:51:54 +00001647 // If we have a detailed preprocessing record, record the macro definition
1648 // ID that corresponds to this macro.
1649 if (PPRec)
1650 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001651
Douglas Gregor8f45df52009-04-16 22:23:12 +00001652 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001653 Record.clear();
1654
Chris Lattner2199f5b2009-04-10 18:08:30 +00001655 // Emit the tokens array.
1656 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1657 // Note that we know that the preprocessor does not have any annotation
1658 // tokens in it because they are created by the parser, and thus can't be
1659 // in a macro definition.
1660 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001661
Chris Lattner2199f5b2009-04-10 18:08:30 +00001662 Record.push_back(Tok.getLocation().getRawEncoding());
1663 Record.push_back(Tok.getLength());
1664
Chris Lattner2199f5b2009-04-10 18:08:30 +00001665 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1666 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001667 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001668 // FIXME: Should translate token kind to a stable encoding.
1669 Record.push_back(Tok.getKind());
1670 // FIXME: Should translate token flags to a stable encoding.
1671 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001672
Sebastian Redl539c5062010-08-18 23:57:32 +00001673 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001674 Record.clear();
1675 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001676 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001677 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001678 Stream.ExitBlock();
1679
1680 if (PPRec)
1681 WritePreprocessorDetail(*PPRec);
1682}
1683
1684void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1685 if (PPRec.begin(Chain) == PPRec.end(Chain))
1686 return;
1687
1688 // Enter the preprocessor block.
1689 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001690
Douglas Gregoraae92242010-03-19 21:51:54 +00001691 // If the preprocessor has a preprocessing record, emit it.
1692 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001693 using namespace llvm;
1694
1695 // Set up the abbreviation for
1696 unsigned InclusionAbbrev = 0;
1697 {
1698 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1699 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1701 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1702 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1703 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1704 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1705 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1706 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1707 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1708 }
1709
1710 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1711 RecordData Record;
1712 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1713 EEnd = PPRec.end(Chain);
1714 E != EEnd; ++E) {
1715 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001716
Douglas Gregor92a96f52011-02-08 21:58:10 +00001717 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1718 // Record this macro definition's location.
1719 MacroID ID = getMacroDefinitionID(MD);
1720
1721 // Don't write the macro definition if it is from another AST file.
1722 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001723 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001724
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001725 // Notify the serialization listener that we're serializing this entity.
1726 if (SerializationListener)
1727 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001728 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001729
Douglas Gregor92a96f52011-02-08 21:58:10 +00001730 unsigned Position = ID - FirstMacroID;
1731 if (Position != MacroDefinitionOffsets.size()) {
1732 if (Position > MacroDefinitionOffsets.size())
1733 MacroDefinitionOffsets.resize(Position + 1);
1734
1735 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1736 } else
1737 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001738
Douglas Gregor92a96f52011-02-08 21:58:10 +00001739 Record.push_back(IndexBase + NumPreprocessingRecords++);
1740 Record.push_back(ID);
1741 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1742 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1743 AddIdentifierRef(MD->getName(), Record);
1744 AddSourceLocation(MD->getLocation(), Record);
1745 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1746 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001747 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001748
Douglas Gregor92a96f52011-02-08 21:58:10 +00001749 // Notify the serialization listener that we're serializing this entity.
1750 if (SerializationListener)
1751 SerializationListener->SerializedPreprocessedEntity(*E,
1752 Stream.GetCurrentBitNo());
1753
1754 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1755 Record.push_back(IndexBase + NumPreprocessingRecords++);
1756 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1757 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1758 AddIdentifierRef(MI->getName(), Record);
1759 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1760 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1761 continue;
1762 }
1763
1764 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1765 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1766 Record.push_back(IndexBase + NumPreprocessingRecords++);
1767 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1768 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1769 Record.push_back(ID->getFileName().size());
1770 Record.push_back(ID->wasInQuotes());
1771 Record.push_back(static_cast<unsigned>(ID->getKind()));
1772 llvm::SmallString<64> Buffer;
1773 Buffer += ID->getFileName();
1774 Buffer += ID->getFile()->getName();
1775 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1776 continue;
1777 }
1778
1779 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1780 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001781 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001782
Douglas Gregoraae92242010-03-19 21:51:54 +00001783 // Write the offsets table for the preprocessing record.
1784 if (NumPreprocessingRecords > 0) {
1785 // Write the offsets table for identifier IDs.
1786 using namespace llvm;
1787 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001788 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1792 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001793
Douglas Gregoraae92242010-03-19 21:51:54 +00001794 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001795 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001796 Record.push_back(NumPreprocessingRecords);
1797 Record.push_back(MacroDefinitionOffsets.size());
1798 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001799 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001800 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1801 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001802}
1803
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001804void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001805 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001806 for (Diagnostic::DiagStatePointsTy::const_iterator
1807 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1808 I != E; ++I) {
1809 const Diagnostic::DiagStatePoint &point = *I;
1810 if (point.Loc.isInvalid())
1811 continue;
1812
1813 Record.push_back(point.Loc.getRawEncoding());
1814 for (Diagnostic::DiagState::iterator
1815 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1816 unsigned diag = I->first, map = I->second;
1817 if (map & 0x10) { // mapping from a diagnostic pragma.
1818 Record.push_back(diag);
1819 Record.push_back(map & 0x7);
1820 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001821 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001822 Record.push_back(-1); // mark the end of the diag/map pairs for this
1823 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001824 }
1825
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001826 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001827 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001828}
1829
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001830void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1831 if (CXXBaseSpecifiersOffsets.empty())
1832 return;
1833
1834 RecordData Record;
1835
1836 // Create a blob abbreviation for the C++ base specifiers offsets.
1837 using namespace llvm;
1838
1839 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1840 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1843 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1844
1845 // Write the selector offsets table.
1846 Record.clear();
1847 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1848 Record.push_back(CXXBaseSpecifiersOffsets.size());
1849 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
1850 (const char *)CXXBaseSpecifiersOffsets.data(),
1851 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
1852}
1853
Douglas Gregorc5046832009-04-27 18:38:38 +00001854//===----------------------------------------------------------------------===//
1855// Type Serialization
1856//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001857
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001858/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001859void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001860 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001861 if (Idx.getIndex() == 0) // we haven't seen this type before.
1862 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001863
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001864 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001865
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001866 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001867 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001868 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001869 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001870 else if (TypeOffsets.size() < Index) {
1871 TypeOffsets.resize(Index + 1);
1872 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001873 }
1874
1875 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001876
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001877 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001878 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001879
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001880 if (T.hasLocalNonFastQualifiers()) {
1881 Qualifiers Qs = T.getLocalQualifiers();
1882 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001883 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001884 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001885 } else {
1886 switch (T->getTypeClass()) {
1887 // For all of the concrete, non-dependent types, call the
1888 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001889#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001890 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001891#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001892#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001893 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001894 }
1895
1896 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001897 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001898
1899 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001900 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001901}
1902
Douglas Gregorc5046832009-04-27 18:38:38 +00001903//===----------------------------------------------------------------------===//
1904// Declaration Serialization
1905//===----------------------------------------------------------------------===//
1906
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001907/// \brief Write the block containing all of the declaration IDs
1908/// lexically declared within the given DeclContext.
1909///
1910/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1911/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001912uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001913 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001914 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001915 return 0;
1916
Douglas Gregor8f45df52009-04-16 22:23:12 +00001917 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001918 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001919 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001920 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001921 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1922 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001923 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001924
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001925 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001926 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001927 reinterpret_cast<char*>(Decls.data()),
1928 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001929 return Offset;
1930}
1931
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001932void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001933 using namespace llvm;
1934 RecordData Record;
1935
1936 // Write the type offsets array
1937 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001938 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1941 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1942 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001943 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001944 Record.push_back(TypeOffsets.size());
1945 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001946 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001947 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1948
1949 // Write the declaration offsets array
1950 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001951 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1954 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1955 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001956 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001957 Record.push_back(DeclOffsets.size());
1958 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001959 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001960 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1961}
1962
Douglas Gregorc5046832009-04-27 18:38:38 +00001963//===----------------------------------------------------------------------===//
1964// Global Method Pool and Selector Serialization
1965//===----------------------------------------------------------------------===//
1966
Douglas Gregore84a9da2009-04-20 20:36:09 +00001967namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001968// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001969class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001970 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001971
1972public:
1973 typedef Selector key_type;
1974 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001975
Sebastian Redl834bb972010-08-04 17:20:04 +00001976 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001977 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001978 ObjCMethodList Instance, Factory;
1979 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001980 typedef const data_type& data_type_ref;
1981
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001982 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001983
Douglas Gregorc78d3462009-04-24 21:10:55 +00001984 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001985 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001986 }
Mike Stump11289f42009-09-09 15:08:12 +00001987
1988 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001989 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1990 data_type_ref Methods) {
1991 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1992 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001993 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1994 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001995 Method = Method->Next)
1996 if (Method->Method)
1997 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001998 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001999 Method = Method->Next)
2000 if (Method->Method)
2001 DataLen += 4;
2002 clang::io::Emit16(Out, DataLen);
2003 return std::make_pair(KeyLen, DataLen);
2004 }
Mike Stump11289f42009-09-09 15:08:12 +00002005
Douglas Gregor95c13f52009-04-25 17:48:32 +00002006 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002007 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002008 assert((Start >> 32) == 0 && "Selector key offset too large");
2009 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002010 unsigned N = Sel.getNumArgs();
2011 clang::io::Emit16(Out, N);
2012 if (N == 0)
2013 N = 1;
2014 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002015 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002016 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2017 }
Mike Stump11289f42009-09-09 15:08:12 +00002018
Douglas Gregorc78d3462009-04-24 21:10:55 +00002019 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002020 data_type_ref Methods, unsigned DataLen) {
2021 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002022 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002023 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002024 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002025 Method = Method->Next)
2026 if (Method->Method)
2027 ++NumInstanceMethods;
2028
2029 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002030 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002031 Method = Method->Next)
2032 if (Method->Method)
2033 ++NumFactoryMethods;
2034
2035 clang::io::Emit16(Out, NumInstanceMethods);
2036 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002037 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002038 Method = Method->Next)
2039 if (Method->Method)
2040 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002041 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002042 Method = Method->Next)
2043 if (Method->Method)
2044 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002045
2046 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002047 }
2048};
2049} // end anonymous namespace
2050
Sebastian Redla19a67f2010-08-03 21:58:15 +00002051/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002052///
2053/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002054/// in an on-disk hash table indexed by the selector. The hash table also
2055/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002056void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002057 using namespace llvm;
2058
Sebastian Redla19a67f2010-08-03 21:58:15 +00002059 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002060 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002061 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002062 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002063 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002064 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002065 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002066 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002067
Sebastian Redla19a67f2010-08-03 21:58:15 +00002068 // Create the on-disk hash table representation. We walk through every
2069 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002070 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002071 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002072 I = SelectorIDs.begin(), E = SelectorIDs.end();
2073 I != E; ++I) {
2074 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002075 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002076 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002077 I->second,
2078 ObjCMethodList(),
2079 ObjCMethodList()
2080 };
2081 if (F != SemaRef.MethodPool.end()) {
2082 Data.Instance = F->second.first;
2083 Data.Factory = F->second.second;
2084 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002085 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002086 // changed.
2087 if (Chain && I->second < FirstSelectorID) {
2088 // Selector already exists. Did it change?
2089 bool changed = false;
2090 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2091 M = M->Next) {
2092 if (M->Method->getPCHLevel() == 0)
2093 changed = true;
2094 }
2095 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2096 M = M->Next) {
2097 if (M->Method->getPCHLevel() == 0)
2098 changed = true;
2099 }
2100 if (!changed)
2101 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002102 } else if (Data.Instance.Method || Data.Factory.Method) {
2103 // A new method pool entry.
2104 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002105 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002106 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002107 }
2108
Douglas Gregorc78d3462009-04-24 21:10:55 +00002109 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002110 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002111 uint32_t BucketOffset;
2112 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002113 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002114 llvm::raw_svector_ostream Out(MethodPool);
2115 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002116 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002117 BucketOffset = Generator.Emit(Out, Trait);
2118 }
2119
2120 // Create a blob abbreviation
2121 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002122 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2126 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2127
Douglas Gregor95c13f52009-04-25 17:48:32 +00002128 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002129 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002130 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002131 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002132 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002133 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002134
2135 // Create a blob abbreviation for the selector table offsets.
2136 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002137 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2140 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2141
2142 // Write the selector offsets table.
2143 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002144 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002145 Record.push_back(SelectorOffsets.size());
2146 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002147 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002148 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002149 }
2150}
2151
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002152/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002153void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002154 using namespace llvm;
2155 if (SemaRef.ReferencedSelectors.empty())
2156 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002157
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002158 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002159
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002160 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002161 // very tricky to fix, and given that @selector shouldn't really appear in
2162 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002163 for (DenseMap<Selector, SourceLocation>::iterator S =
2164 SemaRef.ReferencedSelectors.begin(),
2165 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2166 Selector Sel = (*S).first;
2167 SourceLocation Loc = (*S).second;
2168 AddSelectorRef(Sel, Record);
2169 AddSourceLocation(Loc, Record);
2170 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002171 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002172}
2173
Douglas Gregorc5046832009-04-27 18:38:38 +00002174//===----------------------------------------------------------------------===//
2175// Identifier Table Serialization
2176//===----------------------------------------------------------------------===//
2177
Douglas Gregorc78d3462009-04-24 21:10:55 +00002178namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002179class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002180 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002181 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002182
Douglas Gregor1d583f22009-04-28 21:18:29 +00002183 /// \brief Determines whether this is an "interesting" identifier
2184 /// that needs a full IdentifierInfo structure written into the hash
2185 /// table.
2186 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2187 return II->isPoisoned() ||
2188 II->isExtensionToken() ||
2189 II->hasMacroDefinition() ||
2190 II->getObjCOrBuiltinID() ||
2191 II->getFETokenInfo<void>();
2192 }
2193
Douglas Gregore84a9da2009-04-20 20:36:09 +00002194public:
2195 typedef const IdentifierInfo* key_type;
2196 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002197
Sebastian Redl539c5062010-08-18 23:57:32 +00002198 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002199 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002201 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002202 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002203
2204 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002205 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002206 }
Mike Stump11289f42009-09-09 15:08:12 +00002207
2208 std::pair<unsigned,unsigned>
2209 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002210 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002211 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002212 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2213 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002214 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002215 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002216 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002217 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002218 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2219 DEnd = IdentifierResolver::end();
2220 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002221 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002222 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002223 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002224 // We emit the key length after the data length so that every
2225 // string is preceded by a 16-bit length. This matches the PTH
2226 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002227 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002228 return std::make_pair(KeyLen, DataLen);
2229 }
Mike Stump11289f42009-09-09 15:08:12 +00002230
2231 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002232 unsigned KeyLen) {
2233 // Record the location of the key data. This is used when generating
2234 // the mapping from persistent IDs to strings.
2235 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002236 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002237 }
Mike Stump11289f42009-09-09 15:08:12 +00002238
2239 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002240 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002241 if (!isInterestingIdentifier(II)) {
2242 clang::io::Emit32(Out, ID << 1);
2243 return;
2244 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002245
Douglas Gregor1d583f22009-04-28 21:18:29 +00002246 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002247 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002248 bool hasMacroDefinition =
2249 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002250 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002251 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002252 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2253 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2254 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002255 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002256 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002257 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002258
Douglas Gregorc3366a52009-04-21 23:56:24 +00002259 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002260 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002261
Douglas Gregora868bbd2009-04-21 22:25:48 +00002262 // Emit the declaration IDs in reverse order, because the
2263 // IdentifierResolver provides the declarations as they would be
2264 // visible (e.g., the function "stat" would come before the struct
2265 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2266 // adds declarations to the end of the list (so we need to see the
2267 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002268 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002269 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002270 IdentifierResolver::end());
2271 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2272 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002273 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002274 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002275 }
2276};
2277} // end anonymous namespace
2278
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002279/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002280///
2281/// The identifier table consists of a blob containing string data
2282/// (the actual identifiers themselves) and a separate "offsets" index
2283/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002284void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002285 using namespace llvm;
2286
2287 // Create and write out the blob that contains the identifier
2288 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002289 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002290 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002291 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002292
Douglas Gregore6648fb2009-04-28 20:33:11 +00002293 // Look for any identifiers that were named while processing the
2294 // headers, but are otherwise not needed. We add these to the hash
2295 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002296 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002297 // file.
2298 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2299 IDEnd = PP.getIdentifierTable().end();
2300 ID != IDEnd; ++ID)
2301 getIdentifierRef(ID->second);
2302
Sebastian Redlff4a2952010-07-23 23:49:55 +00002303 // Create the on-disk hash table representation. We only store offsets
2304 // for identifiers that appear here for the first time.
2305 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002306 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002307 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2308 ID != IDEnd; ++ID) {
2309 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002310 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002311 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002312 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002313
Douglas Gregore84a9da2009-04-20 20:36:09 +00002314 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002315 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002316 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002317 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002318 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002319 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002320 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002321 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002322 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002323 }
2324
2325 // Create a blob abbreviation
2326 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002327 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002330 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002331
2332 // Write the identifier table
2333 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002334 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002335 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002336 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002337 }
2338
2339 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002340 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002341 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2343 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2344 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2345
2346 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002347 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002348 Record.push_back(IdentifierOffsets.size());
2349 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002350 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002351 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002352}
2353
Douglas Gregorc5046832009-04-27 18:38:38 +00002354//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002355// DeclContext's Name Lookup Table Serialization
2356//===----------------------------------------------------------------------===//
2357
2358namespace {
2359// Trait used for the on-disk hash table used in the method pool.
2360class ASTDeclContextNameLookupTrait {
2361 ASTWriter &Writer;
2362
2363public:
2364 typedef DeclarationName key_type;
2365 typedef key_type key_type_ref;
2366
2367 typedef DeclContext::lookup_result data_type;
2368 typedef const data_type& data_type_ref;
2369
2370 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2371
2372 unsigned ComputeHash(DeclarationName Name) {
2373 llvm::FoldingSetNodeID ID;
2374 ID.AddInteger(Name.getNameKind());
2375
2376 switch (Name.getNameKind()) {
2377 case DeclarationName::Identifier:
2378 ID.AddString(Name.getAsIdentifierInfo()->getName());
2379 break;
2380 case DeclarationName::ObjCZeroArgSelector:
2381 case DeclarationName::ObjCOneArgSelector:
2382 case DeclarationName::ObjCMultiArgSelector:
2383 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2384 break;
2385 case DeclarationName::CXXConstructorName:
2386 case DeclarationName::CXXDestructorName:
2387 case DeclarationName::CXXConversionFunctionName:
2388 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2389 break;
2390 case DeclarationName::CXXOperatorName:
2391 ID.AddInteger(Name.getCXXOverloadedOperator());
2392 break;
2393 case DeclarationName::CXXLiteralOperatorName:
2394 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2395 case DeclarationName::CXXUsingDirective:
2396 break;
2397 }
2398
2399 return ID.ComputeHash();
2400 }
2401
2402 std::pair<unsigned,unsigned>
2403 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2404 data_type_ref Lookup) {
2405 unsigned KeyLen = 1;
2406 switch (Name.getNameKind()) {
2407 case DeclarationName::Identifier:
2408 case DeclarationName::ObjCZeroArgSelector:
2409 case DeclarationName::ObjCOneArgSelector:
2410 case DeclarationName::ObjCMultiArgSelector:
2411 case DeclarationName::CXXConstructorName:
2412 case DeclarationName::CXXDestructorName:
2413 case DeclarationName::CXXConversionFunctionName:
2414 case DeclarationName::CXXLiteralOperatorName:
2415 KeyLen += 4;
2416 break;
2417 case DeclarationName::CXXOperatorName:
2418 KeyLen += 1;
2419 break;
2420 case DeclarationName::CXXUsingDirective:
2421 break;
2422 }
2423 clang::io::Emit16(Out, KeyLen);
2424
2425 // 2 bytes for num of decls and 4 for each DeclID.
2426 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2427 clang::io::Emit16(Out, DataLen);
2428
2429 return std::make_pair(KeyLen, DataLen);
2430 }
2431
2432 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2433 using namespace clang::io;
2434
2435 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2436 Emit8(Out, Name.getNameKind());
2437 switch (Name.getNameKind()) {
2438 case DeclarationName::Identifier:
2439 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2440 break;
2441 case DeclarationName::ObjCZeroArgSelector:
2442 case DeclarationName::ObjCOneArgSelector:
2443 case DeclarationName::ObjCMultiArgSelector:
2444 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2445 break;
2446 case DeclarationName::CXXConstructorName:
2447 case DeclarationName::CXXDestructorName:
2448 case DeclarationName::CXXConversionFunctionName:
2449 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2450 break;
2451 case DeclarationName::CXXOperatorName:
2452 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2453 Emit8(Out, Name.getCXXOverloadedOperator());
2454 break;
2455 case DeclarationName::CXXLiteralOperatorName:
2456 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2457 break;
2458 case DeclarationName::CXXUsingDirective:
2459 break;
2460 }
2461 }
2462
2463 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2464 data_type Lookup, unsigned DataLen) {
2465 uint64_t Start = Out.tell(); (void)Start;
2466 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2467 for (; Lookup.first != Lookup.second; ++Lookup.first)
2468 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2469
2470 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2471 }
2472};
2473} // end anonymous namespace
2474
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002475/// \brief Write the block containing all of the declaration IDs
2476/// visible from the given DeclContext.
2477///
2478/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002479/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002480uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2481 DeclContext *DC) {
2482 if (DC->getPrimaryContext() != DC)
2483 return 0;
2484
2485 // Since there is no name lookup into functions or methods, don't bother to
2486 // build a visible-declarations table for these entities.
2487 if (DC->isFunctionOrMethod())
2488 return 0;
2489
2490 // If not in C++, we perform name lookup for the translation unit via the
2491 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2492 // FIXME: In C++ we need the visible declarations in order to "see" the
2493 // friend declarations, is there a way to do this without writing the table ?
2494 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2495 return 0;
2496
2497 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002498 if (DC->hasExternalVisibleStorage())
2499 DC->MaterializeVisibleDeclsFromExternalStorage();
2500 else
2501 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002502
2503 // Serialize the contents of the mapping used for lookup. Note that,
2504 // although we have two very different code paths, the serialized
2505 // representation is the same for both cases: a declaration name,
2506 // followed by a size, followed by references to the visible
2507 // declarations that have that name.
2508 uint64_t Offset = Stream.GetCurrentBitNo();
2509 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2510 if (!Map || Map->empty())
2511 return 0;
2512
2513 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2514 ASTDeclContextNameLookupTrait Trait(*this);
2515
2516 // Create the on-disk hash table representation.
2517 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2518 D != DEnd; ++D) {
2519 DeclarationName Name = D->first;
2520 DeclContext::lookup_result Result = D->second.getLookupResult();
2521 Generator.insert(Name, Result, Trait);
2522 }
2523
2524 // Create the on-disk hash table in a buffer.
2525 llvm::SmallString<4096> LookupTable;
2526 uint32_t BucketOffset;
2527 {
2528 llvm::raw_svector_ostream Out(LookupTable);
2529 // Make sure that no bucket is at offset 0
2530 clang::io::Emit32(Out, 0);
2531 BucketOffset = Generator.Emit(Out, Trait);
2532 }
2533
2534 // Write the lookup table
2535 RecordData Record;
2536 Record.push_back(DECL_CONTEXT_VISIBLE);
2537 Record.push_back(BucketOffset);
2538 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2539 LookupTable.str());
2540
2541 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2542 ++NumVisibleDeclContexts;
2543 return Offset;
2544}
2545
Sebastian Redla4071b42010-08-24 00:50:09 +00002546/// \brief Write an UPDATE_VISIBLE block for the given context.
2547///
2548/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2549/// DeclContext in a dependent AST file. As such, they only exist for the TU
2550/// (in C++) and for namespaces.
2551void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002552 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2553 if (!Map || Map->empty())
2554 return;
2555
2556 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2557 ASTDeclContextNameLookupTrait Trait(*this);
2558
2559 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002560 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2561 D != DEnd; ++D) {
2562 DeclarationName Name = D->first;
2563 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002564 // For any name that appears in this table, the results are complete, i.e.
2565 // they overwrite results from previous PCHs. Merging is always a mess.
2566 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002567 }
2568
2569 // Create the on-disk hash table in a buffer.
2570 llvm::SmallString<4096> LookupTable;
2571 uint32_t BucketOffset;
2572 {
2573 llvm::raw_svector_ostream Out(LookupTable);
2574 // Make sure that no bucket is at offset 0
2575 clang::io::Emit32(Out, 0);
2576 BucketOffset = Generator.Emit(Out, Trait);
2577 }
2578
2579 // Write the lookup table
2580 RecordData Record;
2581 Record.push_back(UPDATE_VISIBLE);
2582 Record.push_back(getDeclID(cast<Decl>(DC)));
2583 Record.push_back(BucketOffset);
2584 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2585}
2586
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002587/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2588void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2589 RecordData Record;
2590 Record.push_back(Opts.fp_contract);
2591 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2592}
2593
2594/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2595void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2596 if (!SemaRef.Context.getLangOptions().OpenCL)
2597 return;
2598
2599 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2600 RecordData Record;
2601#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2602#include "clang/Basic/OpenCLExtensions.def"
2603 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2604}
2605
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002606//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002607// General Serialization Routines
2608//===----------------------------------------------------------------------===//
2609
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002610/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002611void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002612 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002613 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2614 const Attr * A = *i;
2615 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2616 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002617
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002618#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002619
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002620 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002621}
2622
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002623void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002624 Record.push_back(Str.size());
2625 Record.insert(Record.end(), Str.begin(), Str.end());
2626}
2627
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002628void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2629 RecordDataImpl &Record) {
2630 Record.push_back(Version.getMajor());
2631 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2632 Record.push_back(*Minor + 1);
2633 else
2634 Record.push_back(0);
2635 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2636 Record.push_back(*Subminor + 1);
2637 else
2638 Record.push_back(0);
2639}
2640
Douglas Gregore84a9da2009-04-20 20:36:09 +00002641/// \brief Note that the identifier II occurs at the given offset
2642/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002643void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002644 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002645 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002646 // up earlier in the chain and thus don't need an offset.
2647 if (ID >= FirstIdentID)
2648 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002649}
2650
Douglas Gregor95c13f52009-04-25 17:48:32 +00002651/// \brief Note that the selector Sel occurs at the given offset
2652/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002653void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002654 unsigned ID = SelectorIDs[Sel];
2655 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002656 // Don't record offsets for selectors that are also available in a different
2657 // file.
2658 if (ID < FirstSelectorID)
2659 return;
2660 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002661}
2662
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002663ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002664 : Stream(Stream), Chain(0), SerializationListener(0),
2665 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002666 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002667 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002668 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2669 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002670 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002671 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2672 NextCXXBaseSpecifiersID(1)
2673{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002674}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002675
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002676void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002677 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002678 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002679 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002680 Stream.Emit((unsigned)'C', 8);
2681 Stream.Emit((unsigned)'P', 8);
2682 Stream.Emit((unsigned)'C', 8);
2683 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002684
Chris Lattner28fa4e62009-04-26 22:26:21 +00002685 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002686
Sebastian Redl143413f2010-07-12 22:02:52 +00002687 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002688 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002689 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002690 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002691}
2692
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002693void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002694 const char *isysroot,
2695 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002696 using namespace llvm;
2697
2698 ASTContext &Context = SemaRef.Context;
2699 Preprocessor &PP = SemaRef.PP;
2700
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002701 // The translation unit is the first declaration we'll emit.
2702 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002703 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002704 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002705
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002706 // Make sure that we emit IdentifierInfos (and any attached
2707 // declarations) for builtins.
2708 {
2709 IdentifierTable &Table = PP.getIdentifierTable();
2710 llvm::SmallVector<const char *, 32> BuiltinNames;
2711 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2712 Context.getLangOptions().NoBuiltin);
2713 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2714 getIdentifierRef(&Table.get(BuiltinNames[I]));
2715 }
2716
Chris Lattner0c797362009-09-08 18:19:27 +00002717 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002718 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002719 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002720 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002721 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2722 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002723 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002724
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002725 // Build a record containing all of the file scoped decls in this file.
2726 RecordData UnusedFileScopedDecls;
2727 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2728 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002729
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002730 RecordData WeakUndeclaredIdentifiers;
2731 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2732 WeakUndeclaredIdentifiers.push_back(
2733 SemaRef.WeakUndeclaredIdentifiers.size());
2734 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2735 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2736 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2737 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2738 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2739 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2740 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2741 }
2742 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002743
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002744 // Build a record containing all of the locally-scoped external
2745 // declarations in this header file. Generally, this record will be
2746 // empty.
2747 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002748 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002749 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002750 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002751 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2752 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2753 TD != TDEnd; ++TD)
2754 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2755
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002756 // Build a record containing all of the ext_vector declarations.
2757 RecordData ExtVectorDecls;
2758 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2759 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2760
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002761 // Build a record containing all of the VTable uses information.
2762 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002763 if (!SemaRef.VTableUses.empty()) {
2764 VTableUses.push_back(SemaRef.VTableUses.size());
2765 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2766 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2767 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2768 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2769 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002770 }
2771
2772 // Build a record containing all of dynamic classes declarations.
2773 RecordData DynamicClasses;
2774 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2775 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2776
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002777 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002778 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002779 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002780 I = SemaRef.PendingInstantiations.begin(),
2781 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2782 AddDeclRef(I->first, PendingInstantiations);
2783 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002784 }
2785 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2786 "There are local ones at end of translation unit!");
2787
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002788 // Build a record containing some declaration references.
2789 RecordData SemaDeclRefs;
2790 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2791 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2792 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2793 }
2794
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002795 RecordData CUDASpecialDeclRefs;
2796 if (Context.getcudaConfigureCallDecl()) {
2797 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2798 }
2799
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002800 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002801 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002802 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002803 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002804 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002805 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002806 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002807 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002808 // Write the record of special types.
2809 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002810
Steve Naroffc277ad12009-07-18 15:33:26 +00002811 AddTypeRef(Context.getBuiltinVaListType(), Record);
2812 AddTypeRef(Context.getObjCIdType(), Record);
2813 AddTypeRef(Context.getObjCSelType(), Record);
2814 AddTypeRef(Context.getObjCProtoType(), Record);
2815 AddTypeRef(Context.getObjCClassType(), Record);
2816 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2817 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2818 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002819 AddTypeRef(Context.getjmp_bufType(), Record);
2820 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002821 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2822 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002823 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002824 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002825 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2826 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002827 Record.push_back(Context.isInt128Installed());
Richard Smith02e85f32011-04-14 22:09:26 +00002828 AddTypeRef(Context.AutoDeductTy, Record);
2829 AddTypeRef(Context.AutoRRefDeductTy, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00002830 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002831
Douglas Gregor1970d882009-04-26 03:49:13 +00002832 // Keep writing types and declarations until all types and
2833 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002834 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002835 WriteDeclsBlockAbbrevs();
2836 while (!DeclTypesToEmit.empty()) {
2837 DeclOrType DOT = DeclTypesToEmit.front();
2838 DeclTypesToEmit.pop();
2839 if (DOT.isType())
2840 WriteType(DOT.getType());
2841 else
2842 WriteDecl(Context, DOT.getDecl());
2843 }
2844 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002845
Douglas Gregor45053152009-10-17 17:25:45 +00002846 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002847 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002848 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002849 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002850 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002851 WriteFPPragmaOptions(SemaRef.getFPOptions());
2852 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002853
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002854 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002855 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002856
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002857 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002858
Douglas Gregord4df8652009-04-22 22:02:47 +00002859 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002860 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002861 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002862
2863 // Write the record containing tentative definitions.
2864 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002865 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002866
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002867 // Write the record containing unused file scoped decls.
2868 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002869 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002870
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002871 // Write the record containing weak undeclared identifiers.
2872 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002873 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002874 WeakUndeclaredIdentifiers);
2875
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002876 // Write the record containing locally-scoped external definitions.
2877 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002878 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002879 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002880
2881 // Write the record containing ext_vector type names.
2882 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002883 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002884
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002885 // Write the record containing VTable uses information.
2886 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002887 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002888
2889 // Write the record containing dynamic classes declarations.
2890 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002891 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002892
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002893 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002894 if (!PendingInstantiations.empty())
2895 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002896
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002897 // Write the record containing declaration references of Sema.
2898 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002899 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002900
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002901 // Write the record containing CUDA-specific declaration references.
2902 if (!CUDASpecialDeclRefs.empty())
2903 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2904
Douglas Gregor08f01292009-04-17 22:13:46 +00002905 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002906 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002907 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002908 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002909 Record.push_back(NumLexicalDeclContexts);
2910 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002911 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002912 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002913}
2914
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002915void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002916 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002917 using namespace llvm;
2918
2919 ASTContext &Context = SemaRef.Context;
2920 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002921
Sebastian Redl143413f2010-07-12 22:02:52 +00002922 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002923 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002924 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002925 if (StatCalls && !isysroot)
2926 WriteStatCache(*StatCalls);
2927 // FIXME: Source manager block should only write new stuff, which could be
2928 // done by tracking the largest ID in the chain
2929 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002930
2931 // The special types are in the chained PCH.
2932
2933 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002934 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002935 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002936 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002937 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2938 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002939 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002940 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002941 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002942 else if ((*I)->isChangedSinceDeserialization())
2943 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002944 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002945 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002946 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002947 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002948 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2949 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2950 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002951 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002952 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2953 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002954 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002955 // And a visible updates block for the DeclContexts.
2956 Abv = new llvm::BitCodeAbbrev();
2957 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2958 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2959 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2960 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2961 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2962 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002963
Sebastian Redl98912122010-07-27 23:01:28 +00002964 // Build a record containing all of the new tentative definitions in this
2965 // file, in TentativeDefinitions order.
2966 RecordData TentativeDefinitions;
2967 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2968 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2969 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2970 }
2971
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002972 // Build a record containing all of the file scoped decls in this file.
2973 RecordData UnusedFileScopedDecls;
2974 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2975 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2976 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002977 }
2978
Sebastian Redl08aca90252010-08-05 18:21:25 +00002979 // We write the entire table, overwriting the tables from the chain.
2980 RecordData WeakUndeclaredIdentifiers;
2981 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2982 WeakUndeclaredIdentifiers.push_back(
2983 SemaRef.WeakUndeclaredIdentifiers.size());
2984 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2985 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2986 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2987 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2988 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2989 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2990 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2991 }
2992 }
2993
Sebastian Redl98912122010-07-27 23:01:28 +00002994 // Build a record containing all of the locally-scoped external
2995 // declarations in this header file. Generally, this record will be
2996 // empty.
2997 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002998 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002999 // nondeterminstic!
3000 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3001 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3002 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3003 TD != TDEnd; ++TD) {
3004 if (TD->second->getPCHLevel() == 0)
3005 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3006 }
3007
3008 // Build a record containing all of the ext_vector declarations.
3009 RecordData ExtVectorDecls;
3010 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3011 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3012 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3013 }
3014
Sebastian Redl08aca90252010-08-05 18:21:25 +00003015 // Build a record containing all of the VTable uses information.
3016 // We write everything here, because it's too hard to determine whether
3017 // a use is new to this part.
3018 RecordData VTableUses;
3019 if (!SemaRef.VTableUses.empty()) {
3020 VTableUses.push_back(SemaRef.VTableUses.size());
3021 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3022 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3023 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3024 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3025 }
3026 }
3027
3028 // Build a record containing all of dynamic classes declarations.
3029 RecordData DynamicClasses;
3030 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3031 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3032 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3033
3034 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003035 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003036 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003037 I = SemaRef.PendingInstantiations.begin(),
3038 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00003039 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00003040 AddDeclRef(I->first, PendingInstantiations);
3041 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003042 }
3043 }
3044 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3045 "There are local ones at end of translation unit!");
3046
3047 // Build a record containing some declaration references.
3048 // It's not worth the effort to avoid duplication here.
3049 RecordData SemaDeclRefs;
3050 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3051 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3052 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3053 }
3054
Sebastian Redl539c5062010-08-18 23:57:32 +00003055 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003056 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003057 for (DeclsToRewriteTy::iterator
3058 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3059 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003060 while (!DeclTypesToEmit.empty()) {
3061 DeclOrType DOT = DeclTypesToEmit.front();
3062 DeclTypesToEmit.pop();
3063 if (DOT.isType())
3064 WriteType(DOT.getType());
3065 else
3066 WriteDecl(Context, DOT.getDecl());
3067 }
3068 Stream.ExitBlock();
3069
Sebastian Redl98912122010-07-27 23:01:28 +00003070 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003071 WriteSelectors(SemaRef);
3072 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003073 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003074 WriteFPPragmaOptions(SemaRef.getFPOptions());
3075 WriteOpenCLExtensions(SemaRef);
3076
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003077 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003078 // FIXME: For chained PCH only write the new mappings (we currently
3079 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003080 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003081
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003082 WriteCXXBaseSpecifiersOffsets();
3083
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003084 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003085 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003086 RecordData FirstLatestDeclIDs;
3087 for (FirstLatestDeclMap::iterator
3088 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3089 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3090 "Expected first & second to be in different PCHs");
3091 AddDeclRef(I->first, FirstLatestDeclIDs);
3092 AddDeclRef(I->second, FirstLatestDeclIDs);
3093 }
3094 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003095 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003096
Sebastian Redl98912122010-07-27 23:01:28 +00003097 // Write the record containing external, unnamed definitions.
3098 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003099 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003100
3101 // Write the record containing tentative definitions.
3102 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003103 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003104
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003105 // Write the record containing unused file scoped decls.
3106 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003107 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003108
Sebastian Redl08aca90252010-08-05 18:21:25 +00003109 // Write the record containing weak undeclared identifiers.
3110 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003111 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003112 WeakUndeclaredIdentifiers);
3113
Sebastian Redl98912122010-07-27 23:01:28 +00003114 // Write the record containing locally-scoped external definitions.
3115 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003116 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003117 LocallyScopedExternalDecls);
3118
3119 // Write the record containing ext_vector type names.
3120 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003121 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003122
Sebastian Redl08aca90252010-08-05 18:21:25 +00003123 // Write the record containing VTable uses information.
3124 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003125 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003126
3127 // Write the record containing dynamic classes declarations.
3128 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003129 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003130
3131 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003132 if (!PendingInstantiations.empty())
3133 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003134
3135 // Write the record containing declaration references of Sema.
3136 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003137 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003138
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003139 // Write the updates to DeclContexts.
3140 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3141 I = UpdatedDeclContexts.begin(),
3142 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003143 I != E; ++I)
3144 WriteDeclContextVisibleUpdate(*I);
3145
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003146 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003147
Sebastian Redl98912122010-07-27 23:01:28 +00003148 Record.clear();
3149 Record.push_back(NumStatements);
3150 Record.push_back(NumMacros);
3151 Record.push_back(NumLexicalDeclContexts);
3152 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003153 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003154 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003155 Stream.ExitBlock();
3156}
3157
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003158void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003159 if (DeclUpdates.empty())
3160 return;
3161
3162 RecordData OffsetsRecord;
3163 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3164 for (DeclUpdateMap::iterator
3165 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3166 const Decl *D = I->first;
3167 UpdateRecord &URec = I->second;
3168
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003169 if (DeclsToRewrite.count(D))
3170 continue; // The decl will be written completely,no need to store updates.
3171
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003172 uint64_t Offset = Stream.GetCurrentBitNo();
3173 Stream.EmitRecord(DECL_UPDATES, URec);
3174
3175 OffsetsRecord.push_back(GetDeclRef(D));
3176 OffsetsRecord.push_back(Offset);
3177 }
3178 Stream.ExitBlock();
3179 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3180}
3181
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003182void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003183 if (ReplacedDecls.empty())
3184 return;
3185
3186 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003187 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003188 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3189 Record.push_back(I->first);
3190 Record.push_back(I->second);
3191 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003192 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003193}
3194
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003195void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003196 Record.push_back(Loc.getRawEncoding());
3197}
3198
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003199void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003200 AddSourceLocation(Range.getBegin(), Record);
3201 AddSourceLocation(Range.getEnd(), Record);
3202}
3203
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003204void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003205 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003206 const uint64_t *Words = Value.getRawData();
3207 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003208}
3209
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003210void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003211 Record.push_back(Value.isUnsigned());
3212 AddAPInt(Value, Record);
3213}
3214
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003215void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003216 AddAPInt(Value.bitcastToAPInt(), Record);
3217}
3218
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003219void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003220 Record.push_back(getIdentifierRef(II));
3221}
3222
Sebastian Redl539c5062010-08-18 23:57:32 +00003223IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003224 if (II == 0)
3225 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003226
Sebastian Redl539c5062010-08-18 23:57:32 +00003227 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003228 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003229 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003230 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003231}
3232
Sebastian Redl50e26582010-09-15 19:54:06 +00003233MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003234 if (MD == 0)
3235 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003236
3237 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003238 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003239 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003240 return ID;
3241}
3242
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003243void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003244 Record.push_back(getSelectorRef(SelRef));
3245}
3246
Sebastian Redl539c5062010-08-18 23:57:32 +00003247SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003248 if (Sel.getAsOpaquePtr() == 0) {
3249 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003250 }
3251
Sebastian Redl539c5062010-08-18 23:57:32 +00003252 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003253 if (SID == 0 && Chain) {
3254 // This might trigger a ReadSelector callback, which will set the ID for
3255 // this selector.
3256 Chain->LoadSelector(Sel);
3257 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003258 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003259 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003260 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003261 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003262}
3263
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003264void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003265 AddDeclRef(Temp->getDestructor(), Record);
3266}
3267
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003268void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3269 CXXBaseSpecifier const *BasesEnd,
3270 RecordDataImpl &Record) {
3271 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3272 CXXBaseSpecifiersToWrite.push_back(
3273 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3274 Bases, BasesEnd));
3275 Record.push_back(NextCXXBaseSpecifiersID++);
3276}
3277
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003278void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003279 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003280 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003281 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003282 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003283 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003284 break;
3285 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003286 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003287 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003288 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003289 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003290 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003291 break;
3292 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003293 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003294 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003295 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003296 break;
John McCall0ad16662009-10-29 08:12:44 +00003297 case TemplateArgument::Null:
3298 case TemplateArgument::Integral:
3299 case TemplateArgument::Declaration:
3300 case TemplateArgument::Pack:
3301 break;
3302 }
3303}
3304
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003305void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003306 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003307 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003308
3309 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3310 bool InfoHasSameExpr
3311 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3312 Record.push_back(InfoHasSameExpr);
3313 if (InfoHasSameExpr)
3314 return; // Avoid storing the same expr twice.
3315 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003316 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3317 Record);
3318}
3319
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003320void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3321 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003322 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003323 AddTypeRef(QualType(), Record);
3324 return;
3325 }
3326
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003327 AddTypeLoc(TInfo->getTypeLoc(), Record);
3328}
3329
3330void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3331 AddTypeRef(TL.getType(), Record);
3332
John McCall8f115c62009-10-16 21:56:05 +00003333 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003334 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003335 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003336}
3337
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003338void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003339 Record.push_back(GetOrCreateTypeID(T));
3340}
3341
3342TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003343 return MakeTypeID(T,
3344 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3345}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003346
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003347TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003348 return MakeTypeID(T,
3349 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003350}
3351
3352TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3353 if (T.isNull())
3354 return TypeIdx();
3355 assert(!T.getLocalFastQualifiers());
3356
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003357 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003358 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003359 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003360 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003361 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003362 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003363 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003364 return Idx;
3365}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003366
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003367TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003368 if (T.isNull())
3369 return TypeIdx();
3370 assert(!T.getLocalFastQualifiers());
3371
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003372 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3373 assert(I != TypeIdxs.end() && "Type not emitted!");
3374 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003375}
3376
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003377void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003378 Record.push_back(GetDeclRef(D));
3379}
3380
Sebastian Redl539c5062010-08-18 23:57:32 +00003381DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003382 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003383 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003384 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003385 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003386 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003387 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003388 // We haven't seen this declaration before. Give it a new ID and
3389 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003390 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003391 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003392 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3393 // We don't add it to the replacement collection here, because we don't
3394 // have the offset yet.
3395 DeclTypesToEmit.push(const_cast<Decl *>(D));
3396 // Reset the flag, so that we don't add this decl multiple times.
3397 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003398 }
3399
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003400 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003401}
3402
Sebastian Redl539c5062010-08-18 23:57:32 +00003403DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003404 if (D == 0)
3405 return 0;
3406
3407 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3408 return DeclIDs[D];
3409}
3410
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003411void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003412 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003413 Record.push_back(Name.getNameKind());
3414 switch (Name.getNameKind()) {
3415 case DeclarationName::Identifier:
3416 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3417 break;
3418
3419 case DeclarationName::ObjCZeroArgSelector:
3420 case DeclarationName::ObjCOneArgSelector:
3421 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003422 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003423 break;
3424
3425 case DeclarationName::CXXConstructorName:
3426 case DeclarationName::CXXDestructorName:
3427 case DeclarationName::CXXConversionFunctionName:
3428 AddTypeRef(Name.getCXXNameType(), Record);
3429 break;
3430
3431 case DeclarationName::CXXOperatorName:
3432 Record.push_back(Name.getCXXOverloadedOperator());
3433 break;
3434
Alexis Hunt3d221f22009-11-29 07:34:05 +00003435 case DeclarationName::CXXLiteralOperatorName:
3436 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3437 break;
3438
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003439 case DeclarationName::CXXUsingDirective:
3440 // No extra data to emit
3441 break;
3442 }
3443}
Chris Lattnerca025db2010-05-07 21:43:38 +00003444
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003445void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003446 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003447 switch (Name.getNameKind()) {
3448 case DeclarationName::CXXConstructorName:
3449 case DeclarationName::CXXDestructorName:
3450 case DeclarationName::CXXConversionFunctionName:
3451 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3452 break;
3453
3454 case DeclarationName::CXXOperatorName:
3455 AddSourceLocation(
3456 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3457 Record);
3458 AddSourceLocation(
3459 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3460 Record);
3461 break;
3462
3463 case DeclarationName::CXXLiteralOperatorName:
3464 AddSourceLocation(
3465 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3466 Record);
3467 break;
3468
3469 case DeclarationName::Identifier:
3470 case DeclarationName::ObjCZeroArgSelector:
3471 case DeclarationName::ObjCOneArgSelector:
3472 case DeclarationName::ObjCMultiArgSelector:
3473 case DeclarationName::CXXUsingDirective:
3474 break;
3475 }
3476}
3477
3478void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003479 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003480 AddDeclarationName(NameInfo.getName(), Record);
3481 AddSourceLocation(NameInfo.getLoc(), Record);
3482 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3483}
3484
3485void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003486 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003487 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003488 Record.push_back(Info.NumTemplParamLists);
3489 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3490 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3491}
3492
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003493void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003494 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003495 // Nested name specifiers usually aren't too long. I think that 8 would
3496 // typically accomodate the vast majority.
3497 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3498
3499 // Push each of the NNS's onto a stack for serialization in reverse order.
3500 while (NNS) {
3501 NestedNames.push_back(NNS);
3502 NNS = NNS->getPrefix();
3503 }
3504
3505 Record.push_back(NestedNames.size());
3506 while(!NestedNames.empty()) {
3507 NNS = NestedNames.pop_back_val();
3508 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3509 Record.push_back(Kind);
3510 switch (Kind) {
3511 case NestedNameSpecifier::Identifier:
3512 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3513 break;
3514
3515 case NestedNameSpecifier::Namespace:
3516 AddDeclRef(NNS->getAsNamespace(), Record);
3517 break;
3518
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003519 case NestedNameSpecifier::NamespaceAlias:
3520 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3521 break;
3522
Chris Lattnerca025db2010-05-07 21:43:38 +00003523 case NestedNameSpecifier::TypeSpec:
3524 case NestedNameSpecifier::TypeSpecWithTemplate:
3525 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3526 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3527 break;
3528
3529 case NestedNameSpecifier::Global:
3530 // Don't need to write an associated value.
3531 break;
3532 }
3533 }
3534}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003535
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003536void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3537 RecordDataImpl &Record) {
3538 // Nested name specifiers usually aren't too long. I think that 8 would
3539 // typically accomodate the vast majority.
3540 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3541
3542 // Push each of the nested-name-specifiers's onto a stack for
3543 // serialization in reverse order.
3544 while (NNS) {
3545 NestedNames.push_back(NNS);
3546 NNS = NNS.getPrefix();
3547 }
3548
3549 Record.push_back(NestedNames.size());
3550 while(!NestedNames.empty()) {
3551 NNS = NestedNames.pop_back_val();
3552 NestedNameSpecifier::SpecifierKind Kind
3553 = NNS.getNestedNameSpecifier()->getKind();
3554 Record.push_back(Kind);
3555 switch (Kind) {
3556 case NestedNameSpecifier::Identifier:
3557 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3558 AddSourceRange(NNS.getLocalSourceRange(), Record);
3559 break;
3560
3561 case NestedNameSpecifier::Namespace:
3562 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3563 AddSourceRange(NNS.getLocalSourceRange(), Record);
3564 break;
3565
3566 case NestedNameSpecifier::NamespaceAlias:
3567 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3568 AddSourceRange(NNS.getLocalSourceRange(), Record);
3569 break;
3570
3571 case NestedNameSpecifier::TypeSpec:
3572 case NestedNameSpecifier::TypeSpecWithTemplate:
3573 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3574 AddTypeLoc(NNS.getTypeLoc(), Record);
3575 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3576 break;
3577
3578 case NestedNameSpecifier::Global:
3579 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3580 break;
3581 }
3582 }
3583}
3584
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003585void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003586 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003587 Record.push_back(Kind);
3588 switch (Kind) {
3589 case TemplateName::Template:
3590 AddDeclRef(Name.getAsTemplateDecl(), Record);
3591 break;
3592
3593 case TemplateName::OverloadedTemplate: {
3594 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3595 Record.push_back(OvT->size());
3596 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3597 I != E; ++I)
3598 AddDeclRef(*I, Record);
3599 break;
3600 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003601
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003602 case TemplateName::QualifiedTemplate: {
3603 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3604 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3605 Record.push_back(QualT->hasTemplateKeyword());
3606 AddDeclRef(QualT->getTemplateDecl(), Record);
3607 break;
3608 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003609
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003610 case TemplateName::DependentTemplate: {
3611 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3612 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3613 Record.push_back(DepT->isIdentifier());
3614 if (DepT->isIdentifier())
3615 AddIdentifierRef(DepT->getIdentifier(), Record);
3616 else
3617 Record.push_back(DepT->getOperator());
3618 break;
3619 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003620
3621 case TemplateName::SubstTemplateTemplateParmPack: {
3622 SubstTemplateTemplateParmPackStorage *SubstPack
3623 = Name.getAsSubstTemplateTemplateParmPack();
3624 AddDeclRef(SubstPack->getParameterPack(), Record);
3625 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3626 break;
3627 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003628 }
3629}
3630
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003631void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003632 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003633 Record.push_back(Arg.getKind());
3634 switch (Arg.getKind()) {
3635 case TemplateArgument::Null:
3636 break;
3637 case TemplateArgument::Type:
3638 AddTypeRef(Arg.getAsType(), Record);
3639 break;
3640 case TemplateArgument::Declaration:
3641 AddDeclRef(Arg.getAsDecl(), Record);
3642 break;
3643 case TemplateArgument::Integral:
3644 AddAPSInt(*Arg.getAsIntegral(), Record);
3645 AddTypeRef(Arg.getIntegralType(), Record);
3646 break;
3647 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003648 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3649 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003650 case TemplateArgument::TemplateExpansion:
3651 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003652 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3653 Record.push_back(*NumExpansions + 1);
3654 else
3655 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003656 break;
3657 case TemplateArgument::Expression:
3658 AddStmt(Arg.getAsExpr());
3659 break;
3660 case TemplateArgument::Pack:
3661 Record.push_back(Arg.pack_size());
3662 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3663 I != E; ++I)
3664 AddTemplateArgument(*I, Record);
3665 break;
3666 }
3667}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003668
3669void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003670ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003671 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003672 assert(TemplateParams && "No TemplateParams!");
3673 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3674 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3675 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3676 Record.push_back(TemplateParams->size());
3677 for (TemplateParameterList::const_iterator
3678 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3679 P != PEnd; ++P)
3680 AddDeclRef(*P, Record);
3681}
3682
3683/// \brief Emit a template argument list.
3684void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003685ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003686 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003687 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003688 Record.push_back(TemplateArgs->size());
3689 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003690 AddTemplateArgument(TemplateArgs->get(i), Record);
3691}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003692
3693
3694void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003695ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003696 Record.push_back(Set.size());
3697 for (UnresolvedSetImpl::const_iterator
3698 I = Set.begin(), E = Set.end(); I != E; ++I) {
3699 AddDeclRef(I.getDecl(), Record);
3700 Record.push_back(I.getAccess());
3701 }
3702}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003703
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003704void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003705 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003706 Record.push_back(Base.isVirtual());
3707 Record.push_back(Base.isBaseOfClass());
3708 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003709 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003710 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003711 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003712 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3713 : SourceLocation(),
3714 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003715}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003716
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003717void ASTWriter::FlushCXXBaseSpecifiers() {
3718 RecordData Record;
3719 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3720 Record.clear();
3721
3722 // Record the offset of this base-specifier set.
3723 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3724 if (Index == CXXBaseSpecifiersOffsets.size())
3725 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3726 else {
3727 if (Index > CXXBaseSpecifiersOffsets.size())
3728 CXXBaseSpecifiersOffsets.resize(Index + 1);
3729 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3730 }
3731
3732 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3733 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3734 Record.push_back(BEnd - B);
3735 for (; B != BEnd; ++B)
3736 AddCXXBaseSpecifier(*B, Record);
3737 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003738
3739 // Flush any expressions that were written as part of the base specifiers.
3740 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003741 }
3742
3743 CXXBaseSpecifiersToWrite.clear();
3744}
3745
Alexis Hunt1d792652011-01-08 20:30:50 +00003746void ASTWriter::AddCXXCtorInitializers(
3747 const CXXCtorInitializer * const *CtorInitializers,
3748 unsigned NumCtorInitializers,
3749 RecordDataImpl &Record) {
3750 Record.push_back(NumCtorInitializers);
3751 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3752 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003753
3754 Record.push_back(Init->isBaseInitializer());
3755 if (Init->isBaseInitializer()) {
3756 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3757 Record.push_back(Init->isBaseVirtual());
3758 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003759 Record.push_back(Init->isIndirectMemberInitializer());
3760 if (Init->isIndirectMemberInitializer())
3761 AddDeclRef(Init->getIndirectMember(), Record);
3762 else
3763 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003764 }
Francois Pichetd583da02010-12-04 09:14:42 +00003765
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003766 AddSourceLocation(Init->getMemberLocation(), Record);
3767 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003768 AddSourceLocation(Init->getLParenLoc(), Record);
3769 AddSourceLocation(Init->getRParenLoc(), Record);
3770 Record.push_back(Init->isWritten());
3771 if (Init->isWritten()) {
3772 Record.push_back(Init->getSourceOrder());
3773 } else {
3774 Record.push_back(Init->getNumArrayIndices());
3775 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3776 AddDeclRef(Init->getArrayIndex(i), Record);
3777 }
3778 }
3779}
3780
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003781void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3782 assert(D->DefinitionData);
3783 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3784 Record.push_back(Data.UserDeclaredConstructor);
3785 Record.push_back(Data.UserDeclaredCopyConstructor);
3786 Record.push_back(Data.UserDeclaredCopyAssignment);
3787 Record.push_back(Data.UserDeclaredDestructor);
3788 Record.push_back(Data.Aggregate);
3789 Record.push_back(Data.PlainOldData);
3790 Record.push_back(Data.Empty);
3791 Record.push_back(Data.Polymorphic);
3792 Record.push_back(Data.Abstract);
3793 Record.push_back(Data.HasTrivialConstructor);
3794 Record.push_back(Data.HasTrivialCopyConstructor);
3795 Record.push_back(Data.HasTrivialCopyAssignment);
3796 Record.push_back(Data.HasTrivialDestructor);
3797 Record.push_back(Data.ComputedVisibleConversions);
3798 Record.push_back(Data.DeclaredDefaultConstructor);
3799 Record.push_back(Data.DeclaredCopyConstructor);
3800 Record.push_back(Data.DeclaredCopyAssignment);
3801 Record.push_back(Data.DeclaredDestructor);
3802
3803 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003804 if (Data.NumBases > 0)
3805 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3806 Record);
3807
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003808 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3809 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003810 if (Data.NumVBases > 0)
3811 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3812 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003813
3814 AddUnresolvedSet(Data.Conversions, Record);
3815 AddUnresolvedSet(Data.VisibleConversions, Record);
3816 // Data.Definition is the owning decl, no need to write it.
3817 AddDeclRef(Data.FirstFriend, Record);
3818}
3819
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003820void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003821 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003822 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003823 assert(FirstDeclID == NextDeclID &&
3824 FirstTypeID == NextTypeID &&
3825 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003826 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003827 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003828 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003829 "Setting chain after writing has started.");
3830 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003831
3832 FirstDeclID += Chain->getTotalNumDecls();
3833 FirstTypeID += Chain->getTotalNumTypes();
3834 FirstIdentID += Chain->getTotalNumIdentifiers();
3835 FirstSelectorID += Chain->getTotalNumSelectors();
3836 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003837 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003838 NextDeclID = FirstDeclID;
3839 NextTypeID = FirstTypeID;
3840 NextIdentID = FirstIdentID;
3841 NextSelectorID = FirstSelectorID;
3842 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003843 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003844}
3845
Sebastian Redl539c5062010-08-18 23:57:32 +00003846void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003847 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003848 if (II->hasMacroDefinition())
3849 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003850}
3851
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003852void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003853 // Always take the highest-numbered type index. This copes with an interesting
3854 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003855 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003856 // keep the higher-numbered entry so that we can properly write it out to
3857 // the AST file.
3858 TypeIdx &StoredIdx = TypeIdxs[T];
3859 if (Idx.getIndex() >= StoredIdx.getIndex())
3860 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003861}
3862
Sebastian Redl539c5062010-08-18 23:57:32 +00003863void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003864 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003865}
Sebastian Redl834bb972010-08-04 17:20:04 +00003866
Sebastian Redl539c5062010-08-18 23:57:32 +00003867void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003868 SelectorIDs[S] = ID;
3869}
Douglas Gregor91096292010-10-02 19:29:26 +00003870
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003871void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003872 MacroDefinition *MD) {
3873 MacroDefinitions[MD] = ID;
3874}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003875
3876void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3877 assert(D->isDefinition());
3878 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3879 // We are interested when a PCH decl is modified.
3880 if (RD->getPCHLevel() > 0) {
3881 // A forward reference was mutated into a definition. Rewrite it.
3882 // FIXME: This happens during template instantiation, should we
3883 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003884 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003885 }
3886
3887 for (CXXRecordDecl::redecl_iterator
3888 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3889 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3890 if (Redecl == RD)
3891 continue;
3892
3893 // We are interested when a PCH decl is modified.
3894 if (Redecl->getPCHLevel() > 0) {
3895 UpdateRecord &Record = DeclUpdates[Redecl];
3896 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3897 assert(Redecl->DefinitionData);
3898 assert(Redecl->DefinitionData->Definition == D);
3899 AddDeclRef(D, Record); // the DefinitionDecl
3900 }
3901 }
3902 }
3903}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003904void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3905 // TU and namespaces are handled elsewhere.
3906 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3907 return;
3908
3909 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3910 return; // Not a source decl added to a DeclContext from PCH.
3911
3912 AddUpdatedDeclContext(DC);
3913}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003914
3915void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3916 assert(D->isImplicit());
3917 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3918 return; // Not a source member added to a class from PCH.
3919 if (!isa<CXXMethodDecl>(D))
3920 return; // We are interested in lazily declared implicit methods.
3921
3922 // A decl coming from PCH was modified.
3923 assert(RD->isDefinition());
3924 UpdateRecord &Record = DeclUpdates[RD];
3925 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3926 AddDeclRef(D, Record);
3927}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003928
3929void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3930 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003931 // The specializations set is kept in the canonical template.
3932 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003933 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3934 return; // Not a source specialization added to a template from PCH.
3935
3936 UpdateRecord &Record = DeclUpdates[TD];
3937 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3938 AddDeclRef(D, Record);
3939}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003940
Sebastian Redl9ab988f2011-04-14 14:07:59 +00003941void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
3942 const FunctionDecl *D) {
3943 // The specializations set is kept in the canonical template.
3944 TD = TD->getCanonicalDecl();
3945 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3946 return; // Not a source specialization added to a template from PCH.
3947
3948 UpdateRecord &Record = DeclUpdates[TD];
3949 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3950 AddDeclRef(D, Record);
3951}
3952
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003953ASTSerializationListener::~ASTSerializationListener() { }