blob: b85aac1cc9f3ba1fc1964655b1853a06e34f5a15 [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
Douglas Gregordbe39272011-02-01 15:15:22 +0000914 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +0000915 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
916 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
917 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
918
Chris Lattner258172e2009-04-27 07:35:58 +0000919 // Whether static initializers are protected by locks.
920 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000921 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000922 Record.push_back(LangOpts.Blocks); // block extension to C
923 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
924 // they are unused.
925 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
926 // (modulo the platform support).
927
Chris Lattner51924e512010-06-26 21:25:03 +0000928 Record.push_back(LangOpts.getSignedOverflowBehavior());
929 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000930
931 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000932 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000933 // defined.
934 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
935 // opposed to __DYNAMIC__).
936 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
937
938 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
939 // used (instead of C99 semantics).
940 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000941 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
942 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000943 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
944 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000945 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +0000946 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
947 // to the smallest integer type with
948 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +0000949 Record.push_back(LangOpts.getGCMode());
950 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000951 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000952 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000953 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000954 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +0000955 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000956 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000957 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000958 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000959}
960
Douglas Gregora7f71a92009-04-10 03:52:48 +0000961//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000962// stat cache Serialization
963//===----------------------------------------------------------------------===//
964
965namespace {
966// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000967class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000968public:
969 typedef const char * key_type;
970 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000971
Chris Lattner2a6fa472010-11-23 19:28:12 +0000972 typedef struct stat data_type;
973 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000974
975 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000976 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000977 }
Mike Stump11289f42009-09-09 15:08:12 +0000978
979 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000980 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
981 data_type_ref Data) {
982 unsigned StrLen = strlen(path);
983 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000984 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000985 clang::io::Emit8(Out, DataLen);
986 return std::make_pair(StrLen + 1, DataLen);
987 }
Mike Stump11289f42009-09-09 15:08:12 +0000988
Douglas Gregorc5046832009-04-27 18:38:38 +0000989 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
990 Out.write(path, KeyLen);
991 }
Mike Stump11289f42009-09-09 15:08:12 +0000992
Chris Lattner2a6fa472010-11-23 19:28:12 +0000993 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000994 data_type_ref Data, unsigned DataLen) {
995 using namespace clang::io;
996 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000997
Chris Lattner2a6fa472010-11-23 19:28:12 +0000998 Emit32(Out, (uint32_t) Data.st_ino);
999 Emit32(Out, (uint32_t) Data.st_dev);
1000 Emit16(Out, (uint16_t) Data.st_mode);
1001 Emit64(Out, (uint64_t) Data.st_mtime);
1002 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001003
1004 assert(Out.tell() - Start == DataLen && "Wrong data length");
1005 }
1006};
1007} // end anonymous namespace
1008
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001009/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001010void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001011 // Build the on-disk hash table containing information about every
1012 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001013 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001014 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001015 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001016 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001017 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1018 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001019 Generator.insert(Filename, Stat->second);
1020 }
Mike Stump11289f42009-09-09 15:08:12 +00001021
Douglas Gregorc5046832009-04-27 18:38:38 +00001022 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001023 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001024 uint32_t BucketOffset;
1025 {
1026 llvm::raw_svector_ostream Out(StatCacheData);
1027 // Make sure that no bucket is at offset 0
1028 clang::io::Emit32(Out, 0);
1029 BucketOffset = Generator.Emit(Out);
1030 }
1031
1032 // Create a blob abbreviation
1033 using namespace llvm;
1034 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001035 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1039 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1040
1041 // Write the stat cache
1042 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001043 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001044 Record.push_back(BucketOffset);
1045 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001046 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001047}
1048
1049//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001050// Source Manager Serialization
1051//===----------------------------------------------------------------------===//
1052
1053/// \brief Create an abbreviation for the SLocEntry that refers to a
1054/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001055static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001056 using namespace llvm;
1057 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001058 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001063 // FileEntry fields.
1064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001066 // HeaderFileInfo fields.
1067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1069 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001072 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001073}
1074
1075/// \brief Create an abbreviation for the SLocEntry that refers to a
1076/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001077static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001078 using namespace llvm;
1079 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001080 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001086 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001087}
1088
1089/// \brief Create an abbreviation for the SLocEntry that refers to a
1090/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001091static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001092 using namespace llvm;
1093 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001094 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001096 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001097}
1098
1099/// \brief Create an abbreviation for the SLocEntry that refers to an
1100/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001101static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001102 using namespace llvm;
1103 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001104 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1107 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1108 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001109 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001110 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001111}
1112
1113/// \brief Writes the block containing the serialized form of the
1114/// source manager.
1115///
1116/// TODO: We should probably use an on-disk hash table (stored in a
1117/// blob), indexed based on the file name, so that we only create
1118/// entries for files that we actually need. In the common case (no
1119/// errors), we probably won't have to create file entries for any of
1120/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001121void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001122 const Preprocessor &PP,
1123 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001124 RecordData Record;
1125
Chris Lattner0910e3b2009-04-10 17:16:57 +00001126 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001127 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001128
1129 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001130 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1131 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1132 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1133 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001134
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001135 // Write the line table.
1136 if (SourceMgr.hasLineTable()) {
1137 LineTableInfo &LineTable = SourceMgr.getLineTable();
1138
1139 // Emit the file names
1140 Record.push_back(LineTable.getNumFilenames());
1141 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1142 // Emit the file name
1143 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001144 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001145 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1146 Record.push_back(FilenameLen);
1147 if (FilenameLen)
1148 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1149 }
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001151 // Emit the line entries
1152 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1153 L != LEnd; ++L) {
1154 // Emit the file ID
1155 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001156
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001157 // Emit the line entries
1158 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001159 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001160 LEEnd = L->second.end();
1161 LE != LEEnd; ++LE) {
1162 Record.push_back(LE->FileOffset);
1163 Record.push_back(LE->LineNo);
1164 Record.push_back(LE->FilenameID);
1165 Record.push_back((unsigned)LE->FileKind);
1166 Record.push_back(LE->IncludeOffset);
1167 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001168 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001169 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001170 }
1171
Douglas Gregor258ae542009-04-27 06:38:32 +00001172 // Write out the source location entry table. We skip the first
1173 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001174 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001175 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001176 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1177 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1178 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1179 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001180 // Get this source location entry.
1181 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001182
Douglas Gregor258ae542009-04-27 06:38:32 +00001183 // Record the offset of this source-location entry.
1184 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1185
1186 // Figure out which record code to use.
1187 unsigned Code;
1188 if (SLoc->isFile()) {
1189 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001190 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001191 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001192 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001193 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001194 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001195 Record.clear();
1196 Record.push_back(Code);
1197
1198 Record.push_back(SLoc->getOffset());
1199 if (SLoc->isFile()) {
1200 const SrcMgr::FileInfo &File = SLoc->getFile();
1201 Record.push_back(File.getIncludeLoc().getRawEncoding());
1202 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1203 Record.push_back(File.hasLineDirectives());
1204
1205 const SrcMgr::ContentCache *Content = File.getContentCache();
1206 if (Content->Entry) {
1207 // The source location entry is a file. The blob associated
1208 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001209
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001210 // Emit size/modification time for this file.
1211 Record.push_back(Content->Entry->getSize());
1212 Record.push_back(Content->Entry->getModificationTime());
1213
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001214 // Emit header-search information associated with this file.
1215 HeaderFileInfo HFI;
1216 HeaderSearch &HS = PP.getHeaderSearchInfo();
1217 if (Content->Entry->getUID() < HS.header_file_size())
1218 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1219 Record.push_back(HFI.isImport);
1220 Record.push_back(HFI.DirInfo);
1221 Record.push_back(HFI.NumIncludes);
1222 AddIdentifierRef(HFI.ControllingMacro, Record);
1223
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001224 // Turn the file name into an absolute path, if it isn't already.
1225 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001226 llvm::SmallString<128> FilePath(Filename);
1227 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001228 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001229
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001230 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001231 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001232
1233 // FIXME: For now, preload all file source locations, so that
1234 // we get the appropriate File entries in the reader. This is
1235 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001236 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001237 } else {
1238 // The source location entry is a buffer. The blob associated
1239 // with this entry contains the contents of the buffer.
1240
1241 // We add one to the size so that we capture the trailing NULL
1242 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1243 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001244 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001245 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001246 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001247 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1248 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001249 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001250 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001251 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001252 llvm::StringRef(Buffer->getBufferStart(),
1253 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001254
1255 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001256 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001257 }
1258 } else {
1259 // The source location entry is an instantiation.
1260 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1261 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1262 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1263 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1264
1265 // Compute the token length for this macro expansion.
1266 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001267 if (I + 1 != N)
1268 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001269 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1270 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1271 }
1272 }
1273
Douglas Gregor8f45df52009-04-16 22:23:12 +00001274 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001275
1276 if (SLocEntryOffsets.empty())
1277 return;
1278
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001279 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001280 // table is used for lazily loading source-location information.
1281 using namespace llvm;
1282 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001283 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1287 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001288
Douglas Gregor258ae542009-04-27 06:38:32 +00001289 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001290 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001291 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001292 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1293 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001294 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001295 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001296 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001297
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001298 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001299 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001300 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001301}
1302
Douglas Gregorc5046832009-04-27 18:38:38 +00001303//===----------------------------------------------------------------------===//
1304// Preprocessor Serialization
1305//===----------------------------------------------------------------------===//
1306
Chris Lattnereeffaef2009-04-10 17:15:23 +00001307/// \brief Writes the block containing the serialized form of the
1308/// preprocessor.
1309///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001310void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001311 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001312
Chris Lattner0af3ba12009-04-13 01:29:17 +00001313 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1314 if (PP.getCounterValue() != 0) {
1315 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001316 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001317 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001318 }
1319
1320 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001321 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001322
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001323 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001324 // FIXME: use diagnostics subsystem for localization etc.
1325 if (PP.SawDateOrTime())
1326 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001327
Douglas Gregor796d76a2010-10-20 22:00:55 +00001328
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001329 // Loop over all the macro definitions that are live at the end of the file,
1330 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001331 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001332 unsigned InclusionAbbrev = 0;
1333 if (PPRec) {
1334 using namespace llvm;
1335 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1336 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1343 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001344 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001345 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001346
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001347 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1348 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001349 // FIXME: This emits macros in hash table order, we should do it in a stable
1350 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001351 MacroInfo *MI = I->second;
1352
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001353 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001354 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001355 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001356
1357 // FIXME: There is a (probably minor) optimization we could do here, if
1358 // the macro comes from the original PCH but the identifier comes from a
1359 // chained PCH, by storing the offset into the original PCH rather than
1360 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001361 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001362 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001363 continue;
1364
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001365 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001366 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001367 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1368 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001370 unsigned Code;
1371 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001372 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001373 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001374 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001375
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001376 Record.push_back(MI->isC99Varargs());
1377 Record.push_back(MI->isGNUVarargs());
1378 Record.push_back(MI->getNumArgs());
1379 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1380 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001381 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001382 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001383
Douglas Gregoraae92242010-03-19 21:51:54 +00001384 // If we have a detailed preprocessing record, record the macro definition
1385 // ID that corresponds to this macro.
1386 if (PPRec)
1387 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001388
Douglas Gregor8f45df52009-04-16 22:23:12 +00001389 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001390 Record.clear();
1391
Chris Lattner2199f5b2009-04-10 18:08:30 +00001392 // Emit the tokens array.
1393 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1394 // Note that we know that the preprocessor does not have any annotation
1395 // tokens in it because they are created by the parser, and thus can't be
1396 // in a macro definition.
1397 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001398
Chris Lattner2199f5b2009-04-10 18:08:30 +00001399 Record.push_back(Tok.getLocation().getRawEncoding());
1400 Record.push_back(Tok.getLength());
1401
Chris Lattner2199f5b2009-04-10 18:08:30 +00001402 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1403 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001404 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001405
Chris Lattner2199f5b2009-04-10 18:08:30 +00001406 // FIXME: Should translate token kind to a stable encoding.
1407 Record.push_back(Tok.getKind());
1408 // FIXME: Should translate token flags to a stable encoding.
1409 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001410
Sebastian Redl539c5062010-08-18 23:57:32 +00001411 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001412 Record.clear();
1413 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001414 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001415 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001416
Douglas Gregoraae92242010-03-19 21:51:54 +00001417 // If the preprocessor has a preprocessing record, emit it.
1418 unsigned NumPreprocessingRecords = 0;
1419 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001420 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001421 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1422 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001423 E != EEnd; ++E) {
1424 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001425
Douglas Gregoraae92242010-03-19 21:51:54 +00001426 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1427 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001428 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001429
Douglas Gregor91096292010-10-02 19:29:26 +00001430 // Don't write the macro definition if it is from another AST file.
1431 if (ID < FirstMacroID)
1432 continue;
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001433
1434 // Notify the serialization listener that we're serializing this entity.
1435 if (SerializationListener)
1436 SerializationListener->SerializedPreprocessedEntity(*E,
1437 Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001438
Douglas Gregor91096292010-10-02 19:29:26 +00001439 unsigned Position = ID - FirstMacroID;
1440 if (Position != MacroDefinitionOffsets.size()) {
1441 if (Position > MacroDefinitionOffsets.size())
1442 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001443
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001444 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001445 } else
1446 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001447
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001448 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001449 Record.push_back(ID);
1450 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1451 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1452 AddIdentifierRef(MD->getName(), Record);
1453 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001454 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001455 continue;
1456 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001457
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001458 // Notify the serialization listener that we're serializing this entity.
1459 if (SerializationListener)
1460 SerializationListener->SerializedPreprocessedEntity(*E,
1461 Stream.GetCurrentBitNo());
1462
1463 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1464 Record.push_back(IndexBase + NumPreprocessingRecords++);
1465 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1466 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1467 AddIdentifierRef(MI->getName(), Record);
1468 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1469 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1470 continue;
1471 }
1472
Douglas Gregor796d76a2010-10-20 22:00:55 +00001473 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1474 Record.push_back(PP_INCLUSION_DIRECTIVE);
1475 Record.push_back(IndexBase + NumPreprocessingRecords++);
1476 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1477 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1478 Record.push_back(ID->getFileName().size());
1479 Record.push_back(ID->wasInQuotes());
1480 Record.push_back(static_cast<unsigned>(ID->getKind()));
1481 llvm::SmallString<64> Buffer;
1482 Buffer += ID->getFileName();
1483 Buffer += ID->getFile()->getName();
1484 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1485 continue;
1486 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001487
1488 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregoraae92242010-03-19 21:51:54 +00001489 }
1490 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001491
Douglas Gregor8f45df52009-04-16 22:23:12 +00001492 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001493
Douglas Gregoraae92242010-03-19 21:51:54 +00001494 // Write the offsets table for the preprocessing record.
1495 if (NumPreprocessingRecords > 0) {
1496 // Write the offsets table for identifier IDs.
1497 using namespace llvm;
1498 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001499 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1503 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001504
Douglas Gregoraae92242010-03-19 21:51:54 +00001505 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001506 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001507 Record.push_back(NumPreprocessingRecords);
1508 Record.push_back(MacroDefinitionOffsets.size());
1509 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001510 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001511 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1512 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001513}
1514
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001515void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001516 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001517 for (Diagnostic::DiagStatePointsTy::const_iterator
1518 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1519 I != E; ++I) {
1520 const Diagnostic::DiagStatePoint &point = *I;
1521 if (point.Loc.isInvalid())
1522 continue;
1523
1524 Record.push_back(point.Loc.getRawEncoding());
1525 for (Diagnostic::DiagState::iterator
1526 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1527 unsigned diag = I->first, map = I->second;
1528 if (map & 0x10) { // mapping from a diagnostic pragma.
1529 Record.push_back(diag);
1530 Record.push_back(map & 0x7);
1531 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001532 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001533 Record.push_back(-1); // mark the end of the diag/map pairs for this
1534 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001535 }
1536
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001537 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001538 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001539}
1540
Douglas Gregorc5046832009-04-27 18:38:38 +00001541//===----------------------------------------------------------------------===//
1542// Type Serialization
1543//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001544
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001545/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001546void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001547 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001548 if (Idx.getIndex() == 0) // we haven't seen this type before.
1549 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001550
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001551 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001552
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001553 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001554 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001555 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001556 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001557 else if (TypeOffsets.size() < Index) {
1558 TypeOffsets.resize(Index + 1);
1559 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001560 }
1561
1562 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001563
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001564 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001565 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001566
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001567 if (T.hasLocalNonFastQualifiers()) {
1568 Qualifiers Qs = T.getLocalQualifiers();
1569 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001570 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001571 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001572 } else {
1573 switch (T->getTypeClass()) {
1574 // For all of the concrete, non-dependent types, call the
1575 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001576#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001577 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001578#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001579#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001580 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001581 }
1582
1583 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001584 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001585
1586 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001587 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001588}
1589
Douglas Gregorc5046832009-04-27 18:38:38 +00001590//===----------------------------------------------------------------------===//
1591// Declaration Serialization
1592//===----------------------------------------------------------------------===//
1593
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001594/// \brief Write the block containing all of the declaration IDs
1595/// lexically declared within the given DeclContext.
1596///
1597/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1598/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001599uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001600 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001601 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001602 return 0;
1603
Douglas Gregor8f45df52009-04-16 22:23:12 +00001604 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001605 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001606 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001607 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001608 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1609 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001610 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001611
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001612 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001613 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001614 reinterpret_cast<char*>(Decls.data()),
1615 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001616 return Offset;
1617}
1618
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001619void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001620 using namespace llvm;
1621 RecordData Record;
1622
1623 // Write the type offsets array
1624 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001625 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1628 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1629 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001630 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001631 Record.push_back(TypeOffsets.size());
1632 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001633 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001634 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1635
1636 // Write the declaration offsets array
1637 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001638 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1640 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1641 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1642 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001643 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001644 Record.push_back(DeclOffsets.size());
1645 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001646 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001647 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1648}
1649
Douglas Gregorc5046832009-04-27 18:38:38 +00001650//===----------------------------------------------------------------------===//
1651// Global Method Pool and Selector Serialization
1652//===----------------------------------------------------------------------===//
1653
Douglas Gregore84a9da2009-04-20 20:36:09 +00001654namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001655// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001656class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001657 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001658
1659public:
1660 typedef Selector key_type;
1661 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001662
Sebastian Redl834bb972010-08-04 17:20:04 +00001663 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001664 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001665 ObjCMethodList Instance, Factory;
1666 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001667 typedef const data_type& data_type_ref;
1668
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001669 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001670
Douglas Gregorc78d3462009-04-24 21:10:55 +00001671 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001672 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001673 }
Mike Stump11289f42009-09-09 15:08:12 +00001674
1675 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001676 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1677 data_type_ref Methods) {
1678 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1679 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001680 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1681 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001682 Method = Method->Next)
1683 if (Method->Method)
1684 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001685 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001686 Method = Method->Next)
1687 if (Method->Method)
1688 DataLen += 4;
1689 clang::io::Emit16(Out, DataLen);
1690 return std::make_pair(KeyLen, DataLen);
1691 }
Mike Stump11289f42009-09-09 15:08:12 +00001692
Douglas Gregor95c13f52009-04-25 17:48:32 +00001693 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001694 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001695 assert((Start >> 32) == 0 && "Selector key offset too large");
1696 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001697 unsigned N = Sel.getNumArgs();
1698 clang::io::Emit16(Out, N);
1699 if (N == 0)
1700 N = 1;
1701 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001702 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001703 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1704 }
Mike Stump11289f42009-09-09 15:08:12 +00001705
Douglas Gregorc78d3462009-04-24 21:10:55 +00001706 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001707 data_type_ref Methods, unsigned DataLen) {
1708 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001709 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001710 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001711 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001712 Method = Method->Next)
1713 if (Method->Method)
1714 ++NumInstanceMethods;
1715
1716 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001717 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001718 Method = Method->Next)
1719 if (Method->Method)
1720 ++NumFactoryMethods;
1721
1722 clang::io::Emit16(Out, NumInstanceMethods);
1723 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001724 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001725 Method = Method->Next)
1726 if (Method->Method)
1727 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001728 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001729 Method = Method->Next)
1730 if (Method->Method)
1731 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001732
1733 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001734 }
1735};
1736} // end anonymous namespace
1737
Sebastian Redla19a67f2010-08-03 21:58:15 +00001738/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001739///
1740/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001741/// in an on-disk hash table indexed by the selector. The hash table also
1742/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001743void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001744 using namespace llvm;
1745
Sebastian Redla19a67f2010-08-03 21:58:15 +00001746 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001747 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001748 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001749 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001750 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001751 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001752 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001753 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001754
Sebastian Redla19a67f2010-08-03 21:58:15 +00001755 // Create the on-disk hash table representation. We walk through every
1756 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001757 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001758 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001759 I = SelectorIDs.begin(), E = SelectorIDs.end();
1760 I != E; ++I) {
1761 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001762 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001763 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001764 I->second,
1765 ObjCMethodList(),
1766 ObjCMethodList()
1767 };
1768 if (F != SemaRef.MethodPool.end()) {
1769 Data.Instance = F->second.first;
1770 Data.Factory = F->second.second;
1771 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001772 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001773 // changed.
1774 if (Chain && I->second < FirstSelectorID) {
1775 // Selector already exists. Did it change?
1776 bool changed = false;
1777 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1778 M = M->Next) {
1779 if (M->Method->getPCHLevel() == 0)
1780 changed = true;
1781 }
1782 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1783 M = M->Next) {
1784 if (M->Method->getPCHLevel() == 0)
1785 changed = true;
1786 }
1787 if (!changed)
1788 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001789 } else if (Data.Instance.Method || Data.Factory.Method) {
1790 // A new method pool entry.
1791 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001792 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001793 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001794 }
1795
Douglas Gregorc78d3462009-04-24 21:10:55 +00001796 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001797 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001798 uint32_t BucketOffset;
1799 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001800 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001801 llvm::raw_svector_ostream Out(MethodPool);
1802 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001803 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001804 BucketOffset = Generator.Emit(Out, Trait);
1805 }
1806
1807 // Create a blob abbreviation
1808 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001809 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1813 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1814
Douglas Gregor95c13f52009-04-25 17:48:32 +00001815 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001816 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001817 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001818 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001819 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001820 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001821
1822 // Create a blob abbreviation for the selector table offsets.
1823 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001824 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001825 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1827 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1828
1829 // Write the selector offsets table.
1830 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001831 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001832 Record.push_back(SelectorOffsets.size());
1833 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001834 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001835 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001836 }
1837}
1838
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001839/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001840void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001841 using namespace llvm;
1842 if (SemaRef.ReferencedSelectors.empty())
1843 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001844
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001845 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001846
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001847 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001848 // very tricky to fix, and given that @selector shouldn't really appear in
1849 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001850 for (DenseMap<Selector, SourceLocation>::iterator S =
1851 SemaRef.ReferencedSelectors.begin(),
1852 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1853 Selector Sel = (*S).first;
1854 SourceLocation Loc = (*S).second;
1855 AddSelectorRef(Sel, Record);
1856 AddSourceLocation(Loc, Record);
1857 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001858 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001859}
1860
Douglas Gregorc5046832009-04-27 18:38:38 +00001861//===----------------------------------------------------------------------===//
1862// Identifier Table Serialization
1863//===----------------------------------------------------------------------===//
1864
Douglas Gregorc78d3462009-04-24 21:10:55 +00001865namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001866class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001867 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001868 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001869
Douglas Gregor1d583f22009-04-28 21:18:29 +00001870 /// \brief Determines whether this is an "interesting" identifier
1871 /// that needs a full IdentifierInfo structure written into the hash
1872 /// table.
1873 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1874 return II->isPoisoned() ||
1875 II->isExtensionToken() ||
1876 II->hasMacroDefinition() ||
1877 II->getObjCOrBuiltinID() ||
1878 II->getFETokenInfo<void>();
1879 }
1880
Douglas Gregore84a9da2009-04-20 20:36:09 +00001881public:
1882 typedef const IdentifierInfo* key_type;
1883 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001884
Sebastian Redl539c5062010-08-18 23:57:32 +00001885 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001886 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001887
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001888 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001889 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001890
1891 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001892 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001893 }
Mike Stump11289f42009-09-09 15:08:12 +00001894
1895 std::pair<unsigned,unsigned>
1896 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001897 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001898 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001899 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1900 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001901 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001902 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001903 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001904 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001905 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1906 DEnd = IdentifierResolver::end();
1907 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001908 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001909 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001910 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001911 // We emit the key length after the data length so that every
1912 // string is preceded by a 16-bit length. This matches the PTH
1913 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001914 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001915 return std::make_pair(KeyLen, DataLen);
1916 }
Mike Stump11289f42009-09-09 15:08:12 +00001917
1918 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001919 unsigned KeyLen) {
1920 // Record the location of the key data. This is used when generating
1921 // the mapping from persistent IDs to strings.
1922 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001923 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001924 }
Mike Stump11289f42009-09-09 15:08:12 +00001925
1926 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001927 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001928 if (!isInterestingIdentifier(II)) {
1929 clang::io::Emit32(Out, ID << 1);
1930 return;
1931 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001932
Douglas Gregor1d583f22009-04-28 21:18:29 +00001933 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001934 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001935 bool hasMacroDefinition =
1936 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001937 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001938 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001939 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1940 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1941 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001942 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001943 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001944 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001945
Douglas Gregorc3366a52009-04-21 23:56:24 +00001946 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001947 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001948
Douglas Gregora868bbd2009-04-21 22:25:48 +00001949 // Emit the declaration IDs in reverse order, because the
1950 // IdentifierResolver provides the declarations as they would be
1951 // visible (e.g., the function "stat" would come before the struct
1952 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1953 // adds declarations to the end of the list (so we need to see the
1954 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001955 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001956 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001957 IdentifierResolver::end());
1958 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1959 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001960 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001961 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001962 }
1963};
1964} // end anonymous namespace
1965
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001966/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001967///
1968/// The identifier table consists of a blob containing string data
1969/// (the actual identifiers themselves) and a separate "offsets" index
1970/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001971void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001972 using namespace llvm;
1973
1974 // Create and write out the blob that contains the identifier
1975 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001976 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001977 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001978 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001979
Douglas Gregore6648fb2009-04-28 20:33:11 +00001980 // Look for any identifiers that were named while processing the
1981 // headers, but are otherwise not needed. We add these to the hash
1982 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001983 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001984 // file.
1985 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1986 IDEnd = PP.getIdentifierTable().end();
1987 ID != IDEnd; ++ID)
1988 getIdentifierRef(ID->second);
1989
Sebastian Redlff4a2952010-07-23 23:49:55 +00001990 // Create the on-disk hash table representation. We only store offsets
1991 // for identifiers that appear here for the first time.
1992 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001993 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001994 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1995 ID != IDEnd; ++ID) {
1996 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001997 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001998 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001999 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002000
Douglas Gregore84a9da2009-04-20 20:36:09 +00002001 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002002 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002003 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002004 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002005 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002006 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002007 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002008 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002009 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002010 }
2011
2012 // Create a blob abbreviation
2013 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002014 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002017 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002018
2019 // Write the identifier table
2020 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002021 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002022 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002023 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002024 }
2025
2026 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002027 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002028 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2031 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2032
2033 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002034 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002035 Record.push_back(IdentifierOffsets.size());
2036 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002037 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002038 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002039}
2040
Douglas Gregorc5046832009-04-27 18:38:38 +00002041//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002042// DeclContext's Name Lookup Table Serialization
2043//===----------------------------------------------------------------------===//
2044
2045namespace {
2046// Trait used for the on-disk hash table used in the method pool.
2047class ASTDeclContextNameLookupTrait {
2048 ASTWriter &Writer;
2049
2050public:
2051 typedef DeclarationName key_type;
2052 typedef key_type key_type_ref;
2053
2054 typedef DeclContext::lookup_result data_type;
2055 typedef const data_type& data_type_ref;
2056
2057 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2058
2059 unsigned ComputeHash(DeclarationName Name) {
2060 llvm::FoldingSetNodeID ID;
2061 ID.AddInteger(Name.getNameKind());
2062
2063 switch (Name.getNameKind()) {
2064 case DeclarationName::Identifier:
2065 ID.AddString(Name.getAsIdentifierInfo()->getName());
2066 break;
2067 case DeclarationName::ObjCZeroArgSelector:
2068 case DeclarationName::ObjCOneArgSelector:
2069 case DeclarationName::ObjCMultiArgSelector:
2070 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2071 break;
2072 case DeclarationName::CXXConstructorName:
2073 case DeclarationName::CXXDestructorName:
2074 case DeclarationName::CXXConversionFunctionName:
2075 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2076 break;
2077 case DeclarationName::CXXOperatorName:
2078 ID.AddInteger(Name.getCXXOverloadedOperator());
2079 break;
2080 case DeclarationName::CXXLiteralOperatorName:
2081 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2082 case DeclarationName::CXXUsingDirective:
2083 break;
2084 }
2085
2086 return ID.ComputeHash();
2087 }
2088
2089 std::pair<unsigned,unsigned>
2090 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2091 data_type_ref Lookup) {
2092 unsigned KeyLen = 1;
2093 switch (Name.getNameKind()) {
2094 case DeclarationName::Identifier:
2095 case DeclarationName::ObjCZeroArgSelector:
2096 case DeclarationName::ObjCOneArgSelector:
2097 case DeclarationName::ObjCMultiArgSelector:
2098 case DeclarationName::CXXConstructorName:
2099 case DeclarationName::CXXDestructorName:
2100 case DeclarationName::CXXConversionFunctionName:
2101 case DeclarationName::CXXLiteralOperatorName:
2102 KeyLen += 4;
2103 break;
2104 case DeclarationName::CXXOperatorName:
2105 KeyLen += 1;
2106 break;
2107 case DeclarationName::CXXUsingDirective:
2108 break;
2109 }
2110 clang::io::Emit16(Out, KeyLen);
2111
2112 // 2 bytes for num of decls and 4 for each DeclID.
2113 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2114 clang::io::Emit16(Out, DataLen);
2115
2116 return std::make_pair(KeyLen, DataLen);
2117 }
2118
2119 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2120 using namespace clang::io;
2121
2122 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2123 Emit8(Out, Name.getNameKind());
2124 switch (Name.getNameKind()) {
2125 case DeclarationName::Identifier:
2126 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2127 break;
2128 case DeclarationName::ObjCZeroArgSelector:
2129 case DeclarationName::ObjCOneArgSelector:
2130 case DeclarationName::ObjCMultiArgSelector:
2131 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2132 break;
2133 case DeclarationName::CXXConstructorName:
2134 case DeclarationName::CXXDestructorName:
2135 case DeclarationName::CXXConversionFunctionName:
2136 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2137 break;
2138 case DeclarationName::CXXOperatorName:
2139 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2140 Emit8(Out, Name.getCXXOverloadedOperator());
2141 break;
2142 case DeclarationName::CXXLiteralOperatorName:
2143 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2144 break;
2145 case DeclarationName::CXXUsingDirective:
2146 break;
2147 }
2148 }
2149
2150 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2151 data_type Lookup, unsigned DataLen) {
2152 uint64_t Start = Out.tell(); (void)Start;
2153 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2154 for (; Lookup.first != Lookup.second; ++Lookup.first)
2155 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2156
2157 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2158 }
2159};
2160} // end anonymous namespace
2161
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002162/// \brief Write the block containing all of the declaration IDs
2163/// visible from the given DeclContext.
2164///
2165/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002166/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002167uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2168 DeclContext *DC) {
2169 if (DC->getPrimaryContext() != DC)
2170 return 0;
2171
2172 // Since there is no name lookup into functions or methods, don't bother to
2173 // build a visible-declarations table for these entities.
2174 if (DC->isFunctionOrMethod())
2175 return 0;
2176
2177 // If not in C++, we perform name lookup for the translation unit via the
2178 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2179 // FIXME: In C++ we need the visible declarations in order to "see" the
2180 // friend declarations, is there a way to do this without writing the table ?
2181 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2182 return 0;
2183
2184 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002185 if (DC->hasExternalVisibleStorage())
2186 DC->MaterializeVisibleDeclsFromExternalStorage();
2187 else
2188 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002189
2190 // Serialize the contents of the mapping used for lookup. Note that,
2191 // although we have two very different code paths, the serialized
2192 // representation is the same for both cases: a declaration name,
2193 // followed by a size, followed by references to the visible
2194 // declarations that have that name.
2195 uint64_t Offset = Stream.GetCurrentBitNo();
2196 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2197 if (!Map || Map->empty())
2198 return 0;
2199
2200 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2201 ASTDeclContextNameLookupTrait Trait(*this);
2202
2203 // Create the on-disk hash table representation.
2204 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2205 D != DEnd; ++D) {
2206 DeclarationName Name = D->first;
2207 DeclContext::lookup_result Result = D->second.getLookupResult();
2208 Generator.insert(Name, Result, Trait);
2209 }
2210
2211 // Create the on-disk hash table in a buffer.
2212 llvm::SmallString<4096> LookupTable;
2213 uint32_t BucketOffset;
2214 {
2215 llvm::raw_svector_ostream Out(LookupTable);
2216 // Make sure that no bucket is at offset 0
2217 clang::io::Emit32(Out, 0);
2218 BucketOffset = Generator.Emit(Out, Trait);
2219 }
2220
2221 // Write the lookup table
2222 RecordData Record;
2223 Record.push_back(DECL_CONTEXT_VISIBLE);
2224 Record.push_back(BucketOffset);
2225 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2226 LookupTable.str());
2227
2228 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2229 ++NumVisibleDeclContexts;
2230 return Offset;
2231}
2232
Sebastian Redla4071b42010-08-24 00:50:09 +00002233/// \brief Write an UPDATE_VISIBLE block for the given context.
2234///
2235/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2236/// DeclContext in a dependent AST file. As such, they only exist for the TU
2237/// (in C++) and for namespaces.
2238void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002239 // Make the context build its lookup table, but don't make it load external
2240 // decls.
2241 DC->lookup(DeclarationName());
2242
2243 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2244 if (!Map || Map->empty())
2245 return;
2246
2247 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2248 ASTDeclContextNameLookupTrait Trait(*this);
2249
2250 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002251 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2252 D != DEnd; ++D) {
2253 DeclarationName Name = D->first;
2254 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002255 // For any name that appears in this table, the results are complete, i.e.
2256 // they overwrite results from previous PCHs. Merging is always a mess.
2257 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002258 }
2259
2260 // Create the on-disk hash table in a buffer.
2261 llvm::SmallString<4096> LookupTable;
2262 uint32_t BucketOffset;
2263 {
2264 llvm::raw_svector_ostream Out(LookupTable);
2265 // Make sure that no bucket is at offset 0
2266 clang::io::Emit32(Out, 0);
2267 BucketOffset = Generator.Emit(Out, Trait);
2268 }
2269
2270 // Write the lookup table
2271 RecordData Record;
2272 Record.push_back(UPDATE_VISIBLE);
2273 Record.push_back(getDeclID(cast<Decl>(DC)));
2274 Record.push_back(BucketOffset);
2275 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2276}
2277
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002278//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002279// General Serialization Routines
2280//===----------------------------------------------------------------------===//
2281
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002282/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002283void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002284 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002285 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2286 const Attr * A = *i;
2287 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2288 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002289
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002290#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002291
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002292 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002293}
2294
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002295void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002296 Record.push_back(Str.size());
2297 Record.insert(Record.end(), Str.begin(), Str.end());
2298}
2299
Douglas Gregore84a9da2009-04-20 20:36:09 +00002300/// \brief Note that the identifier II occurs at the given offset
2301/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002302void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002303 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002304 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002305 // up earlier in the chain and thus don't need an offset.
2306 if (ID >= FirstIdentID)
2307 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002308}
2309
Douglas Gregor95c13f52009-04-25 17:48:32 +00002310/// \brief Note that the selector Sel occurs at the given offset
2311/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002312void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002313 unsigned ID = SelectorIDs[Sel];
2314 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002315 // Don't record offsets for selectors that are also available in a different
2316 // file.
2317 if (ID < FirstSelectorID)
2318 return;
2319 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002320}
2321
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002322ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002323 : Stream(Stream), Chain(0), SerializationListener(0),
2324 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002325 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002326 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002327 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2328 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002329 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002330 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2331 NextCXXBaseSpecifiersID(1)
2332{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002333}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002334
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002335void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002336 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002337 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002338 Stream.Emit((unsigned)'C', 8);
2339 Stream.Emit((unsigned)'P', 8);
2340 Stream.Emit((unsigned)'C', 8);
2341 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002342
Chris Lattner28fa4e62009-04-26 22:26:21 +00002343 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002344
Sebastian Redl143413f2010-07-12 22:02:52 +00002345 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002346 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002347 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002348 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002349}
2350
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002351void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002352 const char *isysroot) {
2353 using namespace llvm;
2354
2355 ASTContext &Context = SemaRef.Context;
2356 Preprocessor &PP = SemaRef.PP;
2357
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002358 // The translation unit is the first declaration we'll emit.
2359 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002360 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002361 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002362
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002363 // Make sure that we emit IdentifierInfos (and any attached
2364 // declarations) for builtins.
2365 {
2366 IdentifierTable &Table = PP.getIdentifierTable();
2367 llvm::SmallVector<const char *, 32> BuiltinNames;
2368 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2369 Context.getLangOptions().NoBuiltin);
2370 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2371 getIdentifierRef(&Table.get(BuiltinNames[I]));
2372 }
2373
Chris Lattner0c797362009-09-08 18:19:27 +00002374 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002375 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002376 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002377 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002378 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2379 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002380 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002381
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002382 // Build a record containing all of the file scoped decls in this file.
2383 RecordData UnusedFileScopedDecls;
2384 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2385 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002386
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002387 RecordData WeakUndeclaredIdentifiers;
2388 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2389 WeakUndeclaredIdentifiers.push_back(
2390 SemaRef.WeakUndeclaredIdentifiers.size());
2391 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2392 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2393 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2394 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2395 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2396 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2397 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2398 }
2399 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002400
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002401 // Build a record containing all of the locally-scoped external
2402 // declarations in this header file. Generally, this record will be
2403 // empty.
2404 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002405 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002406 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002407 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002408 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2409 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2410 TD != TDEnd; ++TD)
2411 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2412
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002413 // Build a record containing all of the ext_vector declarations.
2414 RecordData ExtVectorDecls;
2415 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2416 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2417
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002418 // Build a record containing all of the VTable uses information.
2419 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002420 if (!SemaRef.VTableUses.empty()) {
2421 VTableUses.push_back(SemaRef.VTableUses.size());
2422 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2423 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2424 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2425 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2426 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002427 }
2428
2429 // Build a record containing all of dynamic classes declarations.
2430 RecordData DynamicClasses;
2431 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2432 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2433
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002434 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002435 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002436 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002437 I = SemaRef.PendingInstantiations.begin(),
2438 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2439 AddDeclRef(I->first, PendingInstantiations);
2440 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002441 }
2442 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2443 "There are local ones at end of translation unit!");
2444
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002445 // Build a record containing some declaration references.
2446 RecordData SemaDeclRefs;
2447 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2448 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2449 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2450 }
2451
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002452 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002453 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002454 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002455 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002456 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002457 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002458 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002459 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002460 // Write the record of special types.
2461 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002462
Steve Naroffc277ad12009-07-18 15:33:26 +00002463 AddTypeRef(Context.getBuiltinVaListType(), Record);
2464 AddTypeRef(Context.getObjCIdType(), Record);
2465 AddTypeRef(Context.getObjCSelType(), Record);
2466 AddTypeRef(Context.getObjCProtoType(), Record);
2467 AddTypeRef(Context.getObjCClassType(), Record);
2468 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2469 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2470 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002471 AddTypeRef(Context.getjmp_bufType(), Record);
2472 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002473 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2474 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002475 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002476 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002477 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2478 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002479 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002480 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002481
Douglas Gregor1970d882009-04-26 03:49:13 +00002482 // Keep writing types and declarations until all types and
2483 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002484 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002485 WriteDeclsBlockAbbrevs();
2486 while (!DeclTypesToEmit.empty()) {
2487 DeclOrType DOT = DeclTypesToEmit.front();
2488 DeclTypesToEmit.pop();
2489 if (DOT.isType())
2490 WriteType(DOT.getType());
2491 else
2492 WriteDecl(Context, DOT.getDecl());
2493 }
2494 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002495
Douglas Gregor45053152009-10-17 17:25:45 +00002496 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002497 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002498 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002499 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002500
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002501 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002502 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002503
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002504 // Write the C++ base-specifier set offsets.
2505 if (!CXXBaseSpecifiersOffsets.empty()) {
2506 // Create a blob abbreviation for the C++ base specifiers offsets.
2507 using namespace llvm;
2508
2509 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2510 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2513 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2514
2515 // Write the selector offsets table.
2516 Record.clear();
2517 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2518 Record.push_back(CXXBaseSpecifiersOffsets.size());
2519 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2520 (const char *)CXXBaseSpecifiersOffsets.data(),
2521 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2522 }
2523
Douglas Gregord4df8652009-04-22 22:02:47 +00002524 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002525 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002526 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002527
2528 // Write the record containing tentative definitions.
2529 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002530 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002531
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002532 // Write the record containing unused file scoped decls.
2533 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002534 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002535
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002536 // Write the record containing weak undeclared identifiers.
2537 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002538 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002539 WeakUndeclaredIdentifiers);
2540
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002541 // Write the record containing locally-scoped external definitions.
2542 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002543 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002544 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002545
2546 // Write the record containing ext_vector type names.
2547 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002548 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002549
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002550 // Write the record containing VTable uses information.
2551 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002552 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002553
2554 // Write the record containing dynamic classes declarations.
2555 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002556 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002557
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002558 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002559 if (!PendingInstantiations.empty())
2560 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002561
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002562 // Write the record containing declaration references of Sema.
2563 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002564 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002565
Douglas Gregor08f01292009-04-17 22:13:46 +00002566 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002567 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002568 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002569 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002570 Record.push_back(NumLexicalDeclContexts);
2571 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002572 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002573 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002574}
2575
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002576void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002577 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002578 using namespace llvm;
2579
2580 ASTContext &Context = SemaRef.Context;
2581 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002582
Sebastian Redl143413f2010-07-12 22:02:52 +00002583 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002584 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002585 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002586 if (StatCalls && !isysroot)
2587 WriteStatCache(*StatCalls);
2588 // FIXME: Source manager block should only write new stuff, which could be
2589 // done by tracking the largest ID in the chain
2590 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002591
2592 // The special types are in the chained PCH.
2593
2594 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002595 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002596 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002597 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002598 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2599 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002600 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002601 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002602 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002603 else if ((*I)->isChangedSinceDeserialization())
2604 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002605 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002606 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002607 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002608 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002609 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2610 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2611 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002612 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002613 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2614 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002615 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002616 // And a visible updates block for the DeclContexts.
2617 Abv = new llvm::BitCodeAbbrev();
2618 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2619 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2620 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2621 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2622 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2623 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002624
Sebastian Redl98912122010-07-27 23:01:28 +00002625 // Build a record containing all of the new tentative definitions in this
2626 // file, in TentativeDefinitions order.
2627 RecordData TentativeDefinitions;
2628 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2629 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2630 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2631 }
2632
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002633 // Build a record containing all of the file scoped decls in this file.
2634 RecordData UnusedFileScopedDecls;
2635 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2636 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2637 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002638 }
2639
Sebastian Redl08aca90252010-08-05 18:21:25 +00002640 // We write the entire table, overwriting the tables from the chain.
2641 RecordData WeakUndeclaredIdentifiers;
2642 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2643 WeakUndeclaredIdentifiers.push_back(
2644 SemaRef.WeakUndeclaredIdentifiers.size());
2645 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2646 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2647 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2648 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2649 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2650 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2651 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2652 }
2653 }
2654
Sebastian Redl98912122010-07-27 23:01:28 +00002655 // Build a record containing all of the locally-scoped external
2656 // declarations in this header file. Generally, this record will be
2657 // empty.
2658 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002659 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002660 // nondeterminstic!
2661 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2662 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2663 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2664 TD != TDEnd; ++TD) {
2665 if (TD->second->getPCHLevel() == 0)
2666 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2667 }
2668
2669 // Build a record containing all of the ext_vector declarations.
2670 RecordData ExtVectorDecls;
2671 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2672 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2673 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2674 }
2675
Sebastian Redl08aca90252010-08-05 18:21:25 +00002676 // Build a record containing all of the VTable uses information.
2677 // We write everything here, because it's too hard to determine whether
2678 // a use is new to this part.
2679 RecordData VTableUses;
2680 if (!SemaRef.VTableUses.empty()) {
2681 VTableUses.push_back(SemaRef.VTableUses.size());
2682 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2683 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2684 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2685 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2686 }
2687 }
2688
2689 // Build a record containing all of dynamic classes declarations.
2690 RecordData DynamicClasses;
2691 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2692 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2693 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2694
2695 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002696 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002697 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002698 I = SemaRef.PendingInstantiations.begin(),
2699 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002700 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002701 AddDeclRef(I->first, PendingInstantiations);
2702 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002703 }
2704 }
2705 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2706 "There are local ones at end of translation unit!");
2707
2708 // Build a record containing some declaration references.
2709 // It's not worth the effort to avoid duplication here.
2710 RecordData SemaDeclRefs;
2711 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2712 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2713 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2714 }
2715
Sebastian Redl539c5062010-08-18 23:57:32 +00002716 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002717 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002718 for (DeclsToRewriteTy::iterator
2719 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2720 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002721 while (!DeclTypesToEmit.empty()) {
2722 DeclOrType DOT = DeclTypesToEmit.front();
2723 DeclTypesToEmit.pop();
2724 if (DOT.isType())
2725 WriteType(DOT.getType());
2726 else
2727 WriteDecl(Context, DOT.getDecl());
2728 }
2729 Stream.ExitBlock();
2730
Sebastian Redl98912122010-07-27 23:01:28 +00002731 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002732 WriteSelectors(SemaRef);
2733 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002734 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002735 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002736 // FIXME: For chained PCH only write the new mappings (we currently
2737 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002738 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002739
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002740 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002741 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002742 RecordData FirstLatestDeclIDs;
2743 for (FirstLatestDeclMap::iterator
2744 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2745 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2746 "Expected first & second to be in different PCHs");
2747 AddDeclRef(I->first, FirstLatestDeclIDs);
2748 AddDeclRef(I->second, FirstLatestDeclIDs);
2749 }
2750 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002751 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002752
Sebastian Redl98912122010-07-27 23:01:28 +00002753 // Write the record containing external, unnamed definitions.
2754 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002755 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002756
2757 // Write the record containing tentative definitions.
2758 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002759 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002760
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002761 // Write the record containing unused file scoped decls.
2762 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002763 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002764
Sebastian Redl08aca90252010-08-05 18:21:25 +00002765 // Write the record containing weak undeclared identifiers.
2766 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002767 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002768 WeakUndeclaredIdentifiers);
2769
Sebastian Redl98912122010-07-27 23:01:28 +00002770 // Write the record containing locally-scoped external definitions.
2771 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002772 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002773 LocallyScopedExternalDecls);
2774
2775 // Write the record containing ext_vector type names.
2776 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002777 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002778
Sebastian Redl08aca90252010-08-05 18:21:25 +00002779 // Write the record containing VTable uses information.
2780 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002781 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002782
2783 // Write the record containing dynamic classes declarations.
2784 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002785 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002786
2787 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002788 if (!PendingInstantiations.empty())
2789 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002790
2791 // Write the record containing declaration references of Sema.
2792 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002793 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002794
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002795 // Write the updates to DeclContexts.
2796 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2797 I = UpdatedDeclContexts.begin(),
2798 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002799 I != E; ++I)
2800 WriteDeclContextVisibleUpdate(*I);
2801
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002802 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002803
Sebastian Redl98912122010-07-27 23:01:28 +00002804 Record.clear();
2805 Record.push_back(NumStatements);
2806 Record.push_back(NumMacros);
2807 Record.push_back(NumLexicalDeclContexts);
2808 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002809 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002810 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002811 Stream.ExitBlock();
2812}
2813
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002814void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002815 if (DeclUpdates.empty())
2816 return;
2817
2818 RecordData OffsetsRecord;
2819 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2820 for (DeclUpdateMap::iterator
2821 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2822 const Decl *D = I->first;
2823 UpdateRecord &URec = I->second;
2824
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002825 if (DeclsToRewrite.count(D))
2826 continue; // The decl will be written completely,no need to store updates.
2827
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002828 uint64_t Offset = Stream.GetCurrentBitNo();
2829 Stream.EmitRecord(DECL_UPDATES, URec);
2830
2831 OffsetsRecord.push_back(GetDeclRef(D));
2832 OffsetsRecord.push_back(Offset);
2833 }
2834 Stream.ExitBlock();
2835 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2836}
2837
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002838void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002839 if (ReplacedDecls.empty())
2840 return;
2841
2842 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002843 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002844 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2845 Record.push_back(I->first);
2846 Record.push_back(I->second);
2847 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002848 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002849}
2850
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002851void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002852 Record.push_back(Loc.getRawEncoding());
2853}
2854
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002855void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002856 AddSourceLocation(Range.getBegin(), Record);
2857 AddSourceLocation(Range.getEnd(), Record);
2858}
2859
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002860void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002861 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002862 const uint64_t *Words = Value.getRawData();
2863 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002864}
2865
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002866void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002867 Record.push_back(Value.isUnsigned());
2868 AddAPInt(Value, Record);
2869}
2870
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002871void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002872 AddAPInt(Value.bitcastToAPInt(), Record);
2873}
2874
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002875void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002876 Record.push_back(getIdentifierRef(II));
2877}
2878
Sebastian Redl539c5062010-08-18 23:57:32 +00002879IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002880 if (II == 0)
2881 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002882
Sebastian Redl539c5062010-08-18 23:57:32 +00002883 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002884 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002885 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002886 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002887}
2888
Sebastian Redl50e26582010-09-15 19:54:06 +00002889MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002890 if (MD == 0)
2891 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002892
2893 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002894 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002895 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002896 return ID;
2897}
2898
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002899void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002900 Record.push_back(getSelectorRef(SelRef));
2901}
2902
Sebastian Redl539c5062010-08-18 23:57:32 +00002903SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002904 if (Sel.getAsOpaquePtr() == 0) {
2905 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002906 }
2907
Sebastian Redl539c5062010-08-18 23:57:32 +00002908 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002909 if (SID == 0 && Chain) {
2910 // This might trigger a ReadSelector callback, which will set the ID for
2911 // this selector.
2912 Chain->LoadSelector(Sel);
2913 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002914 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002915 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002916 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002917 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002918}
2919
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002920void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002921 AddDeclRef(Temp->getDestructor(), Record);
2922}
2923
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002924void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2925 CXXBaseSpecifier const *BasesEnd,
2926 RecordDataImpl &Record) {
2927 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2928 CXXBaseSpecifiersToWrite.push_back(
2929 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2930 Bases, BasesEnd));
2931 Record.push_back(NextCXXBaseSpecifiersID++);
2932}
2933
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002934void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002935 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002936 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002937 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002938 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002939 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002940 break;
2941 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002942 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002943 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002944 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002945 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2946 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002947 break;
2948 case TemplateArgument::TemplateExpansion:
2949 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2950 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00002951 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002952 break;
John McCall0ad16662009-10-29 08:12:44 +00002953 case TemplateArgument::Null:
2954 case TemplateArgument::Integral:
2955 case TemplateArgument::Declaration:
2956 case TemplateArgument::Pack:
2957 break;
2958 }
2959}
2960
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002961void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002962 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002963 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002964
2965 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2966 bool InfoHasSameExpr
2967 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2968 Record.push_back(InfoHasSameExpr);
2969 if (InfoHasSameExpr)
2970 return; // Avoid storing the same expr twice.
2971 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002972 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2973 Record);
2974}
2975
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002976void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002977 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002978 AddTypeRef(QualType(), Record);
2979 return;
2980 }
2981
John McCallbcd03502009-12-07 02:54:59 +00002982 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002983 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002984 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002985 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002986}
2987
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002988void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002989 Record.push_back(GetOrCreateTypeID(T));
2990}
2991
2992TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002993 return MakeTypeID(T,
2994 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2995}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002996
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002997TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002998 return MakeTypeID(T,
2999 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003000}
3001
3002TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3003 if (T.isNull())
3004 return TypeIdx();
3005 assert(!T.getLocalFastQualifiers());
3006
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003007 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003008 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003009 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003010 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003011 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003012 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003013 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003014 return Idx;
3015}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003016
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003017TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003018 if (T.isNull())
3019 return TypeIdx();
3020 assert(!T.getLocalFastQualifiers());
3021
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003022 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3023 assert(I != TypeIdxs.end() && "Type not emitted!");
3024 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003025}
3026
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003027void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003028 Record.push_back(GetDeclRef(D));
3029}
3030
Sebastian Redl539c5062010-08-18 23:57:32 +00003031DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003032 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003033 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003034 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003035 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003036 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003037 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003038 // We haven't seen this declaration before. Give it a new ID and
3039 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003040 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003041 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003042 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3043 // We don't add it to the replacement collection here, because we don't
3044 // have the offset yet.
3045 DeclTypesToEmit.push(const_cast<Decl *>(D));
3046 // Reset the flag, so that we don't add this decl multiple times.
3047 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003048 }
3049
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003050 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003051}
3052
Sebastian Redl539c5062010-08-18 23:57:32 +00003053DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003054 if (D == 0)
3055 return 0;
3056
3057 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3058 return DeclIDs[D];
3059}
3060
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003061void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003062 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003063 Record.push_back(Name.getNameKind());
3064 switch (Name.getNameKind()) {
3065 case DeclarationName::Identifier:
3066 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3067 break;
3068
3069 case DeclarationName::ObjCZeroArgSelector:
3070 case DeclarationName::ObjCOneArgSelector:
3071 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003072 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003073 break;
3074
3075 case DeclarationName::CXXConstructorName:
3076 case DeclarationName::CXXDestructorName:
3077 case DeclarationName::CXXConversionFunctionName:
3078 AddTypeRef(Name.getCXXNameType(), Record);
3079 break;
3080
3081 case DeclarationName::CXXOperatorName:
3082 Record.push_back(Name.getCXXOverloadedOperator());
3083 break;
3084
Alexis Hunt3d221f22009-11-29 07:34:05 +00003085 case DeclarationName::CXXLiteralOperatorName:
3086 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3087 break;
3088
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003089 case DeclarationName::CXXUsingDirective:
3090 // No extra data to emit
3091 break;
3092 }
3093}
Chris Lattnerca025db2010-05-07 21:43:38 +00003094
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003095void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003096 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003097 switch (Name.getNameKind()) {
3098 case DeclarationName::CXXConstructorName:
3099 case DeclarationName::CXXDestructorName:
3100 case DeclarationName::CXXConversionFunctionName:
3101 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3102 break;
3103
3104 case DeclarationName::CXXOperatorName:
3105 AddSourceLocation(
3106 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3107 Record);
3108 AddSourceLocation(
3109 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3110 Record);
3111 break;
3112
3113 case DeclarationName::CXXLiteralOperatorName:
3114 AddSourceLocation(
3115 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3116 Record);
3117 break;
3118
3119 case DeclarationName::Identifier:
3120 case DeclarationName::ObjCZeroArgSelector:
3121 case DeclarationName::ObjCOneArgSelector:
3122 case DeclarationName::ObjCMultiArgSelector:
3123 case DeclarationName::CXXUsingDirective:
3124 break;
3125 }
3126}
3127
3128void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003129 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003130 AddDeclarationName(NameInfo.getName(), Record);
3131 AddSourceLocation(NameInfo.getLoc(), Record);
3132 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3133}
3134
3135void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003136 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003137 AddNestedNameSpecifier(Info.NNS, Record);
3138 AddSourceRange(Info.NNSRange, Record);
3139 Record.push_back(Info.NumTemplParamLists);
3140 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3141 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3142}
3143
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003144void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003145 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003146 // Nested name specifiers usually aren't too long. I think that 8 would
3147 // typically accomodate the vast majority.
3148 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3149
3150 // Push each of the NNS's onto a stack for serialization in reverse order.
3151 while (NNS) {
3152 NestedNames.push_back(NNS);
3153 NNS = NNS->getPrefix();
3154 }
3155
3156 Record.push_back(NestedNames.size());
3157 while(!NestedNames.empty()) {
3158 NNS = NestedNames.pop_back_val();
3159 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3160 Record.push_back(Kind);
3161 switch (Kind) {
3162 case NestedNameSpecifier::Identifier:
3163 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3164 break;
3165
3166 case NestedNameSpecifier::Namespace:
3167 AddDeclRef(NNS->getAsNamespace(), Record);
3168 break;
3169
3170 case NestedNameSpecifier::TypeSpec:
3171 case NestedNameSpecifier::TypeSpecWithTemplate:
3172 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3173 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3174 break;
3175
3176 case NestedNameSpecifier::Global:
3177 // Don't need to write an associated value.
3178 break;
3179 }
3180 }
3181}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003182
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003183void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003184 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003185 Record.push_back(Kind);
3186 switch (Kind) {
3187 case TemplateName::Template:
3188 AddDeclRef(Name.getAsTemplateDecl(), Record);
3189 break;
3190
3191 case TemplateName::OverloadedTemplate: {
3192 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3193 Record.push_back(OvT->size());
3194 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3195 I != E; ++I)
3196 AddDeclRef(*I, Record);
3197 break;
3198 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003199
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003200 case TemplateName::QualifiedTemplate: {
3201 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3202 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3203 Record.push_back(QualT->hasTemplateKeyword());
3204 AddDeclRef(QualT->getTemplateDecl(), Record);
3205 break;
3206 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003207
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003208 case TemplateName::DependentTemplate: {
3209 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3210 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3211 Record.push_back(DepT->isIdentifier());
3212 if (DepT->isIdentifier())
3213 AddIdentifierRef(DepT->getIdentifier(), Record);
3214 else
3215 Record.push_back(DepT->getOperator());
3216 break;
3217 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003218
3219 case TemplateName::SubstTemplateTemplateParmPack: {
3220 SubstTemplateTemplateParmPackStorage *SubstPack
3221 = Name.getAsSubstTemplateTemplateParmPack();
3222 AddDeclRef(SubstPack->getParameterPack(), Record);
3223 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3224 break;
3225 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003226 }
3227}
3228
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003229void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003230 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003231 Record.push_back(Arg.getKind());
3232 switch (Arg.getKind()) {
3233 case TemplateArgument::Null:
3234 break;
3235 case TemplateArgument::Type:
3236 AddTypeRef(Arg.getAsType(), Record);
3237 break;
3238 case TemplateArgument::Declaration:
3239 AddDeclRef(Arg.getAsDecl(), Record);
3240 break;
3241 case TemplateArgument::Integral:
3242 AddAPSInt(*Arg.getAsIntegral(), Record);
3243 AddTypeRef(Arg.getIntegralType(), Record);
3244 break;
3245 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003246 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3247 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003248 case TemplateArgument::TemplateExpansion:
3249 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003250 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3251 Record.push_back(*NumExpansions + 1);
3252 else
3253 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003254 break;
3255 case TemplateArgument::Expression:
3256 AddStmt(Arg.getAsExpr());
3257 break;
3258 case TemplateArgument::Pack:
3259 Record.push_back(Arg.pack_size());
3260 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3261 I != E; ++I)
3262 AddTemplateArgument(*I, Record);
3263 break;
3264 }
3265}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003266
3267void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003268ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003269 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003270 assert(TemplateParams && "No TemplateParams!");
3271 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3272 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3273 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3274 Record.push_back(TemplateParams->size());
3275 for (TemplateParameterList::const_iterator
3276 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3277 P != PEnd; ++P)
3278 AddDeclRef(*P, Record);
3279}
3280
3281/// \brief Emit a template argument list.
3282void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003283ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003284 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003285 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003286 Record.push_back(TemplateArgs->size());
3287 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003288 AddTemplateArgument(TemplateArgs->get(i), Record);
3289}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003290
3291
3292void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003293ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003294 Record.push_back(Set.size());
3295 for (UnresolvedSetImpl::const_iterator
3296 I = Set.begin(), E = Set.end(); I != E; ++I) {
3297 AddDeclRef(I.getDecl(), Record);
3298 Record.push_back(I.getAccess());
3299 }
3300}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003301
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003302void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003303 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003304 Record.push_back(Base.isVirtual());
3305 Record.push_back(Base.isBaseOfClass());
3306 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003307 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003308 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003309 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3310 : SourceLocation(),
3311 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003312}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003313
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003314void ASTWriter::FlushCXXBaseSpecifiers() {
3315 RecordData Record;
3316 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3317 Record.clear();
3318
3319 // Record the offset of this base-specifier set.
3320 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3321 if (Index == CXXBaseSpecifiersOffsets.size())
3322 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3323 else {
3324 if (Index > CXXBaseSpecifiersOffsets.size())
3325 CXXBaseSpecifiersOffsets.resize(Index + 1);
3326 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3327 }
3328
3329 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3330 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3331 Record.push_back(BEnd - B);
3332 for (; B != BEnd; ++B)
3333 AddCXXBaseSpecifier(*B, Record);
3334 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003335
3336 // Flush any expressions that were written as part of the base specifiers.
3337 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003338 }
3339
3340 CXXBaseSpecifiersToWrite.clear();
3341}
3342
Alexis Hunt1d792652011-01-08 20:30:50 +00003343void ASTWriter::AddCXXCtorInitializers(
3344 const CXXCtorInitializer * const *CtorInitializers,
3345 unsigned NumCtorInitializers,
3346 RecordDataImpl &Record) {
3347 Record.push_back(NumCtorInitializers);
3348 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3349 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003350
3351 Record.push_back(Init->isBaseInitializer());
3352 if (Init->isBaseInitializer()) {
3353 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3354 Record.push_back(Init->isBaseVirtual());
3355 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003356 Record.push_back(Init->isIndirectMemberInitializer());
3357 if (Init->isIndirectMemberInitializer())
3358 AddDeclRef(Init->getIndirectMember(), Record);
3359 else
3360 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003361 }
Francois Pichetd583da02010-12-04 09:14:42 +00003362
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003363 AddSourceLocation(Init->getMemberLocation(), Record);
3364 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003365 AddSourceLocation(Init->getLParenLoc(), Record);
3366 AddSourceLocation(Init->getRParenLoc(), Record);
3367 Record.push_back(Init->isWritten());
3368 if (Init->isWritten()) {
3369 Record.push_back(Init->getSourceOrder());
3370 } else {
3371 Record.push_back(Init->getNumArrayIndices());
3372 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3373 AddDeclRef(Init->getArrayIndex(i), Record);
3374 }
3375 }
3376}
3377
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003378void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3379 assert(D->DefinitionData);
3380 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3381 Record.push_back(Data.UserDeclaredConstructor);
3382 Record.push_back(Data.UserDeclaredCopyConstructor);
3383 Record.push_back(Data.UserDeclaredCopyAssignment);
3384 Record.push_back(Data.UserDeclaredDestructor);
3385 Record.push_back(Data.Aggregate);
3386 Record.push_back(Data.PlainOldData);
3387 Record.push_back(Data.Empty);
3388 Record.push_back(Data.Polymorphic);
3389 Record.push_back(Data.Abstract);
3390 Record.push_back(Data.HasTrivialConstructor);
3391 Record.push_back(Data.HasTrivialCopyConstructor);
3392 Record.push_back(Data.HasTrivialCopyAssignment);
3393 Record.push_back(Data.HasTrivialDestructor);
3394 Record.push_back(Data.ComputedVisibleConversions);
3395 Record.push_back(Data.DeclaredDefaultConstructor);
3396 Record.push_back(Data.DeclaredCopyConstructor);
3397 Record.push_back(Data.DeclaredCopyAssignment);
3398 Record.push_back(Data.DeclaredDestructor);
3399
3400 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003401 if (Data.NumBases > 0)
3402 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3403 Record);
3404
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003405 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3406 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003407 if (Data.NumVBases > 0)
3408 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3409 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003410
3411 AddUnresolvedSet(Data.Conversions, Record);
3412 AddUnresolvedSet(Data.VisibleConversions, Record);
3413 // Data.Definition is the owning decl, no need to write it.
3414 AddDeclRef(Data.FirstFriend, Record);
3415}
3416
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003417void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003418 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003419 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003420 assert(FirstDeclID == NextDeclID &&
3421 FirstTypeID == NextTypeID &&
3422 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003423 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003424 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003425 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003426 "Setting chain after writing has started.");
3427 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003428
3429 FirstDeclID += Chain->getTotalNumDecls();
3430 FirstTypeID += Chain->getTotalNumTypes();
3431 FirstIdentID += Chain->getTotalNumIdentifiers();
3432 FirstSelectorID += Chain->getTotalNumSelectors();
3433 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003434 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003435 NextDeclID = FirstDeclID;
3436 NextTypeID = FirstTypeID;
3437 NextIdentID = FirstIdentID;
3438 NextSelectorID = FirstSelectorID;
3439 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003440 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003441}
3442
Sebastian Redl539c5062010-08-18 23:57:32 +00003443void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003444 IdentifierIDs[II] = ID;
3445}
3446
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003447void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003448 // Always take the highest-numbered type index. This copes with an interesting
3449 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003450 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003451 // keep the higher-numbered entry so that we can properly write it out to
3452 // the AST file.
3453 TypeIdx &StoredIdx = TypeIdxs[T];
3454 if (Idx.getIndex() >= StoredIdx.getIndex())
3455 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003456}
3457
Sebastian Redl539c5062010-08-18 23:57:32 +00003458void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003459 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003460}
Sebastian Redl834bb972010-08-04 17:20:04 +00003461
Sebastian Redl539c5062010-08-18 23:57:32 +00003462void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003463 SelectorIDs[S] = ID;
3464}
Douglas Gregor91096292010-10-02 19:29:26 +00003465
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003466void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003467 MacroDefinition *MD) {
3468 MacroDefinitions[MD] = ID;
3469}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003470
3471void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3472 assert(D->isDefinition());
3473 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3474 // We are interested when a PCH decl is modified.
3475 if (RD->getPCHLevel() > 0) {
3476 // A forward reference was mutated into a definition. Rewrite it.
3477 // FIXME: This happens during template instantiation, should we
3478 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003479 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003480 }
3481
3482 for (CXXRecordDecl::redecl_iterator
3483 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3484 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3485 if (Redecl == RD)
3486 continue;
3487
3488 // We are interested when a PCH decl is modified.
3489 if (Redecl->getPCHLevel() > 0) {
3490 UpdateRecord &Record = DeclUpdates[Redecl];
3491 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3492 assert(Redecl->DefinitionData);
3493 assert(Redecl->DefinitionData->Definition == D);
3494 AddDeclRef(D, Record); // the DefinitionDecl
3495 }
3496 }
3497 }
3498}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003499void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3500 // TU and namespaces are handled elsewhere.
3501 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3502 return;
3503
3504 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3505 return; // Not a source decl added to a DeclContext from PCH.
3506
3507 AddUpdatedDeclContext(DC);
3508}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003509
3510void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3511 assert(D->isImplicit());
3512 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3513 return; // Not a source member added to a class from PCH.
3514 if (!isa<CXXMethodDecl>(D))
3515 return; // We are interested in lazily declared implicit methods.
3516
3517 // A decl coming from PCH was modified.
3518 assert(RD->isDefinition());
3519 UpdateRecord &Record = DeclUpdates[RD];
3520 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3521 AddDeclRef(D, Record);
3522}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003523
3524void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3525 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003526 // The specializations set is kept in the canonical template.
3527 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003528 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3529 return; // Not a source specialization added to a template from PCH.
3530
3531 UpdateRecord &Record = DeclUpdates[TD];
3532 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3533 AddDeclRef(D, Record);
3534}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003535
3536ASTSerializationListener::~ASTSerializationListener() { }