blob: f172b7aceca29c4d5642147710ca28d988201bad [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 Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000048#include <string.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051
Sebastian Redl3df5a082010-07-30 17:03:48 +000052template <typename T, typename Allocator>
53T *data(std::vector<T, Allocator> &v) {
54 return v.empty() ? 0 : &v.front();
55}
56template <typename T, typename Allocator>
57const T *data(const std::vector<T, Allocator> &v) {
58 return v.empty() ? 0 : &v.front();
59}
60
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061//===----------------------------------------------------------------------===//
62// Type serialization
63//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000064
Douglas Gregoref84c4b2009-04-09 22:27:44 +000065namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000066 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000067 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000068 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069
70 public:
71 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000072 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000074 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000075 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076
77 void VisitArrayType(const ArrayType *T);
78 void VisitFunctionType(const FunctionType *T);
79 void VisitTagType(const TagType *T);
80
81#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
82#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083#include "clang/AST/TypeNodes.def"
84 };
85}
86
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000087void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088 assert(false && "Built-in types are never serialized");
89}
90
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000091void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000093 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000098 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000113 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114}
115
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000117 Writer.AddTypeRef(T->getPointeeType(), Record);
118 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000119 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000123 Writer.AddTypeRef(T->getElementType(), Record);
124 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000125 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000129 VisitArrayType(T);
130 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000131 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000136 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137}
138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000141 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
142 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000143 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 Writer.AddTypeRef(T->getElementType(), Record);
149 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000150 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000156 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157}
158
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000161 FunctionType::ExtInfo C = T->getExtInfo();
162 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000163 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000164 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000165 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166}
167
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000170 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000171}
172
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000173void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174 VisitFunctionType(T);
175 Record.push_back(T->getNumArgs());
176 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
177 Writer.AddTypeRef(T->getArgType(I), Record);
178 Record.push_back(T->isVariadic());
179 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000180 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000181 Record.push_back(T->hasExceptionSpec());
182 Record.push_back(T->hasAnyExceptionSpec());
183 Record.push_back(T->getNumExceptions());
184 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
185 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000186 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000187}
188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000190 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000191 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000192}
John McCallb96ec562009-12-04 22:46:56 +0000193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000196 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
197 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000198 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000202 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000213 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000214}
215
Richard Smith30482bc2011-02-20 03:19:35 +0000216void ASTTypeWriter::VisitAutoType(const AutoType *T) {
217 Writer.AddTypeRef(T->getDeducedType(), Record);
218 Code = TYPE_AUTO;
219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000222 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000224 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225 "Cannot serialize in the middle of a type definition");
226}
227
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000230 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231}
232
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000235 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236}
237
John McCall81904512011-01-06 01:58:22 +0000238void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
239 Writer.AddTypeRef(T->getModifiedType(), Record);
240 Writer.AddTypeRef(T->getEquivalentType(), Record);
241 Record.push_back(T->getAttrKind());
242 Code = TYPE_ATTRIBUTED;
243}
244
Mike Stump11289f42009-09-09 15:08:12 +0000245void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000246ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000247 const SubstTemplateTypeParmType *T) {
248 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
249 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000250 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000251}
252
253void
Douglas Gregorada4b792011-01-14 02:55:32 +0000254ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
255 const SubstTemplateTypeParmPackType *T) {
256 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
257 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
258 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
259}
260
261void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000262ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000264 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000265 Writer.AddTemplateName(T->getTemplateName(), Record);
266 Record.push_back(T->getNumArgs());
267 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
268 ArgI != ArgE; ++ArgI)
269 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000270 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
271 : T->getCanonicalTypeInternal(),
272 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000273 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000274}
275
276void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000277ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000278 VisitArrayType(T);
279 Writer.AddStmt(T->getSizeExpr());
280 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000281 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000282}
283
284void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000285ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000286 const DependentSizedExtVectorType *T) {
287 // FIXME: Serialize this type (C++ only)
288 assert(false && "Cannot serialize dependent sized extended vector types");
289}
290
291void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000292ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000293 Record.push_back(T->getDepth());
294 Record.push_back(T->getIndex());
295 Record.push_back(T->isParameterPack());
296 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000297 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000298}
299
300void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000301ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000302 Record.push_back(T->getKeyword());
303 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
304 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000305 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
306 : T->getCanonicalTypeInternal(),
307 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000308 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000313 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000314 Record.push_back(T->getKeyword());
315 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
316 Writer.AddIdentifierRef(T->getIdentifier(), Record);
317 Record.push_back(T->getNumArgs());
318 for (DependentTemplateSpecializationType::iterator
319 I = T->begin(), E = T->end(); I != E; ++I)
320 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000321 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000322}
323
Douglas Gregord2fa7662010-12-20 02:24:11 +0000324void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
325 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000326 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
327 Record.push_back(*NumExpansions + 1);
328 else
329 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000330 Code = TYPE_PACK_EXPANSION;
331}
332
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000333void ASTTypeWriter::VisitParenType(const ParenType *T) {
334 Writer.AddTypeRef(T->getInnerType(), Record);
335 Code = TYPE_PAREN;
336}
337
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000338void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000339 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000340 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
341 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000343}
344
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000345void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000346 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000347 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000352 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000353 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000354}
355
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000357 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000358 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000359 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000360 E = T->qual_end(); I != E; ++I)
361 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000362 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000363}
364
Steve Narofffb4330f2009-06-17 22:40:22 +0000365void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000366ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000367 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000368 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000369}
370
John McCall8f115c62009-10-16 21:56:05 +0000371namespace {
372
373class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000374 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000375 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000376
377public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000378 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000379 : Writer(Writer), Record(Record) { }
380
John McCall17001972009-10-18 01:05:36 +0000381#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000382#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000383 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000384#include "clang/AST/TypeLocNodes.def"
385
John McCall17001972009-10-18 01:05:36 +0000386 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
387 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000388};
389
390}
391
John McCall17001972009-10-18 01:05:36 +0000392void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
393 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000394}
John McCall17001972009-10-18 01:05:36 +0000395void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000396 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
397 if (TL.needsExtraLocalData()) {
398 Record.push_back(TL.getWrittenTypeSpec());
399 Record.push_back(TL.getWrittenSignSpec());
400 Record.push_back(TL.getWrittenWidthSpec());
401 Record.push_back(TL.hasModeAttr());
402 }
John McCall8f115c62009-10-16 21:56:05 +0000403}
John McCall17001972009-10-18 01:05:36 +0000404void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000406}
John McCall17001972009-10-18 01:05:36 +0000407void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000409}
John McCall17001972009-10-18 01:05:36 +0000410void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000412}
John McCall17001972009-10-18 01:05:36 +0000413void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000415}
John McCall17001972009-10-18 01:05:36 +0000416void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000418}
John McCall17001972009-10-18 01:05:36 +0000419void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
420 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000421 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000422}
John McCall17001972009-10-18 01:05:36 +0000423void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
425 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
426 Record.push_back(TL.getSizeExpr() ? 1 : 0);
427 if (TL.getSizeExpr())
428 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000429}
John McCall17001972009-10-18 01:05:36 +0000430void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
431 VisitArrayTypeLoc(TL);
432}
433void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
434 VisitArrayTypeLoc(TL);
435}
436void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
437 VisitArrayTypeLoc(TL);
438}
439void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
440 DependentSizedArrayTypeLoc TL) {
441 VisitArrayTypeLoc(TL);
442}
443void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
444 DependentSizedExtVectorTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
446}
447void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getNameLoc(), Record);
449}
450void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
453void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
455 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000456 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000457 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
458 Writer.AddDeclRef(TL.getArg(i), Record);
459}
460void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
461 VisitFunctionTypeLoc(TL);
462}
463void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
464 VisitFunctionTypeLoc(TL);
465}
John McCallb96ec562009-12-04 22:46:56 +0000466void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getNameLoc(), Record);
468}
John McCall17001972009-10-18 01:05:36 +0000469void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getNameLoc(), Record);
471}
472void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000473 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
474 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
475 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000476}
477void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000478 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
479 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
480 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
481 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000482}
483void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
Richard Smith30482bc2011-02-20 03:19:35 +0000486void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getNameLoc(), Record);
488}
John McCall17001972009-10-18 01:05:36 +0000489void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
491}
492void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
John McCall81904512011-01-06 01:58:22 +0000495void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
497 if (TL.hasAttrOperand()) {
498 SourceRange range = TL.getAttrOperandParensRange();
499 Writer.AddSourceLocation(range.getBegin(), Record);
500 Writer.AddSourceLocation(range.getEnd(), Record);
501 }
502 if (TL.hasAttrExprOperand()) {
503 Expr *operand = TL.getAttrExprOperand();
504 Record.push_back(operand ? 1 : 0);
505 if (operand) Writer.AddStmt(operand);
506 } else if (TL.hasAttrEnumOperand()) {
507 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
508 }
509}
John McCall17001972009-10-18 01:05:36 +0000510void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
511 Writer.AddSourceLocation(TL.getNameLoc(), Record);
512}
John McCallcebee162009-10-18 09:09:24 +0000513void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
514 SubstTemplateTypeParmTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getNameLoc(), Record);
516}
Douglas Gregorada4b792011-01-14 02:55:32 +0000517void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
518 SubstTemplateTypeParmPackTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
John McCall17001972009-10-18 01:05:36 +0000521void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
522 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000523 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
524 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
525 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
526 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000527 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
528 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000529}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000530void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
532 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
533}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000534void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000535 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000536 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000537}
John McCalle78aac42010-03-10 03:28:59 +0000538void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getNameLoc(), Record);
540}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000541void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000542 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000543 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000544 Writer.AddSourceLocation(TL.getNameLoc(), Record);
545}
John McCallc392f372010-06-11 00:33:02 +0000546void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
547 DependentTemplateSpecializationTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000549 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000550 Writer.AddSourceLocation(TL.getNameLoc(), Record);
551 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
552 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
553 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000554 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
555 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000556}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000557void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
559}
John McCall17001972009-10-18 01:05:36 +0000560void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000562}
563void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
564 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000565 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
566 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
567 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
568 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000569}
John McCallfc93cf92009-10-22 22:37:11 +0000570void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000572}
John McCall8f115c62009-10-16 21:56:05 +0000573
Chris Lattner19cea4e2009-04-22 05:57:30 +0000574//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000575// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000576//===----------------------------------------------------------------------===//
577
Chris Lattner28fa4e62009-04-26 22:26:21 +0000578static void EmitBlockID(unsigned ID, const char *Name,
579 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000580 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000581 Record.clear();
582 Record.push_back(ID);
583 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
584
585 // Emit the block name if present.
586 if (Name == 0 || Name[0] == 0) return;
587 Record.clear();
588 while (*Name)
589 Record.push_back(*Name++);
590 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
591}
592
593static void EmitRecordID(unsigned ID, const char *Name,
594 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000595 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000596 Record.clear();
597 Record.push_back(ID);
598 while (*Name)
599 Record.push_back(*Name++);
600 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000601}
602
603static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000604 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000605#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000606 RECORD(STMT_STOP);
607 RECORD(STMT_NULL_PTR);
608 RECORD(STMT_NULL);
609 RECORD(STMT_COMPOUND);
610 RECORD(STMT_CASE);
611 RECORD(STMT_DEFAULT);
612 RECORD(STMT_LABEL);
613 RECORD(STMT_IF);
614 RECORD(STMT_SWITCH);
615 RECORD(STMT_WHILE);
616 RECORD(STMT_DO);
617 RECORD(STMT_FOR);
618 RECORD(STMT_GOTO);
619 RECORD(STMT_INDIRECT_GOTO);
620 RECORD(STMT_CONTINUE);
621 RECORD(STMT_BREAK);
622 RECORD(STMT_RETURN);
623 RECORD(STMT_DECL);
624 RECORD(STMT_ASM);
625 RECORD(EXPR_PREDEFINED);
626 RECORD(EXPR_DECL_REF);
627 RECORD(EXPR_INTEGER_LITERAL);
628 RECORD(EXPR_FLOATING_LITERAL);
629 RECORD(EXPR_IMAGINARY_LITERAL);
630 RECORD(EXPR_STRING_LITERAL);
631 RECORD(EXPR_CHARACTER_LITERAL);
632 RECORD(EXPR_PAREN);
633 RECORD(EXPR_UNARY_OPERATOR);
634 RECORD(EXPR_SIZEOF_ALIGN_OF);
635 RECORD(EXPR_ARRAY_SUBSCRIPT);
636 RECORD(EXPR_CALL);
637 RECORD(EXPR_MEMBER);
638 RECORD(EXPR_BINARY_OPERATOR);
639 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
640 RECORD(EXPR_CONDITIONAL_OPERATOR);
641 RECORD(EXPR_IMPLICIT_CAST);
642 RECORD(EXPR_CSTYLE_CAST);
643 RECORD(EXPR_COMPOUND_LITERAL);
644 RECORD(EXPR_EXT_VECTOR_ELEMENT);
645 RECORD(EXPR_INIT_LIST);
646 RECORD(EXPR_DESIGNATED_INIT);
647 RECORD(EXPR_IMPLICIT_VALUE_INIT);
648 RECORD(EXPR_VA_ARG);
649 RECORD(EXPR_ADDR_LABEL);
650 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000651 RECORD(EXPR_CHOOSE);
652 RECORD(EXPR_GNU_NULL);
653 RECORD(EXPR_SHUFFLE_VECTOR);
654 RECORD(EXPR_BLOCK);
655 RECORD(EXPR_BLOCK_DECL_REF);
656 RECORD(EXPR_OBJC_STRING_LITERAL);
657 RECORD(EXPR_OBJC_ENCODE);
658 RECORD(EXPR_OBJC_SELECTOR_EXPR);
659 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
660 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
661 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
662 RECORD(EXPR_OBJC_KVC_REF_EXPR);
663 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000664 RECORD(STMT_OBJC_FOR_COLLECTION);
665 RECORD(STMT_OBJC_CATCH);
666 RECORD(STMT_OBJC_FINALLY);
667 RECORD(STMT_OBJC_AT_TRY);
668 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
669 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000670 RECORD(EXPR_CXX_OPERATOR_CALL);
671 RECORD(EXPR_CXX_CONSTRUCT);
672 RECORD(EXPR_CXX_STATIC_CAST);
673 RECORD(EXPR_CXX_DYNAMIC_CAST);
674 RECORD(EXPR_CXX_REINTERPRET_CAST);
675 RECORD(EXPR_CXX_CONST_CAST);
676 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
677 RECORD(EXPR_CXX_BOOL_LITERAL);
678 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000679 RECORD(EXPR_CXX_TYPEID_EXPR);
680 RECORD(EXPR_CXX_TYPEID_TYPE);
681 RECORD(EXPR_CXX_UUIDOF_EXPR);
682 RECORD(EXPR_CXX_UUIDOF_TYPE);
683 RECORD(EXPR_CXX_THIS);
684 RECORD(EXPR_CXX_THROW);
685 RECORD(EXPR_CXX_DEFAULT_ARG);
686 RECORD(EXPR_CXX_BIND_TEMPORARY);
687 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
688 RECORD(EXPR_CXX_NEW);
689 RECORD(EXPR_CXX_DELETE);
690 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
691 RECORD(EXPR_EXPR_WITH_CLEANUPS);
692 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
693 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
694 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
695 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
696 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
697 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
698 RECORD(EXPR_CXX_NOEXCEPT);
699 RECORD(EXPR_OPAQUE_VALUE);
700 RECORD(EXPR_BINARY_TYPE_TRAIT);
701 RECORD(EXPR_PACK_EXPANSION);
702 RECORD(EXPR_SIZEOF_PACK);
703 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000704 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000705#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000706}
Mike Stump11289f42009-09-09 15:08:12 +0000707
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000708void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000709 RecordData Record;
710 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000711
Sebastian Redl539c5062010-08-18 23:57:32 +0000712#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
713#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000714
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000715 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000716 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000717 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000718 RECORD(TYPE_OFFSET);
719 RECORD(DECL_OFFSET);
720 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000721 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000722 RECORD(IDENTIFIER_OFFSET);
723 RECORD(IDENTIFIER_TABLE);
724 RECORD(EXTERNAL_DEFINITIONS);
725 RECORD(SPECIAL_TYPES);
726 RECORD(STATISTICS);
727 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000728 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000729 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
730 RECORD(SELECTOR_OFFSETS);
731 RECORD(METHOD_POOL);
732 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000733 RECORD(SOURCE_LOCATION_OFFSETS);
734 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000735 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000736 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000737 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000738 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000739 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000740 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000741 RECORD(TU_UPDATE_LEXICAL);
742 RECORD(REDECLS_UPDATE_LATEST);
743 RECORD(SEMA_DECL_REFS);
744 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
745 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
746 RECORD(DECL_REPLACEMENTS);
747 RECORD(UPDATE_VISIBLE);
748 RECORD(DECL_UPDATE_OFFSETS);
749 RECORD(DECL_UPDATES);
750 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
751 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000752 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000753 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000754 RECORD(FP_PRAGMA_OPTIONS);
755 RECORD(OPENCL_EXTENSIONS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000756
Chris Lattner28fa4e62009-04-26 22:26:21 +0000757 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000758 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000759 RECORD(SM_SLOC_FILE_ENTRY);
760 RECORD(SM_SLOC_BUFFER_ENTRY);
761 RECORD(SM_SLOC_BUFFER_BLOB);
762 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
763 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattner28fa4e62009-04-26 22:26:21 +0000765 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000766 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000767 RECORD(PP_MACRO_OBJECT_LIKE);
768 RECORD(PP_MACRO_FUNCTION_LIKE);
769 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000770
Douglas Gregor12bfa382009-10-17 00:13:19 +0000771 // Decls and Types block.
772 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000773 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000774 RECORD(TYPE_COMPLEX);
775 RECORD(TYPE_POINTER);
776 RECORD(TYPE_BLOCK_POINTER);
777 RECORD(TYPE_LVALUE_REFERENCE);
778 RECORD(TYPE_RVALUE_REFERENCE);
779 RECORD(TYPE_MEMBER_POINTER);
780 RECORD(TYPE_CONSTANT_ARRAY);
781 RECORD(TYPE_INCOMPLETE_ARRAY);
782 RECORD(TYPE_VARIABLE_ARRAY);
783 RECORD(TYPE_VECTOR);
784 RECORD(TYPE_EXT_VECTOR);
785 RECORD(TYPE_FUNCTION_PROTO);
786 RECORD(TYPE_FUNCTION_NO_PROTO);
787 RECORD(TYPE_TYPEDEF);
788 RECORD(TYPE_TYPEOF_EXPR);
789 RECORD(TYPE_TYPEOF);
790 RECORD(TYPE_RECORD);
791 RECORD(TYPE_ENUM);
792 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000793 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000794 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000795 RECORD(TYPE_DECLTYPE);
796 RECORD(TYPE_ELABORATED);
797 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
798 RECORD(TYPE_UNRESOLVED_USING);
799 RECORD(TYPE_INJECTED_CLASS_NAME);
800 RECORD(TYPE_OBJC_OBJECT);
801 RECORD(TYPE_TEMPLATE_TYPE_PARM);
802 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
803 RECORD(TYPE_DEPENDENT_NAME);
804 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
805 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
806 RECORD(TYPE_PAREN);
807 RECORD(TYPE_PACK_EXPANSION);
808 RECORD(TYPE_ATTRIBUTED);
809 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000810 RECORD(DECL_TRANSLATION_UNIT);
811 RECORD(DECL_TYPEDEF);
812 RECORD(DECL_ENUM);
813 RECORD(DECL_RECORD);
814 RECORD(DECL_ENUM_CONSTANT);
815 RECORD(DECL_FUNCTION);
816 RECORD(DECL_OBJC_METHOD);
817 RECORD(DECL_OBJC_INTERFACE);
818 RECORD(DECL_OBJC_PROTOCOL);
819 RECORD(DECL_OBJC_IVAR);
820 RECORD(DECL_OBJC_AT_DEFS_FIELD);
821 RECORD(DECL_OBJC_CLASS);
822 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
823 RECORD(DECL_OBJC_CATEGORY);
824 RECORD(DECL_OBJC_CATEGORY_IMPL);
825 RECORD(DECL_OBJC_IMPLEMENTATION);
826 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
827 RECORD(DECL_OBJC_PROPERTY);
828 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000829 RECORD(DECL_FIELD);
830 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000831 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000832 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000833 RECORD(DECL_FILE_SCOPE_ASM);
834 RECORD(DECL_BLOCK);
835 RECORD(DECL_CONTEXT_LEXICAL);
836 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000837 RECORD(DECL_NAMESPACE);
838 RECORD(DECL_NAMESPACE_ALIAS);
839 RECORD(DECL_USING);
840 RECORD(DECL_USING_SHADOW);
841 RECORD(DECL_USING_DIRECTIVE);
842 RECORD(DECL_UNRESOLVED_USING_VALUE);
843 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
844 RECORD(DECL_LINKAGE_SPEC);
845 RECORD(DECL_CXX_RECORD);
846 RECORD(DECL_CXX_METHOD);
847 RECORD(DECL_CXX_CONSTRUCTOR);
848 RECORD(DECL_CXX_DESTRUCTOR);
849 RECORD(DECL_CXX_CONVERSION);
850 RECORD(DECL_ACCESS_SPEC);
851 RECORD(DECL_FRIEND);
852 RECORD(DECL_FRIEND_TEMPLATE);
853 RECORD(DECL_CLASS_TEMPLATE);
854 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
855 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
856 RECORD(DECL_FUNCTION_TEMPLATE);
857 RECORD(DECL_TEMPLATE_TYPE_PARM);
858 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
859 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
860 RECORD(DECL_STATIC_ASSERT);
861 RECORD(DECL_CXX_BASE_SPECIFIERS);
862 RECORD(DECL_INDIRECTFIELD);
863 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
864
Douglas Gregor92a96f52011-02-08 21:58:10 +0000865 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
866 RECORD(PPD_MACRO_INSTANTIATION);
867 RECORD(PPD_MACRO_DEFINITION);
868 RECORD(PPD_INCLUSION_DIRECTIVE);
869
Douglas Gregor12bfa382009-10-17 00:13:19 +0000870 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000871 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000872#undef RECORD
873#undef BLOCK
874 Stream.ExitBlock();
875}
876
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000877/// \brief Adjusts the given filename to only write out the portion of the
878/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000879///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000880/// \param Filename the file name to adjust.
881///
882/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
883/// the returned filename will be adjusted by this system root.
884///
885/// \returns either the original filename (if it needs no adjustment) or the
886/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000887static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000888adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
889 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000890
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000891 if (!isysroot)
892 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000893
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000894 // Verify that the filename and the system root have the same prefix.
895 unsigned Pos = 0;
896 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
897 if (Filename[Pos] != isysroot[Pos])
898 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000899
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000900 // We hit the end of the filename before we hit the end of the system root.
901 if (!Filename[Pos])
902 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000904 // If the file name has a '/' at the current position, skip over the '/'.
905 // We distinguish sysroot-based includes from absolute includes by the
906 // absence of '/' at the beginning of sysroot-based includes.
907 if (Filename[Pos] == '/')
908 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000910 return Filename + Pos;
911}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000912
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000913/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000914void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
915 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000916 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000917
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000918 // Metadata
919 const TargetInfo &Target = Context.Target;
920 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000921 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000922 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000923 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
924 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000925 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
926 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
927 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000928 // Target triple or chained PCH name
929 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000930 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000931
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000932 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000933 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
934 Record.push_back(VERSION_MAJOR);
935 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000936 Record.push_back(CLANG_VERSION_MAJOR);
937 Record.push_back(CLANG_VERSION_MINOR);
938 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000939 // FIXME: This writes the absolute path for chained headers.
940 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
941 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000942
Douglas Gregor45fe0362009-05-12 01:31:05 +0000943 // Original file name
944 SourceManager &SM = Context.getSourceManager();
945 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
946 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000947 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000948 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
949 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
950
Michael J. Spencer740857f2010-12-21 16:45:57 +0000951 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000952
Michael J. Spencer740857f2010-12-21 16:45:57 +0000953 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000954
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000955 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000956 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000957 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000958 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000959 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000960 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000961 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000962
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000963 // Original PCH directory
964 if (!OutputFile.empty() && OutputFile != "-") {
965 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
966 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
968 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
969
970 llvm::SmallString<128> OutputPath(OutputFile);
971
972 llvm::sys::fs::make_absolute(OutputPath);
973 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
974
975 RecordData Record;
976 Record.push_back(ORIGINAL_PCH_DIR);
977 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
978 }
979
Ted Kremenek18e066f2010-01-22 22:12:47 +0000980 // Repository branch/version information.
981 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000982 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000983 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
984 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000985 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000986 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000987 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
988 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000989}
990
991/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000992void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000993 RecordData Record;
994 Record.push_back(LangOpts.Trigraphs);
995 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
996 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
997 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
998 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000999 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001000 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1001 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1002 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1003 Record.push_back(LangOpts.C99); // C99 Support
1004 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001005 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1006 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001007 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1008 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001009 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001010
Douglas Gregor55abb232009-04-10 20:39:37 +00001011 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1012 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001013 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001014 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001015 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001016 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001017 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001018 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1019 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001020 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001021
Douglas Gregor55abb232009-04-10 20:39:37 +00001022 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001023 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1024 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001025 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001026 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001027 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001028 Record.push_back(LangOpts.CXXExceptions);
1029 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001030
Douglas Gregordbe39272011-02-01 15:15:22 +00001031 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001032 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1033 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1034 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1035
Chris Lattner258172e2009-04-27 07:35:58 +00001036 // Whether static initializers are protected by locks.
1037 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001038 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001039 Record.push_back(LangOpts.Blocks); // block extension to C
1040 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1041 // they are unused.
1042 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1043 // (modulo the platform support).
1044
Chris Lattner51924e512010-06-26 21:25:03 +00001045 Record.push_back(LangOpts.getSignedOverflowBehavior());
1046 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001047
1048 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001049 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001050 // defined.
1051 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1052 // opposed to __DYNAMIC__).
1053 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1054
1055 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1056 // used (instead of C99 semantics).
1057 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001058 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1059 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001060 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1061 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001062 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001063 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1064 // to the smallest integer type with
1065 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001066 Record.push_back(LangOpts.getGCMode());
1067 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001068 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001069 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001070 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001071 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001072 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001073 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001074 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001075 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001076 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001077 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001078}
1079
Douglas Gregora7f71a92009-04-10 03:52:48 +00001080//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001081// stat cache Serialization
1082//===----------------------------------------------------------------------===//
1083
1084namespace {
1085// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001086class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001087public:
1088 typedef const char * key_type;
1089 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattner2a6fa472010-11-23 19:28:12 +00001091 typedef struct stat data_type;
1092 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001093
1094 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001095 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 }
Mike Stump11289f42009-09-09 15:08:12 +00001097
1098 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001099 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1100 data_type_ref Data) {
1101 unsigned StrLen = strlen(path);
1102 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001103 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001104 clang::io::Emit8(Out, DataLen);
1105 return std::make_pair(StrLen + 1, DataLen);
1106 }
Mike Stump11289f42009-09-09 15:08:12 +00001107
Douglas Gregorc5046832009-04-27 18:38:38 +00001108 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1109 Out.write(path, KeyLen);
1110 }
Mike Stump11289f42009-09-09 15:08:12 +00001111
Chris Lattner2a6fa472010-11-23 19:28:12 +00001112 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001113 data_type_ref Data, unsigned DataLen) {
1114 using namespace clang::io;
1115 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001116
Chris Lattner2a6fa472010-11-23 19:28:12 +00001117 Emit32(Out, (uint32_t) Data.st_ino);
1118 Emit32(Out, (uint32_t) Data.st_dev);
1119 Emit16(Out, (uint16_t) Data.st_mode);
1120 Emit64(Out, (uint64_t) Data.st_mtime);
1121 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001122
1123 assert(Out.tell() - Start == DataLen && "Wrong data length");
1124 }
1125};
1126} // end anonymous namespace
1127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001128/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001129void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001130 // Build the on-disk hash table containing information about every
1131 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001132 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001133 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001134 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001135 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001136 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1137 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001138 Generator.insert(Filename, Stat->second);
1139 }
Mike Stump11289f42009-09-09 15:08:12 +00001140
Douglas Gregorc5046832009-04-27 18:38:38 +00001141 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001142 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001143 uint32_t BucketOffset;
1144 {
1145 llvm::raw_svector_ostream Out(StatCacheData);
1146 // Make sure that no bucket is at offset 0
1147 clang::io::Emit32(Out, 0);
1148 BucketOffset = Generator.Emit(Out);
1149 }
1150
1151 // Create a blob abbreviation
1152 using namespace llvm;
1153 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001154 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1158 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1159
1160 // Write the stat cache
1161 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001162 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001163 Record.push_back(BucketOffset);
1164 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001165 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001166}
1167
1168//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001169// Source Manager Serialization
1170//===----------------------------------------------------------------------===//
1171
1172/// \brief Create an abbreviation for the SLocEntry that refers to a
1173/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001174static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175 using namespace llvm;
1176 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001177 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001182 // FileEntry fields.
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001186 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001187}
1188
1189/// \brief Create an abbreviation for the SLocEntry that refers to a
1190/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001191static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001192 using namespace llvm;
1193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001194 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001200 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001201}
1202
1203/// \brief Create an abbreviation for the SLocEntry that refers to a
1204/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001205static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001206 using namespace llvm;
1207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001208 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001210 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001211}
1212
1213/// \brief Create an abbreviation for the SLocEntry that refers to an
1214/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001215static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001216 using namespace llvm;
1217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001218 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001224 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001225}
1226
Douglas Gregor09b69892011-02-10 17:09:37 +00001227namespace {
1228 // Trait used for the on-disk hash table of header search information.
1229 class HeaderFileInfoTrait {
1230 ASTWriter &Writer;
1231 HeaderSearch &HS;
1232
1233 public:
1234 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1235 : Writer(Writer), HS(HS) { }
1236
1237 typedef const char *key_type;
1238 typedef key_type key_type_ref;
1239
1240 typedef HeaderFileInfo data_type;
1241 typedef const data_type &data_type_ref;
1242
1243 static unsigned ComputeHash(const char *path) {
1244 // The hash is based only on the filename portion of the key, so that the
1245 // reader can match based on filenames when symlinking or excess path
1246 // elements ("foo/../", "../") change the form of the name. However,
1247 // complete path is still the key.
1248 return llvm::HashString(llvm::sys::path::filename(path));
1249 }
1250
1251 std::pair<unsigned,unsigned>
1252 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1253 data_type_ref Data) {
1254 unsigned StrLen = strlen(path);
1255 clang::io::Emit16(Out, StrLen);
1256 unsigned DataLen = 1 + 2 + 4;
1257 clang::io::Emit8(Out, DataLen);
1258 return std::make_pair(StrLen + 1, DataLen);
1259 }
1260
1261 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1262 Out.write(path, KeyLen);
1263 }
1264
1265 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1266 data_type_ref Data, unsigned DataLen) {
1267 using namespace clang::io;
1268 uint64_t Start = Out.tell(); (void)Start;
1269
1270 unsigned char Flags = (Data.isImport << 3)
1271 | (Data.DirInfo << 1)
1272 | Data.Resolved;
1273 Emit8(Out, (uint8_t)Flags);
1274 Emit16(Out, (uint16_t) Data.NumIncludes);
1275
1276 if (!Data.ControllingMacro)
1277 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1278 else
1279 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1280 assert(Out.tell() - Start == DataLen && "Wrong data length");
1281 }
1282 };
1283} // end anonymous namespace
1284
1285/// \brief Write the header search block for the list of files that
1286///
1287/// \param HS The header search structure to save.
1288///
1289/// \param Chain Whether we're creating a chained AST file.
1290void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1291 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1292 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1293
1294 if (FilesByUID.size() > HS.header_file_size())
1295 FilesByUID.resize(HS.header_file_size());
1296
1297 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1298 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1299 llvm::SmallVector<const char *, 4> SavedStrings;
1300 unsigned NumHeaderSearchEntries = 0;
1301 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1302 const FileEntry *File = FilesByUID[UID];
1303 if (!File)
1304 continue;
1305
1306 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1307 if (HFI.External && Chain)
1308 continue;
1309
1310 // Turn the file name into an absolute path, if it isn't already.
1311 const char *Filename = File->getName();
1312 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1313
1314 // If we performed any translation on the file name at all, we need to
1315 // save this string, since the generator will refer to it later.
1316 if (Filename != File->getName()) {
1317 Filename = strdup(Filename);
1318 SavedStrings.push_back(Filename);
1319 }
1320
1321 Generator.insert(Filename, HFI, GeneratorTrait);
1322 ++NumHeaderSearchEntries;
1323 }
1324
1325 // Create the on-disk hash table in a buffer.
1326 llvm::SmallString<4096> TableData;
1327 uint32_t BucketOffset;
1328 {
1329 llvm::raw_svector_ostream Out(TableData);
1330 // Make sure that no bucket is at offset 0
1331 clang::io::Emit32(Out, 0);
1332 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1333 }
1334
1335 // Create a blob abbreviation
1336 using namespace llvm;
1337 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1338 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1342 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1343
1344 // Write the stat cache
1345 RecordData Record;
1346 Record.push_back(HEADER_SEARCH_TABLE);
1347 Record.push_back(BucketOffset);
1348 Record.push_back(NumHeaderSearchEntries);
1349 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1350
1351 // Free all of the strings we had to duplicate.
1352 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1353 free((void*)SavedStrings[I]);
1354}
1355
Douglas Gregora7f71a92009-04-10 03:52:48 +00001356/// \brief Writes the block containing the serialized form of the
1357/// source manager.
1358///
1359/// TODO: We should probably use an on-disk hash table (stored in a
1360/// blob), indexed based on the file name, so that we only create
1361/// entries for files that we actually need. In the common case (no
1362/// errors), we probably won't have to create file entries for any of
1363/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001364void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001365 const Preprocessor &PP,
1366 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001367 RecordData Record;
1368
Chris Lattner0910e3b2009-04-10 17:16:57 +00001369 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001370 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001371
1372 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001373 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1374 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1375 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1376 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001377
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001378 // Write the line table.
1379 if (SourceMgr.hasLineTable()) {
1380 LineTableInfo &LineTable = SourceMgr.getLineTable();
1381
1382 // Emit the file names
1383 Record.push_back(LineTable.getNumFilenames());
1384 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1385 // Emit the file name
1386 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001387 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001388 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1389 Record.push_back(FilenameLen);
1390 if (FilenameLen)
1391 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001394 // Emit the line entries
1395 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1396 L != LEnd; ++L) {
1397 // Emit the file ID
1398 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001399
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001400 // Emit the line entries
1401 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001402 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001403 LEEnd = L->second.end();
1404 LE != LEEnd; ++LE) {
1405 Record.push_back(LE->FileOffset);
1406 Record.push_back(LE->LineNo);
1407 Record.push_back(LE->FilenameID);
1408 Record.push_back((unsigned)LE->FileKind);
1409 Record.push_back(LE->IncludeOffset);
1410 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001411 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001412 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001413 }
1414
Douglas Gregor258ae542009-04-27 06:38:32 +00001415 // Write out the source location entry table. We skip the first
1416 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001417 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001418 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001419 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1420 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1421 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1422 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001423 // Get this source location entry.
1424 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001425
Douglas Gregor258ae542009-04-27 06:38:32 +00001426 // Record the offset of this source-location entry.
1427 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1428
1429 // Figure out which record code to use.
1430 unsigned Code;
1431 if (SLoc->isFile()) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001432 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001433 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001434 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001435 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001436 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001437 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001438 Record.clear();
1439 Record.push_back(Code);
1440
1441 Record.push_back(SLoc->getOffset());
1442 if (SLoc->isFile()) {
1443 const SrcMgr::FileInfo &File = SLoc->getFile();
1444 Record.push_back(File.getIncludeLoc().getRawEncoding());
1445 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1446 Record.push_back(File.hasLineDirectives());
1447
1448 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001449 if (Content->OrigEntry) {
1450 assert(Content->OrigEntry == Content->ContentsEntry &&
1451 "Writing to AST an overriden file is not supported");
1452
Douglas Gregor258ae542009-04-27 06:38:32 +00001453 // The source location entry is a file. The blob associated
1454 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001455
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001456 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001457 Record.push_back(Content->OrigEntry->getSize());
1458 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001459
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001460 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001461 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001462 llvm::SmallString<128> FilePath(Filename);
1463 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001464 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001465
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001466 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001467 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001468 } else {
1469 // The source location entry is a buffer. The blob associated
1470 // with this entry contains the contents of the buffer.
1471
1472 // We add one to the size so that we capture the trailing NULL
1473 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1474 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001475 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001476 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001477 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001478 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1479 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001480 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001481 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001482 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001483 llvm::StringRef(Buffer->getBufferStart(),
1484 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001485
1486 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001487 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001488 }
1489 } else {
1490 // The source location entry is an instantiation.
1491 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1492 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1493 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1494 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1495
1496 // Compute the token length for this macro expansion.
1497 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001498 if (I + 1 != N)
1499 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001500 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1501 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1502 }
1503 }
1504
Douglas Gregor8f45df52009-04-16 22:23:12 +00001505 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001506
1507 if (SLocEntryOffsets.empty())
1508 return;
1509
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001510 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001511 // table is used for lazily loading source-location information.
1512 using namespace llvm;
1513 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001514 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001515 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1516 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1517 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1518 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001519
Douglas Gregor258ae542009-04-27 06:38:32 +00001520 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001521 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001522 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001523 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1524 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001525 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001526 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001527 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001528
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001529 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001530 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001531 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001532}
1533
Douglas Gregorc5046832009-04-27 18:38:38 +00001534//===----------------------------------------------------------------------===//
1535// Preprocessor Serialization
1536//===----------------------------------------------------------------------===//
1537
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001538static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1539 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1540 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1541 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1542 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1543 return X.first->getName().compare(Y.first->getName());
1544}
1545
Chris Lattnereeffaef2009-04-10 17:15:23 +00001546/// \brief Writes the block containing the serialized form of the
1547/// preprocessor.
1548///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001549void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001550 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001551
Chris Lattner0af3ba12009-04-13 01:29:17 +00001552 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1553 if (PP.getCounterValue() != 0) {
1554 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001555 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001556 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001557 }
1558
1559 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001560 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001561
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001562 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001563 // FIXME: use diagnostics subsystem for localization etc.
1564 if (PP.SawDateOrTime())
1565 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001566
Douglas Gregor796d76a2010-10-20 22:00:55 +00001567
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001568 // Loop over all the macro definitions that are live at the end of the file,
1569 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001570 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001571
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001572 // Construct the list of macro definitions that need to be serialized.
1573 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1574 MacrosToEmit;
1575 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001576 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1577 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001578 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001579 MacroDefinitionsSeen.insert(I->first);
1580 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1581 }
1582
1583 // Sort the set of macro definitions that need to be serialized by the
1584 // name of the macro, to provide a stable ordering.
1585 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1586 &compareMacroDefinitions);
1587
Douglas Gregor68051a72011-02-11 00:26:14 +00001588 // Resolve any identifiers that defined macros at the time they were
1589 // deserialized, adding them to the list of macros to emit (if appropriate).
1590 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1591 IdentifierInfo *Name
1592 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1593 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1594 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1595 }
1596
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001597 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1598 const IdentifierInfo *Name = MacrosToEmit[I].first;
1599 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001600 if (!MI)
1601 continue;
1602
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001603 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001604 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001605 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001606
1607 // FIXME: There is a (probably minor) optimization we could do here, if
1608 // the macro comes from the original PCH but the identifier comes from a
1609 // chained PCH, by storing the offset into the original PCH rather than
1610 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001611 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001612 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001613 continue;
1614
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001615 AddIdentifierRef(Name, Record);
1616 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001617 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1618 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001619
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001620 unsigned Code;
1621 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001622 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001623 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001624 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001625
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001626 Record.push_back(MI->isC99Varargs());
1627 Record.push_back(MI->isGNUVarargs());
1628 Record.push_back(MI->getNumArgs());
1629 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1630 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001631 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001632 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001633
Douglas Gregoraae92242010-03-19 21:51:54 +00001634 // If we have a detailed preprocessing record, record the macro definition
1635 // ID that corresponds to this macro.
1636 if (PPRec)
1637 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001638
Douglas Gregor8f45df52009-04-16 22:23:12 +00001639 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001640 Record.clear();
1641
Chris Lattner2199f5b2009-04-10 18:08:30 +00001642 // Emit the tokens array.
1643 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1644 // Note that we know that the preprocessor does not have any annotation
1645 // tokens in it because they are created by the parser, and thus can't be
1646 // in a macro definition.
1647 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001648
Chris Lattner2199f5b2009-04-10 18:08:30 +00001649 Record.push_back(Tok.getLocation().getRawEncoding());
1650 Record.push_back(Tok.getLength());
1651
Chris Lattner2199f5b2009-04-10 18:08:30 +00001652 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1653 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001654 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001655 // FIXME: Should translate token kind to a stable encoding.
1656 Record.push_back(Tok.getKind());
1657 // FIXME: Should translate token flags to a stable encoding.
1658 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001659
Sebastian Redl539c5062010-08-18 23:57:32 +00001660 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001661 Record.clear();
1662 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001663 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001664 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001665 Stream.ExitBlock();
1666
1667 if (PPRec)
1668 WritePreprocessorDetail(*PPRec);
1669}
1670
1671void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1672 if (PPRec.begin(Chain) == PPRec.end(Chain))
1673 return;
1674
1675 // Enter the preprocessor block.
1676 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001677
Douglas Gregoraae92242010-03-19 21:51:54 +00001678 // If the preprocessor has a preprocessing record, emit it.
1679 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001680 using namespace llvm;
1681
1682 // Set up the abbreviation for
1683 unsigned InclusionAbbrev = 0;
1684 {
1685 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1686 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1688 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1690 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1691 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1692 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1693 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1694 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1695 }
1696
1697 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1698 RecordData Record;
1699 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1700 EEnd = PPRec.end(Chain);
1701 E != EEnd; ++E) {
1702 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001703
Douglas Gregor92a96f52011-02-08 21:58:10 +00001704 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1705 // Record this macro definition's location.
1706 MacroID ID = getMacroDefinitionID(MD);
1707
1708 // Don't write the macro definition if it is from another AST file.
1709 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001710 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001711
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001712 // Notify the serialization listener that we're serializing this entity.
1713 if (SerializationListener)
1714 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001715 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001716
Douglas Gregor92a96f52011-02-08 21:58:10 +00001717 unsigned Position = ID - FirstMacroID;
1718 if (Position != MacroDefinitionOffsets.size()) {
1719 if (Position > MacroDefinitionOffsets.size())
1720 MacroDefinitionOffsets.resize(Position + 1);
1721
1722 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1723 } else
1724 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001725
Douglas Gregor92a96f52011-02-08 21:58:10 +00001726 Record.push_back(IndexBase + NumPreprocessingRecords++);
1727 Record.push_back(ID);
1728 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1729 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1730 AddIdentifierRef(MD->getName(), Record);
1731 AddSourceLocation(MD->getLocation(), Record);
1732 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1733 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001734 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001735
Douglas Gregor92a96f52011-02-08 21:58:10 +00001736 // Notify the serialization listener that we're serializing this entity.
1737 if (SerializationListener)
1738 SerializationListener->SerializedPreprocessedEntity(*E,
1739 Stream.GetCurrentBitNo());
1740
1741 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1742 Record.push_back(IndexBase + NumPreprocessingRecords++);
1743 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1744 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1745 AddIdentifierRef(MI->getName(), Record);
1746 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1747 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1748 continue;
1749 }
1750
1751 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1752 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1753 Record.push_back(IndexBase + NumPreprocessingRecords++);
1754 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1755 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1756 Record.push_back(ID->getFileName().size());
1757 Record.push_back(ID->wasInQuotes());
1758 Record.push_back(static_cast<unsigned>(ID->getKind()));
1759 llvm::SmallString<64> Buffer;
1760 Buffer += ID->getFileName();
1761 Buffer += ID->getFile()->getName();
1762 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1763 continue;
1764 }
1765
1766 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1767 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001768 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001769
Douglas Gregoraae92242010-03-19 21:51:54 +00001770 // Write the offsets table for the preprocessing record.
1771 if (NumPreprocessingRecords > 0) {
1772 // Write the offsets table for identifier IDs.
1773 using namespace llvm;
1774 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001775 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1779 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001780
Douglas Gregoraae92242010-03-19 21:51:54 +00001781 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001782 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001783 Record.push_back(NumPreprocessingRecords);
1784 Record.push_back(MacroDefinitionOffsets.size());
1785 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001786 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001787 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1788 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001789}
1790
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001791void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001792 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001793 for (Diagnostic::DiagStatePointsTy::const_iterator
1794 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1795 I != E; ++I) {
1796 const Diagnostic::DiagStatePoint &point = *I;
1797 if (point.Loc.isInvalid())
1798 continue;
1799
1800 Record.push_back(point.Loc.getRawEncoding());
1801 for (Diagnostic::DiagState::iterator
1802 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1803 unsigned diag = I->first, map = I->second;
1804 if (map & 0x10) { // mapping from a diagnostic pragma.
1805 Record.push_back(diag);
1806 Record.push_back(map & 0x7);
1807 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001808 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001809 Record.push_back(-1); // mark the end of the diag/map pairs for this
1810 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001811 }
1812
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001813 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001814 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001815}
1816
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001817void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1818 if (CXXBaseSpecifiersOffsets.empty())
1819 return;
1820
1821 RecordData Record;
1822
1823 // Create a blob abbreviation for the C++ base specifiers offsets.
1824 using namespace llvm;
1825
1826 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1827 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1829 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1830 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1831
1832 // Write the selector offsets table.
1833 Record.clear();
1834 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1835 Record.push_back(CXXBaseSpecifiersOffsets.size());
1836 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
1837 (const char *)CXXBaseSpecifiersOffsets.data(),
1838 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
1839}
1840
Douglas Gregorc5046832009-04-27 18:38:38 +00001841//===----------------------------------------------------------------------===//
1842// Type Serialization
1843//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001844
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001845/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001846void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001847 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001848 if (Idx.getIndex() == 0) // we haven't seen this type before.
1849 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001850
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001851 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001852
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001853 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001854 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001855 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001856 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001857 else if (TypeOffsets.size() < Index) {
1858 TypeOffsets.resize(Index + 1);
1859 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001860 }
1861
1862 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001863
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001864 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001865 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001866
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001867 if (T.hasLocalNonFastQualifiers()) {
1868 Qualifiers Qs = T.getLocalQualifiers();
1869 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001870 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001871 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001872 } else {
1873 switch (T->getTypeClass()) {
1874 // For all of the concrete, non-dependent types, call the
1875 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001876#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001877 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001878#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001879#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001880 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001881 }
1882
1883 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001884 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001885
1886 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001887 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001888}
1889
Douglas Gregorc5046832009-04-27 18:38:38 +00001890//===----------------------------------------------------------------------===//
1891// Declaration Serialization
1892//===----------------------------------------------------------------------===//
1893
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001894/// \brief Write the block containing all of the declaration IDs
1895/// lexically declared within the given DeclContext.
1896///
1897/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1898/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001899uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001900 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001901 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001902 return 0;
1903
Douglas Gregor8f45df52009-04-16 22:23:12 +00001904 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001905 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001906 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001907 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001908 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1909 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001910 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001911
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001912 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001913 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001914 reinterpret_cast<char*>(Decls.data()),
1915 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001916 return Offset;
1917}
1918
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001919void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001920 using namespace llvm;
1921 RecordData Record;
1922
1923 // Write the type offsets array
1924 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001925 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1928 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1929 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001931 Record.push_back(TypeOffsets.size());
1932 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001933 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001934 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1935
1936 // Write the declaration offsets array
1937 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001938 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1941 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1942 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001943 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001944 Record.push_back(DeclOffsets.size());
1945 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001946 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001947 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1948}
1949
Douglas Gregorc5046832009-04-27 18:38:38 +00001950//===----------------------------------------------------------------------===//
1951// Global Method Pool and Selector Serialization
1952//===----------------------------------------------------------------------===//
1953
Douglas Gregore84a9da2009-04-20 20:36:09 +00001954namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001955// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001956class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001957 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001958
1959public:
1960 typedef Selector key_type;
1961 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001962
Sebastian Redl834bb972010-08-04 17:20:04 +00001963 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001964 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001965 ObjCMethodList Instance, Factory;
1966 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001967 typedef const data_type& data_type_ref;
1968
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001969 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001970
Douglas Gregorc78d3462009-04-24 21:10:55 +00001971 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001972 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001973 }
Mike Stump11289f42009-09-09 15:08:12 +00001974
1975 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001976 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1977 data_type_ref Methods) {
1978 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1979 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001980 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1981 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001982 Method = Method->Next)
1983 if (Method->Method)
1984 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001985 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001986 Method = Method->Next)
1987 if (Method->Method)
1988 DataLen += 4;
1989 clang::io::Emit16(Out, DataLen);
1990 return std::make_pair(KeyLen, DataLen);
1991 }
Mike Stump11289f42009-09-09 15:08:12 +00001992
Douglas Gregor95c13f52009-04-25 17:48:32 +00001993 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001994 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001995 assert((Start >> 32) == 0 && "Selector key offset too large");
1996 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001997 unsigned N = Sel.getNumArgs();
1998 clang::io::Emit16(Out, N);
1999 if (N == 0)
2000 N = 1;
2001 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002002 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002003 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2004 }
Mike Stump11289f42009-09-09 15:08:12 +00002005
Douglas Gregorc78d3462009-04-24 21:10:55 +00002006 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002007 data_type_ref Methods, unsigned DataLen) {
2008 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002009 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002010 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002011 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002012 Method = Method->Next)
2013 if (Method->Method)
2014 ++NumInstanceMethods;
2015
2016 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002017 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002018 Method = Method->Next)
2019 if (Method->Method)
2020 ++NumFactoryMethods;
2021
2022 clang::io::Emit16(Out, NumInstanceMethods);
2023 clang::io::Emit16(Out, NumFactoryMethods);
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 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002028 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002029 Method = Method->Next)
2030 if (Method->Method)
2031 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002032
2033 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002034 }
2035};
2036} // end anonymous namespace
2037
Sebastian Redla19a67f2010-08-03 21:58:15 +00002038/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002039///
2040/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002041/// in an on-disk hash table indexed by the selector. The hash table also
2042/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002043void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002044 using namespace llvm;
2045
Sebastian Redla19a67f2010-08-03 21:58:15 +00002046 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002047 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002048 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002049 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002050 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002051 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002052 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002053 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002054
Sebastian Redla19a67f2010-08-03 21:58:15 +00002055 // Create the on-disk hash table representation. We walk through every
2056 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002057 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002058 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002059 I = SelectorIDs.begin(), E = SelectorIDs.end();
2060 I != E; ++I) {
2061 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002062 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002063 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002064 I->second,
2065 ObjCMethodList(),
2066 ObjCMethodList()
2067 };
2068 if (F != SemaRef.MethodPool.end()) {
2069 Data.Instance = F->second.first;
2070 Data.Factory = F->second.second;
2071 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002072 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002073 // changed.
2074 if (Chain && I->second < FirstSelectorID) {
2075 // Selector already exists. Did it change?
2076 bool changed = false;
2077 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2078 M = M->Next) {
2079 if (M->Method->getPCHLevel() == 0)
2080 changed = true;
2081 }
2082 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2083 M = M->Next) {
2084 if (M->Method->getPCHLevel() == 0)
2085 changed = true;
2086 }
2087 if (!changed)
2088 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002089 } else if (Data.Instance.Method || Data.Factory.Method) {
2090 // A new method pool entry.
2091 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002092 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002093 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002094 }
2095
Douglas Gregorc78d3462009-04-24 21:10:55 +00002096 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002097 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002098 uint32_t BucketOffset;
2099 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002100 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002101 llvm::raw_svector_ostream Out(MethodPool);
2102 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002103 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002104 BucketOffset = Generator.Emit(Out, Trait);
2105 }
2106
2107 // Create a blob abbreviation
2108 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002109 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002110 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002111 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2113 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2114
Douglas Gregor95c13f52009-04-25 17:48:32 +00002115 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002116 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002117 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002118 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002119 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002120 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002121
2122 // Create a blob abbreviation for the selector table offsets.
2123 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002124 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2127 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2128
2129 // Write the selector offsets table.
2130 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002131 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002132 Record.push_back(SelectorOffsets.size());
2133 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002134 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002135 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002136 }
2137}
2138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002139/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002140void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002141 using namespace llvm;
2142 if (SemaRef.ReferencedSelectors.empty())
2143 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002144
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002145 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002147 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002148 // very tricky to fix, and given that @selector shouldn't really appear in
2149 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002150 for (DenseMap<Selector, SourceLocation>::iterator S =
2151 SemaRef.ReferencedSelectors.begin(),
2152 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2153 Selector Sel = (*S).first;
2154 SourceLocation Loc = (*S).second;
2155 AddSelectorRef(Sel, Record);
2156 AddSourceLocation(Loc, Record);
2157 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002158 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002159}
2160
Douglas Gregorc5046832009-04-27 18:38:38 +00002161//===----------------------------------------------------------------------===//
2162// Identifier Table Serialization
2163//===----------------------------------------------------------------------===//
2164
Douglas Gregorc78d3462009-04-24 21:10:55 +00002165namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002166class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002167 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002168 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002169
Douglas Gregor1d583f22009-04-28 21:18:29 +00002170 /// \brief Determines whether this is an "interesting" identifier
2171 /// that needs a full IdentifierInfo structure written into the hash
2172 /// table.
2173 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2174 return II->isPoisoned() ||
2175 II->isExtensionToken() ||
2176 II->hasMacroDefinition() ||
2177 II->getObjCOrBuiltinID() ||
2178 II->getFETokenInfo<void>();
2179 }
2180
Douglas Gregore84a9da2009-04-20 20:36:09 +00002181public:
2182 typedef const IdentifierInfo* key_type;
2183 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002184
Sebastian Redl539c5062010-08-18 23:57:32 +00002185 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002186 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002187
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002188 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002189 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002190
2191 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002192 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002193 }
Mike Stump11289f42009-09-09 15:08:12 +00002194
2195 std::pair<unsigned,unsigned>
2196 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002197 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002198 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002199 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2200 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002201 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002202 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002203 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002204 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002205 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2206 DEnd = IdentifierResolver::end();
2207 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002208 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002209 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002210 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002211 // We emit the key length after the data length so that every
2212 // string is preceded by a 16-bit length. This matches the PTH
2213 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002214 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002215 return std::make_pair(KeyLen, DataLen);
2216 }
Mike Stump11289f42009-09-09 15:08:12 +00002217
2218 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002219 unsigned KeyLen) {
2220 // Record the location of the key data. This is used when generating
2221 // the mapping from persistent IDs to strings.
2222 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002223 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002224 }
Mike Stump11289f42009-09-09 15:08:12 +00002225
2226 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002227 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002228 if (!isInterestingIdentifier(II)) {
2229 clang::io::Emit32(Out, ID << 1);
2230 return;
2231 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002232
Douglas Gregor1d583f22009-04-28 21:18:29 +00002233 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002234 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002235 bool hasMacroDefinition =
2236 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002237 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002238 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002239 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2240 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2241 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002242 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002243 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002244 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002245
Douglas Gregorc3366a52009-04-21 23:56:24 +00002246 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002247 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002248
Douglas Gregora868bbd2009-04-21 22:25:48 +00002249 // Emit the declaration IDs in reverse order, because the
2250 // IdentifierResolver provides the declarations as they would be
2251 // visible (e.g., the function "stat" would come before the struct
2252 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2253 // adds declarations to the end of the list (so we need to see the
2254 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002255 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002256 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002257 IdentifierResolver::end());
2258 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2259 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002260 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002261 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002262 }
2263};
2264} // end anonymous namespace
2265
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002266/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002267///
2268/// The identifier table consists of a blob containing string data
2269/// (the actual identifiers themselves) and a separate "offsets" index
2270/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002271void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002272 using namespace llvm;
2273
2274 // Create and write out the blob that contains the identifier
2275 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002276 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002277 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002278 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002279
Douglas Gregore6648fb2009-04-28 20:33:11 +00002280 // Look for any identifiers that were named while processing the
2281 // headers, but are otherwise not needed. We add these to the hash
2282 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002283 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002284 // file.
2285 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2286 IDEnd = PP.getIdentifierTable().end();
2287 ID != IDEnd; ++ID)
2288 getIdentifierRef(ID->second);
2289
Sebastian Redlff4a2952010-07-23 23:49:55 +00002290 // Create the on-disk hash table representation. We only store offsets
2291 // for identifiers that appear here for the first time.
2292 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002293 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002294 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2295 ID != IDEnd; ++ID) {
2296 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002297 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002298 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002299 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002300
Douglas Gregore84a9da2009-04-20 20:36:09 +00002301 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002302 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002303 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002304 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002305 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002306 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002307 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002308 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002309 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002310 }
2311
2312 // Create a blob abbreviation
2313 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002314 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002317 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002318
2319 // Write the identifier table
2320 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002321 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002322 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002323 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002324 }
2325
2326 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002327 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002328 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2331 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2332
2333 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002334 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002335 Record.push_back(IdentifierOffsets.size());
2336 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002337 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002338 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002339}
2340
Douglas Gregorc5046832009-04-27 18:38:38 +00002341//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002342// DeclContext's Name Lookup Table Serialization
2343//===----------------------------------------------------------------------===//
2344
2345namespace {
2346// Trait used for the on-disk hash table used in the method pool.
2347class ASTDeclContextNameLookupTrait {
2348 ASTWriter &Writer;
2349
2350public:
2351 typedef DeclarationName key_type;
2352 typedef key_type key_type_ref;
2353
2354 typedef DeclContext::lookup_result data_type;
2355 typedef const data_type& data_type_ref;
2356
2357 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2358
2359 unsigned ComputeHash(DeclarationName Name) {
2360 llvm::FoldingSetNodeID ID;
2361 ID.AddInteger(Name.getNameKind());
2362
2363 switch (Name.getNameKind()) {
2364 case DeclarationName::Identifier:
2365 ID.AddString(Name.getAsIdentifierInfo()->getName());
2366 break;
2367 case DeclarationName::ObjCZeroArgSelector:
2368 case DeclarationName::ObjCOneArgSelector:
2369 case DeclarationName::ObjCMultiArgSelector:
2370 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2371 break;
2372 case DeclarationName::CXXConstructorName:
2373 case DeclarationName::CXXDestructorName:
2374 case DeclarationName::CXXConversionFunctionName:
2375 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2376 break;
2377 case DeclarationName::CXXOperatorName:
2378 ID.AddInteger(Name.getCXXOverloadedOperator());
2379 break;
2380 case DeclarationName::CXXLiteralOperatorName:
2381 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2382 case DeclarationName::CXXUsingDirective:
2383 break;
2384 }
2385
2386 return ID.ComputeHash();
2387 }
2388
2389 std::pair<unsigned,unsigned>
2390 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2391 data_type_ref Lookup) {
2392 unsigned KeyLen = 1;
2393 switch (Name.getNameKind()) {
2394 case DeclarationName::Identifier:
2395 case DeclarationName::ObjCZeroArgSelector:
2396 case DeclarationName::ObjCOneArgSelector:
2397 case DeclarationName::ObjCMultiArgSelector:
2398 case DeclarationName::CXXConstructorName:
2399 case DeclarationName::CXXDestructorName:
2400 case DeclarationName::CXXConversionFunctionName:
2401 case DeclarationName::CXXLiteralOperatorName:
2402 KeyLen += 4;
2403 break;
2404 case DeclarationName::CXXOperatorName:
2405 KeyLen += 1;
2406 break;
2407 case DeclarationName::CXXUsingDirective:
2408 break;
2409 }
2410 clang::io::Emit16(Out, KeyLen);
2411
2412 // 2 bytes for num of decls and 4 for each DeclID.
2413 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2414 clang::io::Emit16(Out, DataLen);
2415
2416 return std::make_pair(KeyLen, DataLen);
2417 }
2418
2419 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2420 using namespace clang::io;
2421
2422 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2423 Emit8(Out, Name.getNameKind());
2424 switch (Name.getNameKind()) {
2425 case DeclarationName::Identifier:
2426 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2427 break;
2428 case DeclarationName::ObjCZeroArgSelector:
2429 case DeclarationName::ObjCOneArgSelector:
2430 case DeclarationName::ObjCMultiArgSelector:
2431 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2432 break;
2433 case DeclarationName::CXXConstructorName:
2434 case DeclarationName::CXXDestructorName:
2435 case DeclarationName::CXXConversionFunctionName:
2436 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2437 break;
2438 case DeclarationName::CXXOperatorName:
2439 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2440 Emit8(Out, Name.getCXXOverloadedOperator());
2441 break;
2442 case DeclarationName::CXXLiteralOperatorName:
2443 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2444 break;
2445 case DeclarationName::CXXUsingDirective:
2446 break;
2447 }
2448 }
2449
2450 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2451 data_type Lookup, unsigned DataLen) {
2452 uint64_t Start = Out.tell(); (void)Start;
2453 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2454 for (; Lookup.first != Lookup.second; ++Lookup.first)
2455 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2456
2457 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2458 }
2459};
2460} // end anonymous namespace
2461
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002462/// \brief Write the block containing all of the declaration IDs
2463/// visible from the given DeclContext.
2464///
2465/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002466/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002467uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2468 DeclContext *DC) {
2469 if (DC->getPrimaryContext() != DC)
2470 return 0;
2471
2472 // Since there is no name lookup into functions or methods, don't bother to
2473 // build a visible-declarations table for these entities.
2474 if (DC->isFunctionOrMethod())
2475 return 0;
2476
2477 // If not in C++, we perform name lookup for the translation unit via the
2478 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2479 // FIXME: In C++ we need the visible declarations in order to "see" the
2480 // friend declarations, is there a way to do this without writing the table ?
2481 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2482 return 0;
2483
2484 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002485 if (DC->hasExternalVisibleStorage())
2486 DC->MaterializeVisibleDeclsFromExternalStorage();
2487 else
2488 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002489
2490 // Serialize the contents of the mapping used for lookup. Note that,
2491 // although we have two very different code paths, the serialized
2492 // representation is the same for both cases: a declaration name,
2493 // followed by a size, followed by references to the visible
2494 // declarations that have that name.
2495 uint64_t Offset = Stream.GetCurrentBitNo();
2496 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2497 if (!Map || Map->empty())
2498 return 0;
2499
2500 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2501 ASTDeclContextNameLookupTrait Trait(*this);
2502
2503 // Create the on-disk hash table representation.
2504 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2505 D != DEnd; ++D) {
2506 DeclarationName Name = D->first;
2507 DeclContext::lookup_result Result = D->second.getLookupResult();
2508 Generator.insert(Name, Result, Trait);
2509 }
2510
2511 // Create the on-disk hash table in a buffer.
2512 llvm::SmallString<4096> LookupTable;
2513 uint32_t BucketOffset;
2514 {
2515 llvm::raw_svector_ostream Out(LookupTable);
2516 // Make sure that no bucket is at offset 0
2517 clang::io::Emit32(Out, 0);
2518 BucketOffset = Generator.Emit(Out, Trait);
2519 }
2520
2521 // Write the lookup table
2522 RecordData Record;
2523 Record.push_back(DECL_CONTEXT_VISIBLE);
2524 Record.push_back(BucketOffset);
2525 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2526 LookupTable.str());
2527
2528 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2529 ++NumVisibleDeclContexts;
2530 return Offset;
2531}
2532
Sebastian Redla4071b42010-08-24 00:50:09 +00002533/// \brief Write an UPDATE_VISIBLE block for the given context.
2534///
2535/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2536/// DeclContext in a dependent AST file. As such, they only exist for the TU
2537/// (in C++) and for namespaces.
2538void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002539 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2540 if (!Map || Map->empty())
2541 return;
2542
2543 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2544 ASTDeclContextNameLookupTrait Trait(*this);
2545
2546 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002547 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2548 D != DEnd; ++D) {
2549 DeclarationName Name = D->first;
2550 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002551 // For any name that appears in this table, the results are complete, i.e.
2552 // they overwrite results from previous PCHs. Merging is always a mess.
2553 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002554 }
2555
2556 // Create the on-disk hash table in a buffer.
2557 llvm::SmallString<4096> LookupTable;
2558 uint32_t BucketOffset;
2559 {
2560 llvm::raw_svector_ostream Out(LookupTable);
2561 // Make sure that no bucket is at offset 0
2562 clang::io::Emit32(Out, 0);
2563 BucketOffset = Generator.Emit(Out, Trait);
2564 }
2565
2566 // Write the lookup table
2567 RecordData Record;
2568 Record.push_back(UPDATE_VISIBLE);
2569 Record.push_back(getDeclID(cast<Decl>(DC)));
2570 Record.push_back(BucketOffset);
2571 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2572}
2573
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002574/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2575void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2576 RecordData Record;
2577 Record.push_back(Opts.fp_contract);
2578 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2579}
2580
2581/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2582void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2583 if (!SemaRef.Context.getLangOptions().OpenCL)
2584 return;
2585
2586 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2587 RecordData Record;
2588#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2589#include "clang/Basic/OpenCLExtensions.def"
2590 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2591}
2592
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002593//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002594// General Serialization Routines
2595//===----------------------------------------------------------------------===//
2596
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002597/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002598void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002599 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002600 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2601 const Attr * A = *i;
2602 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2603 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002604
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002605#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002606
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002607 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002608}
2609
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002610void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002611 Record.push_back(Str.size());
2612 Record.insert(Record.end(), Str.begin(), Str.end());
2613}
2614
Douglas Gregore84a9da2009-04-20 20:36:09 +00002615/// \brief Note that the identifier II occurs at the given offset
2616/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002617void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002618 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002619 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002620 // up earlier in the chain and thus don't need an offset.
2621 if (ID >= FirstIdentID)
2622 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002623}
2624
Douglas Gregor95c13f52009-04-25 17:48:32 +00002625/// \brief Note that the selector Sel occurs at the given offset
2626/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002627void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002628 unsigned ID = SelectorIDs[Sel];
2629 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002630 // Don't record offsets for selectors that are also available in a different
2631 // file.
2632 if (ID < FirstSelectorID)
2633 return;
2634 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002635}
2636
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002637ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002638 : Stream(Stream), Chain(0), SerializationListener(0),
2639 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002640 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002641 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002642 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2643 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002644 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002645 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2646 NextCXXBaseSpecifiersID(1)
2647{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002648}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002649
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002650void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002651 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002652 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002653 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002654 Stream.Emit((unsigned)'C', 8);
2655 Stream.Emit((unsigned)'P', 8);
2656 Stream.Emit((unsigned)'C', 8);
2657 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002658
Chris Lattner28fa4e62009-04-26 22:26:21 +00002659 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002660
Sebastian Redl143413f2010-07-12 22:02:52 +00002661 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002662 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002663 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002664 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002665}
2666
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002667void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002668 const char *isysroot,
2669 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002670 using namespace llvm;
2671
2672 ASTContext &Context = SemaRef.Context;
2673 Preprocessor &PP = SemaRef.PP;
2674
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002675 // The translation unit is the first declaration we'll emit.
2676 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002677 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002678 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002679
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002680 // Make sure that we emit IdentifierInfos (and any attached
2681 // declarations) for builtins.
2682 {
2683 IdentifierTable &Table = PP.getIdentifierTable();
2684 llvm::SmallVector<const char *, 32> BuiltinNames;
2685 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2686 Context.getLangOptions().NoBuiltin);
2687 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2688 getIdentifierRef(&Table.get(BuiltinNames[I]));
2689 }
2690
Chris Lattner0c797362009-09-08 18:19:27 +00002691 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002692 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002693 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002694 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002695 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2696 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002697 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002698
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002699 // Build a record containing all of the file scoped decls in this file.
2700 RecordData UnusedFileScopedDecls;
2701 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2702 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002703
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002704 RecordData WeakUndeclaredIdentifiers;
2705 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2706 WeakUndeclaredIdentifiers.push_back(
2707 SemaRef.WeakUndeclaredIdentifiers.size());
2708 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2709 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2710 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2711 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2712 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2713 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2714 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2715 }
2716 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002717
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002718 // Build a record containing all of the locally-scoped external
2719 // declarations in this header file. Generally, this record will be
2720 // empty.
2721 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002722 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002723 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002724 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002725 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2726 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2727 TD != TDEnd; ++TD)
2728 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2729
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002730 // Build a record containing all of the ext_vector declarations.
2731 RecordData ExtVectorDecls;
2732 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2733 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2734
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002735 // Build a record containing all of the VTable uses information.
2736 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002737 if (!SemaRef.VTableUses.empty()) {
2738 VTableUses.push_back(SemaRef.VTableUses.size());
2739 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2740 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2741 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2742 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2743 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002744 }
2745
2746 // Build a record containing all of dynamic classes declarations.
2747 RecordData DynamicClasses;
2748 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2749 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2750
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002751 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002752 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002753 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002754 I = SemaRef.PendingInstantiations.begin(),
2755 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2756 AddDeclRef(I->first, PendingInstantiations);
2757 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002758 }
2759 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2760 "There are local ones at end of translation unit!");
2761
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002762 // Build a record containing some declaration references.
2763 RecordData SemaDeclRefs;
2764 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2765 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2766 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2767 }
2768
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002769 RecordData CUDASpecialDeclRefs;
2770 if (Context.getcudaConfigureCallDecl()) {
2771 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2772 }
2773
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002774 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002775 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002776 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002777 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002778 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002779 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002780 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002781 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002782 // Write the record of special types.
2783 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002784
Steve Naroffc277ad12009-07-18 15:33:26 +00002785 AddTypeRef(Context.getBuiltinVaListType(), Record);
2786 AddTypeRef(Context.getObjCIdType(), Record);
2787 AddTypeRef(Context.getObjCSelType(), Record);
2788 AddTypeRef(Context.getObjCProtoType(), Record);
2789 AddTypeRef(Context.getObjCClassType(), Record);
2790 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2791 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2792 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002793 AddTypeRef(Context.getjmp_bufType(), Record);
2794 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002795 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2796 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002797 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002798 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002799 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2800 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002801 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002802 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002803
Douglas Gregor1970d882009-04-26 03:49:13 +00002804 // Keep writing types and declarations until all types and
2805 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002806 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002807 WriteDeclsBlockAbbrevs();
2808 while (!DeclTypesToEmit.empty()) {
2809 DeclOrType DOT = DeclTypesToEmit.front();
2810 DeclTypesToEmit.pop();
2811 if (DOT.isType())
2812 WriteType(DOT.getType());
2813 else
2814 WriteDecl(Context, DOT.getDecl());
2815 }
2816 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002817
Douglas Gregor45053152009-10-17 17:25:45 +00002818 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002819 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002820 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002821 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002822 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002823 WriteFPPragmaOptions(SemaRef.getFPOptions());
2824 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002825
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002826 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002827 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002828
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002829 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002830
Douglas Gregord4df8652009-04-22 22:02:47 +00002831 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002832 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002833 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002834
2835 // Write the record containing tentative definitions.
2836 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002837 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002838
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002839 // Write the record containing unused file scoped decls.
2840 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002841 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002842
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002843 // Write the record containing weak undeclared identifiers.
2844 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002845 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002846 WeakUndeclaredIdentifiers);
2847
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002848 // Write the record containing locally-scoped external definitions.
2849 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002850 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002851 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002852
2853 // Write the record containing ext_vector type names.
2854 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002855 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002856
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002857 // Write the record containing VTable uses information.
2858 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002859 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002860
2861 // Write the record containing dynamic classes declarations.
2862 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002863 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002864
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002865 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002866 if (!PendingInstantiations.empty())
2867 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002868
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002869 // Write the record containing declaration references of Sema.
2870 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002871 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002872
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002873 // Write the record containing CUDA-specific declaration references.
2874 if (!CUDASpecialDeclRefs.empty())
2875 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2876
Douglas Gregor08f01292009-04-17 22:13:46 +00002877 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002878 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002879 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002880 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002881 Record.push_back(NumLexicalDeclContexts);
2882 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002883 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002884 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002885}
2886
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002887void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002888 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002889 using namespace llvm;
2890
2891 ASTContext &Context = SemaRef.Context;
2892 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002893
Sebastian Redl143413f2010-07-12 22:02:52 +00002894 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002895 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002896 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002897 if (StatCalls && !isysroot)
2898 WriteStatCache(*StatCalls);
2899 // FIXME: Source manager block should only write new stuff, which could be
2900 // done by tracking the largest ID in the chain
2901 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002902
2903 // The special types are in the chained PCH.
2904
2905 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002906 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002907 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002908 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002909 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2910 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002911 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002912 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002913 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002914 else if ((*I)->isChangedSinceDeserialization())
2915 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002916 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002917 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002918 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002919 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002920 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2921 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2922 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002923 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002924 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2925 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002926 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002927 // And a visible updates block for the DeclContexts.
2928 Abv = new llvm::BitCodeAbbrev();
2929 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2930 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2931 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2932 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2933 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2934 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002935
Sebastian Redl98912122010-07-27 23:01:28 +00002936 // Build a record containing all of the new tentative definitions in this
2937 // file, in TentativeDefinitions order.
2938 RecordData TentativeDefinitions;
2939 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2940 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2941 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2942 }
2943
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002944 // Build a record containing all of the file scoped decls in this file.
2945 RecordData UnusedFileScopedDecls;
2946 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2947 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2948 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002949 }
2950
Sebastian Redl08aca90252010-08-05 18:21:25 +00002951 // We write the entire table, overwriting the tables from the chain.
2952 RecordData WeakUndeclaredIdentifiers;
2953 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2954 WeakUndeclaredIdentifiers.push_back(
2955 SemaRef.WeakUndeclaredIdentifiers.size());
2956 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2957 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2958 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2959 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2960 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2961 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2962 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2963 }
2964 }
2965
Sebastian Redl98912122010-07-27 23:01:28 +00002966 // Build a record containing all of the locally-scoped external
2967 // declarations in this header file. Generally, this record will be
2968 // empty.
2969 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002970 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002971 // nondeterminstic!
2972 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2973 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2974 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2975 TD != TDEnd; ++TD) {
2976 if (TD->second->getPCHLevel() == 0)
2977 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2978 }
2979
2980 // Build a record containing all of the ext_vector declarations.
2981 RecordData ExtVectorDecls;
2982 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2983 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2984 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2985 }
2986
Sebastian Redl08aca90252010-08-05 18:21:25 +00002987 // Build a record containing all of the VTable uses information.
2988 // We write everything here, because it's too hard to determine whether
2989 // a use is new to this part.
2990 RecordData VTableUses;
2991 if (!SemaRef.VTableUses.empty()) {
2992 VTableUses.push_back(SemaRef.VTableUses.size());
2993 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2994 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2995 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2996 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2997 }
2998 }
2999
3000 // Build a record containing all of dynamic classes declarations.
3001 RecordData DynamicClasses;
3002 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3003 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3004 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3005
3006 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003007 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003008 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003009 I = SemaRef.PendingInstantiations.begin(),
3010 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00003011 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00003012 AddDeclRef(I->first, PendingInstantiations);
3013 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003014 }
3015 }
3016 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3017 "There are local ones at end of translation unit!");
3018
3019 // Build a record containing some declaration references.
3020 // It's not worth the effort to avoid duplication here.
3021 RecordData SemaDeclRefs;
3022 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3023 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3024 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3025 }
3026
Sebastian Redl539c5062010-08-18 23:57:32 +00003027 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003028 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003029 for (DeclsToRewriteTy::iterator
3030 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3031 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003032 while (!DeclTypesToEmit.empty()) {
3033 DeclOrType DOT = DeclTypesToEmit.front();
3034 DeclTypesToEmit.pop();
3035 if (DOT.isType())
3036 WriteType(DOT.getType());
3037 else
3038 WriteDecl(Context, DOT.getDecl());
3039 }
3040 Stream.ExitBlock();
3041
Sebastian Redl98912122010-07-27 23:01:28 +00003042 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003043 WriteSelectors(SemaRef);
3044 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003045 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003046 WriteFPPragmaOptions(SemaRef.getFPOptions());
3047 WriteOpenCLExtensions(SemaRef);
3048
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003049 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003050 // FIXME: For chained PCH only write the new mappings (we currently
3051 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003052 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003053
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003054 WriteCXXBaseSpecifiersOffsets();
3055
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003056 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003057 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003058 RecordData FirstLatestDeclIDs;
3059 for (FirstLatestDeclMap::iterator
3060 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3061 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3062 "Expected first & second to be in different PCHs");
3063 AddDeclRef(I->first, FirstLatestDeclIDs);
3064 AddDeclRef(I->second, FirstLatestDeclIDs);
3065 }
3066 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003067 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003068
Sebastian Redl98912122010-07-27 23:01:28 +00003069 // Write the record containing external, unnamed definitions.
3070 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003071 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003072
3073 // Write the record containing tentative definitions.
3074 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003075 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003076
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003077 // Write the record containing unused file scoped decls.
3078 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003079 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003080
Sebastian Redl08aca90252010-08-05 18:21:25 +00003081 // Write the record containing weak undeclared identifiers.
3082 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003083 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003084 WeakUndeclaredIdentifiers);
3085
Sebastian Redl98912122010-07-27 23:01:28 +00003086 // Write the record containing locally-scoped external definitions.
3087 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003088 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003089 LocallyScopedExternalDecls);
3090
3091 // Write the record containing ext_vector type names.
3092 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003093 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003094
Sebastian Redl08aca90252010-08-05 18:21:25 +00003095 // Write the record containing VTable uses information.
3096 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003097 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003098
3099 // Write the record containing dynamic classes declarations.
3100 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003101 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003102
3103 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003104 if (!PendingInstantiations.empty())
3105 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003106
3107 // Write the record containing declaration references of Sema.
3108 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003109 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003110
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003111 // Write the updates to DeclContexts.
3112 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3113 I = UpdatedDeclContexts.begin(),
3114 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003115 I != E; ++I)
3116 WriteDeclContextVisibleUpdate(*I);
3117
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003118 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003119
Sebastian Redl98912122010-07-27 23:01:28 +00003120 Record.clear();
3121 Record.push_back(NumStatements);
3122 Record.push_back(NumMacros);
3123 Record.push_back(NumLexicalDeclContexts);
3124 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003125 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003126 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003127 Stream.ExitBlock();
3128}
3129
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003130void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003131 if (DeclUpdates.empty())
3132 return;
3133
3134 RecordData OffsetsRecord;
3135 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3136 for (DeclUpdateMap::iterator
3137 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3138 const Decl *D = I->first;
3139 UpdateRecord &URec = I->second;
3140
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003141 if (DeclsToRewrite.count(D))
3142 continue; // The decl will be written completely,no need to store updates.
3143
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003144 uint64_t Offset = Stream.GetCurrentBitNo();
3145 Stream.EmitRecord(DECL_UPDATES, URec);
3146
3147 OffsetsRecord.push_back(GetDeclRef(D));
3148 OffsetsRecord.push_back(Offset);
3149 }
3150 Stream.ExitBlock();
3151 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3152}
3153
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003154void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003155 if (ReplacedDecls.empty())
3156 return;
3157
3158 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003159 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003160 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3161 Record.push_back(I->first);
3162 Record.push_back(I->second);
3163 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003164 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003165}
3166
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003167void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003168 Record.push_back(Loc.getRawEncoding());
3169}
3170
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003171void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003172 AddSourceLocation(Range.getBegin(), Record);
3173 AddSourceLocation(Range.getEnd(), Record);
3174}
3175
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003176void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003177 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003178 const uint64_t *Words = Value.getRawData();
3179 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003180}
3181
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003182void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003183 Record.push_back(Value.isUnsigned());
3184 AddAPInt(Value, Record);
3185}
3186
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003187void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003188 AddAPInt(Value.bitcastToAPInt(), Record);
3189}
3190
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003191void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003192 Record.push_back(getIdentifierRef(II));
3193}
3194
Sebastian Redl539c5062010-08-18 23:57:32 +00003195IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003196 if (II == 0)
3197 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003198
Sebastian Redl539c5062010-08-18 23:57:32 +00003199 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003200 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003201 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003202 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003203}
3204
Sebastian Redl50e26582010-09-15 19:54:06 +00003205MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003206 if (MD == 0)
3207 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003208
3209 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003210 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003211 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003212 return ID;
3213}
3214
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003215void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003216 Record.push_back(getSelectorRef(SelRef));
3217}
3218
Sebastian Redl539c5062010-08-18 23:57:32 +00003219SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003220 if (Sel.getAsOpaquePtr() == 0) {
3221 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003222 }
3223
Sebastian Redl539c5062010-08-18 23:57:32 +00003224 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003225 if (SID == 0 && Chain) {
3226 // This might trigger a ReadSelector callback, which will set the ID for
3227 // this selector.
3228 Chain->LoadSelector(Sel);
3229 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003230 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003231 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003232 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003233 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003234}
3235
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003236void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003237 AddDeclRef(Temp->getDestructor(), Record);
3238}
3239
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003240void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3241 CXXBaseSpecifier const *BasesEnd,
3242 RecordDataImpl &Record) {
3243 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3244 CXXBaseSpecifiersToWrite.push_back(
3245 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3246 Bases, BasesEnd));
3247 Record.push_back(NextCXXBaseSpecifiersID++);
3248}
3249
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003250void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003251 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003252 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003253 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003254 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003255 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003256 break;
3257 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003258 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003259 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003260 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003261 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003262 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003263 break;
3264 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003265 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003266 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003267 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003268 break;
John McCall0ad16662009-10-29 08:12:44 +00003269 case TemplateArgument::Null:
3270 case TemplateArgument::Integral:
3271 case TemplateArgument::Declaration:
3272 case TemplateArgument::Pack:
3273 break;
3274 }
3275}
3276
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003277void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003278 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003279 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003280
3281 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3282 bool InfoHasSameExpr
3283 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3284 Record.push_back(InfoHasSameExpr);
3285 if (InfoHasSameExpr)
3286 return; // Avoid storing the same expr twice.
3287 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003288 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3289 Record);
3290}
3291
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003292void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3293 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003294 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003295 AddTypeRef(QualType(), Record);
3296 return;
3297 }
3298
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003299 AddTypeLoc(TInfo->getTypeLoc(), Record);
3300}
3301
3302void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3303 AddTypeRef(TL.getType(), Record);
3304
John McCall8f115c62009-10-16 21:56:05 +00003305 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003306 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003307 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003308}
3309
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003310void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003311 Record.push_back(GetOrCreateTypeID(T));
3312}
3313
3314TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003315 return MakeTypeID(T,
3316 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3317}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003318
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003319TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003320 return MakeTypeID(T,
3321 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003322}
3323
3324TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3325 if (T.isNull())
3326 return TypeIdx();
3327 assert(!T.getLocalFastQualifiers());
3328
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003329 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003330 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003331 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003332 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003333 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003334 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003335 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003336 return Idx;
3337}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003338
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003339TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003340 if (T.isNull())
3341 return TypeIdx();
3342 assert(!T.getLocalFastQualifiers());
3343
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003344 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3345 assert(I != TypeIdxs.end() && "Type not emitted!");
3346 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003347}
3348
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003349void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003350 Record.push_back(GetDeclRef(D));
3351}
3352
Sebastian Redl539c5062010-08-18 23:57:32 +00003353DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003354 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003355 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003356 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003357 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003358 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003359 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003360 // We haven't seen this declaration before. Give it a new ID and
3361 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003362 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003363 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003364 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3365 // We don't add it to the replacement collection here, because we don't
3366 // have the offset yet.
3367 DeclTypesToEmit.push(const_cast<Decl *>(D));
3368 // Reset the flag, so that we don't add this decl multiple times.
3369 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003370 }
3371
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003372 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003373}
3374
Sebastian Redl539c5062010-08-18 23:57:32 +00003375DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003376 if (D == 0)
3377 return 0;
3378
3379 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3380 return DeclIDs[D];
3381}
3382
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003383void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003384 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003385 Record.push_back(Name.getNameKind());
3386 switch (Name.getNameKind()) {
3387 case DeclarationName::Identifier:
3388 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3389 break;
3390
3391 case DeclarationName::ObjCZeroArgSelector:
3392 case DeclarationName::ObjCOneArgSelector:
3393 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003394 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003395 break;
3396
3397 case DeclarationName::CXXConstructorName:
3398 case DeclarationName::CXXDestructorName:
3399 case DeclarationName::CXXConversionFunctionName:
3400 AddTypeRef(Name.getCXXNameType(), Record);
3401 break;
3402
3403 case DeclarationName::CXXOperatorName:
3404 Record.push_back(Name.getCXXOverloadedOperator());
3405 break;
3406
Alexis Hunt3d221f22009-11-29 07:34:05 +00003407 case DeclarationName::CXXLiteralOperatorName:
3408 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3409 break;
3410
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003411 case DeclarationName::CXXUsingDirective:
3412 // No extra data to emit
3413 break;
3414 }
3415}
Chris Lattnerca025db2010-05-07 21:43:38 +00003416
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003417void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003418 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003419 switch (Name.getNameKind()) {
3420 case DeclarationName::CXXConstructorName:
3421 case DeclarationName::CXXDestructorName:
3422 case DeclarationName::CXXConversionFunctionName:
3423 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3424 break;
3425
3426 case DeclarationName::CXXOperatorName:
3427 AddSourceLocation(
3428 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3429 Record);
3430 AddSourceLocation(
3431 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3432 Record);
3433 break;
3434
3435 case DeclarationName::CXXLiteralOperatorName:
3436 AddSourceLocation(
3437 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3438 Record);
3439 break;
3440
3441 case DeclarationName::Identifier:
3442 case DeclarationName::ObjCZeroArgSelector:
3443 case DeclarationName::ObjCOneArgSelector:
3444 case DeclarationName::ObjCMultiArgSelector:
3445 case DeclarationName::CXXUsingDirective:
3446 break;
3447 }
3448}
3449
3450void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003451 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003452 AddDeclarationName(NameInfo.getName(), Record);
3453 AddSourceLocation(NameInfo.getLoc(), Record);
3454 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3455}
3456
3457void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003458 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003459 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003460 Record.push_back(Info.NumTemplParamLists);
3461 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3462 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3463}
3464
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003465void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003466 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003467 // Nested name specifiers usually aren't too long. I think that 8 would
3468 // typically accomodate the vast majority.
3469 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3470
3471 // Push each of the NNS's onto a stack for serialization in reverse order.
3472 while (NNS) {
3473 NestedNames.push_back(NNS);
3474 NNS = NNS->getPrefix();
3475 }
3476
3477 Record.push_back(NestedNames.size());
3478 while(!NestedNames.empty()) {
3479 NNS = NestedNames.pop_back_val();
3480 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3481 Record.push_back(Kind);
3482 switch (Kind) {
3483 case NestedNameSpecifier::Identifier:
3484 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3485 break;
3486
3487 case NestedNameSpecifier::Namespace:
3488 AddDeclRef(NNS->getAsNamespace(), Record);
3489 break;
3490
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003491 case NestedNameSpecifier::NamespaceAlias:
3492 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3493 break;
3494
Chris Lattnerca025db2010-05-07 21:43:38 +00003495 case NestedNameSpecifier::TypeSpec:
3496 case NestedNameSpecifier::TypeSpecWithTemplate:
3497 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3498 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3499 break;
3500
3501 case NestedNameSpecifier::Global:
3502 // Don't need to write an associated value.
3503 break;
3504 }
3505 }
3506}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003507
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003508void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3509 RecordDataImpl &Record) {
3510 // Nested name specifiers usually aren't too long. I think that 8 would
3511 // typically accomodate the vast majority.
3512 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3513
3514 // Push each of the nested-name-specifiers's onto a stack for
3515 // serialization in reverse order.
3516 while (NNS) {
3517 NestedNames.push_back(NNS);
3518 NNS = NNS.getPrefix();
3519 }
3520
3521 Record.push_back(NestedNames.size());
3522 while(!NestedNames.empty()) {
3523 NNS = NestedNames.pop_back_val();
3524 NestedNameSpecifier::SpecifierKind Kind
3525 = NNS.getNestedNameSpecifier()->getKind();
3526 Record.push_back(Kind);
3527 switch (Kind) {
3528 case NestedNameSpecifier::Identifier:
3529 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3530 AddSourceRange(NNS.getLocalSourceRange(), Record);
3531 break;
3532
3533 case NestedNameSpecifier::Namespace:
3534 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3535 AddSourceRange(NNS.getLocalSourceRange(), Record);
3536 break;
3537
3538 case NestedNameSpecifier::NamespaceAlias:
3539 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3540 AddSourceRange(NNS.getLocalSourceRange(), Record);
3541 break;
3542
3543 case NestedNameSpecifier::TypeSpec:
3544 case NestedNameSpecifier::TypeSpecWithTemplate:
3545 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3546 AddTypeLoc(NNS.getTypeLoc(), Record);
3547 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3548 break;
3549
3550 case NestedNameSpecifier::Global:
3551 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3552 break;
3553 }
3554 }
3555}
3556
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003557void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003558 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003559 Record.push_back(Kind);
3560 switch (Kind) {
3561 case TemplateName::Template:
3562 AddDeclRef(Name.getAsTemplateDecl(), Record);
3563 break;
3564
3565 case TemplateName::OverloadedTemplate: {
3566 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3567 Record.push_back(OvT->size());
3568 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3569 I != E; ++I)
3570 AddDeclRef(*I, Record);
3571 break;
3572 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003573
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003574 case TemplateName::QualifiedTemplate: {
3575 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3576 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3577 Record.push_back(QualT->hasTemplateKeyword());
3578 AddDeclRef(QualT->getTemplateDecl(), Record);
3579 break;
3580 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003581
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003582 case TemplateName::DependentTemplate: {
3583 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3584 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3585 Record.push_back(DepT->isIdentifier());
3586 if (DepT->isIdentifier())
3587 AddIdentifierRef(DepT->getIdentifier(), Record);
3588 else
3589 Record.push_back(DepT->getOperator());
3590 break;
3591 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003592
3593 case TemplateName::SubstTemplateTemplateParmPack: {
3594 SubstTemplateTemplateParmPackStorage *SubstPack
3595 = Name.getAsSubstTemplateTemplateParmPack();
3596 AddDeclRef(SubstPack->getParameterPack(), Record);
3597 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3598 break;
3599 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003600 }
3601}
3602
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003603void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003604 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003605 Record.push_back(Arg.getKind());
3606 switch (Arg.getKind()) {
3607 case TemplateArgument::Null:
3608 break;
3609 case TemplateArgument::Type:
3610 AddTypeRef(Arg.getAsType(), Record);
3611 break;
3612 case TemplateArgument::Declaration:
3613 AddDeclRef(Arg.getAsDecl(), Record);
3614 break;
3615 case TemplateArgument::Integral:
3616 AddAPSInt(*Arg.getAsIntegral(), Record);
3617 AddTypeRef(Arg.getIntegralType(), Record);
3618 break;
3619 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003620 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3621 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003622 case TemplateArgument::TemplateExpansion:
3623 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003624 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3625 Record.push_back(*NumExpansions + 1);
3626 else
3627 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003628 break;
3629 case TemplateArgument::Expression:
3630 AddStmt(Arg.getAsExpr());
3631 break;
3632 case TemplateArgument::Pack:
3633 Record.push_back(Arg.pack_size());
3634 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3635 I != E; ++I)
3636 AddTemplateArgument(*I, Record);
3637 break;
3638 }
3639}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003640
3641void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003642ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003643 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003644 assert(TemplateParams && "No TemplateParams!");
3645 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3646 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3647 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3648 Record.push_back(TemplateParams->size());
3649 for (TemplateParameterList::const_iterator
3650 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3651 P != PEnd; ++P)
3652 AddDeclRef(*P, Record);
3653}
3654
3655/// \brief Emit a template argument list.
3656void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003657ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003658 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003659 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003660 Record.push_back(TemplateArgs->size());
3661 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003662 AddTemplateArgument(TemplateArgs->get(i), Record);
3663}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003664
3665
3666void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003667ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003668 Record.push_back(Set.size());
3669 for (UnresolvedSetImpl::const_iterator
3670 I = Set.begin(), E = Set.end(); I != E; ++I) {
3671 AddDeclRef(I.getDecl(), Record);
3672 Record.push_back(I.getAccess());
3673 }
3674}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003675
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003676void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003677 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003678 Record.push_back(Base.isVirtual());
3679 Record.push_back(Base.isBaseOfClass());
3680 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003681 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003682 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003683 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003684 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3685 : SourceLocation(),
3686 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003687}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003688
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003689void ASTWriter::FlushCXXBaseSpecifiers() {
3690 RecordData Record;
3691 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3692 Record.clear();
3693
3694 // Record the offset of this base-specifier set.
3695 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3696 if (Index == CXXBaseSpecifiersOffsets.size())
3697 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3698 else {
3699 if (Index > CXXBaseSpecifiersOffsets.size())
3700 CXXBaseSpecifiersOffsets.resize(Index + 1);
3701 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3702 }
3703
3704 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3705 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3706 Record.push_back(BEnd - B);
3707 for (; B != BEnd; ++B)
3708 AddCXXBaseSpecifier(*B, Record);
3709 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003710
3711 // Flush any expressions that were written as part of the base specifiers.
3712 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003713 }
3714
3715 CXXBaseSpecifiersToWrite.clear();
3716}
3717
Alexis Hunt1d792652011-01-08 20:30:50 +00003718void ASTWriter::AddCXXCtorInitializers(
3719 const CXXCtorInitializer * const *CtorInitializers,
3720 unsigned NumCtorInitializers,
3721 RecordDataImpl &Record) {
3722 Record.push_back(NumCtorInitializers);
3723 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3724 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003725
3726 Record.push_back(Init->isBaseInitializer());
3727 if (Init->isBaseInitializer()) {
3728 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3729 Record.push_back(Init->isBaseVirtual());
3730 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003731 Record.push_back(Init->isIndirectMemberInitializer());
3732 if (Init->isIndirectMemberInitializer())
3733 AddDeclRef(Init->getIndirectMember(), Record);
3734 else
3735 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003736 }
Francois Pichetd583da02010-12-04 09:14:42 +00003737
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003738 AddSourceLocation(Init->getMemberLocation(), Record);
3739 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003740 AddSourceLocation(Init->getLParenLoc(), Record);
3741 AddSourceLocation(Init->getRParenLoc(), Record);
3742 Record.push_back(Init->isWritten());
3743 if (Init->isWritten()) {
3744 Record.push_back(Init->getSourceOrder());
3745 } else {
3746 Record.push_back(Init->getNumArrayIndices());
3747 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3748 AddDeclRef(Init->getArrayIndex(i), Record);
3749 }
3750 }
3751}
3752
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003753void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3754 assert(D->DefinitionData);
3755 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3756 Record.push_back(Data.UserDeclaredConstructor);
3757 Record.push_back(Data.UserDeclaredCopyConstructor);
3758 Record.push_back(Data.UserDeclaredCopyAssignment);
3759 Record.push_back(Data.UserDeclaredDestructor);
3760 Record.push_back(Data.Aggregate);
3761 Record.push_back(Data.PlainOldData);
3762 Record.push_back(Data.Empty);
3763 Record.push_back(Data.Polymorphic);
3764 Record.push_back(Data.Abstract);
3765 Record.push_back(Data.HasTrivialConstructor);
3766 Record.push_back(Data.HasTrivialCopyConstructor);
3767 Record.push_back(Data.HasTrivialCopyAssignment);
3768 Record.push_back(Data.HasTrivialDestructor);
3769 Record.push_back(Data.ComputedVisibleConversions);
3770 Record.push_back(Data.DeclaredDefaultConstructor);
3771 Record.push_back(Data.DeclaredCopyConstructor);
3772 Record.push_back(Data.DeclaredCopyAssignment);
3773 Record.push_back(Data.DeclaredDestructor);
3774
3775 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003776 if (Data.NumBases > 0)
3777 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3778 Record);
3779
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003780 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3781 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003782 if (Data.NumVBases > 0)
3783 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3784 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003785
3786 AddUnresolvedSet(Data.Conversions, Record);
3787 AddUnresolvedSet(Data.VisibleConversions, Record);
3788 // Data.Definition is the owning decl, no need to write it.
3789 AddDeclRef(Data.FirstFriend, Record);
3790}
3791
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003792void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003793 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003794 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003795 assert(FirstDeclID == NextDeclID &&
3796 FirstTypeID == NextTypeID &&
3797 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003798 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003799 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003800 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003801 "Setting chain after writing has started.");
3802 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003803
3804 FirstDeclID += Chain->getTotalNumDecls();
3805 FirstTypeID += Chain->getTotalNumTypes();
3806 FirstIdentID += Chain->getTotalNumIdentifiers();
3807 FirstSelectorID += Chain->getTotalNumSelectors();
3808 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003809 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003810 NextDeclID = FirstDeclID;
3811 NextTypeID = FirstTypeID;
3812 NextIdentID = FirstIdentID;
3813 NextSelectorID = FirstSelectorID;
3814 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003815 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003816}
3817
Sebastian Redl539c5062010-08-18 23:57:32 +00003818void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003819 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003820 if (II->hasMacroDefinition())
3821 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003822}
3823
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003824void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003825 // Always take the highest-numbered type index. This copes with an interesting
3826 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003827 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003828 // keep the higher-numbered entry so that we can properly write it out to
3829 // the AST file.
3830 TypeIdx &StoredIdx = TypeIdxs[T];
3831 if (Idx.getIndex() >= StoredIdx.getIndex())
3832 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003833}
3834
Sebastian Redl539c5062010-08-18 23:57:32 +00003835void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003836 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003837}
Sebastian Redl834bb972010-08-04 17:20:04 +00003838
Sebastian Redl539c5062010-08-18 23:57:32 +00003839void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003840 SelectorIDs[S] = ID;
3841}
Douglas Gregor91096292010-10-02 19:29:26 +00003842
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003843void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003844 MacroDefinition *MD) {
3845 MacroDefinitions[MD] = ID;
3846}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003847
3848void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3849 assert(D->isDefinition());
3850 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3851 // We are interested when a PCH decl is modified.
3852 if (RD->getPCHLevel() > 0) {
3853 // A forward reference was mutated into a definition. Rewrite it.
3854 // FIXME: This happens during template instantiation, should we
3855 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003856 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003857 }
3858
3859 for (CXXRecordDecl::redecl_iterator
3860 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3861 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3862 if (Redecl == RD)
3863 continue;
3864
3865 // We are interested when a PCH decl is modified.
3866 if (Redecl->getPCHLevel() > 0) {
3867 UpdateRecord &Record = DeclUpdates[Redecl];
3868 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3869 assert(Redecl->DefinitionData);
3870 assert(Redecl->DefinitionData->Definition == D);
3871 AddDeclRef(D, Record); // the DefinitionDecl
3872 }
3873 }
3874 }
3875}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003876void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3877 // TU and namespaces are handled elsewhere.
3878 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3879 return;
3880
3881 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3882 return; // Not a source decl added to a DeclContext from PCH.
3883
3884 AddUpdatedDeclContext(DC);
3885}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003886
3887void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3888 assert(D->isImplicit());
3889 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3890 return; // Not a source member added to a class from PCH.
3891 if (!isa<CXXMethodDecl>(D))
3892 return; // We are interested in lazily declared implicit methods.
3893
3894 // A decl coming from PCH was modified.
3895 assert(RD->isDefinition());
3896 UpdateRecord &Record = DeclUpdates[RD];
3897 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3898 AddDeclRef(D, Record);
3899}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003900
3901void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3902 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003903 // The specializations set is kept in the canonical template.
3904 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003905 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3906 return; // Not a source specialization added to a template from PCH.
3907
3908 UpdateRecord &Record = DeclUpdates[TD];
3909 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3910 AddDeclRef(D, Record);
3911}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003912
3913ASTSerializationListener::~ASTSerializationListener() { }