blob: 8fcb535a9c891f280226cbc61713ad53ecb142bc [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);
535 Writer.AddSourceRange(TL.getQualifierRange(), 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);
542 Writer.AddSourceRange(TL.getQualifierRange(), 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);
548 Writer.AddSourceRange(TL.getQualifierRange(), Record);
549 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.
Daniel Dunbar925152c2010-02-10 18:48:44 +00001026 Record.push_back(LangOpts.SjLjExceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001027 Record.push_back(LangOpts.ObjCExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001028
Douglas Gregordbe39272011-02-01 15:15:22 +00001029 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001030 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1031 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1032 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1033
Chris Lattner258172e2009-04-27 07:35:58 +00001034 // Whether static initializers are protected by locks.
1035 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001036 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001037 Record.push_back(LangOpts.Blocks); // block extension to C
1038 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1039 // they are unused.
1040 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1041 // (modulo the platform support).
1042
Chris Lattner51924e512010-06-26 21:25:03 +00001043 Record.push_back(LangOpts.getSignedOverflowBehavior());
1044 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001045
1046 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001047 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001048 // defined.
1049 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1050 // opposed to __DYNAMIC__).
1051 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1052
1053 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1054 // used (instead of C99 semantics).
1055 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001056 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1057 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001058 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1059 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001060 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001061 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1062 // to the smallest integer type with
1063 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001064 Record.push_back(LangOpts.getGCMode());
1065 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001066 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001067 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001068 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001069 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001070 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001071 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001072 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001073 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +00001074 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001075}
1076
Douglas Gregora7f71a92009-04-10 03:52:48 +00001077//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001078// stat cache Serialization
1079//===----------------------------------------------------------------------===//
1080
1081namespace {
1082// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001083class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001084public:
1085 typedef const char * key_type;
1086 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001087
Chris Lattner2a6fa472010-11-23 19:28:12 +00001088 typedef struct stat data_type;
1089 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001090
1091 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001092 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001093 }
Mike Stump11289f42009-09-09 15:08:12 +00001094
1095 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1097 data_type_ref Data) {
1098 unsigned StrLen = strlen(path);
1099 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001100 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001101 clang::io::Emit8(Out, DataLen);
1102 return std::make_pair(StrLen + 1, DataLen);
1103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Douglas Gregorc5046832009-04-27 18:38:38 +00001105 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1106 Out.write(path, KeyLen);
1107 }
Mike Stump11289f42009-09-09 15:08:12 +00001108
Chris Lattner2a6fa472010-11-23 19:28:12 +00001109 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001110 data_type_ref Data, unsigned DataLen) {
1111 using namespace clang::io;
1112 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001113
Chris Lattner2a6fa472010-11-23 19:28:12 +00001114 Emit32(Out, (uint32_t) Data.st_ino);
1115 Emit32(Out, (uint32_t) Data.st_dev);
1116 Emit16(Out, (uint16_t) Data.st_mode);
1117 Emit64(Out, (uint64_t) Data.st_mtime);
1118 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001119
1120 assert(Out.tell() - Start == DataLen && "Wrong data length");
1121 }
1122};
1123} // end anonymous namespace
1124
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001125/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001126void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001127 // Build the on-disk hash table containing information about every
1128 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001129 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001130 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001131 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001132 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001133 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1134 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001135 Generator.insert(Filename, Stat->second);
1136 }
Mike Stump11289f42009-09-09 15:08:12 +00001137
Douglas Gregorc5046832009-04-27 18:38:38 +00001138 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001139 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001140 uint32_t BucketOffset;
1141 {
1142 llvm::raw_svector_ostream Out(StatCacheData);
1143 // Make sure that no bucket is at offset 0
1144 clang::io::Emit32(Out, 0);
1145 BucketOffset = Generator.Emit(Out);
1146 }
1147
1148 // Create a blob abbreviation
1149 using namespace llvm;
1150 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001151 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1155 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1156
1157 // Write the stat cache
1158 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001159 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001160 Record.push_back(BucketOffset);
1161 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001162 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001163}
1164
1165//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001166// Source Manager Serialization
1167//===----------------------------------------------------------------------===//
1168
1169/// \brief Create an abbreviation for the SLocEntry that refers to a
1170/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001171static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001172 using namespace llvm;
1173 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001174 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001179 // FileEntry fields.
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001183 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184}
1185
1186/// \brief Create an abbreviation for the SLocEntry that refers to a
1187/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001188static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001189 using namespace llvm;
1190 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001191 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001197 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001198}
1199
1200/// \brief Create an abbreviation for the SLocEntry that refers to a
1201/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001202static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001203 using namespace llvm;
1204 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001205 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001207 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001208}
1209
1210/// \brief Create an abbreviation for the SLocEntry that refers to an
1211/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001212static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001213 using namespace llvm;
1214 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001215 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001221 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001222}
1223
Douglas Gregor09b69892011-02-10 17:09:37 +00001224namespace {
1225 // Trait used for the on-disk hash table of header search information.
1226 class HeaderFileInfoTrait {
1227 ASTWriter &Writer;
1228 HeaderSearch &HS;
1229
1230 public:
1231 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1232 : Writer(Writer), HS(HS) { }
1233
1234 typedef const char *key_type;
1235 typedef key_type key_type_ref;
1236
1237 typedef HeaderFileInfo data_type;
1238 typedef const data_type &data_type_ref;
1239
1240 static unsigned ComputeHash(const char *path) {
1241 // The hash is based only on the filename portion of the key, so that the
1242 // reader can match based on filenames when symlinking or excess path
1243 // elements ("foo/../", "../") change the form of the name. However,
1244 // complete path is still the key.
1245 return llvm::HashString(llvm::sys::path::filename(path));
1246 }
1247
1248 std::pair<unsigned,unsigned>
1249 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1250 data_type_ref Data) {
1251 unsigned StrLen = strlen(path);
1252 clang::io::Emit16(Out, StrLen);
1253 unsigned DataLen = 1 + 2 + 4;
1254 clang::io::Emit8(Out, DataLen);
1255 return std::make_pair(StrLen + 1, DataLen);
1256 }
1257
1258 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1259 Out.write(path, KeyLen);
1260 }
1261
1262 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1263 data_type_ref Data, unsigned DataLen) {
1264 using namespace clang::io;
1265 uint64_t Start = Out.tell(); (void)Start;
1266
1267 unsigned char Flags = (Data.isImport << 3)
1268 | (Data.DirInfo << 1)
1269 | Data.Resolved;
1270 Emit8(Out, (uint8_t)Flags);
1271 Emit16(Out, (uint16_t) Data.NumIncludes);
1272
1273 if (!Data.ControllingMacro)
1274 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1275 else
1276 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1277 assert(Out.tell() - Start == DataLen && "Wrong data length");
1278 }
1279 };
1280} // end anonymous namespace
1281
1282/// \brief Write the header search block for the list of files that
1283///
1284/// \param HS The header search structure to save.
1285///
1286/// \param Chain Whether we're creating a chained AST file.
1287void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1288 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1289 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1290
1291 if (FilesByUID.size() > HS.header_file_size())
1292 FilesByUID.resize(HS.header_file_size());
1293
1294 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1295 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1296 llvm::SmallVector<const char *, 4> SavedStrings;
1297 unsigned NumHeaderSearchEntries = 0;
1298 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1299 const FileEntry *File = FilesByUID[UID];
1300 if (!File)
1301 continue;
1302
1303 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1304 if (HFI.External && Chain)
1305 continue;
1306
1307 // Turn the file name into an absolute path, if it isn't already.
1308 const char *Filename = File->getName();
1309 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1310
1311 // If we performed any translation on the file name at all, we need to
1312 // save this string, since the generator will refer to it later.
1313 if (Filename != File->getName()) {
1314 Filename = strdup(Filename);
1315 SavedStrings.push_back(Filename);
1316 }
1317
1318 Generator.insert(Filename, HFI, GeneratorTrait);
1319 ++NumHeaderSearchEntries;
1320 }
1321
1322 // Create the on-disk hash table in a buffer.
1323 llvm::SmallString<4096> TableData;
1324 uint32_t BucketOffset;
1325 {
1326 llvm::raw_svector_ostream Out(TableData);
1327 // Make sure that no bucket is at offset 0
1328 clang::io::Emit32(Out, 0);
1329 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1330 }
1331
1332 // Create a blob abbreviation
1333 using namespace llvm;
1334 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1335 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1339 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1340
1341 // Write the stat cache
1342 RecordData Record;
1343 Record.push_back(HEADER_SEARCH_TABLE);
1344 Record.push_back(BucketOffset);
1345 Record.push_back(NumHeaderSearchEntries);
1346 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1347
1348 // Free all of the strings we had to duplicate.
1349 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1350 free((void*)SavedStrings[I]);
1351}
1352
Douglas Gregora7f71a92009-04-10 03:52:48 +00001353/// \brief Writes the block containing the serialized form of the
1354/// source manager.
1355///
1356/// TODO: We should probably use an on-disk hash table (stored in a
1357/// blob), indexed based on the file name, so that we only create
1358/// entries for files that we actually need. In the common case (no
1359/// errors), we probably won't have to create file entries for any of
1360/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001361void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001362 const Preprocessor &PP,
1363 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001364 RecordData Record;
1365
Chris Lattner0910e3b2009-04-10 17:16:57 +00001366 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001367 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001368
1369 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001370 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1371 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1372 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1373 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001374
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001375 // Write the line table.
1376 if (SourceMgr.hasLineTable()) {
1377 LineTableInfo &LineTable = SourceMgr.getLineTable();
1378
1379 // Emit the file names
1380 Record.push_back(LineTable.getNumFilenames());
1381 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1382 // Emit the file name
1383 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001384 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001385 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1386 Record.push_back(FilenameLen);
1387 if (FilenameLen)
1388 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1389 }
Mike Stump11289f42009-09-09 15:08:12 +00001390
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001391 // Emit the line entries
1392 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1393 L != LEnd; ++L) {
1394 // Emit the file ID
1395 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001396
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001397 // Emit the line entries
1398 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001399 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001400 LEEnd = L->second.end();
1401 LE != LEEnd; ++LE) {
1402 Record.push_back(LE->FileOffset);
1403 Record.push_back(LE->LineNo);
1404 Record.push_back(LE->FilenameID);
1405 Record.push_back((unsigned)LE->FileKind);
1406 Record.push_back(LE->IncludeOffset);
1407 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001408 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001409 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001410 }
1411
Douglas Gregor258ae542009-04-27 06:38:32 +00001412 // Write out the source location entry table. We skip the first
1413 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001414 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001415 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001416 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1417 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1418 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1419 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001420 // Get this source location entry.
1421 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001422
Douglas Gregor258ae542009-04-27 06:38:32 +00001423 // Record the offset of this source-location entry.
1424 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1425
1426 // Figure out which record code to use.
1427 unsigned Code;
1428 if (SLoc->isFile()) {
1429 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001430 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001431 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001432 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001433 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001434 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001435 Record.clear();
1436 Record.push_back(Code);
1437
1438 Record.push_back(SLoc->getOffset());
1439 if (SLoc->isFile()) {
1440 const SrcMgr::FileInfo &File = SLoc->getFile();
1441 Record.push_back(File.getIncludeLoc().getRawEncoding());
1442 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1443 Record.push_back(File.hasLineDirectives());
1444
1445 const SrcMgr::ContentCache *Content = File.getContentCache();
1446 if (Content->Entry) {
1447 // The source location entry is a file. The blob associated
1448 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001449
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001450 // Emit size/modification time for this file.
1451 Record.push_back(Content->Entry->getSize());
1452 Record.push_back(Content->Entry->getModificationTime());
1453
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001454 // Turn the file name into an absolute path, if it isn't already.
1455 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001456 llvm::SmallString<128> FilePath(Filename);
1457 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001458 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001459
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001460 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001461 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001462 } else {
1463 // The source location entry is a buffer. The blob associated
1464 // with this entry contains the contents of the buffer.
1465
1466 // We add one to the size so that we capture the trailing NULL
1467 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1468 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001469 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001470 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001471 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001472 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1473 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001474 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001475 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001476 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001477 llvm::StringRef(Buffer->getBufferStart(),
1478 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001479
1480 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001481 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001482 }
1483 } else {
1484 // The source location entry is an instantiation.
1485 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1486 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1487 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1488 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1489
1490 // Compute the token length for this macro expansion.
1491 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001492 if (I + 1 != N)
1493 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001494 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1495 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1496 }
1497 }
1498
Douglas Gregor8f45df52009-04-16 22:23:12 +00001499 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001500
1501 if (SLocEntryOffsets.empty())
1502 return;
1503
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001504 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001505 // table is used for lazily loading source-location information.
1506 using namespace llvm;
1507 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001508 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1512 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001513
Douglas Gregor258ae542009-04-27 06:38:32 +00001514 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001515 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001516 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001517 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1518 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001519 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001520 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001521 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001522
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001523 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001524 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001525 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001526}
1527
Douglas Gregorc5046832009-04-27 18:38:38 +00001528//===----------------------------------------------------------------------===//
1529// Preprocessor Serialization
1530//===----------------------------------------------------------------------===//
1531
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001532static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1533 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1534 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1535 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1536 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1537 return X.first->getName().compare(Y.first->getName());
1538}
1539
Chris Lattnereeffaef2009-04-10 17:15:23 +00001540/// \brief Writes the block containing the serialized form of the
1541/// preprocessor.
1542///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001543void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001544 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001545
Chris Lattner0af3ba12009-04-13 01:29:17 +00001546 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1547 if (PP.getCounterValue() != 0) {
1548 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001549 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001550 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001551 }
1552
1553 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001554 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001555
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001556 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001557 // FIXME: use diagnostics subsystem for localization etc.
1558 if (PP.SawDateOrTime())
1559 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001560
Douglas Gregor796d76a2010-10-20 22:00:55 +00001561
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001562 // Loop over all the macro definitions that are live at the end of the file,
1563 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001564 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001565
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001566 // Construct the list of macro definitions that need to be serialized.
1567 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1568 MacrosToEmit;
1569 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001570 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1571 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001572 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001573 MacroDefinitionsSeen.insert(I->first);
1574 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1575 }
1576
1577 // Sort the set of macro definitions that need to be serialized by the
1578 // name of the macro, to provide a stable ordering.
1579 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1580 &compareMacroDefinitions);
1581
Douglas Gregor68051a72011-02-11 00:26:14 +00001582 // Resolve any identifiers that defined macros at the time they were
1583 // deserialized, adding them to the list of macros to emit (if appropriate).
1584 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1585 IdentifierInfo *Name
1586 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1587 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1588 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1589 }
1590
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001591 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1592 const IdentifierInfo *Name = MacrosToEmit[I].first;
1593 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001594 if (!MI)
1595 continue;
1596
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001597 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001598 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001599 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001600
1601 // FIXME: There is a (probably minor) optimization we could do here, if
1602 // the macro comes from the original PCH but the identifier comes from a
1603 // chained PCH, by storing the offset into the original PCH rather than
1604 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001605 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001606 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001607 continue;
1608
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001609 AddIdentifierRef(Name, Record);
1610 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001611 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1612 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001613
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001614 unsigned Code;
1615 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001616 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001617 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001618 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001619
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001620 Record.push_back(MI->isC99Varargs());
1621 Record.push_back(MI->isGNUVarargs());
1622 Record.push_back(MI->getNumArgs());
1623 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1624 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001625 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001626 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001627
Douglas Gregoraae92242010-03-19 21:51:54 +00001628 // If we have a detailed preprocessing record, record the macro definition
1629 // ID that corresponds to this macro.
1630 if (PPRec)
1631 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001632
Douglas Gregor8f45df52009-04-16 22:23:12 +00001633 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001634 Record.clear();
1635
Chris Lattner2199f5b2009-04-10 18:08:30 +00001636 // Emit the tokens array.
1637 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1638 // Note that we know that the preprocessor does not have any annotation
1639 // tokens in it because they are created by the parser, and thus can't be
1640 // in a macro definition.
1641 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001642
Chris Lattner2199f5b2009-04-10 18:08:30 +00001643 Record.push_back(Tok.getLocation().getRawEncoding());
1644 Record.push_back(Tok.getLength());
1645
Chris Lattner2199f5b2009-04-10 18:08:30 +00001646 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1647 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001648 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001649 // FIXME: Should translate token kind to a stable encoding.
1650 Record.push_back(Tok.getKind());
1651 // FIXME: Should translate token flags to a stable encoding.
1652 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001653
Sebastian Redl539c5062010-08-18 23:57:32 +00001654 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001655 Record.clear();
1656 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001657 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001658 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001659 Stream.ExitBlock();
1660
1661 if (PPRec)
1662 WritePreprocessorDetail(*PPRec);
1663}
1664
1665void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1666 if (PPRec.begin(Chain) == PPRec.end(Chain))
1667 return;
1668
1669 // Enter the preprocessor block.
1670 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001671
Douglas Gregoraae92242010-03-19 21:51:54 +00001672 // If the preprocessor has a preprocessing record, emit it.
1673 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001674 using namespace llvm;
1675
1676 // Set up the abbreviation for
1677 unsigned InclusionAbbrev = 0;
1678 {
1679 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1680 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1681 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1682 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1683 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1684 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1685 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1688 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1689 }
1690
1691 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1692 RecordData Record;
1693 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1694 EEnd = PPRec.end(Chain);
1695 E != EEnd; ++E) {
1696 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001697
Douglas Gregor92a96f52011-02-08 21:58:10 +00001698 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1699 // Record this macro definition's location.
1700 MacroID ID = getMacroDefinitionID(MD);
1701
1702 // Don't write the macro definition if it is from another AST file.
1703 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001704 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001705
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001706 // Notify the serialization listener that we're serializing this entity.
1707 if (SerializationListener)
1708 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001709 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001710
Douglas Gregor92a96f52011-02-08 21:58:10 +00001711 unsigned Position = ID - FirstMacroID;
1712 if (Position != MacroDefinitionOffsets.size()) {
1713 if (Position > MacroDefinitionOffsets.size())
1714 MacroDefinitionOffsets.resize(Position + 1);
1715
1716 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1717 } else
1718 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001719
Douglas Gregor92a96f52011-02-08 21:58:10 +00001720 Record.push_back(IndexBase + NumPreprocessingRecords++);
1721 Record.push_back(ID);
1722 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1723 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1724 AddIdentifierRef(MD->getName(), Record);
1725 AddSourceLocation(MD->getLocation(), Record);
1726 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1727 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001728 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001729
Douglas Gregor92a96f52011-02-08 21:58:10 +00001730 // Notify the serialization listener that we're serializing this entity.
1731 if (SerializationListener)
1732 SerializationListener->SerializedPreprocessedEntity(*E,
1733 Stream.GetCurrentBitNo());
1734
1735 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1736 Record.push_back(IndexBase + NumPreprocessingRecords++);
1737 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1738 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1739 AddIdentifierRef(MI->getName(), Record);
1740 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1741 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1742 continue;
1743 }
1744
1745 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1746 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1747 Record.push_back(IndexBase + NumPreprocessingRecords++);
1748 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1749 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1750 Record.push_back(ID->getFileName().size());
1751 Record.push_back(ID->wasInQuotes());
1752 Record.push_back(static_cast<unsigned>(ID->getKind()));
1753 llvm::SmallString<64> Buffer;
1754 Buffer += ID->getFileName();
1755 Buffer += ID->getFile()->getName();
1756 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1757 continue;
1758 }
1759
1760 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1761 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001762 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001763
Douglas Gregoraae92242010-03-19 21:51:54 +00001764 // Write the offsets table for the preprocessing record.
1765 if (NumPreprocessingRecords > 0) {
1766 // Write the offsets table for identifier IDs.
1767 using namespace llvm;
1768 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001769 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1773 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001774
Douglas Gregoraae92242010-03-19 21:51:54 +00001775 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001776 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001777 Record.push_back(NumPreprocessingRecords);
1778 Record.push_back(MacroDefinitionOffsets.size());
1779 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001780 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001781 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1782 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001783}
1784
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001785void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001786 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001787 for (Diagnostic::DiagStatePointsTy::const_iterator
1788 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1789 I != E; ++I) {
1790 const Diagnostic::DiagStatePoint &point = *I;
1791 if (point.Loc.isInvalid())
1792 continue;
1793
1794 Record.push_back(point.Loc.getRawEncoding());
1795 for (Diagnostic::DiagState::iterator
1796 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1797 unsigned diag = I->first, map = I->second;
1798 if (map & 0x10) { // mapping from a diagnostic pragma.
1799 Record.push_back(diag);
1800 Record.push_back(map & 0x7);
1801 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001802 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001803 Record.push_back(-1); // mark the end of the diag/map pairs for this
1804 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001805 }
1806
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001807 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001808 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001809}
1810
Douglas Gregorc5046832009-04-27 18:38:38 +00001811//===----------------------------------------------------------------------===//
1812// Type Serialization
1813//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001814
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001815/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001816void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001817 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001818 if (Idx.getIndex() == 0) // we haven't seen this type before.
1819 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001820
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001821 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001822
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001823 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001824 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001825 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001826 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001827 else if (TypeOffsets.size() < Index) {
1828 TypeOffsets.resize(Index + 1);
1829 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001830 }
1831
1832 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001833
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001834 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001835 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001836
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001837 if (T.hasLocalNonFastQualifiers()) {
1838 Qualifiers Qs = T.getLocalQualifiers();
1839 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001840 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001841 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001842 } else {
1843 switch (T->getTypeClass()) {
1844 // For all of the concrete, non-dependent types, call the
1845 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001846#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001847 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001848#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001849#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001850 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001851 }
1852
1853 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001854 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001855
1856 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001857 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001858}
1859
Douglas Gregorc5046832009-04-27 18:38:38 +00001860//===----------------------------------------------------------------------===//
1861// Declaration Serialization
1862//===----------------------------------------------------------------------===//
1863
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001864/// \brief Write the block containing all of the declaration IDs
1865/// lexically declared within the given DeclContext.
1866///
1867/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1868/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001869uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001870 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001871 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001872 return 0;
1873
Douglas Gregor8f45df52009-04-16 22:23:12 +00001874 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001875 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001876 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001877 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001878 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1879 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001880 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001881
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001882 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001883 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001884 reinterpret_cast<char*>(Decls.data()),
1885 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001886 return Offset;
1887}
1888
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001889void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001890 using namespace llvm;
1891 RecordData Record;
1892
1893 // Write the type offsets array
1894 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001895 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1898 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1899 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001900 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001901 Record.push_back(TypeOffsets.size());
1902 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001903 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001904 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1905
1906 // Write the declaration offsets array
1907 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001908 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1911 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1912 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001913 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001914 Record.push_back(DeclOffsets.size());
1915 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001916 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001917 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1918}
1919
Douglas Gregorc5046832009-04-27 18:38:38 +00001920//===----------------------------------------------------------------------===//
1921// Global Method Pool and Selector Serialization
1922//===----------------------------------------------------------------------===//
1923
Douglas Gregore84a9da2009-04-20 20:36:09 +00001924namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001925// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001926class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001927 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001928
1929public:
1930 typedef Selector key_type;
1931 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001932
Sebastian Redl834bb972010-08-04 17:20:04 +00001933 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001934 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001935 ObjCMethodList Instance, Factory;
1936 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001937 typedef const data_type& data_type_ref;
1938
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001939 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001940
Douglas Gregorc78d3462009-04-24 21:10:55 +00001941 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001942 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001943 }
Mike Stump11289f42009-09-09 15:08:12 +00001944
1945 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001946 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1947 data_type_ref Methods) {
1948 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1949 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001950 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1951 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001952 Method = Method->Next)
1953 if (Method->Method)
1954 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001955 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001956 Method = Method->Next)
1957 if (Method->Method)
1958 DataLen += 4;
1959 clang::io::Emit16(Out, DataLen);
1960 return std::make_pair(KeyLen, DataLen);
1961 }
Mike Stump11289f42009-09-09 15:08:12 +00001962
Douglas Gregor95c13f52009-04-25 17:48:32 +00001963 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001964 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001965 assert((Start >> 32) == 0 && "Selector key offset too large");
1966 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001967 unsigned N = Sel.getNumArgs();
1968 clang::io::Emit16(Out, N);
1969 if (N == 0)
1970 N = 1;
1971 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001972 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001973 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1974 }
Mike Stump11289f42009-09-09 15:08:12 +00001975
Douglas Gregorc78d3462009-04-24 21:10:55 +00001976 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001977 data_type_ref Methods, unsigned DataLen) {
1978 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001979 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001980 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001981 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001982 Method = Method->Next)
1983 if (Method->Method)
1984 ++NumInstanceMethods;
1985
1986 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001987 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001988 Method = Method->Next)
1989 if (Method->Method)
1990 ++NumFactoryMethods;
1991
1992 clang::io::Emit16(Out, NumInstanceMethods);
1993 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001994 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001995 Method = Method->Next)
1996 if (Method->Method)
1997 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001998 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001999 Method = Method->Next)
2000 if (Method->Method)
2001 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002002
2003 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002004 }
2005};
2006} // end anonymous namespace
2007
Sebastian Redla19a67f2010-08-03 21:58:15 +00002008/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002009///
2010/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002011/// in an on-disk hash table indexed by the selector. The hash table also
2012/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002013void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002014 using namespace llvm;
2015
Sebastian Redla19a67f2010-08-03 21:58:15 +00002016 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002017 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002018 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002019 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002020 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002021 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002022 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002023 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002024
Sebastian Redla19a67f2010-08-03 21:58:15 +00002025 // Create the on-disk hash table representation. We walk through every
2026 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002027 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002028 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002029 I = SelectorIDs.begin(), E = SelectorIDs.end();
2030 I != E; ++I) {
2031 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002032 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002033 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002034 I->second,
2035 ObjCMethodList(),
2036 ObjCMethodList()
2037 };
2038 if (F != SemaRef.MethodPool.end()) {
2039 Data.Instance = F->second.first;
2040 Data.Factory = F->second.second;
2041 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002042 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002043 // changed.
2044 if (Chain && I->second < FirstSelectorID) {
2045 // Selector already exists. Did it change?
2046 bool changed = false;
2047 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2048 M = M->Next) {
2049 if (M->Method->getPCHLevel() == 0)
2050 changed = true;
2051 }
2052 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2053 M = M->Next) {
2054 if (M->Method->getPCHLevel() == 0)
2055 changed = true;
2056 }
2057 if (!changed)
2058 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002059 } else if (Data.Instance.Method || Data.Factory.Method) {
2060 // A new method pool entry.
2061 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002062 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002063 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002064 }
2065
Douglas Gregorc78d3462009-04-24 21:10:55 +00002066 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002067 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002068 uint32_t BucketOffset;
2069 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002070 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002071 llvm::raw_svector_ostream Out(MethodPool);
2072 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002073 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002074 BucketOffset = Generator.Emit(Out, Trait);
2075 }
2076
2077 // Create a blob abbreviation
2078 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002079 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002080 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2083 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2084
Douglas Gregor95c13f52009-04-25 17:48:32 +00002085 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002086 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002087 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002088 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002089 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002090 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002091
2092 // Create a blob abbreviation for the selector table offsets.
2093 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002094 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2097 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2098
2099 // Write the selector offsets table.
2100 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002101 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002102 Record.push_back(SelectorOffsets.size());
2103 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002104 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002105 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002106 }
2107}
2108
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002109/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002110void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002111 using namespace llvm;
2112 if (SemaRef.ReferencedSelectors.empty())
2113 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002114
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002115 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002116
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002117 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002118 // very tricky to fix, and given that @selector shouldn't really appear in
2119 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002120 for (DenseMap<Selector, SourceLocation>::iterator S =
2121 SemaRef.ReferencedSelectors.begin(),
2122 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2123 Selector Sel = (*S).first;
2124 SourceLocation Loc = (*S).second;
2125 AddSelectorRef(Sel, Record);
2126 AddSourceLocation(Loc, Record);
2127 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002128 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002129}
2130
Douglas Gregorc5046832009-04-27 18:38:38 +00002131//===----------------------------------------------------------------------===//
2132// Identifier Table Serialization
2133//===----------------------------------------------------------------------===//
2134
Douglas Gregorc78d3462009-04-24 21:10:55 +00002135namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002136class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002137 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002138 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002139
Douglas Gregor1d583f22009-04-28 21:18:29 +00002140 /// \brief Determines whether this is an "interesting" identifier
2141 /// that needs a full IdentifierInfo structure written into the hash
2142 /// table.
2143 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2144 return II->isPoisoned() ||
2145 II->isExtensionToken() ||
2146 II->hasMacroDefinition() ||
2147 II->getObjCOrBuiltinID() ||
2148 II->getFETokenInfo<void>();
2149 }
2150
Douglas Gregore84a9da2009-04-20 20:36:09 +00002151public:
2152 typedef const IdentifierInfo* key_type;
2153 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002154
Sebastian Redl539c5062010-08-18 23:57:32 +00002155 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002156 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002157
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002158 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002159 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002160
2161 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002162 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002163 }
Mike Stump11289f42009-09-09 15:08:12 +00002164
2165 std::pair<unsigned,unsigned>
2166 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002167 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002168 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002169 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2170 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002171 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002172 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002173 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002174 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002175 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2176 DEnd = IdentifierResolver::end();
2177 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002178 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002179 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002180 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002181 // We emit the key length after the data length so that every
2182 // string is preceded by a 16-bit length. This matches the PTH
2183 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002184 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002185 return std::make_pair(KeyLen, DataLen);
2186 }
Mike Stump11289f42009-09-09 15:08:12 +00002187
2188 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002189 unsigned KeyLen) {
2190 // Record the location of the key data. This is used when generating
2191 // the mapping from persistent IDs to strings.
2192 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002193 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002194 }
Mike Stump11289f42009-09-09 15:08:12 +00002195
2196 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002197 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002198 if (!isInterestingIdentifier(II)) {
2199 clang::io::Emit32(Out, ID << 1);
2200 return;
2201 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002202
Douglas Gregor1d583f22009-04-28 21:18:29 +00002203 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002204 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002205 bool hasMacroDefinition =
2206 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002207 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002208 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002209 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2210 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2211 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002212 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002213 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002214 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002215
Douglas Gregorc3366a52009-04-21 23:56:24 +00002216 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002217 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002218
Douglas Gregora868bbd2009-04-21 22:25:48 +00002219 // Emit the declaration IDs in reverse order, because the
2220 // IdentifierResolver provides the declarations as they would be
2221 // visible (e.g., the function "stat" would come before the struct
2222 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2223 // adds declarations to the end of the list (so we need to see the
2224 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002225 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002226 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002227 IdentifierResolver::end());
2228 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2229 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002230 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002231 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002232 }
2233};
2234} // end anonymous namespace
2235
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002236/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002237///
2238/// The identifier table consists of a blob containing string data
2239/// (the actual identifiers themselves) and a separate "offsets" index
2240/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002241void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002242 using namespace llvm;
2243
2244 // Create and write out the blob that contains the identifier
2245 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002246 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002247 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002248 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002249
Douglas Gregore6648fb2009-04-28 20:33:11 +00002250 // Look for any identifiers that were named while processing the
2251 // headers, but are otherwise not needed. We add these to the hash
2252 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002253 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002254 // file.
2255 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2256 IDEnd = PP.getIdentifierTable().end();
2257 ID != IDEnd; ++ID)
2258 getIdentifierRef(ID->second);
2259
Sebastian Redlff4a2952010-07-23 23:49:55 +00002260 // Create the on-disk hash table representation. We only store offsets
2261 // for identifiers that appear here for the first time.
2262 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002263 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002264 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2265 ID != IDEnd; ++ID) {
2266 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002267 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002268 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002269 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002270
Douglas Gregore84a9da2009-04-20 20:36:09 +00002271 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002272 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002273 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002274 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002275 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002276 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002277 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002278 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002279 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002280 }
2281
2282 // Create a blob abbreviation
2283 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002284 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002287 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002288
2289 // Write the identifier table
2290 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002291 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002292 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002293 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002294 }
2295
2296 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002297 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002298 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2301 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2302
2303 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002304 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002305 Record.push_back(IdentifierOffsets.size());
2306 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002307 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002308 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002309}
2310
Douglas Gregorc5046832009-04-27 18:38:38 +00002311//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002312// DeclContext's Name Lookup Table Serialization
2313//===----------------------------------------------------------------------===//
2314
2315namespace {
2316// Trait used for the on-disk hash table used in the method pool.
2317class ASTDeclContextNameLookupTrait {
2318 ASTWriter &Writer;
2319
2320public:
2321 typedef DeclarationName key_type;
2322 typedef key_type key_type_ref;
2323
2324 typedef DeclContext::lookup_result data_type;
2325 typedef const data_type& data_type_ref;
2326
2327 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2328
2329 unsigned ComputeHash(DeclarationName Name) {
2330 llvm::FoldingSetNodeID ID;
2331 ID.AddInteger(Name.getNameKind());
2332
2333 switch (Name.getNameKind()) {
2334 case DeclarationName::Identifier:
2335 ID.AddString(Name.getAsIdentifierInfo()->getName());
2336 break;
2337 case DeclarationName::ObjCZeroArgSelector:
2338 case DeclarationName::ObjCOneArgSelector:
2339 case DeclarationName::ObjCMultiArgSelector:
2340 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2341 break;
2342 case DeclarationName::CXXConstructorName:
2343 case DeclarationName::CXXDestructorName:
2344 case DeclarationName::CXXConversionFunctionName:
2345 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2346 break;
2347 case DeclarationName::CXXOperatorName:
2348 ID.AddInteger(Name.getCXXOverloadedOperator());
2349 break;
2350 case DeclarationName::CXXLiteralOperatorName:
2351 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2352 case DeclarationName::CXXUsingDirective:
2353 break;
2354 }
2355
2356 return ID.ComputeHash();
2357 }
2358
2359 std::pair<unsigned,unsigned>
2360 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2361 data_type_ref Lookup) {
2362 unsigned KeyLen = 1;
2363 switch (Name.getNameKind()) {
2364 case DeclarationName::Identifier:
2365 case DeclarationName::ObjCZeroArgSelector:
2366 case DeclarationName::ObjCOneArgSelector:
2367 case DeclarationName::ObjCMultiArgSelector:
2368 case DeclarationName::CXXConstructorName:
2369 case DeclarationName::CXXDestructorName:
2370 case DeclarationName::CXXConversionFunctionName:
2371 case DeclarationName::CXXLiteralOperatorName:
2372 KeyLen += 4;
2373 break;
2374 case DeclarationName::CXXOperatorName:
2375 KeyLen += 1;
2376 break;
2377 case DeclarationName::CXXUsingDirective:
2378 break;
2379 }
2380 clang::io::Emit16(Out, KeyLen);
2381
2382 // 2 bytes for num of decls and 4 for each DeclID.
2383 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2384 clang::io::Emit16(Out, DataLen);
2385
2386 return std::make_pair(KeyLen, DataLen);
2387 }
2388
2389 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2390 using namespace clang::io;
2391
2392 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2393 Emit8(Out, Name.getNameKind());
2394 switch (Name.getNameKind()) {
2395 case DeclarationName::Identifier:
2396 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2397 break;
2398 case DeclarationName::ObjCZeroArgSelector:
2399 case DeclarationName::ObjCOneArgSelector:
2400 case DeclarationName::ObjCMultiArgSelector:
2401 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2402 break;
2403 case DeclarationName::CXXConstructorName:
2404 case DeclarationName::CXXDestructorName:
2405 case DeclarationName::CXXConversionFunctionName:
2406 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2407 break;
2408 case DeclarationName::CXXOperatorName:
2409 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2410 Emit8(Out, Name.getCXXOverloadedOperator());
2411 break;
2412 case DeclarationName::CXXLiteralOperatorName:
2413 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2414 break;
2415 case DeclarationName::CXXUsingDirective:
2416 break;
2417 }
2418 }
2419
2420 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2421 data_type Lookup, unsigned DataLen) {
2422 uint64_t Start = Out.tell(); (void)Start;
2423 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2424 for (; Lookup.first != Lookup.second; ++Lookup.first)
2425 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2426
2427 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2428 }
2429};
2430} // end anonymous namespace
2431
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002432/// \brief Write the block containing all of the declaration IDs
2433/// visible from the given DeclContext.
2434///
2435/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002436/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002437uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2438 DeclContext *DC) {
2439 if (DC->getPrimaryContext() != DC)
2440 return 0;
2441
2442 // Since there is no name lookup into functions or methods, don't bother to
2443 // build a visible-declarations table for these entities.
2444 if (DC->isFunctionOrMethod())
2445 return 0;
2446
2447 // If not in C++, we perform name lookup for the translation unit via the
2448 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2449 // FIXME: In C++ we need the visible declarations in order to "see" the
2450 // friend declarations, is there a way to do this without writing the table ?
2451 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2452 return 0;
2453
2454 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002455 if (DC->hasExternalVisibleStorage())
2456 DC->MaterializeVisibleDeclsFromExternalStorage();
2457 else
2458 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002459
2460 // Serialize the contents of the mapping used for lookup. Note that,
2461 // although we have two very different code paths, the serialized
2462 // representation is the same for both cases: a declaration name,
2463 // followed by a size, followed by references to the visible
2464 // declarations that have that name.
2465 uint64_t Offset = Stream.GetCurrentBitNo();
2466 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2467 if (!Map || Map->empty())
2468 return 0;
2469
2470 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2471 ASTDeclContextNameLookupTrait Trait(*this);
2472
2473 // Create the on-disk hash table representation.
2474 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2475 D != DEnd; ++D) {
2476 DeclarationName Name = D->first;
2477 DeclContext::lookup_result Result = D->second.getLookupResult();
2478 Generator.insert(Name, Result, Trait);
2479 }
2480
2481 // Create the on-disk hash table in a buffer.
2482 llvm::SmallString<4096> LookupTable;
2483 uint32_t BucketOffset;
2484 {
2485 llvm::raw_svector_ostream Out(LookupTable);
2486 // Make sure that no bucket is at offset 0
2487 clang::io::Emit32(Out, 0);
2488 BucketOffset = Generator.Emit(Out, Trait);
2489 }
2490
2491 // Write the lookup table
2492 RecordData Record;
2493 Record.push_back(DECL_CONTEXT_VISIBLE);
2494 Record.push_back(BucketOffset);
2495 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2496 LookupTable.str());
2497
2498 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2499 ++NumVisibleDeclContexts;
2500 return Offset;
2501}
2502
Sebastian Redla4071b42010-08-24 00:50:09 +00002503/// \brief Write an UPDATE_VISIBLE block for the given context.
2504///
2505/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2506/// DeclContext in a dependent AST file. As such, they only exist for the TU
2507/// (in C++) and for namespaces.
2508void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002509 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2510 if (!Map || Map->empty())
2511 return;
2512
2513 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2514 ASTDeclContextNameLookupTrait Trait(*this);
2515
2516 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002517 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2518 D != DEnd; ++D) {
2519 DeclarationName Name = D->first;
2520 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002521 // For any name that appears in this table, the results are complete, i.e.
2522 // they overwrite results from previous PCHs. Merging is always a mess.
2523 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002524 }
2525
2526 // Create the on-disk hash table in a buffer.
2527 llvm::SmallString<4096> LookupTable;
2528 uint32_t BucketOffset;
2529 {
2530 llvm::raw_svector_ostream Out(LookupTable);
2531 // Make sure that no bucket is at offset 0
2532 clang::io::Emit32(Out, 0);
2533 BucketOffset = Generator.Emit(Out, Trait);
2534 }
2535
2536 // Write the lookup table
2537 RecordData Record;
2538 Record.push_back(UPDATE_VISIBLE);
2539 Record.push_back(getDeclID(cast<Decl>(DC)));
2540 Record.push_back(BucketOffset);
2541 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2542}
2543
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002544/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2545void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2546 RecordData Record;
2547 Record.push_back(Opts.fp_contract);
2548 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2549}
2550
2551/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2552void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2553 if (!SemaRef.Context.getLangOptions().OpenCL)
2554 return;
2555
2556 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2557 RecordData Record;
2558#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2559#include "clang/Basic/OpenCLExtensions.def"
2560 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2561}
2562
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002563//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002564// General Serialization Routines
2565//===----------------------------------------------------------------------===//
2566
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002567/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002568void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002569 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002570 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2571 const Attr * A = *i;
2572 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2573 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002574
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002575#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002576
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002577 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002578}
2579
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002580void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002581 Record.push_back(Str.size());
2582 Record.insert(Record.end(), Str.begin(), Str.end());
2583}
2584
Douglas Gregore84a9da2009-04-20 20:36:09 +00002585/// \brief Note that the identifier II occurs at the given offset
2586/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002587void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002588 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002589 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002590 // up earlier in the chain and thus don't need an offset.
2591 if (ID >= FirstIdentID)
2592 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002593}
2594
Douglas Gregor95c13f52009-04-25 17:48:32 +00002595/// \brief Note that the selector Sel occurs at the given offset
2596/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002597void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002598 unsigned ID = SelectorIDs[Sel];
2599 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002600 // Don't record offsets for selectors that are also available in a different
2601 // file.
2602 if (ID < FirstSelectorID)
2603 return;
2604 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002605}
2606
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002607ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002608 : Stream(Stream), Chain(0), SerializationListener(0),
2609 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002610 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002611 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002612 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2613 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002614 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002615 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2616 NextCXXBaseSpecifiersID(1)
2617{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002618}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002619
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002620void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002621 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002622 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002623 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002624 Stream.Emit((unsigned)'C', 8);
2625 Stream.Emit((unsigned)'P', 8);
2626 Stream.Emit((unsigned)'C', 8);
2627 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002628
Chris Lattner28fa4e62009-04-26 22:26:21 +00002629 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002630
Sebastian Redl143413f2010-07-12 22:02:52 +00002631 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002632 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002633 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002634 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002635}
2636
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002637void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002638 const char *isysroot,
2639 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002640 using namespace llvm;
2641
2642 ASTContext &Context = SemaRef.Context;
2643 Preprocessor &PP = SemaRef.PP;
2644
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002645 // The translation unit is the first declaration we'll emit.
2646 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002647 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002648 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002649
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002650 // Make sure that we emit IdentifierInfos (and any attached
2651 // declarations) for builtins.
2652 {
2653 IdentifierTable &Table = PP.getIdentifierTable();
2654 llvm::SmallVector<const char *, 32> BuiltinNames;
2655 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2656 Context.getLangOptions().NoBuiltin);
2657 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2658 getIdentifierRef(&Table.get(BuiltinNames[I]));
2659 }
2660
Chris Lattner0c797362009-09-08 18:19:27 +00002661 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002662 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002663 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002664 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002665 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2666 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002667 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002668
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002669 // Build a record containing all of the file scoped decls in this file.
2670 RecordData UnusedFileScopedDecls;
2671 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2672 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002673
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002674 RecordData WeakUndeclaredIdentifiers;
2675 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2676 WeakUndeclaredIdentifiers.push_back(
2677 SemaRef.WeakUndeclaredIdentifiers.size());
2678 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2679 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2680 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2681 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2682 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2683 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2684 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2685 }
2686 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002687
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002688 // Build a record containing all of the locally-scoped external
2689 // declarations in this header file. Generally, this record will be
2690 // empty.
2691 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002692 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002693 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002694 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002695 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2696 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2697 TD != TDEnd; ++TD)
2698 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2699
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002700 // Build a record containing all of the ext_vector declarations.
2701 RecordData ExtVectorDecls;
2702 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2703 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2704
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002705 // Build a record containing all of the VTable uses information.
2706 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002707 if (!SemaRef.VTableUses.empty()) {
2708 VTableUses.push_back(SemaRef.VTableUses.size());
2709 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2710 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2711 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2712 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2713 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002714 }
2715
2716 // Build a record containing all of dynamic classes declarations.
2717 RecordData DynamicClasses;
2718 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2719 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2720
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002721 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002722 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002723 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002724 I = SemaRef.PendingInstantiations.begin(),
2725 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2726 AddDeclRef(I->first, PendingInstantiations);
2727 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002728 }
2729 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2730 "There are local ones at end of translation unit!");
2731
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002732 // Build a record containing some declaration references.
2733 RecordData SemaDeclRefs;
2734 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2735 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2736 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2737 }
2738
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002739 RecordData CUDASpecialDeclRefs;
2740 if (Context.getcudaConfigureCallDecl()) {
2741 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2742 }
2743
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002744 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002745 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002746 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002747 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002748 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002749 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002750 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002751 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002752 // Write the record of special types.
2753 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002754
Steve Naroffc277ad12009-07-18 15:33:26 +00002755 AddTypeRef(Context.getBuiltinVaListType(), Record);
2756 AddTypeRef(Context.getObjCIdType(), Record);
2757 AddTypeRef(Context.getObjCSelType(), Record);
2758 AddTypeRef(Context.getObjCProtoType(), Record);
2759 AddTypeRef(Context.getObjCClassType(), Record);
2760 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2761 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2762 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002763 AddTypeRef(Context.getjmp_bufType(), Record);
2764 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002765 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2766 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002767 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002768 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002769 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2770 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002771 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002772 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002773
Douglas Gregor1970d882009-04-26 03:49:13 +00002774 // Keep writing types and declarations until all types and
2775 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002776 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002777 WriteDeclsBlockAbbrevs();
2778 while (!DeclTypesToEmit.empty()) {
2779 DeclOrType DOT = DeclTypesToEmit.front();
2780 DeclTypesToEmit.pop();
2781 if (DOT.isType())
2782 WriteType(DOT.getType());
2783 else
2784 WriteDecl(Context, DOT.getDecl());
2785 }
2786 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002787
Douglas Gregor45053152009-10-17 17:25:45 +00002788 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002789 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002790 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002791 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002792 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002793 WriteFPPragmaOptions(SemaRef.getFPOptions());
2794 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002795
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002796 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002797 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002798
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002799 // Write the C++ base-specifier set offsets.
2800 if (!CXXBaseSpecifiersOffsets.empty()) {
2801 // Create a blob abbreviation for the C++ base specifiers offsets.
2802 using namespace llvm;
2803
2804 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2805 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2808 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2809
2810 // Write the selector offsets table.
2811 Record.clear();
2812 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2813 Record.push_back(CXXBaseSpecifiersOffsets.size());
2814 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2815 (const char *)CXXBaseSpecifiersOffsets.data(),
2816 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2817 }
2818
Douglas Gregord4df8652009-04-22 22:02:47 +00002819 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002820 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002821 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002822
2823 // Write the record containing tentative definitions.
2824 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002825 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002826
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002827 // Write the record containing unused file scoped decls.
2828 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002829 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002830
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002831 // Write the record containing weak undeclared identifiers.
2832 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002833 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002834 WeakUndeclaredIdentifiers);
2835
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002836 // Write the record containing locally-scoped external definitions.
2837 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002838 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002839 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002840
2841 // Write the record containing ext_vector type names.
2842 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002843 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002844
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002845 // Write the record containing VTable uses information.
2846 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002847 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002848
2849 // Write the record containing dynamic classes declarations.
2850 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002851 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002852
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002853 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002854 if (!PendingInstantiations.empty())
2855 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002856
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002857 // Write the record containing declaration references of Sema.
2858 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002859 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002860
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002861 // Write the record containing CUDA-specific declaration references.
2862 if (!CUDASpecialDeclRefs.empty())
2863 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2864
Douglas Gregor08f01292009-04-17 22:13:46 +00002865 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002866 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002867 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002868 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002869 Record.push_back(NumLexicalDeclContexts);
2870 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002871 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002872 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002873}
2874
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002875void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002876 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002877 using namespace llvm;
2878
2879 ASTContext &Context = SemaRef.Context;
2880 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002881
Sebastian Redl143413f2010-07-12 22:02:52 +00002882 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002883 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002884 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002885 if (StatCalls && !isysroot)
2886 WriteStatCache(*StatCalls);
2887 // FIXME: Source manager block should only write new stuff, which could be
2888 // done by tracking the largest ID in the chain
2889 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002890
2891 // The special types are in the chained PCH.
2892
2893 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002894 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002895 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002896 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002897 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2898 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002899 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002900 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002901 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002902 else if ((*I)->isChangedSinceDeserialization())
2903 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002904 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002905 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002906 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002907 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002908 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2909 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2910 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002911 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002912 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2913 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002914 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002915 // And a visible updates block for the DeclContexts.
2916 Abv = new llvm::BitCodeAbbrev();
2917 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2918 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2919 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2920 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2921 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2922 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002923
Sebastian Redl98912122010-07-27 23:01:28 +00002924 // Build a record containing all of the new tentative definitions in this
2925 // file, in TentativeDefinitions order.
2926 RecordData TentativeDefinitions;
2927 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2928 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2929 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2930 }
2931
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002932 // Build a record containing all of the file scoped decls in this file.
2933 RecordData UnusedFileScopedDecls;
2934 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2935 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2936 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002937 }
2938
Sebastian Redl08aca90252010-08-05 18:21:25 +00002939 // We write the entire table, overwriting the tables from the chain.
2940 RecordData WeakUndeclaredIdentifiers;
2941 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2942 WeakUndeclaredIdentifiers.push_back(
2943 SemaRef.WeakUndeclaredIdentifiers.size());
2944 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2945 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2946 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2947 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2948 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2949 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2950 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2951 }
2952 }
2953
Sebastian Redl98912122010-07-27 23:01:28 +00002954 // Build a record containing all of the locally-scoped external
2955 // declarations in this header file. Generally, this record will be
2956 // empty.
2957 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002958 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002959 // nondeterminstic!
2960 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2961 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2962 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2963 TD != TDEnd; ++TD) {
2964 if (TD->second->getPCHLevel() == 0)
2965 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2966 }
2967
2968 // Build a record containing all of the ext_vector declarations.
2969 RecordData ExtVectorDecls;
2970 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2971 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2972 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2973 }
2974
Sebastian Redl08aca90252010-08-05 18:21:25 +00002975 // Build a record containing all of the VTable uses information.
2976 // We write everything here, because it's too hard to determine whether
2977 // a use is new to this part.
2978 RecordData VTableUses;
2979 if (!SemaRef.VTableUses.empty()) {
2980 VTableUses.push_back(SemaRef.VTableUses.size());
2981 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2982 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2983 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2984 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2985 }
2986 }
2987
2988 // Build a record containing all of dynamic classes declarations.
2989 RecordData DynamicClasses;
2990 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2991 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2992 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2993
2994 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002995 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002996 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002997 I = SemaRef.PendingInstantiations.begin(),
2998 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002999 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00003000 AddDeclRef(I->first, PendingInstantiations);
3001 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003002 }
3003 }
3004 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3005 "There are local ones at end of translation unit!");
3006
3007 // Build a record containing some declaration references.
3008 // It's not worth the effort to avoid duplication here.
3009 RecordData SemaDeclRefs;
3010 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3011 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3012 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3013 }
3014
Sebastian Redl539c5062010-08-18 23:57:32 +00003015 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003016 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003017 for (DeclsToRewriteTy::iterator
3018 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3019 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003020 while (!DeclTypesToEmit.empty()) {
3021 DeclOrType DOT = DeclTypesToEmit.front();
3022 DeclTypesToEmit.pop();
3023 if (DOT.isType())
3024 WriteType(DOT.getType());
3025 else
3026 WriteDecl(Context, DOT.getDecl());
3027 }
3028 Stream.ExitBlock();
3029
Sebastian Redl98912122010-07-27 23:01:28 +00003030 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003031 WriteSelectors(SemaRef);
3032 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003033 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003034 WriteFPPragmaOptions(SemaRef.getFPOptions());
3035 WriteOpenCLExtensions(SemaRef);
3036
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003037 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003038 // FIXME: For chained PCH only write the new mappings (we currently
3039 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003040 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003041
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003042 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003043 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003044 RecordData FirstLatestDeclIDs;
3045 for (FirstLatestDeclMap::iterator
3046 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3047 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3048 "Expected first & second to be in different PCHs");
3049 AddDeclRef(I->first, FirstLatestDeclIDs);
3050 AddDeclRef(I->second, FirstLatestDeclIDs);
3051 }
3052 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003053 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003054
Sebastian Redl98912122010-07-27 23:01:28 +00003055 // Write the record containing external, unnamed definitions.
3056 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003057 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003058
3059 // Write the record containing tentative definitions.
3060 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003061 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003062
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003063 // Write the record containing unused file scoped decls.
3064 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003065 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003066
Sebastian Redl08aca90252010-08-05 18:21:25 +00003067 // Write the record containing weak undeclared identifiers.
3068 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003069 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003070 WeakUndeclaredIdentifiers);
3071
Sebastian Redl98912122010-07-27 23:01:28 +00003072 // Write the record containing locally-scoped external definitions.
3073 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003074 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003075 LocallyScopedExternalDecls);
3076
3077 // Write the record containing ext_vector type names.
3078 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003079 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003080
Sebastian Redl08aca90252010-08-05 18:21:25 +00003081 // Write the record containing VTable uses information.
3082 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003083 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003084
3085 // Write the record containing dynamic classes declarations.
3086 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003087 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003088
3089 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003090 if (!PendingInstantiations.empty())
3091 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003092
3093 // Write the record containing declaration references of Sema.
3094 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003095 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003096
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003097 // Write the updates to DeclContexts.
3098 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3099 I = UpdatedDeclContexts.begin(),
3100 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003101 I != E; ++I)
3102 WriteDeclContextVisibleUpdate(*I);
3103
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003104 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003105
Sebastian Redl98912122010-07-27 23:01:28 +00003106 Record.clear();
3107 Record.push_back(NumStatements);
3108 Record.push_back(NumMacros);
3109 Record.push_back(NumLexicalDeclContexts);
3110 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003111 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003112 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003113 Stream.ExitBlock();
3114}
3115
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003116void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003117 if (DeclUpdates.empty())
3118 return;
3119
3120 RecordData OffsetsRecord;
3121 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3122 for (DeclUpdateMap::iterator
3123 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3124 const Decl *D = I->first;
3125 UpdateRecord &URec = I->second;
3126
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003127 if (DeclsToRewrite.count(D))
3128 continue; // The decl will be written completely,no need to store updates.
3129
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003130 uint64_t Offset = Stream.GetCurrentBitNo();
3131 Stream.EmitRecord(DECL_UPDATES, URec);
3132
3133 OffsetsRecord.push_back(GetDeclRef(D));
3134 OffsetsRecord.push_back(Offset);
3135 }
3136 Stream.ExitBlock();
3137 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3138}
3139
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003140void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003141 if (ReplacedDecls.empty())
3142 return;
3143
3144 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003145 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003146 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3147 Record.push_back(I->first);
3148 Record.push_back(I->second);
3149 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003150 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003151}
3152
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003153void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003154 Record.push_back(Loc.getRawEncoding());
3155}
3156
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003157void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003158 AddSourceLocation(Range.getBegin(), Record);
3159 AddSourceLocation(Range.getEnd(), Record);
3160}
3161
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003162void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003163 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003164 const uint64_t *Words = Value.getRawData();
3165 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003166}
3167
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003168void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003169 Record.push_back(Value.isUnsigned());
3170 AddAPInt(Value, Record);
3171}
3172
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003173void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003174 AddAPInt(Value.bitcastToAPInt(), Record);
3175}
3176
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003177void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003178 Record.push_back(getIdentifierRef(II));
3179}
3180
Sebastian Redl539c5062010-08-18 23:57:32 +00003181IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003182 if (II == 0)
3183 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003184
Sebastian Redl539c5062010-08-18 23:57:32 +00003185 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003186 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003187 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003188 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003189}
3190
Sebastian Redl50e26582010-09-15 19:54:06 +00003191MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003192 if (MD == 0)
3193 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003194
3195 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003196 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003197 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003198 return ID;
3199}
3200
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003201void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003202 Record.push_back(getSelectorRef(SelRef));
3203}
3204
Sebastian Redl539c5062010-08-18 23:57:32 +00003205SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003206 if (Sel.getAsOpaquePtr() == 0) {
3207 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003208 }
3209
Sebastian Redl539c5062010-08-18 23:57:32 +00003210 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003211 if (SID == 0 && Chain) {
3212 // This might trigger a ReadSelector callback, which will set the ID for
3213 // this selector.
3214 Chain->LoadSelector(Sel);
3215 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003216 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003217 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003218 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003219 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003220}
3221
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003222void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003223 AddDeclRef(Temp->getDestructor(), Record);
3224}
3225
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003226void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3227 CXXBaseSpecifier const *BasesEnd,
3228 RecordDataImpl &Record) {
3229 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3230 CXXBaseSpecifiersToWrite.push_back(
3231 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3232 Bases, BasesEnd));
3233 Record.push_back(NextCXXBaseSpecifiersID++);
3234}
3235
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003236void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003237 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003238 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003239 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003240 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003241 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003242 break;
3243 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003244 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003245 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003246 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003247 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3248 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003249 break;
3250 case TemplateArgument::TemplateExpansion:
3251 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3252 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003253 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003254 break;
John McCall0ad16662009-10-29 08:12:44 +00003255 case TemplateArgument::Null:
3256 case TemplateArgument::Integral:
3257 case TemplateArgument::Declaration:
3258 case TemplateArgument::Pack:
3259 break;
3260 }
3261}
3262
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003263void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003264 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003265 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003266
3267 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3268 bool InfoHasSameExpr
3269 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3270 Record.push_back(InfoHasSameExpr);
3271 if (InfoHasSameExpr)
3272 return; // Avoid storing the same expr twice.
3273 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003274 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3275 Record);
3276}
3277
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003278void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003279 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003280 AddTypeRef(QualType(), Record);
3281 return;
3282 }
3283
John McCallbcd03502009-12-07 02:54:59 +00003284 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00003285 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00003286 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003287 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003288}
3289
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003290void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003291 Record.push_back(GetOrCreateTypeID(T));
3292}
3293
3294TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003295 return MakeTypeID(T,
3296 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3297}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003298
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003299TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003300 return MakeTypeID(T,
3301 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003302}
3303
3304TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3305 if (T.isNull())
3306 return TypeIdx();
3307 assert(!T.getLocalFastQualifiers());
3308
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003309 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003310 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003311 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003312 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003313 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003314 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003315 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003316 return Idx;
3317}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003318
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003319TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003320 if (T.isNull())
3321 return TypeIdx();
3322 assert(!T.getLocalFastQualifiers());
3323
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003324 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3325 assert(I != TypeIdxs.end() && "Type not emitted!");
3326 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003327}
3328
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003329void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003330 Record.push_back(GetDeclRef(D));
3331}
3332
Sebastian Redl539c5062010-08-18 23:57:32 +00003333DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003334 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003335 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003336 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003337 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003338 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003339 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003340 // We haven't seen this declaration before. Give it a new ID and
3341 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003342 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003343 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003344 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3345 // We don't add it to the replacement collection here, because we don't
3346 // have the offset yet.
3347 DeclTypesToEmit.push(const_cast<Decl *>(D));
3348 // Reset the flag, so that we don't add this decl multiple times.
3349 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003350 }
3351
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003352 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003353}
3354
Sebastian Redl539c5062010-08-18 23:57:32 +00003355DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003356 if (D == 0)
3357 return 0;
3358
3359 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3360 return DeclIDs[D];
3361}
3362
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003363void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003364 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003365 Record.push_back(Name.getNameKind());
3366 switch (Name.getNameKind()) {
3367 case DeclarationName::Identifier:
3368 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3369 break;
3370
3371 case DeclarationName::ObjCZeroArgSelector:
3372 case DeclarationName::ObjCOneArgSelector:
3373 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003374 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003375 break;
3376
3377 case DeclarationName::CXXConstructorName:
3378 case DeclarationName::CXXDestructorName:
3379 case DeclarationName::CXXConversionFunctionName:
3380 AddTypeRef(Name.getCXXNameType(), Record);
3381 break;
3382
3383 case DeclarationName::CXXOperatorName:
3384 Record.push_back(Name.getCXXOverloadedOperator());
3385 break;
3386
Alexis Hunt3d221f22009-11-29 07:34:05 +00003387 case DeclarationName::CXXLiteralOperatorName:
3388 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3389 break;
3390
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003391 case DeclarationName::CXXUsingDirective:
3392 // No extra data to emit
3393 break;
3394 }
3395}
Chris Lattnerca025db2010-05-07 21:43:38 +00003396
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003397void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003398 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003399 switch (Name.getNameKind()) {
3400 case DeclarationName::CXXConstructorName:
3401 case DeclarationName::CXXDestructorName:
3402 case DeclarationName::CXXConversionFunctionName:
3403 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3404 break;
3405
3406 case DeclarationName::CXXOperatorName:
3407 AddSourceLocation(
3408 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3409 Record);
3410 AddSourceLocation(
3411 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3412 Record);
3413 break;
3414
3415 case DeclarationName::CXXLiteralOperatorName:
3416 AddSourceLocation(
3417 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3418 Record);
3419 break;
3420
3421 case DeclarationName::Identifier:
3422 case DeclarationName::ObjCZeroArgSelector:
3423 case DeclarationName::ObjCOneArgSelector:
3424 case DeclarationName::ObjCMultiArgSelector:
3425 case DeclarationName::CXXUsingDirective:
3426 break;
3427 }
3428}
3429
3430void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003431 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003432 AddDeclarationName(NameInfo.getName(), Record);
3433 AddSourceLocation(NameInfo.getLoc(), Record);
3434 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3435}
3436
3437void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003438 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003439 AddNestedNameSpecifier(Info.NNS, Record);
3440 AddSourceRange(Info.NNSRange, Record);
3441 Record.push_back(Info.NumTemplParamLists);
3442 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3443 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3444}
3445
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003446void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003447 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003448 // Nested name specifiers usually aren't too long. I think that 8 would
3449 // typically accomodate the vast majority.
3450 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3451
3452 // Push each of the NNS's onto a stack for serialization in reverse order.
3453 while (NNS) {
3454 NestedNames.push_back(NNS);
3455 NNS = NNS->getPrefix();
3456 }
3457
3458 Record.push_back(NestedNames.size());
3459 while(!NestedNames.empty()) {
3460 NNS = NestedNames.pop_back_val();
3461 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3462 Record.push_back(Kind);
3463 switch (Kind) {
3464 case NestedNameSpecifier::Identifier:
3465 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3466 break;
3467
3468 case NestedNameSpecifier::Namespace:
3469 AddDeclRef(NNS->getAsNamespace(), Record);
3470 break;
3471
3472 case NestedNameSpecifier::TypeSpec:
3473 case NestedNameSpecifier::TypeSpecWithTemplate:
3474 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3475 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3476 break;
3477
3478 case NestedNameSpecifier::Global:
3479 // Don't need to write an associated value.
3480 break;
3481 }
3482 }
3483}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003484
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003485void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003486 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003487 Record.push_back(Kind);
3488 switch (Kind) {
3489 case TemplateName::Template:
3490 AddDeclRef(Name.getAsTemplateDecl(), Record);
3491 break;
3492
3493 case TemplateName::OverloadedTemplate: {
3494 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3495 Record.push_back(OvT->size());
3496 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3497 I != E; ++I)
3498 AddDeclRef(*I, Record);
3499 break;
3500 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003501
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003502 case TemplateName::QualifiedTemplate: {
3503 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3504 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3505 Record.push_back(QualT->hasTemplateKeyword());
3506 AddDeclRef(QualT->getTemplateDecl(), Record);
3507 break;
3508 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003509
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003510 case TemplateName::DependentTemplate: {
3511 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3512 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3513 Record.push_back(DepT->isIdentifier());
3514 if (DepT->isIdentifier())
3515 AddIdentifierRef(DepT->getIdentifier(), Record);
3516 else
3517 Record.push_back(DepT->getOperator());
3518 break;
3519 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003520
3521 case TemplateName::SubstTemplateTemplateParmPack: {
3522 SubstTemplateTemplateParmPackStorage *SubstPack
3523 = Name.getAsSubstTemplateTemplateParmPack();
3524 AddDeclRef(SubstPack->getParameterPack(), Record);
3525 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3526 break;
3527 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003528 }
3529}
3530
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003531void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003532 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003533 Record.push_back(Arg.getKind());
3534 switch (Arg.getKind()) {
3535 case TemplateArgument::Null:
3536 break;
3537 case TemplateArgument::Type:
3538 AddTypeRef(Arg.getAsType(), Record);
3539 break;
3540 case TemplateArgument::Declaration:
3541 AddDeclRef(Arg.getAsDecl(), Record);
3542 break;
3543 case TemplateArgument::Integral:
3544 AddAPSInt(*Arg.getAsIntegral(), Record);
3545 AddTypeRef(Arg.getIntegralType(), Record);
3546 break;
3547 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003548 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3549 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003550 case TemplateArgument::TemplateExpansion:
3551 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003552 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3553 Record.push_back(*NumExpansions + 1);
3554 else
3555 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003556 break;
3557 case TemplateArgument::Expression:
3558 AddStmt(Arg.getAsExpr());
3559 break;
3560 case TemplateArgument::Pack:
3561 Record.push_back(Arg.pack_size());
3562 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3563 I != E; ++I)
3564 AddTemplateArgument(*I, Record);
3565 break;
3566 }
3567}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003568
3569void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003570ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003571 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003572 assert(TemplateParams && "No TemplateParams!");
3573 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3574 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3575 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3576 Record.push_back(TemplateParams->size());
3577 for (TemplateParameterList::const_iterator
3578 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3579 P != PEnd; ++P)
3580 AddDeclRef(*P, Record);
3581}
3582
3583/// \brief Emit a template argument list.
3584void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003585ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003586 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003587 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003588 Record.push_back(TemplateArgs->size());
3589 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003590 AddTemplateArgument(TemplateArgs->get(i), Record);
3591}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003592
3593
3594void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003595ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003596 Record.push_back(Set.size());
3597 for (UnresolvedSetImpl::const_iterator
3598 I = Set.begin(), E = Set.end(); I != E; ++I) {
3599 AddDeclRef(I.getDecl(), Record);
3600 Record.push_back(I.getAccess());
3601 }
3602}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003603
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003604void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003605 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003606 Record.push_back(Base.isVirtual());
3607 Record.push_back(Base.isBaseOfClass());
3608 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003609 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003610 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003611 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003612 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3613 : SourceLocation(),
3614 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003615}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003616
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003617void ASTWriter::FlushCXXBaseSpecifiers() {
3618 RecordData Record;
3619 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3620 Record.clear();
3621
3622 // Record the offset of this base-specifier set.
3623 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3624 if (Index == CXXBaseSpecifiersOffsets.size())
3625 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3626 else {
3627 if (Index > CXXBaseSpecifiersOffsets.size())
3628 CXXBaseSpecifiersOffsets.resize(Index + 1);
3629 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3630 }
3631
3632 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3633 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3634 Record.push_back(BEnd - B);
3635 for (; B != BEnd; ++B)
3636 AddCXXBaseSpecifier(*B, Record);
3637 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003638
3639 // Flush any expressions that were written as part of the base specifiers.
3640 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003641 }
3642
3643 CXXBaseSpecifiersToWrite.clear();
3644}
3645
Alexis Hunt1d792652011-01-08 20:30:50 +00003646void ASTWriter::AddCXXCtorInitializers(
3647 const CXXCtorInitializer * const *CtorInitializers,
3648 unsigned NumCtorInitializers,
3649 RecordDataImpl &Record) {
3650 Record.push_back(NumCtorInitializers);
3651 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3652 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003653
3654 Record.push_back(Init->isBaseInitializer());
3655 if (Init->isBaseInitializer()) {
3656 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3657 Record.push_back(Init->isBaseVirtual());
3658 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003659 Record.push_back(Init->isIndirectMemberInitializer());
3660 if (Init->isIndirectMemberInitializer())
3661 AddDeclRef(Init->getIndirectMember(), Record);
3662 else
3663 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003664 }
Francois Pichetd583da02010-12-04 09:14:42 +00003665
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003666 AddSourceLocation(Init->getMemberLocation(), Record);
3667 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003668 AddSourceLocation(Init->getLParenLoc(), Record);
3669 AddSourceLocation(Init->getRParenLoc(), Record);
3670 Record.push_back(Init->isWritten());
3671 if (Init->isWritten()) {
3672 Record.push_back(Init->getSourceOrder());
3673 } else {
3674 Record.push_back(Init->getNumArrayIndices());
3675 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3676 AddDeclRef(Init->getArrayIndex(i), Record);
3677 }
3678 }
3679}
3680
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003681void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3682 assert(D->DefinitionData);
3683 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3684 Record.push_back(Data.UserDeclaredConstructor);
3685 Record.push_back(Data.UserDeclaredCopyConstructor);
3686 Record.push_back(Data.UserDeclaredCopyAssignment);
3687 Record.push_back(Data.UserDeclaredDestructor);
3688 Record.push_back(Data.Aggregate);
3689 Record.push_back(Data.PlainOldData);
3690 Record.push_back(Data.Empty);
3691 Record.push_back(Data.Polymorphic);
3692 Record.push_back(Data.Abstract);
3693 Record.push_back(Data.HasTrivialConstructor);
3694 Record.push_back(Data.HasTrivialCopyConstructor);
3695 Record.push_back(Data.HasTrivialCopyAssignment);
3696 Record.push_back(Data.HasTrivialDestructor);
3697 Record.push_back(Data.ComputedVisibleConversions);
3698 Record.push_back(Data.DeclaredDefaultConstructor);
3699 Record.push_back(Data.DeclaredCopyConstructor);
3700 Record.push_back(Data.DeclaredCopyAssignment);
3701 Record.push_back(Data.DeclaredDestructor);
3702
3703 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003704 if (Data.NumBases > 0)
3705 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3706 Record);
3707
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003708 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3709 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003710 if (Data.NumVBases > 0)
3711 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3712 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003713
3714 AddUnresolvedSet(Data.Conversions, Record);
3715 AddUnresolvedSet(Data.VisibleConversions, Record);
3716 // Data.Definition is the owning decl, no need to write it.
3717 AddDeclRef(Data.FirstFriend, Record);
3718}
3719
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003720void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003721 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003722 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003723 assert(FirstDeclID == NextDeclID &&
3724 FirstTypeID == NextTypeID &&
3725 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003726 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003727 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003728 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003729 "Setting chain after writing has started.");
3730 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003731
3732 FirstDeclID += Chain->getTotalNumDecls();
3733 FirstTypeID += Chain->getTotalNumTypes();
3734 FirstIdentID += Chain->getTotalNumIdentifiers();
3735 FirstSelectorID += Chain->getTotalNumSelectors();
3736 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003737 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003738 NextDeclID = FirstDeclID;
3739 NextTypeID = FirstTypeID;
3740 NextIdentID = FirstIdentID;
3741 NextSelectorID = FirstSelectorID;
3742 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003743 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003744}
3745
Sebastian Redl539c5062010-08-18 23:57:32 +00003746void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003747 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003748 if (II->hasMacroDefinition())
3749 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003750}
3751
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003752void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003753 // Always take the highest-numbered type index. This copes with an interesting
3754 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003755 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003756 // keep the higher-numbered entry so that we can properly write it out to
3757 // the AST file.
3758 TypeIdx &StoredIdx = TypeIdxs[T];
3759 if (Idx.getIndex() >= StoredIdx.getIndex())
3760 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003761}
3762
Sebastian Redl539c5062010-08-18 23:57:32 +00003763void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003764 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003765}
Sebastian Redl834bb972010-08-04 17:20:04 +00003766
Sebastian Redl539c5062010-08-18 23:57:32 +00003767void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003768 SelectorIDs[S] = ID;
3769}
Douglas Gregor91096292010-10-02 19:29:26 +00003770
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003771void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003772 MacroDefinition *MD) {
3773 MacroDefinitions[MD] = ID;
3774}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003775
3776void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3777 assert(D->isDefinition());
3778 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3779 // We are interested when a PCH decl is modified.
3780 if (RD->getPCHLevel() > 0) {
3781 // A forward reference was mutated into a definition. Rewrite it.
3782 // FIXME: This happens during template instantiation, should we
3783 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003784 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003785 }
3786
3787 for (CXXRecordDecl::redecl_iterator
3788 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3789 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3790 if (Redecl == RD)
3791 continue;
3792
3793 // We are interested when a PCH decl is modified.
3794 if (Redecl->getPCHLevel() > 0) {
3795 UpdateRecord &Record = DeclUpdates[Redecl];
3796 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3797 assert(Redecl->DefinitionData);
3798 assert(Redecl->DefinitionData->Definition == D);
3799 AddDeclRef(D, Record); // the DefinitionDecl
3800 }
3801 }
3802 }
3803}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003804void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3805 // TU and namespaces are handled elsewhere.
3806 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3807 return;
3808
3809 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3810 return; // Not a source decl added to a DeclContext from PCH.
3811
3812 AddUpdatedDeclContext(DC);
3813}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003814
3815void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3816 assert(D->isImplicit());
3817 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3818 return; // Not a source member added to a class from PCH.
3819 if (!isa<CXXMethodDecl>(D))
3820 return; // We are interested in lazily declared implicit methods.
3821
3822 // A decl coming from PCH was modified.
3823 assert(RD->isDefinition());
3824 UpdateRecord &Record = DeclUpdates[RD];
3825 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3826 AddDeclRef(D, Record);
3827}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003828
3829void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3830 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003831 // The specializations set is kept in the canonical template.
3832 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003833 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3834 return; // Not a source specialization added to a template from PCH.
3835
3836 UpdateRecord &Record = DeclUpdates[TD];
3837 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3838 AddDeclRef(D, Record);
3839}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003840
3841ASTSerializationListener::~ASTSerializationListener() { }