blob: 54a2648cd3c7fa9ca66a7afbd32e88418d012243 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050
Sebastian Redl3df5a082010-07-30 17:03:48 +000051template <typename T, typename Allocator>
52T *data(std::vector<T, Allocator> &v) {
53 return v.empty() ? 0 : &v.front();
54}
55template <typename T, typename Allocator>
56const T *data(const std::vector<T, Allocator> &v) {
57 return v.empty() ? 0 : &v.front();
58}
59
Douglas Gregoref84c4b2009-04-09 22:27:44 +000060//===----------------------------------------------------------------------===//
61// Type serialization
62//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000063
Douglas Gregoref84c4b2009-04-09 22:27:44 +000064namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000065 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000066 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000067 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068
69 public:
70 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000071 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000073 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000074 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075
76 void VisitArrayType(const ArrayType *T);
77 void VisitFunctionType(const FunctionType *T);
78 void VisitTagType(const TagType *T);
79
80#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
81#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082#include "clang/AST/TypeNodes.def"
83 };
84}
85
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000086void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000087 assert(false && "Built-in types are never serialized");
88}
89
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000090void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000091 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000092 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093}
94
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000095void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000097 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098}
99
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000101 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000102 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103}
104
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000107 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108}
109
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000112 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000116 Writer.AddTypeRef(T->getPointeeType(), Record);
117 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000118 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119}
120
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122 Writer.AddTypeRef(T->getElementType(), Record);
123 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000124 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128 VisitArrayType(T);
129 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000140 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
141 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000142 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 Writer.AddTypeRef(T->getElementType(), Record);
148 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000149 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000150 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000155 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156}
157
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000160 FunctionType::ExtInfo C = T->getExtInfo();
161 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000162 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000163 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000164 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000169 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170}
171
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173 VisitFunctionType(T);
174 Record.push_back(T->getNumArgs());
175 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
176 Writer.AddTypeRef(T->getArgType(I), Record);
177 Record.push_back(T->isVariadic());
178 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000179 Record.push_back(T->hasExceptionSpec());
180 Record.push_back(T->hasAnyExceptionSpec());
181 Record.push_back(T->getNumExceptions());
182 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
183 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000184 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000188 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000189 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000190}
John McCallb96ec562009-12-04 22:46:56 +0000191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000194 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
195 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000200 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202}
203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000206 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207}
208
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000210 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000211 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000212}
213
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000215 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000217 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218 "Cannot serialize in the middle of a type definition");
219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224}
225
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000228 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229}
230
John McCall81904512011-01-06 01:58:22 +0000231void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
232 Writer.AddTypeRef(T->getModifiedType(), Record);
233 Writer.AddTypeRef(T->getEquivalentType(), Record);
234 Record.push_back(T->getAttrKind());
235 Code = TYPE_ATTRIBUTED;
236}
237
Mike Stump11289f42009-09-09 15:08:12 +0000238void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000240 const SubstTemplateTypeParmType *T) {
241 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
242 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000243 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000244}
245
246void
Douglas Gregorada4b792011-01-14 02:55:32 +0000247ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
248 const SubstTemplateTypeParmPackType *T) {
249 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
250 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
251 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
252}
253
254void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000255ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000257 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000258 Writer.AddTemplateName(T->getTemplateName(), Record);
259 Record.push_back(T->getNumArgs());
260 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
261 ArgI != ArgE; ++ArgI)
262 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000263 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
264 : T->getCanonicalTypeInternal(),
265 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000266 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000267}
268
269void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000271 VisitArrayType(T);
272 Writer.AddStmt(T->getSizeExpr());
273 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000274 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000275}
276
277void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000278ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000279 const DependentSizedExtVectorType *T) {
280 // FIXME: Serialize this type (C++ only)
281 assert(false && "Cannot serialize dependent sized extended vector types");
282}
283
284void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000285ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000286 Record.push_back(T->getDepth());
287 Record.push_back(T->getIndex());
288 Record.push_back(T->isParameterPack());
289 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000290 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000291}
292
293void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000294ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000295 Record.push_back(T->getKeyword());
296 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
297 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000298 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
299 : T->getCanonicalTypeInternal(),
300 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000301 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000306 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000307 Record.push_back(T->getKeyword());
308 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
309 Writer.AddIdentifierRef(T->getIdentifier(), Record);
310 Record.push_back(T->getNumArgs());
311 for (DependentTemplateSpecializationType::iterator
312 I = T->begin(), E = T->end(); I != E; ++I)
313 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000314 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000315}
316
Douglas Gregord2fa7662010-12-20 02:24:11 +0000317void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
318 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000319 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
320 Record.push_back(*NumExpansions + 1);
321 else
322 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000323 Code = TYPE_PACK_EXPANSION;
324}
325
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000326void ASTTypeWriter::VisitParenType(const ParenType *T) {
327 Writer.AddTypeRef(T->getInnerType(), Record);
328 Code = TYPE_PAREN;
329}
330
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000331void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000332 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000333 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
334 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000335 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000336}
337
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000338void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000339 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000340 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000341 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000342}
343
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000344void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000345 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000346 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000347}
348
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000349void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000350 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000351 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000352 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000353 E = T->qual_end(); I != E; ++I)
354 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000355 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000356}
357
Steve Narofffb4330f2009-06-17 22:40:22 +0000358void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000359ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000360 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000361 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000362}
363
John McCall8f115c62009-10-16 21:56:05 +0000364namespace {
365
366class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000367 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000368 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000369
370public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000371 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000372 : Writer(Writer), Record(Record) { }
373
John McCall17001972009-10-18 01:05:36 +0000374#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000375#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000376 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000377#include "clang/AST/TypeLocNodes.def"
378
John McCall17001972009-10-18 01:05:36 +0000379 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
380 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000381};
382
383}
384
John McCall17001972009-10-18 01:05:36 +0000385void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
386 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000387}
John McCall17001972009-10-18 01:05:36 +0000388void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000389 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
390 if (TL.needsExtraLocalData()) {
391 Record.push_back(TL.getWrittenTypeSpec());
392 Record.push_back(TL.getWrittenSignSpec());
393 Record.push_back(TL.getWrittenWidthSpec());
394 Record.push_back(TL.hasModeAttr());
395 }
John McCall8f115c62009-10-16 21:56:05 +0000396}
John McCall17001972009-10-18 01:05:36 +0000397void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
398 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000399}
John McCall17001972009-10-18 01:05:36 +0000400void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000402}
John McCall17001972009-10-18 01:05:36 +0000403void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000405}
John McCall17001972009-10-18 01:05:36 +0000406void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000408}
John McCall17001972009-10-18 01:05:36 +0000409void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000411}
John McCall17001972009-10-18 01:05:36 +0000412void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000414}
John McCall17001972009-10-18 01:05:36 +0000415void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
416 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
417 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
418 Record.push_back(TL.getSizeExpr() ? 1 : 0);
419 if (TL.getSizeExpr())
420 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000421}
John McCall17001972009-10-18 01:05:36 +0000422void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
423 VisitArrayTypeLoc(TL);
424}
425void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
426 VisitArrayTypeLoc(TL);
427}
428void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
429 VisitArrayTypeLoc(TL);
430}
431void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
432 DependentSizedArrayTypeLoc TL) {
433 VisitArrayTypeLoc(TL);
434}
435void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
436 DependentSizedExtVectorTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getNameLoc(), Record);
438}
439void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getNameLoc(), Record);
441}
442void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
445void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
447 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000448 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000449 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
450 Writer.AddDeclRef(TL.getArg(i), Record);
451}
452void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
453 VisitFunctionTypeLoc(TL);
454}
455void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
456 VisitFunctionTypeLoc(TL);
457}
John McCallb96ec562009-12-04 22:46:56 +0000458void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getNameLoc(), Record);
460}
John McCall17001972009-10-18 01:05:36 +0000461void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
462 Writer.AddSourceLocation(TL.getNameLoc(), Record);
463}
464void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000465 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
466 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
467 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000468}
469void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000470 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
471 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
472 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
473 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000474}
475void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
477}
478void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
481void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
John McCall81904512011-01-06 01:58:22 +0000484void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
486 if (TL.hasAttrOperand()) {
487 SourceRange range = TL.getAttrOperandParensRange();
488 Writer.AddSourceLocation(range.getBegin(), Record);
489 Writer.AddSourceLocation(range.getEnd(), Record);
490 }
491 if (TL.hasAttrExprOperand()) {
492 Expr *operand = TL.getAttrExprOperand();
493 Record.push_back(operand ? 1 : 0);
494 if (operand) Writer.AddStmt(operand);
495 } else if (TL.hasAttrEnumOperand()) {
496 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
497 }
498}
John McCall17001972009-10-18 01:05:36 +0000499void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getNameLoc(), Record);
501}
John McCallcebee162009-10-18 09:09:24 +0000502void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
503 SubstTemplateTypeParmTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
Douglas Gregorada4b792011-01-14 02:55:32 +0000506void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
507 SubstTemplateTypeParmPackTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
John McCall17001972009-10-18 01:05:36 +0000510void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
511 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000512 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
513 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
514 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
515 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000516 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
517 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000518}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000519void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
521 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
522}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000523void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000524 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
525 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000526}
John McCalle78aac42010-03-10 03:28:59 +0000527void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000530void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000531 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
532 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
John McCallc392f372010-06-11 00:33:02 +0000535void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
536 DependentTemplateSpecializationTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
538 Writer.AddSourceRange(TL.getQualifierRange(), Record);
539 Writer.AddSourceLocation(TL.getNameLoc(), Record);
540 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
541 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
542 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000543 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
544 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000545}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000546void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
548}
John McCall17001972009-10-18 01:05:36 +0000549void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
550 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000551}
552void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
553 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000554 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
555 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
556 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
557 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000558}
John McCallfc93cf92009-10-22 22:37:11 +0000559void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000561}
John McCall8f115c62009-10-16 21:56:05 +0000562
Chris Lattner19cea4e2009-04-22 05:57:30 +0000563//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000564// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000565//===----------------------------------------------------------------------===//
566
Chris Lattner28fa4e62009-04-26 22:26:21 +0000567static void EmitBlockID(unsigned ID, const char *Name,
568 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000569 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000570 Record.clear();
571 Record.push_back(ID);
572 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
573
574 // Emit the block name if present.
575 if (Name == 0 || Name[0] == 0) return;
576 Record.clear();
577 while (*Name)
578 Record.push_back(*Name++);
579 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
580}
581
582static void EmitRecordID(unsigned ID, const char *Name,
583 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000584 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000585 Record.clear();
586 Record.push_back(ID);
587 while (*Name)
588 Record.push_back(*Name++);
589 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000590}
591
592static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000593 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000594#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000595 RECORD(STMT_STOP);
596 RECORD(STMT_NULL_PTR);
597 RECORD(STMT_NULL);
598 RECORD(STMT_COMPOUND);
599 RECORD(STMT_CASE);
600 RECORD(STMT_DEFAULT);
601 RECORD(STMT_LABEL);
602 RECORD(STMT_IF);
603 RECORD(STMT_SWITCH);
604 RECORD(STMT_WHILE);
605 RECORD(STMT_DO);
606 RECORD(STMT_FOR);
607 RECORD(STMT_GOTO);
608 RECORD(STMT_INDIRECT_GOTO);
609 RECORD(STMT_CONTINUE);
610 RECORD(STMT_BREAK);
611 RECORD(STMT_RETURN);
612 RECORD(STMT_DECL);
613 RECORD(STMT_ASM);
614 RECORD(EXPR_PREDEFINED);
615 RECORD(EXPR_DECL_REF);
616 RECORD(EXPR_INTEGER_LITERAL);
617 RECORD(EXPR_FLOATING_LITERAL);
618 RECORD(EXPR_IMAGINARY_LITERAL);
619 RECORD(EXPR_STRING_LITERAL);
620 RECORD(EXPR_CHARACTER_LITERAL);
621 RECORD(EXPR_PAREN);
622 RECORD(EXPR_UNARY_OPERATOR);
623 RECORD(EXPR_SIZEOF_ALIGN_OF);
624 RECORD(EXPR_ARRAY_SUBSCRIPT);
625 RECORD(EXPR_CALL);
626 RECORD(EXPR_MEMBER);
627 RECORD(EXPR_BINARY_OPERATOR);
628 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
629 RECORD(EXPR_CONDITIONAL_OPERATOR);
630 RECORD(EXPR_IMPLICIT_CAST);
631 RECORD(EXPR_CSTYLE_CAST);
632 RECORD(EXPR_COMPOUND_LITERAL);
633 RECORD(EXPR_EXT_VECTOR_ELEMENT);
634 RECORD(EXPR_INIT_LIST);
635 RECORD(EXPR_DESIGNATED_INIT);
636 RECORD(EXPR_IMPLICIT_VALUE_INIT);
637 RECORD(EXPR_VA_ARG);
638 RECORD(EXPR_ADDR_LABEL);
639 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000640 RECORD(EXPR_CHOOSE);
641 RECORD(EXPR_GNU_NULL);
642 RECORD(EXPR_SHUFFLE_VECTOR);
643 RECORD(EXPR_BLOCK);
644 RECORD(EXPR_BLOCK_DECL_REF);
645 RECORD(EXPR_OBJC_STRING_LITERAL);
646 RECORD(EXPR_OBJC_ENCODE);
647 RECORD(EXPR_OBJC_SELECTOR_EXPR);
648 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
649 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
650 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
651 RECORD(EXPR_OBJC_KVC_REF_EXPR);
652 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000653 RECORD(STMT_OBJC_FOR_COLLECTION);
654 RECORD(STMT_OBJC_CATCH);
655 RECORD(STMT_OBJC_FINALLY);
656 RECORD(STMT_OBJC_AT_TRY);
657 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
658 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000659 RECORD(EXPR_CXX_OPERATOR_CALL);
660 RECORD(EXPR_CXX_CONSTRUCT);
661 RECORD(EXPR_CXX_STATIC_CAST);
662 RECORD(EXPR_CXX_DYNAMIC_CAST);
663 RECORD(EXPR_CXX_REINTERPRET_CAST);
664 RECORD(EXPR_CXX_CONST_CAST);
665 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
666 RECORD(EXPR_CXX_BOOL_LITERAL);
667 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000668#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000669}
Mike Stump11289f42009-09-09 15:08:12 +0000670
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000671void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000672 RecordData Record;
673 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Sebastian Redl539c5062010-08-18 23:57:32 +0000675#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
676#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000677
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000678 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000679 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000680 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000681 RECORD(TYPE_OFFSET);
682 RECORD(DECL_OFFSET);
683 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000684 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000685 RECORD(IDENTIFIER_OFFSET);
686 RECORD(IDENTIFIER_TABLE);
687 RECORD(EXTERNAL_DEFINITIONS);
688 RECORD(SPECIAL_TYPES);
689 RECORD(STATISTICS);
690 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000691 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000692 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
693 RECORD(SELECTOR_OFFSETS);
694 RECORD(METHOD_POOL);
695 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000696 RECORD(SOURCE_LOCATION_OFFSETS);
697 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000698 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000699 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000700 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000701 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000702 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000703 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000704
Chris Lattner28fa4e62009-04-26 22:26:21 +0000705 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000706 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000707 RECORD(SM_SLOC_FILE_ENTRY);
708 RECORD(SM_SLOC_BUFFER_ENTRY);
709 RECORD(SM_SLOC_BUFFER_BLOB);
710 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
711 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000712
Chris Lattner28fa4e62009-04-26 22:26:21 +0000713 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000714 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000715 RECORD(PP_MACRO_OBJECT_LIKE);
716 RECORD(PP_MACRO_FUNCTION_LIKE);
717 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000718 RECORD(PP_MACRO_INSTANTIATION);
719 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000720
Douglas Gregor12bfa382009-10-17 00:13:19 +0000721 // Decls and Types block.
722 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000723 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000724 RECORD(TYPE_COMPLEX);
725 RECORD(TYPE_POINTER);
726 RECORD(TYPE_BLOCK_POINTER);
727 RECORD(TYPE_LVALUE_REFERENCE);
728 RECORD(TYPE_RVALUE_REFERENCE);
729 RECORD(TYPE_MEMBER_POINTER);
730 RECORD(TYPE_CONSTANT_ARRAY);
731 RECORD(TYPE_INCOMPLETE_ARRAY);
732 RECORD(TYPE_VARIABLE_ARRAY);
733 RECORD(TYPE_VECTOR);
734 RECORD(TYPE_EXT_VECTOR);
735 RECORD(TYPE_FUNCTION_PROTO);
736 RECORD(TYPE_FUNCTION_NO_PROTO);
737 RECORD(TYPE_TYPEDEF);
738 RECORD(TYPE_TYPEOF_EXPR);
739 RECORD(TYPE_TYPEOF);
740 RECORD(TYPE_RECORD);
741 RECORD(TYPE_ENUM);
742 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000743 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000744 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000745 RECORD(DECL_TRANSLATION_UNIT);
746 RECORD(DECL_TYPEDEF);
747 RECORD(DECL_ENUM);
748 RECORD(DECL_RECORD);
749 RECORD(DECL_ENUM_CONSTANT);
750 RECORD(DECL_FUNCTION);
751 RECORD(DECL_OBJC_METHOD);
752 RECORD(DECL_OBJC_INTERFACE);
753 RECORD(DECL_OBJC_PROTOCOL);
754 RECORD(DECL_OBJC_IVAR);
755 RECORD(DECL_OBJC_AT_DEFS_FIELD);
756 RECORD(DECL_OBJC_CLASS);
757 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
758 RECORD(DECL_OBJC_CATEGORY);
759 RECORD(DECL_OBJC_CATEGORY_IMPL);
760 RECORD(DECL_OBJC_IMPLEMENTATION);
761 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
762 RECORD(DECL_OBJC_PROPERTY);
763 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000764 RECORD(DECL_FIELD);
765 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000766 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000767 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000768 RECORD(DECL_FILE_SCOPE_ASM);
769 RECORD(DECL_BLOCK);
770 RECORD(DECL_CONTEXT_LEXICAL);
771 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000772 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000773 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000774#undef RECORD
775#undef BLOCK
776 Stream.ExitBlock();
777}
778
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000779/// \brief Adjusts the given filename to only write out the portion of the
780/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000781///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000782/// \param Filename the file name to adjust.
783///
784/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
785/// the returned filename will be adjusted by this system root.
786///
787/// \returns either the original filename (if it needs no adjustment) or the
788/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000789static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000790adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
791 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000792
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000793 if (!isysroot)
794 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000795
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000796 // Verify that the filename and the system root have the same prefix.
797 unsigned Pos = 0;
798 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
799 if (Filename[Pos] != isysroot[Pos])
800 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000801
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000802 // We hit the end of the filename before we hit the end of the system root.
803 if (!Filename[Pos])
804 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000805
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000806 // If the file name has a '/' at the current position, skip over the '/'.
807 // We distinguish sysroot-based includes from absolute includes by the
808 // absence of '/' at the beginning of sysroot-based includes.
809 if (Filename[Pos] == '/')
810 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000811
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000812 return Filename + Pos;
813}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000814
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000815/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000816void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000817 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000818
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000819 // Metadata
820 const TargetInfo &Target = Context.Target;
821 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000822 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000823 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000824 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
825 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000826 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
827 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
828 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000829 // Target triple or chained PCH name
830 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000831 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000832
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000833 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000834 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
835 Record.push_back(VERSION_MAJOR);
836 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000837 Record.push_back(CLANG_VERSION_MAJOR);
838 Record.push_back(CLANG_VERSION_MINOR);
839 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000840 // FIXME: This writes the absolute path for chained headers.
841 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
842 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000843
Douglas Gregor45fe0362009-05-12 01:31:05 +0000844 // Original file name
845 SourceManager &SM = Context.getSourceManager();
846 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
847 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000848 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000849 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
850 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
851
Michael J. Spencer740857f2010-12-21 16:45:57 +0000852 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000853
Michael J. Spencer740857f2010-12-21 16:45:57 +0000854 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000855
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000856 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000857 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000858 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000859 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000860 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000861 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000862 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000863
Ted Kremenek18e066f2010-01-22 22:12:47 +0000864 // Repository branch/version information.
865 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000866 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000867 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
868 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000869 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000870 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000871 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
872 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000873}
874
875/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000876void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000877 RecordData Record;
878 Record.push_back(LangOpts.Trigraphs);
879 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
880 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
881 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
882 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000883 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000884 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
885 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
886 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
887 Record.push_back(LangOpts.C99); // C99 Support
888 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000889 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
890 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000891 Record.push_back(LangOpts.CPlusPlus); // C++ Support
892 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000893 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000894
Douglas Gregor55abb232009-04-10 20:39:37 +0000895 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
896 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000897 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000898 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000899 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000900 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +0000901 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +0000902 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
903 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000904 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000905
Douglas Gregor55abb232009-04-10 20:39:37 +0000906 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000907 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
908 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000909 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000910 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000911 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000912
913 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
914 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
915 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
916
Chris Lattner258172e2009-04-27 07:35:58 +0000917 // Whether static initializers are protected by locks.
918 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000919 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000920 Record.push_back(LangOpts.Blocks); // block extension to C
921 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
922 // they are unused.
923 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
924 // (modulo the platform support).
925
Chris Lattner51924e512010-06-26 21:25:03 +0000926 Record.push_back(LangOpts.getSignedOverflowBehavior());
927 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000928
929 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000930 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000931 // defined.
932 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
933 // opposed to __DYNAMIC__).
934 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
935
936 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
937 // used (instead of C99 semantics).
938 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000939 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
940 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000941 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
942 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000943 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000944 Record.push_back(LangOpts.getGCMode());
945 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000946 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000947 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000948 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000949 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +0000950 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000951 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000952 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000953 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000954}
955
Douglas Gregora7f71a92009-04-10 03:52:48 +0000956//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000957// stat cache Serialization
958//===----------------------------------------------------------------------===//
959
960namespace {
961// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000962class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000963public:
964 typedef const char * key_type;
965 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000966
Chris Lattner2a6fa472010-11-23 19:28:12 +0000967 typedef struct stat data_type;
968 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000969
970 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000971 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000972 }
Mike Stump11289f42009-09-09 15:08:12 +0000973
974 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000975 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
976 data_type_ref Data) {
977 unsigned StrLen = strlen(path);
978 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000979 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000980 clang::io::Emit8(Out, DataLen);
981 return std::make_pair(StrLen + 1, DataLen);
982 }
Mike Stump11289f42009-09-09 15:08:12 +0000983
Douglas Gregorc5046832009-04-27 18:38:38 +0000984 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
985 Out.write(path, KeyLen);
986 }
Mike Stump11289f42009-09-09 15:08:12 +0000987
Chris Lattner2a6fa472010-11-23 19:28:12 +0000988 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000989 data_type_ref Data, unsigned DataLen) {
990 using namespace clang::io;
991 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000992
Chris Lattner2a6fa472010-11-23 19:28:12 +0000993 Emit32(Out, (uint32_t) Data.st_ino);
994 Emit32(Out, (uint32_t) Data.st_dev);
995 Emit16(Out, (uint16_t) Data.st_mode);
996 Emit64(Out, (uint64_t) Data.st_mtime);
997 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +0000998
999 assert(Out.tell() - Start == DataLen && "Wrong data length");
1000 }
1001};
1002} // end anonymous namespace
1003
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001004/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001005void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001006 // Build the on-disk hash table containing information about every
1007 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001008 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001009 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001010 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001011 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001012 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1013 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001014 Generator.insert(Filename, Stat->second);
1015 }
Mike Stump11289f42009-09-09 15:08:12 +00001016
Douglas Gregorc5046832009-04-27 18:38:38 +00001017 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001018 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001019 uint32_t BucketOffset;
1020 {
1021 llvm::raw_svector_ostream Out(StatCacheData);
1022 // Make sure that no bucket is at offset 0
1023 clang::io::Emit32(Out, 0);
1024 BucketOffset = Generator.Emit(Out);
1025 }
1026
1027 // Create a blob abbreviation
1028 using namespace llvm;
1029 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001030 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1034 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1035
1036 // Write the stat cache
1037 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001038 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001039 Record.push_back(BucketOffset);
1040 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001041 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001042}
1043
1044//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001045// Source Manager Serialization
1046//===----------------------------------------------------------------------===//
1047
1048/// \brief Create an abbreviation for the SLocEntry that refers to a
1049/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001050static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001051 using namespace llvm;
1052 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001053 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001058 // FileEntry fields.
1059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001061 // HeaderFileInfo fields.
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001067 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001068}
1069
1070/// \brief Create an abbreviation for the SLocEntry that refers to a
1071/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001072static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001073 using namespace llvm;
1074 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001075 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1077 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1078 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1079 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1080 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001081 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001082}
1083
1084/// \brief Create an abbreviation for the SLocEntry that refers to a
1085/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001086static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001087 using namespace llvm;
1088 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001089 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001091 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001092}
1093
1094/// \brief Create an abbreviation for the SLocEntry that refers to an
1095/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001096static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001097 using namespace llvm;
1098 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001099 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1102 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001105 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001106}
1107
1108/// \brief Writes the block containing the serialized form of the
1109/// source manager.
1110///
1111/// TODO: We should probably use an on-disk hash table (stored in a
1112/// blob), indexed based on the file name, so that we only create
1113/// entries for files that we actually need. In the common case (no
1114/// errors), we probably won't have to create file entries for any of
1115/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001116void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001117 const Preprocessor &PP,
1118 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001119 RecordData Record;
1120
Chris Lattner0910e3b2009-04-10 17:16:57 +00001121 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001122 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001123
1124 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001125 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1126 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1127 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1128 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001129
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001130 // Write the line table.
1131 if (SourceMgr.hasLineTable()) {
1132 LineTableInfo &LineTable = SourceMgr.getLineTable();
1133
1134 // Emit the file names
1135 Record.push_back(LineTable.getNumFilenames());
1136 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1137 // Emit the file name
1138 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001139 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001140 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1141 Record.push_back(FilenameLen);
1142 if (FilenameLen)
1143 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1144 }
Mike Stump11289f42009-09-09 15:08:12 +00001145
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001146 // Emit the line entries
1147 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1148 L != LEnd; ++L) {
1149 // Emit the file ID
1150 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001151
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001152 // Emit the line entries
1153 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001154 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001155 LEEnd = L->second.end();
1156 LE != LEEnd; ++LE) {
1157 Record.push_back(LE->FileOffset);
1158 Record.push_back(LE->LineNo);
1159 Record.push_back(LE->FilenameID);
1160 Record.push_back((unsigned)LE->FileKind);
1161 Record.push_back(LE->IncludeOffset);
1162 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001163 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001164 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001165 }
1166
Douglas Gregor258ae542009-04-27 06:38:32 +00001167 // Write out the source location entry table. We skip the first
1168 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001169 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001170 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001171 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1172 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1173 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1174 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001175 // Get this source location entry.
1176 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001177
Douglas Gregor258ae542009-04-27 06:38:32 +00001178 // Record the offset of this source-location entry.
1179 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1180
1181 // Figure out which record code to use.
1182 unsigned Code;
1183 if (SLoc->isFile()) {
1184 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001185 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001186 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001187 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001188 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001189 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001190 Record.clear();
1191 Record.push_back(Code);
1192
1193 Record.push_back(SLoc->getOffset());
1194 if (SLoc->isFile()) {
1195 const SrcMgr::FileInfo &File = SLoc->getFile();
1196 Record.push_back(File.getIncludeLoc().getRawEncoding());
1197 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1198 Record.push_back(File.hasLineDirectives());
1199
1200 const SrcMgr::ContentCache *Content = File.getContentCache();
1201 if (Content->Entry) {
1202 // The source location entry is a file. The blob associated
1203 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001205 // Emit size/modification time for this file.
1206 Record.push_back(Content->Entry->getSize());
1207 Record.push_back(Content->Entry->getModificationTime());
1208
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001209 // Emit header-search information associated with this file.
1210 HeaderFileInfo HFI;
1211 HeaderSearch &HS = PP.getHeaderSearchInfo();
1212 if (Content->Entry->getUID() < HS.header_file_size())
1213 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1214 Record.push_back(HFI.isImport);
1215 Record.push_back(HFI.DirInfo);
1216 Record.push_back(HFI.NumIncludes);
1217 AddIdentifierRef(HFI.ControllingMacro, Record);
1218
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001219 // Turn the file name into an absolute path, if it isn't already.
1220 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001221 llvm::SmallString<128> FilePath(Filename);
1222 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001223 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001224
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001225 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001226 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001227
1228 // FIXME: For now, preload all file source locations, so that
1229 // we get the appropriate File entries in the reader. This is
1230 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001231 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001232 } else {
1233 // The source location entry is a buffer. The blob associated
1234 // with this entry contains the contents of the buffer.
1235
1236 // We add one to the size so that we capture the trailing NULL
1237 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1238 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001239 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001240 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001241 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001242 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1243 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001244 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001245 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001246 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001247 llvm::StringRef(Buffer->getBufferStart(),
1248 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001249
1250 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001251 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001252 }
1253 } else {
1254 // The source location entry is an instantiation.
1255 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1256 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1257 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1258 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1259
1260 // Compute the token length for this macro expansion.
1261 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001262 if (I + 1 != N)
1263 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001264 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1265 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1266 }
1267 }
1268
Douglas Gregor8f45df52009-04-16 22:23:12 +00001269 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001270
1271 if (SLocEntryOffsets.empty())
1272 return;
1273
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001274 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001275 // table is used for lazily loading source-location information.
1276 using namespace llvm;
1277 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001278 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1282 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001283
Douglas Gregor258ae542009-04-27 06:38:32 +00001284 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001285 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001286 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001287 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1288 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001289 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001290 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001291 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001292
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001293 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001294 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001295 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001296}
1297
Douglas Gregorc5046832009-04-27 18:38:38 +00001298//===----------------------------------------------------------------------===//
1299// Preprocessor Serialization
1300//===----------------------------------------------------------------------===//
1301
Chris Lattnereeffaef2009-04-10 17:15:23 +00001302/// \brief Writes the block containing the serialized form of the
1303/// preprocessor.
1304///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001305void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001306 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001307
Chris Lattner0af3ba12009-04-13 01:29:17 +00001308 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1309 if (PP.getCounterValue() != 0) {
1310 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001311 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001312 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001313 }
1314
1315 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001316 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001317
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001318 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001319 // FIXME: use diagnostics subsystem for localization etc.
1320 if (PP.SawDateOrTime())
1321 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001322
Douglas Gregor796d76a2010-10-20 22:00:55 +00001323
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001324 // Loop over all the macro definitions that are live at the end of the file,
1325 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001326 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001327 unsigned InclusionAbbrev = 0;
1328 if (PPRec) {
1329 using namespace llvm;
1330 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1331 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1335 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001339 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001340 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001341
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001342 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1343 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001344 // FIXME: This emits macros in hash table order, we should do it in a stable
1345 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001346 MacroInfo *MI = I->second;
1347
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001348 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001349 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001350 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001351
1352 // FIXME: There is a (probably minor) optimization we could do here, if
1353 // the macro comes from the original PCH but the identifier comes from a
1354 // chained PCH, by storing the offset into the original PCH rather than
1355 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001356 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001357 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001358 continue;
1359
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001360 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001361 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001362 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1363 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001364
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001365 unsigned Code;
1366 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001367 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001368 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001369 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001370
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001371 Record.push_back(MI->isC99Varargs());
1372 Record.push_back(MI->isGNUVarargs());
1373 Record.push_back(MI->getNumArgs());
1374 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1375 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001376 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001377 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001378
Douglas Gregoraae92242010-03-19 21:51:54 +00001379 // If we have a detailed preprocessing record, record the macro definition
1380 // ID that corresponds to this macro.
1381 if (PPRec)
1382 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001383
Douglas Gregor8f45df52009-04-16 22:23:12 +00001384 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001385 Record.clear();
1386
Chris Lattner2199f5b2009-04-10 18:08:30 +00001387 // Emit the tokens array.
1388 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1389 // Note that we know that the preprocessor does not have any annotation
1390 // tokens in it because they are created by the parser, and thus can't be
1391 // in a macro definition.
1392 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001393
Chris Lattner2199f5b2009-04-10 18:08:30 +00001394 Record.push_back(Tok.getLocation().getRawEncoding());
1395 Record.push_back(Tok.getLength());
1396
Chris Lattner2199f5b2009-04-10 18:08:30 +00001397 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1398 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001399 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001400
Chris Lattner2199f5b2009-04-10 18:08:30 +00001401 // FIXME: Should translate token kind to a stable encoding.
1402 Record.push_back(Tok.getKind());
1403 // FIXME: Should translate token flags to a stable encoding.
1404 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001405
Sebastian Redl539c5062010-08-18 23:57:32 +00001406 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001407 Record.clear();
1408 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001409 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001410 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001411
Douglas Gregoraae92242010-03-19 21:51:54 +00001412 // If the preprocessor has a preprocessing record, emit it.
1413 unsigned NumPreprocessingRecords = 0;
1414 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001415 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001416 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1417 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001418 E != EEnd; ++E) {
1419 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001420
Douglas Gregoraae92242010-03-19 21:51:54 +00001421 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1422 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001423 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001424
Douglas Gregor91096292010-10-02 19:29:26 +00001425 // Don't write the macro definition if it is from another AST file.
1426 if (ID < FirstMacroID)
1427 continue;
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001428
1429 // Notify the serialization listener that we're serializing this entity.
1430 if (SerializationListener)
1431 SerializationListener->SerializedPreprocessedEntity(*E,
1432 Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001433
Douglas Gregor91096292010-10-02 19:29:26 +00001434 unsigned Position = ID - FirstMacroID;
1435 if (Position != MacroDefinitionOffsets.size()) {
1436 if (Position > MacroDefinitionOffsets.size())
1437 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001438
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001439 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001440 } else
1441 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001442
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001443 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001444 Record.push_back(ID);
1445 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1446 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1447 AddIdentifierRef(MD->getName(), Record);
1448 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001449 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001450 continue;
1451 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001452
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001453 // Notify the serialization listener that we're serializing this entity.
1454 if (SerializationListener)
1455 SerializationListener->SerializedPreprocessedEntity(*E,
1456 Stream.GetCurrentBitNo());
1457
1458 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1459 Record.push_back(IndexBase + NumPreprocessingRecords++);
1460 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1461 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1462 AddIdentifierRef(MI->getName(), Record);
1463 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1464 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1465 continue;
1466 }
1467
Douglas Gregor796d76a2010-10-20 22:00:55 +00001468 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1469 Record.push_back(PP_INCLUSION_DIRECTIVE);
1470 Record.push_back(IndexBase + NumPreprocessingRecords++);
1471 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1472 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1473 Record.push_back(ID->getFileName().size());
1474 Record.push_back(ID->wasInQuotes());
1475 Record.push_back(static_cast<unsigned>(ID->getKind()));
1476 llvm::SmallString<64> Buffer;
1477 Buffer += ID->getFileName();
1478 Buffer += ID->getFile()->getName();
1479 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1480 continue;
1481 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001482
1483 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregoraae92242010-03-19 21:51:54 +00001484 }
1485 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001486
Douglas Gregor8f45df52009-04-16 22:23:12 +00001487 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001488
Douglas Gregoraae92242010-03-19 21:51:54 +00001489 // Write the offsets table for the preprocessing record.
1490 if (NumPreprocessingRecords > 0) {
1491 // Write the offsets table for identifier IDs.
1492 using namespace llvm;
1493 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001494 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1497 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1498 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001499
Douglas Gregoraae92242010-03-19 21:51:54 +00001500 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001501 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001502 Record.push_back(NumPreprocessingRecords);
1503 Record.push_back(MacroDefinitionOffsets.size());
1504 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001505 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001506 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1507 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001508}
1509
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001510void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001511 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001512 for (Diagnostic::DiagStatePointsTy::const_iterator
1513 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1514 I != E; ++I) {
1515 const Diagnostic::DiagStatePoint &point = *I;
1516 if (point.Loc.isInvalid())
1517 continue;
1518
1519 Record.push_back(point.Loc.getRawEncoding());
1520 for (Diagnostic::DiagState::iterator
1521 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1522 unsigned diag = I->first, map = I->second;
1523 if (map & 0x10) { // mapping from a diagnostic pragma.
1524 Record.push_back(diag);
1525 Record.push_back(map & 0x7);
1526 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001527 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001528 Record.push_back(-1); // mark the end of the diag/map pairs for this
1529 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001530 }
1531
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001532 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001533 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001534}
1535
Douglas Gregorc5046832009-04-27 18:38:38 +00001536//===----------------------------------------------------------------------===//
1537// Type Serialization
1538//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001539
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001540/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001541void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001542 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001543 if (Idx.getIndex() == 0) // we haven't seen this type before.
1544 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001545
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001546 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001547
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001548 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001549 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001550 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001551 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001552 else if (TypeOffsets.size() < Index) {
1553 TypeOffsets.resize(Index + 1);
1554 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001555 }
1556
1557 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001558
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001559 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001560 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001561
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001562 if (T.hasLocalNonFastQualifiers()) {
1563 Qualifiers Qs = T.getLocalQualifiers();
1564 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001565 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001566 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001567 } else {
1568 switch (T->getTypeClass()) {
1569 // For all of the concrete, non-dependent types, call the
1570 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001571#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001572 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001573#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001574#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001575 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001576 }
1577
1578 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001579 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001580
1581 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001582 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001583}
1584
Douglas Gregorc5046832009-04-27 18:38:38 +00001585//===----------------------------------------------------------------------===//
1586// Declaration Serialization
1587//===----------------------------------------------------------------------===//
1588
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001589/// \brief Write the block containing all of the declaration IDs
1590/// lexically declared within the given DeclContext.
1591///
1592/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1593/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001594uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001595 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001596 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001597 return 0;
1598
Douglas Gregor8f45df52009-04-16 22:23:12 +00001599 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001600 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001601 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001602 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001603 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1604 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001605 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001606
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001607 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001608 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001609 reinterpret_cast<char*>(Decls.data()),
1610 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001611 return Offset;
1612}
1613
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001614void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001615 using namespace llvm;
1616 RecordData Record;
1617
1618 // Write the type offsets array
1619 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001620 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1623 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1624 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001625 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001626 Record.push_back(TypeOffsets.size());
1627 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001628 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001629 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1630
1631 // Write the declaration offsets array
1632 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001633 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1636 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1637 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001638 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001639 Record.push_back(DeclOffsets.size());
1640 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001641 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001642 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1643}
1644
Douglas Gregorc5046832009-04-27 18:38:38 +00001645//===----------------------------------------------------------------------===//
1646// Global Method Pool and Selector Serialization
1647//===----------------------------------------------------------------------===//
1648
Douglas Gregore84a9da2009-04-20 20:36:09 +00001649namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001650// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001651class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001652 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001653
1654public:
1655 typedef Selector key_type;
1656 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001657
Sebastian Redl834bb972010-08-04 17:20:04 +00001658 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001659 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001660 ObjCMethodList Instance, Factory;
1661 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001662 typedef const data_type& data_type_ref;
1663
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001664 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001665
Douglas Gregorc78d3462009-04-24 21:10:55 +00001666 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001667 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001668 }
Mike Stump11289f42009-09-09 15:08:12 +00001669
1670 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001671 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1672 data_type_ref Methods) {
1673 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1674 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001675 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1676 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001677 Method = Method->Next)
1678 if (Method->Method)
1679 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001680 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001681 Method = Method->Next)
1682 if (Method->Method)
1683 DataLen += 4;
1684 clang::io::Emit16(Out, DataLen);
1685 return std::make_pair(KeyLen, DataLen);
1686 }
Mike Stump11289f42009-09-09 15:08:12 +00001687
Douglas Gregor95c13f52009-04-25 17:48:32 +00001688 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001689 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001690 assert((Start >> 32) == 0 && "Selector key offset too large");
1691 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001692 unsigned N = Sel.getNumArgs();
1693 clang::io::Emit16(Out, N);
1694 if (N == 0)
1695 N = 1;
1696 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001697 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001698 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1699 }
Mike Stump11289f42009-09-09 15:08:12 +00001700
Douglas Gregorc78d3462009-04-24 21:10:55 +00001701 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001702 data_type_ref Methods, unsigned DataLen) {
1703 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001704 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001705 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001706 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001707 Method = Method->Next)
1708 if (Method->Method)
1709 ++NumInstanceMethods;
1710
1711 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001712 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001713 Method = Method->Next)
1714 if (Method->Method)
1715 ++NumFactoryMethods;
1716
1717 clang::io::Emit16(Out, NumInstanceMethods);
1718 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001719 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001720 Method = Method->Next)
1721 if (Method->Method)
1722 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001723 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001724 Method = Method->Next)
1725 if (Method->Method)
1726 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001727
1728 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001729 }
1730};
1731} // end anonymous namespace
1732
Sebastian Redla19a67f2010-08-03 21:58:15 +00001733/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001734///
1735/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001736/// in an on-disk hash table indexed by the selector. The hash table also
1737/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001738void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001739 using namespace llvm;
1740
Sebastian Redla19a67f2010-08-03 21:58:15 +00001741 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001742 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001743 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001744 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001745 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001746 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001747 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001748 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001749
Sebastian Redla19a67f2010-08-03 21:58:15 +00001750 // Create the on-disk hash table representation. We walk through every
1751 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001752 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001753 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001754 I = SelectorIDs.begin(), E = SelectorIDs.end();
1755 I != E; ++I) {
1756 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001757 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001758 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001759 I->second,
1760 ObjCMethodList(),
1761 ObjCMethodList()
1762 };
1763 if (F != SemaRef.MethodPool.end()) {
1764 Data.Instance = F->second.first;
1765 Data.Factory = F->second.second;
1766 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001767 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001768 // changed.
1769 if (Chain && I->second < FirstSelectorID) {
1770 // Selector already exists. Did it change?
1771 bool changed = false;
1772 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1773 M = M->Next) {
1774 if (M->Method->getPCHLevel() == 0)
1775 changed = true;
1776 }
1777 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1778 M = M->Next) {
1779 if (M->Method->getPCHLevel() == 0)
1780 changed = true;
1781 }
1782 if (!changed)
1783 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001784 } else if (Data.Instance.Method || Data.Factory.Method) {
1785 // A new method pool entry.
1786 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001787 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001788 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001789 }
1790
Douglas Gregorc78d3462009-04-24 21:10:55 +00001791 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001792 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001793 uint32_t BucketOffset;
1794 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001795 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001796 llvm::raw_svector_ostream Out(MethodPool);
1797 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001798 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001799 BucketOffset = Generator.Emit(Out, Trait);
1800 }
1801
1802 // Create a blob abbreviation
1803 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001804 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1808 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1809
Douglas Gregor95c13f52009-04-25 17:48:32 +00001810 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001811 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001812 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001813 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001814 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001815 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001816
1817 // Create a blob abbreviation for the selector table offsets.
1818 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001819 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001820 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001821 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1822 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1823
1824 // Write the selector offsets table.
1825 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001826 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001827 Record.push_back(SelectorOffsets.size());
1828 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001829 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001830 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001831 }
1832}
1833
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001834/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001835void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001836 using namespace llvm;
1837 if (SemaRef.ReferencedSelectors.empty())
1838 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001839
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001840 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001841
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001842 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001843 // very tricky to fix, and given that @selector shouldn't really appear in
1844 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001845 for (DenseMap<Selector, SourceLocation>::iterator S =
1846 SemaRef.ReferencedSelectors.begin(),
1847 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1848 Selector Sel = (*S).first;
1849 SourceLocation Loc = (*S).second;
1850 AddSelectorRef(Sel, Record);
1851 AddSourceLocation(Loc, Record);
1852 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001853 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001854}
1855
Douglas Gregorc5046832009-04-27 18:38:38 +00001856//===----------------------------------------------------------------------===//
1857// Identifier Table Serialization
1858//===----------------------------------------------------------------------===//
1859
Douglas Gregorc78d3462009-04-24 21:10:55 +00001860namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001861class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001862 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001863 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001864
Douglas Gregor1d583f22009-04-28 21:18:29 +00001865 /// \brief Determines whether this is an "interesting" identifier
1866 /// that needs a full IdentifierInfo structure written into the hash
1867 /// table.
1868 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1869 return II->isPoisoned() ||
1870 II->isExtensionToken() ||
1871 II->hasMacroDefinition() ||
1872 II->getObjCOrBuiltinID() ||
1873 II->getFETokenInfo<void>();
1874 }
1875
Douglas Gregore84a9da2009-04-20 20:36:09 +00001876public:
1877 typedef const IdentifierInfo* key_type;
1878 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001879
Sebastian Redl539c5062010-08-18 23:57:32 +00001880 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001881 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001882
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001883 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001884 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001885
1886 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001887 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001888 }
Mike Stump11289f42009-09-09 15:08:12 +00001889
1890 std::pair<unsigned,unsigned>
1891 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001892 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001893 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001894 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1895 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001896 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001897 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001898 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001899 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001900 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1901 DEnd = IdentifierResolver::end();
1902 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001903 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001904 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001905 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001906 // We emit the key length after the data length so that every
1907 // string is preceded by a 16-bit length. This matches the PTH
1908 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001909 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001910 return std::make_pair(KeyLen, DataLen);
1911 }
Mike Stump11289f42009-09-09 15:08:12 +00001912
1913 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001914 unsigned KeyLen) {
1915 // Record the location of the key data. This is used when generating
1916 // the mapping from persistent IDs to strings.
1917 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001918 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001919 }
Mike Stump11289f42009-09-09 15:08:12 +00001920
1921 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001922 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001923 if (!isInterestingIdentifier(II)) {
1924 clang::io::Emit32(Out, ID << 1);
1925 return;
1926 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001927
Douglas Gregor1d583f22009-04-28 21:18:29 +00001928 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001929 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001930 bool hasMacroDefinition =
1931 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001932 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001933 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001934 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1935 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1936 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001937 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001938 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001939 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001940
Douglas Gregorc3366a52009-04-21 23:56:24 +00001941 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001942 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001943
Douglas Gregora868bbd2009-04-21 22:25:48 +00001944 // Emit the declaration IDs in reverse order, because the
1945 // IdentifierResolver provides the declarations as they would be
1946 // visible (e.g., the function "stat" would come before the struct
1947 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1948 // adds declarations to the end of the list (so we need to see the
1949 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001950 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001951 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001952 IdentifierResolver::end());
1953 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1954 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001955 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001956 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001957 }
1958};
1959} // end anonymous namespace
1960
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001961/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001962///
1963/// The identifier table consists of a blob containing string data
1964/// (the actual identifiers themselves) and a separate "offsets" index
1965/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001966void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001967 using namespace llvm;
1968
1969 // Create and write out the blob that contains the identifier
1970 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001971 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001972 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001973 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001974
Douglas Gregore6648fb2009-04-28 20:33:11 +00001975 // Look for any identifiers that were named while processing the
1976 // headers, but are otherwise not needed. We add these to the hash
1977 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001978 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001979 // file.
1980 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1981 IDEnd = PP.getIdentifierTable().end();
1982 ID != IDEnd; ++ID)
1983 getIdentifierRef(ID->second);
1984
Sebastian Redlff4a2952010-07-23 23:49:55 +00001985 // Create the on-disk hash table representation. We only store offsets
1986 // for identifiers that appear here for the first time.
1987 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001988 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001989 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1990 ID != IDEnd; ++ID) {
1991 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001992 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001993 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001994 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001995
Douglas Gregore84a9da2009-04-20 20:36:09 +00001996 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001997 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001998 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001999 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002000 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002001 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002002 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002003 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002004 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002005 }
2006
2007 // Create a blob abbreviation
2008 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002009 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002012 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002013
2014 // Write the identifier table
2015 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002016 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002017 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002018 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002019 }
2020
2021 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002022 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002023 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2026 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2027
2028 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002029 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002030 Record.push_back(IdentifierOffsets.size());
2031 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002032 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002033 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002034}
2035
Douglas Gregorc5046832009-04-27 18:38:38 +00002036//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002037// DeclContext's Name Lookup Table Serialization
2038//===----------------------------------------------------------------------===//
2039
2040namespace {
2041// Trait used for the on-disk hash table used in the method pool.
2042class ASTDeclContextNameLookupTrait {
2043 ASTWriter &Writer;
2044
2045public:
2046 typedef DeclarationName key_type;
2047 typedef key_type key_type_ref;
2048
2049 typedef DeclContext::lookup_result data_type;
2050 typedef const data_type& data_type_ref;
2051
2052 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2053
2054 unsigned ComputeHash(DeclarationName Name) {
2055 llvm::FoldingSetNodeID ID;
2056 ID.AddInteger(Name.getNameKind());
2057
2058 switch (Name.getNameKind()) {
2059 case DeclarationName::Identifier:
2060 ID.AddString(Name.getAsIdentifierInfo()->getName());
2061 break;
2062 case DeclarationName::ObjCZeroArgSelector:
2063 case DeclarationName::ObjCOneArgSelector:
2064 case DeclarationName::ObjCMultiArgSelector:
2065 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2066 break;
2067 case DeclarationName::CXXConstructorName:
2068 case DeclarationName::CXXDestructorName:
2069 case DeclarationName::CXXConversionFunctionName:
2070 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2071 break;
2072 case DeclarationName::CXXOperatorName:
2073 ID.AddInteger(Name.getCXXOverloadedOperator());
2074 break;
2075 case DeclarationName::CXXLiteralOperatorName:
2076 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2077 case DeclarationName::CXXUsingDirective:
2078 break;
2079 }
2080
2081 return ID.ComputeHash();
2082 }
2083
2084 std::pair<unsigned,unsigned>
2085 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2086 data_type_ref Lookup) {
2087 unsigned KeyLen = 1;
2088 switch (Name.getNameKind()) {
2089 case DeclarationName::Identifier:
2090 case DeclarationName::ObjCZeroArgSelector:
2091 case DeclarationName::ObjCOneArgSelector:
2092 case DeclarationName::ObjCMultiArgSelector:
2093 case DeclarationName::CXXConstructorName:
2094 case DeclarationName::CXXDestructorName:
2095 case DeclarationName::CXXConversionFunctionName:
2096 case DeclarationName::CXXLiteralOperatorName:
2097 KeyLen += 4;
2098 break;
2099 case DeclarationName::CXXOperatorName:
2100 KeyLen += 1;
2101 break;
2102 case DeclarationName::CXXUsingDirective:
2103 break;
2104 }
2105 clang::io::Emit16(Out, KeyLen);
2106
2107 // 2 bytes for num of decls and 4 for each DeclID.
2108 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2109 clang::io::Emit16(Out, DataLen);
2110
2111 return std::make_pair(KeyLen, DataLen);
2112 }
2113
2114 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2115 using namespace clang::io;
2116
2117 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2118 Emit8(Out, Name.getNameKind());
2119 switch (Name.getNameKind()) {
2120 case DeclarationName::Identifier:
2121 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2122 break;
2123 case DeclarationName::ObjCZeroArgSelector:
2124 case DeclarationName::ObjCOneArgSelector:
2125 case DeclarationName::ObjCMultiArgSelector:
2126 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2127 break;
2128 case DeclarationName::CXXConstructorName:
2129 case DeclarationName::CXXDestructorName:
2130 case DeclarationName::CXXConversionFunctionName:
2131 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2132 break;
2133 case DeclarationName::CXXOperatorName:
2134 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2135 Emit8(Out, Name.getCXXOverloadedOperator());
2136 break;
2137 case DeclarationName::CXXLiteralOperatorName:
2138 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2139 break;
2140 case DeclarationName::CXXUsingDirective:
2141 break;
2142 }
2143 }
2144
2145 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2146 data_type Lookup, unsigned DataLen) {
2147 uint64_t Start = Out.tell(); (void)Start;
2148 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2149 for (; Lookup.first != Lookup.second; ++Lookup.first)
2150 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2151
2152 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2153 }
2154};
2155} // end anonymous namespace
2156
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002157/// \brief Write the block containing all of the declaration IDs
2158/// visible from the given DeclContext.
2159///
2160/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002161/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002162uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2163 DeclContext *DC) {
2164 if (DC->getPrimaryContext() != DC)
2165 return 0;
2166
2167 // Since there is no name lookup into functions or methods, don't bother to
2168 // build a visible-declarations table for these entities.
2169 if (DC->isFunctionOrMethod())
2170 return 0;
2171
2172 // If not in C++, we perform name lookup for the translation unit via the
2173 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2174 // FIXME: In C++ we need the visible declarations in order to "see" the
2175 // friend declarations, is there a way to do this without writing the table ?
2176 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2177 return 0;
2178
2179 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002180 if (DC->hasExternalVisibleStorage())
2181 DC->MaterializeVisibleDeclsFromExternalStorage();
2182 else
2183 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002184
2185 // Serialize the contents of the mapping used for lookup. Note that,
2186 // although we have two very different code paths, the serialized
2187 // representation is the same for both cases: a declaration name,
2188 // followed by a size, followed by references to the visible
2189 // declarations that have that name.
2190 uint64_t Offset = Stream.GetCurrentBitNo();
2191 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2192 if (!Map || Map->empty())
2193 return 0;
2194
2195 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2196 ASTDeclContextNameLookupTrait Trait(*this);
2197
2198 // Create the on-disk hash table representation.
2199 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2200 D != DEnd; ++D) {
2201 DeclarationName Name = D->first;
2202 DeclContext::lookup_result Result = D->second.getLookupResult();
2203 Generator.insert(Name, Result, Trait);
2204 }
2205
2206 // Create the on-disk hash table in a buffer.
2207 llvm::SmallString<4096> LookupTable;
2208 uint32_t BucketOffset;
2209 {
2210 llvm::raw_svector_ostream Out(LookupTable);
2211 // Make sure that no bucket is at offset 0
2212 clang::io::Emit32(Out, 0);
2213 BucketOffset = Generator.Emit(Out, Trait);
2214 }
2215
2216 // Write the lookup table
2217 RecordData Record;
2218 Record.push_back(DECL_CONTEXT_VISIBLE);
2219 Record.push_back(BucketOffset);
2220 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2221 LookupTable.str());
2222
2223 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2224 ++NumVisibleDeclContexts;
2225 return Offset;
2226}
2227
Sebastian Redla4071b42010-08-24 00:50:09 +00002228/// \brief Write an UPDATE_VISIBLE block for the given context.
2229///
2230/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2231/// DeclContext in a dependent AST file. As such, they only exist for the TU
2232/// (in C++) and for namespaces.
2233void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002234 // Make the context build its lookup table, but don't make it load external
2235 // decls.
2236 DC->lookup(DeclarationName());
2237
2238 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2239 if (!Map || Map->empty())
2240 return;
2241
2242 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2243 ASTDeclContextNameLookupTrait Trait(*this);
2244
2245 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002246 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2247 D != DEnd; ++D) {
2248 DeclarationName Name = D->first;
2249 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002250 // For any name that appears in this table, the results are complete, i.e.
2251 // they overwrite results from previous PCHs. Merging is always a mess.
2252 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002253 }
2254
2255 // Create the on-disk hash table in a buffer.
2256 llvm::SmallString<4096> LookupTable;
2257 uint32_t BucketOffset;
2258 {
2259 llvm::raw_svector_ostream Out(LookupTable);
2260 // Make sure that no bucket is at offset 0
2261 clang::io::Emit32(Out, 0);
2262 BucketOffset = Generator.Emit(Out, Trait);
2263 }
2264
2265 // Write the lookup table
2266 RecordData Record;
2267 Record.push_back(UPDATE_VISIBLE);
2268 Record.push_back(getDeclID(cast<Decl>(DC)));
2269 Record.push_back(BucketOffset);
2270 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2271}
2272
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002273//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002274// General Serialization Routines
2275//===----------------------------------------------------------------------===//
2276
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002277/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002278void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002279 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002280 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2281 const Attr * A = *i;
2282 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2283 AddSourceLocation(A->getLocation(), Record);
2284 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002285
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002286#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002287
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002288 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002289}
2290
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002291void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002292 Record.push_back(Str.size());
2293 Record.insert(Record.end(), Str.begin(), Str.end());
2294}
2295
Douglas Gregore84a9da2009-04-20 20:36:09 +00002296/// \brief Note that the identifier II occurs at the given offset
2297/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002298void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002299 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002300 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002301 // up earlier in the chain and thus don't need an offset.
2302 if (ID >= FirstIdentID)
2303 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002304}
2305
Douglas Gregor95c13f52009-04-25 17:48:32 +00002306/// \brief Note that the selector Sel occurs at the given offset
2307/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002308void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002309 unsigned ID = SelectorIDs[Sel];
2310 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002311 // Don't record offsets for selectors that are also available in a different
2312 // file.
2313 if (ID < FirstSelectorID)
2314 return;
2315 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002316}
2317
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002318ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002319 : Stream(Stream), Chain(0), SerializationListener(0),
2320 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002321 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002322 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002323 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2324 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002325 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002326 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2327 NextCXXBaseSpecifiersID(1)
2328{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002329}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002330
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002331void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002332 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002333 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002334 Stream.Emit((unsigned)'C', 8);
2335 Stream.Emit((unsigned)'P', 8);
2336 Stream.Emit((unsigned)'C', 8);
2337 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002338
Chris Lattner28fa4e62009-04-26 22:26:21 +00002339 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002340
Sebastian Redl143413f2010-07-12 22:02:52 +00002341 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002342 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002343 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002344 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002345}
2346
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002347void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002348 const char *isysroot) {
2349 using namespace llvm;
2350
2351 ASTContext &Context = SemaRef.Context;
2352 Preprocessor &PP = SemaRef.PP;
2353
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002354 // The translation unit is the first declaration we'll emit.
2355 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002356 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002357 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002358
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002359 // Make sure that we emit IdentifierInfos (and any attached
2360 // declarations) for builtins.
2361 {
2362 IdentifierTable &Table = PP.getIdentifierTable();
2363 llvm::SmallVector<const char *, 32> BuiltinNames;
2364 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2365 Context.getLangOptions().NoBuiltin);
2366 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2367 getIdentifierRef(&Table.get(BuiltinNames[I]));
2368 }
2369
Chris Lattner0c797362009-09-08 18:19:27 +00002370 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002371 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002372 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002373 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002374 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2375 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002376 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002377
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002378 // Build a record containing all of the file scoped decls in this file.
2379 RecordData UnusedFileScopedDecls;
2380 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2381 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002382
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002383 RecordData WeakUndeclaredIdentifiers;
2384 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2385 WeakUndeclaredIdentifiers.push_back(
2386 SemaRef.WeakUndeclaredIdentifiers.size());
2387 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2388 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2389 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2390 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2391 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2392 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2393 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2394 }
2395 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002396
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002397 // Build a record containing all of the locally-scoped external
2398 // declarations in this header file. Generally, this record will be
2399 // empty.
2400 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002401 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002402 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002403 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002404 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2405 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2406 TD != TDEnd; ++TD)
2407 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2408
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002409 // Build a record containing all of the ext_vector declarations.
2410 RecordData ExtVectorDecls;
2411 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2412 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2413
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002414 // Build a record containing all of the VTable uses information.
2415 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002416 if (!SemaRef.VTableUses.empty()) {
2417 VTableUses.push_back(SemaRef.VTableUses.size());
2418 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2419 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2420 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2421 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2422 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002423 }
2424
2425 // Build a record containing all of dynamic classes declarations.
2426 RecordData DynamicClasses;
2427 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2428 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2429
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002430 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002431 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002432 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002433 I = SemaRef.PendingInstantiations.begin(),
2434 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2435 AddDeclRef(I->first, PendingInstantiations);
2436 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002437 }
2438 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2439 "There are local ones at end of translation unit!");
2440
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002441 // Build a record containing some declaration references.
2442 RecordData SemaDeclRefs;
2443 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2444 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2445 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2446 }
2447
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002448 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002449 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002450 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002451 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002452 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002453 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002454 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002455 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002456 // Write the record of special types.
2457 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002458
Steve Naroffc277ad12009-07-18 15:33:26 +00002459 AddTypeRef(Context.getBuiltinVaListType(), Record);
2460 AddTypeRef(Context.getObjCIdType(), Record);
2461 AddTypeRef(Context.getObjCSelType(), Record);
2462 AddTypeRef(Context.getObjCProtoType(), Record);
2463 AddTypeRef(Context.getObjCClassType(), Record);
2464 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2465 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2466 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002467 AddTypeRef(Context.getjmp_bufType(), Record);
2468 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002469 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2470 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002471 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002472 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002473 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2474 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002475 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002476 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002477
Douglas Gregor1970d882009-04-26 03:49:13 +00002478 // Keep writing types and declarations until all types and
2479 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002480 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002481 WriteDeclsBlockAbbrevs();
2482 while (!DeclTypesToEmit.empty()) {
2483 DeclOrType DOT = DeclTypesToEmit.front();
2484 DeclTypesToEmit.pop();
2485 if (DOT.isType())
2486 WriteType(DOT.getType());
2487 else
2488 WriteDecl(Context, DOT.getDecl());
2489 }
2490 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002491
Douglas Gregor45053152009-10-17 17:25:45 +00002492 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002493 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002494 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002495 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002496
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002497 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002498 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002499
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002500 // Write the C++ base-specifier set offsets.
2501 if (!CXXBaseSpecifiersOffsets.empty()) {
2502 // Create a blob abbreviation for the C++ base specifiers offsets.
2503 using namespace llvm;
2504
2505 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2506 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2507 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2508 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2509 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2510
2511 // Write the selector offsets table.
2512 Record.clear();
2513 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2514 Record.push_back(CXXBaseSpecifiersOffsets.size());
2515 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2516 (const char *)CXXBaseSpecifiersOffsets.data(),
2517 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2518 }
2519
Douglas Gregord4df8652009-04-22 22:02:47 +00002520 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002521 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002522 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002523
2524 // Write the record containing tentative definitions.
2525 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002526 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002527
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002528 // Write the record containing unused file scoped decls.
2529 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002530 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002531
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002532 // Write the record containing weak undeclared identifiers.
2533 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002534 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002535 WeakUndeclaredIdentifiers);
2536
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002537 // Write the record containing locally-scoped external definitions.
2538 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002539 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002540 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002541
2542 // Write the record containing ext_vector type names.
2543 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002544 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002545
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002546 // Write the record containing VTable uses information.
2547 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002548 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002549
2550 // Write the record containing dynamic classes declarations.
2551 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002552 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002553
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002554 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002555 if (!PendingInstantiations.empty())
2556 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002557
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002558 // Write the record containing declaration references of Sema.
2559 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002560 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002561
Douglas Gregor08f01292009-04-17 22:13:46 +00002562 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002563 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002564 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002565 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002566 Record.push_back(NumLexicalDeclContexts);
2567 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002568 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002569 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002570}
2571
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002572void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002573 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002574 using namespace llvm;
2575
2576 ASTContext &Context = SemaRef.Context;
2577 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002578
Sebastian Redl143413f2010-07-12 22:02:52 +00002579 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002580 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002581 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002582 if (StatCalls && !isysroot)
2583 WriteStatCache(*StatCalls);
2584 // FIXME: Source manager block should only write new stuff, which could be
2585 // done by tracking the largest ID in the chain
2586 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002587
2588 // The special types are in the chained PCH.
2589
2590 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002591 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002592 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002593 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002594 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2595 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002596 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002597 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002598 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002599 else if ((*I)->isChangedSinceDeserialization())
2600 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002601 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002602 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002603 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002604 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002605 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2606 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2607 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002608 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002609 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2610 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002611 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002612 // And a visible updates block for the DeclContexts.
2613 Abv = new llvm::BitCodeAbbrev();
2614 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2615 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2616 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2617 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2618 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2619 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002620
Sebastian Redl98912122010-07-27 23:01:28 +00002621 // Build a record containing all of the new tentative definitions in this
2622 // file, in TentativeDefinitions order.
2623 RecordData TentativeDefinitions;
2624 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2625 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2626 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2627 }
2628
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002629 // Build a record containing all of the file scoped decls in this file.
2630 RecordData UnusedFileScopedDecls;
2631 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2632 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2633 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002634 }
2635
Sebastian Redl08aca90252010-08-05 18:21:25 +00002636 // We write the entire table, overwriting the tables from the chain.
2637 RecordData WeakUndeclaredIdentifiers;
2638 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2639 WeakUndeclaredIdentifiers.push_back(
2640 SemaRef.WeakUndeclaredIdentifiers.size());
2641 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2642 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2643 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2644 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2645 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2646 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2647 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2648 }
2649 }
2650
Sebastian Redl98912122010-07-27 23:01:28 +00002651 // Build a record containing all of the locally-scoped external
2652 // declarations in this header file. Generally, this record will be
2653 // empty.
2654 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002655 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002656 // nondeterminstic!
2657 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2658 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2659 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2660 TD != TDEnd; ++TD) {
2661 if (TD->second->getPCHLevel() == 0)
2662 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2663 }
2664
2665 // Build a record containing all of the ext_vector declarations.
2666 RecordData ExtVectorDecls;
2667 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2668 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2669 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2670 }
2671
Sebastian Redl08aca90252010-08-05 18:21:25 +00002672 // Build a record containing all of the VTable uses information.
2673 // We write everything here, because it's too hard to determine whether
2674 // a use is new to this part.
2675 RecordData VTableUses;
2676 if (!SemaRef.VTableUses.empty()) {
2677 VTableUses.push_back(SemaRef.VTableUses.size());
2678 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2679 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2680 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2681 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2682 }
2683 }
2684
2685 // Build a record containing all of dynamic classes declarations.
2686 RecordData DynamicClasses;
2687 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2688 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2689 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2690
2691 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002692 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002693 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002694 I = SemaRef.PendingInstantiations.begin(),
2695 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002696 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002697 AddDeclRef(I->first, PendingInstantiations);
2698 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002699 }
2700 }
2701 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2702 "There are local ones at end of translation unit!");
2703
2704 // Build a record containing some declaration references.
2705 // It's not worth the effort to avoid duplication here.
2706 RecordData SemaDeclRefs;
2707 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2708 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2709 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2710 }
2711
Sebastian Redl539c5062010-08-18 23:57:32 +00002712 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002713 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002714 for (DeclsToRewriteTy::iterator
2715 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2716 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002717 while (!DeclTypesToEmit.empty()) {
2718 DeclOrType DOT = DeclTypesToEmit.front();
2719 DeclTypesToEmit.pop();
2720 if (DOT.isType())
2721 WriteType(DOT.getType());
2722 else
2723 WriteDecl(Context, DOT.getDecl());
2724 }
2725 Stream.ExitBlock();
2726
Sebastian Redl98912122010-07-27 23:01:28 +00002727 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002728 WriteSelectors(SemaRef);
2729 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002730 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002731 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002732 // FIXME: For chained PCH only write the new mappings (we currently
2733 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002734 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002735
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002736 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002737 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002738 RecordData FirstLatestDeclIDs;
2739 for (FirstLatestDeclMap::iterator
2740 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2741 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2742 "Expected first & second to be in different PCHs");
2743 AddDeclRef(I->first, FirstLatestDeclIDs);
2744 AddDeclRef(I->second, FirstLatestDeclIDs);
2745 }
2746 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002747 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002748
Sebastian Redl98912122010-07-27 23:01:28 +00002749 // Write the record containing external, unnamed definitions.
2750 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002751 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002752
2753 // Write the record containing tentative definitions.
2754 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002755 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002756
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002757 // Write the record containing unused file scoped decls.
2758 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002759 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002760
Sebastian Redl08aca90252010-08-05 18:21:25 +00002761 // Write the record containing weak undeclared identifiers.
2762 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002763 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002764 WeakUndeclaredIdentifiers);
2765
Sebastian Redl98912122010-07-27 23:01:28 +00002766 // Write the record containing locally-scoped external definitions.
2767 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002768 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002769 LocallyScopedExternalDecls);
2770
2771 // Write the record containing ext_vector type names.
2772 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002773 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002774
Sebastian Redl08aca90252010-08-05 18:21:25 +00002775 // Write the record containing VTable uses information.
2776 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002777 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002778
2779 // Write the record containing dynamic classes declarations.
2780 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002781 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002782
2783 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002784 if (!PendingInstantiations.empty())
2785 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002786
2787 // Write the record containing declaration references of Sema.
2788 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002789 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002790
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002791 // Write the updates to DeclContexts.
2792 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2793 I = UpdatedDeclContexts.begin(),
2794 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002795 I != E; ++I)
2796 WriteDeclContextVisibleUpdate(*I);
2797
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002798 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002799
Sebastian Redl98912122010-07-27 23:01:28 +00002800 Record.clear();
2801 Record.push_back(NumStatements);
2802 Record.push_back(NumMacros);
2803 Record.push_back(NumLexicalDeclContexts);
2804 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002805 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002806 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002807 Stream.ExitBlock();
2808}
2809
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002810void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002811 if (DeclUpdates.empty())
2812 return;
2813
2814 RecordData OffsetsRecord;
2815 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2816 for (DeclUpdateMap::iterator
2817 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2818 const Decl *D = I->first;
2819 UpdateRecord &URec = I->second;
2820
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002821 if (DeclsToRewrite.count(D))
2822 continue; // The decl will be written completely,no need to store updates.
2823
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002824 uint64_t Offset = Stream.GetCurrentBitNo();
2825 Stream.EmitRecord(DECL_UPDATES, URec);
2826
2827 OffsetsRecord.push_back(GetDeclRef(D));
2828 OffsetsRecord.push_back(Offset);
2829 }
2830 Stream.ExitBlock();
2831 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2832}
2833
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002834void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002835 if (ReplacedDecls.empty())
2836 return;
2837
2838 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002839 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002840 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2841 Record.push_back(I->first);
2842 Record.push_back(I->second);
2843 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002844 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002845}
2846
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002847void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002848 Record.push_back(Loc.getRawEncoding());
2849}
2850
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002851void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002852 AddSourceLocation(Range.getBegin(), Record);
2853 AddSourceLocation(Range.getEnd(), Record);
2854}
2855
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002856void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002857 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002858 const uint64_t *Words = Value.getRawData();
2859 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002860}
2861
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002862void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002863 Record.push_back(Value.isUnsigned());
2864 AddAPInt(Value, Record);
2865}
2866
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002867void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002868 AddAPInt(Value.bitcastToAPInt(), Record);
2869}
2870
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002871void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002872 Record.push_back(getIdentifierRef(II));
2873}
2874
Sebastian Redl539c5062010-08-18 23:57:32 +00002875IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002876 if (II == 0)
2877 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002878
Sebastian Redl539c5062010-08-18 23:57:32 +00002879 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002880 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002881 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002882 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002883}
2884
Sebastian Redl50e26582010-09-15 19:54:06 +00002885MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002886 if (MD == 0)
2887 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002888
2889 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002890 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002891 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002892 return ID;
2893}
2894
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002895void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002896 Record.push_back(getSelectorRef(SelRef));
2897}
2898
Sebastian Redl539c5062010-08-18 23:57:32 +00002899SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002900 if (Sel.getAsOpaquePtr() == 0) {
2901 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002902 }
2903
Sebastian Redl539c5062010-08-18 23:57:32 +00002904 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002905 if (SID == 0 && Chain) {
2906 // This might trigger a ReadSelector callback, which will set the ID for
2907 // this selector.
2908 Chain->LoadSelector(Sel);
2909 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002910 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002911 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002912 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002913 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002914}
2915
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002916void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002917 AddDeclRef(Temp->getDestructor(), Record);
2918}
2919
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002920void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2921 CXXBaseSpecifier const *BasesEnd,
2922 RecordDataImpl &Record) {
2923 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2924 CXXBaseSpecifiersToWrite.push_back(
2925 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2926 Bases, BasesEnd));
2927 Record.push_back(NextCXXBaseSpecifiersID++);
2928}
2929
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002930void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002931 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002932 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002933 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002934 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002935 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002936 break;
2937 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002938 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002939 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002940 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002941 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2942 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002943 break;
2944 case TemplateArgument::TemplateExpansion:
2945 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2946 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00002947 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002948 break;
John McCall0ad16662009-10-29 08:12:44 +00002949 case TemplateArgument::Null:
2950 case TemplateArgument::Integral:
2951 case TemplateArgument::Declaration:
2952 case TemplateArgument::Pack:
2953 break;
2954 }
2955}
2956
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002957void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002958 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002959 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002960
2961 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2962 bool InfoHasSameExpr
2963 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2964 Record.push_back(InfoHasSameExpr);
2965 if (InfoHasSameExpr)
2966 return; // Avoid storing the same expr twice.
2967 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002968 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2969 Record);
2970}
2971
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002972void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002973 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002974 AddTypeRef(QualType(), Record);
2975 return;
2976 }
2977
John McCallbcd03502009-12-07 02:54:59 +00002978 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002979 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002980 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002981 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002982}
2983
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002984void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002985 Record.push_back(GetOrCreateTypeID(T));
2986}
2987
2988TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002989 return MakeTypeID(T,
2990 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2991}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002992
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002993TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002994 return MakeTypeID(T,
2995 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002996}
2997
2998TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2999 if (T.isNull())
3000 return TypeIdx();
3001 assert(!T.getLocalFastQualifiers());
3002
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003003 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003004 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003005 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003006 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003007 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003008 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003009 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003010 return Idx;
3011}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003012
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003013TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003014 if (T.isNull())
3015 return TypeIdx();
3016 assert(!T.getLocalFastQualifiers());
3017
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003018 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3019 assert(I != TypeIdxs.end() && "Type not emitted!");
3020 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003021}
3022
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003023void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003024 Record.push_back(GetDeclRef(D));
3025}
3026
Sebastian Redl539c5062010-08-18 23:57:32 +00003027DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003028 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003029 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003030 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003031 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003032 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003033 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003034 // We haven't seen this declaration before. Give it a new ID and
3035 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003036 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003037 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003038 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3039 // We don't add it to the replacement collection here, because we don't
3040 // have the offset yet.
3041 DeclTypesToEmit.push(const_cast<Decl *>(D));
3042 // Reset the flag, so that we don't add this decl multiple times.
3043 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003044 }
3045
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003046 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003047}
3048
Sebastian Redl539c5062010-08-18 23:57:32 +00003049DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003050 if (D == 0)
3051 return 0;
3052
3053 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3054 return DeclIDs[D];
3055}
3056
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003057void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003058 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003059 Record.push_back(Name.getNameKind());
3060 switch (Name.getNameKind()) {
3061 case DeclarationName::Identifier:
3062 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3063 break;
3064
3065 case DeclarationName::ObjCZeroArgSelector:
3066 case DeclarationName::ObjCOneArgSelector:
3067 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003068 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003069 break;
3070
3071 case DeclarationName::CXXConstructorName:
3072 case DeclarationName::CXXDestructorName:
3073 case DeclarationName::CXXConversionFunctionName:
3074 AddTypeRef(Name.getCXXNameType(), Record);
3075 break;
3076
3077 case DeclarationName::CXXOperatorName:
3078 Record.push_back(Name.getCXXOverloadedOperator());
3079 break;
3080
Alexis Hunt3d221f22009-11-29 07:34:05 +00003081 case DeclarationName::CXXLiteralOperatorName:
3082 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3083 break;
3084
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003085 case DeclarationName::CXXUsingDirective:
3086 // No extra data to emit
3087 break;
3088 }
3089}
Chris Lattnerca025db2010-05-07 21:43:38 +00003090
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003091void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003092 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003093 switch (Name.getNameKind()) {
3094 case DeclarationName::CXXConstructorName:
3095 case DeclarationName::CXXDestructorName:
3096 case DeclarationName::CXXConversionFunctionName:
3097 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3098 break;
3099
3100 case DeclarationName::CXXOperatorName:
3101 AddSourceLocation(
3102 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3103 Record);
3104 AddSourceLocation(
3105 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3106 Record);
3107 break;
3108
3109 case DeclarationName::CXXLiteralOperatorName:
3110 AddSourceLocation(
3111 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3112 Record);
3113 break;
3114
3115 case DeclarationName::Identifier:
3116 case DeclarationName::ObjCZeroArgSelector:
3117 case DeclarationName::ObjCOneArgSelector:
3118 case DeclarationName::ObjCMultiArgSelector:
3119 case DeclarationName::CXXUsingDirective:
3120 break;
3121 }
3122}
3123
3124void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003125 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003126 AddDeclarationName(NameInfo.getName(), Record);
3127 AddSourceLocation(NameInfo.getLoc(), Record);
3128 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3129}
3130
3131void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003132 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003133 AddNestedNameSpecifier(Info.NNS, Record);
3134 AddSourceRange(Info.NNSRange, Record);
3135 Record.push_back(Info.NumTemplParamLists);
3136 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3137 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3138}
3139
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003140void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003141 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003142 // Nested name specifiers usually aren't too long. I think that 8 would
3143 // typically accomodate the vast majority.
3144 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3145
3146 // Push each of the NNS's onto a stack for serialization in reverse order.
3147 while (NNS) {
3148 NestedNames.push_back(NNS);
3149 NNS = NNS->getPrefix();
3150 }
3151
3152 Record.push_back(NestedNames.size());
3153 while(!NestedNames.empty()) {
3154 NNS = NestedNames.pop_back_val();
3155 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3156 Record.push_back(Kind);
3157 switch (Kind) {
3158 case NestedNameSpecifier::Identifier:
3159 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3160 break;
3161
3162 case NestedNameSpecifier::Namespace:
3163 AddDeclRef(NNS->getAsNamespace(), Record);
3164 break;
3165
3166 case NestedNameSpecifier::TypeSpec:
3167 case NestedNameSpecifier::TypeSpecWithTemplate:
3168 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3169 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3170 break;
3171
3172 case NestedNameSpecifier::Global:
3173 // Don't need to write an associated value.
3174 break;
3175 }
3176 }
3177}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003178
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003179void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003180 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003181 Record.push_back(Kind);
3182 switch (Kind) {
3183 case TemplateName::Template:
3184 AddDeclRef(Name.getAsTemplateDecl(), Record);
3185 break;
3186
3187 case TemplateName::OverloadedTemplate: {
3188 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3189 Record.push_back(OvT->size());
3190 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3191 I != E; ++I)
3192 AddDeclRef(*I, Record);
3193 break;
3194 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003195
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003196 case TemplateName::QualifiedTemplate: {
3197 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3198 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3199 Record.push_back(QualT->hasTemplateKeyword());
3200 AddDeclRef(QualT->getTemplateDecl(), Record);
3201 break;
3202 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003203
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003204 case TemplateName::DependentTemplate: {
3205 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3206 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3207 Record.push_back(DepT->isIdentifier());
3208 if (DepT->isIdentifier())
3209 AddIdentifierRef(DepT->getIdentifier(), Record);
3210 else
3211 Record.push_back(DepT->getOperator());
3212 break;
3213 }
3214 }
3215}
3216
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003217void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003218 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003219 Record.push_back(Arg.getKind());
3220 switch (Arg.getKind()) {
3221 case TemplateArgument::Null:
3222 break;
3223 case TemplateArgument::Type:
3224 AddTypeRef(Arg.getAsType(), Record);
3225 break;
3226 case TemplateArgument::Declaration:
3227 AddDeclRef(Arg.getAsDecl(), Record);
3228 break;
3229 case TemplateArgument::Integral:
3230 AddAPSInt(*Arg.getAsIntegral(), Record);
3231 AddTypeRef(Arg.getIntegralType(), Record);
3232 break;
3233 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003234 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3235 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003236 case TemplateArgument::TemplateExpansion:
3237 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003238 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3239 Record.push_back(*NumExpansions + 1);
3240 else
3241 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003242 break;
3243 case TemplateArgument::Expression:
3244 AddStmt(Arg.getAsExpr());
3245 break;
3246 case TemplateArgument::Pack:
3247 Record.push_back(Arg.pack_size());
3248 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3249 I != E; ++I)
3250 AddTemplateArgument(*I, Record);
3251 break;
3252 }
3253}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003254
3255void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003256ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003257 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003258 assert(TemplateParams && "No TemplateParams!");
3259 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3260 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3261 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3262 Record.push_back(TemplateParams->size());
3263 for (TemplateParameterList::const_iterator
3264 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3265 P != PEnd; ++P)
3266 AddDeclRef(*P, Record);
3267}
3268
3269/// \brief Emit a template argument list.
3270void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003271ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003272 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003273 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003274 Record.push_back(TemplateArgs->size());
3275 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003276 AddTemplateArgument(TemplateArgs->get(i), Record);
3277}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003278
3279
3280void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003281ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003282 Record.push_back(Set.size());
3283 for (UnresolvedSetImpl::const_iterator
3284 I = Set.begin(), E = Set.end(); I != E; ++I) {
3285 AddDeclRef(I.getDecl(), Record);
3286 Record.push_back(I.getAccess());
3287 }
3288}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003289
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003290void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003291 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003292 Record.push_back(Base.isVirtual());
3293 Record.push_back(Base.isBaseOfClass());
3294 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003295 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003296 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003297 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3298 : SourceLocation(),
3299 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003300}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003301
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003302void ASTWriter::FlushCXXBaseSpecifiers() {
3303 RecordData Record;
3304 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3305 Record.clear();
3306
3307 // Record the offset of this base-specifier set.
3308 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3309 if (Index == CXXBaseSpecifiersOffsets.size())
3310 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3311 else {
3312 if (Index > CXXBaseSpecifiersOffsets.size())
3313 CXXBaseSpecifiersOffsets.resize(Index + 1);
3314 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3315 }
3316
3317 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3318 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3319 Record.push_back(BEnd - B);
3320 for (; B != BEnd; ++B)
3321 AddCXXBaseSpecifier(*B, Record);
3322 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003323
3324 // Flush any expressions that were written as part of the base specifiers.
3325 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003326 }
3327
3328 CXXBaseSpecifiersToWrite.clear();
3329}
3330
Alexis Hunt1d792652011-01-08 20:30:50 +00003331void ASTWriter::AddCXXCtorInitializers(
3332 const CXXCtorInitializer * const *CtorInitializers,
3333 unsigned NumCtorInitializers,
3334 RecordDataImpl &Record) {
3335 Record.push_back(NumCtorInitializers);
3336 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3337 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003338
3339 Record.push_back(Init->isBaseInitializer());
3340 if (Init->isBaseInitializer()) {
3341 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3342 Record.push_back(Init->isBaseVirtual());
3343 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003344 Record.push_back(Init->isIndirectMemberInitializer());
3345 if (Init->isIndirectMemberInitializer())
3346 AddDeclRef(Init->getIndirectMember(), Record);
3347 else
3348 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003349 }
Francois Pichetd583da02010-12-04 09:14:42 +00003350
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003351 AddSourceLocation(Init->getMemberLocation(), Record);
3352 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003353 AddSourceLocation(Init->getLParenLoc(), Record);
3354 AddSourceLocation(Init->getRParenLoc(), Record);
3355 Record.push_back(Init->isWritten());
3356 if (Init->isWritten()) {
3357 Record.push_back(Init->getSourceOrder());
3358 } else {
3359 Record.push_back(Init->getNumArrayIndices());
3360 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3361 AddDeclRef(Init->getArrayIndex(i), Record);
3362 }
3363 }
3364}
3365
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003366void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3367 assert(D->DefinitionData);
3368 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3369 Record.push_back(Data.UserDeclaredConstructor);
3370 Record.push_back(Data.UserDeclaredCopyConstructor);
3371 Record.push_back(Data.UserDeclaredCopyAssignment);
3372 Record.push_back(Data.UserDeclaredDestructor);
3373 Record.push_back(Data.Aggregate);
3374 Record.push_back(Data.PlainOldData);
3375 Record.push_back(Data.Empty);
3376 Record.push_back(Data.Polymorphic);
3377 Record.push_back(Data.Abstract);
3378 Record.push_back(Data.HasTrivialConstructor);
3379 Record.push_back(Data.HasTrivialCopyConstructor);
3380 Record.push_back(Data.HasTrivialCopyAssignment);
3381 Record.push_back(Data.HasTrivialDestructor);
3382 Record.push_back(Data.ComputedVisibleConversions);
3383 Record.push_back(Data.DeclaredDefaultConstructor);
3384 Record.push_back(Data.DeclaredCopyConstructor);
3385 Record.push_back(Data.DeclaredCopyAssignment);
3386 Record.push_back(Data.DeclaredDestructor);
3387
3388 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003389 if (Data.NumBases > 0)
3390 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3391 Record);
3392
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003393 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3394 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003395 if (Data.NumVBases > 0)
3396 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3397 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003398
3399 AddUnresolvedSet(Data.Conversions, Record);
3400 AddUnresolvedSet(Data.VisibleConversions, Record);
3401 // Data.Definition is the owning decl, no need to write it.
3402 AddDeclRef(Data.FirstFriend, Record);
3403}
3404
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003405void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003406 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003407 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003408 assert(FirstDeclID == NextDeclID &&
3409 FirstTypeID == NextTypeID &&
3410 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003411 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003412 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003413 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003414 "Setting chain after writing has started.");
3415 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003416
3417 FirstDeclID += Chain->getTotalNumDecls();
3418 FirstTypeID += Chain->getTotalNumTypes();
3419 FirstIdentID += Chain->getTotalNumIdentifiers();
3420 FirstSelectorID += Chain->getTotalNumSelectors();
3421 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003422 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003423 NextDeclID = FirstDeclID;
3424 NextTypeID = FirstTypeID;
3425 NextIdentID = FirstIdentID;
3426 NextSelectorID = FirstSelectorID;
3427 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003428 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003429}
3430
Sebastian Redl539c5062010-08-18 23:57:32 +00003431void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003432 IdentifierIDs[II] = ID;
3433}
3434
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003435void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003436 // Always take the highest-numbered type index. This copes with an interesting
3437 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003438 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003439 // keep the higher-numbered entry so that we can properly write it out to
3440 // the AST file.
3441 TypeIdx &StoredIdx = TypeIdxs[T];
3442 if (Idx.getIndex() >= StoredIdx.getIndex())
3443 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003444}
3445
Sebastian Redl539c5062010-08-18 23:57:32 +00003446void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003447 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003448}
Sebastian Redl834bb972010-08-04 17:20:04 +00003449
Sebastian Redl539c5062010-08-18 23:57:32 +00003450void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003451 SelectorIDs[S] = ID;
3452}
Douglas Gregor91096292010-10-02 19:29:26 +00003453
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003454void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003455 MacroDefinition *MD) {
3456 MacroDefinitions[MD] = ID;
3457}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003458
3459void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3460 assert(D->isDefinition());
3461 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3462 // We are interested when a PCH decl is modified.
3463 if (RD->getPCHLevel() > 0) {
3464 // A forward reference was mutated into a definition. Rewrite it.
3465 // FIXME: This happens during template instantiation, should we
3466 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003467 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003468 }
3469
3470 for (CXXRecordDecl::redecl_iterator
3471 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3472 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3473 if (Redecl == RD)
3474 continue;
3475
3476 // We are interested when a PCH decl is modified.
3477 if (Redecl->getPCHLevel() > 0) {
3478 UpdateRecord &Record = DeclUpdates[Redecl];
3479 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3480 assert(Redecl->DefinitionData);
3481 assert(Redecl->DefinitionData->Definition == D);
3482 AddDeclRef(D, Record); // the DefinitionDecl
3483 }
3484 }
3485 }
3486}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003487void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3488 // TU and namespaces are handled elsewhere.
3489 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3490 return;
3491
3492 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3493 return; // Not a source decl added to a DeclContext from PCH.
3494
3495 AddUpdatedDeclContext(DC);
3496}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003497
3498void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3499 assert(D->isImplicit());
3500 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3501 return; // Not a source member added to a class from PCH.
3502 if (!isa<CXXMethodDecl>(D))
3503 return; // We are interested in lazily declared implicit methods.
3504
3505 // A decl coming from PCH was modified.
3506 assert(RD->isDefinition());
3507 UpdateRecord &Record = DeclUpdates[RD];
3508 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3509 AddDeclRef(D, Record);
3510}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003511
3512void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3513 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003514 // The specializations set is kept in the canonical template.
3515 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003516 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3517 return; // Not a source specialization added to a template from PCH.
3518
3519 UpdateRecord &Record = DeclUpdates[TD];
3520 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3521 AddDeclRef(D, Record);
3522}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003523
3524ASTSerializationListener::~ASTSerializationListener() { }