blob: 77d99cd6b354894cdbea0cc9aaea2dab2393fdcd [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"
Douglas Gregora7f71a92009-04-10 03:52:48 +000044#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000045#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000046#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000048using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049
Sebastian Redl3df5a082010-07-30 17:03:48 +000050template <typename T, typename Allocator>
51T *data(std::vector<T, Allocator> &v) {
52 return v.empty() ? 0 : &v.front();
53}
54template <typename T, typename Allocator>
55const T *data(const std::vector<T, Allocator> &v) {
56 return v.empty() ? 0 : &v.front();
57}
58
Douglas Gregoref84c4b2009-04-09 22:27:44 +000059//===----------------------------------------------------------------------===//
60// Type serialization
61//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000062
Douglas Gregoref84c4b2009-04-09 22:27:44 +000063namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000064 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000065 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000066 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000067
68 public:
69 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000070 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000072 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000073 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000074
75 void VisitArrayType(const ArrayType *T);
76 void VisitFunctionType(const FunctionType *T);
77 void VisitTagType(const TagType *T);
78
79#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
80#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000081#include "clang/AST/TypeNodes.def"
82 };
83}
84
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000085void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000086 assert(false && "Built-in types are never serialized");
87}
88
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000089void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000091 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092}
93
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000094void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000096 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097}
98
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000099void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000100 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000101 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102}
103
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000104void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000106 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107}
108
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000109void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000111 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112}
113
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000114void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000115 Writer.AddTypeRef(T->getPointeeType(), Record);
116 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000117 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000118}
119
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000120void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121 Writer.AddTypeRef(T->getElementType(), Record);
122 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000123 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000124}
125
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000126void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000127 VisitArrayType(T);
128 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000129 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130}
131
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000134 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135}
136
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000137void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000138 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000139 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
140 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000141 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000142 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143}
144
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146 Writer.AddTypeRef(T->getElementType(), Record);
147 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000148 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000149 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150}
151
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000152void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000154 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155}
156
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000159 FunctionType::ExtInfo C = T->getExtInfo();
160 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000161 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000162 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000163 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000168 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169}
170
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000172 VisitFunctionType(T);
173 Record.push_back(T->getNumArgs());
174 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
175 Writer.AddTypeRef(T->getArgType(I), Record);
176 Record.push_back(T->isVariadic());
177 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000178 Record.push_back(T->hasExceptionSpec());
179 Record.push_back(T->hasAnyExceptionSpec());
180 Record.push_back(T->getNumExceptions());
181 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
182 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000183 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184}
185
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000187 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000188 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000189}
John McCallb96ec562009-12-04 22:46:56 +0000190
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000191void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000193 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
194 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000195 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196}
197
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000198void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000199 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000200 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000201}
202
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000203void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000205 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000206}
207
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000208void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000209 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000210 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000211}
212
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000214 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000215 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000216 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217 "Cannot serialize in the middle of a type definition");
218}
219
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000220void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000221 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000222 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223}
224
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000225void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000227 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228}
229
Mike Stump11289f42009-09-09 15:08:12 +0000230void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000231ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000232 const SubstTemplateTypeParmType *T) {
233 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
234 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000235 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000236}
237
238void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000241 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000242 Writer.AddTemplateName(T->getTemplateName(), Record);
243 Record.push_back(T->getNumArgs());
244 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
245 ArgI != ArgE; ++ArgI)
246 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000247 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
248 : T->getCanonicalTypeInternal(),
249 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000250 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000251}
252
253void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000254ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000255 VisitArrayType(T);
256 Writer.AddStmt(T->getSizeExpr());
257 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000258 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000259}
260
261void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000262ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000263 const DependentSizedExtVectorType *T) {
264 // FIXME: Serialize this type (C++ only)
265 assert(false && "Cannot serialize dependent sized extended vector types");
266}
267
268void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000269ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000270 Record.push_back(T->getDepth());
271 Record.push_back(T->getIndex());
272 Record.push_back(T->isParameterPack());
273 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000274 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000275}
276
277void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000278ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000279 Record.push_back(T->getKeyword());
280 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
281 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000282 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
283 : T->getCanonicalTypeInternal(),
284 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000285 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000286}
287
288void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000289ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000290 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000291 Record.push_back(T->getKeyword());
292 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
293 Writer.AddIdentifierRef(T->getIdentifier(), Record);
294 Record.push_back(T->getNumArgs());
295 for (DependentTemplateSpecializationType::iterator
296 I = T->begin(), E = T->end(); I != E; ++I)
297 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000298 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000299}
300
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000301void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000302 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000303 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
304 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000305 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000306}
307
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000308void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000309 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000310 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000311 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000312}
313
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000314void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000315 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000316 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000317}
318
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000319void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000320 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000321 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000322 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000323 E = T->qual_end(); I != E; ++I)
324 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000325 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000326}
327
Steve Narofffb4330f2009-06-17 22:40:22 +0000328void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000329ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000330 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000331 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000332}
333
John McCall8f115c62009-10-16 21:56:05 +0000334namespace {
335
336class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000337 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000338 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000339
340public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000341 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000342 : Writer(Writer), Record(Record) { }
343
John McCall17001972009-10-18 01:05:36 +0000344#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000345#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000346 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000347#include "clang/AST/TypeLocNodes.def"
348
John McCall17001972009-10-18 01:05:36 +0000349 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
350 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000351};
352
353}
354
John McCall17001972009-10-18 01:05:36 +0000355void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
356 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000357}
John McCall17001972009-10-18 01:05:36 +0000358void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000359 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
360 if (TL.needsExtraLocalData()) {
361 Record.push_back(TL.getWrittenTypeSpec());
362 Record.push_back(TL.getWrittenSignSpec());
363 Record.push_back(TL.getWrittenWidthSpec());
364 Record.push_back(TL.hasModeAttr());
365 }
John McCall8f115c62009-10-16 21:56:05 +0000366}
John McCall17001972009-10-18 01:05:36 +0000367void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000369}
John McCall17001972009-10-18 01:05:36 +0000370void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000372}
John McCall17001972009-10-18 01:05:36 +0000373void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000375}
John McCall17001972009-10-18 01:05:36 +0000376void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000378}
John McCall17001972009-10-18 01:05:36 +0000379void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000381}
John McCall17001972009-10-18 01:05:36 +0000382void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000384}
John McCall17001972009-10-18 01:05:36 +0000385void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
387 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
388 Record.push_back(TL.getSizeExpr() ? 1 : 0);
389 if (TL.getSizeExpr())
390 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000391}
John McCall17001972009-10-18 01:05:36 +0000392void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
393 VisitArrayTypeLoc(TL);
394}
395void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
396 VisitArrayTypeLoc(TL);
397}
398void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
399 VisitArrayTypeLoc(TL);
400}
401void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
402 DependentSizedArrayTypeLoc TL) {
403 VisitArrayTypeLoc(TL);
404}
405void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
406 DependentSizedExtVectorTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
409void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getNameLoc(), Record);
414}
415void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
416 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
417 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000418 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000419 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
420 Writer.AddDeclRef(TL.getArg(i), Record);
421}
422void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
423 VisitFunctionTypeLoc(TL);
424}
425void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
426 VisitFunctionTypeLoc(TL);
427}
John McCallb96ec562009-12-04 22:46:56 +0000428void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getNameLoc(), Record);
430}
John McCall17001972009-10-18 01:05:36 +0000431void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getNameLoc(), Record);
433}
434void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000435 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
436 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
437 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000438}
439void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000440 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
441 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
442 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
443 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000444}
445void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getNameLoc(), Record);
447}
448void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
449 Writer.AddSourceLocation(TL.getNameLoc(), Record);
450}
451void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
452 Writer.AddSourceLocation(TL.getNameLoc(), Record);
453}
John McCall17001972009-10-18 01:05:36 +0000454void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
456}
John McCallcebee162009-10-18 09:09:24 +0000457void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
458 SubstTemplateTypeParmTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getNameLoc(), Record);
460}
John McCall17001972009-10-18 01:05:36 +0000461void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
462 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000463 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
464 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
465 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
466 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000467 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
468 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000469}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000470void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000471 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
472 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000473}
John McCalle78aac42010-03-10 03:28:59 +0000474void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
475 Writer.AddSourceLocation(TL.getNameLoc(), Record);
476}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000477void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000478 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
479 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
481}
John McCallc392f372010-06-11 00:33:02 +0000482void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
483 DependentTemplateSpecializationTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
485 Writer.AddSourceRange(TL.getQualifierRange(), Record);
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
487 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
488 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
489 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000490 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
491 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000492}
John McCall17001972009-10-18 01:05:36 +0000493void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000495}
496void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
497 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000498 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
499 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
500 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
501 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000502}
John McCallfc93cf92009-10-22 22:37:11 +0000503void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000505}
John McCall8f115c62009-10-16 21:56:05 +0000506
Chris Lattner19cea4e2009-04-22 05:57:30 +0000507//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000508// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000509//===----------------------------------------------------------------------===//
510
Chris Lattner28fa4e62009-04-26 22:26:21 +0000511static void EmitBlockID(unsigned ID, const char *Name,
512 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000513 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000514 Record.clear();
515 Record.push_back(ID);
516 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
517
518 // Emit the block name if present.
519 if (Name == 0 || Name[0] == 0) return;
520 Record.clear();
521 while (*Name)
522 Record.push_back(*Name++);
523 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
524}
525
526static void EmitRecordID(unsigned ID, const char *Name,
527 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000528 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000529 Record.clear();
530 Record.push_back(ID);
531 while (*Name)
532 Record.push_back(*Name++);
533 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000534}
535
536static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000537 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000538#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000539 RECORD(STMT_STOP);
540 RECORD(STMT_NULL_PTR);
541 RECORD(STMT_NULL);
542 RECORD(STMT_COMPOUND);
543 RECORD(STMT_CASE);
544 RECORD(STMT_DEFAULT);
545 RECORD(STMT_LABEL);
546 RECORD(STMT_IF);
547 RECORD(STMT_SWITCH);
548 RECORD(STMT_WHILE);
549 RECORD(STMT_DO);
550 RECORD(STMT_FOR);
551 RECORD(STMT_GOTO);
552 RECORD(STMT_INDIRECT_GOTO);
553 RECORD(STMT_CONTINUE);
554 RECORD(STMT_BREAK);
555 RECORD(STMT_RETURN);
556 RECORD(STMT_DECL);
557 RECORD(STMT_ASM);
558 RECORD(EXPR_PREDEFINED);
559 RECORD(EXPR_DECL_REF);
560 RECORD(EXPR_INTEGER_LITERAL);
561 RECORD(EXPR_FLOATING_LITERAL);
562 RECORD(EXPR_IMAGINARY_LITERAL);
563 RECORD(EXPR_STRING_LITERAL);
564 RECORD(EXPR_CHARACTER_LITERAL);
565 RECORD(EXPR_PAREN);
566 RECORD(EXPR_UNARY_OPERATOR);
567 RECORD(EXPR_SIZEOF_ALIGN_OF);
568 RECORD(EXPR_ARRAY_SUBSCRIPT);
569 RECORD(EXPR_CALL);
570 RECORD(EXPR_MEMBER);
571 RECORD(EXPR_BINARY_OPERATOR);
572 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
573 RECORD(EXPR_CONDITIONAL_OPERATOR);
574 RECORD(EXPR_IMPLICIT_CAST);
575 RECORD(EXPR_CSTYLE_CAST);
576 RECORD(EXPR_COMPOUND_LITERAL);
577 RECORD(EXPR_EXT_VECTOR_ELEMENT);
578 RECORD(EXPR_INIT_LIST);
579 RECORD(EXPR_DESIGNATED_INIT);
580 RECORD(EXPR_IMPLICIT_VALUE_INIT);
581 RECORD(EXPR_VA_ARG);
582 RECORD(EXPR_ADDR_LABEL);
583 RECORD(EXPR_STMT);
584 RECORD(EXPR_TYPES_COMPATIBLE);
585 RECORD(EXPR_CHOOSE);
586 RECORD(EXPR_GNU_NULL);
587 RECORD(EXPR_SHUFFLE_VECTOR);
588 RECORD(EXPR_BLOCK);
589 RECORD(EXPR_BLOCK_DECL_REF);
590 RECORD(EXPR_OBJC_STRING_LITERAL);
591 RECORD(EXPR_OBJC_ENCODE);
592 RECORD(EXPR_OBJC_SELECTOR_EXPR);
593 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
594 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
595 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
596 RECORD(EXPR_OBJC_KVC_REF_EXPR);
597 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000598 RECORD(STMT_OBJC_FOR_COLLECTION);
599 RECORD(STMT_OBJC_CATCH);
600 RECORD(STMT_OBJC_FINALLY);
601 RECORD(STMT_OBJC_AT_TRY);
602 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
603 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000604 RECORD(EXPR_CXX_OPERATOR_CALL);
605 RECORD(EXPR_CXX_CONSTRUCT);
606 RECORD(EXPR_CXX_STATIC_CAST);
607 RECORD(EXPR_CXX_DYNAMIC_CAST);
608 RECORD(EXPR_CXX_REINTERPRET_CAST);
609 RECORD(EXPR_CXX_CONST_CAST);
610 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
611 RECORD(EXPR_CXX_BOOL_LITERAL);
612 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000613#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000614}
Mike Stump11289f42009-09-09 15:08:12 +0000615
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000616void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000617 RecordData Record;
618 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000619
Sebastian Redl539c5062010-08-18 23:57:32 +0000620#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
621#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000622
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000623 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000624 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000625 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000626 RECORD(TYPE_OFFSET);
627 RECORD(DECL_OFFSET);
628 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000629 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000630 RECORD(IDENTIFIER_OFFSET);
631 RECORD(IDENTIFIER_TABLE);
632 RECORD(EXTERNAL_DEFINITIONS);
633 RECORD(SPECIAL_TYPES);
634 RECORD(STATISTICS);
635 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000636 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000637 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
638 RECORD(SELECTOR_OFFSETS);
639 RECORD(METHOD_POOL);
640 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000641 RECORD(SOURCE_LOCATION_OFFSETS);
642 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000643 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000644 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000645 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000646 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000647 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000648 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000649
Chris Lattner28fa4e62009-04-26 22:26:21 +0000650 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000651 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000652 RECORD(SM_SLOC_FILE_ENTRY);
653 RECORD(SM_SLOC_BUFFER_ENTRY);
654 RECORD(SM_SLOC_BUFFER_BLOB);
655 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
656 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000657
Chris Lattner28fa4e62009-04-26 22:26:21 +0000658 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000659 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000660 RECORD(PP_MACRO_OBJECT_LIKE);
661 RECORD(PP_MACRO_FUNCTION_LIKE);
662 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000663 RECORD(PP_MACRO_INSTANTIATION);
664 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000665
Douglas Gregor12bfa382009-10-17 00:13:19 +0000666 // Decls and Types block.
667 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000668 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000669 RECORD(TYPE_COMPLEX);
670 RECORD(TYPE_POINTER);
671 RECORD(TYPE_BLOCK_POINTER);
672 RECORD(TYPE_LVALUE_REFERENCE);
673 RECORD(TYPE_RVALUE_REFERENCE);
674 RECORD(TYPE_MEMBER_POINTER);
675 RECORD(TYPE_CONSTANT_ARRAY);
676 RECORD(TYPE_INCOMPLETE_ARRAY);
677 RECORD(TYPE_VARIABLE_ARRAY);
678 RECORD(TYPE_VECTOR);
679 RECORD(TYPE_EXT_VECTOR);
680 RECORD(TYPE_FUNCTION_PROTO);
681 RECORD(TYPE_FUNCTION_NO_PROTO);
682 RECORD(TYPE_TYPEDEF);
683 RECORD(TYPE_TYPEOF_EXPR);
684 RECORD(TYPE_TYPEOF);
685 RECORD(TYPE_RECORD);
686 RECORD(TYPE_ENUM);
687 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000688 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000689 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000690 RECORD(DECL_TRANSLATION_UNIT);
691 RECORD(DECL_TYPEDEF);
692 RECORD(DECL_ENUM);
693 RECORD(DECL_RECORD);
694 RECORD(DECL_ENUM_CONSTANT);
695 RECORD(DECL_FUNCTION);
696 RECORD(DECL_OBJC_METHOD);
697 RECORD(DECL_OBJC_INTERFACE);
698 RECORD(DECL_OBJC_PROTOCOL);
699 RECORD(DECL_OBJC_IVAR);
700 RECORD(DECL_OBJC_AT_DEFS_FIELD);
701 RECORD(DECL_OBJC_CLASS);
702 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
703 RECORD(DECL_OBJC_CATEGORY);
704 RECORD(DECL_OBJC_CATEGORY_IMPL);
705 RECORD(DECL_OBJC_IMPLEMENTATION);
706 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
707 RECORD(DECL_OBJC_PROPERTY);
708 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000709 RECORD(DECL_FIELD);
710 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000711 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000712 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000713 RECORD(DECL_FILE_SCOPE_ASM);
714 RECORD(DECL_BLOCK);
715 RECORD(DECL_CONTEXT_LEXICAL);
716 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000717 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000718 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000719#undef RECORD
720#undef BLOCK
721 Stream.ExitBlock();
722}
723
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000724/// \brief Adjusts the given filename to only write out the portion of the
725/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000726///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000727/// \param Filename the file name to adjust.
728///
729/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
730/// the returned filename will be adjusted by this system root.
731///
732/// \returns either the original filename (if it needs no adjustment) or the
733/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000734static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000735adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
736 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000737
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000738 if (!isysroot)
739 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000740
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000741 // Verify that the filename and the system root have the same prefix.
742 unsigned Pos = 0;
743 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
744 if (Filename[Pos] != isysroot[Pos])
745 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000746
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000747 // We hit the end of the filename before we hit the end of the system root.
748 if (!Filename[Pos])
749 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000750
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000751 // If the file name has a '/' at the current position, skip over the '/'.
752 // We distinguish sysroot-based includes from absolute includes by the
753 // absence of '/' at the beginning of sysroot-based includes.
754 if (Filename[Pos] == '/')
755 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000756
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000757 return Filename + Pos;
758}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000759
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000760/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000761void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000762 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000763
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000764 // Metadata
765 const TargetInfo &Target = Context.Target;
766 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000767 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000768 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000771 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
772 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
773 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000774 // Target triple or chained PCH name
775 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000776 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000777
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000778 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000779 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
780 Record.push_back(VERSION_MAJOR);
781 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000782 Record.push_back(CLANG_VERSION_MAJOR);
783 Record.push_back(CLANG_VERSION_MINOR);
784 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000785 // FIXME: This writes the absolute path for chained headers.
786 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
787 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000788
Douglas Gregor45fe0362009-05-12 01:31:05 +0000789 // Original file name
790 SourceManager &SM = Context.getSourceManager();
791 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
792 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000793 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000794 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
795 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
796
797 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000798
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000799 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000800
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000801 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000802 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000803 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000804 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000805 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000806 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000807 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000808
Ted Kremenek18e066f2010-01-22 22:12:47 +0000809 // Repository branch/version information.
810 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000811 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000812 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
813 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000814 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000815 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000816 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
817 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000818}
819
820/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000821void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000822 RecordData Record;
823 Record.push_back(LangOpts.Trigraphs);
824 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
825 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
826 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
827 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000828 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000829 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
830 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
831 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
832 Record.push_back(LangOpts.C99); // C99 Support
833 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000834 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
835 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000836 Record.push_back(LangOpts.CPlusPlus); // C++ Support
837 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000838 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000839
Douglas Gregor55abb232009-04-10 20:39:37 +0000840 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
841 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000842 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000843 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000844 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000845 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000846 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000847
Douglas Gregor55abb232009-04-10 20:39:37 +0000848 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000849 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
850 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000851 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000852 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000853 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000854
855 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
856 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
857 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
858
Chris Lattner258172e2009-04-27 07:35:58 +0000859 // Whether static initializers are protected by locks.
860 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000861 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000862 Record.push_back(LangOpts.Blocks); // block extension to C
863 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
864 // they are unused.
865 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
866 // (modulo the platform support).
867
Chris Lattner51924e512010-06-26 21:25:03 +0000868 Record.push_back(LangOpts.getSignedOverflowBehavior());
869 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000870
871 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000872 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000873 // defined.
874 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
875 // opposed to __DYNAMIC__).
876 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
877
878 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
879 // used (instead of C99 semantics).
880 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000881 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
882 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000883 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
884 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000885 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000886 Record.push_back(LangOpts.getGCMode());
887 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000888 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000889 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000890 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000891 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +0000892 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000893 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000894 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000895 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000896}
897
Douglas Gregora7f71a92009-04-10 03:52:48 +0000898//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000899// stat cache Serialization
900//===----------------------------------------------------------------------===//
901
902namespace {
903// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000904class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000905public:
906 typedef const char * key_type;
907 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000908
Chris Lattner2a6fa472010-11-23 19:28:12 +0000909 typedef struct stat data_type;
910 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000911
912 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000913 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000914 }
Mike Stump11289f42009-09-09 15:08:12 +0000915
916 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000917 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
918 data_type_ref Data) {
919 unsigned StrLen = strlen(path);
920 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000921 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000922 clang::io::Emit8(Out, DataLen);
923 return std::make_pair(StrLen + 1, DataLen);
924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Douglas Gregorc5046832009-04-27 18:38:38 +0000926 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
927 Out.write(path, KeyLen);
928 }
Mike Stump11289f42009-09-09 15:08:12 +0000929
Chris Lattner2a6fa472010-11-23 19:28:12 +0000930 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000931 data_type_ref Data, unsigned DataLen) {
932 using namespace clang::io;
933 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000934
Chris Lattner2a6fa472010-11-23 19:28:12 +0000935 Emit32(Out, (uint32_t) Data.st_ino);
936 Emit32(Out, (uint32_t) Data.st_dev);
937 Emit16(Out, (uint16_t) Data.st_mode);
938 Emit64(Out, (uint64_t) Data.st_mtime);
939 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +0000940
941 assert(Out.tell() - Start == DataLen && "Wrong data length");
942 }
943};
944} // end anonymous namespace
945
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000946/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000947void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000948 // Build the on-disk hash table containing information about every
949 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000950 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000951 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000952 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000953 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000954 Stat != StatEnd; ++Stat, ++NumStatEntries) {
955 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000956 Generator.insert(Filename, Stat->second);
957 }
Mike Stump11289f42009-09-09 15:08:12 +0000958
Douglas Gregorc5046832009-04-27 18:38:38 +0000959 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000960 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000961 uint32_t BucketOffset;
962 {
963 llvm::raw_svector_ostream Out(StatCacheData);
964 // Make sure that no bucket is at offset 0
965 clang::io::Emit32(Out, 0);
966 BucketOffset = Generator.Emit(Out);
967 }
968
969 // Create a blob abbreviation
970 using namespace llvm;
971 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000972 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
976 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
977
978 // Write the stat cache
979 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000980 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +0000981 Record.push_back(BucketOffset);
982 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000983 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000984}
985
986//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000987// Source Manager Serialization
988//===----------------------------------------------------------------------===//
989
990/// \brief Create an abbreviation for the SLocEntry that refers to a
991/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000992static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000993 using namespace llvm;
994 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000995 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +0000996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001000 // FileEntry fields.
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001003 // HeaderFileInfo fields.
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001009 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001010}
1011
1012/// \brief Create an abbreviation for the SLocEntry that refers to a
1013/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001014static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001015 using namespace llvm;
1016 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001017 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001023 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001024}
1025
1026/// \brief Create an abbreviation for the SLocEntry that refers to a
1027/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001028static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001029 using namespace llvm;
1030 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001031 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001033 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001034}
1035
1036/// \brief Create an abbreviation for the SLocEntry that refers to an
1037/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001038static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001039 using namespace llvm;
1040 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001041 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001047 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001048}
1049
1050/// \brief Writes the block containing the serialized form of the
1051/// source manager.
1052///
1053/// TODO: We should probably use an on-disk hash table (stored in a
1054/// blob), indexed based on the file name, so that we only create
1055/// entries for files that we actually need. In the common case (no
1056/// errors), we probably won't have to create file entries for any of
1057/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001058void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001059 const Preprocessor &PP,
1060 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001061 RecordData Record;
1062
Chris Lattner0910e3b2009-04-10 17:16:57 +00001063 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001064 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001065
1066 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001067 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1068 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1069 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1070 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001071
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001072 // Write the line table.
1073 if (SourceMgr.hasLineTable()) {
1074 LineTableInfo &LineTable = SourceMgr.getLineTable();
1075
1076 // Emit the file names
1077 Record.push_back(LineTable.getNumFilenames());
1078 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1079 // Emit the file name
1080 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001081 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001082 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1083 Record.push_back(FilenameLen);
1084 if (FilenameLen)
1085 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001088 // Emit the line entries
1089 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1090 L != LEnd; ++L) {
1091 // Emit the file ID
1092 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001093
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001094 // Emit the line entries
1095 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001096 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001097 LEEnd = L->second.end();
1098 LE != LEEnd; ++LE) {
1099 Record.push_back(LE->FileOffset);
1100 Record.push_back(LE->LineNo);
1101 Record.push_back(LE->FilenameID);
1102 Record.push_back((unsigned)LE->FileKind);
1103 Record.push_back(LE->IncludeOffset);
1104 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001105 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001106 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001107 }
1108
Douglas Gregor258ae542009-04-27 06:38:32 +00001109 // Write out the source location entry table. We skip the first
1110 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001111 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001112 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001113 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1114 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1115 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1116 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001117 // Get this source location entry.
1118 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001119
Douglas Gregor258ae542009-04-27 06:38:32 +00001120 // Record the offset of this source-location entry.
1121 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1122
1123 // Figure out which record code to use.
1124 unsigned Code;
1125 if (SLoc->isFile()) {
1126 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001127 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001128 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001129 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001130 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001131 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001132 Record.clear();
1133 Record.push_back(Code);
1134
1135 Record.push_back(SLoc->getOffset());
1136 if (SLoc->isFile()) {
1137 const SrcMgr::FileInfo &File = SLoc->getFile();
1138 Record.push_back(File.getIncludeLoc().getRawEncoding());
1139 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1140 Record.push_back(File.hasLineDirectives());
1141
1142 const SrcMgr::ContentCache *Content = File.getContentCache();
1143 if (Content->Entry) {
1144 // The source location entry is a file. The blob associated
1145 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001146
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001147 // Emit size/modification time for this file.
1148 Record.push_back(Content->Entry->getSize());
1149 Record.push_back(Content->Entry->getModificationTime());
1150
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001151 // Emit header-search information associated with this file.
1152 HeaderFileInfo HFI;
1153 HeaderSearch &HS = PP.getHeaderSearchInfo();
1154 if (Content->Entry->getUID() < HS.header_file_size())
1155 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1156 Record.push_back(HFI.isImport);
1157 Record.push_back(HFI.DirInfo);
1158 Record.push_back(HFI.NumIncludes);
1159 AddIdentifierRef(HFI.ControllingMacro, Record);
1160
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001161 // Turn the file name into an absolute path, if it isn't already.
1162 const char *Filename = Content->Entry->getName();
1163 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001164 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001165 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001166
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001167 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001168 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001169
1170 // FIXME: For now, preload all file source locations, so that
1171 // we get the appropriate File entries in the reader. This is
1172 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001173 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001174 } else {
1175 // The source location entry is a buffer. The blob associated
1176 // with this entry contains the contents of the buffer.
1177
1178 // We add one to the size so that we capture the trailing NULL
1179 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1180 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001181 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001182 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001183 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001184 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1185 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001186 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001187 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001188 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001189 llvm::StringRef(Buffer->getBufferStart(),
1190 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001191
1192 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001193 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001194 }
1195 } else {
1196 // The source location entry is an instantiation.
1197 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1198 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1199 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1200 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1201
1202 // Compute the token length for this macro expansion.
1203 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001204 if (I + 1 != N)
1205 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001206 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1207 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1208 }
1209 }
1210
Douglas Gregor8f45df52009-04-16 22:23:12 +00001211 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001212
1213 if (SLocEntryOffsets.empty())
1214 return;
1215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001216 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001217 // table is used for lazily loading source-location information.
1218 using namespace llvm;
1219 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001220 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1224 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001225
Douglas Gregor258ae542009-04-27 06:38:32 +00001226 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001227 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001228 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001229 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1230 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001231 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001232 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001233 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001234
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001235 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001236 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001237 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001238}
1239
Douglas Gregorc5046832009-04-27 18:38:38 +00001240//===----------------------------------------------------------------------===//
1241// Preprocessor Serialization
1242//===----------------------------------------------------------------------===//
1243
Chris Lattnereeffaef2009-04-10 17:15:23 +00001244/// \brief Writes the block containing the serialized form of the
1245/// preprocessor.
1246///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001247void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001248 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001249
Chris Lattner0af3ba12009-04-13 01:29:17 +00001250 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1251 if (PP.getCounterValue() != 0) {
1252 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001253 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001254 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001255 }
1256
1257 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001258 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001260 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001261 // FIXME: use diagnostics subsystem for localization etc.
1262 if (PP.SawDateOrTime())
1263 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001264
Douglas Gregor796d76a2010-10-20 22:00:55 +00001265
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001266 // Loop over all the macro definitions that are live at the end of the file,
1267 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001268 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001269 unsigned InclusionAbbrev = 0;
1270 if (PPRec) {
1271 using namespace llvm;
1272 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1273 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001281 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001282 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001283
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001284 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1285 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001286 // FIXME: This emits macros in hash table order, we should do it in a stable
1287 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001288 MacroInfo *MI = I->second;
1289
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001290 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001291 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001292 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001293
1294 // FIXME: There is a (probably minor) optimization we could do here, if
1295 // the macro comes from the original PCH but the identifier comes from a
1296 // chained PCH, by storing the offset into the original PCH rather than
1297 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001298 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001299 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001300 continue;
1301
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001302 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001303 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001304 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1305 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001306
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001307 unsigned Code;
1308 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001309 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001310 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001311 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001312
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001313 Record.push_back(MI->isC99Varargs());
1314 Record.push_back(MI->isGNUVarargs());
1315 Record.push_back(MI->getNumArgs());
1316 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1317 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001318 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001319 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001320
Douglas Gregoraae92242010-03-19 21:51:54 +00001321 // If we have a detailed preprocessing record, record the macro definition
1322 // ID that corresponds to this macro.
1323 if (PPRec)
1324 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001325
Douglas Gregor8f45df52009-04-16 22:23:12 +00001326 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001327 Record.clear();
1328
Chris Lattner2199f5b2009-04-10 18:08:30 +00001329 // Emit the tokens array.
1330 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1331 // Note that we know that the preprocessor does not have any annotation
1332 // tokens in it because they are created by the parser, and thus can't be
1333 // in a macro definition.
1334 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001335
Chris Lattner2199f5b2009-04-10 18:08:30 +00001336 Record.push_back(Tok.getLocation().getRawEncoding());
1337 Record.push_back(Tok.getLength());
1338
Chris Lattner2199f5b2009-04-10 18:08:30 +00001339 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1340 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001341 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001342
Chris Lattner2199f5b2009-04-10 18:08:30 +00001343 // FIXME: Should translate token kind to a stable encoding.
1344 Record.push_back(Tok.getKind());
1345 // FIXME: Should translate token flags to a stable encoding.
1346 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001347
Sebastian Redl539c5062010-08-18 23:57:32 +00001348 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001349 Record.clear();
1350 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001351 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001352 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001353
Douglas Gregoraae92242010-03-19 21:51:54 +00001354 // If the preprocessor has a preprocessing record, emit it.
1355 unsigned NumPreprocessingRecords = 0;
1356 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001357 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001358 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1359 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001360 E != EEnd; ++E) {
1361 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001362
Douglas Gregoraae92242010-03-19 21:51:54 +00001363 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1364 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001365 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001366
Douglas Gregor91096292010-10-02 19:29:26 +00001367 // Don't write the macro definition if it is from another AST file.
1368 if (ID < FirstMacroID)
1369 continue;
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001370
1371 // Notify the serialization listener that we're serializing this entity.
1372 if (SerializationListener)
1373 SerializationListener->SerializedPreprocessedEntity(*E,
1374 Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001375
Douglas Gregor91096292010-10-02 19:29:26 +00001376 unsigned Position = ID - FirstMacroID;
1377 if (Position != MacroDefinitionOffsets.size()) {
1378 if (Position > MacroDefinitionOffsets.size())
1379 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001380
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001381 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001382 } else
1383 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001384
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001385 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001386 Record.push_back(ID);
1387 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1388 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1389 AddIdentifierRef(MD->getName(), Record);
1390 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001391 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001392 continue;
1393 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001394
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001395 // Notify the serialization listener that we're serializing this entity.
1396 if (SerializationListener)
1397 SerializationListener->SerializedPreprocessedEntity(*E,
1398 Stream.GetCurrentBitNo());
1399
1400 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1401 Record.push_back(IndexBase + NumPreprocessingRecords++);
1402 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1403 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1404 AddIdentifierRef(MI->getName(), Record);
1405 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1406 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1407 continue;
1408 }
1409
Douglas Gregor796d76a2010-10-20 22:00:55 +00001410 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1411 Record.push_back(PP_INCLUSION_DIRECTIVE);
1412 Record.push_back(IndexBase + NumPreprocessingRecords++);
1413 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1414 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1415 Record.push_back(ID->getFileName().size());
1416 Record.push_back(ID->wasInQuotes());
1417 Record.push_back(static_cast<unsigned>(ID->getKind()));
1418 llvm::SmallString<64> Buffer;
1419 Buffer += ID->getFileName();
1420 Buffer += ID->getFile()->getName();
1421 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1422 continue;
1423 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001424
1425 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregoraae92242010-03-19 21:51:54 +00001426 }
1427 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001428
Douglas Gregor8f45df52009-04-16 22:23:12 +00001429 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001430
Douglas Gregoraae92242010-03-19 21:51:54 +00001431 // Write the offsets table for the preprocessing record.
1432 if (NumPreprocessingRecords > 0) {
1433 // Write the offsets table for identifier IDs.
1434 using namespace llvm;
1435 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001436 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1440 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001441
Douglas Gregoraae92242010-03-19 21:51:54 +00001442 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001443 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001444 Record.push_back(NumPreprocessingRecords);
1445 Record.push_back(MacroDefinitionOffsets.size());
1446 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001447 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001448 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1449 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001450}
1451
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001452void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1453 RecordData Record;
1454 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
1455 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i);
1456 if (Map & 0x8) { // user mapping.
1457 Record.push_back(i);
1458 Record.push_back(Map & 0x7);
1459 }
1460 }
1461
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001462 if (!Record.empty())
1463 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001464}
1465
Douglas Gregorc5046832009-04-27 18:38:38 +00001466//===----------------------------------------------------------------------===//
1467// Type Serialization
1468//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001469
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001470/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001471void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001472 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001473 if (Idx.getIndex() == 0) // we haven't seen this type before.
1474 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001475
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001476 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001477
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001478 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001479 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001480 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001481 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001482 else if (TypeOffsets.size() < Index) {
1483 TypeOffsets.resize(Index + 1);
1484 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001485 }
1486
1487 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001488
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001489 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001490 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001491
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001492 if (T.hasLocalNonFastQualifiers()) {
1493 Qualifiers Qs = T.getLocalQualifiers();
1494 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001495 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001496 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001497 } else {
1498 switch (T->getTypeClass()) {
1499 // For all of the concrete, non-dependent types, call the
1500 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001501#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001502 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001503#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001504#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001505 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001506 }
1507
1508 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001509 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001510
1511 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001512 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001513}
1514
Douglas Gregorc5046832009-04-27 18:38:38 +00001515//===----------------------------------------------------------------------===//
1516// Declaration Serialization
1517//===----------------------------------------------------------------------===//
1518
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001519/// \brief Write the block containing all of the declaration IDs
1520/// lexically declared within the given DeclContext.
1521///
1522/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1523/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001524uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001525 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001526 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001527 return 0;
1528
Douglas Gregor8f45df52009-04-16 22:23:12 +00001529 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001530 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001531 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001532 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001533 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1534 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001535 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001536
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001537 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001538 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001539 reinterpret_cast<char*>(Decls.data()),
1540 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001541 return Offset;
1542}
1543
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001544void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001545 using namespace llvm;
1546 RecordData Record;
1547
1548 // Write the type offsets array
1549 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001550 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1553 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1554 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001555 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001556 Record.push_back(TypeOffsets.size());
1557 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001558 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001559 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1560
1561 // Write the declaration offsets array
1562 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001563 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1566 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1567 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001568 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001569 Record.push_back(DeclOffsets.size());
1570 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001571 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001572 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1573}
1574
Douglas Gregorc5046832009-04-27 18:38:38 +00001575//===----------------------------------------------------------------------===//
1576// Global Method Pool and Selector Serialization
1577//===----------------------------------------------------------------------===//
1578
Douglas Gregore84a9da2009-04-20 20:36:09 +00001579namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001580// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001581class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001582 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001583
1584public:
1585 typedef Selector key_type;
1586 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001587
Sebastian Redl834bb972010-08-04 17:20:04 +00001588 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001589 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001590 ObjCMethodList Instance, Factory;
1591 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001592 typedef const data_type& data_type_ref;
1593
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001594 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001595
Douglas Gregorc78d3462009-04-24 21:10:55 +00001596 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001597 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001598 }
Mike Stump11289f42009-09-09 15:08:12 +00001599
1600 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001601 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1602 data_type_ref Methods) {
1603 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1604 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001605 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1606 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001607 Method = Method->Next)
1608 if (Method->Method)
1609 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001610 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001611 Method = Method->Next)
1612 if (Method->Method)
1613 DataLen += 4;
1614 clang::io::Emit16(Out, DataLen);
1615 return std::make_pair(KeyLen, DataLen);
1616 }
Mike Stump11289f42009-09-09 15:08:12 +00001617
Douglas Gregor95c13f52009-04-25 17:48:32 +00001618 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001619 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001620 assert((Start >> 32) == 0 && "Selector key offset too large");
1621 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001622 unsigned N = Sel.getNumArgs();
1623 clang::io::Emit16(Out, N);
1624 if (N == 0)
1625 N = 1;
1626 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001627 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001628 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1629 }
Mike Stump11289f42009-09-09 15:08:12 +00001630
Douglas Gregorc78d3462009-04-24 21:10:55 +00001631 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001632 data_type_ref Methods, unsigned DataLen) {
1633 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001634 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001635 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001636 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001637 Method = Method->Next)
1638 if (Method->Method)
1639 ++NumInstanceMethods;
1640
1641 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001642 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001643 Method = Method->Next)
1644 if (Method->Method)
1645 ++NumFactoryMethods;
1646
1647 clang::io::Emit16(Out, NumInstanceMethods);
1648 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001649 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001650 Method = Method->Next)
1651 if (Method->Method)
1652 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001653 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001654 Method = Method->Next)
1655 if (Method->Method)
1656 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001657
1658 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001659 }
1660};
1661} // end anonymous namespace
1662
Sebastian Redla19a67f2010-08-03 21:58:15 +00001663/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001664///
1665/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001666/// in an on-disk hash table indexed by the selector. The hash table also
1667/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001668void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001669 using namespace llvm;
1670
Sebastian Redla19a67f2010-08-03 21:58:15 +00001671 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001672 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001673 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001674 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001675 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001676 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001677 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001678 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001679
Sebastian Redla19a67f2010-08-03 21:58:15 +00001680 // Create the on-disk hash table representation. We walk through every
1681 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001682 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001683 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001684 I = SelectorIDs.begin(), E = SelectorIDs.end();
1685 I != E; ++I) {
1686 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001687 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001688 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001689 I->second,
1690 ObjCMethodList(),
1691 ObjCMethodList()
1692 };
1693 if (F != SemaRef.MethodPool.end()) {
1694 Data.Instance = F->second.first;
1695 Data.Factory = F->second.second;
1696 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001697 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001698 // changed.
1699 if (Chain && I->second < FirstSelectorID) {
1700 // Selector already exists. Did it change?
1701 bool changed = false;
1702 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1703 M = M->Next) {
1704 if (M->Method->getPCHLevel() == 0)
1705 changed = true;
1706 }
1707 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1708 M = M->Next) {
1709 if (M->Method->getPCHLevel() == 0)
1710 changed = true;
1711 }
1712 if (!changed)
1713 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001714 } else if (Data.Instance.Method || Data.Factory.Method) {
1715 // A new method pool entry.
1716 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001717 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001718 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001719 }
1720
Douglas Gregorc78d3462009-04-24 21:10:55 +00001721 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001722 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001723 uint32_t BucketOffset;
1724 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001725 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001726 llvm::raw_svector_ostream Out(MethodPool);
1727 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001728 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001729 BucketOffset = Generator.Emit(Out, Trait);
1730 }
1731
1732 // Create a blob abbreviation
1733 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001734 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1738 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1739
Douglas Gregor95c13f52009-04-25 17:48:32 +00001740 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001741 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001742 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001743 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001744 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001745 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001746
1747 // Create a blob abbreviation for the selector table offsets.
1748 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001749 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1752 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1753
1754 // Write the selector offsets table.
1755 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001756 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001757 Record.push_back(SelectorOffsets.size());
1758 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001759 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001760 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001761 }
1762}
1763
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001764/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001765void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001766 using namespace llvm;
1767 if (SemaRef.ReferencedSelectors.empty())
1768 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001769
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001770 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001771
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001772 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001773 // very tricky to fix, and given that @selector shouldn't really appear in
1774 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001775 for (DenseMap<Selector, SourceLocation>::iterator S =
1776 SemaRef.ReferencedSelectors.begin(),
1777 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1778 Selector Sel = (*S).first;
1779 SourceLocation Loc = (*S).second;
1780 AddSelectorRef(Sel, Record);
1781 AddSourceLocation(Loc, Record);
1782 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001783 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001784}
1785
Douglas Gregorc5046832009-04-27 18:38:38 +00001786//===----------------------------------------------------------------------===//
1787// Identifier Table Serialization
1788//===----------------------------------------------------------------------===//
1789
Douglas Gregorc78d3462009-04-24 21:10:55 +00001790namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001791class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001792 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001793 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001794
Douglas Gregor1d583f22009-04-28 21:18:29 +00001795 /// \brief Determines whether this is an "interesting" identifier
1796 /// that needs a full IdentifierInfo structure written into the hash
1797 /// table.
1798 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1799 return II->isPoisoned() ||
1800 II->isExtensionToken() ||
1801 II->hasMacroDefinition() ||
1802 II->getObjCOrBuiltinID() ||
1803 II->getFETokenInfo<void>();
1804 }
1805
Douglas Gregore84a9da2009-04-20 20:36:09 +00001806public:
1807 typedef const IdentifierInfo* key_type;
1808 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001809
Sebastian Redl539c5062010-08-18 23:57:32 +00001810 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001811 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001812
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001813 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001814 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001815
1816 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001817 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001818 }
Mike Stump11289f42009-09-09 15:08:12 +00001819
1820 std::pair<unsigned,unsigned>
1821 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001822 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001823 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001824 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1825 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001826 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001827 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001828 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001829 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001830 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1831 DEnd = IdentifierResolver::end();
1832 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001833 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001834 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001835 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001836 // We emit the key length after the data length so that every
1837 // string is preceded by a 16-bit length. This matches the PTH
1838 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001839 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001840 return std::make_pair(KeyLen, DataLen);
1841 }
Mike Stump11289f42009-09-09 15:08:12 +00001842
1843 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001844 unsigned KeyLen) {
1845 // Record the location of the key data. This is used when generating
1846 // the mapping from persistent IDs to strings.
1847 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001848 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001849 }
Mike Stump11289f42009-09-09 15:08:12 +00001850
1851 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001852 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001853 if (!isInterestingIdentifier(II)) {
1854 clang::io::Emit32(Out, ID << 1);
1855 return;
1856 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001857
Douglas Gregor1d583f22009-04-28 21:18:29 +00001858 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001859 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001860 bool hasMacroDefinition =
1861 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001862 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001863 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001864 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1865 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1866 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001867 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001868 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001869 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001870
Douglas Gregorc3366a52009-04-21 23:56:24 +00001871 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001872 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001873
Douglas Gregora868bbd2009-04-21 22:25:48 +00001874 // Emit the declaration IDs in reverse order, because the
1875 // IdentifierResolver provides the declarations as they would be
1876 // visible (e.g., the function "stat" would come before the struct
1877 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1878 // adds declarations to the end of the list (so we need to see the
1879 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001880 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001881 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001882 IdentifierResolver::end());
1883 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1884 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001885 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001886 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001887 }
1888};
1889} // end anonymous namespace
1890
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001891/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001892///
1893/// The identifier table consists of a blob containing string data
1894/// (the actual identifiers themselves) and a separate "offsets" index
1895/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001896void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001897 using namespace llvm;
1898
1899 // Create and write out the blob that contains the identifier
1900 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001901 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001902 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001903 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001904
Douglas Gregore6648fb2009-04-28 20:33:11 +00001905 // Look for any identifiers that were named while processing the
1906 // headers, but are otherwise not needed. We add these to the hash
1907 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001908 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001909 // file.
1910 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1911 IDEnd = PP.getIdentifierTable().end();
1912 ID != IDEnd; ++ID)
1913 getIdentifierRef(ID->second);
1914
Sebastian Redlff4a2952010-07-23 23:49:55 +00001915 // Create the on-disk hash table representation. We only store offsets
1916 // for identifiers that appear here for the first time.
1917 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001918 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001919 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1920 ID != IDEnd; ++ID) {
1921 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001922 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001923 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001924 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001925
Douglas Gregore84a9da2009-04-20 20:36:09 +00001926 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001927 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001928 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001929 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001930 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001931 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001932 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001933 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001934 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001935 }
1936
1937 // Create a blob abbreviation
1938 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001939 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001942 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001943
1944 // Write the identifier table
1945 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001946 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001947 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001948 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001949 }
1950
1951 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001952 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001953 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1956 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1957
1958 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001959 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001960 Record.push_back(IdentifierOffsets.size());
1961 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001962 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001963 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001964}
1965
Douglas Gregorc5046832009-04-27 18:38:38 +00001966//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001967// DeclContext's Name Lookup Table Serialization
1968//===----------------------------------------------------------------------===//
1969
1970namespace {
1971// Trait used for the on-disk hash table used in the method pool.
1972class ASTDeclContextNameLookupTrait {
1973 ASTWriter &Writer;
1974
1975public:
1976 typedef DeclarationName key_type;
1977 typedef key_type key_type_ref;
1978
1979 typedef DeclContext::lookup_result data_type;
1980 typedef const data_type& data_type_ref;
1981
1982 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1983
1984 unsigned ComputeHash(DeclarationName Name) {
1985 llvm::FoldingSetNodeID ID;
1986 ID.AddInteger(Name.getNameKind());
1987
1988 switch (Name.getNameKind()) {
1989 case DeclarationName::Identifier:
1990 ID.AddString(Name.getAsIdentifierInfo()->getName());
1991 break;
1992 case DeclarationName::ObjCZeroArgSelector:
1993 case DeclarationName::ObjCOneArgSelector:
1994 case DeclarationName::ObjCMultiArgSelector:
1995 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1996 break;
1997 case DeclarationName::CXXConstructorName:
1998 case DeclarationName::CXXDestructorName:
1999 case DeclarationName::CXXConversionFunctionName:
2000 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2001 break;
2002 case DeclarationName::CXXOperatorName:
2003 ID.AddInteger(Name.getCXXOverloadedOperator());
2004 break;
2005 case DeclarationName::CXXLiteralOperatorName:
2006 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2007 case DeclarationName::CXXUsingDirective:
2008 break;
2009 }
2010
2011 return ID.ComputeHash();
2012 }
2013
2014 std::pair<unsigned,unsigned>
2015 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2016 data_type_ref Lookup) {
2017 unsigned KeyLen = 1;
2018 switch (Name.getNameKind()) {
2019 case DeclarationName::Identifier:
2020 case DeclarationName::ObjCZeroArgSelector:
2021 case DeclarationName::ObjCOneArgSelector:
2022 case DeclarationName::ObjCMultiArgSelector:
2023 case DeclarationName::CXXConstructorName:
2024 case DeclarationName::CXXDestructorName:
2025 case DeclarationName::CXXConversionFunctionName:
2026 case DeclarationName::CXXLiteralOperatorName:
2027 KeyLen += 4;
2028 break;
2029 case DeclarationName::CXXOperatorName:
2030 KeyLen += 1;
2031 break;
2032 case DeclarationName::CXXUsingDirective:
2033 break;
2034 }
2035 clang::io::Emit16(Out, KeyLen);
2036
2037 // 2 bytes for num of decls and 4 for each DeclID.
2038 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2039 clang::io::Emit16(Out, DataLen);
2040
2041 return std::make_pair(KeyLen, DataLen);
2042 }
2043
2044 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2045 using namespace clang::io;
2046
2047 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2048 Emit8(Out, Name.getNameKind());
2049 switch (Name.getNameKind()) {
2050 case DeclarationName::Identifier:
2051 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2052 break;
2053 case DeclarationName::ObjCZeroArgSelector:
2054 case DeclarationName::ObjCOneArgSelector:
2055 case DeclarationName::ObjCMultiArgSelector:
2056 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2057 break;
2058 case DeclarationName::CXXConstructorName:
2059 case DeclarationName::CXXDestructorName:
2060 case DeclarationName::CXXConversionFunctionName:
2061 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2062 break;
2063 case DeclarationName::CXXOperatorName:
2064 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2065 Emit8(Out, Name.getCXXOverloadedOperator());
2066 break;
2067 case DeclarationName::CXXLiteralOperatorName:
2068 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2069 break;
2070 case DeclarationName::CXXUsingDirective:
2071 break;
2072 }
2073 }
2074
2075 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2076 data_type Lookup, unsigned DataLen) {
2077 uint64_t Start = Out.tell(); (void)Start;
2078 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2079 for (; Lookup.first != Lookup.second; ++Lookup.first)
2080 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2081
2082 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2083 }
2084};
2085} // end anonymous namespace
2086
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002087/// \brief Write the block containing all of the declaration IDs
2088/// visible from the given DeclContext.
2089///
2090/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002091/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002092uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2093 DeclContext *DC) {
2094 if (DC->getPrimaryContext() != DC)
2095 return 0;
2096
2097 // Since there is no name lookup into functions or methods, don't bother to
2098 // build a visible-declarations table for these entities.
2099 if (DC->isFunctionOrMethod())
2100 return 0;
2101
2102 // If not in C++, we perform name lookup for the translation unit via the
2103 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2104 // FIXME: In C++ we need the visible declarations in order to "see" the
2105 // friend declarations, is there a way to do this without writing the table ?
2106 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2107 return 0;
2108
2109 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002110 if (DC->hasExternalVisibleStorage())
2111 DC->MaterializeVisibleDeclsFromExternalStorage();
2112 else
2113 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002114
2115 // Serialize the contents of the mapping used for lookup. Note that,
2116 // although we have two very different code paths, the serialized
2117 // representation is the same for both cases: a declaration name,
2118 // followed by a size, followed by references to the visible
2119 // declarations that have that name.
2120 uint64_t Offset = Stream.GetCurrentBitNo();
2121 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2122 if (!Map || Map->empty())
2123 return 0;
2124
2125 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2126 ASTDeclContextNameLookupTrait Trait(*this);
2127
2128 // Create the on-disk hash table representation.
2129 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2130 D != DEnd; ++D) {
2131 DeclarationName Name = D->first;
2132 DeclContext::lookup_result Result = D->second.getLookupResult();
2133 Generator.insert(Name, Result, Trait);
2134 }
2135
2136 // Create the on-disk hash table in a buffer.
2137 llvm::SmallString<4096> LookupTable;
2138 uint32_t BucketOffset;
2139 {
2140 llvm::raw_svector_ostream Out(LookupTable);
2141 // Make sure that no bucket is at offset 0
2142 clang::io::Emit32(Out, 0);
2143 BucketOffset = Generator.Emit(Out, Trait);
2144 }
2145
2146 // Write the lookup table
2147 RecordData Record;
2148 Record.push_back(DECL_CONTEXT_VISIBLE);
2149 Record.push_back(BucketOffset);
2150 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2151 LookupTable.str());
2152
2153 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2154 ++NumVisibleDeclContexts;
2155 return Offset;
2156}
2157
Sebastian Redla4071b42010-08-24 00:50:09 +00002158/// \brief Write an UPDATE_VISIBLE block for the given context.
2159///
2160/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2161/// DeclContext in a dependent AST file. As such, they only exist for the TU
2162/// (in C++) and for namespaces.
2163void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002164 // Make the context build its lookup table, but don't make it load external
2165 // decls.
2166 DC->lookup(DeclarationName());
2167
2168 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2169 if (!Map || Map->empty())
2170 return;
2171
2172 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2173 ASTDeclContextNameLookupTrait Trait(*this);
2174
2175 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002176 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2177 D != DEnd; ++D) {
2178 DeclarationName Name = D->first;
2179 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002180 // For any name that appears in this table, the results are complete, i.e.
2181 // they overwrite results from previous PCHs. Merging is always a mess.
2182 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002183 }
2184
2185 // Create the on-disk hash table in a buffer.
2186 llvm::SmallString<4096> LookupTable;
2187 uint32_t BucketOffset;
2188 {
2189 llvm::raw_svector_ostream Out(LookupTable);
2190 // Make sure that no bucket is at offset 0
2191 clang::io::Emit32(Out, 0);
2192 BucketOffset = Generator.Emit(Out, Trait);
2193 }
2194
2195 // Write the lookup table
2196 RecordData Record;
2197 Record.push_back(UPDATE_VISIBLE);
2198 Record.push_back(getDeclID(cast<Decl>(DC)));
2199 Record.push_back(BucketOffset);
2200 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2201}
2202
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002203//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002204// General Serialization Routines
2205//===----------------------------------------------------------------------===//
2206
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002207/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002208void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002209 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002210 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2211 const Attr * A = *i;
2212 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2213 AddSourceLocation(A->getLocation(), Record);
2214 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002215
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002216#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002217
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002218 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002219}
2220
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002221void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002222 Record.push_back(Str.size());
2223 Record.insert(Record.end(), Str.begin(), Str.end());
2224}
2225
Douglas Gregore84a9da2009-04-20 20:36:09 +00002226/// \brief Note that the identifier II occurs at the given offset
2227/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002228void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002229 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002230 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002231 // up earlier in the chain and thus don't need an offset.
2232 if (ID >= FirstIdentID)
2233 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002234}
2235
Douglas Gregor95c13f52009-04-25 17:48:32 +00002236/// \brief Note that the selector Sel occurs at the given offset
2237/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002238void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002239 unsigned ID = SelectorIDs[Sel];
2240 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002241 // Don't record offsets for selectors that are also available in a different
2242 // file.
2243 if (ID < FirstSelectorID)
2244 return;
2245 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002246}
2247
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002248ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002249 : Stream(Stream), Chain(0), SerializationListener(0),
2250 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002251 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002252 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002253 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2254 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002255 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002256 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2257 NextCXXBaseSpecifiersID(1)
2258{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002259}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002260
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002261void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002262 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002263 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002264 Stream.Emit((unsigned)'C', 8);
2265 Stream.Emit((unsigned)'P', 8);
2266 Stream.Emit((unsigned)'C', 8);
2267 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002268
Chris Lattner28fa4e62009-04-26 22:26:21 +00002269 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002270
Sebastian Redl143413f2010-07-12 22:02:52 +00002271 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002272 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002273 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002274 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002275}
2276
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002277void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002278 const char *isysroot) {
2279 using namespace llvm;
2280
2281 ASTContext &Context = SemaRef.Context;
2282 Preprocessor &PP = SemaRef.PP;
2283
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002284 // The translation unit is the first declaration we'll emit.
2285 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002286 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002287 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002288
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002289 // Make sure that we emit IdentifierInfos (and any attached
2290 // declarations) for builtins.
2291 {
2292 IdentifierTable &Table = PP.getIdentifierTable();
2293 llvm::SmallVector<const char *, 32> BuiltinNames;
2294 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2295 Context.getLangOptions().NoBuiltin);
2296 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2297 getIdentifierRef(&Table.get(BuiltinNames[I]));
2298 }
2299
Chris Lattner0c797362009-09-08 18:19:27 +00002300 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002301 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002302 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002303 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002304 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2305 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002306 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002307
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002308 // Build a record containing all of the file scoped decls in this file.
2309 RecordData UnusedFileScopedDecls;
2310 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2311 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002312
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002313 RecordData WeakUndeclaredIdentifiers;
2314 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2315 WeakUndeclaredIdentifiers.push_back(
2316 SemaRef.WeakUndeclaredIdentifiers.size());
2317 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2318 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2319 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2320 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2321 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2322 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2323 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2324 }
2325 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002326
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002327 // Build a record containing all of the locally-scoped external
2328 // declarations in this header file. Generally, this record will be
2329 // empty.
2330 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002331 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002332 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002333 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002334 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2335 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2336 TD != TDEnd; ++TD)
2337 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2338
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002339 // Build a record containing all of the ext_vector declarations.
2340 RecordData ExtVectorDecls;
2341 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2342 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2343
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002344 // Build a record containing all of the VTable uses information.
2345 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002346 if (!SemaRef.VTableUses.empty()) {
2347 VTableUses.push_back(SemaRef.VTableUses.size());
2348 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2349 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2350 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2351 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2352 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002353 }
2354
2355 // Build a record containing all of dynamic classes declarations.
2356 RecordData DynamicClasses;
2357 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2358 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2359
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002360 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002361 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002362 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002363 I = SemaRef.PendingInstantiations.begin(),
2364 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2365 AddDeclRef(I->first, PendingInstantiations);
2366 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002367 }
2368 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2369 "There are local ones at end of translation unit!");
2370
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002371 // Build a record containing some declaration references.
2372 RecordData SemaDeclRefs;
2373 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2374 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2375 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2376 }
2377
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002378 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002379 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002380 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002381 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002382 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002383 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002384 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002385 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002386 // Write the record of special types.
2387 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002388
Steve Naroffc277ad12009-07-18 15:33:26 +00002389 AddTypeRef(Context.getBuiltinVaListType(), Record);
2390 AddTypeRef(Context.getObjCIdType(), Record);
2391 AddTypeRef(Context.getObjCSelType(), Record);
2392 AddTypeRef(Context.getObjCProtoType(), Record);
2393 AddTypeRef(Context.getObjCClassType(), Record);
2394 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2395 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2396 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002397 AddTypeRef(Context.getjmp_bufType(), Record);
2398 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002399 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2400 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002401 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002402 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002403 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2404 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002405 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002406 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002407
Douglas Gregor1970d882009-04-26 03:49:13 +00002408 // Keep writing types and declarations until all types and
2409 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002410 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002411 WriteDeclsBlockAbbrevs();
2412 while (!DeclTypesToEmit.empty()) {
2413 DeclOrType DOT = DeclTypesToEmit.front();
2414 DeclTypesToEmit.pop();
2415 if (DOT.isType())
2416 WriteType(DOT.getType());
2417 else
2418 WriteDecl(Context, DOT.getDecl());
2419 }
2420 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002421
Douglas Gregor45053152009-10-17 17:25:45 +00002422 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002423 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002424 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002425 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002426
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002427 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002428 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002429
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002430 // Write the C++ base-specifier set offsets.
2431 if (!CXXBaseSpecifiersOffsets.empty()) {
2432 // Create a blob abbreviation for the C++ base specifiers offsets.
2433 using namespace llvm;
2434
2435 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2436 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2439 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2440
2441 // Write the selector offsets table.
2442 Record.clear();
2443 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2444 Record.push_back(CXXBaseSpecifiersOffsets.size());
2445 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2446 (const char *)CXXBaseSpecifiersOffsets.data(),
2447 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2448 }
2449
Douglas Gregord4df8652009-04-22 22:02:47 +00002450 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002451 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002452 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002453
2454 // Write the record containing tentative definitions.
2455 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002456 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002457
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002458 // Write the record containing unused file scoped decls.
2459 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002460 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002461
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002462 // Write the record containing weak undeclared identifiers.
2463 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002464 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002465 WeakUndeclaredIdentifiers);
2466
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002467 // Write the record containing locally-scoped external definitions.
2468 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002469 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002470 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002471
2472 // Write the record containing ext_vector type names.
2473 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002474 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002475
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002476 // Write the record containing VTable uses information.
2477 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002478 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002479
2480 // Write the record containing dynamic classes declarations.
2481 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002482 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002483
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002484 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002485 if (!PendingInstantiations.empty())
2486 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002487
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002488 // Write the record containing declaration references of Sema.
2489 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002490 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002491
Douglas Gregor08f01292009-04-17 22:13:46 +00002492 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002493 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002494 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002495 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002496 Record.push_back(NumLexicalDeclContexts);
2497 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002498 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002499 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002500}
2501
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002502void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002503 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002504 using namespace llvm;
2505
2506 ASTContext &Context = SemaRef.Context;
2507 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002508
Sebastian Redl143413f2010-07-12 22:02:52 +00002509 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002510 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002511 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002512 if (StatCalls && !isysroot)
2513 WriteStatCache(*StatCalls);
2514 // FIXME: Source manager block should only write new stuff, which could be
2515 // done by tracking the largest ID in the chain
2516 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002517
2518 // The special types are in the chained PCH.
2519
2520 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002521 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002522 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002523 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002524 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2525 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002526 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002527 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002528 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002529 else if ((*I)->isChangedSinceDeserialization())
2530 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002531 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002532 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002533 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002534 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002535 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2536 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2537 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002538 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002539 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2540 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002541 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002542 // And a visible updates block for the DeclContexts.
2543 Abv = new llvm::BitCodeAbbrev();
2544 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2545 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2546 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2547 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2548 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2549 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002550
Sebastian Redl98912122010-07-27 23:01:28 +00002551 // Build a record containing all of the new tentative definitions in this
2552 // file, in TentativeDefinitions order.
2553 RecordData TentativeDefinitions;
2554 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2555 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2556 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2557 }
2558
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002559 // Build a record containing all of the file scoped decls in this file.
2560 RecordData UnusedFileScopedDecls;
2561 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2562 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2563 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002564 }
2565
Sebastian Redl08aca90252010-08-05 18:21:25 +00002566 // We write the entire table, overwriting the tables from the chain.
2567 RecordData WeakUndeclaredIdentifiers;
2568 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2569 WeakUndeclaredIdentifiers.push_back(
2570 SemaRef.WeakUndeclaredIdentifiers.size());
2571 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2572 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2573 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2574 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2575 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2576 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2577 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2578 }
2579 }
2580
Sebastian Redl98912122010-07-27 23:01:28 +00002581 // Build a record containing all of the locally-scoped external
2582 // declarations in this header file. Generally, this record will be
2583 // empty.
2584 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002585 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002586 // nondeterminstic!
2587 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2588 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2589 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2590 TD != TDEnd; ++TD) {
2591 if (TD->second->getPCHLevel() == 0)
2592 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2593 }
2594
2595 // Build a record containing all of the ext_vector declarations.
2596 RecordData ExtVectorDecls;
2597 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2598 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2599 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2600 }
2601
Sebastian Redl08aca90252010-08-05 18:21:25 +00002602 // Build a record containing all of the VTable uses information.
2603 // We write everything here, because it's too hard to determine whether
2604 // a use is new to this part.
2605 RecordData VTableUses;
2606 if (!SemaRef.VTableUses.empty()) {
2607 VTableUses.push_back(SemaRef.VTableUses.size());
2608 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2609 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2610 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2611 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2612 }
2613 }
2614
2615 // Build a record containing all of dynamic classes declarations.
2616 RecordData DynamicClasses;
2617 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2618 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2619 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2620
2621 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002622 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002623 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002624 I = SemaRef.PendingInstantiations.begin(),
2625 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002626 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002627 AddDeclRef(I->first, PendingInstantiations);
2628 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002629 }
2630 }
2631 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2632 "There are local ones at end of translation unit!");
2633
2634 // Build a record containing some declaration references.
2635 // It's not worth the effort to avoid duplication here.
2636 RecordData SemaDeclRefs;
2637 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2638 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2639 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2640 }
2641
Sebastian Redl539c5062010-08-18 23:57:32 +00002642 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002643 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002644 for (DeclsToRewriteTy::iterator
2645 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2646 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002647 while (!DeclTypesToEmit.empty()) {
2648 DeclOrType DOT = DeclTypesToEmit.front();
2649 DeclTypesToEmit.pop();
2650 if (DOT.isType())
2651 WriteType(DOT.getType());
2652 else
2653 WriteDecl(Context, DOT.getDecl());
2654 }
2655 Stream.ExitBlock();
2656
Sebastian Redl98912122010-07-27 23:01:28 +00002657 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002658 WriteSelectors(SemaRef);
2659 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002660 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002661 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002662 // FIXME: For chained PCH only write the new mappings (we currently
2663 // write all of them again).
2664 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002665
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002666 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002667 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002668 RecordData FirstLatestDeclIDs;
2669 for (FirstLatestDeclMap::iterator
2670 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2671 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2672 "Expected first & second to be in different PCHs");
2673 AddDeclRef(I->first, FirstLatestDeclIDs);
2674 AddDeclRef(I->second, FirstLatestDeclIDs);
2675 }
2676 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002677 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002678
Sebastian Redl98912122010-07-27 23:01:28 +00002679 // Write the record containing external, unnamed definitions.
2680 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002681 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002682
2683 // Write the record containing tentative definitions.
2684 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002685 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002686
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002687 // Write the record containing unused file scoped decls.
2688 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002689 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002690
Sebastian Redl08aca90252010-08-05 18:21:25 +00002691 // Write the record containing weak undeclared identifiers.
2692 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002693 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002694 WeakUndeclaredIdentifiers);
2695
Sebastian Redl98912122010-07-27 23:01:28 +00002696 // Write the record containing locally-scoped external definitions.
2697 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002698 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002699 LocallyScopedExternalDecls);
2700
2701 // Write the record containing ext_vector type names.
2702 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002703 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002704
Sebastian Redl08aca90252010-08-05 18:21:25 +00002705 // Write the record containing VTable uses information.
2706 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002707 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002708
2709 // Write the record containing dynamic classes declarations.
2710 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002711 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002712
2713 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002714 if (!PendingInstantiations.empty())
2715 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002716
2717 // Write the record containing declaration references of Sema.
2718 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002719 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002720
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002721 // Write the updates to DeclContexts.
2722 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2723 I = UpdatedDeclContexts.begin(),
2724 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002725 I != E; ++I)
2726 WriteDeclContextVisibleUpdate(*I);
2727
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002728 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002729
Sebastian Redl98912122010-07-27 23:01:28 +00002730 Record.clear();
2731 Record.push_back(NumStatements);
2732 Record.push_back(NumMacros);
2733 Record.push_back(NumLexicalDeclContexts);
2734 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002735 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002736 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002737 Stream.ExitBlock();
2738}
2739
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002740void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002741 if (DeclUpdates.empty())
2742 return;
2743
2744 RecordData OffsetsRecord;
2745 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2746 for (DeclUpdateMap::iterator
2747 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2748 const Decl *D = I->first;
2749 UpdateRecord &URec = I->second;
2750
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002751 if (DeclsToRewrite.count(D))
2752 continue; // The decl will be written completely,no need to store updates.
2753
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002754 uint64_t Offset = Stream.GetCurrentBitNo();
2755 Stream.EmitRecord(DECL_UPDATES, URec);
2756
2757 OffsetsRecord.push_back(GetDeclRef(D));
2758 OffsetsRecord.push_back(Offset);
2759 }
2760 Stream.ExitBlock();
2761 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2762}
2763
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002764void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002765 if (ReplacedDecls.empty())
2766 return;
2767
2768 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002769 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002770 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2771 Record.push_back(I->first);
2772 Record.push_back(I->second);
2773 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002774 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002775}
2776
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002777void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002778 Record.push_back(Loc.getRawEncoding());
2779}
2780
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002781void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002782 AddSourceLocation(Range.getBegin(), Record);
2783 AddSourceLocation(Range.getEnd(), Record);
2784}
2785
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002786void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002787 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002788 const uint64_t *Words = Value.getRawData();
2789 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002790}
2791
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002792void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002793 Record.push_back(Value.isUnsigned());
2794 AddAPInt(Value, Record);
2795}
2796
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002797void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002798 AddAPInt(Value.bitcastToAPInt(), Record);
2799}
2800
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002801void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002802 Record.push_back(getIdentifierRef(II));
2803}
2804
Sebastian Redl539c5062010-08-18 23:57:32 +00002805IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002806 if (II == 0)
2807 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002808
Sebastian Redl539c5062010-08-18 23:57:32 +00002809 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002810 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002811 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002812 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002813}
2814
Sebastian Redl50e26582010-09-15 19:54:06 +00002815MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002816 if (MD == 0)
2817 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002818
2819 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002820 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002821 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002822 return ID;
2823}
2824
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002825void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002826 Record.push_back(getSelectorRef(SelRef));
2827}
2828
Sebastian Redl539c5062010-08-18 23:57:32 +00002829SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002830 if (Sel.getAsOpaquePtr() == 0) {
2831 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002832 }
2833
Sebastian Redl539c5062010-08-18 23:57:32 +00002834 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002835 if (SID == 0 && Chain) {
2836 // This might trigger a ReadSelector callback, which will set the ID for
2837 // this selector.
2838 Chain->LoadSelector(Sel);
2839 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002840 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002841 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002842 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002843 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002844}
2845
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002846void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002847 AddDeclRef(Temp->getDestructor(), Record);
2848}
2849
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002850void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2851 CXXBaseSpecifier const *BasesEnd,
2852 RecordDataImpl &Record) {
2853 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2854 CXXBaseSpecifiersToWrite.push_back(
2855 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2856 Bases, BasesEnd));
2857 Record.push_back(NextCXXBaseSpecifiersID++);
2858}
2859
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002860void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002861 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002862 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002863 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002864 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002865 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002866 break;
2867 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002868 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002869 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002870 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002871 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2872 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002873 break;
John McCall0ad16662009-10-29 08:12:44 +00002874 case TemplateArgument::Null:
2875 case TemplateArgument::Integral:
2876 case TemplateArgument::Declaration:
2877 case TemplateArgument::Pack:
2878 break;
2879 }
2880}
2881
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002882void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002883 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002884 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002885
2886 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2887 bool InfoHasSameExpr
2888 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2889 Record.push_back(InfoHasSameExpr);
2890 if (InfoHasSameExpr)
2891 return; // Avoid storing the same expr twice.
2892 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002893 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2894 Record);
2895}
2896
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002897void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002898 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002899 AddTypeRef(QualType(), Record);
2900 return;
2901 }
2902
John McCallbcd03502009-12-07 02:54:59 +00002903 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002904 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002905 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002906 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002907}
2908
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002909void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002910 Record.push_back(GetOrCreateTypeID(T));
2911}
2912
2913TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002914 return MakeTypeID(T,
2915 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2916}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002917
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002918TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002919 return MakeTypeID(T,
2920 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002921}
2922
2923TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2924 if (T.isNull())
2925 return TypeIdx();
2926 assert(!T.getLocalFastQualifiers());
2927
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002928 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002929 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002930 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002931 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002932 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002933 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002934 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002935 return Idx;
2936}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002937
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002938TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002939 if (T.isNull())
2940 return TypeIdx();
2941 assert(!T.getLocalFastQualifiers());
2942
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002943 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2944 assert(I != TypeIdxs.end() && "Type not emitted!");
2945 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002946}
2947
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002948void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002949 Record.push_back(GetDeclRef(D));
2950}
2951
Sebastian Redl539c5062010-08-18 23:57:32 +00002952DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002953 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002954 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002955 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002956 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00002957 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002958 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002959 // We haven't seen this declaration before. Give it a new ID and
2960 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002961 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002962 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002963 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2964 // We don't add it to the replacement collection here, because we don't
2965 // have the offset yet.
2966 DeclTypesToEmit.push(const_cast<Decl *>(D));
2967 // Reset the flag, so that we don't add this decl multiple times.
2968 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002969 }
2970
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002971 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002972}
2973
Sebastian Redl539c5062010-08-18 23:57:32 +00002974DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002975 if (D == 0)
2976 return 0;
2977
2978 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2979 return DeclIDs[D];
2980}
2981
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002982void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002983 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002984 Record.push_back(Name.getNameKind());
2985 switch (Name.getNameKind()) {
2986 case DeclarationName::Identifier:
2987 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2988 break;
2989
2990 case DeclarationName::ObjCZeroArgSelector:
2991 case DeclarationName::ObjCOneArgSelector:
2992 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002993 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002994 break;
2995
2996 case DeclarationName::CXXConstructorName:
2997 case DeclarationName::CXXDestructorName:
2998 case DeclarationName::CXXConversionFunctionName:
2999 AddTypeRef(Name.getCXXNameType(), Record);
3000 break;
3001
3002 case DeclarationName::CXXOperatorName:
3003 Record.push_back(Name.getCXXOverloadedOperator());
3004 break;
3005
Alexis Hunt3d221f22009-11-29 07:34:05 +00003006 case DeclarationName::CXXLiteralOperatorName:
3007 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3008 break;
3009
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003010 case DeclarationName::CXXUsingDirective:
3011 // No extra data to emit
3012 break;
3013 }
3014}
Chris Lattnerca025db2010-05-07 21:43:38 +00003015
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003016void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003017 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003018 switch (Name.getNameKind()) {
3019 case DeclarationName::CXXConstructorName:
3020 case DeclarationName::CXXDestructorName:
3021 case DeclarationName::CXXConversionFunctionName:
3022 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3023 break;
3024
3025 case DeclarationName::CXXOperatorName:
3026 AddSourceLocation(
3027 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3028 Record);
3029 AddSourceLocation(
3030 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3031 Record);
3032 break;
3033
3034 case DeclarationName::CXXLiteralOperatorName:
3035 AddSourceLocation(
3036 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3037 Record);
3038 break;
3039
3040 case DeclarationName::Identifier:
3041 case DeclarationName::ObjCZeroArgSelector:
3042 case DeclarationName::ObjCOneArgSelector:
3043 case DeclarationName::ObjCMultiArgSelector:
3044 case DeclarationName::CXXUsingDirective:
3045 break;
3046 }
3047}
3048
3049void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003050 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003051 AddDeclarationName(NameInfo.getName(), Record);
3052 AddSourceLocation(NameInfo.getLoc(), Record);
3053 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3054}
3055
3056void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003057 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003058 AddNestedNameSpecifier(Info.NNS, Record);
3059 AddSourceRange(Info.NNSRange, Record);
3060 Record.push_back(Info.NumTemplParamLists);
3061 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3062 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3063}
3064
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003065void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003066 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003067 // Nested name specifiers usually aren't too long. I think that 8 would
3068 // typically accomodate the vast majority.
3069 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3070
3071 // Push each of the NNS's onto a stack for serialization in reverse order.
3072 while (NNS) {
3073 NestedNames.push_back(NNS);
3074 NNS = NNS->getPrefix();
3075 }
3076
3077 Record.push_back(NestedNames.size());
3078 while(!NestedNames.empty()) {
3079 NNS = NestedNames.pop_back_val();
3080 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3081 Record.push_back(Kind);
3082 switch (Kind) {
3083 case NestedNameSpecifier::Identifier:
3084 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3085 break;
3086
3087 case NestedNameSpecifier::Namespace:
3088 AddDeclRef(NNS->getAsNamespace(), Record);
3089 break;
3090
3091 case NestedNameSpecifier::TypeSpec:
3092 case NestedNameSpecifier::TypeSpecWithTemplate:
3093 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3094 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3095 break;
3096
3097 case NestedNameSpecifier::Global:
3098 // Don't need to write an associated value.
3099 break;
3100 }
3101 }
3102}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003103
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003104void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003105 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003106 Record.push_back(Kind);
3107 switch (Kind) {
3108 case TemplateName::Template:
3109 AddDeclRef(Name.getAsTemplateDecl(), Record);
3110 break;
3111
3112 case TemplateName::OverloadedTemplate: {
3113 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3114 Record.push_back(OvT->size());
3115 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3116 I != E; ++I)
3117 AddDeclRef(*I, Record);
3118 break;
3119 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003120
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003121 case TemplateName::QualifiedTemplate: {
3122 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3123 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3124 Record.push_back(QualT->hasTemplateKeyword());
3125 AddDeclRef(QualT->getTemplateDecl(), Record);
3126 break;
3127 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003128
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003129 case TemplateName::DependentTemplate: {
3130 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3131 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3132 Record.push_back(DepT->isIdentifier());
3133 if (DepT->isIdentifier())
3134 AddIdentifierRef(DepT->getIdentifier(), Record);
3135 else
3136 Record.push_back(DepT->getOperator());
3137 break;
3138 }
3139 }
3140}
3141
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003142void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003143 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003144 Record.push_back(Arg.getKind());
3145 switch (Arg.getKind()) {
3146 case TemplateArgument::Null:
3147 break;
3148 case TemplateArgument::Type:
3149 AddTypeRef(Arg.getAsType(), Record);
3150 break;
3151 case TemplateArgument::Declaration:
3152 AddDeclRef(Arg.getAsDecl(), Record);
3153 break;
3154 case TemplateArgument::Integral:
3155 AddAPSInt(*Arg.getAsIntegral(), Record);
3156 AddTypeRef(Arg.getIntegralType(), Record);
3157 break;
3158 case TemplateArgument::Template:
3159 AddTemplateName(Arg.getAsTemplate(), Record);
3160 break;
3161 case TemplateArgument::Expression:
3162 AddStmt(Arg.getAsExpr());
3163 break;
3164 case TemplateArgument::Pack:
3165 Record.push_back(Arg.pack_size());
3166 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3167 I != E; ++I)
3168 AddTemplateArgument(*I, Record);
3169 break;
3170 }
3171}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003172
3173void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003174ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003175 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003176 assert(TemplateParams && "No TemplateParams!");
3177 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3178 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3179 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3180 Record.push_back(TemplateParams->size());
3181 for (TemplateParameterList::const_iterator
3182 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3183 P != PEnd; ++P)
3184 AddDeclRef(*P, Record);
3185}
3186
3187/// \brief Emit a template argument list.
3188void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003189ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003190 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003191 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003192 Record.push_back(TemplateArgs->size());
3193 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003194 AddTemplateArgument(TemplateArgs->get(i), Record);
3195}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003196
3197
3198void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003199ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003200 Record.push_back(Set.size());
3201 for (UnresolvedSetImpl::const_iterator
3202 I = Set.begin(), E = Set.end(); I != E; ++I) {
3203 AddDeclRef(I.getDecl(), Record);
3204 Record.push_back(I.getAccess());
3205 }
3206}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003207
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003208void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003209 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003210 Record.push_back(Base.isVirtual());
3211 Record.push_back(Base.isBaseOfClass());
3212 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003213 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003214 AddSourceRange(Base.getSourceRange(), Record);
3215}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003216
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003217void ASTWriter::FlushCXXBaseSpecifiers() {
3218 RecordData Record;
3219 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3220 Record.clear();
3221
3222 // Record the offset of this base-specifier set.
3223 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3224 if (Index == CXXBaseSpecifiersOffsets.size())
3225 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3226 else {
3227 if (Index > CXXBaseSpecifiersOffsets.size())
3228 CXXBaseSpecifiersOffsets.resize(Index + 1);
3229 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3230 }
3231
3232 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3233 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3234 Record.push_back(BEnd - B);
3235 for (; B != BEnd; ++B)
3236 AddCXXBaseSpecifier(*B, Record);
3237 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003238
3239 // Flush any expressions that were written as part of the base specifiers.
3240 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003241 }
3242
3243 CXXBaseSpecifiersToWrite.clear();
3244}
3245
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003246void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003247 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003248 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003249 Record.push_back(NumBaseOrMembers);
3250 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3251 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3252
3253 Record.push_back(Init->isBaseInitializer());
3254 if (Init->isBaseInitializer()) {
3255 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3256 Record.push_back(Init->isBaseVirtual());
3257 } else {
3258 AddDeclRef(Init->getMember(), Record);
3259 }
3260 AddSourceLocation(Init->getMemberLocation(), Record);
3261 AddStmt(Init->getInit());
3262 AddDeclRef(Init->getAnonUnionMember(), Record);
3263 AddSourceLocation(Init->getLParenLoc(), Record);
3264 AddSourceLocation(Init->getRParenLoc(), Record);
3265 Record.push_back(Init->isWritten());
3266 if (Init->isWritten()) {
3267 Record.push_back(Init->getSourceOrder());
3268 } else {
3269 Record.push_back(Init->getNumArrayIndices());
3270 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3271 AddDeclRef(Init->getArrayIndex(i), Record);
3272 }
3273 }
3274}
3275
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003276void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3277 assert(D->DefinitionData);
3278 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3279 Record.push_back(Data.UserDeclaredConstructor);
3280 Record.push_back(Data.UserDeclaredCopyConstructor);
3281 Record.push_back(Data.UserDeclaredCopyAssignment);
3282 Record.push_back(Data.UserDeclaredDestructor);
3283 Record.push_back(Data.Aggregate);
3284 Record.push_back(Data.PlainOldData);
3285 Record.push_back(Data.Empty);
3286 Record.push_back(Data.Polymorphic);
3287 Record.push_back(Data.Abstract);
3288 Record.push_back(Data.HasTrivialConstructor);
3289 Record.push_back(Data.HasTrivialCopyConstructor);
3290 Record.push_back(Data.HasTrivialCopyAssignment);
3291 Record.push_back(Data.HasTrivialDestructor);
3292 Record.push_back(Data.ComputedVisibleConversions);
3293 Record.push_back(Data.DeclaredDefaultConstructor);
3294 Record.push_back(Data.DeclaredCopyConstructor);
3295 Record.push_back(Data.DeclaredCopyAssignment);
3296 Record.push_back(Data.DeclaredDestructor);
3297
3298 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003299 if (Data.NumBases > 0)
3300 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3301 Record);
3302
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003303 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3304 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003305 if (Data.NumVBases > 0)
3306 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3307 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003308
3309 AddUnresolvedSet(Data.Conversions, Record);
3310 AddUnresolvedSet(Data.VisibleConversions, Record);
3311 // Data.Definition is the owning decl, no need to write it.
3312 AddDeclRef(Data.FirstFriend, Record);
3313}
3314
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003315void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003316 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003317 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003318 assert(FirstDeclID == NextDeclID &&
3319 FirstTypeID == NextTypeID &&
3320 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003321 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003322 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003323 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003324 "Setting chain after writing has started.");
3325 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003326
3327 FirstDeclID += Chain->getTotalNumDecls();
3328 FirstTypeID += Chain->getTotalNumTypes();
3329 FirstIdentID += Chain->getTotalNumIdentifiers();
3330 FirstSelectorID += Chain->getTotalNumSelectors();
3331 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003332 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003333 NextDeclID = FirstDeclID;
3334 NextTypeID = FirstTypeID;
3335 NextIdentID = FirstIdentID;
3336 NextSelectorID = FirstSelectorID;
3337 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003338 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003339}
3340
Sebastian Redl539c5062010-08-18 23:57:32 +00003341void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003342 IdentifierIDs[II] = ID;
3343}
3344
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003345void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003346 // Always take the highest-numbered type index. This copes with an interesting
3347 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003348 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003349 // keep the higher-numbered entry so that we can properly write it out to
3350 // the AST file.
3351 TypeIdx &StoredIdx = TypeIdxs[T];
3352 if (Idx.getIndex() >= StoredIdx.getIndex())
3353 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003354}
3355
Sebastian Redl539c5062010-08-18 23:57:32 +00003356void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003357 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003358}
Sebastian Redl834bb972010-08-04 17:20:04 +00003359
Sebastian Redl539c5062010-08-18 23:57:32 +00003360void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003361 SelectorIDs[S] = ID;
3362}
Douglas Gregor91096292010-10-02 19:29:26 +00003363
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003364void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003365 MacroDefinition *MD) {
3366 MacroDefinitions[MD] = ID;
3367}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003368
3369void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3370 assert(D->isDefinition());
3371 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3372 // We are interested when a PCH decl is modified.
3373 if (RD->getPCHLevel() > 0) {
3374 // A forward reference was mutated into a definition. Rewrite it.
3375 // FIXME: This happens during template instantiation, should we
3376 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003377 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003378 }
3379
3380 for (CXXRecordDecl::redecl_iterator
3381 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3382 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3383 if (Redecl == RD)
3384 continue;
3385
3386 // We are interested when a PCH decl is modified.
3387 if (Redecl->getPCHLevel() > 0) {
3388 UpdateRecord &Record = DeclUpdates[Redecl];
3389 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3390 assert(Redecl->DefinitionData);
3391 assert(Redecl->DefinitionData->Definition == D);
3392 AddDeclRef(D, Record); // the DefinitionDecl
3393 }
3394 }
3395 }
3396}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003397void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3398 // TU and namespaces are handled elsewhere.
3399 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3400 return;
3401
3402 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3403 return; // Not a source decl added to a DeclContext from PCH.
3404
3405 AddUpdatedDeclContext(DC);
3406}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003407
3408void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3409 assert(D->isImplicit());
3410 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3411 return; // Not a source member added to a class from PCH.
3412 if (!isa<CXXMethodDecl>(D))
3413 return; // We are interested in lazily declared implicit methods.
3414
3415 // A decl coming from PCH was modified.
3416 assert(RD->isDefinition());
3417 UpdateRecord &Record = DeclUpdates[RD];
3418 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3419 AddDeclRef(D, Record);
3420}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003421
3422void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3423 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003424 // The specializations set is kept in the canonical template.
3425 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003426 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3427 return; // Not a source specialization added to a template from PCH.
3428
3429 UpdateRecord &Record = DeclUpdates[TD];
3430 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3431 AddDeclRef(D, Record);
3432}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003433
3434ASTSerializationListener::~ASTSerializationListener() { }