blob: 78a01e29b7e03f2ae9c4f7c9f62de33f50e128f8 [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);
John McCall8f115c62009-10-16 21:56:05 +0000421}
John McCall17001972009-10-18 01:05:36 +0000422void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
424 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
425 Record.push_back(TL.getSizeExpr() ? 1 : 0);
426 if (TL.getSizeExpr())
427 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000428}
John McCall17001972009-10-18 01:05:36 +0000429void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
430 VisitArrayTypeLoc(TL);
431}
432void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
433 VisitArrayTypeLoc(TL);
434}
435void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
436 VisitArrayTypeLoc(TL);
437}
438void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
439 DependentSizedArrayTypeLoc TL) {
440 VisitArrayTypeLoc(TL);
441}
442void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
443 DependentSizedExtVectorTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
446void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
449void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
451}
452void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
454 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000455 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000456 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
457 Writer.AddDeclRef(TL.getArg(i), Record);
458}
459void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
460 VisitFunctionTypeLoc(TL);
461}
462void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
463 VisitFunctionTypeLoc(TL);
464}
John McCallb96ec562009-12-04 22:46:56 +0000465void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
John McCall17001972009-10-18 01:05:36 +0000468void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000472 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
473 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
474 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000475}
476void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000477 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
478 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
479 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
480 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000481}
482void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
484}
Richard Smith30482bc2011-02-20 03:19:35 +0000485void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
487}
John McCall17001972009-10-18 01:05:36 +0000488void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
490}
491void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
492 Writer.AddSourceLocation(TL.getNameLoc(), Record);
493}
John McCall81904512011-01-06 01:58:22 +0000494void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
496 if (TL.hasAttrOperand()) {
497 SourceRange range = TL.getAttrOperandParensRange();
498 Writer.AddSourceLocation(range.getBegin(), Record);
499 Writer.AddSourceLocation(range.getEnd(), Record);
500 }
501 if (TL.hasAttrExprOperand()) {
502 Expr *operand = TL.getAttrExprOperand();
503 Record.push_back(operand ? 1 : 0);
504 if (operand) Writer.AddStmt(operand);
505 } else if (TL.hasAttrEnumOperand()) {
506 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
507 }
508}
John McCall17001972009-10-18 01:05:36 +0000509void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
John McCallcebee162009-10-18 09:09:24 +0000512void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
513 SubstTemplateTypeParmTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
Douglas Gregorada4b792011-01-14 02:55:32 +0000516void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
517 SubstTemplateTypeParmPackTypeLoc TL) {
518 Writer.AddSourceLocation(TL.getNameLoc(), Record);
519}
John McCall17001972009-10-18 01:05:36 +0000520void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
521 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000522 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
523 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
524 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
525 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000526 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
527 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000528}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000529void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
531 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
532}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000533void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000534 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000535 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000536}
John McCalle78aac42010-03-10 03:28:59 +0000537void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000540void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000541 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000542 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCallc392f372010-06-11 00:33:02 +0000545void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
546 DependentTemplateSpecializationTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000548 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
551 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
552 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000553 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
554 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000555}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000556void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
558}
John McCall17001972009-10-18 01:05:36 +0000559void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000561}
562void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
563 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000564 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
565 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
566 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
567 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000568}
John McCallfc93cf92009-10-22 22:37:11 +0000569void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
570 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000571}
John McCall8f115c62009-10-16 21:56:05 +0000572
Chris Lattner19cea4e2009-04-22 05:57:30 +0000573//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000574// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000575//===----------------------------------------------------------------------===//
576
Chris Lattner28fa4e62009-04-26 22:26:21 +0000577static void EmitBlockID(unsigned ID, const char *Name,
578 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000579 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000580 Record.clear();
581 Record.push_back(ID);
582 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
583
584 // Emit the block name if present.
585 if (Name == 0 || Name[0] == 0) return;
586 Record.clear();
587 while (*Name)
588 Record.push_back(*Name++);
589 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
590}
591
592static void EmitRecordID(unsigned ID, const char *Name,
593 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000594 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000595 Record.clear();
596 Record.push_back(ID);
597 while (*Name)
598 Record.push_back(*Name++);
599 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000600}
601
602static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000603 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000604#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000605 RECORD(STMT_STOP);
606 RECORD(STMT_NULL_PTR);
607 RECORD(STMT_NULL);
608 RECORD(STMT_COMPOUND);
609 RECORD(STMT_CASE);
610 RECORD(STMT_DEFAULT);
611 RECORD(STMT_LABEL);
612 RECORD(STMT_IF);
613 RECORD(STMT_SWITCH);
614 RECORD(STMT_WHILE);
615 RECORD(STMT_DO);
616 RECORD(STMT_FOR);
617 RECORD(STMT_GOTO);
618 RECORD(STMT_INDIRECT_GOTO);
619 RECORD(STMT_CONTINUE);
620 RECORD(STMT_BREAK);
621 RECORD(STMT_RETURN);
622 RECORD(STMT_DECL);
623 RECORD(STMT_ASM);
624 RECORD(EXPR_PREDEFINED);
625 RECORD(EXPR_DECL_REF);
626 RECORD(EXPR_INTEGER_LITERAL);
627 RECORD(EXPR_FLOATING_LITERAL);
628 RECORD(EXPR_IMAGINARY_LITERAL);
629 RECORD(EXPR_STRING_LITERAL);
630 RECORD(EXPR_CHARACTER_LITERAL);
631 RECORD(EXPR_PAREN);
632 RECORD(EXPR_UNARY_OPERATOR);
633 RECORD(EXPR_SIZEOF_ALIGN_OF);
634 RECORD(EXPR_ARRAY_SUBSCRIPT);
635 RECORD(EXPR_CALL);
636 RECORD(EXPR_MEMBER);
637 RECORD(EXPR_BINARY_OPERATOR);
638 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
639 RECORD(EXPR_CONDITIONAL_OPERATOR);
640 RECORD(EXPR_IMPLICIT_CAST);
641 RECORD(EXPR_CSTYLE_CAST);
642 RECORD(EXPR_COMPOUND_LITERAL);
643 RECORD(EXPR_EXT_VECTOR_ELEMENT);
644 RECORD(EXPR_INIT_LIST);
645 RECORD(EXPR_DESIGNATED_INIT);
646 RECORD(EXPR_IMPLICIT_VALUE_INIT);
647 RECORD(EXPR_VA_ARG);
648 RECORD(EXPR_ADDR_LABEL);
649 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000650 RECORD(EXPR_CHOOSE);
651 RECORD(EXPR_GNU_NULL);
652 RECORD(EXPR_SHUFFLE_VECTOR);
653 RECORD(EXPR_BLOCK);
654 RECORD(EXPR_BLOCK_DECL_REF);
655 RECORD(EXPR_OBJC_STRING_LITERAL);
656 RECORD(EXPR_OBJC_ENCODE);
657 RECORD(EXPR_OBJC_SELECTOR_EXPR);
658 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
659 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
660 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
661 RECORD(EXPR_OBJC_KVC_REF_EXPR);
662 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000663 RECORD(STMT_OBJC_FOR_COLLECTION);
664 RECORD(STMT_OBJC_CATCH);
665 RECORD(STMT_OBJC_FINALLY);
666 RECORD(STMT_OBJC_AT_TRY);
667 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
668 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000669 RECORD(EXPR_CXX_OPERATOR_CALL);
670 RECORD(EXPR_CXX_CONSTRUCT);
671 RECORD(EXPR_CXX_STATIC_CAST);
672 RECORD(EXPR_CXX_DYNAMIC_CAST);
673 RECORD(EXPR_CXX_REINTERPRET_CAST);
674 RECORD(EXPR_CXX_CONST_CAST);
675 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
676 RECORD(EXPR_CXX_BOOL_LITERAL);
677 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000678 RECORD(EXPR_CXX_TYPEID_EXPR);
679 RECORD(EXPR_CXX_TYPEID_TYPE);
680 RECORD(EXPR_CXX_UUIDOF_EXPR);
681 RECORD(EXPR_CXX_UUIDOF_TYPE);
682 RECORD(EXPR_CXX_THIS);
683 RECORD(EXPR_CXX_THROW);
684 RECORD(EXPR_CXX_DEFAULT_ARG);
685 RECORD(EXPR_CXX_BIND_TEMPORARY);
686 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
687 RECORD(EXPR_CXX_NEW);
688 RECORD(EXPR_CXX_DELETE);
689 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
690 RECORD(EXPR_EXPR_WITH_CLEANUPS);
691 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
692 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
693 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
694 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
695 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
696 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
697 RECORD(EXPR_CXX_NOEXCEPT);
698 RECORD(EXPR_OPAQUE_VALUE);
699 RECORD(EXPR_BINARY_TYPE_TRAIT);
700 RECORD(EXPR_PACK_EXPANSION);
701 RECORD(EXPR_SIZEOF_PACK);
702 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000703 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000704#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000705}
Mike Stump11289f42009-09-09 15:08:12 +0000706
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000707void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000708 RecordData Record;
709 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000710
Sebastian Redl539c5062010-08-18 23:57:32 +0000711#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
712#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000713
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000714 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000715 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000716 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000717 RECORD(TYPE_OFFSET);
718 RECORD(DECL_OFFSET);
719 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000720 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000721 RECORD(IDENTIFIER_OFFSET);
722 RECORD(IDENTIFIER_TABLE);
723 RECORD(EXTERNAL_DEFINITIONS);
724 RECORD(SPECIAL_TYPES);
725 RECORD(STATISTICS);
726 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000727 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000728 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
729 RECORD(SELECTOR_OFFSETS);
730 RECORD(METHOD_POOL);
731 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000732 RECORD(SOURCE_LOCATION_OFFSETS);
733 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000734 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000735 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000736 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000737 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000738 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000739 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000740 RECORD(TU_UPDATE_LEXICAL);
741 RECORD(REDECLS_UPDATE_LATEST);
742 RECORD(SEMA_DECL_REFS);
743 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
744 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
745 RECORD(DECL_REPLACEMENTS);
746 RECORD(UPDATE_VISIBLE);
747 RECORD(DECL_UPDATE_OFFSETS);
748 RECORD(DECL_UPDATES);
749 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
750 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000751 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000752 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000753 RECORD(FP_PRAGMA_OPTIONS);
754 RECORD(OPENCL_EXTENSIONS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000755
Chris Lattner28fa4e62009-04-26 22:26:21 +0000756 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000757 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000758 RECORD(SM_SLOC_FILE_ENTRY);
759 RECORD(SM_SLOC_BUFFER_ENTRY);
760 RECORD(SM_SLOC_BUFFER_BLOB);
761 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
762 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattner28fa4e62009-04-26 22:26:21 +0000764 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000765 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000766 RECORD(PP_MACRO_OBJECT_LIKE);
767 RECORD(PP_MACRO_FUNCTION_LIKE);
768 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000769
Douglas Gregor12bfa382009-10-17 00:13:19 +0000770 // Decls and Types block.
771 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000772 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000773 RECORD(TYPE_COMPLEX);
774 RECORD(TYPE_POINTER);
775 RECORD(TYPE_BLOCK_POINTER);
776 RECORD(TYPE_LVALUE_REFERENCE);
777 RECORD(TYPE_RVALUE_REFERENCE);
778 RECORD(TYPE_MEMBER_POINTER);
779 RECORD(TYPE_CONSTANT_ARRAY);
780 RECORD(TYPE_INCOMPLETE_ARRAY);
781 RECORD(TYPE_VARIABLE_ARRAY);
782 RECORD(TYPE_VECTOR);
783 RECORD(TYPE_EXT_VECTOR);
784 RECORD(TYPE_FUNCTION_PROTO);
785 RECORD(TYPE_FUNCTION_NO_PROTO);
786 RECORD(TYPE_TYPEDEF);
787 RECORD(TYPE_TYPEOF_EXPR);
788 RECORD(TYPE_TYPEOF);
789 RECORD(TYPE_RECORD);
790 RECORD(TYPE_ENUM);
791 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000792 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000793 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000794 RECORD(TYPE_DECLTYPE);
795 RECORD(TYPE_ELABORATED);
796 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
797 RECORD(TYPE_UNRESOLVED_USING);
798 RECORD(TYPE_INJECTED_CLASS_NAME);
799 RECORD(TYPE_OBJC_OBJECT);
800 RECORD(TYPE_TEMPLATE_TYPE_PARM);
801 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
802 RECORD(TYPE_DEPENDENT_NAME);
803 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
804 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
805 RECORD(TYPE_PAREN);
806 RECORD(TYPE_PACK_EXPANSION);
807 RECORD(TYPE_ATTRIBUTED);
808 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000809 RECORD(DECL_TRANSLATION_UNIT);
810 RECORD(DECL_TYPEDEF);
811 RECORD(DECL_ENUM);
812 RECORD(DECL_RECORD);
813 RECORD(DECL_ENUM_CONSTANT);
814 RECORD(DECL_FUNCTION);
815 RECORD(DECL_OBJC_METHOD);
816 RECORD(DECL_OBJC_INTERFACE);
817 RECORD(DECL_OBJC_PROTOCOL);
818 RECORD(DECL_OBJC_IVAR);
819 RECORD(DECL_OBJC_AT_DEFS_FIELD);
820 RECORD(DECL_OBJC_CLASS);
821 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
822 RECORD(DECL_OBJC_CATEGORY);
823 RECORD(DECL_OBJC_CATEGORY_IMPL);
824 RECORD(DECL_OBJC_IMPLEMENTATION);
825 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
826 RECORD(DECL_OBJC_PROPERTY);
827 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000828 RECORD(DECL_FIELD);
829 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000830 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000831 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000832 RECORD(DECL_FILE_SCOPE_ASM);
833 RECORD(DECL_BLOCK);
834 RECORD(DECL_CONTEXT_LEXICAL);
835 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000836 RECORD(DECL_NAMESPACE);
837 RECORD(DECL_NAMESPACE_ALIAS);
838 RECORD(DECL_USING);
839 RECORD(DECL_USING_SHADOW);
840 RECORD(DECL_USING_DIRECTIVE);
841 RECORD(DECL_UNRESOLVED_USING_VALUE);
842 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
843 RECORD(DECL_LINKAGE_SPEC);
844 RECORD(DECL_CXX_RECORD);
845 RECORD(DECL_CXX_METHOD);
846 RECORD(DECL_CXX_CONSTRUCTOR);
847 RECORD(DECL_CXX_DESTRUCTOR);
848 RECORD(DECL_CXX_CONVERSION);
849 RECORD(DECL_ACCESS_SPEC);
850 RECORD(DECL_FRIEND);
851 RECORD(DECL_FRIEND_TEMPLATE);
852 RECORD(DECL_CLASS_TEMPLATE);
853 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
854 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
855 RECORD(DECL_FUNCTION_TEMPLATE);
856 RECORD(DECL_TEMPLATE_TYPE_PARM);
857 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
858 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
859 RECORD(DECL_STATIC_ASSERT);
860 RECORD(DECL_CXX_BASE_SPECIFIERS);
861 RECORD(DECL_INDIRECTFIELD);
862 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
863
Douglas Gregor92a96f52011-02-08 21:58:10 +0000864 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
865 RECORD(PPD_MACRO_INSTANTIATION);
866 RECORD(PPD_MACRO_DEFINITION);
867 RECORD(PPD_INCLUSION_DIRECTIVE);
868
Douglas Gregor12bfa382009-10-17 00:13:19 +0000869 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000870 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000871#undef RECORD
872#undef BLOCK
873 Stream.ExitBlock();
874}
875
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000876/// \brief Adjusts the given filename to only write out the portion of the
877/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000878///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000879/// \param Filename the file name to adjust.
880///
881/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
882/// the returned filename will be adjusted by this system root.
883///
884/// \returns either the original filename (if it needs no adjustment) or the
885/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000886static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000887adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
888 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000889
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000890 if (!isysroot)
891 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000892
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000893 // Verify that the filename and the system root have the same prefix.
894 unsigned Pos = 0;
895 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
896 if (Filename[Pos] != isysroot[Pos])
897 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000898
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000899 // We hit the end of the filename before we hit the end of the system root.
900 if (!Filename[Pos])
901 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000902
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000903 // If the file name has a '/' at the current position, skip over the '/'.
904 // We distinguish sysroot-based includes from absolute includes by the
905 // absence of '/' at the beginning of sysroot-based includes.
906 if (Filename[Pos] == '/')
907 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000908
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000909 return Filename + Pos;
910}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000911
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000912/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000913void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
914 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000915 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000916
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000917 // Metadata
918 const TargetInfo &Target = Context.Target;
919 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000920 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000921 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000922 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
923 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000924 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
925 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
926 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000927 // Target triple or chained PCH name
928 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000929 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000930
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000931 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000932 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
933 Record.push_back(VERSION_MAJOR);
934 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000935 Record.push_back(CLANG_VERSION_MAJOR);
936 Record.push_back(CLANG_VERSION_MINOR);
937 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000938 // FIXME: This writes the absolute path for chained headers.
939 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
940 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000941
Douglas Gregor45fe0362009-05-12 01:31:05 +0000942 // Original file name
943 SourceManager &SM = Context.getSourceManager();
944 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
945 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000946 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000947 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
948 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
949
Michael J. Spencer740857f2010-12-21 16:45:57 +0000950 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000951
Michael J. Spencer740857f2010-12-21 16:45:57 +0000952 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000953
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000954 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000955 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000956 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000957 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000958 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000959 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000960 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000961
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000962 // Original PCH directory
963 if (!OutputFile.empty() && OutputFile != "-") {
964 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
965 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
967 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
968
969 llvm::SmallString<128> OutputPath(OutputFile);
970
971 llvm::sys::fs::make_absolute(OutputPath);
972 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
973
974 RecordData Record;
975 Record.push_back(ORIGINAL_PCH_DIR);
976 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
977 }
978
Ted Kremenek18e066f2010-01-22 22:12:47 +0000979 // Repository branch/version information.
980 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000981 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000982 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
983 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000984 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000985 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000986 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
987 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000988}
989
990/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000991void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000992 RecordData Record;
993 Record.push_back(LangOpts.Trigraphs);
994 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
995 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
996 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
997 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000998 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000999 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1000 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1001 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1002 Record.push_back(LangOpts.C99); // C99 Support
1003 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001004 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1005 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001006 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1007 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001008 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001009
Douglas Gregor55abb232009-04-10 20:39:37 +00001010 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1011 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001012 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001013 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001014 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001015 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001016 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001017 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1018 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001019 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001020
Douglas Gregor55abb232009-04-10 20:39:37 +00001021 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001022 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1023 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001024 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001025 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001026 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001027 Record.push_back(LangOpts.CXXExceptions);
1028 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001029
Douglas Gregordbe39272011-02-01 15:15:22 +00001030 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001031 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1032 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1033 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1034
Chris Lattner258172e2009-04-27 07:35:58 +00001035 // Whether static initializers are protected by locks.
1036 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001037 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001038 Record.push_back(LangOpts.Blocks); // block extension to C
1039 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1040 // they are unused.
1041 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1042 // (modulo the platform support).
1043
Chris Lattner51924e512010-06-26 21:25:03 +00001044 Record.push_back(LangOpts.getSignedOverflowBehavior());
1045 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001046
1047 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001048 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001049 // defined.
1050 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1051 // opposed to __DYNAMIC__).
1052 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1053
1054 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1055 // used (instead of C99 semantics).
1056 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001057 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1058 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001059 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1060 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001061 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001062 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1063 // to the smallest integer type with
1064 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001065 Record.push_back(LangOpts.getGCMode());
1066 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001067 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001068 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001069 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001070 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001071 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001072 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001073 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001074 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001075 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001076 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001077}
1078
Douglas Gregora7f71a92009-04-10 03:52:48 +00001079//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001080// stat cache Serialization
1081//===----------------------------------------------------------------------===//
1082
1083namespace {
1084// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001085class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001086public:
1087 typedef const char * key_type;
1088 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001089
Chris Lattner2a6fa472010-11-23 19:28:12 +00001090 typedef struct stat data_type;
1091 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001092
1093 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001094 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001095 }
Mike Stump11289f42009-09-09 15:08:12 +00001096
1097 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001098 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1099 data_type_ref Data) {
1100 unsigned StrLen = strlen(path);
1101 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001102 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001103 clang::io::Emit8(Out, DataLen);
1104 return std::make_pair(StrLen + 1, DataLen);
1105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Douglas Gregorc5046832009-04-27 18:38:38 +00001107 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1108 Out.write(path, KeyLen);
1109 }
Mike Stump11289f42009-09-09 15:08:12 +00001110
Chris Lattner2a6fa472010-11-23 19:28:12 +00001111 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001112 data_type_ref Data, unsigned DataLen) {
1113 using namespace clang::io;
1114 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001115
Chris Lattner2a6fa472010-11-23 19:28:12 +00001116 Emit32(Out, (uint32_t) Data.st_ino);
1117 Emit32(Out, (uint32_t) Data.st_dev);
1118 Emit16(Out, (uint16_t) Data.st_mode);
1119 Emit64(Out, (uint64_t) Data.st_mtime);
1120 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001121
1122 assert(Out.tell() - Start == DataLen && "Wrong data length");
1123 }
1124};
1125} // end anonymous namespace
1126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001127/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001128void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001129 // Build the on-disk hash table containing information about every
1130 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001131 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001132 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001133 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001134 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001135 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1136 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001137 Generator.insert(Filename, Stat->second);
1138 }
Mike Stump11289f42009-09-09 15:08:12 +00001139
Douglas Gregorc5046832009-04-27 18:38:38 +00001140 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001141 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001142 uint32_t BucketOffset;
1143 {
1144 llvm::raw_svector_ostream Out(StatCacheData);
1145 // Make sure that no bucket is at offset 0
1146 clang::io::Emit32(Out, 0);
1147 BucketOffset = Generator.Emit(Out);
1148 }
1149
1150 // Create a blob abbreviation
1151 using namespace llvm;
1152 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001153 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1157 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1158
1159 // Write the stat cache
1160 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001161 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001162 Record.push_back(BucketOffset);
1163 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001164 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001165}
1166
1167//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001168// Source Manager Serialization
1169//===----------------------------------------------------------------------===//
1170
1171/// \brief Create an abbreviation for the SLocEntry that refers to a
1172/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001173static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001174 using namespace llvm;
1175 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001176 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001181 // FileEntry fields.
1182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001185 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001186}
1187
1188/// \brief Create an abbreviation for the SLocEntry that refers to a
1189/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001190static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001191 using namespace llvm;
1192 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001193 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001199 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001200}
1201
1202/// \brief Create an abbreviation for the SLocEntry that refers to a
1203/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001204static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001205 using namespace llvm;
1206 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001207 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001209 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001210}
1211
1212/// \brief Create an abbreviation for the SLocEntry that refers to an
1213/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001214static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001215 using namespace llvm;
1216 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001217 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001223 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001224}
1225
Douglas Gregor09b69892011-02-10 17:09:37 +00001226namespace {
1227 // Trait used for the on-disk hash table of header search information.
1228 class HeaderFileInfoTrait {
1229 ASTWriter &Writer;
1230 HeaderSearch &HS;
1231
1232 public:
1233 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1234 : Writer(Writer), HS(HS) { }
1235
1236 typedef const char *key_type;
1237 typedef key_type key_type_ref;
1238
1239 typedef HeaderFileInfo data_type;
1240 typedef const data_type &data_type_ref;
1241
1242 static unsigned ComputeHash(const char *path) {
1243 // The hash is based only on the filename portion of the key, so that the
1244 // reader can match based on filenames when symlinking or excess path
1245 // elements ("foo/../", "../") change the form of the name. However,
1246 // complete path is still the key.
1247 return llvm::HashString(llvm::sys::path::filename(path));
1248 }
1249
1250 std::pair<unsigned,unsigned>
1251 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1252 data_type_ref Data) {
1253 unsigned StrLen = strlen(path);
1254 clang::io::Emit16(Out, StrLen);
1255 unsigned DataLen = 1 + 2 + 4;
1256 clang::io::Emit8(Out, DataLen);
1257 return std::make_pair(StrLen + 1, DataLen);
1258 }
1259
1260 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1261 Out.write(path, KeyLen);
1262 }
1263
1264 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1265 data_type_ref Data, unsigned DataLen) {
1266 using namespace clang::io;
1267 uint64_t Start = Out.tell(); (void)Start;
1268
1269 unsigned char Flags = (Data.isImport << 3)
1270 | (Data.DirInfo << 1)
1271 | Data.Resolved;
1272 Emit8(Out, (uint8_t)Flags);
1273 Emit16(Out, (uint16_t) Data.NumIncludes);
1274
1275 if (!Data.ControllingMacro)
1276 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1277 else
1278 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1279 assert(Out.tell() - Start == DataLen && "Wrong data length");
1280 }
1281 };
1282} // end anonymous namespace
1283
1284/// \brief Write the header search block for the list of files that
1285///
1286/// \param HS The header search structure to save.
1287///
1288/// \param Chain Whether we're creating a chained AST file.
1289void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1290 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1291 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1292
1293 if (FilesByUID.size() > HS.header_file_size())
1294 FilesByUID.resize(HS.header_file_size());
1295
1296 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1297 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1298 llvm::SmallVector<const char *, 4> SavedStrings;
1299 unsigned NumHeaderSearchEntries = 0;
1300 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1301 const FileEntry *File = FilesByUID[UID];
1302 if (!File)
1303 continue;
1304
1305 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1306 if (HFI.External && Chain)
1307 continue;
1308
1309 // Turn the file name into an absolute path, if it isn't already.
1310 const char *Filename = File->getName();
1311 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1312
1313 // If we performed any translation on the file name at all, we need to
1314 // save this string, since the generator will refer to it later.
1315 if (Filename != File->getName()) {
1316 Filename = strdup(Filename);
1317 SavedStrings.push_back(Filename);
1318 }
1319
1320 Generator.insert(Filename, HFI, GeneratorTrait);
1321 ++NumHeaderSearchEntries;
1322 }
1323
1324 // Create the on-disk hash table in a buffer.
1325 llvm::SmallString<4096> TableData;
1326 uint32_t BucketOffset;
1327 {
1328 llvm::raw_svector_ostream Out(TableData);
1329 // Make sure that no bucket is at offset 0
1330 clang::io::Emit32(Out, 0);
1331 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1332 }
1333
1334 // Create a blob abbreviation
1335 using namespace llvm;
1336 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1337 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1341 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1342
1343 // Write the stat cache
1344 RecordData Record;
1345 Record.push_back(HEADER_SEARCH_TABLE);
1346 Record.push_back(BucketOffset);
1347 Record.push_back(NumHeaderSearchEntries);
1348 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1349
1350 // Free all of the strings we had to duplicate.
1351 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1352 free((void*)SavedStrings[I]);
1353}
1354
Douglas Gregora7f71a92009-04-10 03:52:48 +00001355/// \brief Writes the block containing the serialized form of the
1356/// source manager.
1357///
1358/// TODO: We should probably use an on-disk hash table (stored in a
1359/// blob), indexed based on the file name, so that we only create
1360/// entries for files that we actually need. In the common case (no
1361/// errors), we probably won't have to create file entries for any of
1362/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001363void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001364 const Preprocessor &PP,
1365 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001366 RecordData Record;
1367
Chris Lattner0910e3b2009-04-10 17:16:57 +00001368 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001369 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001370
1371 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001372 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1373 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1374 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1375 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001376
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001377 // Write the line table.
1378 if (SourceMgr.hasLineTable()) {
1379 LineTableInfo &LineTable = SourceMgr.getLineTable();
1380
1381 // Emit the file names
1382 Record.push_back(LineTable.getNumFilenames());
1383 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1384 // Emit the file name
1385 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001386 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001387 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1388 Record.push_back(FilenameLen);
1389 if (FilenameLen)
1390 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1391 }
Mike Stump11289f42009-09-09 15:08:12 +00001392
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001393 // Emit the line entries
1394 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1395 L != LEnd; ++L) {
1396 // Emit the file ID
1397 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001398
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001399 // Emit the line entries
1400 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001401 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001402 LEEnd = L->second.end();
1403 LE != LEEnd; ++LE) {
1404 Record.push_back(LE->FileOffset);
1405 Record.push_back(LE->LineNo);
1406 Record.push_back(LE->FilenameID);
1407 Record.push_back((unsigned)LE->FileKind);
1408 Record.push_back(LE->IncludeOffset);
1409 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001410 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001411 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001412 }
1413
Douglas Gregor258ae542009-04-27 06:38:32 +00001414 // Write out the source location entry table. We skip the first
1415 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001416 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001417 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001418 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1419 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1420 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1421 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001422 // Get this source location entry.
1423 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001424
Douglas Gregor258ae542009-04-27 06:38:32 +00001425 // Record the offset of this source-location entry.
1426 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1427
1428 // Figure out which record code to use.
1429 unsigned Code;
1430 if (SLoc->isFile()) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001431 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001432 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001433 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001434 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001435 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001436 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001437 Record.clear();
1438 Record.push_back(Code);
1439
1440 Record.push_back(SLoc->getOffset());
1441 if (SLoc->isFile()) {
1442 const SrcMgr::FileInfo &File = SLoc->getFile();
1443 Record.push_back(File.getIncludeLoc().getRawEncoding());
1444 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1445 Record.push_back(File.hasLineDirectives());
1446
1447 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001448 if (Content->OrigEntry) {
1449 assert(Content->OrigEntry == Content->ContentsEntry &&
1450 "Writing to AST an overriden file is not supported");
1451
Douglas Gregor258ae542009-04-27 06:38:32 +00001452 // The source location entry is a file. The blob associated
1453 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001454
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001455 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001456 Record.push_back(Content->OrigEntry->getSize());
1457 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001458
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001459 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001460 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001461 llvm::SmallString<128> FilePath(Filename);
1462 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001463 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001464
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001465 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001466 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001467 } else {
1468 // The source location entry is a buffer. The blob associated
1469 // with this entry contains the contents of the buffer.
1470
1471 // We add one to the size so that we capture the trailing NULL
1472 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1473 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001474 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001475 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001476 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001477 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1478 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001479 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001480 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001481 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001482 llvm::StringRef(Buffer->getBufferStart(),
1483 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001484
1485 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001486 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001487 }
1488 } else {
1489 // The source location entry is an instantiation.
1490 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1491 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1492 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1493 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1494
1495 // Compute the token length for this macro expansion.
1496 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001497 if (I + 1 != N)
1498 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001499 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1500 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1501 }
1502 }
1503
Douglas Gregor8f45df52009-04-16 22:23:12 +00001504 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001505
1506 if (SLocEntryOffsets.empty())
1507 return;
1508
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001509 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001510 // table is used for lazily loading source-location information.
1511 using namespace llvm;
1512 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001513 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1515 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1516 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1517 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001518
Douglas Gregor258ae542009-04-27 06:38:32 +00001519 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001520 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001521 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001522 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1523 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001524 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001525 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001526 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001527
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001528 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001529 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001530 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001531}
1532
Douglas Gregorc5046832009-04-27 18:38:38 +00001533//===----------------------------------------------------------------------===//
1534// Preprocessor Serialization
1535//===----------------------------------------------------------------------===//
1536
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001537static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1538 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1539 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1540 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1541 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1542 return X.first->getName().compare(Y.first->getName());
1543}
1544
Chris Lattnereeffaef2009-04-10 17:15:23 +00001545/// \brief Writes the block containing the serialized form of the
1546/// preprocessor.
1547///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001548void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001549 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001550
Chris Lattner0af3ba12009-04-13 01:29:17 +00001551 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1552 if (PP.getCounterValue() != 0) {
1553 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001554 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001555 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001556 }
1557
1558 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001559 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001560
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001561 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001562 // FIXME: use diagnostics subsystem for localization etc.
1563 if (PP.SawDateOrTime())
1564 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001565
Douglas Gregor796d76a2010-10-20 22:00:55 +00001566
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001567 // Loop over all the macro definitions that are live at the end of the file,
1568 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001569 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001570
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001571 // Construct the list of macro definitions that need to be serialized.
1572 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1573 MacrosToEmit;
1574 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001575 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1576 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001577 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001578 MacroDefinitionsSeen.insert(I->first);
1579 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1580 }
1581
1582 // Sort the set of macro definitions that need to be serialized by the
1583 // name of the macro, to provide a stable ordering.
1584 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1585 &compareMacroDefinitions);
1586
Douglas Gregor68051a72011-02-11 00:26:14 +00001587 // Resolve any identifiers that defined macros at the time they were
1588 // deserialized, adding them to the list of macros to emit (if appropriate).
1589 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1590 IdentifierInfo *Name
1591 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1592 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1593 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1594 }
1595
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001596 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1597 const IdentifierInfo *Name = MacrosToEmit[I].first;
1598 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001599 if (!MI)
1600 continue;
1601
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001602 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001603 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001604 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001605
1606 // FIXME: There is a (probably minor) optimization we could do here, if
1607 // the macro comes from the original PCH but the identifier comes from a
1608 // chained PCH, by storing the offset into the original PCH rather than
1609 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001610 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001611 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001612 continue;
1613
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001614 AddIdentifierRef(Name, Record);
1615 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001616 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1617 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001618
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001619 unsigned Code;
1620 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001621 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001622 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001623 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001624
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001625 Record.push_back(MI->isC99Varargs());
1626 Record.push_back(MI->isGNUVarargs());
1627 Record.push_back(MI->getNumArgs());
1628 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1629 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001630 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001631 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001632
Douglas Gregoraae92242010-03-19 21:51:54 +00001633 // If we have a detailed preprocessing record, record the macro definition
1634 // ID that corresponds to this macro.
1635 if (PPRec)
1636 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001637
Douglas Gregor8f45df52009-04-16 22:23:12 +00001638 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001639 Record.clear();
1640
Chris Lattner2199f5b2009-04-10 18:08:30 +00001641 // Emit the tokens array.
1642 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1643 // Note that we know that the preprocessor does not have any annotation
1644 // tokens in it because they are created by the parser, and thus can't be
1645 // in a macro definition.
1646 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001647
Chris Lattner2199f5b2009-04-10 18:08:30 +00001648 Record.push_back(Tok.getLocation().getRawEncoding());
1649 Record.push_back(Tok.getLength());
1650
Chris Lattner2199f5b2009-04-10 18:08:30 +00001651 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1652 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001653 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001654 // FIXME: Should translate token kind to a stable encoding.
1655 Record.push_back(Tok.getKind());
1656 // FIXME: Should translate token flags to a stable encoding.
1657 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001658
Sebastian Redl539c5062010-08-18 23:57:32 +00001659 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001660 Record.clear();
1661 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001662 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001663 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001664 Stream.ExitBlock();
1665
1666 if (PPRec)
1667 WritePreprocessorDetail(*PPRec);
1668}
1669
1670void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1671 if (PPRec.begin(Chain) == PPRec.end(Chain))
1672 return;
1673
1674 // Enter the preprocessor block.
1675 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001676
Douglas Gregoraae92242010-03-19 21:51:54 +00001677 // If the preprocessor has a preprocessing record, emit it.
1678 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001679 using namespace llvm;
1680
1681 // Set up the abbreviation for
1682 unsigned InclusionAbbrev = 0;
1683 {
1684 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1685 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1688 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1690 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1691 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1692 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1693 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1694 }
1695
1696 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1697 RecordData Record;
1698 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1699 EEnd = PPRec.end(Chain);
1700 E != EEnd; ++E) {
1701 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001702
Douglas Gregor92a96f52011-02-08 21:58:10 +00001703 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1704 // Record this macro definition's location.
1705 MacroID ID = getMacroDefinitionID(MD);
1706
1707 // Don't write the macro definition if it is from another AST file.
1708 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001709 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001710
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001711 // Notify the serialization listener that we're serializing this entity.
1712 if (SerializationListener)
1713 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001714 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001715
Douglas Gregor92a96f52011-02-08 21:58:10 +00001716 unsigned Position = ID - FirstMacroID;
1717 if (Position != MacroDefinitionOffsets.size()) {
1718 if (Position > MacroDefinitionOffsets.size())
1719 MacroDefinitionOffsets.resize(Position + 1);
1720
1721 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1722 } else
1723 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001724
Douglas Gregor92a96f52011-02-08 21:58:10 +00001725 Record.push_back(IndexBase + NumPreprocessingRecords++);
1726 Record.push_back(ID);
1727 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1728 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1729 AddIdentifierRef(MD->getName(), Record);
1730 AddSourceLocation(MD->getLocation(), Record);
1731 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1732 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001733 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001734
Douglas Gregor92a96f52011-02-08 21:58:10 +00001735 // Notify the serialization listener that we're serializing this entity.
1736 if (SerializationListener)
1737 SerializationListener->SerializedPreprocessedEntity(*E,
1738 Stream.GetCurrentBitNo());
1739
1740 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1741 Record.push_back(IndexBase + NumPreprocessingRecords++);
1742 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1743 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1744 AddIdentifierRef(MI->getName(), Record);
1745 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1746 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1747 continue;
1748 }
1749
1750 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1751 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1752 Record.push_back(IndexBase + NumPreprocessingRecords++);
1753 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1754 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1755 Record.push_back(ID->getFileName().size());
1756 Record.push_back(ID->wasInQuotes());
1757 Record.push_back(static_cast<unsigned>(ID->getKind()));
1758 llvm::SmallString<64> Buffer;
1759 Buffer += ID->getFileName();
1760 Buffer += ID->getFile()->getName();
1761 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1762 continue;
1763 }
1764
1765 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1766 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001767 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001768
Douglas Gregoraae92242010-03-19 21:51:54 +00001769 // Write the offsets table for the preprocessing record.
1770 if (NumPreprocessingRecords > 0) {
1771 // Write the offsets table for identifier IDs.
1772 using namespace llvm;
1773 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001774 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1778 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001779
Douglas Gregoraae92242010-03-19 21:51:54 +00001780 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001781 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001782 Record.push_back(NumPreprocessingRecords);
1783 Record.push_back(MacroDefinitionOffsets.size());
1784 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001785 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001786 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1787 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001788}
1789
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001790void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001791 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001792 for (Diagnostic::DiagStatePointsTy::const_iterator
1793 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1794 I != E; ++I) {
1795 const Diagnostic::DiagStatePoint &point = *I;
1796 if (point.Loc.isInvalid())
1797 continue;
1798
1799 Record.push_back(point.Loc.getRawEncoding());
1800 for (Diagnostic::DiagState::iterator
1801 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1802 unsigned diag = I->first, map = I->second;
1803 if (map & 0x10) { // mapping from a diagnostic pragma.
1804 Record.push_back(diag);
1805 Record.push_back(map & 0x7);
1806 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001807 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001808 Record.push_back(-1); // mark the end of the diag/map pairs for this
1809 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001810 }
1811
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001812 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001813 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001814}
1815
Douglas Gregorc5046832009-04-27 18:38:38 +00001816//===----------------------------------------------------------------------===//
1817// Type Serialization
1818//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001819
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001820/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001821void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001822 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001823 if (Idx.getIndex() == 0) // we haven't seen this type before.
1824 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001825
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001826 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001827
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001828 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001829 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001830 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001831 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001832 else if (TypeOffsets.size() < Index) {
1833 TypeOffsets.resize(Index + 1);
1834 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001835 }
1836
1837 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001838
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001839 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001840 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001841
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001842 if (T.hasLocalNonFastQualifiers()) {
1843 Qualifiers Qs = T.getLocalQualifiers();
1844 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001845 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001846 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001847 } else {
1848 switch (T->getTypeClass()) {
1849 // For all of the concrete, non-dependent types, call the
1850 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001851#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001852 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001853#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001854#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001855 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001856 }
1857
1858 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001859 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001860
1861 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001862 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001863}
1864
Douglas Gregorc5046832009-04-27 18:38:38 +00001865//===----------------------------------------------------------------------===//
1866// Declaration Serialization
1867//===----------------------------------------------------------------------===//
1868
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001869/// \brief Write the block containing all of the declaration IDs
1870/// lexically declared within the given DeclContext.
1871///
1872/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1873/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001874uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001875 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001876 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001877 return 0;
1878
Douglas Gregor8f45df52009-04-16 22:23:12 +00001879 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001880 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001881 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001882 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001883 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1884 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001885 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001886
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001887 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001888 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001889 reinterpret_cast<char*>(Decls.data()),
1890 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001891 return Offset;
1892}
1893
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001894void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001895 using namespace llvm;
1896 RecordData Record;
1897
1898 // Write the type offsets array
1899 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001900 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1903 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1904 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001905 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001906 Record.push_back(TypeOffsets.size());
1907 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001908 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001909 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1910
1911 // Write the declaration offsets array
1912 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001913 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1916 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1917 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001918 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001919 Record.push_back(DeclOffsets.size());
1920 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001921 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001922 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1923}
1924
Douglas Gregorc5046832009-04-27 18:38:38 +00001925//===----------------------------------------------------------------------===//
1926// Global Method Pool and Selector Serialization
1927//===----------------------------------------------------------------------===//
1928
Douglas Gregore84a9da2009-04-20 20:36:09 +00001929namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001930// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001931class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001932 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001933
1934public:
1935 typedef Selector key_type;
1936 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001937
Sebastian Redl834bb972010-08-04 17:20:04 +00001938 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001939 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001940 ObjCMethodList Instance, Factory;
1941 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001942 typedef const data_type& data_type_ref;
1943
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001944 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001945
Douglas Gregorc78d3462009-04-24 21:10:55 +00001946 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001947 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001948 }
Mike Stump11289f42009-09-09 15:08:12 +00001949
1950 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001951 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1952 data_type_ref Methods) {
1953 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1954 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001955 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1956 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001957 Method = Method->Next)
1958 if (Method->Method)
1959 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001960 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001961 Method = Method->Next)
1962 if (Method->Method)
1963 DataLen += 4;
1964 clang::io::Emit16(Out, DataLen);
1965 return std::make_pair(KeyLen, DataLen);
1966 }
Mike Stump11289f42009-09-09 15:08:12 +00001967
Douglas Gregor95c13f52009-04-25 17:48:32 +00001968 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001969 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001970 assert((Start >> 32) == 0 && "Selector key offset too large");
1971 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001972 unsigned N = Sel.getNumArgs();
1973 clang::io::Emit16(Out, N);
1974 if (N == 0)
1975 N = 1;
1976 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001977 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001978 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1979 }
Mike Stump11289f42009-09-09 15:08:12 +00001980
Douglas Gregorc78d3462009-04-24 21:10:55 +00001981 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001982 data_type_ref Methods, unsigned DataLen) {
1983 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001984 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001985 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001986 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001987 Method = Method->Next)
1988 if (Method->Method)
1989 ++NumInstanceMethods;
1990
1991 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001992 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001993 Method = Method->Next)
1994 if (Method->Method)
1995 ++NumFactoryMethods;
1996
1997 clang::io::Emit16(Out, NumInstanceMethods);
1998 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001999 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002000 Method = Method->Next)
2001 if (Method->Method)
2002 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002003 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002004 Method = Method->Next)
2005 if (Method->Method)
2006 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002007
2008 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002009 }
2010};
2011} // end anonymous namespace
2012
Sebastian Redla19a67f2010-08-03 21:58:15 +00002013/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002014///
2015/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002016/// in an on-disk hash table indexed by the selector. The hash table also
2017/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002018void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002019 using namespace llvm;
2020
Sebastian Redla19a67f2010-08-03 21:58:15 +00002021 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002022 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002023 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002024 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002025 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002026 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002027 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002028 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002029
Sebastian Redla19a67f2010-08-03 21:58:15 +00002030 // Create the on-disk hash table representation. We walk through every
2031 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002032 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002033 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002034 I = SelectorIDs.begin(), E = SelectorIDs.end();
2035 I != E; ++I) {
2036 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002037 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002038 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002039 I->second,
2040 ObjCMethodList(),
2041 ObjCMethodList()
2042 };
2043 if (F != SemaRef.MethodPool.end()) {
2044 Data.Instance = F->second.first;
2045 Data.Factory = F->second.second;
2046 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002047 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002048 // changed.
2049 if (Chain && I->second < FirstSelectorID) {
2050 // Selector already exists. Did it change?
2051 bool changed = false;
2052 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2053 M = M->Next) {
2054 if (M->Method->getPCHLevel() == 0)
2055 changed = true;
2056 }
2057 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2058 M = M->Next) {
2059 if (M->Method->getPCHLevel() == 0)
2060 changed = true;
2061 }
2062 if (!changed)
2063 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002064 } else if (Data.Instance.Method || Data.Factory.Method) {
2065 // A new method pool entry.
2066 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002067 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002068 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002069 }
2070
Douglas Gregorc78d3462009-04-24 21:10:55 +00002071 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002072 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002073 uint32_t BucketOffset;
2074 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002075 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002076 llvm::raw_svector_ostream Out(MethodPool);
2077 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002078 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002079 BucketOffset = Generator.Emit(Out, Trait);
2080 }
2081
2082 // Create a blob abbreviation
2083 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002084 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2088 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2089
Douglas Gregor95c13f52009-04-25 17:48:32 +00002090 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002091 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002092 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002093 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002094 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002095 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002096
2097 // Create a blob abbreviation for the selector table offsets.
2098 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002099 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2102 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2103
2104 // Write the selector offsets table.
2105 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002106 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002107 Record.push_back(SelectorOffsets.size());
2108 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002109 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002110 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002111 }
2112}
2113
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002114/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002115void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002116 using namespace llvm;
2117 if (SemaRef.ReferencedSelectors.empty())
2118 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002119
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002120 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002122 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002123 // very tricky to fix, and given that @selector shouldn't really appear in
2124 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002125 for (DenseMap<Selector, SourceLocation>::iterator S =
2126 SemaRef.ReferencedSelectors.begin(),
2127 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2128 Selector Sel = (*S).first;
2129 SourceLocation Loc = (*S).second;
2130 AddSelectorRef(Sel, Record);
2131 AddSourceLocation(Loc, Record);
2132 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002133 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002134}
2135
Douglas Gregorc5046832009-04-27 18:38:38 +00002136//===----------------------------------------------------------------------===//
2137// Identifier Table Serialization
2138//===----------------------------------------------------------------------===//
2139
Douglas Gregorc78d3462009-04-24 21:10:55 +00002140namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002141class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002142 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002143 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002144
Douglas Gregor1d583f22009-04-28 21:18:29 +00002145 /// \brief Determines whether this is an "interesting" identifier
2146 /// that needs a full IdentifierInfo structure written into the hash
2147 /// table.
2148 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2149 return II->isPoisoned() ||
2150 II->isExtensionToken() ||
2151 II->hasMacroDefinition() ||
2152 II->getObjCOrBuiltinID() ||
2153 II->getFETokenInfo<void>();
2154 }
2155
Douglas Gregore84a9da2009-04-20 20:36:09 +00002156public:
2157 typedef const IdentifierInfo* key_type;
2158 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002159
Sebastian Redl539c5062010-08-18 23:57:32 +00002160 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002161 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002162
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002163 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002164 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002165
2166 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002167 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002168 }
Mike Stump11289f42009-09-09 15:08:12 +00002169
2170 std::pair<unsigned,unsigned>
2171 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002172 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002173 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002174 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2175 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002176 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002177 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002178 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002179 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002180 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2181 DEnd = IdentifierResolver::end();
2182 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002183 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002184 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002185 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002186 // We emit the key length after the data length so that every
2187 // string is preceded by a 16-bit length. This matches the PTH
2188 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002189 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002190 return std::make_pair(KeyLen, DataLen);
2191 }
Mike Stump11289f42009-09-09 15:08:12 +00002192
2193 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002194 unsigned KeyLen) {
2195 // Record the location of the key data. This is used when generating
2196 // the mapping from persistent IDs to strings.
2197 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002198 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002199 }
Mike Stump11289f42009-09-09 15:08:12 +00002200
2201 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002202 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002203 if (!isInterestingIdentifier(II)) {
2204 clang::io::Emit32(Out, ID << 1);
2205 return;
2206 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002207
Douglas Gregor1d583f22009-04-28 21:18:29 +00002208 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002209 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002210 bool hasMacroDefinition =
2211 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002212 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002213 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002214 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2215 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2216 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002217 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002218 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002219 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002220
Douglas Gregorc3366a52009-04-21 23:56:24 +00002221 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002222 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002223
Douglas Gregora868bbd2009-04-21 22:25:48 +00002224 // Emit the declaration IDs in reverse order, because the
2225 // IdentifierResolver provides the declarations as they would be
2226 // visible (e.g., the function "stat" would come before the struct
2227 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2228 // adds declarations to the end of the list (so we need to see the
2229 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002230 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002231 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002232 IdentifierResolver::end());
2233 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2234 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002235 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002236 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002237 }
2238};
2239} // end anonymous namespace
2240
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002241/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002242///
2243/// The identifier table consists of a blob containing string data
2244/// (the actual identifiers themselves) and a separate "offsets" index
2245/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002246void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002247 using namespace llvm;
2248
2249 // Create and write out the blob that contains the identifier
2250 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002251 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002252 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002253 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002254
Douglas Gregore6648fb2009-04-28 20:33:11 +00002255 // Look for any identifiers that were named while processing the
2256 // headers, but are otherwise not needed. We add these to the hash
2257 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002258 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002259 // file.
2260 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2261 IDEnd = PP.getIdentifierTable().end();
2262 ID != IDEnd; ++ID)
2263 getIdentifierRef(ID->second);
2264
Sebastian Redlff4a2952010-07-23 23:49:55 +00002265 // Create the on-disk hash table representation. We only store offsets
2266 // for identifiers that appear here for the first time.
2267 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002268 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002269 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2270 ID != IDEnd; ++ID) {
2271 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002272 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002273 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002274 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002275
Douglas Gregore84a9da2009-04-20 20:36:09 +00002276 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002277 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002278 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002279 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002280 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002281 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002282 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002283 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002284 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002285 }
2286
2287 // Create a blob abbreviation
2288 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002289 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002292 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002293
2294 // Write the identifier table
2295 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002296 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002297 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002298 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002299 }
2300
2301 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002302 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002303 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2306 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2307
2308 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002309 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002310 Record.push_back(IdentifierOffsets.size());
2311 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002312 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002313 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002314}
2315
Douglas Gregorc5046832009-04-27 18:38:38 +00002316//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002317// DeclContext's Name Lookup Table Serialization
2318//===----------------------------------------------------------------------===//
2319
2320namespace {
2321// Trait used for the on-disk hash table used in the method pool.
2322class ASTDeclContextNameLookupTrait {
2323 ASTWriter &Writer;
2324
2325public:
2326 typedef DeclarationName key_type;
2327 typedef key_type key_type_ref;
2328
2329 typedef DeclContext::lookup_result data_type;
2330 typedef const data_type& data_type_ref;
2331
2332 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2333
2334 unsigned ComputeHash(DeclarationName Name) {
2335 llvm::FoldingSetNodeID ID;
2336 ID.AddInteger(Name.getNameKind());
2337
2338 switch (Name.getNameKind()) {
2339 case DeclarationName::Identifier:
2340 ID.AddString(Name.getAsIdentifierInfo()->getName());
2341 break;
2342 case DeclarationName::ObjCZeroArgSelector:
2343 case DeclarationName::ObjCOneArgSelector:
2344 case DeclarationName::ObjCMultiArgSelector:
2345 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2346 break;
2347 case DeclarationName::CXXConstructorName:
2348 case DeclarationName::CXXDestructorName:
2349 case DeclarationName::CXXConversionFunctionName:
2350 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2351 break;
2352 case DeclarationName::CXXOperatorName:
2353 ID.AddInteger(Name.getCXXOverloadedOperator());
2354 break;
2355 case DeclarationName::CXXLiteralOperatorName:
2356 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2357 case DeclarationName::CXXUsingDirective:
2358 break;
2359 }
2360
2361 return ID.ComputeHash();
2362 }
2363
2364 std::pair<unsigned,unsigned>
2365 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2366 data_type_ref Lookup) {
2367 unsigned KeyLen = 1;
2368 switch (Name.getNameKind()) {
2369 case DeclarationName::Identifier:
2370 case DeclarationName::ObjCZeroArgSelector:
2371 case DeclarationName::ObjCOneArgSelector:
2372 case DeclarationName::ObjCMultiArgSelector:
2373 case DeclarationName::CXXConstructorName:
2374 case DeclarationName::CXXDestructorName:
2375 case DeclarationName::CXXConversionFunctionName:
2376 case DeclarationName::CXXLiteralOperatorName:
2377 KeyLen += 4;
2378 break;
2379 case DeclarationName::CXXOperatorName:
2380 KeyLen += 1;
2381 break;
2382 case DeclarationName::CXXUsingDirective:
2383 break;
2384 }
2385 clang::io::Emit16(Out, KeyLen);
2386
2387 // 2 bytes for num of decls and 4 for each DeclID.
2388 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2389 clang::io::Emit16(Out, DataLen);
2390
2391 return std::make_pair(KeyLen, DataLen);
2392 }
2393
2394 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2395 using namespace clang::io;
2396
2397 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2398 Emit8(Out, Name.getNameKind());
2399 switch (Name.getNameKind()) {
2400 case DeclarationName::Identifier:
2401 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2402 break;
2403 case DeclarationName::ObjCZeroArgSelector:
2404 case DeclarationName::ObjCOneArgSelector:
2405 case DeclarationName::ObjCMultiArgSelector:
2406 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2407 break;
2408 case DeclarationName::CXXConstructorName:
2409 case DeclarationName::CXXDestructorName:
2410 case DeclarationName::CXXConversionFunctionName:
2411 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2412 break;
2413 case DeclarationName::CXXOperatorName:
2414 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2415 Emit8(Out, Name.getCXXOverloadedOperator());
2416 break;
2417 case DeclarationName::CXXLiteralOperatorName:
2418 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2419 break;
2420 case DeclarationName::CXXUsingDirective:
2421 break;
2422 }
2423 }
2424
2425 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2426 data_type Lookup, unsigned DataLen) {
2427 uint64_t Start = Out.tell(); (void)Start;
2428 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2429 for (; Lookup.first != Lookup.second; ++Lookup.first)
2430 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2431
2432 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2433 }
2434};
2435} // end anonymous namespace
2436
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002437/// \brief Write the block containing all of the declaration IDs
2438/// visible from the given DeclContext.
2439///
2440/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002441/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002442uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2443 DeclContext *DC) {
2444 if (DC->getPrimaryContext() != DC)
2445 return 0;
2446
2447 // Since there is no name lookup into functions or methods, don't bother to
2448 // build a visible-declarations table for these entities.
2449 if (DC->isFunctionOrMethod())
2450 return 0;
2451
2452 // If not in C++, we perform name lookup for the translation unit via the
2453 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2454 // FIXME: In C++ we need the visible declarations in order to "see" the
2455 // friend declarations, is there a way to do this without writing the table ?
2456 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2457 return 0;
2458
2459 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002460 if (DC->hasExternalVisibleStorage())
2461 DC->MaterializeVisibleDeclsFromExternalStorage();
2462 else
2463 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002464
2465 // Serialize the contents of the mapping used for lookup. Note that,
2466 // although we have two very different code paths, the serialized
2467 // representation is the same for both cases: a declaration name,
2468 // followed by a size, followed by references to the visible
2469 // declarations that have that name.
2470 uint64_t Offset = Stream.GetCurrentBitNo();
2471 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2472 if (!Map || Map->empty())
2473 return 0;
2474
2475 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2476 ASTDeclContextNameLookupTrait Trait(*this);
2477
2478 // Create the on-disk hash table representation.
2479 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2480 D != DEnd; ++D) {
2481 DeclarationName Name = D->first;
2482 DeclContext::lookup_result Result = D->second.getLookupResult();
2483 Generator.insert(Name, Result, Trait);
2484 }
2485
2486 // Create the on-disk hash table in a buffer.
2487 llvm::SmallString<4096> LookupTable;
2488 uint32_t BucketOffset;
2489 {
2490 llvm::raw_svector_ostream Out(LookupTable);
2491 // Make sure that no bucket is at offset 0
2492 clang::io::Emit32(Out, 0);
2493 BucketOffset = Generator.Emit(Out, Trait);
2494 }
2495
2496 // Write the lookup table
2497 RecordData Record;
2498 Record.push_back(DECL_CONTEXT_VISIBLE);
2499 Record.push_back(BucketOffset);
2500 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2501 LookupTable.str());
2502
2503 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2504 ++NumVisibleDeclContexts;
2505 return Offset;
2506}
2507
Sebastian Redla4071b42010-08-24 00:50:09 +00002508/// \brief Write an UPDATE_VISIBLE block for the given context.
2509///
2510/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2511/// DeclContext in a dependent AST file. As such, they only exist for the TU
2512/// (in C++) and for namespaces.
2513void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002514 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2515 if (!Map || Map->empty())
2516 return;
2517
2518 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2519 ASTDeclContextNameLookupTrait Trait(*this);
2520
2521 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002522 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2523 D != DEnd; ++D) {
2524 DeclarationName Name = D->first;
2525 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002526 // For any name that appears in this table, the results are complete, i.e.
2527 // they overwrite results from previous PCHs. Merging is always a mess.
2528 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002529 }
2530
2531 // Create the on-disk hash table in a buffer.
2532 llvm::SmallString<4096> LookupTable;
2533 uint32_t BucketOffset;
2534 {
2535 llvm::raw_svector_ostream Out(LookupTable);
2536 // Make sure that no bucket is at offset 0
2537 clang::io::Emit32(Out, 0);
2538 BucketOffset = Generator.Emit(Out, Trait);
2539 }
2540
2541 // Write the lookup table
2542 RecordData Record;
2543 Record.push_back(UPDATE_VISIBLE);
2544 Record.push_back(getDeclID(cast<Decl>(DC)));
2545 Record.push_back(BucketOffset);
2546 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2547}
2548
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002549/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2550void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2551 RecordData Record;
2552 Record.push_back(Opts.fp_contract);
2553 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2554}
2555
2556/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2557void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2558 if (!SemaRef.Context.getLangOptions().OpenCL)
2559 return;
2560
2561 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2562 RecordData Record;
2563#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2564#include "clang/Basic/OpenCLExtensions.def"
2565 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2566}
2567
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002568//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002569// General Serialization Routines
2570//===----------------------------------------------------------------------===//
2571
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002572/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002573void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002574 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002575 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2576 const Attr * A = *i;
2577 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2578 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002579
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002580#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002581
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002582 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002583}
2584
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002585void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002586 Record.push_back(Str.size());
2587 Record.insert(Record.end(), Str.begin(), Str.end());
2588}
2589
Douglas Gregore84a9da2009-04-20 20:36:09 +00002590/// \brief Note that the identifier II occurs at the given offset
2591/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002592void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002593 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002594 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002595 // up earlier in the chain and thus don't need an offset.
2596 if (ID >= FirstIdentID)
2597 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002598}
2599
Douglas Gregor95c13f52009-04-25 17:48:32 +00002600/// \brief Note that the selector Sel occurs at the given offset
2601/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002602void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002603 unsigned ID = SelectorIDs[Sel];
2604 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002605 // Don't record offsets for selectors that are also available in a different
2606 // file.
2607 if (ID < FirstSelectorID)
2608 return;
2609 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002610}
2611
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002612ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002613 : Stream(Stream), Chain(0), SerializationListener(0),
2614 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002615 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002616 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002617 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2618 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002619 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002620 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2621 NextCXXBaseSpecifiersID(1)
2622{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002623}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002624
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002625void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002626 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002627 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002628 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002629 Stream.Emit((unsigned)'C', 8);
2630 Stream.Emit((unsigned)'P', 8);
2631 Stream.Emit((unsigned)'C', 8);
2632 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002633
Chris Lattner28fa4e62009-04-26 22:26:21 +00002634 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002635
Sebastian Redl143413f2010-07-12 22:02:52 +00002636 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002637 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002638 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002639 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002640}
2641
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002642void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002643 const char *isysroot,
2644 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002645 using namespace llvm;
2646
2647 ASTContext &Context = SemaRef.Context;
2648 Preprocessor &PP = SemaRef.PP;
2649
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002650 // The translation unit is the first declaration we'll emit.
2651 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002652 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002653 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002654
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002655 // Make sure that we emit IdentifierInfos (and any attached
2656 // declarations) for builtins.
2657 {
2658 IdentifierTable &Table = PP.getIdentifierTable();
2659 llvm::SmallVector<const char *, 32> BuiltinNames;
2660 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2661 Context.getLangOptions().NoBuiltin);
2662 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2663 getIdentifierRef(&Table.get(BuiltinNames[I]));
2664 }
2665
Chris Lattner0c797362009-09-08 18:19:27 +00002666 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002667 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002668 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002669 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002670 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2671 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002672 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002673
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002674 // Build a record containing all of the file scoped decls in this file.
2675 RecordData UnusedFileScopedDecls;
2676 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2677 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002678
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002679 RecordData WeakUndeclaredIdentifiers;
2680 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2681 WeakUndeclaredIdentifiers.push_back(
2682 SemaRef.WeakUndeclaredIdentifiers.size());
2683 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2684 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2685 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2686 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2687 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2688 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2689 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2690 }
2691 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002692
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002693 // Build a record containing all of the locally-scoped external
2694 // declarations in this header file. Generally, this record will be
2695 // empty.
2696 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002697 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002698 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002699 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002700 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2701 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2702 TD != TDEnd; ++TD)
2703 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2704
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002705 // Build a record containing all of the ext_vector declarations.
2706 RecordData ExtVectorDecls;
2707 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2708 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2709
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002710 // Build a record containing all of the VTable uses information.
2711 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002712 if (!SemaRef.VTableUses.empty()) {
2713 VTableUses.push_back(SemaRef.VTableUses.size());
2714 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2715 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2716 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2717 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2718 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002719 }
2720
2721 // Build a record containing all of dynamic classes declarations.
2722 RecordData DynamicClasses;
2723 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2724 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2725
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002726 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002727 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002728 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002729 I = SemaRef.PendingInstantiations.begin(),
2730 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2731 AddDeclRef(I->first, PendingInstantiations);
2732 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002733 }
2734 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2735 "There are local ones at end of translation unit!");
2736
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002737 // Build a record containing some declaration references.
2738 RecordData SemaDeclRefs;
2739 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2740 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2741 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2742 }
2743
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002744 RecordData CUDASpecialDeclRefs;
2745 if (Context.getcudaConfigureCallDecl()) {
2746 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2747 }
2748
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002749 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002750 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002751 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002752 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002753 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002754 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002755 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002756 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002757 // Write the record of special types.
2758 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002759
Steve Naroffc277ad12009-07-18 15:33:26 +00002760 AddTypeRef(Context.getBuiltinVaListType(), Record);
2761 AddTypeRef(Context.getObjCIdType(), Record);
2762 AddTypeRef(Context.getObjCSelType(), Record);
2763 AddTypeRef(Context.getObjCProtoType(), Record);
2764 AddTypeRef(Context.getObjCClassType(), Record);
2765 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2766 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2767 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002768 AddTypeRef(Context.getjmp_bufType(), Record);
2769 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002770 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2771 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002772 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002773 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002774 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2775 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002776 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002777 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002778
Douglas Gregor1970d882009-04-26 03:49:13 +00002779 // Keep writing types and declarations until all types and
2780 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002781 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002782 WriteDeclsBlockAbbrevs();
2783 while (!DeclTypesToEmit.empty()) {
2784 DeclOrType DOT = DeclTypesToEmit.front();
2785 DeclTypesToEmit.pop();
2786 if (DOT.isType())
2787 WriteType(DOT.getType());
2788 else
2789 WriteDecl(Context, DOT.getDecl());
2790 }
2791 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002792
Douglas Gregor45053152009-10-17 17:25:45 +00002793 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002794 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002795 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002796 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002797 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002798 WriteFPPragmaOptions(SemaRef.getFPOptions());
2799 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002800
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002801 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002802 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002803
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002804 // Write the C++ base-specifier set offsets.
2805 if (!CXXBaseSpecifiersOffsets.empty()) {
2806 // Create a blob abbreviation for the C++ base specifiers offsets.
2807 using namespace llvm;
2808
2809 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2810 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2813 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2814
2815 // Write the selector offsets table.
2816 Record.clear();
2817 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2818 Record.push_back(CXXBaseSpecifiersOffsets.size());
2819 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2820 (const char *)CXXBaseSpecifiersOffsets.data(),
2821 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2822 }
2823
Douglas Gregord4df8652009-04-22 22:02:47 +00002824 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002825 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002826 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002827
2828 // Write the record containing tentative definitions.
2829 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002830 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002831
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002832 // Write the record containing unused file scoped decls.
2833 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002834 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002835
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002836 // Write the record containing weak undeclared identifiers.
2837 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002838 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002839 WeakUndeclaredIdentifiers);
2840
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002841 // Write the record containing locally-scoped external definitions.
2842 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002843 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002844 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002845
2846 // Write the record containing ext_vector type names.
2847 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002848 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002849
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002850 // Write the record containing VTable uses information.
2851 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002852 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002853
2854 // Write the record containing dynamic classes declarations.
2855 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002856 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002857
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002858 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002859 if (!PendingInstantiations.empty())
2860 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002861
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002862 // Write the record containing declaration references of Sema.
2863 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002864 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002865
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002866 // Write the record containing CUDA-specific declaration references.
2867 if (!CUDASpecialDeclRefs.empty())
2868 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2869
Douglas Gregor08f01292009-04-17 22:13:46 +00002870 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002871 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002872 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002873 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002874 Record.push_back(NumLexicalDeclContexts);
2875 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002876 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002877 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002878}
2879
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002880void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002881 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002882 using namespace llvm;
2883
2884 ASTContext &Context = SemaRef.Context;
2885 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002886
Sebastian Redl143413f2010-07-12 22:02:52 +00002887 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002888 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002889 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002890 if (StatCalls && !isysroot)
2891 WriteStatCache(*StatCalls);
2892 // FIXME: Source manager block should only write new stuff, which could be
2893 // done by tracking the largest ID in the chain
2894 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002895
2896 // The special types are in the chained PCH.
2897
2898 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002899 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002900 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002901 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002902 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2903 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002904 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002905 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002906 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002907 else if ((*I)->isChangedSinceDeserialization())
2908 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002909 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002910 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002911 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002912 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002913 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2914 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2915 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002916 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002917 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2918 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002919 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002920 // And a visible updates block for the DeclContexts.
2921 Abv = new llvm::BitCodeAbbrev();
2922 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2923 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2924 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2925 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2926 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2927 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002928
Sebastian Redl98912122010-07-27 23:01:28 +00002929 // Build a record containing all of the new tentative definitions in this
2930 // file, in TentativeDefinitions order.
2931 RecordData TentativeDefinitions;
2932 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2933 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2934 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2935 }
2936
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002937 // Build a record containing all of the file scoped decls in this file.
2938 RecordData UnusedFileScopedDecls;
2939 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2940 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2941 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002942 }
2943
Sebastian Redl08aca90252010-08-05 18:21:25 +00002944 // We write the entire table, overwriting the tables from the chain.
2945 RecordData WeakUndeclaredIdentifiers;
2946 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2947 WeakUndeclaredIdentifiers.push_back(
2948 SemaRef.WeakUndeclaredIdentifiers.size());
2949 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2950 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2951 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2952 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2953 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2954 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2955 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2956 }
2957 }
2958
Sebastian Redl98912122010-07-27 23:01:28 +00002959 // Build a record containing all of the locally-scoped external
2960 // declarations in this header file. Generally, this record will be
2961 // empty.
2962 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002963 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002964 // nondeterminstic!
2965 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2966 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2967 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2968 TD != TDEnd; ++TD) {
2969 if (TD->second->getPCHLevel() == 0)
2970 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2971 }
2972
2973 // Build a record containing all of the ext_vector declarations.
2974 RecordData ExtVectorDecls;
2975 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2976 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2977 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2978 }
2979
Sebastian Redl08aca90252010-08-05 18:21:25 +00002980 // Build a record containing all of the VTable uses information.
2981 // We write everything here, because it's too hard to determine whether
2982 // a use is new to this part.
2983 RecordData VTableUses;
2984 if (!SemaRef.VTableUses.empty()) {
2985 VTableUses.push_back(SemaRef.VTableUses.size());
2986 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2987 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2988 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2989 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2990 }
2991 }
2992
2993 // Build a record containing all of dynamic classes declarations.
2994 RecordData DynamicClasses;
2995 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2996 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2997 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2998
2999 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003000 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003001 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003002 I = SemaRef.PendingInstantiations.begin(),
3003 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00003004 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00003005 AddDeclRef(I->first, PendingInstantiations);
3006 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003007 }
3008 }
3009 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3010 "There are local ones at end of translation unit!");
3011
3012 // Build a record containing some declaration references.
3013 // It's not worth the effort to avoid duplication here.
3014 RecordData SemaDeclRefs;
3015 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3016 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3017 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3018 }
3019
Sebastian Redl539c5062010-08-18 23:57:32 +00003020 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003021 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003022 for (DeclsToRewriteTy::iterator
3023 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3024 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003025 while (!DeclTypesToEmit.empty()) {
3026 DeclOrType DOT = DeclTypesToEmit.front();
3027 DeclTypesToEmit.pop();
3028 if (DOT.isType())
3029 WriteType(DOT.getType());
3030 else
3031 WriteDecl(Context, DOT.getDecl());
3032 }
3033 Stream.ExitBlock();
3034
Sebastian Redl98912122010-07-27 23:01:28 +00003035 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003036 WriteSelectors(SemaRef);
3037 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003038 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003039 WriteFPPragmaOptions(SemaRef.getFPOptions());
3040 WriteOpenCLExtensions(SemaRef);
3041
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003042 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003043 // FIXME: For chained PCH only write the new mappings (we currently
3044 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003045 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003046
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003047 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003048 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003049 RecordData FirstLatestDeclIDs;
3050 for (FirstLatestDeclMap::iterator
3051 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3052 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3053 "Expected first & second to be in different PCHs");
3054 AddDeclRef(I->first, FirstLatestDeclIDs);
3055 AddDeclRef(I->second, FirstLatestDeclIDs);
3056 }
3057 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003058 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003059
Sebastian Redl98912122010-07-27 23:01:28 +00003060 // Write the record containing external, unnamed definitions.
3061 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003062 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003063
3064 // Write the record containing tentative definitions.
3065 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003066 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003067
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003068 // Write the record containing unused file scoped decls.
3069 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003070 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003071
Sebastian Redl08aca90252010-08-05 18:21:25 +00003072 // Write the record containing weak undeclared identifiers.
3073 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003074 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003075 WeakUndeclaredIdentifiers);
3076
Sebastian Redl98912122010-07-27 23:01:28 +00003077 // Write the record containing locally-scoped external definitions.
3078 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003079 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003080 LocallyScopedExternalDecls);
3081
3082 // Write the record containing ext_vector type names.
3083 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003084 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003085
Sebastian Redl08aca90252010-08-05 18:21:25 +00003086 // Write the record containing VTable uses information.
3087 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003088 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003089
3090 // Write the record containing dynamic classes declarations.
3091 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003092 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003093
3094 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003095 if (!PendingInstantiations.empty())
3096 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003097
3098 // Write the record containing declaration references of Sema.
3099 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003100 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003101
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003102 // Write the updates to DeclContexts.
3103 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3104 I = UpdatedDeclContexts.begin(),
3105 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003106 I != E; ++I)
3107 WriteDeclContextVisibleUpdate(*I);
3108
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003109 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003110
Sebastian Redl98912122010-07-27 23:01:28 +00003111 Record.clear();
3112 Record.push_back(NumStatements);
3113 Record.push_back(NumMacros);
3114 Record.push_back(NumLexicalDeclContexts);
3115 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003116 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003117 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003118 Stream.ExitBlock();
3119}
3120
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003121void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003122 if (DeclUpdates.empty())
3123 return;
3124
3125 RecordData OffsetsRecord;
3126 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3127 for (DeclUpdateMap::iterator
3128 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3129 const Decl *D = I->first;
3130 UpdateRecord &URec = I->second;
3131
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003132 if (DeclsToRewrite.count(D))
3133 continue; // The decl will be written completely,no need to store updates.
3134
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003135 uint64_t Offset = Stream.GetCurrentBitNo();
3136 Stream.EmitRecord(DECL_UPDATES, URec);
3137
3138 OffsetsRecord.push_back(GetDeclRef(D));
3139 OffsetsRecord.push_back(Offset);
3140 }
3141 Stream.ExitBlock();
3142 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3143}
3144
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003145void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003146 if (ReplacedDecls.empty())
3147 return;
3148
3149 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003150 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003151 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3152 Record.push_back(I->first);
3153 Record.push_back(I->second);
3154 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003155 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003156}
3157
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003158void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003159 Record.push_back(Loc.getRawEncoding());
3160}
3161
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003162void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003163 AddSourceLocation(Range.getBegin(), Record);
3164 AddSourceLocation(Range.getEnd(), Record);
3165}
3166
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003167void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003168 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003169 const uint64_t *Words = Value.getRawData();
3170 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003171}
3172
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003173void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003174 Record.push_back(Value.isUnsigned());
3175 AddAPInt(Value, Record);
3176}
3177
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003178void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003179 AddAPInt(Value.bitcastToAPInt(), Record);
3180}
3181
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003182void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003183 Record.push_back(getIdentifierRef(II));
3184}
3185
Sebastian Redl539c5062010-08-18 23:57:32 +00003186IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003187 if (II == 0)
3188 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003189
Sebastian Redl539c5062010-08-18 23:57:32 +00003190 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003191 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003192 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003193 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003194}
3195
Sebastian Redl50e26582010-09-15 19:54:06 +00003196MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003197 if (MD == 0)
3198 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003199
3200 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003201 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003202 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003203 return ID;
3204}
3205
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003206void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003207 Record.push_back(getSelectorRef(SelRef));
3208}
3209
Sebastian Redl539c5062010-08-18 23:57:32 +00003210SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003211 if (Sel.getAsOpaquePtr() == 0) {
3212 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003213 }
3214
Sebastian Redl539c5062010-08-18 23:57:32 +00003215 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003216 if (SID == 0 && Chain) {
3217 // This might trigger a ReadSelector callback, which will set the ID for
3218 // this selector.
3219 Chain->LoadSelector(Sel);
3220 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003221 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003222 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003223 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003224 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003225}
3226
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003227void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003228 AddDeclRef(Temp->getDestructor(), Record);
3229}
3230
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003231void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3232 CXXBaseSpecifier const *BasesEnd,
3233 RecordDataImpl &Record) {
3234 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3235 CXXBaseSpecifiersToWrite.push_back(
3236 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3237 Bases, BasesEnd));
3238 Record.push_back(NextCXXBaseSpecifiersID++);
3239}
3240
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003241void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003242 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003243 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003244 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003245 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003246 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003247 break;
3248 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003249 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003250 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003251 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003252 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003253 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003254 break;
3255 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003256 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003257 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003258 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003259 break;
John McCall0ad16662009-10-29 08:12:44 +00003260 case TemplateArgument::Null:
3261 case TemplateArgument::Integral:
3262 case TemplateArgument::Declaration:
3263 case TemplateArgument::Pack:
3264 break;
3265 }
3266}
3267
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003268void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003269 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003270 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003271
3272 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3273 bool InfoHasSameExpr
3274 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3275 Record.push_back(InfoHasSameExpr);
3276 if (InfoHasSameExpr)
3277 return; // Avoid storing the same expr twice.
3278 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003279 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3280 Record);
3281}
3282
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003283void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3284 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003285 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003286 AddTypeRef(QualType(), Record);
3287 return;
3288 }
3289
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003290 AddTypeLoc(TInfo->getTypeLoc(), Record);
3291}
3292
3293void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3294 AddTypeRef(TL.getType(), Record);
3295
John McCall8f115c62009-10-16 21:56:05 +00003296 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003297 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003298 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003299}
3300
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003301void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003302 Record.push_back(GetOrCreateTypeID(T));
3303}
3304
3305TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003306 return MakeTypeID(T,
3307 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3308}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003309
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003310TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003311 return MakeTypeID(T,
3312 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003313}
3314
3315TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3316 if (T.isNull())
3317 return TypeIdx();
3318 assert(!T.getLocalFastQualifiers());
3319
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003320 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003321 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003322 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003323 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003324 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003325 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003326 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003327 return Idx;
3328}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003329
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003330TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003331 if (T.isNull())
3332 return TypeIdx();
3333 assert(!T.getLocalFastQualifiers());
3334
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003335 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3336 assert(I != TypeIdxs.end() && "Type not emitted!");
3337 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003338}
3339
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003340void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003341 Record.push_back(GetDeclRef(D));
3342}
3343
Sebastian Redl539c5062010-08-18 23:57:32 +00003344DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003345 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003346 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003347 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003348 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003349 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003350 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003351 // We haven't seen this declaration before. Give it a new ID and
3352 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003353 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003354 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003355 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3356 // We don't add it to the replacement collection here, because we don't
3357 // have the offset yet.
3358 DeclTypesToEmit.push(const_cast<Decl *>(D));
3359 // Reset the flag, so that we don't add this decl multiple times.
3360 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003361 }
3362
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003363 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003364}
3365
Sebastian Redl539c5062010-08-18 23:57:32 +00003366DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003367 if (D == 0)
3368 return 0;
3369
3370 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3371 return DeclIDs[D];
3372}
3373
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003374void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003375 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003376 Record.push_back(Name.getNameKind());
3377 switch (Name.getNameKind()) {
3378 case DeclarationName::Identifier:
3379 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3380 break;
3381
3382 case DeclarationName::ObjCZeroArgSelector:
3383 case DeclarationName::ObjCOneArgSelector:
3384 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003385 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003386 break;
3387
3388 case DeclarationName::CXXConstructorName:
3389 case DeclarationName::CXXDestructorName:
3390 case DeclarationName::CXXConversionFunctionName:
3391 AddTypeRef(Name.getCXXNameType(), Record);
3392 break;
3393
3394 case DeclarationName::CXXOperatorName:
3395 Record.push_back(Name.getCXXOverloadedOperator());
3396 break;
3397
Alexis Hunt3d221f22009-11-29 07:34:05 +00003398 case DeclarationName::CXXLiteralOperatorName:
3399 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3400 break;
3401
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003402 case DeclarationName::CXXUsingDirective:
3403 // No extra data to emit
3404 break;
3405 }
3406}
Chris Lattnerca025db2010-05-07 21:43:38 +00003407
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003408void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003409 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003410 switch (Name.getNameKind()) {
3411 case DeclarationName::CXXConstructorName:
3412 case DeclarationName::CXXDestructorName:
3413 case DeclarationName::CXXConversionFunctionName:
3414 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3415 break;
3416
3417 case DeclarationName::CXXOperatorName:
3418 AddSourceLocation(
3419 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3420 Record);
3421 AddSourceLocation(
3422 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3423 Record);
3424 break;
3425
3426 case DeclarationName::CXXLiteralOperatorName:
3427 AddSourceLocation(
3428 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3429 Record);
3430 break;
3431
3432 case DeclarationName::Identifier:
3433 case DeclarationName::ObjCZeroArgSelector:
3434 case DeclarationName::ObjCOneArgSelector:
3435 case DeclarationName::ObjCMultiArgSelector:
3436 case DeclarationName::CXXUsingDirective:
3437 break;
3438 }
3439}
3440
3441void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003442 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003443 AddDeclarationName(NameInfo.getName(), Record);
3444 AddSourceLocation(NameInfo.getLoc(), Record);
3445 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3446}
3447
3448void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003449 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003450 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003451 Record.push_back(Info.NumTemplParamLists);
3452 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3453 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3454}
3455
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003456void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003457 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003458 // Nested name specifiers usually aren't too long. I think that 8 would
3459 // typically accomodate the vast majority.
3460 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3461
3462 // Push each of the NNS's onto a stack for serialization in reverse order.
3463 while (NNS) {
3464 NestedNames.push_back(NNS);
3465 NNS = NNS->getPrefix();
3466 }
3467
3468 Record.push_back(NestedNames.size());
3469 while(!NestedNames.empty()) {
3470 NNS = NestedNames.pop_back_val();
3471 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3472 Record.push_back(Kind);
3473 switch (Kind) {
3474 case NestedNameSpecifier::Identifier:
3475 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3476 break;
3477
3478 case NestedNameSpecifier::Namespace:
3479 AddDeclRef(NNS->getAsNamespace(), Record);
3480 break;
3481
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003482 case NestedNameSpecifier::NamespaceAlias:
3483 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3484 break;
3485
Chris Lattnerca025db2010-05-07 21:43:38 +00003486 case NestedNameSpecifier::TypeSpec:
3487 case NestedNameSpecifier::TypeSpecWithTemplate:
3488 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3489 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3490 break;
3491
3492 case NestedNameSpecifier::Global:
3493 // Don't need to write an associated value.
3494 break;
3495 }
3496 }
3497}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003498
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003499void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3500 RecordDataImpl &Record) {
3501 // Nested name specifiers usually aren't too long. I think that 8 would
3502 // typically accomodate the vast majority.
3503 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3504
3505 // Push each of the nested-name-specifiers's onto a stack for
3506 // serialization in reverse order.
3507 while (NNS) {
3508 NestedNames.push_back(NNS);
3509 NNS = NNS.getPrefix();
3510 }
3511
3512 Record.push_back(NestedNames.size());
3513 while(!NestedNames.empty()) {
3514 NNS = NestedNames.pop_back_val();
3515 NestedNameSpecifier::SpecifierKind Kind
3516 = NNS.getNestedNameSpecifier()->getKind();
3517 Record.push_back(Kind);
3518 switch (Kind) {
3519 case NestedNameSpecifier::Identifier:
3520 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3521 AddSourceRange(NNS.getLocalSourceRange(), Record);
3522 break;
3523
3524 case NestedNameSpecifier::Namespace:
3525 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3526 AddSourceRange(NNS.getLocalSourceRange(), Record);
3527 break;
3528
3529 case NestedNameSpecifier::NamespaceAlias:
3530 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3531 AddSourceRange(NNS.getLocalSourceRange(), Record);
3532 break;
3533
3534 case NestedNameSpecifier::TypeSpec:
3535 case NestedNameSpecifier::TypeSpecWithTemplate:
3536 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3537 AddTypeLoc(NNS.getTypeLoc(), Record);
3538 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3539 break;
3540
3541 case NestedNameSpecifier::Global:
3542 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3543 break;
3544 }
3545 }
3546}
3547
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003548void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003549 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003550 Record.push_back(Kind);
3551 switch (Kind) {
3552 case TemplateName::Template:
3553 AddDeclRef(Name.getAsTemplateDecl(), Record);
3554 break;
3555
3556 case TemplateName::OverloadedTemplate: {
3557 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3558 Record.push_back(OvT->size());
3559 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3560 I != E; ++I)
3561 AddDeclRef(*I, Record);
3562 break;
3563 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003564
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003565 case TemplateName::QualifiedTemplate: {
3566 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3567 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3568 Record.push_back(QualT->hasTemplateKeyword());
3569 AddDeclRef(QualT->getTemplateDecl(), Record);
3570 break;
3571 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003572
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003573 case TemplateName::DependentTemplate: {
3574 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3575 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3576 Record.push_back(DepT->isIdentifier());
3577 if (DepT->isIdentifier())
3578 AddIdentifierRef(DepT->getIdentifier(), Record);
3579 else
3580 Record.push_back(DepT->getOperator());
3581 break;
3582 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003583
3584 case TemplateName::SubstTemplateTemplateParmPack: {
3585 SubstTemplateTemplateParmPackStorage *SubstPack
3586 = Name.getAsSubstTemplateTemplateParmPack();
3587 AddDeclRef(SubstPack->getParameterPack(), Record);
3588 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3589 break;
3590 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003591 }
3592}
3593
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003594void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003595 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003596 Record.push_back(Arg.getKind());
3597 switch (Arg.getKind()) {
3598 case TemplateArgument::Null:
3599 break;
3600 case TemplateArgument::Type:
3601 AddTypeRef(Arg.getAsType(), Record);
3602 break;
3603 case TemplateArgument::Declaration:
3604 AddDeclRef(Arg.getAsDecl(), Record);
3605 break;
3606 case TemplateArgument::Integral:
3607 AddAPSInt(*Arg.getAsIntegral(), Record);
3608 AddTypeRef(Arg.getIntegralType(), Record);
3609 break;
3610 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003611 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3612 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003613 case TemplateArgument::TemplateExpansion:
3614 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003615 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3616 Record.push_back(*NumExpansions + 1);
3617 else
3618 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003619 break;
3620 case TemplateArgument::Expression:
3621 AddStmt(Arg.getAsExpr());
3622 break;
3623 case TemplateArgument::Pack:
3624 Record.push_back(Arg.pack_size());
3625 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3626 I != E; ++I)
3627 AddTemplateArgument(*I, Record);
3628 break;
3629 }
3630}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003631
3632void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003633ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003634 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003635 assert(TemplateParams && "No TemplateParams!");
3636 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3637 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3638 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3639 Record.push_back(TemplateParams->size());
3640 for (TemplateParameterList::const_iterator
3641 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3642 P != PEnd; ++P)
3643 AddDeclRef(*P, Record);
3644}
3645
3646/// \brief Emit a template argument list.
3647void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003648ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003649 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003650 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003651 Record.push_back(TemplateArgs->size());
3652 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003653 AddTemplateArgument(TemplateArgs->get(i), Record);
3654}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003655
3656
3657void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003658ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003659 Record.push_back(Set.size());
3660 for (UnresolvedSetImpl::const_iterator
3661 I = Set.begin(), E = Set.end(); I != E; ++I) {
3662 AddDeclRef(I.getDecl(), Record);
3663 Record.push_back(I.getAccess());
3664 }
3665}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003666
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003667void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003668 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003669 Record.push_back(Base.isVirtual());
3670 Record.push_back(Base.isBaseOfClass());
3671 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003672 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003673 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003674 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003675 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3676 : SourceLocation(),
3677 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003678}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003679
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003680void ASTWriter::FlushCXXBaseSpecifiers() {
3681 RecordData Record;
3682 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3683 Record.clear();
3684
3685 // Record the offset of this base-specifier set.
3686 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3687 if (Index == CXXBaseSpecifiersOffsets.size())
3688 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3689 else {
3690 if (Index > CXXBaseSpecifiersOffsets.size())
3691 CXXBaseSpecifiersOffsets.resize(Index + 1);
3692 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3693 }
3694
3695 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3696 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3697 Record.push_back(BEnd - B);
3698 for (; B != BEnd; ++B)
3699 AddCXXBaseSpecifier(*B, Record);
3700 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003701
3702 // Flush any expressions that were written as part of the base specifiers.
3703 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003704 }
3705
3706 CXXBaseSpecifiersToWrite.clear();
3707}
3708
Alexis Hunt1d792652011-01-08 20:30:50 +00003709void ASTWriter::AddCXXCtorInitializers(
3710 const CXXCtorInitializer * const *CtorInitializers,
3711 unsigned NumCtorInitializers,
3712 RecordDataImpl &Record) {
3713 Record.push_back(NumCtorInitializers);
3714 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3715 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003716
3717 Record.push_back(Init->isBaseInitializer());
3718 if (Init->isBaseInitializer()) {
3719 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3720 Record.push_back(Init->isBaseVirtual());
3721 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003722 Record.push_back(Init->isIndirectMemberInitializer());
3723 if (Init->isIndirectMemberInitializer())
3724 AddDeclRef(Init->getIndirectMember(), Record);
3725 else
3726 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003727 }
Francois Pichetd583da02010-12-04 09:14:42 +00003728
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003729 AddSourceLocation(Init->getMemberLocation(), Record);
3730 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003731 AddSourceLocation(Init->getLParenLoc(), Record);
3732 AddSourceLocation(Init->getRParenLoc(), Record);
3733 Record.push_back(Init->isWritten());
3734 if (Init->isWritten()) {
3735 Record.push_back(Init->getSourceOrder());
3736 } else {
3737 Record.push_back(Init->getNumArrayIndices());
3738 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3739 AddDeclRef(Init->getArrayIndex(i), Record);
3740 }
3741 }
3742}
3743
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003744void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3745 assert(D->DefinitionData);
3746 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3747 Record.push_back(Data.UserDeclaredConstructor);
3748 Record.push_back(Data.UserDeclaredCopyConstructor);
3749 Record.push_back(Data.UserDeclaredCopyAssignment);
3750 Record.push_back(Data.UserDeclaredDestructor);
3751 Record.push_back(Data.Aggregate);
3752 Record.push_back(Data.PlainOldData);
3753 Record.push_back(Data.Empty);
3754 Record.push_back(Data.Polymorphic);
3755 Record.push_back(Data.Abstract);
3756 Record.push_back(Data.HasTrivialConstructor);
3757 Record.push_back(Data.HasTrivialCopyConstructor);
3758 Record.push_back(Data.HasTrivialCopyAssignment);
3759 Record.push_back(Data.HasTrivialDestructor);
3760 Record.push_back(Data.ComputedVisibleConversions);
3761 Record.push_back(Data.DeclaredDefaultConstructor);
3762 Record.push_back(Data.DeclaredCopyConstructor);
3763 Record.push_back(Data.DeclaredCopyAssignment);
3764 Record.push_back(Data.DeclaredDestructor);
3765
3766 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003767 if (Data.NumBases > 0)
3768 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3769 Record);
3770
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003771 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3772 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003773 if (Data.NumVBases > 0)
3774 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3775 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003776
3777 AddUnresolvedSet(Data.Conversions, Record);
3778 AddUnresolvedSet(Data.VisibleConversions, Record);
3779 // Data.Definition is the owning decl, no need to write it.
3780 AddDeclRef(Data.FirstFriend, Record);
3781}
3782
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003783void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003784 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003785 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003786 assert(FirstDeclID == NextDeclID &&
3787 FirstTypeID == NextTypeID &&
3788 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003789 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003790 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003791 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003792 "Setting chain after writing has started.");
3793 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003794
3795 FirstDeclID += Chain->getTotalNumDecls();
3796 FirstTypeID += Chain->getTotalNumTypes();
3797 FirstIdentID += Chain->getTotalNumIdentifiers();
3798 FirstSelectorID += Chain->getTotalNumSelectors();
3799 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003800 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003801 NextDeclID = FirstDeclID;
3802 NextTypeID = FirstTypeID;
3803 NextIdentID = FirstIdentID;
3804 NextSelectorID = FirstSelectorID;
3805 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003806 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003807}
3808
Sebastian Redl539c5062010-08-18 23:57:32 +00003809void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003810 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003811 if (II->hasMacroDefinition())
3812 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003813}
3814
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003815void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003816 // Always take the highest-numbered type index. This copes with an interesting
3817 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003818 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003819 // keep the higher-numbered entry so that we can properly write it out to
3820 // the AST file.
3821 TypeIdx &StoredIdx = TypeIdxs[T];
3822 if (Idx.getIndex() >= StoredIdx.getIndex())
3823 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003824}
3825
Sebastian Redl539c5062010-08-18 23:57:32 +00003826void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003827 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003828}
Sebastian Redl834bb972010-08-04 17:20:04 +00003829
Sebastian Redl539c5062010-08-18 23:57:32 +00003830void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003831 SelectorIDs[S] = ID;
3832}
Douglas Gregor91096292010-10-02 19:29:26 +00003833
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003834void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003835 MacroDefinition *MD) {
3836 MacroDefinitions[MD] = ID;
3837}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003838
3839void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3840 assert(D->isDefinition());
3841 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3842 // We are interested when a PCH decl is modified.
3843 if (RD->getPCHLevel() > 0) {
3844 // A forward reference was mutated into a definition. Rewrite it.
3845 // FIXME: This happens during template instantiation, should we
3846 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003847 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003848 }
3849
3850 for (CXXRecordDecl::redecl_iterator
3851 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3852 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3853 if (Redecl == RD)
3854 continue;
3855
3856 // We are interested when a PCH decl is modified.
3857 if (Redecl->getPCHLevel() > 0) {
3858 UpdateRecord &Record = DeclUpdates[Redecl];
3859 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3860 assert(Redecl->DefinitionData);
3861 assert(Redecl->DefinitionData->Definition == D);
3862 AddDeclRef(D, Record); // the DefinitionDecl
3863 }
3864 }
3865 }
3866}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003867void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3868 // TU and namespaces are handled elsewhere.
3869 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3870 return;
3871
3872 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3873 return; // Not a source decl added to a DeclContext from PCH.
3874
3875 AddUpdatedDeclContext(DC);
3876}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003877
3878void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3879 assert(D->isImplicit());
3880 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3881 return; // Not a source member added to a class from PCH.
3882 if (!isa<CXXMethodDecl>(D))
3883 return; // We are interested in lazily declared implicit methods.
3884
3885 // A decl coming from PCH was modified.
3886 assert(RD->isDefinition());
3887 UpdateRecord &Record = DeclUpdates[RD];
3888 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3889 AddDeclRef(D, Record);
3890}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003891
3892void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3893 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003894 // The specializations set is kept in the canonical template.
3895 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003896 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3897 return; // Not a source specialization added to a template from PCH.
3898
3899 UpdateRecord &Record = DeclUpdates[TD];
3900 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3901 AddDeclRef(D, Record);
3902}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003903
3904ASTSerializationListener::~ASTSerializationListener() { }