blob: e90640711f9e2a2371f38d8df183f5785bdd4b42 [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());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000179 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000180 Record.push_back(T->hasExceptionSpec());
181 Record.push_back(T->hasAnyExceptionSpec());
182 Record.push_back(T->getNumExceptions());
183 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
184 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000185 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000186}
187
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000188void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000189 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000190 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000191}
John McCallb96ec562009-12-04 22:46:56 +0000192
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000193void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000194 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000195 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
196 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000197 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198}
199
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000200void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000201 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000202 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000203}
204
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000205void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000206 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000207 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000208}
209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000211 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000212 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000213}
214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000216 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000218 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219 "Cannot serialize in the middle of a type definition");
220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000229 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230}
231
John McCall81904512011-01-06 01:58:22 +0000232void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
233 Writer.AddTypeRef(T->getModifiedType(), Record);
234 Writer.AddTypeRef(T->getEquivalentType(), Record);
235 Record.push_back(T->getAttrKind());
236 Code = TYPE_ATTRIBUTED;
237}
238
Mike Stump11289f42009-09-09 15:08:12 +0000239void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000240ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000241 const SubstTemplateTypeParmType *T) {
242 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
243 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000244 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000245}
246
247void
Douglas Gregorada4b792011-01-14 02:55:32 +0000248ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
249 const SubstTemplateTypeParmPackType *T) {
250 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
251 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
252 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
253}
254
255void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000256ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000257 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000258 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000259 Writer.AddTemplateName(T->getTemplateName(), Record);
260 Record.push_back(T->getNumArgs());
261 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
262 ArgI != ArgE; ++ArgI)
263 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000264 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
265 : T->getCanonicalTypeInternal(),
266 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000267 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000268}
269
270void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000271ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000272 VisitArrayType(T);
273 Writer.AddStmt(T->getSizeExpr());
274 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000275 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000276}
277
278void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000279ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000280 const DependentSizedExtVectorType *T) {
281 // FIXME: Serialize this type (C++ only)
282 assert(false && "Cannot serialize dependent sized extended vector types");
283}
284
285void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000287 Record.push_back(T->getDepth());
288 Record.push_back(T->getIndex());
289 Record.push_back(T->isParameterPack());
290 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000291 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000292}
293
294void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000295ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000296 Record.push_back(T->getKeyword());
297 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
298 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000299 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
300 : T->getCanonicalTypeInternal(),
301 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000302 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000307 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000308 Record.push_back(T->getKeyword());
309 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
310 Writer.AddIdentifierRef(T->getIdentifier(), Record);
311 Record.push_back(T->getNumArgs());
312 for (DependentTemplateSpecializationType::iterator
313 I = T->begin(), E = T->end(); I != E; ++I)
314 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000315 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000316}
317
Douglas Gregord2fa7662010-12-20 02:24:11 +0000318void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
319 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000320 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
321 Record.push_back(*NumExpansions + 1);
322 else
323 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000324 Code = TYPE_PACK_EXPANSION;
325}
326
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000327void ASTTypeWriter::VisitParenType(const ParenType *T) {
328 Writer.AddTypeRef(T->getInnerType(), Record);
329 Code = TYPE_PAREN;
330}
331
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000332void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000333 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000334 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
335 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000336 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000337}
338
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000339void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000340 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000341 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000343}
344
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000345void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000346 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000347 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000348}
349
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000350void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000351 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000352 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000353 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000354 E = T->qual_end(); I != E; ++I)
355 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000356 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000357}
358
Steve Narofffb4330f2009-06-17 22:40:22 +0000359void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000360ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000361 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000362 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000363}
364
John McCall8f115c62009-10-16 21:56:05 +0000365namespace {
366
367class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000368 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000369 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000370
371public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000372 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000373 : Writer(Writer), Record(Record) { }
374
John McCall17001972009-10-18 01:05:36 +0000375#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000376#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000377 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000378#include "clang/AST/TypeLocNodes.def"
379
John McCall17001972009-10-18 01:05:36 +0000380 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
381 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000382};
383
384}
385
John McCall17001972009-10-18 01:05:36 +0000386void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
387 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000388}
John McCall17001972009-10-18 01:05:36 +0000389void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000390 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
391 if (TL.needsExtraLocalData()) {
392 Record.push_back(TL.getWrittenTypeSpec());
393 Record.push_back(TL.getWrittenSignSpec());
394 Record.push_back(TL.getWrittenWidthSpec());
395 Record.push_back(TL.hasModeAttr());
396 }
John McCall8f115c62009-10-16 21:56:05 +0000397}
John McCall17001972009-10-18 01:05:36 +0000398void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
399 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000400}
John McCall17001972009-10-18 01:05:36 +0000401void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
402 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000403}
John McCall17001972009-10-18 01:05:36 +0000404void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000406}
John McCall17001972009-10-18 01:05:36 +0000407void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000409}
John McCall17001972009-10-18 01:05:36 +0000410void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000412}
John McCall17001972009-10-18 01:05:36 +0000413void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000415}
John McCall17001972009-10-18 01:05:36 +0000416void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
418 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
419 Record.push_back(TL.getSizeExpr() ? 1 : 0);
420 if (TL.getSizeExpr())
421 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000422}
John McCall17001972009-10-18 01:05:36 +0000423void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
424 VisitArrayTypeLoc(TL);
425}
426void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
427 VisitArrayTypeLoc(TL);
428}
429void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
430 VisitArrayTypeLoc(TL);
431}
432void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
433 DependentSizedArrayTypeLoc TL) {
434 VisitArrayTypeLoc(TL);
435}
436void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
437 DependentSizedExtVectorTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
439}
440void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
442}
443void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
446void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
448 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000449 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000450 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
451 Writer.AddDeclRef(TL.getArg(i), Record);
452}
453void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
454 VisitFunctionTypeLoc(TL);
455}
456void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
457 VisitFunctionTypeLoc(TL);
458}
John McCallb96ec562009-12-04 22:46:56 +0000459void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
461}
John McCall17001972009-10-18 01:05:36 +0000462void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
465void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000466 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
467 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
468 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000469}
470void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000471 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
472 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
473 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
474 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000475}
476void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
479void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
481}
482void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
484}
John McCall81904512011-01-06 01:58:22 +0000485void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
487 if (TL.hasAttrOperand()) {
488 SourceRange range = TL.getAttrOperandParensRange();
489 Writer.AddSourceLocation(range.getBegin(), Record);
490 Writer.AddSourceLocation(range.getEnd(), Record);
491 }
492 if (TL.hasAttrExprOperand()) {
493 Expr *operand = TL.getAttrExprOperand();
494 Record.push_back(operand ? 1 : 0);
495 if (operand) Writer.AddStmt(operand);
496 } else if (TL.hasAttrEnumOperand()) {
497 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
498 }
499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502}
John McCallcebee162009-10-18 09:09:24 +0000503void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
504 SubstTemplateTypeParmTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
Douglas Gregorada4b792011-01-14 02:55:32 +0000507void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
508 SubstTemplateTypeParmPackTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getNameLoc(), Record);
510}
John McCall17001972009-10-18 01:05:36 +0000511void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
512 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000513 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
514 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
515 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
516 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000517 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
518 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000519}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000520void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
522 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
523}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000524void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000525 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
526 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000527}
John McCalle78aac42010-03-10 03:28:59 +0000528void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
529 Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000531void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000532 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
533 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
John McCallc392f372010-06-11 00:33:02 +0000536void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
537 DependentTemplateSpecializationTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
539 Writer.AddSourceRange(TL.getQualifierRange(), Record);
540 Writer.AddSourceLocation(TL.getNameLoc(), Record);
541 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
542 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
543 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000544 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
545 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000546}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000547void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
549}
John McCall17001972009-10-18 01:05:36 +0000550void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000552}
553void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
554 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000555 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
556 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
557 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
558 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000559}
John McCallfc93cf92009-10-22 22:37:11 +0000560void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000562}
John McCall8f115c62009-10-16 21:56:05 +0000563
Chris Lattner19cea4e2009-04-22 05:57:30 +0000564//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000565// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000566//===----------------------------------------------------------------------===//
567
Chris Lattner28fa4e62009-04-26 22:26:21 +0000568static void EmitBlockID(unsigned ID, const char *Name,
569 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000570 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000571 Record.clear();
572 Record.push_back(ID);
573 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
574
575 // Emit the block name if present.
576 if (Name == 0 || Name[0] == 0) return;
577 Record.clear();
578 while (*Name)
579 Record.push_back(*Name++);
580 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
581}
582
583static void EmitRecordID(unsigned ID, const char *Name,
584 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000585 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000586 Record.clear();
587 Record.push_back(ID);
588 while (*Name)
589 Record.push_back(*Name++);
590 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000591}
592
593static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000594 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000595#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000596 RECORD(STMT_STOP);
597 RECORD(STMT_NULL_PTR);
598 RECORD(STMT_NULL);
599 RECORD(STMT_COMPOUND);
600 RECORD(STMT_CASE);
601 RECORD(STMT_DEFAULT);
602 RECORD(STMT_LABEL);
603 RECORD(STMT_IF);
604 RECORD(STMT_SWITCH);
605 RECORD(STMT_WHILE);
606 RECORD(STMT_DO);
607 RECORD(STMT_FOR);
608 RECORD(STMT_GOTO);
609 RECORD(STMT_INDIRECT_GOTO);
610 RECORD(STMT_CONTINUE);
611 RECORD(STMT_BREAK);
612 RECORD(STMT_RETURN);
613 RECORD(STMT_DECL);
614 RECORD(STMT_ASM);
615 RECORD(EXPR_PREDEFINED);
616 RECORD(EXPR_DECL_REF);
617 RECORD(EXPR_INTEGER_LITERAL);
618 RECORD(EXPR_FLOATING_LITERAL);
619 RECORD(EXPR_IMAGINARY_LITERAL);
620 RECORD(EXPR_STRING_LITERAL);
621 RECORD(EXPR_CHARACTER_LITERAL);
622 RECORD(EXPR_PAREN);
623 RECORD(EXPR_UNARY_OPERATOR);
624 RECORD(EXPR_SIZEOF_ALIGN_OF);
625 RECORD(EXPR_ARRAY_SUBSCRIPT);
626 RECORD(EXPR_CALL);
627 RECORD(EXPR_MEMBER);
628 RECORD(EXPR_BINARY_OPERATOR);
629 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
630 RECORD(EXPR_CONDITIONAL_OPERATOR);
631 RECORD(EXPR_IMPLICIT_CAST);
632 RECORD(EXPR_CSTYLE_CAST);
633 RECORD(EXPR_COMPOUND_LITERAL);
634 RECORD(EXPR_EXT_VECTOR_ELEMENT);
635 RECORD(EXPR_INIT_LIST);
636 RECORD(EXPR_DESIGNATED_INIT);
637 RECORD(EXPR_IMPLICIT_VALUE_INIT);
638 RECORD(EXPR_VA_ARG);
639 RECORD(EXPR_ADDR_LABEL);
640 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000641 RECORD(EXPR_CHOOSE);
642 RECORD(EXPR_GNU_NULL);
643 RECORD(EXPR_SHUFFLE_VECTOR);
644 RECORD(EXPR_BLOCK);
645 RECORD(EXPR_BLOCK_DECL_REF);
646 RECORD(EXPR_OBJC_STRING_LITERAL);
647 RECORD(EXPR_OBJC_ENCODE);
648 RECORD(EXPR_OBJC_SELECTOR_EXPR);
649 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
650 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
651 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
652 RECORD(EXPR_OBJC_KVC_REF_EXPR);
653 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000654 RECORD(STMT_OBJC_FOR_COLLECTION);
655 RECORD(STMT_OBJC_CATCH);
656 RECORD(STMT_OBJC_FINALLY);
657 RECORD(STMT_OBJC_AT_TRY);
658 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
659 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000660 RECORD(EXPR_CXX_OPERATOR_CALL);
661 RECORD(EXPR_CXX_CONSTRUCT);
662 RECORD(EXPR_CXX_STATIC_CAST);
663 RECORD(EXPR_CXX_DYNAMIC_CAST);
664 RECORD(EXPR_CXX_REINTERPRET_CAST);
665 RECORD(EXPR_CXX_CONST_CAST);
666 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
667 RECORD(EXPR_CXX_BOOL_LITERAL);
668 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000669#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000670}
Mike Stump11289f42009-09-09 15:08:12 +0000671
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000672void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000673 RecordData Record;
674 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000675
Sebastian Redl539c5062010-08-18 23:57:32 +0000676#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
677#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000678
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000679 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000680 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000681 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000682 RECORD(TYPE_OFFSET);
683 RECORD(DECL_OFFSET);
684 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000685 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000686 RECORD(IDENTIFIER_OFFSET);
687 RECORD(IDENTIFIER_TABLE);
688 RECORD(EXTERNAL_DEFINITIONS);
689 RECORD(SPECIAL_TYPES);
690 RECORD(STATISTICS);
691 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000692 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000693 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
694 RECORD(SELECTOR_OFFSETS);
695 RECORD(METHOD_POOL);
696 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000697 RECORD(SOURCE_LOCATION_OFFSETS);
698 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000699 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000700 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000701 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000702 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000703 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000704 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000705
Chris Lattner28fa4e62009-04-26 22:26:21 +0000706 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000707 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000708 RECORD(SM_SLOC_FILE_ENTRY);
709 RECORD(SM_SLOC_BUFFER_ENTRY);
710 RECORD(SM_SLOC_BUFFER_BLOB);
711 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
712 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000713
Chris Lattner28fa4e62009-04-26 22:26:21 +0000714 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000715 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000716 RECORD(PP_MACRO_OBJECT_LIKE);
717 RECORD(PP_MACRO_FUNCTION_LIKE);
718 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000719 RECORD(PP_MACRO_INSTANTIATION);
720 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000721
Douglas Gregor12bfa382009-10-17 00:13:19 +0000722 // Decls and Types block.
723 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000724 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000725 RECORD(TYPE_COMPLEX);
726 RECORD(TYPE_POINTER);
727 RECORD(TYPE_BLOCK_POINTER);
728 RECORD(TYPE_LVALUE_REFERENCE);
729 RECORD(TYPE_RVALUE_REFERENCE);
730 RECORD(TYPE_MEMBER_POINTER);
731 RECORD(TYPE_CONSTANT_ARRAY);
732 RECORD(TYPE_INCOMPLETE_ARRAY);
733 RECORD(TYPE_VARIABLE_ARRAY);
734 RECORD(TYPE_VECTOR);
735 RECORD(TYPE_EXT_VECTOR);
736 RECORD(TYPE_FUNCTION_PROTO);
737 RECORD(TYPE_FUNCTION_NO_PROTO);
738 RECORD(TYPE_TYPEDEF);
739 RECORD(TYPE_TYPEOF_EXPR);
740 RECORD(TYPE_TYPEOF);
741 RECORD(TYPE_RECORD);
742 RECORD(TYPE_ENUM);
743 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000744 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000745 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000746 RECORD(DECL_TRANSLATION_UNIT);
747 RECORD(DECL_TYPEDEF);
748 RECORD(DECL_ENUM);
749 RECORD(DECL_RECORD);
750 RECORD(DECL_ENUM_CONSTANT);
751 RECORD(DECL_FUNCTION);
752 RECORD(DECL_OBJC_METHOD);
753 RECORD(DECL_OBJC_INTERFACE);
754 RECORD(DECL_OBJC_PROTOCOL);
755 RECORD(DECL_OBJC_IVAR);
756 RECORD(DECL_OBJC_AT_DEFS_FIELD);
757 RECORD(DECL_OBJC_CLASS);
758 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
759 RECORD(DECL_OBJC_CATEGORY);
760 RECORD(DECL_OBJC_CATEGORY_IMPL);
761 RECORD(DECL_OBJC_IMPLEMENTATION);
762 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
763 RECORD(DECL_OBJC_PROPERTY);
764 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000765 RECORD(DECL_FIELD);
766 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000767 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000768 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000769 RECORD(DECL_FILE_SCOPE_ASM);
770 RECORD(DECL_BLOCK);
771 RECORD(DECL_CONTEXT_LEXICAL);
772 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000773 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000774 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000775#undef RECORD
776#undef BLOCK
777 Stream.ExitBlock();
778}
779
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000780/// \brief Adjusts the given filename to only write out the portion of the
781/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000782///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000783/// \param Filename the file name to adjust.
784///
785/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
786/// the returned filename will be adjusted by this system root.
787///
788/// \returns either the original filename (if it needs no adjustment) or the
789/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000790static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000791adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
792 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000793
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000794 if (!isysroot)
795 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000796
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000797 // Verify that the filename and the system root have the same prefix.
798 unsigned Pos = 0;
799 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
800 if (Filename[Pos] != isysroot[Pos])
801 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000802
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000803 // We hit the end of the filename before we hit the end of the system root.
804 if (!Filename[Pos])
805 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000806
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000807 // If the file name has a '/' at the current position, skip over the '/'.
808 // We distinguish sysroot-based includes from absolute includes by the
809 // absence of '/' at the beginning of sysroot-based includes.
810 if (Filename[Pos] == '/')
811 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000812
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000813 return Filename + Pos;
814}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000815
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000816/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000817void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000818 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000819
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000820 // Metadata
821 const TargetInfo &Target = Context.Target;
822 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000823 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000824 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000825 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
826 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000827 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
828 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
829 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000830 // Target triple or chained PCH name
831 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000832 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000833
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000834 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000835 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
836 Record.push_back(VERSION_MAJOR);
837 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000838 Record.push_back(CLANG_VERSION_MAJOR);
839 Record.push_back(CLANG_VERSION_MINOR);
840 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000841 // FIXME: This writes the absolute path for chained headers.
842 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
843 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000844
Douglas Gregor45fe0362009-05-12 01:31:05 +0000845 // Original file name
846 SourceManager &SM = Context.getSourceManager();
847 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
848 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000849 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000850 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
851 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
852
Michael J. Spencer740857f2010-12-21 16:45:57 +0000853 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000854
Michael J. Spencer740857f2010-12-21 16:45:57 +0000855 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000856
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000857 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000858 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000859 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000860 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000861 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000862 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000863 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000864
Ted Kremenek18e066f2010-01-22 22:12:47 +0000865 // Repository branch/version information.
866 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000867 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000868 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
869 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000870 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000871 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000872 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
873 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000874}
875
876/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000877void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000878 RecordData Record;
879 Record.push_back(LangOpts.Trigraphs);
880 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
881 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
882 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
883 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000884 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000885 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
886 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
887 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
888 Record.push_back(LangOpts.C99); // C99 Support
889 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000890 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
891 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000892 Record.push_back(LangOpts.CPlusPlus); // C++ Support
893 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000894 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000895
Douglas Gregor55abb232009-04-10 20:39:37 +0000896 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
897 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000898 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000899 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000900 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000901 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +0000902 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +0000903 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
904 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000905 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000906
Douglas Gregor55abb232009-04-10 20:39:37 +0000907 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000908 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
909 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000910 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000911 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000912 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000913
914 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
915 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
916 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
917
Chris Lattner258172e2009-04-27 07:35:58 +0000918 // Whether static initializers are protected by locks.
919 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000920 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000921 Record.push_back(LangOpts.Blocks); // block extension to C
922 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
923 // they are unused.
924 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
925 // (modulo the platform support).
926
Chris Lattner51924e512010-06-26 21:25:03 +0000927 Record.push_back(LangOpts.getSignedOverflowBehavior());
928 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000929
930 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000931 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000932 // defined.
933 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
934 // opposed to __DYNAMIC__).
935 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
936
937 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
938 // used (instead of C99 semantics).
939 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000940 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
941 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000942 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
943 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000944 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +0000945 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
946 // to the smallest integer type with
947 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +0000948 Record.push_back(LangOpts.getGCMode());
949 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000950 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000951 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000952 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000953 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +0000954 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000955 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000956 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000957 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000958}
959
Douglas Gregora7f71a92009-04-10 03:52:48 +0000960//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000961// stat cache Serialization
962//===----------------------------------------------------------------------===//
963
964namespace {
965// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000966class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000967public:
968 typedef const char * key_type;
969 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000970
Chris Lattner2a6fa472010-11-23 19:28:12 +0000971 typedef struct stat data_type;
972 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000973
974 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000975 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000976 }
Mike Stump11289f42009-09-09 15:08:12 +0000977
978 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000979 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
980 data_type_ref Data) {
981 unsigned StrLen = strlen(path);
982 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000983 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000984 clang::io::Emit8(Out, DataLen);
985 return std::make_pair(StrLen + 1, DataLen);
986 }
Mike Stump11289f42009-09-09 15:08:12 +0000987
Douglas Gregorc5046832009-04-27 18:38:38 +0000988 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
989 Out.write(path, KeyLen);
990 }
Mike Stump11289f42009-09-09 15:08:12 +0000991
Chris Lattner2a6fa472010-11-23 19:28:12 +0000992 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000993 data_type_ref Data, unsigned DataLen) {
994 using namespace clang::io;
995 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000996
Chris Lattner2a6fa472010-11-23 19:28:12 +0000997 Emit32(Out, (uint32_t) Data.st_ino);
998 Emit32(Out, (uint32_t) Data.st_dev);
999 Emit16(Out, (uint16_t) Data.st_mode);
1000 Emit64(Out, (uint64_t) Data.st_mtime);
1001 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001002
1003 assert(Out.tell() - Start == DataLen && "Wrong data length");
1004 }
1005};
1006} // end anonymous namespace
1007
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001008/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001009void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001010 // Build the on-disk hash table containing information about every
1011 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001012 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001013 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001014 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001015 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001016 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1017 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001018 Generator.insert(Filename, Stat->second);
1019 }
Mike Stump11289f42009-09-09 15:08:12 +00001020
Douglas Gregorc5046832009-04-27 18:38:38 +00001021 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001022 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001023 uint32_t BucketOffset;
1024 {
1025 llvm::raw_svector_ostream Out(StatCacheData);
1026 // Make sure that no bucket is at offset 0
1027 clang::io::Emit32(Out, 0);
1028 BucketOffset = Generator.Emit(Out);
1029 }
1030
1031 // Create a blob abbreviation
1032 using namespace llvm;
1033 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001034 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1038 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1039
1040 // Write the stat cache
1041 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001042 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001043 Record.push_back(BucketOffset);
1044 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001045 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001046}
1047
1048//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001049// Source Manager Serialization
1050//===----------------------------------------------------------------------===//
1051
1052/// \brief Create an abbreviation for the SLocEntry that refers to a
1053/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001054static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001055 using namespace llvm;
1056 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001057 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001062 // FileEntry fields.
1063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001065 // HeaderFileInfo fields.
1066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1069 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001071 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001072}
1073
1074/// \brief Create an abbreviation for the SLocEntry that refers to a
1075/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001076static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001077 using namespace llvm;
1078 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001079 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001080 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001085 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001086}
1087
1088/// \brief Create an abbreviation for the SLocEntry that refers to a
1089/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001090static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001091 using namespace llvm;
1092 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001093 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001095 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001096}
1097
1098/// \brief Create an abbreviation for the SLocEntry that refers to an
1099/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001100static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001101 using namespace llvm;
1102 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001103 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1107 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001108 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001109 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001110}
1111
1112/// \brief Writes the block containing the serialized form of the
1113/// source manager.
1114///
1115/// TODO: We should probably use an on-disk hash table (stored in a
1116/// blob), indexed based on the file name, so that we only create
1117/// entries for files that we actually need. In the common case (no
1118/// errors), we probably won't have to create file entries for any of
1119/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001120void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001121 const Preprocessor &PP,
1122 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001123 RecordData Record;
1124
Chris Lattner0910e3b2009-04-10 17:16:57 +00001125 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001126 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001127
1128 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001129 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1130 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1131 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1132 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001133
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001134 // Write the line table.
1135 if (SourceMgr.hasLineTable()) {
1136 LineTableInfo &LineTable = SourceMgr.getLineTable();
1137
1138 // Emit the file names
1139 Record.push_back(LineTable.getNumFilenames());
1140 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1141 // Emit the file name
1142 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001143 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001144 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1145 Record.push_back(FilenameLen);
1146 if (FilenameLen)
1147 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1148 }
Mike Stump11289f42009-09-09 15:08:12 +00001149
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001150 // Emit the line entries
1151 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1152 L != LEnd; ++L) {
1153 // Emit the file ID
1154 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001155
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001156 // Emit the line entries
1157 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001158 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001159 LEEnd = L->second.end();
1160 LE != LEEnd; ++LE) {
1161 Record.push_back(LE->FileOffset);
1162 Record.push_back(LE->LineNo);
1163 Record.push_back(LE->FilenameID);
1164 Record.push_back((unsigned)LE->FileKind);
1165 Record.push_back(LE->IncludeOffset);
1166 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001167 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001168 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001169 }
1170
Douglas Gregor258ae542009-04-27 06:38:32 +00001171 // Write out the source location entry table. We skip the first
1172 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001173 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001174 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001175 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1176 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1177 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1178 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001179 // Get this source location entry.
1180 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001181
Douglas Gregor258ae542009-04-27 06:38:32 +00001182 // Record the offset of this source-location entry.
1183 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1184
1185 // Figure out which record code to use.
1186 unsigned Code;
1187 if (SLoc->isFile()) {
1188 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001189 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001190 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001191 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001192 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001193 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001194 Record.clear();
1195 Record.push_back(Code);
1196
1197 Record.push_back(SLoc->getOffset());
1198 if (SLoc->isFile()) {
1199 const SrcMgr::FileInfo &File = SLoc->getFile();
1200 Record.push_back(File.getIncludeLoc().getRawEncoding());
1201 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1202 Record.push_back(File.hasLineDirectives());
1203
1204 const SrcMgr::ContentCache *Content = File.getContentCache();
1205 if (Content->Entry) {
1206 // The source location entry is a file. The blob associated
1207 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001208
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001209 // Emit size/modification time for this file.
1210 Record.push_back(Content->Entry->getSize());
1211 Record.push_back(Content->Entry->getModificationTime());
1212
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001213 // Emit header-search information associated with this file.
1214 HeaderFileInfo HFI;
1215 HeaderSearch &HS = PP.getHeaderSearchInfo();
1216 if (Content->Entry->getUID() < HS.header_file_size())
1217 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1218 Record.push_back(HFI.isImport);
1219 Record.push_back(HFI.DirInfo);
1220 Record.push_back(HFI.NumIncludes);
1221 AddIdentifierRef(HFI.ControllingMacro, Record);
1222
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001223 // Turn the file name into an absolute path, if it isn't already.
1224 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001225 llvm::SmallString<128> FilePath(Filename);
1226 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001227 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001228
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001229 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001230 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001231
1232 // FIXME: For now, preload all file source locations, so that
1233 // we get the appropriate File entries in the reader. This is
1234 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001235 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001236 } else {
1237 // The source location entry is a buffer. The blob associated
1238 // with this entry contains the contents of the buffer.
1239
1240 // We add one to the size so that we capture the trailing NULL
1241 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1242 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001243 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001244 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001245 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001246 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1247 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001248 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001249 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001250 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001251 llvm::StringRef(Buffer->getBufferStart(),
1252 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001253
1254 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001255 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001256 }
1257 } else {
1258 // The source location entry is an instantiation.
1259 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1260 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1261 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1262 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1263
1264 // Compute the token length for this macro expansion.
1265 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001266 if (I + 1 != N)
1267 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1269 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1270 }
1271 }
1272
Douglas Gregor8f45df52009-04-16 22:23:12 +00001273 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001274
1275 if (SLocEntryOffsets.empty())
1276 return;
1277
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001278 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001279 // table is used for lazily loading source-location information.
1280 using namespace llvm;
1281 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001282 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1286 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001287
Douglas Gregor258ae542009-04-27 06:38:32 +00001288 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001289 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001290 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001291 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1292 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001293 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001294 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001295 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001296
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001297 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001298 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001299 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001300}
1301
Douglas Gregorc5046832009-04-27 18:38:38 +00001302//===----------------------------------------------------------------------===//
1303// Preprocessor Serialization
1304//===----------------------------------------------------------------------===//
1305
Chris Lattnereeffaef2009-04-10 17:15:23 +00001306/// \brief Writes the block containing the serialized form of the
1307/// preprocessor.
1308///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001309void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001310 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001311
Chris Lattner0af3ba12009-04-13 01:29:17 +00001312 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1313 if (PP.getCounterValue() != 0) {
1314 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001315 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001316 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001317 }
1318
1319 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001320 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001321
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001322 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001323 // FIXME: use diagnostics subsystem for localization etc.
1324 if (PP.SawDateOrTime())
1325 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001326
Douglas Gregor796d76a2010-10-20 22:00:55 +00001327
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001328 // Loop over all the macro definitions that are live at the end of the file,
1329 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001330 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001331 unsigned InclusionAbbrev = 0;
1332 if (PPRec) {
1333 using namespace llvm;
1334 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1335 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001343 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001344 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001345
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001346 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1347 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001348 // FIXME: This emits macros in hash table order, we should do it in a stable
1349 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001350 MacroInfo *MI = I->second;
1351
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001352 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001353 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001354 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001355
1356 // FIXME: There is a (probably minor) optimization we could do here, if
1357 // the macro comes from the original PCH but the identifier comes from a
1358 // chained PCH, by storing the offset into the original PCH rather than
1359 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001360 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001361 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001362 continue;
1363
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001364 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001365 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001366 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1367 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001368
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001369 unsigned Code;
1370 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001371 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001372 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001373 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001374
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001375 Record.push_back(MI->isC99Varargs());
1376 Record.push_back(MI->isGNUVarargs());
1377 Record.push_back(MI->getNumArgs());
1378 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1379 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001380 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001381 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001382
Douglas Gregoraae92242010-03-19 21:51:54 +00001383 // If we have a detailed preprocessing record, record the macro definition
1384 // ID that corresponds to this macro.
1385 if (PPRec)
1386 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001387
Douglas Gregor8f45df52009-04-16 22:23:12 +00001388 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001389 Record.clear();
1390
Chris Lattner2199f5b2009-04-10 18:08:30 +00001391 // Emit the tokens array.
1392 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1393 // Note that we know that the preprocessor does not have any annotation
1394 // tokens in it because they are created by the parser, and thus can't be
1395 // in a macro definition.
1396 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001397
Chris Lattner2199f5b2009-04-10 18:08:30 +00001398 Record.push_back(Tok.getLocation().getRawEncoding());
1399 Record.push_back(Tok.getLength());
1400
Chris Lattner2199f5b2009-04-10 18:08:30 +00001401 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1402 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001403 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001404
Chris Lattner2199f5b2009-04-10 18:08:30 +00001405 // FIXME: Should translate token kind to a stable encoding.
1406 Record.push_back(Tok.getKind());
1407 // FIXME: Should translate token flags to a stable encoding.
1408 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001409
Sebastian Redl539c5062010-08-18 23:57:32 +00001410 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001411 Record.clear();
1412 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001413 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001414 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001415
Douglas Gregoraae92242010-03-19 21:51:54 +00001416 // If the preprocessor has a preprocessing record, emit it.
1417 unsigned NumPreprocessingRecords = 0;
1418 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001419 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001420 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1421 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001422 E != EEnd; ++E) {
1423 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001424
Douglas Gregoraae92242010-03-19 21:51:54 +00001425 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1426 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001427 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001428
Douglas Gregor91096292010-10-02 19:29:26 +00001429 // Don't write the macro definition if it is from another AST file.
1430 if (ID < FirstMacroID)
1431 continue;
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001432
1433 // Notify the serialization listener that we're serializing this entity.
1434 if (SerializationListener)
1435 SerializationListener->SerializedPreprocessedEntity(*E,
1436 Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001437
Douglas Gregor91096292010-10-02 19:29:26 +00001438 unsigned Position = ID - FirstMacroID;
1439 if (Position != MacroDefinitionOffsets.size()) {
1440 if (Position > MacroDefinitionOffsets.size())
1441 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001442
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001443 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001444 } else
1445 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001446
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001447 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001448 Record.push_back(ID);
1449 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1450 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1451 AddIdentifierRef(MD->getName(), Record);
1452 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001453 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001454 continue;
1455 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001456
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001457 // Notify the serialization listener that we're serializing this entity.
1458 if (SerializationListener)
1459 SerializationListener->SerializedPreprocessedEntity(*E,
1460 Stream.GetCurrentBitNo());
1461
1462 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1463 Record.push_back(IndexBase + NumPreprocessingRecords++);
1464 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1465 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1466 AddIdentifierRef(MI->getName(), Record);
1467 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1468 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1469 continue;
1470 }
1471
Douglas Gregor796d76a2010-10-20 22:00:55 +00001472 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1473 Record.push_back(PP_INCLUSION_DIRECTIVE);
1474 Record.push_back(IndexBase + NumPreprocessingRecords++);
1475 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1476 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1477 Record.push_back(ID->getFileName().size());
1478 Record.push_back(ID->wasInQuotes());
1479 Record.push_back(static_cast<unsigned>(ID->getKind()));
1480 llvm::SmallString<64> Buffer;
1481 Buffer += ID->getFileName();
1482 Buffer += ID->getFile()->getName();
1483 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1484 continue;
1485 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001486
1487 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregoraae92242010-03-19 21:51:54 +00001488 }
1489 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001490
Douglas Gregor8f45df52009-04-16 22:23:12 +00001491 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001492
Douglas Gregoraae92242010-03-19 21:51:54 +00001493 // Write the offsets table for the preprocessing record.
1494 if (NumPreprocessingRecords > 0) {
1495 // Write the offsets table for identifier IDs.
1496 using namespace llvm;
1497 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001498 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1502 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001503
Douglas Gregoraae92242010-03-19 21:51:54 +00001504 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001505 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001506 Record.push_back(NumPreprocessingRecords);
1507 Record.push_back(MacroDefinitionOffsets.size());
1508 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001509 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001510 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1511 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001512}
1513
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001514void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001515 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001516 for (Diagnostic::DiagStatePointsTy::const_iterator
1517 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1518 I != E; ++I) {
1519 const Diagnostic::DiagStatePoint &point = *I;
1520 if (point.Loc.isInvalid())
1521 continue;
1522
1523 Record.push_back(point.Loc.getRawEncoding());
1524 for (Diagnostic::DiagState::iterator
1525 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1526 unsigned diag = I->first, map = I->second;
1527 if (map & 0x10) { // mapping from a diagnostic pragma.
1528 Record.push_back(diag);
1529 Record.push_back(map & 0x7);
1530 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001531 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001532 Record.push_back(-1); // mark the end of the diag/map pairs for this
1533 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001534 }
1535
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001536 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001537 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001538}
1539
Douglas Gregorc5046832009-04-27 18:38:38 +00001540//===----------------------------------------------------------------------===//
1541// Type Serialization
1542//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001543
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001544/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001545void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001546 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001547 if (Idx.getIndex() == 0) // we haven't seen this type before.
1548 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001549
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001550 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001551
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001552 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001553 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001554 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001555 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001556 else if (TypeOffsets.size() < Index) {
1557 TypeOffsets.resize(Index + 1);
1558 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001559 }
1560
1561 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001562
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001563 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001564 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001565
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001566 if (T.hasLocalNonFastQualifiers()) {
1567 Qualifiers Qs = T.getLocalQualifiers();
1568 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001569 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001570 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001571 } else {
1572 switch (T->getTypeClass()) {
1573 // For all of the concrete, non-dependent types, call the
1574 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001575#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001576 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001577#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001578#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001579 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001580 }
1581
1582 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001583 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001584
1585 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001586 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001587}
1588
Douglas Gregorc5046832009-04-27 18:38:38 +00001589//===----------------------------------------------------------------------===//
1590// Declaration Serialization
1591//===----------------------------------------------------------------------===//
1592
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001593/// \brief Write the block containing all of the declaration IDs
1594/// lexically declared within the given DeclContext.
1595///
1596/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1597/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001598uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001599 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001600 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001601 return 0;
1602
Douglas Gregor8f45df52009-04-16 22:23:12 +00001603 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001604 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001605 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001606 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001607 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1608 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001609 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001610
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001611 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001612 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001613 reinterpret_cast<char*>(Decls.data()),
1614 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001615 return Offset;
1616}
1617
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001618void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001619 using namespace llvm;
1620 RecordData Record;
1621
1622 // Write the type offsets array
1623 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001624 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1627 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1628 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001629 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001630 Record.push_back(TypeOffsets.size());
1631 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001632 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001633 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1634
1635 // Write the declaration offsets array
1636 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001637 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1640 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1641 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001642 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001643 Record.push_back(DeclOffsets.size());
1644 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001645 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001646 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1647}
1648
Douglas Gregorc5046832009-04-27 18:38:38 +00001649//===----------------------------------------------------------------------===//
1650// Global Method Pool and Selector Serialization
1651//===----------------------------------------------------------------------===//
1652
Douglas Gregore84a9da2009-04-20 20:36:09 +00001653namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001654// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001655class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001656 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001657
1658public:
1659 typedef Selector key_type;
1660 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001661
Sebastian Redl834bb972010-08-04 17:20:04 +00001662 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001663 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001664 ObjCMethodList Instance, Factory;
1665 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001666 typedef const data_type& data_type_ref;
1667
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001668 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001669
Douglas Gregorc78d3462009-04-24 21:10:55 +00001670 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001671 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001672 }
Mike Stump11289f42009-09-09 15:08:12 +00001673
1674 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001675 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1676 data_type_ref Methods) {
1677 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1678 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001679 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1680 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001681 Method = Method->Next)
1682 if (Method->Method)
1683 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001684 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001685 Method = Method->Next)
1686 if (Method->Method)
1687 DataLen += 4;
1688 clang::io::Emit16(Out, DataLen);
1689 return std::make_pair(KeyLen, DataLen);
1690 }
Mike Stump11289f42009-09-09 15:08:12 +00001691
Douglas Gregor95c13f52009-04-25 17:48:32 +00001692 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001693 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001694 assert((Start >> 32) == 0 && "Selector key offset too large");
1695 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001696 unsigned N = Sel.getNumArgs();
1697 clang::io::Emit16(Out, N);
1698 if (N == 0)
1699 N = 1;
1700 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001701 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001702 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1703 }
Mike Stump11289f42009-09-09 15:08:12 +00001704
Douglas Gregorc78d3462009-04-24 21:10:55 +00001705 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001706 data_type_ref Methods, unsigned DataLen) {
1707 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001708 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001709 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001710 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001711 Method = Method->Next)
1712 if (Method->Method)
1713 ++NumInstanceMethods;
1714
1715 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001716 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001717 Method = Method->Next)
1718 if (Method->Method)
1719 ++NumFactoryMethods;
1720
1721 clang::io::Emit16(Out, NumInstanceMethods);
1722 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001723 for (const ObjCMethodList *Method = &Methods.Instance; 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));
Sebastian Redl834bb972010-08-04 17:20:04 +00001727 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001728 Method = Method->Next)
1729 if (Method->Method)
1730 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001731
1732 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001733 }
1734};
1735} // end anonymous namespace
1736
Sebastian Redla19a67f2010-08-03 21:58:15 +00001737/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001738///
1739/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001740/// in an on-disk hash table indexed by the selector. The hash table also
1741/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001742void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001743 using namespace llvm;
1744
Sebastian Redla19a67f2010-08-03 21:58:15 +00001745 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001746 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001747 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001748 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001749 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001750 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001751 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001752 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001753
Sebastian Redla19a67f2010-08-03 21:58:15 +00001754 // Create the on-disk hash table representation. We walk through every
1755 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001756 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001757 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001758 I = SelectorIDs.begin(), E = SelectorIDs.end();
1759 I != E; ++I) {
1760 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001761 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001762 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001763 I->second,
1764 ObjCMethodList(),
1765 ObjCMethodList()
1766 };
1767 if (F != SemaRef.MethodPool.end()) {
1768 Data.Instance = F->second.first;
1769 Data.Factory = F->second.second;
1770 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001771 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001772 // changed.
1773 if (Chain && I->second < FirstSelectorID) {
1774 // Selector already exists. Did it change?
1775 bool changed = false;
1776 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1777 M = M->Next) {
1778 if (M->Method->getPCHLevel() == 0)
1779 changed = true;
1780 }
1781 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1782 M = M->Next) {
1783 if (M->Method->getPCHLevel() == 0)
1784 changed = true;
1785 }
1786 if (!changed)
1787 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001788 } else if (Data.Instance.Method || Data.Factory.Method) {
1789 // A new method pool entry.
1790 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001791 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001792 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001793 }
1794
Douglas Gregorc78d3462009-04-24 21:10:55 +00001795 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001796 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001797 uint32_t BucketOffset;
1798 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001799 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001800 llvm::raw_svector_ostream Out(MethodPool);
1801 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001802 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001803 BucketOffset = Generator.Emit(Out, Trait);
1804 }
1805
1806 // Create a blob abbreviation
1807 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001808 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1812 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1813
Douglas Gregor95c13f52009-04-25 17:48:32 +00001814 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001815 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001816 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001817 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001818 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001819 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001820
1821 // Create a blob abbreviation for the selector table offsets.
1822 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001823 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001824 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001825 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1826 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1827
1828 // Write the selector offsets table.
1829 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001830 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001831 Record.push_back(SelectorOffsets.size());
1832 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001833 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001834 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001835 }
1836}
1837
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001838/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001839void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001840 using namespace llvm;
1841 if (SemaRef.ReferencedSelectors.empty())
1842 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001843
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001844 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001845
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001846 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001847 // very tricky to fix, and given that @selector shouldn't really appear in
1848 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001849 for (DenseMap<Selector, SourceLocation>::iterator S =
1850 SemaRef.ReferencedSelectors.begin(),
1851 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1852 Selector Sel = (*S).first;
1853 SourceLocation Loc = (*S).second;
1854 AddSelectorRef(Sel, Record);
1855 AddSourceLocation(Loc, Record);
1856 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001857 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001858}
1859
Douglas Gregorc5046832009-04-27 18:38:38 +00001860//===----------------------------------------------------------------------===//
1861// Identifier Table Serialization
1862//===----------------------------------------------------------------------===//
1863
Douglas Gregorc78d3462009-04-24 21:10:55 +00001864namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001865class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001866 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001867 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001868
Douglas Gregor1d583f22009-04-28 21:18:29 +00001869 /// \brief Determines whether this is an "interesting" identifier
1870 /// that needs a full IdentifierInfo structure written into the hash
1871 /// table.
1872 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1873 return II->isPoisoned() ||
1874 II->isExtensionToken() ||
1875 II->hasMacroDefinition() ||
1876 II->getObjCOrBuiltinID() ||
1877 II->getFETokenInfo<void>();
1878 }
1879
Douglas Gregore84a9da2009-04-20 20:36:09 +00001880public:
1881 typedef const IdentifierInfo* key_type;
1882 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001883
Sebastian Redl539c5062010-08-18 23:57:32 +00001884 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001885 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001886
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001887 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001888 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001889
1890 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001891 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001892 }
Mike Stump11289f42009-09-09 15:08:12 +00001893
1894 std::pair<unsigned,unsigned>
1895 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001896 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001897 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001898 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1899 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001900 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001901 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001902 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001903 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001904 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1905 DEnd = IdentifierResolver::end();
1906 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001907 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001908 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001909 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001910 // We emit the key length after the data length so that every
1911 // string is preceded by a 16-bit length. This matches the PTH
1912 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001913 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001914 return std::make_pair(KeyLen, DataLen);
1915 }
Mike Stump11289f42009-09-09 15:08:12 +00001916
1917 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001918 unsigned KeyLen) {
1919 // Record the location of the key data. This is used when generating
1920 // the mapping from persistent IDs to strings.
1921 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001922 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001923 }
Mike Stump11289f42009-09-09 15:08:12 +00001924
1925 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001926 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001927 if (!isInterestingIdentifier(II)) {
1928 clang::io::Emit32(Out, ID << 1);
1929 return;
1930 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001931
Douglas Gregor1d583f22009-04-28 21:18:29 +00001932 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001933 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001934 bool hasMacroDefinition =
1935 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001936 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001937 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001938 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1939 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1940 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001941 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001942 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001943 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001944
Douglas Gregorc3366a52009-04-21 23:56:24 +00001945 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001946 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001947
Douglas Gregora868bbd2009-04-21 22:25:48 +00001948 // Emit the declaration IDs in reverse order, because the
1949 // IdentifierResolver provides the declarations as they would be
1950 // visible (e.g., the function "stat" would come before the struct
1951 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1952 // adds declarations to the end of the list (so we need to see the
1953 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001954 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001955 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001956 IdentifierResolver::end());
1957 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1958 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001959 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001960 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001961 }
1962};
1963} // end anonymous namespace
1964
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001965/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001966///
1967/// The identifier table consists of a blob containing string data
1968/// (the actual identifiers themselves) and a separate "offsets" index
1969/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001970void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001971 using namespace llvm;
1972
1973 // Create and write out the blob that contains the identifier
1974 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001975 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001976 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001977 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001978
Douglas Gregore6648fb2009-04-28 20:33:11 +00001979 // Look for any identifiers that were named while processing the
1980 // headers, but are otherwise not needed. We add these to the hash
1981 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001982 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001983 // file.
1984 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1985 IDEnd = PP.getIdentifierTable().end();
1986 ID != IDEnd; ++ID)
1987 getIdentifierRef(ID->second);
1988
Sebastian Redlff4a2952010-07-23 23:49:55 +00001989 // Create the on-disk hash table representation. We only store offsets
1990 // for identifiers that appear here for the first time.
1991 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001992 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001993 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1994 ID != IDEnd; ++ID) {
1995 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001996 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001997 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001998 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001999
Douglas Gregore84a9da2009-04-20 20:36:09 +00002000 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002001 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002002 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002003 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002004 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002005 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002006 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002007 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002008 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002009 }
2010
2011 // Create a blob abbreviation
2012 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002013 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002016 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002017
2018 // Write the identifier table
2019 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002020 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002021 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002022 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002023 }
2024
2025 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002026 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002027 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2030 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2031
2032 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002033 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002034 Record.push_back(IdentifierOffsets.size());
2035 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002036 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002037 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002038}
2039
Douglas Gregorc5046832009-04-27 18:38:38 +00002040//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002041// DeclContext's Name Lookup Table Serialization
2042//===----------------------------------------------------------------------===//
2043
2044namespace {
2045// Trait used for the on-disk hash table used in the method pool.
2046class ASTDeclContextNameLookupTrait {
2047 ASTWriter &Writer;
2048
2049public:
2050 typedef DeclarationName key_type;
2051 typedef key_type key_type_ref;
2052
2053 typedef DeclContext::lookup_result data_type;
2054 typedef const data_type& data_type_ref;
2055
2056 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2057
2058 unsigned ComputeHash(DeclarationName Name) {
2059 llvm::FoldingSetNodeID ID;
2060 ID.AddInteger(Name.getNameKind());
2061
2062 switch (Name.getNameKind()) {
2063 case DeclarationName::Identifier:
2064 ID.AddString(Name.getAsIdentifierInfo()->getName());
2065 break;
2066 case DeclarationName::ObjCZeroArgSelector:
2067 case DeclarationName::ObjCOneArgSelector:
2068 case DeclarationName::ObjCMultiArgSelector:
2069 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2070 break;
2071 case DeclarationName::CXXConstructorName:
2072 case DeclarationName::CXXDestructorName:
2073 case DeclarationName::CXXConversionFunctionName:
2074 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2075 break;
2076 case DeclarationName::CXXOperatorName:
2077 ID.AddInteger(Name.getCXXOverloadedOperator());
2078 break;
2079 case DeclarationName::CXXLiteralOperatorName:
2080 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2081 case DeclarationName::CXXUsingDirective:
2082 break;
2083 }
2084
2085 return ID.ComputeHash();
2086 }
2087
2088 std::pair<unsigned,unsigned>
2089 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2090 data_type_ref Lookup) {
2091 unsigned KeyLen = 1;
2092 switch (Name.getNameKind()) {
2093 case DeclarationName::Identifier:
2094 case DeclarationName::ObjCZeroArgSelector:
2095 case DeclarationName::ObjCOneArgSelector:
2096 case DeclarationName::ObjCMultiArgSelector:
2097 case DeclarationName::CXXConstructorName:
2098 case DeclarationName::CXXDestructorName:
2099 case DeclarationName::CXXConversionFunctionName:
2100 case DeclarationName::CXXLiteralOperatorName:
2101 KeyLen += 4;
2102 break;
2103 case DeclarationName::CXXOperatorName:
2104 KeyLen += 1;
2105 break;
2106 case DeclarationName::CXXUsingDirective:
2107 break;
2108 }
2109 clang::io::Emit16(Out, KeyLen);
2110
2111 // 2 bytes for num of decls and 4 for each DeclID.
2112 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2113 clang::io::Emit16(Out, DataLen);
2114
2115 return std::make_pair(KeyLen, DataLen);
2116 }
2117
2118 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2119 using namespace clang::io;
2120
2121 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2122 Emit8(Out, Name.getNameKind());
2123 switch (Name.getNameKind()) {
2124 case DeclarationName::Identifier:
2125 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2126 break;
2127 case DeclarationName::ObjCZeroArgSelector:
2128 case DeclarationName::ObjCOneArgSelector:
2129 case DeclarationName::ObjCMultiArgSelector:
2130 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2131 break;
2132 case DeclarationName::CXXConstructorName:
2133 case DeclarationName::CXXDestructorName:
2134 case DeclarationName::CXXConversionFunctionName:
2135 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2136 break;
2137 case DeclarationName::CXXOperatorName:
2138 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2139 Emit8(Out, Name.getCXXOverloadedOperator());
2140 break;
2141 case DeclarationName::CXXLiteralOperatorName:
2142 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2143 break;
2144 case DeclarationName::CXXUsingDirective:
2145 break;
2146 }
2147 }
2148
2149 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2150 data_type Lookup, unsigned DataLen) {
2151 uint64_t Start = Out.tell(); (void)Start;
2152 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2153 for (; Lookup.first != Lookup.second; ++Lookup.first)
2154 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2155
2156 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2157 }
2158};
2159} // end anonymous namespace
2160
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002161/// \brief Write the block containing all of the declaration IDs
2162/// visible from the given DeclContext.
2163///
2164/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002165/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002166uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2167 DeclContext *DC) {
2168 if (DC->getPrimaryContext() != DC)
2169 return 0;
2170
2171 // Since there is no name lookup into functions or methods, don't bother to
2172 // build a visible-declarations table for these entities.
2173 if (DC->isFunctionOrMethod())
2174 return 0;
2175
2176 // If not in C++, we perform name lookup for the translation unit via the
2177 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2178 // FIXME: In C++ we need the visible declarations in order to "see" the
2179 // friend declarations, is there a way to do this without writing the table ?
2180 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2181 return 0;
2182
2183 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002184 if (DC->hasExternalVisibleStorage())
2185 DC->MaterializeVisibleDeclsFromExternalStorage();
2186 else
2187 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002188
2189 // Serialize the contents of the mapping used for lookup. Note that,
2190 // although we have two very different code paths, the serialized
2191 // representation is the same for both cases: a declaration name,
2192 // followed by a size, followed by references to the visible
2193 // declarations that have that name.
2194 uint64_t Offset = Stream.GetCurrentBitNo();
2195 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2196 if (!Map || Map->empty())
2197 return 0;
2198
2199 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2200 ASTDeclContextNameLookupTrait Trait(*this);
2201
2202 // Create the on-disk hash table representation.
2203 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2204 D != DEnd; ++D) {
2205 DeclarationName Name = D->first;
2206 DeclContext::lookup_result Result = D->second.getLookupResult();
2207 Generator.insert(Name, Result, Trait);
2208 }
2209
2210 // Create the on-disk hash table in a buffer.
2211 llvm::SmallString<4096> LookupTable;
2212 uint32_t BucketOffset;
2213 {
2214 llvm::raw_svector_ostream Out(LookupTable);
2215 // Make sure that no bucket is at offset 0
2216 clang::io::Emit32(Out, 0);
2217 BucketOffset = Generator.Emit(Out, Trait);
2218 }
2219
2220 // Write the lookup table
2221 RecordData Record;
2222 Record.push_back(DECL_CONTEXT_VISIBLE);
2223 Record.push_back(BucketOffset);
2224 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2225 LookupTable.str());
2226
2227 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2228 ++NumVisibleDeclContexts;
2229 return Offset;
2230}
2231
Sebastian Redla4071b42010-08-24 00:50:09 +00002232/// \brief Write an UPDATE_VISIBLE block for the given context.
2233///
2234/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2235/// DeclContext in a dependent AST file. As such, they only exist for the TU
2236/// (in C++) and for namespaces.
2237void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002238 // Make the context build its lookup table, but don't make it load external
2239 // decls.
2240 DC->lookup(DeclarationName());
2241
2242 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2243 if (!Map || Map->empty())
2244 return;
2245
2246 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2247 ASTDeclContextNameLookupTrait Trait(*this);
2248
2249 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002250 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2251 D != DEnd; ++D) {
2252 DeclarationName Name = D->first;
2253 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002254 // For any name that appears in this table, the results are complete, i.e.
2255 // they overwrite results from previous PCHs. Merging is always a mess.
2256 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002257 }
2258
2259 // Create the on-disk hash table in a buffer.
2260 llvm::SmallString<4096> LookupTable;
2261 uint32_t BucketOffset;
2262 {
2263 llvm::raw_svector_ostream Out(LookupTable);
2264 // Make sure that no bucket is at offset 0
2265 clang::io::Emit32(Out, 0);
2266 BucketOffset = Generator.Emit(Out, Trait);
2267 }
2268
2269 // Write the lookup table
2270 RecordData Record;
2271 Record.push_back(UPDATE_VISIBLE);
2272 Record.push_back(getDeclID(cast<Decl>(DC)));
2273 Record.push_back(BucketOffset);
2274 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2275}
2276
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002277//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002278// General Serialization Routines
2279//===----------------------------------------------------------------------===//
2280
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002281/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002282void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002283 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002284 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2285 const Attr * A = *i;
2286 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2287 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002288
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002289#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002290
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002291 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002292}
2293
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002294void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002295 Record.push_back(Str.size());
2296 Record.insert(Record.end(), Str.begin(), Str.end());
2297}
2298
Douglas Gregore84a9da2009-04-20 20:36:09 +00002299/// \brief Note that the identifier II occurs at the given offset
2300/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002301void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002302 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002303 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002304 // up earlier in the chain and thus don't need an offset.
2305 if (ID >= FirstIdentID)
2306 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002307}
2308
Douglas Gregor95c13f52009-04-25 17:48:32 +00002309/// \brief Note that the selector Sel occurs at the given offset
2310/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002311void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002312 unsigned ID = SelectorIDs[Sel];
2313 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002314 // Don't record offsets for selectors that are also available in a different
2315 // file.
2316 if (ID < FirstSelectorID)
2317 return;
2318 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002319}
2320
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002321ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002322 : Stream(Stream), Chain(0), SerializationListener(0),
2323 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002324 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002325 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002326 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2327 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002328 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002329 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2330 NextCXXBaseSpecifiersID(1)
2331{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002332}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002333
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002334void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002335 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002336 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002337 Stream.Emit((unsigned)'C', 8);
2338 Stream.Emit((unsigned)'P', 8);
2339 Stream.Emit((unsigned)'C', 8);
2340 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002341
Chris Lattner28fa4e62009-04-26 22:26:21 +00002342 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002343
Sebastian Redl143413f2010-07-12 22:02:52 +00002344 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002345 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002346 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002347 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002348}
2349
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002350void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002351 const char *isysroot) {
2352 using namespace llvm;
2353
2354 ASTContext &Context = SemaRef.Context;
2355 Preprocessor &PP = SemaRef.PP;
2356
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002357 // The translation unit is the first declaration we'll emit.
2358 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002359 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002360 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002361
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002362 // Make sure that we emit IdentifierInfos (and any attached
2363 // declarations) for builtins.
2364 {
2365 IdentifierTable &Table = PP.getIdentifierTable();
2366 llvm::SmallVector<const char *, 32> BuiltinNames;
2367 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2368 Context.getLangOptions().NoBuiltin);
2369 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2370 getIdentifierRef(&Table.get(BuiltinNames[I]));
2371 }
2372
Chris Lattner0c797362009-09-08 18:19:27 +00002373 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002374 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002375 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002376 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002377 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2378 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002379 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002380
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002381 // Build a record containing all of the file scoped decls in this file.
2382 RecordData UnusedFileScopedDecls;
2383 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2384 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002385
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002386 RecordData WeakUndeclaredIdentifiers;
2387 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2388 WeakUndeclaredIdentifiers.push_back(
2389 SemaRef.WeakUndeclaredIdentifiers.size());
2390 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2391 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2392 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2393 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2394 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2395 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2396 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2397 }
2398 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002399
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002400 // Build a record containing all of the locally-scoped external
2401 // declarations in this header file. Generally, this record will be
2402 // empty.
2403 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002404 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002405 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002406 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002407 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2408 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2409 TD != TDEnd; ++TD)
2410 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2411
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002412 // Build a record containing all of the ext_vector declarations.
2413 RecordData ExtVectorDecls;
2414 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2415 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2416
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002417 // Build a record containing all of the VTable uses information.
2418 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002419 if (!SemaRef.VTableUses.empty()) {
2420 VTableUses.push_back(SemaRef.VTableUses.size());
2421 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2422 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2423 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2424 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2425 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002426 }
2427
2428 // Build a record containing all of dynamic classes declarations.
2429 RecordData DynamicClasses;
2430 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2431 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2432
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002433 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002434 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002435 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002436 I = SemaRef.PendingInstantiations.begin(),
2437 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2438 AddDeclRef(I->first, PendingInstantiations);
2439 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002440 }
2441 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2442 "There are local ones at end of translation unit!");
2443
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002444 // Build a record containing some declaration references.
2445 RecordData SemaDeclRefs;
2446 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2447 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2448 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2449 }
2450
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002451 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002452 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002453 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002454 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002455 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002456 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002457 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002458 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002459 // Write the record of special types.
2460 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002461
Steve Naroffc277ad12009-07-18 15:33:26 +00002462 AddTypeRef(Context.getBuiltinVaListType(), Record);
2463 AddTypeRef(Context.getObjCIdType(), Record);
2464 AddTypeRef(Context.getObjCSelType(), Record);
2465 AddTypeRef(Context.getObjCProtoType(), Record);
2466 AddTypeRef(Context.getObjCClassType(), Record);
2467 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2468 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2469 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002470 AddTypeRef(Context.getjmp_bufType(), Record);
2471 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002472 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2473 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002474 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002475 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002476 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2477 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002478 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002479 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002480
Douglas Gregor1970d882009-04-26 03:49:13 +00002481 // Keep writing types and declarations until all types and
2482 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002483 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002484 WriteDeclsBlockAbbrevs();
2485 while (!DeclTypesToEmit.empty()) {
2486 DeclOrType DOT = DeclTypesToEmit.front();
2487 DeclTypesToEmit.pop();
2488 if (DOT.isType())
2489 WriteType(DOT.getType());
2490 else
2491 WriteDecl(Context, DOT.getDecl());
2492 }
2493 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002494
Douglas Gregor45053152009-10-17 17:25:45 +00002495 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002496 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002497 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002498 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002499
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002500 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002501 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002502
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002503 // Write the C++ base-specifier set offsets.
2504 if (!CXXBaseSpecifiersOffsets.empty()) {
2505 // Create a blob abbreviation for the C++ base specifiers offsets.
2506 using namespace llvm;
2507
2508 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2509 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2512 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2513
2514 // Write the selector offsets table.
2515 Record.clear();
2516 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2517 Record.push_back(CXXBaseSpecifiersOffsets.size());
2518 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2519 (const char *)CXXBaseSpecifiersOffsets.data(),
2520 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2521 }
2522
Douglas Gregord4df8652009-04-22 22:02:47 +00002523 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002524 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002525 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002526
2527 // Write the record containing tentative definitions.
2528 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002529 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002530
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002531 // Write the record containing unused file scoped decls.
2532 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002533 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002534
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002535 // Write the record containing weak undeclared identifiers.
2536 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002537 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002538 WeakUndeclaredIdentifiers);
2539
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002540 // Write the record containing locally-scoped external definitions.
2541 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002542 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002543 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002544
2545 // Write the record containing ext_vector type names.
2546 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002547 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002548
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002549 // Write the record containing VTable uses information.
2550 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002551 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002552
2553 // Write the record containing dynamic classes declarations.
2554 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002555 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002556
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002557 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002558 if (!PendingInstantiations.empty())
2559 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002560
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002561 // Write the record containing declaration references of Sema.
2562 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002563 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002564
Douglas Gregor08f01292009-04-17 22:13:46 +00002565 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002566 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002567 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002568 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002569 Record.push_back(NumLexicalDeclContexts);
2570 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002571 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002572 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002573}
2574
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002575void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002576 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002577 using namespace llvm;
2578
2579 ASTContext &Context = SemaRef.Context;
2580 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002581
Sebastian Redl143413f2010-07-12 22:02:52 +00002582 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002583 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002584 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002585 if (StatCalls && !isysroot)
2586 WriteStatCache(*StatCalls);
2587 // FIXME: Source manager block should only write new stuff, which could be
2588 // done by tracking the largest ID in the chain
2589 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002590
2591 // The special types are in the chained PCH.
2592
2593 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002594 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002595 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002596 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002597 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2598 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002599 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002600 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002601 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002602 else if ((*I)->isChangedSinceDeserialization())
2603 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002604 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002605 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002606 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002607 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002608 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2609 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2610 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002611 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002612 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2613 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002614 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002615 // And a visible updates block for the DeclContexts.
2616 Abv = new llvm::BitCodeAbbrev();
2617 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2618 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2619 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2620 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2621 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2622 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002623
Sebastian Redl98912122010-07-27 23:01:28 +00002624 // Build a record containing all of the new tentative definitions in this
2625 // file, in TentativeDefinitions order.
2626 RecordData TentativeDefinitions;
2627 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2628 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2629 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2630 }
2631
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002632 // Build a record containing all of the file scoped decls in this file.
2633 RecordData UnusedFileScopedDecls;
2634 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2635 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2636 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002637 }
2638
Sebastian Redl08aca90252010-08-05 18:21:25 +00002639 // We write the entire table, overwriting the tables from the chain.
2640 RecordData WeakUndeclaredIdentifiers;
2641 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2642 WeakUndeclaredIdentifiers.push_back(
2643 SemaRef.WeakUndeclaredIdentifiers.size());
2644 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2645 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2646 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2647 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2648 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2649 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2650 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2651 }
2652 }
2653
Sebastian Redl98912122010-07-27 23:01:28 +00002654 // Build a record containing all of the locally-scoped external
2655 // declarations in this header file. Generally, this record will be
2656 // empty.
2657 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002658 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002659 // nondeterminstic!
2660 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2661 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2662 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2663 TD != TDEnd; ++TD) {
2664 if (TD->second->getPCHLevel() == 0)
2665 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2666 }
2667
2668 // Build a record containing all of the ext_vector declarations.
2669 RecordData ExtVectorDecls;
2670 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2671 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2672 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2673 }
2674
Sebastian Redl08aca90252010-08-05 18:21:25 +00002675 // Build a record containing all of the VTable uses information.
2676 // We write everything here, because it's too hard to determine whether
2677 // a use is new to this part.
2678 RecordData VTableUses;
2679 if (!SemaRef.VTableUses.empty()) {
2680 VTableUses.push_back(SemaRef.VTableUses.size());
2681 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2682 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2683 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2684 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2685 }
2686 }
2687
2688 // Build a record containing all of dynamic classes declarations.
2689 RecordData DynamicClasses;
2690 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2691 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2692 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2693
2694 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002695 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002696 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002697 I = SemaRef.PendingInstantiations.begin(),
2698 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002699 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002700 AddDeclRef(I->first, PendingInstantiations);
2701 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002702 }
2703 }
2704 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2705 "There are local ones at end of translation unit!");
2706
2707 // Build a record containing some declaration references.
2708 // It's not worth the effort to avoid duplication here.
2709 RecordData SemaDeclRefs;
2710 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2711 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2712 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2713 }
2714
Sebastian Redl539c5062010-08-18 23:57:32 +00002715 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002716 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002717 for (DeclsToRewriteTy::iterator
2718 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2719 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002720 while (!DeclTypesToEmit.empty()) {
2721 DeclOrType DOT = DeclTypesToEmit.front();
2722 DeclTypesToEmit.pop();
2723 if (DOT.isType())
2724 WriteType(DOT.getType());
2725 else
2726 WriteDecl(Context, DOT.getDecl());
2727 }
2728 Stream.ExitBlock();
2729
Sebastian Redl98912122010-07-27 23:01:28 +00002730 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002731 WriteSelectors(SemaRef);
2732 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002733 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002734 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002735 // FIXME: For chained PCH only write the new mappings (we currently
2736 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002737 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002738
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002739 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002740 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002741 RecordData FirstLatestDeclIDs;
2742 for (FirstLatestDeclMap::iterator
2743 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2744 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2745 "Expected first & second to be in different PCHs");
2746 AddDeclRef(I->first, FirstLatestDeclIDs);
2747 AddDeclRef(I->second, FirstLatestDeclIDs);
2748 }
2749 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002750 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002751
Sebastian Redl98912122010-07-27 23:01:28 +00002752 // Write the record containing external, unnamed definitions.
2753 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002754 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002755
2756 // Write the record containing tentative definitions.
2757 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002758 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002759
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002760 // Write the record containing unused file scoped decls.
2761 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002762 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002763
Sebastian Redl08aca90252010-08-05 18:21:25 +00002764 // Write the record containing weak undeclared identifiers.
2765 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002766 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002767 WeakUndeclaredIdentifiers);
2768
Sebastian Redl98912122010-07-27 23:01:28 +00002769 // Write the record containing locally-scoped external definitions.
2770 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002771 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002772 LocallyScopedExternalDecls);
2773
2774 // Write the record containing ext_vector type names.
2775 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002776 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002777
Sebastian Redl08aca90252010-08-05 18:21:25 +00002778 // Write the record containing VTable uses information.
2779 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002780 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002781
2782 // Write the record containing dynamic classes declarations.
2783 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002784 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002785
2786 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002787 if (!PendingInstantiations.empty())
2788 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002789
2790 // Write the record containing declaration references of Sema.
2791 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002792 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002793
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002794 // Write the updates to DeclContexts.
2795 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2796 I = UpdatedDeclContexts.begin(),
2797 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002798 I != E; ++I)
2799 WriteDeclContextVisibleUpdate(*I);
2800
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002801 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002802
Sebastian Redl98912122010-07-27 23:01:28 +00002803 Record.clear();
2804 Record.push_back(NumStatements);
2805 Record.push_back(NumMacros);
2806 Record.push_back(NumLexicalDeclContexts);
2807 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002808 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002809 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002810 Stream.ExitBlock();
2811}
2812
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002813void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002814 if (DeclUpdates.empty())
2815 return;
2816
2817 RecordData OffsetsRecord;
2818 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2819 for (DeclUpdateMap::iterator
2820 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2821 const Decl *D = I->first;
2822 UpdateRecord &URec = I->second;
2823
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002824 if (DeclsToRewrite.count(D))
2825 continue; // The decl will be written completely,no need to store updates.
2826
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002827 uint64_t Offset = Stream.GetCurrentBitNo();
2828 Stream.EmitRecord(DECL_UPDATES, URec);
2829
2830 OffsetsRecord.push_back(GetDeclRef(D));
2831 OffsetsRecord.push_back(Offset);
2832 }
2833 Stream.ExitBlock();
2834 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2835}
2836
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002837void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002838 if (ReplacedDecls.empty())
2839 return;
2840
2841 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002842 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002843 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2844 Record.push_back(I->first);
2845 Record.push_back(I->second);
2846 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002847 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002848}
2849
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002850void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002851 Record.push_back(Loc.getRawEncoding());
2852}
2853
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002854void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002855 AddSourceLocation(Range.getBegin(), Record);
2856 AddSourceLocation(Range.getEnd(), Record);
2857}
2858
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002859void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002860 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002861 const uint64_t *Words = Value.getRawData();
2862 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002863}
2864
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002865void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002866 Record.push_back(Value.isUnsigned());
2867 AddAPInt(Value, Record);
2868}
2869
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002870void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002871 AddAPInt(Value.bitcastToAPInt(), Record);
2872}
2873
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002874void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002875 Record.push_back(getIdentifierRef(II));
2876}
2877
Sebastian Redl539c5062010-08-18 23:57:32 +00002878IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002879 if (II == 0)
2880 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002881
Sebastian Redl539c5062010-08-18 23:57:32 +00002882 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002883 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002884 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002885 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002886}
2887
Sebastian Redl50e26582010-09-15 19:54:06 +00002888MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002889 if (MD == 0)
2890 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002891
2892 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002893 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002894 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002895 return ID;
2896}
2897
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002898void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002899 Record.push_back(getSelectorRef(SelRef));
2900}
2901
Sebastian Redl539c5062010-08-18 23:57:32 +00002902SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002903 if (Sel.getAsOpaquePtr() == 0) {
2904 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002905 }
2906
Sebastian Redl539c5062010-08-18 23:57:32 +00002907 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002908 if (SID == 0 && Chain) {
2909 // This might trigger a ReadSelector callback, which will set the ID for
2910 // this selector.
2911 Chain->LoadSelector(Sel);
2912 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002913 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002914 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002915 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002916 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002917}
2918
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002919void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002920 AddDeclRef(Temp->getDestructor(), Record);
2921}
2922
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002923void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2924 CXXBaseSpecifier const *BasesEnd,
2925 RecordDataImpl &Record) {
2926 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2927 CXXBaseSpecifiersToWrite.push_back(
2928 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2929 Bases, BasesEnd));
2930 Record.push_back(NextCXXBaseSpecifiersID++);
2931}
2932
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002933void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002934 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002935 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002936 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002937 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002938 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002939 break;
2940 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002941 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002942 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002943 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002944 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2945 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002946 break;
2947 case TemplateArgument::TemplateExpansion:
2948 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2949 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00002950 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002951 break;
John McCall0ad16662009-10-29 08:12:44 +00002952 case TemplateArgument::Null:
2953 case TemplateArgument::Integral:
2954 case TemplateArgument::Declaration:
2955 case TemplateArgument::Pack:
2956 break;
2957 }
2958}
2959
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002960void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002961 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002962 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002963
2964 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2965 bool InfoHasSameExpr
2966 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2967 Record.push_back(InfoHasSameExpr);
2968 if (InfoHasSameExpr)
2969 return; // Avoid storing the same expr twice.
2970 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002971 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2972 Record);
2973}
2974
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002975void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002976 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002977 AddTypeRef(QualType(), Record);
2978 return;
2979 }
2980
John McCallbcd03502009-12-07 02:54:59 +00002981 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002982 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002983 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002984 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002985}
2986
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002987void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002988 Record.push_back(GetOrCreateTypeID(T));
2989}
2990
2991TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002992 return MakeTypeID(T,
2993 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2994}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002995
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002996TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002997 return MakeTypeID(T,
2998 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002999}
3000
3001TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3002 if (T.isNull())
3003 return TypeIdx();
3004 assert(!T.getLocalFastQualifiers());
3005
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003006 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003007 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003008 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003009 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003010 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003011 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003012 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003013 return Idx;
3014}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003015
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003016TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003017 if (T.isNull())
3018 return TypeIdx();
3019 assert(!T.getLocalFastQualifiers());
3020
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003021 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3022 assert(I != TypeIdxs.end() && "Type not emitted!");
3023 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003024}
3025
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003026void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003027 Record.push_back(GetDeclRef(D));
3028}
3029
Sebastian Redl539c5062010-08-18 23:57:32 +00003030DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003031 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003032 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003033 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003034 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003035 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003036 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003037 // We haven't seen this declaration before. Give it a new ID and
3038 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003039 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003040 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003041 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3042 // We don't add it to the replacement collection here, because we don't
3043 // have the offset yet.
3044 DeclTypesToEmit.push(const_cast<Decl *>(D));
3045 // Reset the flag, so that we don't add this decl multiple times.
3046 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003047 }
3048
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003049 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003050}
3051
Sebastian Redl539c5062010-08-18 23:57:32 +00003052DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003053 if (D == 0)
3054 return 0;
3055
3056 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3057 return DeclIDs[D];
3058}
3059
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003060void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003061 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003062 Record.push_back(Name.getNameKind());
3063 switch (Name.getNameKind()) {
3064 case DeclarationName::Identifier:
3065 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3066 break;
3067
3068 case DeclarationName::ObjCZeroArgSelector:
3069 case DeclarationName::ObjCOneArgSelector:
3070 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003071 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003072 break;
3073
3074 case DeclarationName::CXXConstructorName:
3075 case DeclarationName::CXXDestructorName:
3076 case DeclarationName::CXXConversionFunctionName:
3077 AddTypeRef(Name.getCXXNameType(), Record);
3078 break;
3079
3080 case DeclarationName::CXXOperatorName:
3081 Record.push_back(Name.getCXXOverloadedOperator());
3082 break;
3083
Alexis Hunt3d221f22009-11-29 07:34:05 +00003084 case DeclarationName::CXXLiteralOperatorName:
3085 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3086 break;
3087
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003088 case DeclarationName::CXXUsingDirective:
3089 // No extra data to emit
3090 break;
3091 }
3092}
Chris Lattnerca025db2010-05-07 21:43:38 +00003093
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003094void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003095 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003096 switch (Name.getNameKind()) {
3097 case DeclarationName::CXXConstructorName:
3098 case DeclarationName::CXXDestructorName:
3099 case DeclarationName::CXXConversionFunctionName:
3100 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3101 break;
3102
3103 case DeclarationName::CXXOperatorName:
3104 AddSourceLocation(
3105 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3106 Record);
3107 AddSourceLocation(
3108 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3109 Record);
3110 break;
3111
3112 case DeclarationName::CXXLiteralOperatorName:
3113 AddSourceLocation(
3114 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3115 Record);
3116 break;
3117
3118 case DeclarationName::Identifier:
3119 case DeclarationName::ObjCZeroArgSelector:
3120 case DeclarationName::ObjCOneArgSelector:
3121 case DeclarationName::ObjCMultiArgSelector:
3122 case DeclarationName::CXXUsingDirective:
3123 break;
3124 }
3125}
3126
3127void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003128 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003129 AddDeclarationName(NameInfo.getName(), Record);
3130 AddSourceLocation(NameInfo.getLoc(), Record);
3131 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3132}
3133
3134void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003135 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003136 AddNestedNameSpecifier(Info.NNS, Record);
3137 AddSourceRange(Info.NNSRange, Record);
3138 Record.push_back(Info.NumTemplParamLists);
3139 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3140 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3141}
3142
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003143void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003144 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003145 // Nested name specifiers usually aren't too long. I think that 8 would
3146 // typically accomodate the vast majority.
3147 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3148
3149 // Push each of the NNS's onto a stack for serialization in reverse order.
3150 while (NNS) {
3151 NestedNames.push_back(NNS);
3152 NNS = NNS->getPrefix();
3153 }
3154
3155 Record.push_back(NestedNames.size());
3156 while(!NestedNames.empty()) {
3157 NNS = NestedNames.pop_back_val();
3158 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3159 Record.push_back(Kind);
3160 switch (Kind) {
3161 case NestedNameSpecifier::Identifier:
3162 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3163 break;
3164
3165 case NestedNameSpecifier::Namespace:
3166 AddDeclRef(NNS->getAsNamespace(), Record);
3167 break;
3168
3169 case NestedNameSpecifier::TypeSpec:
3170 case NestedNameSpecifier::TypeSpecWithTemplate:
3171 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3172 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3173 break;
3174
3175 case NestedNameSpecifier::Global:
3176 // Don't need to write an associated value.
3177 break;
3178 }
3179 }
3180}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003181
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003182void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003183 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003184 Record.push_back(Kind);
3185 switch (Kind) {
3186 case TemplateName::Template:
3187 AddDeclRef(Name.getAsTemplateDecl(), Record);
3188 break;
3189
3190 case TemplateName::OverloadedTemplate: {
3191 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3192 Record.push_back(OvT->size());
3193 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3194 I != E; ++I)
3195 AddDeclRef(*I, Record);
3196 break;
3197 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003198
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003199 case TemplateName::QualifiedTemplate: {
3200 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3201 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3202 Record.push_back(QualT->hasTemplateKeyword());
3203 AddDeclRef(QualT->getTemplateDecl(), Record);
3204 break;
3205 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003206
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003207 case TemplateName::DependentTemplate: {
3208 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3209 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3210 Record.push_back(DepT->isIdentifier());
3211 if (DepT->isIdentifier())
3212 AddIdentifierRef(DepT->getIdentifier(), Record);
3213 else
3214 Record.push_back(DepT->getOperator());
3215 break;
3216 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003217
3218 case TemplateName::SubstTemplateTemplateParmPack: {
3219 SubstTemplateTemplateParmPackStorage *SubstPack
3220 = Name.getAsSubstTemplateTemplateParmPack();
3221 AddDeclRef(SubstPack->getParameterPack(), Record);
3222 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3223 break;
3224 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003225 }
3226}
3227
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003228void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003229 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003230 Record.push_back(Arg.getKind());
3231 switch (Arg.getKind()) {
3232 case TemplateArgument::Null:
3233 break;
3234 case TemplateArgument::Type:
3235 AddTypeRef(Arg.getAsType(), Record);
3236 break;
3237 case TemplateArgument::Declaration:
3238 AddDeclRef(Arg.getAsDecl(), Record);
3239 break;
3240 case TemplateArgument::Integral:
3241 AddAPSInt(*Arg.getAsIntegral(), Record);
3242 AddTypeRef(Arg.getIntegralType(), Record);
3243 break;
3244 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003245 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3246 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003247 case TemplateArgument::TemplateExpansion:
3248 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003249 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3250 Record.push_back(*NumExpansions + 1);
3251 else
3252 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003253 break;
3254 case TemplateArgument::Expression:
3255 AddStmt(Arg.getAsExpr());
3256 break;
3257 case TemplateArgument::Pack:
3258 Record.push_back(Arg.pack_size());
3259 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3260 I != E; ++I)
3261 AddTemplateArgument(*I, Record);
3262 break;
3263 }
3264}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003265
3266void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003267ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003268 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003269 assert(TemplateParams && "No TemplateParams!");
3270 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3271 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3272 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3273 Record.push_back(TemplateParams->size());
3274 for (TemplateParameterList::const_iterator
3275 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3276 P != PEnd; ++P)
3277 AddDeclRef(*P, Record);
3278}
3279
3280/// \brief Emit a template argument list.
3281void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003282ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003283 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003284 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003285 Record.push_back(TemplateArgs->size());
3286 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003287 AddTemplateArgument(TemplateArgs->get(i), Record);
3288}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003289
3290
3291void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003292ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003293 Record.push_back(Set.size());
3294 for (UnresolvedSetImpl::const_iterator
3295 I = Set.begin(), E = Set.end(); I != E; ++I) {
3296 AddDeclRef(I.getDecl(), Record);
3297 Record.push_back(I.getAccess());
3298 }
3299}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003300
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003301void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003302 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003303 Record.push_back(Base.isVirtual());
3304 Record.push_back(Base.isBaseOfClass());
3305 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003306 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003307 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003308 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3309 : SourceLocation(),
3310 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003311}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003312
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003313void ASTWriter::FlushCXXBaseSpecifiers() {
3314 RecordData Record;
3315 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3316 Record.clear();
3317
3318 // Record the offset of this base-specifier set.
3319 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3320 if (Index == CXXBaseSpecifiersOffsets.size())
3321 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3322 else {
3323 if (Index > CXXBaseSpecifiersOffsets.size())
3324 CXXBaseSpecifiersOffsets.resize(Index + 1);
3325 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3326 }
3327
3328 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3329 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3330 Record.push_back(BEnd - B);
3331 for (; B != BEnd; ++B)
3332 AddCXXBaseSpecifier(*B, Record);
3333 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003334
3335 // Flush any expressions that were written as part of the base specifiers.
3336 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003337 }
3338
3339 CXXBaseSpecifiersToWrite.clear();
3340}
3341
Alexis Hunt1d792652011-01-08 20:30:50 +00003342void ASTWriter::AddCXXCtorInitializers(
3343 const CXXCtorInitializer * const *CtorInitializers,
3344 unsigned NumCtorInitializers,
3345 RecordDataImpl &Record) {
3346 Record.push_back(NumCtorInitializers);
3347 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3348 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003349
3350 Record.push_back(Init->isBaseInitializer());
3351 if (Init->isBaseInitializer()) {
3352 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3353 Record.push_back(Init->isBaseVirtual());
3354 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003355 Record.push_back(Init->isIndirectMemberInitializer());
3356 if (Init->isIndirectMemberInitializer())
3357 AddDeclRef(Init->getIndirectMember(), Record);
3358 else
3359 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003360 }
Francois Pichetd583da02010-12-04 09:14:42 +00003361
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003362 AddSourceLocation(Init->getMemberLocation(), Record);
3363 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003364 AddSourceLocation(Init->getLParenLoc(), Record);
3365 AddSourceLocation(Init->getRParenLoc(), Record);
3366 Record.push_back(Init->isWritten());
3367 if (Init->isWritten()) {
3368 Record.push_back(Init->getSourceOrder());
3369 } else {
3370 Record.push_back(Init->getNumArrayIndices());
3371 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3372 AddDeclRef(Init->getArrayIndex(i), Record);
3373 }
3374 }
3375}
3376
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003377void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3378 assert(D->DefinitionData);
3379 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3380 Record.push_back(Data.UserDeclaredConstructor);
3381 Record.push_back(Data.UserDeclaredCopyConstructor);
3382 Record.push_back(Data.UserDeclaredCopyAssignment);
3383 Record.push_back(Data.UserDeclaredDestructor);
3384 Record.push_back(Data.Aggregate);
3385 Record.push_back(Data.PlainOldData);
3386 Record.push_back(Data.Empty);
3387 Record.push_back(Data.Polymorphic);
3388 Record.push_back(Data.Abstract);
3389 Record.push_back(Data.HasTrivialConstructor);
3390 Record.push_back(Data.HasTrivialCopyConstructor);
3391 Record.push_back(Data.HasTrivialCopyAssignment);
3392 Record.push_back(Data.HasTrivialDestructor);
3393 Record.push_back(Data.ComputedVisibleConversions);
3394 Record.push_back(Data.DeclaredDefaultConstructor);
3395 Record.push_back(Data.DeclaredCopyConstructor);
3396 Record.push_back(Data.DeclaredCopyAssignment);
3397 Record.push_back(Data.DeclaredDestructor);
3398
3399 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003400 if (Data.NumBases > 0)
3401 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3402 Record);
3403
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003404 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3405 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003406 if (Data.NumVBases > 0)
3407 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3408 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003409
3410 AddUnresolvedSet(Data.Conversions, Record);
3411 AddUnresolvedSet(Data.VisibleConversions, Record);
3412 // Data.Definition is the owning decl, no need to write it.
3413 AddDeclRef(Data.FirstFriend, Record);
3414}
3415
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003416void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003417 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003418 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003419 assert(FirstDeclID == NextDeclID &&
3420 FirstTypeID == NextTypeID &&
3421 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003422 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003423 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003424 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003425 "Setting chain after writing has started.");
3426 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003427
3428 FirstDeclID += Chain->getTotalNumDecls();
3429 FirstTypeID += Chain->getTotalNumTypes();
3430 FirstIdentID += Chain->getTotalNumIdentifiers();
3431 FirstSelectorID += Chain->getTotalNumSelectors();
3432 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003433 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003434 NextDeclID = FirstDeclID;
3435 NextTypeID = FirstTypeID;
3436 NextIdentID = FirstIdentID;
3437 NextSelectorID = FirstSelectorID;
3438 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003439 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003440}
3441
Sebastian Redl539c5062010-08-18 23:57:32 +00003442void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003443 IdentifierIDs[II] = ID;
3444}
3445
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003446void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003447 // Always take the highest-numbered type index. This copes with an interesting
3448 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003449 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003450 // keep the higher-numbered entry so that we can properly write it out to
3451 // the AST file.
3452 TypeIdx &StoredIdx = TypeIdxs[T];
3453 if (Idx.getIndex() >= StoredIdx.getIndex())
3454 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003455}
3456
Sebastian Redl539c5062010-08-18 23:57:32 +00003457void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003458 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003459}
Sebastian Redl834bb972010-08-04 17:20:04 +00003460
Sebastian Redl539c5062010-08-18 23:57:32 +00003461void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003462 SelectorIDs[S] = ID;
3463}
Douglas Gregor91096292010-10-02 19:29:26 +00003464
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003465void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003466 MacroDefinition *MD) {
3467 MacroDefinitions[MD] = ID;
3468}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003469
3470void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3471 assert(D->isDefinition());
3472 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3473 // We are interested when a PCH decl is modified.
3474 if (RD->getPCHLevel() > 0) {
3475 // A forward reference was mutated into a definition. Rewrite it.
3476 // FIXME: This happens during template instantiation, should we
3477 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003478 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003479 }
3480
3481 for (CXXRecordDecl::redecl_iterator
3482 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3483 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3484 if (Redecl == RD)
3485 continue;
3486
3487 // We are interested when a PCH decl is modified.
3488 if (Redecl->getPCHLevel() > 0) {
3489 UpdateRecord &Record = DeclUpdates[Redecl];
3490 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3491 assert(Redecl->DefinitionData);
3492 assert(Redecl->DefinitionData->Definition == D);
3493 AddDeclRef(D, Record); // the DefinitionDecl
3494 }
3495 }
3496 }
3497}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003498void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3499 // TU and namespaces are handled elsewhere.
3500 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3501 return;
3502
3503 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3504 return; // Not a source decl added to a DeclContext from PCH.
3505
3506 AddUpdatedDeclContext(DC);
3507}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003508
3509void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3510 assert(D->isImplicit());
3511 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3512 return; // Not a source member added to a class from PCH.
3513 if (!isa<CXXMethodDecl>(D))
3514 return; // We are interested in lazily declared implicit methods.
3515
3516 // A decl coming from PCH was modified.
3517 assert(RD->isDefinition());
3518 UpdateRecord &Record = DeclUpdates[RD];
3519 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3520 AddDeclRef(D, Record);
3521}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003522
3523void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3524 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003525 // The specializations set is kept in the canonical template.
3526 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003527 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3528 return; // Not a source specialization added to a template from PCH.
3529
3530 UpdateRecord &Record = DeclUpdates[TD];
3531 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3532 AddDeclRef(D, Record);
3533}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003534
3535ASTSerializationListener::~ASTSerializationListener() { }