blob: 94be24ad2c598d0d2c4d317ffcfbdd6464328398 [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"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000023#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000024#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000025#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000026#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000027#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000028#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000030#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000031#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000032#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000033#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000034#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000035#include "llvm/ADT/APFloat.h"
36#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000037#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000038#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000039#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000040#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000041#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000043using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044
Sebastian Redl3df5a082010-07-30 17:03:48 +000045template <typename T, typename Allocator>
46T *data(std::vector<T, Allocator> &v) {
47 return v.empty() ? 0 : &v.front();
48}
49template <typename T, typename Allocator>
50const T *data(const std::vector<T, Allocator> &v) {
51 return v.empty() ? 0 : &v.front();
52}
53
Douglas Gregoref84c4b2009-04-09 22:27:44 +000054//===----------------------------------------------------------------------===//
55// Type serialization
56//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000057
Douglas Gregoref84c4b2009-04-09 22:27:44 +000058namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000059 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000060 ASTWriter &Writer;
61 ASTWriter::RecordData &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000062
63 public:
64 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000065 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000066
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000067 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000068 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069
70 void VisitArrayType(const ArrayType *T);
71 void VisitFunctionType(const FunctionType *T);
72 void VisitTagType(const TagType *T);
73
74#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
75#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076#include "clang/AST/TypeNodes.def"
77 };
78}
79
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000080void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000081 assert(false && "Built-in types are never serialized");
82}
83
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000084void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000085 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000086 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000087}
88
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000089void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000091 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092}
93
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000095 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000096 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097}
98
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000099void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000101 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102}
103
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000104void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000106 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107}
108
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000109void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000110 Writer.AddTypeRef(T->getPointeeType(), Record);
111 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000112 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116 Writer.AddTypeRef(T->getElementType(), Record);
117 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000118 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119}
120
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122 VisitArrayType(T);
123 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000124 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000129 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130}
131
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000134 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
135 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000136 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000137 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000138}
139
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000141 Writer.AddTypeRef(T->getElementType(), Record);
142 Record.push_back(T->getNumElements());
Chris Lattner37141f42010-06-23 06:00:24 +0000143 Record.push_back(T->getAltiVecSpecific());
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000149 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150}
151
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000152void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000154 FunctionType::ExtInfo C = T->getExtInfo();
155 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000156 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000157 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000158 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000163 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 VisitFunctionType(T);
168 Record.push_back(T->getNumArgs());
169 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
170 Writer.AddTypeRef(T->getArgType(I), Record);
171 Record.push_back(T->isVariadic());
172 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000173 Record.push_back(T->hasExceptionSpec());
174 Record.push_back(T->hasAnyExceptionSpec());
175 Record.push_back(T->getNumExceptions());
176 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
177 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000178 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179}
180
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000182 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000183 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000184}
John McCallb96ec562009-12-04 22:46:56 +0000185
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000187 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000188 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
189 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000190 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000191}
192
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000193void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000194 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000195 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196}
197
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000198void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000200 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000201}
202
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000203void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000204 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000205 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000206}
207
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000208void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000209 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000210 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000211 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000212 "Cannot serialize in the middle of a type definition");
213}
214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000217 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218}
219
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000220void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000221 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000222 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223}
224
Mike Stump11289f42009-09-09 15:08:12 +0000225void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000227 const SubstTemplateTypeParmType *T) {
228 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
229 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000230 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000231}
232
233void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000234ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000236 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000237 Writer.AddTemplateName(T->getTemplateName(), Record);
238 Record.push_back(T->getNumArgs());
239 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
240 ArgI != ArgE; ++ArgI)
241 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000242 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
243 : T->getCanonicalTypeInternal(),
244 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000246}
247
248void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000249ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000250 VisitArrayType(T);
251 Writer.AddStmt(T->getSizeExpr());
252 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000253 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000254}
255
256void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000257ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000258 const DependentSizedExtVectorType *T) {
259 // FIXME: Serialize this type (C++ only)
260 assert(false && "Cannot serialize dependent sized extended vector types");
261}
262
263void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000264ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000265 Record.push_back(T->getDepth());
266 Record.push_back(T->getIndex());
267 Record.push_back(T->isParameterPack());
268 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000269 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000270}
271
272void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000273ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000274 Record.push_back(T->getKeyword());
275 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
276 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000277 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
278 : T->getCanonicalTypeInternal(),
279 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000280 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000281}
282
283void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000284ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000285 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000286 Record.push_back(T->getKeyword());
287 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
288 Writer.AddIdentifierRef(T->getIdentifier(), Record);
289 Record.push_back(T->getNumArgs());
290 for (DependentTemplateSpecializationType::iterator
291 I = T->begin(), E = T->end(); I != E; ++I)
292 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000293 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000294}
295
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000296void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000297 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000298 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
299 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000300 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000301}
302
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000303void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000304 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000305 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000306 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000307}
308
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000309void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000310 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000311 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000312}
313
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000314void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000315 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000316 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000317 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000318 E = T->qual_end(); I != E; ++I)
319 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000320 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000321}
322
Steve Narofffb4330f2009-06-17 22:40:22 +0000323void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000324ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000325 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000326 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000327}
328
John McCall8f115c62009-10-16 21:56:05 +0000329namespace {
330
331class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000332 ASTWriter &Writer;
333 ASTWriter::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +0000334
335public:
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000336 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
John McCall8f115c62009-10-16 21:56:05 +0000337 : Writer(Writer), Record(Record) { }
338
John McCall17001972009-10-18 01:05:36 +0000339#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000340#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000341 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000342#include "clang/AST/TypeLocNodes.def"
343
John McCall17001972009-10-18 01:05:36 +0000344 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
345 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000346};
347
348}
349
John McCall17001972009-10-18 01:05:36 +0000350void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
351 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000352}
John McCall17001972009-10-18 01:05:36 +0000353void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000354 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
355 if (TL.needsExtraLocalData()) {
356 Record.push_back(TL.getWrittenTypeSpec());
357 Record.push_back(TL.getWrittenSignSpec());
358 Record.push_back(TL.getWrittenWidthSpec());
359 Record.push_back(TL.hasModeAttr());
360 }
John McCall8f115c62009-10-16 21:56:05 +0000361}
John McCall17001972009-10-18 01:05:36 +0000362void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
363 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000364}
John McCall17001972009-10-18 01:05:36 +0000365void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
366 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000367}
John McCall17001972009-10-18 01:05:36 +0000368void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
369 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000370}
John McCall17001972009-10-18 01:05:36 +0000371void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
372 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000373}
John McCall17001972009-10-18 01:05:36 +0000374void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
375 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000376}
John McCall17001972009-10-18 01:05:36 +0000377void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000379}
John McCall17001972009-10-18 01:05:36 +0000380void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
381 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
382 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
383 Record.push_back(TL.getSizeExpr() ? 1 : 0);
384 if (TL.getSizeExpr())
385 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000386}
John McCall17001972009-10-18 01:05:36 +0000387void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
388 VisitArrayTypeLoc(TL);
389}
390void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
391 VisitArrayTypeLoc(TL);
392}
393void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
394 VisitArrayTypeLoc(TL);
395}
396void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
397 DependentSizedArrayTypeLoc TL) {
398 VisitArrayTypeLoc(TL);
399}
400void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
401 DependentSizedExtVectorTypeLoc TL) {
402 Writer.AddSourceLocation(TL.getNameLoc(), Record);
403}
404void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getNameLoc(), Record);
406}
407void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getNameLoc(), Record);
409}
410void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
412 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
413 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
414 Writer.AddDeclRef(TL.getArg(i), Record);
415}
416void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
417 VisitFunctionTypeLoc(TL);
418}
419void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
420 VisitFunctionTypeLoc(TL);
421}
John McCallb96ec562009-12-04 22:46:56 +0000422void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getNameLoc(), Record);
424}
John McCall17001972009-10-18 01:05:36 +0000425void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
427}
428void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000429 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
430 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
431 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000432}
433void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000434 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
435 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
436 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
437 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000438}
439void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getNameLoc(), Record);
441}
442void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
445void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getNameLoc(), Record);
447}
John McCall17001972009-10-18 01:05:36 +0000448void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
449 Writer.AddSourceLocation(TL.getNameLoc(), Record);
450}
John McCallcebee162009-10-18 09:09:24 +0000451void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
452 SubstTemplateTypeParmTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getNameLoc(), Record);
454}
John McCall17001972009-10-18 01:05:36 +0000455void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
456 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000457 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
458 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
459 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
460 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000461 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
462 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000463}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000464void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000465 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
466 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000467}
John McCalle78aac42010-03-10 03:28:59 +0000468void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000471void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000472 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
473 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
475}
John McCallc392f372010-06-11 00:33:02 +0000476void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
477 DependentTemplateSpecializationTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
479 Writer.AddSourceRange(TL.getQualifierRange(), Record);
480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
481 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
482 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
483 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000484 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
485 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000489}
490void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
491 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000492 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
493 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
494 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
495 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000496}
John McCallfc93cf92009-10-22 22:37:11 +0000497void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000499}
John McCall8f115c62009-10-16 21:56:05 +0000500
Chris Lattner19cea4e2009-04-22 05:57:30 +0000501//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000502// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000503//===----------------------------------------------------------------------===//
504
Chris Lattner28fa4e62009-04-26 22:26:21 +0000505static void EmitBlockID(unsigned ID, const char *Name,
506 llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000507 ASTWriter::RecordData &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000508 Record.clear();
509 Record.push_back(ID);
510 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
511
512 // Emit the block name if present.
513 if (Name == 0 || Name[0] == 0) return;
514 Record.clear();
515 while (*Name)
516 Record.push_back(*Name++);
517 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
518}
519
520static void EmitRecordID(unsigned ID, const char *Name,
521 llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000522 ASTWriter::RecordData &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000523 Record.clear();
524 Record.push_back(ID);
525 while (*Name)
526 Record.push_back(*Name++);
527 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000528}
529
530static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000531 ASTWriter::RecordData &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000532#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000533 RECORD(STMT_STOP);
534 RECORD(STMT_NULL_PTR);
535 RECORD(STMT_NULL);
536 RECORD(STMT_COMPOUND);
537 RECORD(STMT_CASE);
538 RECORD(STMT_DEFAULT);
539 RECORD(STMT_LABEL);
540 RECORD(STMT_IF);
541 RECORD(STMT_SWITCH);
542 RECORD(STMT_WHILE);
543 RECORD(STMT_DO);
544 RECORD(STMT_FOR);
545 RECORD(STMT_GOTO);
546 RECORD(STMT_INDIRECT_GOTO);
547 RECORD(STMT_CONTINUE);
548 RECORD(STMT_BREAK);
549 RECORD(STMT_RETURN);
550 RECORD(STMT_DECL);
551 RECORD(STMT_ASM);
552 RECORD(EXPR_PREDEFINED);
553 RECORD(EXPR_DECL_REF);
554 RECORD(EXPR_INTEGER_LITERAL);
555 RECORD(EXPR_FLOATING_LITERAL);
556 RECORD(EXPR_IMAGINARY_LITERAL);
557 RECORD(EXPR_STRING_LITERAL);
558 RECORD(EXPR_CHARACTER_LITERAL);
559 RECORD(EXPR_PAREN);
560 RECORD(EXPR_UNARY_OPERATOR);
561 RECORD(EXPR_SIZEOF_ALIGN_OF);
562 RECORD(EXPR_ARRAY_SUBSCRIPT);
563 RECORD(EXPR_CALL);
564 RECORD(EXPR_MEMBER);
565 RECORD(EXPR_BINARY_OPERATOR);
566 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
567 RECORD(EXPR_CONDITIONAL_OPERATOR);
568 RECORD(EXPR_IMPLICIT_CAST);
569 RECORD(EXPR_CSTYLE_CAST);
570 RECORD(EXPR_COMPOUND_LITERAL);
571 RECORD(EXPR_EXT_VECTOR_ELEMENT);
572 RECORD(EXPR_INIT_LIST);
573 RECORD(EXPR_DESIGNATED_INIT);
574 RECORD(EXPR_IMPLICIT_VALUE_INIT);
575 RECORD(EXPR_VA_ARG);
576 RECORD(EXPR_ADDR_LABEL);
577 RECORD(EXPR_STMT);
578 RECORD(EXPR_TYPES_COMPATIBLE);
579 RECORD(EXPR_CHOOSE);
580 RECORD(EXPR_GNU_NULL);
581 RECORD(EXPR_SHUFFLE_VECTOR);
582 RECORD(EXPR_BLOCK);
583 RECORD(EXPR_BLOCK_DECL_REF);
584 RECORD(EXPR_OBJC_STRING_LITERAL);
585 RECORD(EXPR_OBJC_ENCODE);
586 RECORD(EXPR_OBJC_SELECTOR_EXPR);
587 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
588 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
589 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
590 RECORD(EXPR_OBJC_KVC_REF_EXPR);
591 RECORD(EXPR_OBJC_MESSAGE_EXPR);
592 RECORD(EXPR_OBJC_SUPER_EXPR);
593 RECORD(STMT_OBJC_FOR_COLLECTION);
594 RECORD(STMT_OBJC_CATCH);
595 RECORD(STMT_OBJC_FINALLY);
596 RECORD(STMT_OBJC_AT_TRY);
597 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
598 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000599 RECORD(EXPR_CXX_OPERATOR_CALL);
600 RECORD(EXPR_CXX_CONSTRUCT);
601 RECORD(EXPR_CXX_STATIC_CAST);
602 RECORD(EXPR_CXX_DYNAMIC_CAST);
603 RECORD(EXPR_CXX_REINTERPRET_CAST);
604 RECORD(EXPR_CXX_CONST_CAST);
605 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
606 RECORD(EXPR_CXX_BOOL_LITERAL);
607 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000608#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000609}
Mike Stump11289f42009-09-09 15:08:12 +0000610
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000611void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000612 RecordData Record;
613 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Sebastian Redl539c5062010-08-18 23:57:32 +0000615#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
616#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000617
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000618 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000619 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000620 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000621 RECORD(TYPE_OFFSET);
622 RECORD(DECL_OFFSET);
623 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000624 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000625 RECORD(IDENTIFIER_OFFSET);
626 RECORD(IDENTIFIER_TABLE);
627 RECORD(EXTERNAL_DEFINITIONS);
628 RECORD(SPECIAL_TYPES);
629 RECORD(STATISTICS);
630 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000631 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000632 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
633 RECORD(SELECTOR_OFFSETS);
634 RECORD(METHOD_POOL);
635 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000636 RECORD(SOURCE_LOCATION_OFFSETS);
637 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000638 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000639 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000640 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000641 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000642 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000643 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregoraae92242010-03-19 21:51:54 +0000644
Chris Lattner28fa4e62009-04-26 22:26:21 +0000645 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000646 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000647 RECORD(SM_SLOC_FILE_ENTRY);
648 RECORD(SM_SLOC_BUFFER_ENTRY);
649 RECORD(SM_SLOC_BUFFER_BLOB);
650 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
651 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattner28fa4e62009-04-26 22:26:21 +0000653 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000654 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000655 RECORD(PP_MACRO_OBJECT_LIKE);
656 RECORD(PP_MACRO_FUNCTION_LIKE);
657 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000658 RECORD(PP_MACRO_INSTANTIATION);
659 RECORD(PP_MACRO_DEFINITION);
660
Douglas Gregor12bfa382009-10-17 00:13:19 +0000661 // Decls and Types block.
662 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000663 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000664 RECORD(TYPE_COMPLEX);
665 RECORD(TYPE_POINTER);
666 RECORD(TYPE_BLOCK_POINTER);
667 RECORD(TYPE_LVALUE_REFERENCE);
668 RECORD(TYPE_RVALUE_REFERENCE);
669 RECORD(TYPE_MEMBER_POINTER);
670 RECORD(TYPE_CONSTANT_ARRAY);
671 RECORD(TYPE_INCOMPLETE_ARRAY);
672 RECORD(TYPE_VARIABLE_ARRAY);
673 RECORD(TYPE_VECTOR);
674 RECORD(TYPE_EXT_VECTOR);
675 RECORD(TYPE_FUNCTION_PROTO);
676 RECORD(TYPE_FUNCTION_NO_PROTO);
677 RECORD(TYPE_TYPEDEF);
678 RECORD(TYPE_TYPEOF_EXPR);
679 RECORD(TYPE_TYPEOF);
680 RECORD(TYPE_RECORD);
681 RECORD(TYPE_ENUM);
682 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000683 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000684 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000685 RECORD(DECL_ATTR);
686 RECORD(DECL_TRANSLATION_UNIT);
687 RECORD(DECL_TYPEDEF);
688 RECORD(DECL_ENUM);
689 RECORD(DECL_RECORD);
690 RECORD(DECL_ENUM_CONSTANT);
691 RECORD(DECL_FUNCTION);
692 RECORD(DECL_OBJC_METHOD);
693 RECORD(DECL_OBJC_INTERFACE);
694 RECORD(DECL_OBJC_PROTOCOL);
695 RECORD(DECL_OBJC_IVAR);
696 RECORD(DECL_OBJC_AT_DEFS_FIELD);
697 RECORD(DECL_OBJC_CLASS);
698 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
699 RECORD(DECL_OBJC_CATEGORY);
700 RECORD(DECL_OBJC_CATEGORY_IMPL);
701 RECORD(DECL_OBJC_IMPLEMENTATION);
702 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
703 RECORD(DECL_OBJC_PROPERTY);
704 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000705 RECORD(DECL_FIELD);
706 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000707 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000708 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000709 RECORD(DECL_FILE_SCOPE_ASM);
710 RECORD(DECL_BLOCK);
711 RECORD(DECL_CONTEXT_LEXICAL);
712 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000713 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000714 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000715#undef RECORD
716#undef BLOCK
717 Stream.ExitBlock();
718}
719
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000720/// \brief Adjusts the given filename to only write out the portion of the
721/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000722///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000723/// \param Filename the file name to adjust.
724///
725/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
726/// the returned filename will be adjusted by this system root.
727///
728/// \returns either the original filename (if it needs no adjustment) or the
729/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000730static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000731adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
732 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000733
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000734 if (!isysroot)
735 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000736
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000737 // Verify that the filename and the system root have the same prefix.
738 unsigned Pos = 0;
739 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
740 if (Filename[Pos] != isysroot[Pos])
741 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000742
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000743 // We hit the end of the filename before we hit the end of the system root.
744 if (!Filename[Pos])
745 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000746
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000747 // If the file name has a '/' at the current position, skip over the '/'.
748 // We distinguish sysroot-based includes from absolute includes by the
749 // absence of '/' at the beginning of sysroot-based includes.
750 if (Filename[Pos] == '/')
751 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000752
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000753 return Filename + Pos;
754}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000755
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000756/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000757void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000758 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000759
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000760 // Metadata
761 const TargetInfo &Target = Context.Target;
762 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000763 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000764 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000765 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
766 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000770 // Target triple or chained PCH name
771 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000772 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000773
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000774 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000775 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
776 Record.push_back(VERSION_MAJOR);
777 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000778 Record.push_back(CLANG_VERSION_MAJOR);
779 Record.push_back(CLANG_VERSION_MINOR);
780 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000781 // FIXME: This writes the absolute path for chained headers.
782 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
783 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000784
Douglas Gregor45fe0362009-05-12 01:31:05 +0000785 // Original file name
786 SourceManager &SM = Context.getSourceManager();
787 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
788 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000789 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000790 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
791 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
792
793 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000794
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000795 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000796
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000797 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000798 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000799 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000800 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000801 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000802 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000803 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000804
Ted Kremenek18e066f2010-01-22 22:12:47 +0000805 // Repository branch/version information.
806 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000807 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000808 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
809 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000810 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000811 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000812 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
813 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000814}
815
816/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000817void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000818 RecordData Record;
819 Record.push_back(LangOpts.Trigraphs);
820 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
821 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
822 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
823 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000824 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000825 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
826 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
827 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
828 Record.push_back(LangOpts.C99); // C99 Support
829 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
830 Record.push_back(LangOpts.CPlusPlus); // C++ Support
831 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000832 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000833
Douglas Gregor55abb232009-04-10 20:39:37 +0000834 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
835 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000836 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000837 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000838 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000839 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000840 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000841
Douglas Gregor55abb232009-04-10 20:39:37 +0000842 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000843 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
844 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000845 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000846 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000847 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000848
849 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
850 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
851 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
852
Chris Lattner258172e2009-04-27 07:35:58 +0000853 // Whether static initializers are protected by locks.
854 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000855 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000856 Record.push_back(LangOpts.Blocks); // block extension to C
857 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
858 // they are unused.
859 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
860 // (modulo the platform support).
861
Chris Lattner51924e512010-06-26 21:25:03 +0000862 Record.push_back(LangOpts.getSignedOverflowBehavior());
863 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000864
865 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000866 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000867 // defined.
868 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
869 // opposed to __DYNAMIC__).
870 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
871
872 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
873 // used (instead of C99 semantics).
874 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000875 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
876 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000877 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
878 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000879 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000880 Record.push_back(LangOpts.getGCMode());
881 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000882 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000883 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000884 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000885 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000886 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000887 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000888 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000889}
890
Douglas Gregora7f71a92009-04-10 03:52:48 +0000891//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000892// stat cache Serialization
893//===----------------------------------------------------------------------===//
894
895namespace {
896// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000897class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000898public:
899 typedef const char * key_type;
900 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000901
Douglas Gregorc5046832009-04-27 18:38:38 +0000902 typedef std::pair<int, struct stat> data_type;
903 typedef const data_type& data_type_ref;
904
905 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000906 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000907 }
Mike Stump11289f42009-09-09 15:08:12 +0000908
909 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000910 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
911 data_type_ref Data) {
912 unsigned StrLen = strlen(path);
913 clang::io::Emit16(Out, StrLen);
914 unsigned DataLen = 1; // result value
915 if (Data.first == 0)
916 DataLen += 4 + 4 + 2 + 8 + 8;
917 clang::io::Emit8(Out, DataLen);
918 return std::make_pair(StrLen + 1, DataLen);
919 }
Mike Stump11289f42009-09-09 15:08:12 +0000920
Douglas Gregorc5046832009-04-27 18:38:38 +0000921 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
922 Out.write(path, KeyLen);
923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
Douglas Gregorc5046832009-04-27 18:38:38 +0000925 void EmitData(llvm::raw_ostream& Out, key_type_ref,
926 data_type_ref Data, unsigned DataLen) {
927 using namespace clang::io;
928 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000929
Douglas Gregorc5046832009-04-27 18:38:38 +0000930 // Result of stat()
931 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000932
Douglas Gregorc5046832009-04-27 18:38:38 +0000933 if (Data.first == 0) {
934 Emit32(Out, (uint32_t) Data.second.st_ino);
935 Emit32(Out, (uint32_t) Data.second.st_dev);
936 Emit16(Out, (uint16_t) Data.second.st_mode);
937 Emit64(Out, (uint64_t) Data.second.st_mtime);
938 Emit64(Out, (uint64_t) Data.second.st_size);
939 }
940
941 assert(Out.tell() - Start == DataLen && "Wrong data length");
942 }
943};
944} // end anonymous namespace
945
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000946/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000947void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000948 // Build the on-disk hash table containing information about every
949 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000950 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000951 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000952 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000953 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000954 Stat != StatEnd; ++Stat, ++NumStatEntries) {
955 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000956 Generator.insert(Filename, Stat->second);
957 }
Mike Stump11289f42009-09-09 15:08:12 +0000958
Douglas Gregorc5046832009-04-27 18:38:38 +0000959 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000960 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000961 uint32_t BucketOffset;
962 {
963 llvm::raw_svector_ostream Out(StatCacheData);
964 // Make sure that no bucket is at offset 0
965 clang::io::Emit32(Out, 0);
966 BucketOffset = Generator.Emit(Out);
967 }
968
969 // Create a blob abbreviation
970 using namespace llvm;
971 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000972 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
976 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
977
978 // Write the stat cache
979 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000980 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +0000981 Record.push_back(BucketOffset);
982 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000983 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000984}
985
986//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000987// Source Manager Serialization
988//===----------------------------------------------------------------------===//
989
990/// \brief Create an abbreviation for the SLocEntry that refers to a
991/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000992static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000993 using namespace llvm;
994 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000995 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +0000996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001000 // FileEntry fields.
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001003 // HeaderFileInfo fields.
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001009 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001010}
1011
1012/// \brief Create an abbreviation for the SLocEntry that refers to a
1013/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001014static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001015 using namespace llvm;
1016 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001017 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001023 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001024}
1025
1026/// \brief Create an abbreviation for the SLocEntry that refers to a
1027/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001028static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001029 using namespace llvm;
1030 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001031 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001033 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001034}
1035
1036/// \brief Create an abbreviation for the SLocEntry that refers to an
1037/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001038static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001039 using namespace llvm;
1040 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001041 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001047 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001048}
1049
1050/// \brief Writes the block containing the serialized form of the
1051/// source manager.
1052///
1053/// TODO: We should probably use an on-disk hash table (stored in a
1054/// blob), indexed based on the file name, so that we only create
1055/// entries for files that we actually need. In the common case (no
1056/// errors), we probably won't have to create file entries for any of
1057/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001058void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001059 const Preprocessor &PP,
1060 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001061 RecordData Record;
1062
Chris Lattner0910e3b2009-04-10 17:16:57 +00001063 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001064 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001065
1066 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001067 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1068 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1069 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1070 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001071
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001072 // Write the line table.
1073 if (SourceMgr.hasLineTable()) {
1074 LineTableInfo &LineTable = SourceMgr.getLineTable();
1075
1076 // Emit the file names
1077 Record.push_back(LineTable.getNumFilenames());
1078 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1079 // Emit the file name
1080 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001081 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001082 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1083 Record.push_back(FilenameLen);
1084 if (FilenameLen)
1085 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001088 // Emit the line entries
1089 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1090 L != LEnd; ++L) {
1091 // Emit the file ID
1092 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001093
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001094 // Emit the line entries
1095 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001096 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001097 LEEnd = L->second.end();
1098 LE != LEEnd; ++LE) {
1099 Record.push_back(LE->FileOffset);
1100 Record.push_back(LE->LineNo);
1101 Record.push_back(LE->FilenameID);
1102 Record.push_back((unsigned)LE->FileKind);
1103 Record.push_back(LE->IncludeOffset);
1104 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001105 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001106 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001107 }
1108
Douglas Gregor258ae542009-04-27 06:38:32 +00001109 // Write out the source location entry table. We skip the first
1110 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001111 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001112 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001113 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1114 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1115 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1116 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001117 // Get this source location entry.
1118 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001119
Douglas Gregor258ae542009-04-27 06:38:32 +00001120 // Record the offset of this source-location entry.
1121 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1122
1123 // Figure out which record code to use.
1124 unsigned Code;
1125 if (SLoc->isFile()) {
1126 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001127 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001128 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001129 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001130 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001131 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001132 Record.clear();
1133 Record.push_back(Code);
1134
1135 Record.push_back(SLoc->getOffset());
1136 if (SLoc->isFile()) {
1137 const SrcMgr::FileInfo &File = SLoc->getFile();
1138 Record.push_back(File.getIncludeLoc().getRawEncoding());
1139 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1140 Record.push_back(File.hasLineDirectives());
1141
1142 const SrcMgr::ContentCache *Content = File.getContentCache();
1143 if (Content->Entry) {
1144 // The source location entry is a file. The blob associated
1145 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001146
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001147 // Emit size/modification time for this file.
1148 Record.push_back(Content->Entry->getSize());
1149 Record.push_back(Content->Entry->getModificationTime());
1150
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001151 // Emit header-search information associated with this file.
1152 HeaderFileInfo HFI;
1153 HeaderSearch &HS = PP.getHeaderSearchInfo();
1154 if (Content->Entry->getUID() < HS.header_file_size())
1155 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1156 Record.push_back(HFI.isImport);
1157 Record.push_back(HFI.DirInfo);
1158 Record.push_back(HFI.NumIncludes);
1159 AddIdentifierRef(HFI.ControllingMacro, Record);
1160
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001161 // Turn the file name into an absolute path, if it isn't already.
1162 const char *Filename = Content->Entry->getName();
1163 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001164 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001165 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001166
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001167 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001168 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001169
1170 // FIXME: For now, preload all file source locations, so that
1171 // we get the appropriate File entries in the reader. This is
1172 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001173 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001174 } else {
1175 // The source location entry is a buffer. The blob associated
1176 // with this entry contains the contents of the buffer.
1177
1178 // We add one to the size so that we capture the trailing NULL
1179 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1180 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001181 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001182 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001183 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001184 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1185 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001186 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001187 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001188 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001189 llvm::StringRef(Buffer->getBufferStart(),
1190 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001191
1192 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001193 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001194 }
1195 } else {
1196 // The source location entry is an instantiation.
1197 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1198 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1199 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1200 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1201
1202 // Compute the token length for this macro expansion.
1203 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001204 if (I + 1 != N)
1205 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001206 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1207 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1208 }
1209 }
1210
Douglas Gregor8f45df52009-04-16 22:23:12 +00001211 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001212
1213 if (SLocEntryOffsets.empty())
1214 return;
1215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001216 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001217 // table is used for lazily loading source-location information.
1218 using namespace llvm;
1219 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001220 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1224 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001225
Douglas Gregor258ae542009-04-27 06:38:32 +00001226 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001227 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001228 Record.push_back(SLocEntryOffsets.size());
1229 Record.push_back(SourceMgr.getNextOffset());
1230 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001231 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001232 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001233
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001234 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001235 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001236 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001237}
1238
Douglas Gregorc5046832009-04-27 18:38:38 +00001239//===----------------------------------------------------------------------===//
1240// Preprocessor Serialization
1241//===----------------------------------------------------------------------===//
1242
Chris Lattnereeffaef2009-04-10 17:15:23 +00001243/// \brief Writes the block containing the serialized form of the
1244/// preprocessor.
1245///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001246void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001247 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001248
Chris Lattner0af3ba12009-04-13 01:29:17 +00001249 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1250 if (PP.getCounterValue() != 0) {
1251 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001252 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001253 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001254 }
1255
1256 // Enter the preprocessor block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001257 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001258
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001259 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001260 // FIXME: use diagnostics subsystem for localization etc.
1261 if (PP.SawDateOrTime())
1262 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001263
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001264 // Loop over all the macro definitions that are live at the end of the file,
1265 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001266 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001267 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1268 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001269 // FIXME: This emits macros in hash table order, we should do it in a stable
1270 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001271 MacroInfo *MI = I->second;
1272
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001273 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001274 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001275 // Also skip macros from a AST file if we're chaining.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001276 if (MI->isBuiltinMacro() || (Chain && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001277 continue;
1278
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001279 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001280 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001281 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1282 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001283
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001284 unsigned Code;
1285 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001286 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001287 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001288 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001289
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001290 Record.push_back(MI->isC99Varargs());
1291 Record.push_back(MI->isGNUVarargs());
1292 Record.push_back(MI->getNumArgs());
1293 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1294 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001295 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001296 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001297
1298 // If we have a detailed preprocessing record, record the macro definition
1299 // ID that corresponds to this macro.
1300 if (PPRec)
1301 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1302
Douglas Gregor8f45df52009-04-16 22:23:12 +00001303 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001304 Record.clear();
1305
Chris Lattner2199f5b2009-04-10 18:08:30 +00001306 // Emit the tokens array.
1307 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1308 // Note that we know that the preprocessor does not have any annotation
1309 // tokens in it because they are created by the parser, and thus can't be
1310 // in a macro definition.
1311 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001312
Chris Lattner2199f5b2009-04-10 18:08:30 +00001313 Record.push_back(Tok.getLocation().getRawEncoding());
1314 Record.push_back(Tok.getLength());
1315
Chris Lattner2199f5b2009-04-10 18:08:30 +00001316 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1317 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001318 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001319
Chris Lattner2199f5b2009-04-10 18:08:30 +00001320 // FIXME: Should translate token kind to a stable encoding.
1321 Record.push_back(Tok.getKind());
1322 // FIXME: Should translate token flags to a stable encoding.
1323 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001324
Sebastian Redl539c5062010-08-18 23:57:32 +00001325 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001326 Record.clear();
1327 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001328 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001329 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001330
1331 // If the preprocessor has a preprocessing record, emit it.
1332 unsigned NumPreprocessingRecords = 0;
1333 if (PPRec) {
1334 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1335 E != EEnd; ++E) {
1336 Record.clear();
1337
1338 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1339 Record.push_back(NumPreprocessingRecords++);
1340 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1341 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1342 AddIdentifierRef(MI->getName(), Record);
1343 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
Sebastian Redl539c5062010-08-18 23:57:32 +00001344 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001345 continue;
1346 }
1347
1348 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1349 // Record this macro definition's location.
Sebastian Redl539c5062010-08-18 23:57:32 +00001350 IdentID ID = getMacroDefinitionID(MD);
Douglas Gregoraae92242010-03-19 21:51:54 +00001351 if (ID != MacroDefinitionOffsets.size()) {
1352 if (ID > MacroDefinitionOffsets.size())
1353 MacroDefinitionOffsets.resize(ID + 1);
1354
1355 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1356 } else
1357 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1358
1359 Record.push_back(NumPreprocessingRecords++);
1360 Record.push_back(ID);
1361 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1362 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1363 AddIdentifierRef(MD->getName(), Record);
1364 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001365 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001366 continue;
1367 }
1368 }
1369 }
1370
Douglas Gregor8f45df52009-04-16 22:23:12 +00001371 Stream.ExitBlock();
Douglas Gregoraae92242010-03-19 21:51:54 +00001372
1373 // Write the offsets table for the preprocessing record.
1374 if (NumPreprocessingRecords > 0) {
1375 // Write the offsets table for identifier IDs.
1376 using namespace llvm;
1377 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001378 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1382 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1383
1384 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001385 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001386 Record.push_back(NumPreprocessingRecords);
1387 Record.push_back(MacroDefinitionOffsets.size());
1388 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001389 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001390 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1391 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001392}
1393
Douglas Gregorc5046832009-04-27 18:38:38 +00001394//===----------------------------------------------------------------------===//
1395// Type Serialization
1396//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001397
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001398/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001399void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001400 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001401 if (Idx.getIndex() == 0) // we haven't seen this type before.
1402 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001403
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001404 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001405 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001406 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001407 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001408 else if (TypeOffsets.size() < Index) {
1409 TypeOffsets.resize(Index + 1);
1410 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001411 }
1412
1413 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001414
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001415 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001416 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001417
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001418 if (T.hasLocalNonFastQualifiers()) {
1419 Qualifiers Qs = T.getLocalQualifiers();
1420 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001421 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001422 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001423 } else {
1424 switch (T->getTypeClass()) {
1425 // For all of the concrete, non-dependent types, call the
1426 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001427#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001428 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001429#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001430#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001431 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001432 }
1433
1434 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001435 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001436
1437 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001438 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001439}
1440
Douglas Gregorc5046832009-04-27 18:38:38 +00001441//===----------------------------------------------------------------------===//
1442// Declaration Serialization
1443//===----------------------------------------------------------------------===//
1444
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001445/// \brief Write the block containing all of the declaration IDs
1446/// lexically declared within the given DeclContext.
1447///
1448/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1449/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001450uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001451 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001452 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001453 return 0;
1454
Douglas Gregor8f45df52009-04-16 22:23:12 +00001455 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001456 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001457 Record.push_back(DECL_CONTEXT_LEXICAL);
1458 llvm::SmallVector<DeclID, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001459 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1460 D != DEnd; ++D)
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001461 Decls.push_back(GetDeclRef(*D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001462
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001463 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001464 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Sebastian Redl539c5062010-08-18 23:57:32 +00001465 reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(DeclID));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001466 return Offset;
1467}
1468
1469/// \brief Write the block containing all of the declaration IDs
1470/// visible from the given DeclContext.
1471///
1472/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1473/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001474uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001475 DeclContext *DC) {
1476 if (DC->getPrimaryContext() != DC)
1477 return 0;
1478
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001479 // Since there is no name lookup into functions or methods, don't bother to
1480 // build a visible-declarations table for these entities.
1481 if (DC->isFunctionOrMethod())
1482 return 0;
1483
1484 // If not in C++, we perform name lookup for the translation unit via the
1485 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1486 // FIXME: In C++ we need the visible declarations in order to "see" the
1487 // friend declarations, is there a way to do this without writing the table ?
1488 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor13d190f2009-04-18 15:49:20 +00001489 return 0;
1490
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001491 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001492 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001493
1494 // Serialize the contents of the mapping used for lookup. Note that,
1495 // although we have two very different code paths, the serialized
1496 // representation is the same for both cases: a declaration name,
1497 // followed by a size, followed by references to the visible
1498 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001499 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001500 RecordData Record;
1501 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001502 if (!Map)
1503 return 0;
1504
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001505 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1506 D != DEnd; ++D) {
1507 AddDeclarationName(D->first, Record);
1508 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1509 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001510 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001511 AddDeclRef(*Result.first, Record);
1512 }
1513
1514 if (Record.size() == 0)
1515 return 0;
1516
Sebastian Redl539c5062010-08-18 23:57:32 +00001517 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001518 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001519 return Offset;
1520}
1521
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001522void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001523 using namespace llvm;
1524 RecordData Record;
1525
1526 // Write the type offsets array
1527 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001528 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1531 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1532 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001533 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001534 Record.push_back(TypeOffsets.size());
1535 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001536 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001537 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1538
1539 // Write the declaration offsets array
1540 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001541 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1544 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1545 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001546 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001547 Record.push_back(DeclOffsets.size());
1548 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001549 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001550 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1551}
1552
Douglas Gregorc5046832009-04-27 18:38:38 +00001553//===----------------------------------------------------------------------===//
1554// Global Method Pool and Selector Serialization
1555//===----------------------------------------------------------------------===//
1556
Douglas Gregore84a9da2009-04-20 20:36:09 +00001557namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001558// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001559class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001560 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001561
1562public:
1563 typedef Selector key_type;
1564 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001565
Sebastian Redl834bb972010-08-04 17:20:04 +00001566 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001567 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001568 ObjCMethodList Instance, Factory;
1569 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001570 typedef const data_type& data_type_ref;
1571
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001572 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001573
Douglas Gregorc78d3462009-04-24 21:10:55 +00001574 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001575 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001576 }
Mike Stump11289f42009-09-09 15:08:12 +00001577
1578 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001579 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1580 data_type_ref Methods) {
1581 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1582 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001583 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1584 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001585 Method = Method->Next)
1586 if (Method->Method)
1587 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001588 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001589 Method = Method->Next)
1590 if (Method->Method)
1591 DataLen += 4;
1592 clang::io::Emit16(Out, DataLen);
1593 return std::make_pair(KeyLen, DataLen);
1594 }
Mike Stump11289f42009-09-09 15:08:12 +00001595
Douglas Gregor95c13f52009-04-25 17:48:32 +00001596 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001597 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001598 assert((Start >> 32) == 0 && "Selector key offset too large");
1599 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001600 unsigned N = Sel.getNumArgs();
1601 clang::io::Emit16(Out, N);
1602 if (N == 0)
1603 N = 1;
1604 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001605 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001606 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1607 }
Mike Stump11289f42009-09-09 15:08:12 +00001608
Douglas Gregorc78d3462009-04-24 21:10:55 +00001609 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001610 data_type_ref Methods, unsigned DataLen) {
1611 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001612 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001613 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001614 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001615 Method = Method->Next)
1616 if (Method->Method)
1617 ++NumInstanceMethods;
1618
1619 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001620 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001621 Method = Method->Next)
1622 if (Method->Method)
1623 ++NumFactoryMethods;
1624
1625 clang::io::Emit16(Out, NumInstanceMethods);
1626 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001627 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001628 Method = Method->Next)
1629 if (Method->Method)
1630 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001631 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001632 Method = Method->Next)
1633 if (Method->Method)
1634 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001635
1636 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001637 }
1638};
1639} // end anonymous namespace
1640
Sebastian Redla19a67f2010-08-03 21:58:15 +00001641/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001642///
1643/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001644/// in an on-disk hash table indexed by the selector. The hash table also
1645/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001646void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001647 using namespace llvm;
1648
Sebastian Redla19a67f2010-08-03 21:58:15 +00001649 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001650 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001651 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001652 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001653 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001654 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001655 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001656 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001657
Sebastian Redla19a67f2010-08-03 21:58:15 +00001658 // Create the on-disk hash table representation. We walk through every
1659 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001660 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001661 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001662 I = SelectorIDs.begin(), E = SelectorIDs.end();
1663 I != E; ++I) {
1664 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001665 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001666 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001667 I->second,
1668 ObjCMethodList(),
1669 ObjCMethodList()
1670 };
1671 if (F != SemaRef.MethodPool.end()) {
1672 Data.Instance = F->second.first;
1673 Data.Factory = F->second.second;
1674 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001675 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001676 // changed.
1677 if (Chain && I->second < FirstSelectorID) {
1678 // Selector already exists. Did it change?
1679 bool changed = false;
1680 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1681 M = M->Next) {
1682 if (M->Method->getPCHLevel() == 0)
1683 changed = true;
1684 }
1685 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1686 M = M->Next) {
1687 if (M->Method->getPCHLevel() == 0)
1688 changed = true;
1689 }
1690 if (!changed)
1691 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001692 } else if (Data.Instance.Method || Data.Factory.Method) {
1693 // A new method pool entry.
1694 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001695 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001696 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001697 }
1698
Douglas Gregorc78d3462009-04-24 21:10:55 +00001699 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001700 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001701 uint32_t BucketOffset;
1702 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001703 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001704 llvm::raw_svector_ostream Out(MethodPool);
1705 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001706 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001707 BucketOffset = Generator.Emit(Out, Trait);
1708 }
1709
1710 // Create a blob abbreviation
1711 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001712 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001715 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1716 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1717
Douglas Gregor95c13f52009-04-25 17:48:32 +00001718 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001719 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001720 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001721 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001722 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001723 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001724
1725 // Create a blob abbreviation for the selector table offsets.
1726 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1730 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1731
1732 // Write the selector offsets table.
1733 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001734 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001735 Record.push_back(SelectorOffsets.size());
1736 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001737 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001738 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001739 }
1740}
1741
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001742/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001743void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001744 using namespace llvm;
1745 if (SemaRef.ReferencedSelectors.empty())
1746 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001747
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001748 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001749
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001750 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001751 // very tricky to fix, and given that @selector shouldn't really appear in
1752 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001753 for (DenseMap<Selector, SourceLocation>::iterator S =
1754 SemaRef.ReferencedSelectors.begin(),
1755 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1756 Selector Sel = (*S).first;
1757 SourceLocation Loc = (*S).second;
1758 AddSelectorRef(Sel, Record);
1759 AddSourceLocation(Loc, Record);
1760 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001761 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001762}
1763
Douglas Gregorc5046832009-04-27 18:38:38 +00001764//===----------------------------------------------------------------------===//
1765// Identifier Table Serialization
1766//===----------------------------------------------------------------------===//
1767
Douglas Gregorc78d3462009-04-24 21:10:55 +00001768namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001769class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001770 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001771 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001772
Douglas Gregor1d583f22009-04-28 21:18:29 +00001773 /// \brief Determines whether this is an "interesting" identifier
1774 /// that needs a full IdentifierInfo structure written into the hash
1775 /// table.
1776 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1777 return II->isPoisoned() ||
1778 II->isExtensionToken() ||
1779 II->hasMacroDefinition() ||
1780 II->getObjCOrBuiltinID() ||
1781 II->getFETokenInfo<void>();
1782 }
1783
Douglas Gregore84a9da2009-04-20 20:36:09 +00001784public:
1785 typedef const IdentifierInfo* key_type;
1786 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001787
Sebastian Redl539c5062010-08-18 23:57:32 +00001788 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001789 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001790
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001791 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001792 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001793
1794 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001795 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001796 }
Mike Stump11289f42009-09-09 15:08:12 +00001797
1798 std::pair<unsigned,unsigned>
1799 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001800 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001801 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001802 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1803 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001804 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001805 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001806 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001807 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001808 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1809 DEnd = IdentifierResolver::end();
1810 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001811 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001812 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001813 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001814 // We emit the key length after the data length so that every
1815 // string is preceded by a 16-bit length. This matches the PTH
1816 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001817 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001818 return std::make_pair(KeyLen, DataLen);
1819 }
Mike Stump11289f42009-09-09 15:08:12 +00001820
1821 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001822 unsigned KeyLen) {
1823 // Record the location of the key data. This is used when generating
1824 // the mapping from persistent IDs to strings.
1825 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001826 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001827 }
Mike Stump11289f42009-09-09 15:08:12 +00001828
1829 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001830 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001831 if (!isInterestingIdentifier(II)) {
1832 clang::io::Emit32(Out, ID << 1);
1833 return;
1834 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001835
Douglas Gregor1d583f22009-04-28 21:18:29 +00001836 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001837 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001838 bool hasMacroDefinition =
1839 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001840 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001841 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001842 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1843 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1844 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001845 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001846 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001847 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001848
Douglas Gregorc3366a52009-04-21 23:56:24 +00001849 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001850 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001851
Douglas Gregora868bbd2009-04-21 22:25:48 +00001852 // Emit the declaration IDs in reverse order, because the
1853 // IdentifierResolver provides the declarations as they would be
1854 // visible (e.g., the function "stat" would come before the struct
1855 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1856 // adds declarations to the end of the list (so we need to see the
1857 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001858 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001859 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001860 IdentifierResolver::end());
1861 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1862 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001863 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001864 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001865 }
1866};
1867} // end anonymous namespace
1868
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001869/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001870///
1871/// The identifier table consists of a blob containing string data
1872/// (the actual identifiers themselves) and a separate "offsets" index
1873/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001874void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001875 using namespace llvm;
1876
1877 // Create and write out the blob that contains the identifier
1878 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001879 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001880 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001881 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001882
Douglas Gregore6648fb2009-04-28 20:33:11 +00001883 // Look for any identifiers that were named while processing the
1884 // headers, but are otherwise not needed. We add these to the hash
1885 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001886 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001887 // file.
1888 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1889 IDEnd = PP.getIdentifierTable().end();
1890 ID != IDEnd; ++ID)
1891 getIdentifierRef(ID->second);
1892
Sebastian Redlff4a2952010-07-23 23:49:55 +00001893 // Create the on-disk hash table representation. We only store offsets
1894 // for identifiers that appear here for the first time.
1895 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001896 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001897 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1898 ID != IDEnd; ++ID) {
1899 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001900 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001901 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001902 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001903
Douglas Gregore84a9da2009-04-20 20:36:09 +00001904 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001905 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001906 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001907 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001908 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001909 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001910 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001911 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001912 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001913 }
1914
1915 // Create a blob abbreviation
1916 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001917 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001920 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001921
1922 // Write the identifier table
1923 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001924 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001925 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001926 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001927 }
1928
1929 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001930 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001931 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1934 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1935
1936 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001937 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001938 Record.push_back(IdentifierOffsets.size());
1939 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001940 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001941 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001942}
1943
Douglas Gregorc5046832009-04-27 18:38:38 +00001944//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001945// DeclContext's Name Lookup Table Serialization
1946//===----------------------------------------------------------------------===//
1947
1948namespace {
1949// Trait used for the on-disk hash table used in the method pool.
1950class ASTDeclContextNameLookupTrait {
1951 ASTWriter &Writer;
1952
1953public:
1954 typedef DeclarationName key_type;
1955 typedef key_type key_type_ref;
1956
1957 typedef DeclContext::lookup_result data_type;
1958 typedef const data_type& data_type_ref;
1959
1960 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1961
1962 unsigned ComputeHash(DeclarationName Name) {
1963 llvm::FoldingSetNodeID ID;
1964 ID.AddInteger(Name.getNameKind());
1965
1966 switch (Name.getNameKind()) {
1967 case DeclarationName::Identifier:
1968 ID.AddString(Name.getAsIdentifierInfo()->getName());
1969 break;
1970 case DeclarationName::ObjCZeroArgSelector:
1971 case DeclarationName::ObjCOneArgSelector:
1972 case DeclarationName::ObjCMultiArgSelector:
1973 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1974 break;
1975 case DeclarationName::CXXConstructorName:
1976 case DeclarationName::CXXDestructorName:
1977 case DeclarationName::CXXConversionFunctionName:
1978 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1979 break;
1980 case DeclarationName::CXXOperatorName:
1981 ID.AddInteger(Name.getCXXOverloadedOperator());
1982 break;
1983 case DeclarationName::CXXLiteralOperatorName:
1984 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1985 case DeclarationName::CXXUsingDirective:
1986 break;
1987 }
1988
1989 return ID.ComputeHash();
1990 }
1991
1992 std::pair<unsigned,unsigned>
1993 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
1994 data_type_ref Lookup) {
1995 unsigned KeyLen = 1;
1996 switch (Name.getNameKind()) {
1997 case DeclarationName::Identifier:
1998 case DeclarationName::ObjCZeroArgSelector:
1999 case DeclarationName::ObjCOneArgSelector:
2000 case DeclarationName::ObjCMultiArgSelector:
2001 case DeclarationName::CXXConstructorName:
2002 case DeclarationName::CXXDestructorName:
2003 case DeclarationName::CXXConversionFunctionName:
2004 case DeclarationName::CXXLiteralOperatorName:
2005 KeyLen += 4;
2006 break;
2007 case DeclarationName::CXXOperatorName:
2008 KeyLen += 1;
2009 break;
2010 case DeclarationName::CXXUsingDirective:
2011 break;
2012 }
2013 clang::io::Emit16(Out, KeyLen);
2014
2015 // 2 bytes for num of decls and 4 for each DeclID.
2016 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2017 clang::io::Emit16(Out, DataLen);
2018
2019 return std::make_pair(KeyLen, DataLen);
2020 }
2021
2022 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2023 using namespace clang::io;
2024
2025 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2026 Emit8(Out, Name.getNameKind());
2027 switch (Name.getNameKind()) {
2028 case DeclarationName::Identifier:
2029 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2030 break;
2031 case DeclarationName::ObjCZeroArgSelector:
2032 case DeclarationName::ObjCOneArgSelector:
2033 case DeclarationName::ObjCMultiArgSelector:
2034 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2035 break;
2036 case DeclarationName::CXXConstructorName:
2037 case DeclarationName::CXXDestructorName:
2038 case DeclarationName::CXXConversionFunctionName:
2039 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2040 break;
2041 case DeclarationName::CXXOperatorName:
2042 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2043 Emit8(Out, Name.getCXXOverloadedOperator());
2044 break;
2045 case DeclarationName::CXXLiteralOperatorName:
2046 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2047 break;
2048 case DeclarationName::CXXUsingDirective:
2049 break;
2050 }
2051 }
2052
2053 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2054 data_type Lookup, unsigned DataLen) {
2055 uint64_t Start = Out.tell(); (void)Start;
2056 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2057 for (; Lookup.first != Lookup.second; ++Lookup.first)
2058 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2059
2060 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2061 }
2062};
2063} // end anonymous namespace
2064
2065//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002066// General Serialization Routines
2067//===----------------------------------------------------------------------===//
2068
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002069/// \brief Write a record containing the given attributes.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002070void ASTWriter::WriteAttributeRecord(const AttrVec &Attrs) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002071 RecordData Record;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002072 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2073 const Attr * A = *i;
2074 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2075 AddSourceLocation(A->getLocation(), Record);
2076 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002077
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002078#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002079
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002080 }
2081
Sebastian Redl539c5062010-08-18 23:57:32 +00002082 Stream.EmitRecord(DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002083}
2084
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002085void ASTWriter::AddString(const std::string &Str, RecordData &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002086 Record.push_back(Str.size());
2087 Record.insert(Record.end(), Str.begin(), Str.end());
2088}
2089
Douglas Gregore84a9da2009-04-20 20:36:09 +00002090/// \brief Note that the identifier II occurs at the given offset
2091/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002092void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002093 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002094 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002095 // up earlier in the chain and thus don't need an offset.
2096 if (ID >= FirstIdentID)
2097 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002098}
2099
Douglas Gregor95c13f52009-04-25 17:48:32 +00002100/// \brief Note that the selector Sel occurs at the given offset
2101/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002102void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002103 unsigned ID = SelectorIDs[Sel];
2104 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002105 // Don't record offsets for selectors that are also available in a different
2106 // file.
2107 if (ID < FirstSelectorID)
2108 return;
2109 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002110}
2111
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002112ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redld95a56e2010-08-04 18:21:41 +00002113 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002114 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002115 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
2116 NextSelectorID(FirstSelectorID), CollectedStmts(&StmtsToEmit),
2117 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2118 NumVisibleDeclContexts(0) {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002119}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002120
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002121void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002122 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002123 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002124 Stream.Emit((unsigned)'C', 8);
2125 Stream.Emit((unsigned)'P', 8);
2126 Stream.Emit((unsigned)'C', 8);
2127 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002128
Chris Lattner28fa4e62009-04-26 22:26:21 +00002129 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002130
Sebastian Redl143413f2010-07-12 22:02:52 +00002131 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002132 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002133 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002134 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002135}
2136
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002137void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002138 const char *isysroot) {
2139 using namespace llvm;
2140
2141 ASTContext &Context = SemaRef.Context;
2142 Preprocessor &PP = SemaRef.PP;
2143
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002144 // The translation unit is the first declaration we'll emit.
2145 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002146 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002147 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002148
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002149 // Make sure that we emit IdentifierInfos (and any attached
2150 // declarations) for builtins.
2151 {
2152 IdentifierTable &Table = PP.getIdentifierTable();
2153 llvm::SmallVector<const char *, 32> BuiltinNames;
2154 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2155 Context.getLangOptions().NoBuiltin);
2156 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2157 getIdentifierRef(&Table.get(BuiltinNames[I]));
2158 }
2159
Chris Lattner0c797362009-09-08 18:19:27 +00002160 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002161 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002162 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002163 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002164 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2165 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002166 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002167
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002168 // Build a record containing all of the file scoped decls in this file.
2169 RecordData UnusedFileScopedDecls;
2170 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2171 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002172
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002173 RecordData WeakUndeclaredIdentifiers;
2174 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2175 WeakUndeclaredIdentifiers.push_back(
2176 SemaRef.WeakUndeclaredIdentifiers.size());
2177 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2178 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2179 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2180 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2181 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2182 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2183 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2184 }
2185 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002186
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002187 // Build a record containing all of the locally-scoped external
2188 // declarations in this header file. Generally, this record will be
2189 // empty.
2190 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002191 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002192 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002193 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002194 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2195 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2196 TD != TDEnd; ++TD)
2197 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2198
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002199 // Build a record containing all of the ext_vector declarations.
2200 RecordData ExtVectorDecls;
2201 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2202 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2203
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002204 // Build a record containing all of the VTable uses information.
2205 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002206 if (!SemaRef.VTableUses.empty()) {
2207 VTableUses.push_back(SemaRef.VTableUses.size());
2208 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2209 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2210 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2211 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2212 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002213 }
2214
2215 // Build a record containing all of dynamic classes declarations.
2216 RecordData DynamicClasses;
2217 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2218 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2219
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002220 // Build a record containing all of pending implicit instantiations.
2221 RecordData PendingImplicitInstantiations;
2222 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2223 I = SemaRef.PendingImplicitInstantiations.begin(),
2224 N = SemaRef.PendingImplicitInstantiations.end(); I != N; ++I) {
2225 AddDeclRef(I->first, PendingImplicitInstantiations);
2226 AddSourceLocation(I->second, PendingImplicitInstantiations);
2227 }
2228 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2229 "There are local ones at end of translation unit!");
2230
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002231 // Build a record containing some declaration references.
2232 RecordData SemaDeclRefs;
2233 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2234 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2235 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2236 }
2237
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002238 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002239 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002240 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002241 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002242 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002243 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002244 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002245 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002246 // Write the record of special types.
2247 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002248
Steve Naroffc277ad12009-07-18 15:33:26 +00002249 AddTypeRef(Context.getBuiltinVaListType(), Record);
2250 AddTypeRef(Context.getObjCIdType(), Record);
2251 AddTypeRef(Context.getObjCSelType(), Record);
2252 AddTypeRef(Context.getObjCProtoType(), Record);
2253 AddTypeRef(Context.getObjCClassType(), Record);
2254 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2255 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2256 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002257 AddTypeRef(Context.getjmp_bufType(), Record);
2258 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002259 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2260 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002261 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002262 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002263 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2264 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002265 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002266 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002267
Douglas Gregor1970d882009-04-26 03:49:13 +00002268 // Keep writing types and declarations until all types and
2269 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002270 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002271 WriteDeclsBlockAbbrevs();
2272 while (!DeclTypesToEmit.empty()) {
2273 DeclOrType DOT = DeclTypesToEmit.front();
2274 DeclTypesToEmit.pop();
2275 if (DOT.isType())
2276 WriteType(DOT.getType());
2277 else
2278 WriteDecl(Context, DOT.getDecl());
2279 }
2280 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002281
Douglas Gregor45053152009-10-17 17:25:45 +00002282 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002283 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002284 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002285 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002286
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002287 WriteTypeDeclOffsets();
Douglas Gregor652d82a2009-04-18 05:55:16 +00002288
Douglas Gregord4df8652009-04-22 22:02:47 +00002289 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002290 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002291 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002292
2293 // Write the record containing tentative definitions.
2294 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002295 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002296
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002297 // Write the record containing unused file scoped decls.
2298 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002299 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002300
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002301 // Write the record containing weak undeclared identifiers.
2302 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002303 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002304 WeakUndeclaredIdentifiers);
2305
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002306 // Write the record containing locally-scoped external definitions.
2307 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002308 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002309 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002310
2311 // Write the record containing ext_vector type names.
2312 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002313 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002314
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002315 // Write the record containing VTable uses information.
2316 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002317 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002318
2319 // Write the record containing dynamic classes declarations.
2320 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002321 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002322
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002323 // Write the record containing pending implicit instantiations.
2324 if (!PendingImplicitInstantiations.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002325 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS,
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002326 PendingImplicitInstantiations);
2327
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002328 // Write the record containing declaration references of Sema.
2329 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002330 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002331
Douglas Gregor08f01292009-04-17 22:13:46 +00002332 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002333 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002334 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002335 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002336 Record.push_back(NumLexicalDeclContexts);
2337 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002338 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002339 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002340}
2341
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002342void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002343 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002344 using namespace llvm;
2345
Sebastian Redl07a89a82010-07-30 00:29:29 +00002346 FirstDeclID += Chain->getTotalNumDecls();
2347 FirstTypeID += Chain->getTotalNumTypes();
2348 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redld95a56e2010-08-04 18:21:41 +00002349 FirstSelectorID += Chain->getTotalNumSelectors();
Sebastian Redl07a89a82010-07-30 00:29:29 +00002350 NextDeclID = FirstDeclID;
2351 NextTypeID = FirstTypeID;
2352 NextIdentID = FirstIdentID;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002353 NextSelectorID = FirstSelectorID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00002354
Sebastian Redl143413f2010-07-12 22:02:52 +00002355 ASTContext &Context = SemaRef.Context;
2356 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002357
Sebastian Redl143413f2010-07-12 22:02:52 +00002358 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002359 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002360 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002361 if (StatCalls && !isysroot)
2362 WriteStatCache(*StatCalls);
2363 // FIXME: Source manager block should only write new stuff, which could be
2364 // done by tracking the largest ID in the chain
2365 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002366
2367 // The special types are in the chained PCH.
2368
2369 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002370 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002371 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Sebastian Redl539c5062010-08-18 23:57:32 +00002372 llvm::SmallVector<DeclID, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002373 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2374 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002375 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002376 if ((*I)->getPCHLevel() == 0)
2377 NewGlobalDecls.push_back(GetDeclRef(*I));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002378 else if ((*I)->isChangedSinceDeserialization())
2379 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002380 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002381 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002382 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002383 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002384 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2385 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2386 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002387 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002388 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2389 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Sebastian Redl539c5062010-08-18 23:57:32 +00002390 NewGlobalDecls.size() * sizeof(DeclID));
Sebastian Redl143413f2010-07-12 22:02:52 +00002391
Sebastian Redl98912122010-07-27 23:01:28 +00002392 // Build a record containing all of the new tentative definitions in this
2393 // file, in TentativeDefinitions order.
2394 RecordData TentativeDefinitions;
2395 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2396 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2397 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2398 }
2399
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002400 // Build a record containing all of the file scoped decls in this file.
2401 RecordData UnusedFileScopedDecls;
2402 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2403 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2404 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002405 }
2406
Sebastian Redl08aca90252010-08-05 18:21:25 +00002407 // We write the entire table, overwriting the tables from the chain.
2408 RecordData WeakUndeclaredIdentifiers;
2409 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2410 WeakUndeclaredIdentifiers.push_back(
2411 SemaRef.WeakUndeclaredIdentifiers.size());
2412 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2413 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2414 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2415 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2416 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2417 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2418 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2419 }
2420 }
2421
Sebastian Redl98912122010-07-27 23:01:28 +00002422 // Build a record containing all of the locally-scoped external
2423 // declarations in this header file. Generally, this record will be
2424 // empty.
2425 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002426 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002427 // nondeterminstic!
2428 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2429 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2430 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2431 TD != TDEnd; ++TD) {
2432 if (TD->second->getPCHLevel() == 0)
2433 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2434 }
2435
2436 // Build a record containing all of the ext_vector declarations.
2437 RecordData ExtVectorDecls;
2438 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2439 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2440 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2441 }
2442
Sebastian Redl08aca90252010-08-05 18:21:25 +00002443 // Build a record containing all of the VTable uses information.
2444 // We write everything here, because it's too hard to determine whether
2445 // a use is new to this part.
2446 RecordData VTableUses;
2447 if (!SemaRef.VTableUses.empty()) {
2448 VTableUses.push_back(SemaRef.VTableUses.size());
2449 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2450 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2451 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2452 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2453 }
2454 }
2455
2456 // Build a record containing all of dynamic classes declarations.
2457 RecordData DynamicClasses;
2458 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2459 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2460 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2461
2462 // Build a record containing all of pending implicit instantiations.
2463 RecordData PendingImplicitInstantiations;
2464 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2465 I = SemaRef.PendingImplicitInstantiations.begin(),
2466 N = SemaRef.PendingImplicitInstantiations.end(); I != N; ++I) {
2467 if (I->first->getPCHLevel() == 0) {
2468 AddDeclRef(I->first, PendingImplicitInstantiations);
2469 AddSourceLocation(I->second, PendingImplicitInstantiations);
2470 }
2471 }
2472 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2473 "There are local ones at end of translation unit!");
2474
2475 // Build a record containing some declaration references.
2476 // It's not worth the effort to avoid duplication here.
2477 RecordData SemaDeclRefs;
2478 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2479 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2480 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2481 }
2482
Sebastian Redl539c5062010-08-18 23:57:32 +00002483 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002484 WriteDeclsBlockAbbrevs();
2485 while (!DeclTypesToEmit.empty()) {
2486 DeclOrType DOT = DeclTypesToEmit.front();
2487 DeclTypesToEmit.pop();
2488 if (DOT.isType())
2489 WriteType(DOT.getType());
2490 else
2491 WriteDecl(Context, DOT.getDecl());
2492 }
2493 Stream.ExitBlock();
2494
Sebastian Redl98912122010-07-27 23:01:28 +00002495 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002496 WriteSelectors(SemaRef);
2497 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002498 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002499 WriteTypeDeclOffsets();
Sebastian Redl98912122010-07-27 23:01:28 +00002500
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002501 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002502 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002503 RecordData FirstLatestDeclIDs;
2504 for (FirstLatestDeclMap::iterator
2505 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2506 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2507 "Expected first & second to be in different PCHs");
2508 AddDeclRef(I->first, FirstLatestDeclIDs);
2509 AddDeclRef(I->second, FirstLatestDeclIDs);
2510 }
2511 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002512 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002513
Sebastian Redl98912122010-07-27 23:01:28 +00002514 // Write the record containing external, unnamed definitions.
2515 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002516 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002517
2518 // Write the record containing tentative definitions.
2519 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002520 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002521
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002522 // Write the record containing unused file scoped decls.
2523 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002524 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002525
Sebastian Redl08aca90252010-08-05 18:21:25 +00002526 // Write the record containing weak undeclared identifiers.
2527 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002528 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002529 WeakUndeclaredIdentifiers);
2530
Sebastian Redl98912122010-07-27 23:01:28 +00002531 // Write the record containing locally-scoped external definitions.
2532 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002533 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002534 LocallyScopedExternalDecls);
2535
2536 // Write the record containing ext_vector type names.
2537 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002538 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002539
Sebastian Redl08aca90252010-08-05 18:21:25 +00002540 // Write the record containing VTable uses information.
2541 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002542 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002543
2544 // Write the record containing dynamic classes declarations.
2545 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002546 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002547
2548 // Write the record containing pending implicit instantiations.
2549 if (!PendingImplicitInstantiations.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002550 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002551 PendingImplicitInstantiations);
2552
2553 // Write the record containing declaration references of Sema.
2554 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002555 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002556
2557 Record.clear();
2558 Record.push_back(NumStatements);
2559 Record.push_back(NumMacros);
2560 Record.push_back(NumLexicalDeclContexts);
2561 Record.push_back(NumVisibleDeclContexts);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002562 WriteDeclUpdateBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002563 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002564 Stream.ExitBlock();
2565}
2566
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002567void ASTWriter::WriteDeclUpdateBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002568 if (ReplacedDecls.empty())
2569 return;
2570
2571 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002572 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002573 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2574 Record.push_back(I->first);
2575 Record.push_back(I->second);
2576 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002577 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002578}
2579
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002580void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002581 Record.push_back(Loc.getRawEncoding());
2582}
2583
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002584void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002585 AddSourceLocation(Range.getBegin(), Record);
2586 AddSourceLocation(Range.getEnd(), Record);
2587}
2588
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002589void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002590 Record.push_back(Value.getBitWidth());
2591 unsigned N = Value.getNumWords();
2592 const uint64_t* Words = Value.getRawData();
2593 for (unsigned I = 0; I != N; ++I)
2594 Record.push_back(Words[I]);
2595}
2596
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002597void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002598 Record.push_back(Value.isUnsigned());
2599 AddAPInt(Value, Record);
2600}
2601
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002602void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002603 AddAPInt(Value.bitcastToAPInt(), Record);
2604}
2605
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002606void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002607 Record.push_back(getIdentifierRef(II));
2608}
2609
Sebastian Redl539c5062010-08-18 23:57:32 +00002610IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002611 if (II == 0)
2612 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002613
Sebastian Redl539c5062010-08-18 23:57:32 +00002614 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002615 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002616 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002617 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002618}
2619
Sebastian Redl539c5062010-08-18 23:57:32 +00002620IdentID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002621 if (MD == 0)
2622 return 0;
2623
Sebastian Redl539c5062010-08-18 23:57:32 +00002624 IdentID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002625 if (ID == 0)
2626 ID = MacroDefinitions.size();
2627 return ID;
2628}
2629
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002630void ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002631 Record.push_back(getSelectorRef(SelRef));
2632}
2633
Sebastian Redl539c5062010-08-18 23:57:32 +00002634SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002635 if (Sel.getAsOpaquePtr() == 0) {
2636 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002637 }
2638
Sebastian Redl539c5062010-08-18 23:57:32 +00002639 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002640 if (SID == 0 && Chain) {
2641 // This might trigger a ReadSelector callback, which will set the ID for
2642 // this selector.
2643 Chain->LoadSelector(Sel);
2644 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002645 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002646 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002647 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002648 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002649}
2650
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002651void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002652 AddDeclRef(Temp->getDestructor(), Record);
2653}
2654
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002655void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002656 const TemplateArgumentLocInfo &Arg,
2657 RecordData &Record) {
2658 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002659 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002660 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002661 break;
2662 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002663 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002664 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002665 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002666 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2667 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002668 break;
John McCall0ad16662009-10-29 08:12:44 +00002669 case TemplateArgument::Null:
2670 case TemplateArgument::Integral:
2671 case TemplateArgument::Declaration:
2672 case TemplateArgument::Pack:
2673 break;
2674 }
2675}
2676
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002677void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002678 RecordData &Record) {
2679 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002680
2681 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2682 bool InfoHasSameExpr
2683 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2684 Record.push_back(InfoHasSameExpr);
2685 if (InfoHasSameExpr)
2686 return; // Avoid storing the same expr twice.
2687 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002688 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2689 Record);
2690}
2691
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002692void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002693 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002694 AddTypeRef(QualType(), Record);
2695 return;
2696 }
2697
John McCallbcd03502009-12-07 02:54:59 +00002698 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002699 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002700 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002701 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002702}
2703
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002704void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002705 Record.push_back(GetOrCreateTypeID(T));
2706}
2707
2708TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002709 return MakeTypeID(T,
2710 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2711}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002712
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002713TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002714 return MakeTypeID(T,
2715 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002716}
2717
2718TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2719 if (T.isNull())
2720 return TypeIdx();
2721 assert(!T.getLocalFastQualifiers());
2722
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002723 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002724 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002725 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002726 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002727 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002728 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002729 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002730 return Idx;
2731}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002732
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002733TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002734 if (T.isNull())
2735 return TypeIdx();
2736 assert(!T.getLocalFastQualifiers());
2737
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002738 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2739 assert(I != TypeIdxs.end() && "Type not emitted!");
2740 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002741}
2742
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002743void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002744 Record.push_back(GetDeclRef(D));
2745}
2746
Sebastian Redl539c5062010-08-18 23:57:32 +00002747DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002748 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002749 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002750 }
2751
Sebastian Redl539c5062010-08-18 23:57:32 +00002752 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002753 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002754 // We haven't seen this declaration before. Give it a new ID and
2755 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002756 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002757 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002758 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2759 // We don't add it to the replacement collection here, because we don't
2760 // have the offset yet.
2761 DeclTypesToEmit.push(const_cast<Decl *>(D));
2762 // Reset the flag, so that we don't add this decl multiple times.
2763 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002764 }
2765
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002766 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002767}
2768
Sebastian Redl539c5062010-08-18 23:57:32 +00002769DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002770 if (D == 0)
2771 return 0;
2772
2773 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2774 return DeclIDs[D];
2775}
2776
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002777void ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002778 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002779 Record.push_back(Name.getNameKind());
2780 switch (Name.getNameKind()) {
2781 case DeclarationName::Identifier:
2782 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2783 break;
2784
2785 case DeclarationName::ObjCZeroArgSelector:
2786 case DeclarationName::ObjCOneArgSelector:
2787 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002788 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002789 break;
2790
2791 case DeclarationName::CXXConstructorName:
2792 case DeclarationName::CXXDestructorName:
2793 case DeclarationName::CXXConversionFunctionName:
2794 AddTypeRef(Name.getCXXNameType(), Record);
2795 break;
2796
2797 case DeclarationName::CXXOperatorName:
2798 Record.push_back(Name.getCXXOverloadedOperator());
2799 break;
2800
Alexis Hunt3d221f22009-11-29 07:34:05 +00002801 case DeclarationName::CXXLiteralOperatorName:
2802 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2803 break;
2804
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002805 case DeclarationName::CXXUsingDirective:
2806 // No extra data to emit
2807 break;
2808 }
2809}
Chris Lattnerca025db2010-05-07 21:43:38 +00002810
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002811void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Chris Lattnerca025db2010-05-07 21:43:38 +00002812 RecordData &Record) {
2813 // Nested name specifiers usually aren't too long. I think that 8 would
2814 // typically accomodate the vast majority.
2815 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2816
2817 // Push each of the NNS's onto a stack for serialization in reverse order.
2818 while (NNS) {
2819 NestedNames.push_back(NNS);
2820 NNS = NNS->getPrefix();
2821 }
2822
2823 Record.push_back(NestedNames.size());
2824 while(!NestedNames.empty()) {
2825 NNS = NestedNames.pop_back_val();
2826 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2827 Record.push_back(Kind);
2828 switch (Kind) {
2829 case NestedNameSpecifier::Identifier:
2830 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2831 break;
2832
2833 case NestedNameSpecifier::Namespace:
2834 AddDeclRef(NNS->getAsNamespace(), Record);
2835 break;
2836
2837 case NestedNameSpecifier::TypeSpec:
2838 case NestedNameSpecifier::TypeSpecWithTemplate:
2839 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2840 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2841 break;
2842
2843 case NestedNameSpecifier::Global:
2844 // Don't need to write an associated value.
2845 break;
2846 }
2847 }
2848}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002849
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002850void ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002851 TemplateName::NameKind Kind = Name.getKind();
2852 Record.push_back(Kind);
2853 switch (Kind) {
2854 case TemplateName::Template:
2855 AddDeclRef(Name.getAsTemplateDecl(), Record);
2856 break;
2857
2858 case TemplateName::OverloadedTemplate: {
2859 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2860 Record.push_back(OvT->size());
2861 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2862 I != E; ++I)
2863 AddDeclRef(*I, Record);
2864 break;
2865 }
2866
2867 case TemplateName::QualifiedTemplate: {
2868 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2869 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2870 Record.push_back(QualT->hasTemplateKeyword());
2871 AddDeclRef(QualT->getTemplateDecl(), Record);
2872 break;
2873 }
2874
2875 case TemplateName::DependentTemplate: {
2876 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2877 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2878 Record.push_back(DepT->isIdentifier());
2879 if (DepT->isIdentifier())
2880 AddIdentifierRef(DepT->getIdentifier(), Record);
2881 else
2882 Record.push_back(DepT->getOperator());
2883 break;
2884 }
2885 }
2886}
2887
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002888void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002889 RecordData &Record) {
2890 Record.push_back(Arg.getKind());
2891 switch (Arg.getKind()) {
2892 case TemplateArgument::Null:
2893 break;
2894 case TemplateArgument::Type:
2895 AddTypeRef(Arg.getAsType(), Record);
2896 break;
2897 case TemplateArgument::Declaration:
2898 AddDeclRef(Arg.getAsDecl(), Record);
2899 break;
2900 case TemplateArgument::Integral:
2901 AddAPSInt(*Arg.getAsIntegral(), Record);
2902 AddTypeRef(Arg.getIntegralType(), Record);
2903 break;
2904 case TemplateArgument::Template:
2905 AddTemplateName(Arg.getAsTemplate(), Record);
2906 break;
2907 case TemplateArgument::Expression:
2908 AddStmt(Arg.getAsExpr());
2909 break;
2910 case TemplateArgument::Pack:
2911 Record.push_back(Arg.pack_size());
2912 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2913 I != E; ++I)
2914 AddTemplateArgument(*I, Record);
2915 break;
2916 }
2917}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002918
2919void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002920ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002921 RecordData &Record) {
2922 assert(TemplateParams && "No TemplateParams!");
2923 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2924 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2925 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2926 Record.push_back(TemplateParams->size());
2927 for (TemplateParameterList::const_iterator
2928 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2929 P != PEnd; ++P)
2930 AddDeclRef(*P, Record);
2931}
2932
2933/// \brief Emit a template argument list.
2934void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002935ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002936 RecordData &Record) {
2937 assert(TemplateArgs && "No TemplateArgs!");
2938 Record.push_back(TemplateArgs->flat_size());
2939 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2940 AddTemplateArgument(TemplateArgs->get(i), Record);
2941}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00002942
2943
2944void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002945ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00002946 Record.push_back(Set.size());
2947 for (UnresolvedSetImpl::const_iterator
2948 I = Set.begin(), E = Set.end(); I != E; ++I) {
2949 AddDeclRef(I.getDecl(), Record);
2950 Record.push_back(I.getAccess());
2951 }
2952}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00002953
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002954void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00002955 RecordData &Record) {
2956 Record.push_back(Base.isVirtual());
2957 Record.push_back(Base.isBaseOfClass());
2958 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00002959 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00002960 AddSourceRange(Base.getSourceRange(), Record);
2961}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002962
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002963void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00002964 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
2965 unsigned NumBaseOrMembers, RecordData &Record) {
2966 Record.push_back(NumBaseOrMembers);
2967 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
2968 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
2969
2970 Record.push_back(Init->isBaseInitializer());
2971 if (Init->isBaseInitializer()) {
2972 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
2973 Record.push_back(Init->isBaseVirtual());
2974 } else {
2975 AddDeclRef(Init->getMember(), Record);
2976 }
2977 AddSourceLocation(Init->getMemberLocation(), Record);
2978 AddStmt(Init->getInit());
2979 AddDeclRef(Init->getAnonUnionMember(), Record);
2980 AddSourceLocation(Init->getLParenLoc(), Record);
2981 AddSourceLocation(Init->getRParenLoc(), Record);
2982 Record.push_back(Init->isWritten());
2983 if (Init->isWritten()) {
2984 Record.push_back(Init->getSourceOrder());
2985 } else {
2986 Record.push_back(Init->getNumArrayIndices());
2987 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
2988 AddDeclRef(Init->getArrayIndex(i), Record);
2989 }
2990 }
2991}
2992
Sebastian Redl2c499f62010-08-18 23:56:43 +00002993void ASTWriter::SetReader(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00002994 assert(Reader && "Cannot remove chain");
2995 assert(FirstDeclID == NextDeclID &&
2996 FirstTypeID == NextTypeID &&
2997 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00002998 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00002999 "Setting chain after writing has started.");
3000 Chain = Reader;
3001}
3002
Sebastian Redl539c5062010-08-18 23:57:32 +00003003void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003004 IdentifierIDs[II] = ID;
3005}
3006
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003007void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003008 TypeIdxs[T] = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003009}
3010
Sebastian Redl539c5062010-08-18 23:57:32 +00003011void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003012 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003013}
Sebastian Redl834bb972010-08-04 17:20:04 +00003014
Sebastian Redl539c5062010-08-18 23:57:32 +00003015void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003016 SelectorIDs[S] = ID;
3017}