blob: 468729a371b870a0136d263d153fd3b9e633fe5e [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"
John McCall19c1bfd2010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000039#include "llvm/ADT/APFloat.h"
40#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000041#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000043#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000044#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000045#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000047using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048
Sebastian Redl3df5a082010-07-30 17:03:48 +000049template <typename T, typename Allocator>
50T *data(std::vector<T, Allocator> &v) {
51 return v.empty() ? 0 : &v.front();
52}
53template <typename T, typename Allocator>
54const T *data(const std::vector<T, Allocator> &v) {
55 return v.empty() ? 0 : &v.front();
56}
57
Douglas Gregoref84c4b2009-04-09 22:27:44 +000058//===----------------------------------------------------------------------===//
59// Type serialization
60//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000061
Douglas Gregoref84c4b2009-04-09 22:27:44 +000062namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000063 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000064 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000065 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000066
67 public:
68 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000069 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000070
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000071 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000072 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073
74 void VisitArrayType(const ArrayType *T);
75 void VisitFunctionType(const FunctionType *T);
76 void VisitTagType(const TagType *T);
77
78#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
79#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000080#include "clang/AST/TypeNodes.def"
81 };
82}
83
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000084void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000085 assert(false && "Built-in types are never serialized");
86}
87
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000088void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000090 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000091}
92
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000093void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000095 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096}
97
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000098void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000099 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000100 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101}
102
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000105 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106}
107
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000110 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111}
112
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000114 Writer.AddTypeRef(T->getPointeeType(), Record);
115 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000116 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000117}
118
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120 Writer.AddTypeRef(T->getElementType(), Record);
121 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000122 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000123}
124
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000125void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126 VisitArrayType(T);
127 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000128 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000129}
130
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000131void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000133 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134}
135
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000138 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
139 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000140 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145 Writer.AddTypeRef(T->getElementType(), Record);
146 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000147 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000148 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000149}
150
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000151void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000153 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154}
155
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000158 FunctionType::ExtInfo C = T->getExtInfo();
159 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000160 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000161 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000162 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163}
164
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000167 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168}
169
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000171 VisitFunctionType(T);
172 Record.push_back(T->getNumArgs());
173 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
174 Writer.AddTypeRef(T->getArgType(I), Record);
175 Record.push_back(T->isVariadic());
176 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000177 Record.push_back(T->hasExceptionSpec());
178 Record.push_back(T->hasAnyExceptionSpec());
179 Record.push_back(T->getNumExceptions());
180 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
181 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000182 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183}
184
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000185void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000186 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000187 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000188}
John McCallb96ec562009-12-04 22:46:56 +0000189
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000190void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000191 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000192 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
193 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000194 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195}
196
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000197void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000198 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000199 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000200}
201
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000203 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000204 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205}
206
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000208 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000209 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000210}
211
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000213 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000215 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 "Cannot serialize in the middle of a type definition");
217}
218
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000221 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222}
223
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000226 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227}
228
Mike Stump11289f42009-09-09 15:08:12 +0000229void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000230ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000231 const SubstTemplateTypeParmType *T) {
232 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
233 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000234 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000235}
236
237void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000238ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000240 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000241 Writer.AddTemplateName(T->getTemplateName(), Record);
242 Record.push_back(T->getNumArgs());
243 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
244 ArgI != ArgE; ++ArgI)
245 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000246 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
247 : T->getCanonicalTypeInternal(),
248 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000249 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000250}
251
252void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000253ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000254 VisitArrayType(T);
255 Writer.AddStmt(T->getSizeExpr());
256 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000257 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000258}
259
260void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000261ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000262 const DependentSizedExtVectorType *T) {
263 // FIXME: Serialize this type (C++ only)
264 assert(false && "Cannot serialize dependent sized extended vector types");
265}
266
267void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000268ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000269 Record.push_back(T->getDepth());
270 Record.push_back(T->getIndex());
271 Record.push_back(T->isParameterPack());
272 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000273 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000274}
275
276void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000277ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000278 Record.push_back(T->getKeyword());
279 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
280 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000281 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
282 : T->getCanonicalTypeInternal(),
283 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000284 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000285}
286
287void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000288ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000289 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000290 Record.push_back(T->getKeyword());
291 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
292 Writer.AddIdentifierRef(T->getIdentifier(), Record);
293 Record.push_back(T->getNumArgs());
294 for (DependentTemplateSpecializationType::iterator
295 I = T->begin(), E = T->end(); I != E; ++I)
296 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000297 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000298}
299
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000300void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000301 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000302 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
303 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000304 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000305}
306
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000307void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000308 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000309 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000310 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000311}
312
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000313void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000314 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000315 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000316}
317
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000318void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000319 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000320 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000321 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000322 E = T->qual_end(); I != E; ++I)
323 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000324 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000325}
326
Steve Narofffb4330f2009-06-17 22:40:22 +0000327void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000328ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000329 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000330 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000331}
332
John McCall8f115c62009-10-16 21:56:05 +0000333namespace {
334
335class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000336 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000337 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000338
339public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000340 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000341 : Writer(Writer), Record(Record) { }
342
John McCall17001972009-10-18 01:05:36 +0000343#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000344#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000345 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000346#include "clang/AST/TypeLocNodes.def"
347
John McCall17001972009-10-18 01:05:36 +0000348 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
349 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000350};
351
352}
353
John McCall17001972009-10-18 01:05:36 +0000354void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
355 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000356}
John McCall17001972009-10-18 01:05:36 +0000357void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000358 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
359 if (TL.needsExtraLocalData()) {
360 Record.push_back(TL.getWrittenTypeSpec());
361 Record.push_back(TL.getWrittenSignSpec());
362 Record.push_back(TL.getWrittenWidthSpec());
363 Record.push_back(TL.hasModeAttr());
364 }
John McCall8f115c62009-10-16 21:56:05 +0000365}
John McCall17001972009-10-18 01:05:36 +0000366void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000368}
John McCall17001972009-10-18 01:05:36 +0000369void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
370 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000371}
John McCall17001972009-10-18 01:05:36 +0000372void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
373 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000374}
John McCall17001972009-10-18 01:05:36 +0000375void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
376 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000377}
John McCall17001972009-10-18 01:05:36 +0000378void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000380}
John McCall17001972009-10-18 01:05:36 +0000381void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000383}
John McCall17001972009-10-18 01:05:36 +0000384void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
385 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
386 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
387 Record.push_back(TL.getSizeExpr() ? 1 : 0);
388 if (TL.getSizeExpr())
389 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000390}
John McCall17001972009-10-18 01:05:36 +0000391void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
392 VisitArrayTypeLoc(TL);
393}
394void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
395 VisitArrayTypeLoc(TL);
396}
397void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
398 VisitArrayTypeLoc(TL);
399}
400void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
401 DependentSizedArrayTypeLoc TL) {
402 VisitArrayTypeLoc(TL);
403}
404void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
405 DependentSizedExtVectorTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getNameLoc(), Record);
407}
408void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getNameLoc(), Record);
410}
411void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getNameLoc(), Record);
413}
414void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
416 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000417 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000418 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
419 Writer.AddDeclRef(TL.getArg(i), Record);
420}
421void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
422 VisitFunctionTypeLoc(TL);
423}
424void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
425 VisitFunctionTypeLoc(TL);
426}
John McCallb96ec562009-12-04 22:46:56 +0000427void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
428 Writer.AddSourceLocation(TL.getNameLoc(), Record);
429}
John McCall17001972009-10-18 01:05:36 +0000430void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
432}
433void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc 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);
John McCall17001972009-10-18 01:05:36 +0000437}
438void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000439 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
440 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
441 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
442 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000443}
444void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
446}
447void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getNameLoc(), Record);
449}
450void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
John McCall17001972009-10-18 01:05:36 +0000453void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getNameLoc(), Record);
455}
John McCallcebee162009-10-18 09:09:24 +0000456void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
457 SubstTemplateTypeParmTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getNameLoc(), Record);
459}
John McCall17001972009-10-18 01:05:36 +0000460void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
461 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000462 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
463 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
464 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
465 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000466 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
467 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000468}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000469void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000470 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
471 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000472}
John McCalle78aac42010-03-10 03:28:59 +0000473void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
475}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000476void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000477 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
478 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
John McCallc392f372010-06-11 00:33:02 +0000481void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
482 DependentTemplateSpecializationTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
484 Writer.AddSourceRange(TL.getQualifierRange(), Record);
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
487 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
488 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000489 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
490 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000491}
John McCall17001972009-10-18 01:05:36 +0000492void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000494}
495void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
496 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000497 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
498 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
499 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
500 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000501}
John McCallfc93cf92009-10-22 22:37:11 +0000502void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
503 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000504}
John McCall8f115c62009-10-16 21:56:05 +0000505
Chris Lattner19cea4e2009-04-22 05:57:30 +0000506//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000507// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000508//===----------------------------------------------------------------------===//
509
Chris Lattner28fa4e62009-04-26 22:26:21 +0000510static void EmitBlockID(unsigned ID, const char *Name,
511 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000512 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000513 Record.clear();
514 Record.push_back(ID);
515 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
516
517 // Emit the block name if present.
518 if (Name == 0 || Name[0] == 0) return;
519 Record.clear();
520 while (*Name)
521 Record.push_back(*Name++);
522 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
523}
524
525static void EmitRecordID(unsigned ID, const char *Name,
526 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000527 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000528 Record.clear();
529 Record.push_back(ID);
530 while (*Name)
531 Record.push_back(*Name++);
532 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000533}
534
535static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000536 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000537#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000538 RECORD(STMT_STOP);
539 RECORD(STMT_NULL_PTR);
540 RECORD(STMT_NULL);
541 RECORD(STMT_COMPOUND);
542 RECORD(STMT_CASE);
543 RECORD(STMT_DEFAULT);
544 RECORD(STMT_LABEL);
545 RECORD(STMT_IF);
546 RECORD(STMT_SWITCH);
547 RECORD(STMT_WHILE);
548 RECORD(STMT_DO);
549 RECORD(STMT_FOR);
550 RECORD(STMT_GOTO);
551 RECORD(STMT_INDIRECT_GOTO);
552 RECORD(STMT_CONTINUE);
553 RECORD(STMT_BREAK);
554 RECORD(STMT_RETURN);
555 RECORD(STMT_DECL);
556 RECORD(STMT_ASM);
557 RECORD(EXPR_PREDEFINED);
558 RECORD(EXPR_DECL_REF);
559 RECORD(EXPR_INTEGER_LITERAL);
560 RECORD(EXPR_FLOATING_LITERAL);
561 RECORD(EXPR_IMAGINARY_LITERAL);
562 RECORD(EXPR_STRING_LITERAL);
563 RECORD(EXPR_CHARACTER_LITERAL);
564 RECORD(EXPR_PAREN);
565 RECORD(EXPR_UNARY_OPERATOR);
566 RECORD(EXPR_SIZEOF_ALIGN_OF);
567 RECORD(EXPR_ARRAY_SUBSCRIPT);
568 RECORD(EXPR_CALL);
569 RECORD(EXPR_MEMBER);
570 RECORD(EXPR_BINARY_OPERATOR);
571 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
572 RECORD(EXPR_CONDITIONAL_OPERATOR);
573 RECORD(EXPR_IMPLICIT_CAST);
574 RECORD(EXPR_CSTYLE_CAST);
575 RECORD(EXPR_COMPOUND_LITERAL);
576 RECORD(EXPR_EXT_VECTOR_ELEMENT);
577 RECORD(EXPR_INIT_LIST);
578 RECORD(EXPR_DESIGNATED_INIT);
579 RECORD(EXPR_IMPLICIT_VALUE_INIT);
580 RECORD(EXPR_VA_ARG);
581 RECORD(EXPR_ADDR_LABEL);
582 RECORD(EXPR_STMT);
583 RECORD(EXPR_TYPES_COMPATIBLE);
584 RECORD(EXPR_CHOOSE);
585 RECORD(EXPR_GNU_NULL);
586 RECORD(EXPR_SHUFFLE_VECTOR);
587 RECORD(EXPR_BLOCK);
588 RECORD(EXPR_BLOCK_DECL_REF);
589 RECORD(EXPR_OBJC_STRING_LITERAL);
590 RECORD(EXPR_OBJC_ENCODE);
591 RECORD(EXPR_OBJC_SELECTOR_EXPR);
592 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
593 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
594 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
595 RECORD(EXPR_OBJC_KVC_REF_EXPR);
596 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000597 RECORD(STMT_OBJC_FOR_COLLECTION);
598 RECORD(STMT_OBJC_CATCH);
599 RECORD(STMT_OBJC_FINALLY);
600 RECORD(STMT_OBJC_AT_TRY);
601 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
602 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000603 RECORD(EXPR_CXX_OPERATOR_CALL);
604 RECORD(EXPR_CXX_CONSTRUCT);
605 RECORD(EXPR_CXX_STATIC_CAST);
606 RECORD(EXPR_CXX_DYNAMIC_CAST);
607 RECORD(EXPR_CXX_REINTERPRET_CAST);
608 RECORD(EXPR_CXX_CONST_CAST);
609 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
610 RECORD(EXPR_CXX_BOOL_LITERAL);
611 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000612#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000613}
Mike Stump11289f42009-09-09 15:08:12 +0000614
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000615void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000616 RecordData Record;
617 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000618
Sebastian Redl539c5062010-08-18 23:57:32 +0000619#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
620#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000621
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000622 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000623 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000624 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000625 RECORD(TYPE_OFFSET);
626 RECORD(DECL_OFFSET);
627 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000628 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000629 RECORD(IDENTIFIER_OFFSET);
630 RECORD(IDENTIFIER_TABLE);
631 RECORD(EXTERNAL_DEFINITIONS);
632 RECORD(SPECIAL_TYPES);
633 RECORD(STATISTICS);
634 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000635 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000636 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
637 RECORD(SELECTOR_OFFSETS);
638 RECORD(METHOD_POOL);
639 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000640 RECORD(SOURCE_LOCATION_OFFSETS);
641 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000642 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000643 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000644 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000645 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000646 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000647 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000648
Chris Lattner28fa4e62009-04-26 22:26:21 +0000649 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000650 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000651 RECORD(SM_SLOC_FILE_ENTRY);
652 RECORD(SM_SLOC_BUFFER_ENTRY);
653 RECORD(SM_SLOC_BUFFER_BLOB);
654 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
655 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000656
Chris Lattner28fa4e62009-04-26 22:26:21 +0000657 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000658 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000659 RECORD(PP_MACRO_OBJECT_LIKE);
660 RECORD(PP_MACRO_FUNCTION_LIKE);
661 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000662 RECORD(PP_MACRO_INSTANTIATION);
663 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000664
Douglas Gregor12bfa382009-10-17 00:13:19 +0000665 // Decls and Types block.
666 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000668 RECORD(TYPE_COMPLEX);
669 RECORD(TYPE_POINTER);
670 RECORD(TYPE_BLOCK_POINTER);
671 RECORD(TYPE_LVALUE_REFERENCE);
672 RECORD(TYPE_RVALUE_REFERENCE);
673 RECORD(TYPE_MEMBER_POINTER);
674 RECORD(TYPE_CONSTANT_ARRAY);
675 RECORD(TYPE_INCOMPLETE_ARRAY);
676 RECORD(TYPE_VARIABLE_ARRAY);
677 RECORD(TYPE_VECTOR);
678 RECORD(TYPE_EXT_VECTOR);
679 RECORD(TYPE_FUNCTION_PROTO);
680 RECORD(TYPE_FUNCTION_NO_PROTO);
681 RECORD(TYPE_TYPEDEF);
682 RECORD(TYPE_TYPEOF_EXPR);
683 RECORD(TYPE_TYPEOF);
684 RECORD(TYPE_RECORD);
685 RECORD(TYPE_ENUM);
686 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000687 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000688 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000689 RECORD(DECL_TRANSLATION_UNIT);
690 RECORD(DECL_TYPEDEF);
691 RECORD(DECL_ENUM);
692 RECORD(DECL_RECORD);
693 RECORD(DECL_ENUM_CONSTANT);
694 RECORD(DECL_FUNCTION);
695 RECORD(DECL_OBJC_METHOD);
696 RECORD(DECL_OBJC_INTERFACE);
697 RECORD(DECL_OBJC_PROTOCOL);
698 RECORD(DECL_OBJC_IVAR);
699 RECORD(DECL_OBJC_AT_DEFS_FIELD);
700 RECORD(DECL_OBJC_CLASS);
701 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
702 RECORD(DECL_OBJC_CATEGORY);
703 RECORD(DECL_OBJC_CATEGORY_IMPL);
704 RECORD(DECL_OBJC_IMPLEMENTATION);
705 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
706 RECORD(DECL_OBJC_PROPERTY);
707 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000708 RECORD(DECL_FIELD);
709 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000710 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000711 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000712 RECORD(DECL_FILE_SCOPE_ASM);
713 RECORD(DECL_BLOCK);
714 RECORD(DECL_CONTEXT_LEXICAL);
715 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000716 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000717 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000718#undef RECORD
719#undef BLOCK
720 Stream.ExitBlock();
721}
722
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000723/// \brief Adjusts the given filename to only write out the portion of the
724/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000725///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000726/// \param Filename the file name to adjust.
727///
728/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
729/// the returned filename will be adjusted by this system root.
730///
731/// \returns either the original filename (if it needs no adjustment) or the
732/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000733static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000734adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
735 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000736
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000737 if (!isysroot)
738 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000739
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000740 // Verify that the filename and the system root have the same prefix.
741 unsigned Pos = 0;
742 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
743 if (Filename[Pos] != isysroot[Pos])
744 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000745
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000746 // We hit the end of the filename before we hit the end of the system root.
747 if (!Filename[Pos])
748 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000749
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000750 // If the file name has a '/' at the current position, skip over the '/'.
751 // We distinguish sysroot-based includes from absolute includes by the
752 // absence of '/' at the beginning of sysroot-based includes.
753 if (Filename[Pos] == '/')
754 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000755
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000756 return Filename + Pos;
757}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000758
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000759/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000760void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000761 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000762
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000763 // Metadata
764 const TargetInfo &Target = Context.Target;
765 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000766 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000767 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
771 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
772 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000773 // Target triple or chained PCH name
774 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000775 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000776
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000777 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000778 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
779 Record.push_back(VERSION_MAJOR);
780 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000781 Record.push_back(CLANG_VERSION_MAJOR);
782 Record.push_back(CLANG_VERSION_MINOR);
783 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000784 // FIXME: This writes the absolute path for chained headers.
785 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
786 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000787
Douglas Gregor45fe0362009-05-12 01:31:05 +0000788 // Original file name
789 SourceManager &SM = Context.getSourceManager();
790 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
791 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000792 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000793 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
794 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
795
796 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000797
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000798 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000799
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000800 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000801 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000802 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000803 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000804 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000805 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000806 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000807
Ted Kremenek18e066f2010-01-22 22:12:47 +0000808 // Repository branch/version information.
809 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000810 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000811 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
812 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000813 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000814 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000815 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
816 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000817}
818
819/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000820void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000821 RecordData Record;
822 Record.push_back(LangOpts.Trigraphs);
823 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
824 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
825 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
826 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000827 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000828 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
829 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
830 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
831 Record.push_back(LangOpts.C99); // C99 Support
832 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000833 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
834 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000835 Record.push_back(LangOpts.CPlusPlus); // C++ Support
836 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000837 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000838
Douglas Gregor55abb232009-04-10 20:39:37 +0000839 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
840 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000841 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000842 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000843 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000844 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000845 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000846
Douglas Gregor55abb232009-04-10 20:39:37 +0000847 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000848 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
849 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000850 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000851 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000852 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000853
854 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
855 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
856 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
857
Chris Lattner258172e2009-04-27 07:35:58 +0000858 // Whether static initializers are protected by locks.
859 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000860 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000861 Record.push_back(LangOpts.Blocks); // block extension to C
862 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
863 // they are unused.
864 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
865 // (modulo the platform support).
866
Chris Lattner51924e512010-06-26 21:25:03 +0000867 Record.push_back(LangOpts.getSignedOverflowBehavior());
868 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000869
870 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000871 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000872 // defined.
873 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
874 // opposed to __DYNAMIC__).
875 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
876
877 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
878 // used (instead of C99 semantics).
879 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000880 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
881 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000882 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
883 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000884 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000885 Record.push_back(LangOpts.getGCMode());
886 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000887 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000888 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000889 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000890 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000891 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000892 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000893 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000894}
895
Douglas Gregora7f71a92009-04-10 03:52:48 +0000896//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000897// stat cache Serialization
898//===----------------------------------------------------------------------===//
899
900namespace {
901// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000902class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000903public:
904 typedef const char * key_type;
905 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000906
Chris Lattner2a6fa472010-11-23 19:28:12 +0000907 typedef struct stat data_type;
908 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000909
910 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000911 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
914 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000915 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
916 data_type_ref Data) {
917 unsigned StrLen = strlen(path);
918 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000919 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000920 clang::io::Emit8(Out, DataLen);
921 return std::make_pair(StrLen + 1, DataLen);
922 }
Mike Stump11289f42009-09-09 15:08:12 +0000923
Douglas Gregorc5046832009-04-27 18:38:38 +0000924 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
925 Out.write(path, KeyLen);
926 }
Mike Stump11289f42009-09-09 15:08:12 +0000927
Chris Lattner2a6fa472010-11-23 19:28:12 +0000928 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000929 data_type_ref Data, unsigned DataLen) {
930 using namespace clang::io;
931 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner2a6fa472010-11-23 19:28:12 +0000933 Emit32(Out, (uint32_t) Data.st_ino);
934 Emit32(Out, (uint32_t) Data.st_dev);
935 Emit16(Out, (uint16_t) Data.st_mode);
936 Emit64(Out, (uint64_t) Data.st_mtime);
937 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +0000938
939 assert(Out.tell() - Start == DataLen && "Wrong data length");
940 }
941};
942} // end anonymous namespace
943
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000944/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000945void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000946 // Build the on-disk hash table containing information about every
947 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000948 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000949 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000950 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000951 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000952 Stat != StatEnd; ++Stat, ++NumStatEntries) {
953 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000954 Generator.insert(Filename, Stat->second);
955 }
Mike Stump11289f42009-09-09 15:08:12 +0000956
Douglas Gregorc5046832009-04-27 18:38:38 +0000957 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000958 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000959 uint32_t BucketOffset;
960 {
961 llvm::raw_svector_ostream Out(StatCacheData);
962 // Make sure that no bucket is at offset 0
963 clang::io::Emit32(Out, 0);
964 BucketOffset = Generator.Emit(Out);
965 }
966
967 // Create a blob abbreviation
968 using namespace llvm;
969 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000970 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
974 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
975
976 // Write the stat cache
977 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000978 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +0000979 Record.push_back(BucketOffset);
980 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000981 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000982}
983
984//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000985// Source Manager Serialization
986//===----------------------------------------------------------------------===//
987
988/// \brief Create an abbreviation for the SLocEntry that refers to a
989/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000990static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000991 using namespace llvm;
992 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000993 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +0000994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000998 // FileEntry fields.
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001001 // HeaderFileInfo fields.
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001007 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001008}
1009
1010/// \brief Create an abbreviation for the SLocEntry that refers to a
1011/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001012static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001013 using namespace llvm;
1014 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001015 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001021 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001022}
1023
1024/// \brief Create an abbreviation for the SLocEntry that refers to a
1025/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001026static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001027 using namespace llvm;
1028 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001029 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001031 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001032}
1033
1034/// \brief Create an abbreviation for the SLocEntry that refers to an
1035/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001036static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001037 using namespace llvm;
1038 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001039 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001045 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001046}
1047
1048/// \brief Writes the block containing the serialized form of the
1049/// source manager.
1050///
1051/// TODO: We should probably use an on-disk hash table (stored in a
1052/// blob), indexed based on the file name, so that we only create
1053/// entries for files that we actually need. In the common case (no
1054/// errors), we probably won't have to create file entries for any of
1055/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001056void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001057 const Preprocessor &PP,
1058 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001059 RecordData Record;
1060
Chris Lattner0910e3b2009-04-10 17:16:57 +00001061 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001062 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001063
1064 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001065 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1066 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1067 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1068 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001069
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001070 // Write the line table.
1071 if (SourceMgr.hasLineTable()) {
1072 LineTableInfo &LineTable = SourceMgr.getLineTable();
1073
1074 // Emit the file names
1075 Record.push_back(LineTable.getNumFilenames());
1076 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1077 // Emit the file name
1078 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001079 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001080 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1081 Record.push_back(FilenameLen);
1082 if (FilenameLen)
1083 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1084 }
Mike Stump11289f42009-09-09 15:08:12 +00001085
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001086 // Emit the line entries
1087 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1088 L != LEnd; ++L) {
1089 // Emit the file ID
1090 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001091
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001092 // Emit the line entries
1093 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001094 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001095 LEEnd = L->second.end();
1096 LE != LEEnd; ++LE) {
1097 Record.push_back(LE->FileOffset);
1098 Record.push_back(LE->LineNo);
1099 Record.push_back(LE->FilenameID);
1100 Record.push_back((unsigned)LE->FileKind);
1101 Record.push_back(LE->IncludeOffset);
1102 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001103 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001104 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001105 }
1106
Douglas Gregor258ae542009-04-27 06:38:32 +00001107 // Write out the source location entry table. We skip the first
1108 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001109 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001110 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001111 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1112 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1113 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1114 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001115 // Get this source location entry.
1116 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001117
Douglas Gregor258ae542009-04-27 06:38:32 +00001118 // Record the offset of this source-location entry.
1119 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1120
1121 // Figure out which record code to use.
1122 unsigned Code;
1123 if (SLoc->isFile()) {
1124 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001125 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001126 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001127 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001128 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001129 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001130 Record.clear();
1131 Record.push_back(Code);
1132
1133 Record.push_back(SLoc->getOffset());
1134 if (SLoc->isFile()) {
1135 const SrcMgr::FileInfo &File = SLoc->getFile();
1136 Record.push_back(File.getIncludeLoc().getRawEncoding());
1137 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1138 Record.push_back(File.hasLineDirectives());
1139
1140 const SrcMgr::ContentCache *Content = File.getContentCache();
1141 if (Content->Entry) {
1142 // The source location entry is a file. The blob associated
1143 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001144
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001145 // Emit size/modification time for this file.
1146 Record.push_back(Content->Entry->getSize());
1147 Record.push_back(Content->Entry->getModificationTime());
1148
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001149 // Emit header-search information associated with this file.
1150 HeaderFileInfo HFI;
1151 HeaderSearch &HS = PP.getHeaderSearchInfo();
1152 if (Content->Entry->getUID() < HS.header_file_size())
1153 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1154 Record.push_back(HFI.isImport);
1155 Record.push_back(HFI.DirInfo);
1156 Record.push_back(HFI.NumIncludes);
1157 AddIdentifierRef(HFI.ControllingMacro, Record);
1158
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001159 // Turn the file name into an absolute path, if it isn't already.
1160 const char *Filename = Content->Entry->getName();
1161 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001162 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001163 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001164
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001165 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001166 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001167
1168 // FIXME: For now, preload all file source locations, so that
1169 // we get the appropriate File entries in the reader. This is
1170 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001171 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001172 } else {
1173 // The source location entry is a buffer. The blob associated
1174 // with this entry contains the contents of the buffer.
1175
1176 // We add one to the size so that we capture the trailing NULL
1177 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1178 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001179 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001180 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001181 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001182 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1183 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001184 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001185 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001186 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001187 llvm::StringRef(Buffer->getBufferStart(),
1188 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001189
1190 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001191 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001192 }
1193 } else {
1194 // The source location entry is an instantiation.
1195 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1196 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1197 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1198 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1199
1200 // Compute the token length for this macro expansion.
1201 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001202 if (I + 1 != N)
1203 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001204 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1205 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1206 }
1207 }
1208
Douglas Gregor8f45df52009-04-16 22:23:12 +00001209 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001210
1211 if (SLocEntryOffsets.empty())
1212 return;
1213
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001214 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001215 // table is used for lazily loading source-location information.
1216 using namespace llvm;
1217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001218 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1222 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001223
Douglas Gregor258ae542009-04-27 06:38:32 +00001224 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001225 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001226 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001227 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1228 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001229 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001230 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001231 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001232
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001233 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001234 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001235 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001236}
1237
Douglas Gregorc5046832009-04-27 18:38:38 +00001238//===----------------------------------------------------------------------===//
1239// Preprocessor Serialization
1240//===----------------------------------------------------------------------===//
1241
Chris Lattnereeffaef2009-04-10 17:15:23 +00001242/// \brief Writes the block containing the serialized form of the
1243/// preprocessor.
1244///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001245void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001246 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001247
Chris Lattner0af3ba12009-04-13 01:29:17 +00001248 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1249 if (PP.getCounterValue() != 0) {
1250 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001251 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001252 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001253 }
1254
1255 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001256 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001257
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001258 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001259 // FIXME: use diagnostics subsystem for localization etc.
1260 if (PP.SawDateOrTime())
1261 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001262
Douglas Gregor796d76a2010-10-20 22:00:55 +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();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001267 unsigned InclusionAbbrev = 0;
1268 if (PPRec) {
1269 using namespace llvm;
1270 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1271 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001279 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001280 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001281
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001282 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1283 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001284 // FIXME: This emits macros in hash table order, we should do it in a stable
1285 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001286 MacroInfo *MI = I->second;
1287
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001288 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001289 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001290 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001291
1292 // FIXME: There is a (probably minor) optimization we could do here, if
1293 // the macro comes from the original PCH but the identifier comes from a
1294 // chained PCH, by storing the offset into the original PCH rather than
1295 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001296 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001297 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001298 continue;
1299
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001300 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001301 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001302 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1303 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001304
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001305 unsigned Code;
1306 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001307 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001308 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001309 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001310
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001311 Record.push_back(MI->isC99Varargs());
1312 Record.push_back(MI->isGNUVarargs());
1313 Record.push_back(MI->getNumArgs());
1314 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1315 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001316 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001317 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001318
Douglas Gregoraae92242010-03-19 21:51:54 +00001319 // If we have a detailed preprocessing record, record the macro definition
1320 // ID that corresponds to this macro.
1321 if (PPRec)
1322 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001323
Douglas Gregor8f45df52009-04-16 22:23:12 +00001324 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001325 Record.clear();
1326
Chris Lattner2199f5b2009-04-10 18:08:30 +00001327 // Emit the tokens array.
1328 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1329 // Note that we know that the preprocessor does not have any annotation
1330 // tokens in it because they are created by the parser, and thus can't be
1331 // in a macro definition.
1332 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001333
Chris Lattner2199f5b2009-04-10 18:08:30 +00001334 Record.push_back(Tok.getLocation().getRawEncoding());
1335 Record.push_back(Tok.getLength());
1336
Chris Lattner2199f5b2009-04-10 18:08:30 +00001337 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1338 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001339 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001340
Chris Lattner2199f5b2009-04-10 18:08:30 +00001341 // FIXME: Should translate token kind to a stable encoding.
1342 Record.push_back(Tok.getKind());
1343 // FIXME: Should translate token flags to a stable encoding.
1344 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001345
Sebastian Redl539c5062010-08-18 23:57:32 +00001346 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001347 Record.clear();
1348 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001349 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001350 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001351
Douglas Gregoraae92242010-03-19 21:51:54 +00001352 // If the preprocessor has a preprocessing record, emit it.
1353 unsigned NumPreprocessingRecords = 0;
1354 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001355 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001356 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1357 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001358 E != EEnd; ++E) {
1359 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001360
Douglas Gregoraae92242010-03-19 21:51:54 +00001361 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001362 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001363 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1364 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1365 AddIdentifierRef(MI->getName(), Record);
1366 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
Sebastian Redl539c5062010-08-18 23:57:32 +00001367 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001368 continue;
1369 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001370
Douglas Gregoraae92242010-03-19 21:51:54 +00001371 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1372 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001373 MacroID ID = getMacroDefinitionID(MD);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001374
Douglas Gregor91096292010-10-02 19:29:26 +00001375 // Don't write the macro definition if it is from another AST file.
1376 if (ID < FirstMacroID)
1377 continue;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001378
Douglas Gregor91096292010-10-02 19:29:26 +00001379 unsigned Position = ID - FirstMacroID;
1380 if (Position != MacroDefinitionOffsets.size()) {
1381 if (Position > MacroDefinitionOffsets.size())
1382 MacroDefinitionOffsets.resize(Position + 1);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001383
1384 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001385 } else
1386 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001387
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001388 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001389 Record.push_back(ID);
1390 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1391 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1392 AddIdentifierRef(MD->getName(), Record);
1393 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001394 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001395 continue;
1396 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001397
Douglas Gregor796d76a2010-10-20 22:00:55 +00001398 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1399 Record.push_back(PP_INCLUSION_DIRECTIVE);
1400 Record.push_back(IndexBase + NumPreprocessingRecords++);
1401 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1402 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1403 Record.push_back(ID->getFileName().size());
1404 Record.push_back(ID->wasInQuotes());
1405 Record.push_back(static_cast<unsigned>(ID->getKind()));
1406 llvm::SmallString<64> Buffer;
1407 Buffer += ID->getFileName();
1408 Buffer += ID->getFile()->getName();
1409 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1410 continue;
1411 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001412 }
1413 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001414
Douglas Gregor8f45df52009-04-16 22:23:12 +00001415 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001416
Douglas Gregoraae92242010-03-19 21:51:54 +00001417 // Write the offsets table for the preprocessing record.
1418 if (NumPreprocessingRecords > 0) {
1419 // Write the offsets table for identifier IDs.
1420 using namespace llvm;
1421 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001422 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1426 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001427
Douglas Gregoraae92242010-03-19 21:51:54 +00001428 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001429 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001430 Record.push_back(NumPreprocessingRecords);
1431 Record.push_back(MacroDefinitionOffsets.size());
1432 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001433 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001434 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1435 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001436}
1437
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001438void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1439 RecordData Record;
1440 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
1441 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i);
1442 if (Map & 0x8) { // user mapping.
1443 Record.push_back(i);
1444 Record.push_back(Map & 0x7);
1445 }
1446 }
1447
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001448 if (!Record.empty())
1449 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001450}
1451
Douglas Gregorc5046832009-04-27 18:38:38 +00001452//===----------------------------------------------------------------------===//
1453// Type Serialization
1454//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001455
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001456/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001457void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001458 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001459 if (Idx.getIndex() == 0) // we haven't seen this type before.
1460 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001461
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001462 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001463
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001464 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001465 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001466 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001467 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001468 else if (TypeOffsets.size() < Index) {
1469 TypeOffsets.resize(Index + 1);
1470 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001471 }
1472
1473 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001474
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001475 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001476 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001477
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001478 if (T.hasLocalNonFastQualifiers()) {
1479 Qualifiers Qs = T.getLocalQualifiers();
1480 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001481 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001482 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001483 } else {
1484 switch (T->getTypeClass()) {
1485 // For all of the concrete, non-dependent types, call the
1486 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001487#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001488 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001489#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001490#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001491 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001492 }
1493
1494 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001495 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001496
1497 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001498 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001499}
1500
Douglas Gregorc5046832009-04-27 18:38:38 +00001501//===----------------------------------------------------------------------===//
1502// Declaration Serialization
1503//===----------------------------------------------------------------------===//
1504
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001505/// \brief Write the block containing all of the declaration IDs
1506/// lexically declared within the given DeclContext.
1507///
1508/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1509/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001510uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001511 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001512 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001513 return 0;
1514
Douglas Gregor8f45df52009-04-16 22:23:12 +00001515 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001516 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001517 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001518 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001519 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1520 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001521 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001522
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001523 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001524 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001525 reinterpret_cast<char*>(Decls.data()),
1526 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001527 return Offset;
1528}
1529
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001530void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001531 using namespace llvm;
1532 RecordData Record;
1533
1534 // Write the type offsets array
1535 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001536 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1539 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1540 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001541 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001542 Record.push_back(TypeOffsets.size());
1543 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001544 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001545 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1546
1547 // Write the declaration offsets array
1548 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001549 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1552 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1553 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001554 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001555 Record.push_back(DeclOffsets.size());
1556 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001557 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001558 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1559}
1560
Douglas Gregorc5046832009-04-27 18:38:38 +00001561//===----------------------------------------------------------------------===//
1562// Global Method Pool and Selector Serialization
1563//===----------------------------------------------------------------------===//
1564
Douglas Gregore84a9da2009-04-20 20:36:09 +00001565namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001566// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001567class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001568 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001569
1570public:
1571 typedef Selector key_type;
1572 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001573
Sebastian Redl834bb972010-08-04 17:20:04 +00001574 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001575 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001576 ObjCMethodList Instance, Factory;
1577 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001578 typedef const data_type& data_type_ref;
1579
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001580 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001581
Douglas Gregorc78d3462009-04-24 21:10:55 +00001582 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001583 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001584 }
Mike Stump11289f42009-09-09 15:08:12 +00001585
1586 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001587 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1588 data_type_ref Methods) {
1589 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1590 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001591 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1592 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001593 Method = Method->Next)
1594 if (Method->Method)
1595 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001596 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001597 Method = Method->Next)
1598 if (Method->Method)
1599 DataLen += 4;
1600 clang::io::Emit16(Out, DataLen);
1601 return std::make_pair(KeyLen, DataLen);
1602 }
Mike Stump11289f42009-09-09 15:08:12 +00001603
Douglas Gregor95c13f52009-04-25 17:48:32 +00001604 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001605 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001606 assert((Start >> 32) == 0 && "Selector key offset too large");
1607 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001608 unsigned N = Sel.getNumArgs();
1609 clang::io::Emit16(Out, N);
1610 if (N == 0)
1611 N = 1;
1612 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001613 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001614 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1615 }
Mike Stump11289f42009-09-09 15:08:12 +00001616
Douglas Gregorc78d3462009-04-24 21:10:55 +00001617 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001618 data_type_ref Methods, unsigned DataLen) {
1619 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001620 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001621 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001622 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001623 Method = Method->Next)
1624 if (Method->Method)
1625 ++NumInstanceMethods;
1626
1627 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001628 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001629 Method = Method->Next)
1630 if (Method->Method)
1631 ++NumFactoryMethods;
1632
1633 clang::io::Emit16(Out, NumInstanceMethods);
1634 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001635 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001636 Method = Method->Next)
1637 if (Method->Method)
1638 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001639 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001640 Method = Method->Next)
1641 if (Method->Method)
1642 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001643
1644 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001645 }
1646};
1647} // end anonymous namespace
1648
Sebastian Redla19a67f2010-08-03 21:58:15 +00001649/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001650///
1651/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001652/// in an on-disk hash table indexed by the selector. The hash table also
1653/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001654void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001655 using namespace llvm;
1656
Sebastian Redla19a67f2010-08-03 21:58:15 +00001657 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001658 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001659 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001660 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001661 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001662 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001663 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001664 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001665
Sebastian Redla19a67f2010-08-03 21:58:15 +00001666 // Create the on-disk hash table representation. We walk through every
1667 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001668 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001669 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001670 I = SelectorIDs.begin(), E = SelectorIDs.end();
1671 I != E; ++I) {
1672 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001673 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001674 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001675 I->second,
1676 ObjCMethodList(),
1677 ObjCMethodList()
1678 };
1679 if (F != SemaRef.MethodPool.end()) {
1680 Data.Instance = F->second.first;
1681 Data.Factory = F->second.second;
1682 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001683 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001684 // changed.
1685 if (Chain && I->second < FirstSelectorID) {
1686 // Selector already exists. Did it change?
1687 bool changed = false;
1688 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1689 M = M->Next) {
1690 if (M->Method->getPCHLevel() == 0)
1691 changed = true;
1692 }
1693 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1694 M = M->Next) {
1695 if (M->Method->getPCHLevel() == 0)
1696 changed = true;
1697 }
1698 if (!changed)
1699 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001700 } else if (Data.Instance.Method || Data.Factory.Method) {
1701 // A new method pool entry.
1702 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001703 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001704 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001705 }
1706
Douglas Gregorc78d3462009-04-24 21:10:55 +00001707 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001708 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001709 uint32_t BucketOffset;
1710 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001711 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001712 llvm::raw_svector_ostream Out(MethodPool);
1713 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001714 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001715 BucketOffset = Generator.Emit(Out, Trait);
1716 }
1717
1718 // Create a blob abbreviation
1719 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001720 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001721 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001722 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001723 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1724 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1725
Douglas Gregor95c13f52009-04-25 17:48:32 +00001726 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001727 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001728 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001729 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001730 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001731 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001732
1733 // Create a blob abbreviation for the selector table offsets.
1734 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001735 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1738 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1739
1740 // Write the selector offsets table.
1741 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001742 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001743 Record.push_back(SelectorOffsets.size());
1744 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001745 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001746 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001747 }
1748}
1749
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001750/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001751void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001752 using namespace llvm;
1753 if (SemaRef.ReferencedSelectors.empty())
1754 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001755
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001756 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001757
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001758 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001759 // very tricky to fix, and given that @selector shouldn't really appear in
1760 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001761 for (DenseMap<Selector, SourceLocation>::iterator S =
1762 SemaRef.ReferencedSelectors.begin(),
1763 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1764 Selector Sel = (*S).first;
1765 SourceLocation Loc = (*S).second;
1766 AddSelectorRef(Sel, Record);
1767 AddSourceLocation(Loc, Record);
1768 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001769 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001770}
1771
Douglas Gregorc5046832009-04-27 18:38:38 +00001772//===----------------------------------------------------------------------===//
1773// Identifier Table Serialization
1774//===----------------------------------------------------------------------===//
1775
Douglas Gregorc78d3462009-04-24 21:10:55 +00001776namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001777class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001778 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001779 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001780
Douglas Gregor1d583f22009-04-28 21:18:29 +00001781 /// \brief Determines whether this is an "interesting" identifier
1782 /// that needs a full IdentifierInfo structure written into the hash
1783 /// table.
1784 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1785 return II->isPoisoned() ||
1786 II->isExtensionToken() ||
1787 II->hasMacroDefinition() ||
1788 II->getObjCOrBuiltinID() ||
1789 II->getFETokenInfo<void>();
1790 }
1791
Douglas Gregore84a9da2009-04-20 20:36:09 +00001792public:
1793 typedef const IdentifierInfo* key_type;
1794 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001795
Sebastian Redl539c5062010-08-18 23:57:32 +00001796 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001797 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001798
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001799 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001800 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001801
1802 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001803 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001804 }
Mike Stump11289f42009-09-09 15:08:12 +00001805
1806 std::pair<unsigned,unsigned>
1807 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001808 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001809 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001810 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1811 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001812 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001813 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001814 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001815 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001816 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1817 DEnd = IdentifierResolver::end();
1818 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001819 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001820 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001821 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001822 // We emit the key length after the data length so that every
1823 // string is preceded by a 16-bit length. This matches the PTH
1824 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001825 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001826 return std::make_pair(KeyLen, DataLen);
1827 }
Mike Stump11289f42009-09-09 15:08:12 +00001828
1829 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001830 unsigned KeyLen) {
1831 // Record the location of the key data. This is used when generating
1832 // the mapping from persistent IDs to strings.
1833 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001834 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001835 }
Mike Stump11289f42009-09-09 15:08:12 +00001836
1837 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001838 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001839 if (!isInterestingIdentifier(II)) {
1840 clang::io::Emit32(Out, ID << 1);
1841 return;
1842 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001843
Douglas Gregor1d583f22009-04-28 21:18:29 +00001844 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001845 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001846 bool hasMacroDefinition =
1847 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001848 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001849 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001850 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1851 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1852 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001853 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001854 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001855 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001856
Douglas Gregorc3366a52009-04-21 23:56:24 +00001857 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001858 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001859
Douglas Gregora868bbd2009-04-21 22:25:48 +00001860 // Emit the declaration IDs in reverse order, because the
1861 // IdentifierResolver provides the declarations as they would be
1862 // visible (e.g., the function "stat" would come before the struct
1863 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1864 // adds declarations to the end of the list (so we need to see the
1865 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001866 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001867 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001868 IdentifierResolver::end());
1869 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1870 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001871 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001872 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001873 }
1874};
1875} // end anonymous namespace
1876
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001877/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001878///
1879/// The identifier table consists of a blob containing string data
1880/// (the actual identifiers themselves) and a separate "offsets" index
1881/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001882void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001883 using namespace llvm;
1884
1885 // Create and write out the blob that contains the identifier
1886 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001887 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001888 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001889 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001890
Douglas Gregore6648fb2009-04-28 20:33:11 +00001891 // Look for any identifiers that were named while processing the
1892 // headers, but are otherwise not needed. We add these to the hash
1893 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001894 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001895 // file.
1896 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1897 IDEnd = PP.getIdentifierTable().end();
1898 ID != IDEnd; ++ID)
1899 getIdentifierRef(ID->second);
1900
Sebastian Redlff4a2952010-07-23 23:49:55 +00001901 // Create the on-disk hash table representation. We only store offsets
1902 // for identifiers that appear here for the first time.
1903 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001904 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001905 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1906 ID != IDEnd; ++ID) {
1907 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001908 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001909 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001910 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001911
Douglas Gregore84a9da2009-04-20 20:36:09 +00001912 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001913 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001914 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001915 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001916 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001917 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001918 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001919 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001920 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001921 }
1922
1923 // Create a blob abbreviation
1924 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001925 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001928 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001929
1930 // Write the identifier table
1931 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001932 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001933 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001934 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001935 }
1936
1937 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001938 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001939 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1942 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1943
1944 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001945 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001946 Record.push_back(IdentifierOffsets.size());
1947 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001948 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001949 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001950}
1951
Douglas Gregorc5046832009-04-27 18:38:38 +00001952//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001953// DeclContext's Name Lookup Table Serialization
1954//===----------------------------------------------------------------------===//
1955
1956namespace {
1957// Trait used for the on-disk hash table used in the method pool.
1958class ASTDeclContextNameLookupTrait {
1959 ASTWriter &Writer;
1960
1961public:
1962 typedef DeclarationName key_type;
1963 typedef key_type key_type_ref;
1964
1965 typedef DeclContext::lookup_result data_type;
1966 typedef const data_type& data_type_ref;
1967
1968 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1969
1970 unsigned ComputeHash(DeclarationName Name) {
1971 llvm::FoldingSetNodeID ID;
1972 ID.AddInteger(Name.getNameKind());
1973
1974 switch (Name.getNameKind()) {
1975 case DeclarationName::Identifier:
1976 ID.AddString(Name.getAsIdentifierInfo()->getName());
1977 break;
1978 case DeclarationName::ObjCZeroArgSelector:
1979 case DeclarationName::ObjCOneArgSelector:
1980 case DeclarationName::ObjCMultiArgSelector:
1981 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1982 break;
1983 case DeclarationName::CXXConstructorName:
1984 case DeclarationName::CXXDestructorName:
1985 case DeclarationName::CXXConversionFunctionName:
1986 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1987 break;
1988 case DeclarationName::CXXOperatorName:
1989 ID.AddInteger(Name.getCXXOverloadedOperator());
1990 break;
1991 case DeclarationName::CXXLiteralOperatorName:
1992 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1993 case DeclarationName::CXXUsingDirective:
1994 break;
1995 }
1996
1997 return ID.ComputeHash();
1998 }
1999
2000 std::pair<unsigned,unsigned>
2001 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2002 data_type_ref Lookup) {
2003 unsigned KeyLen = 1;
2004 switch (Name.getNameKind()) {
2005 case DeclarationName::Identifier:
2006 case DeclarationName::ObjCZeroArgSelector:
2007 case DeclarationName::ObjCOneArgSelector:
2008 case DeclarationName::ObjCMultiArgSelector:
2009 case DeclarationName::CXXConstructorName:
2010 case DeclarationName::CXXDestructorName:
2011 case DeclarationName::CXXConversionFunctionName:
2012 case DeclarationName::CXXLiteralOperatorName:
2013 KeyLen += 4;
2014 break;
2015 case DeclarationName::CXXOperatorName:
2016 KeyLen += 1;
2017 break;
2018 case DeclarationName::CXXUsingDirective:
2019 break;
2020 }
2021 clang::io::Emit16(Out, KeyLen);
2022
2023 // 2 bytes for num of decls and 4 for each DeclID.
2024 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2025 clang::io::Emit16(Out, DataLen);
2026
2027 return std::make_pair(KeyLen, DataLen);
2028 }
2029
2030 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2031 using namespace clang::io;
2032
2033 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2034 Emit8(Out, Name.getNameKind());
2035 switch (Name.getNameKind()) {
2036 case DeclarationName::Identifier:
2037 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2038 break;
2039 case DeclarationName::ObjCZeroArgSelector:
2040 case DeclarationName::ObjCOneArgSelector:
2041 case DeclarationName::ObjCMultiArgSelector:
2042 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2043 break;
2044 case DeclarationName::CXXConstructorName:
2045 case DeclarationName::CXXDestructorName:
2046 case DeclarationName::CXXConversionFunctionName:
2047 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2048 break;
2049 case DeclarationName::CXXOperatorName:
2050 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2051 Emit8(Out, Name.getCXXOverloadedOperator());
2052 break;
2053 case DeclarationName::CXXLiteralOperatorName:
2054 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2055 break;
2056 case DeclarationName::CXXUsingDirective:
2057 break;
2058 }
2059 }
2060
2061 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2062 data_type Lookup, unsigned DataLen) {
2063 uint64_t Start = Out.tell(); (void)Start;
2064 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2065 for (; Lookup.first != Lookup.second; ++Lookup.first)
2066 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2067
2068 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2069 }
2070};
2071} // end anonymous namespace
2072
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002073/// \brief Write the block containing all of the declaration IDs
2074/// visible from the given DeclContext.
2075///
2076/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002077/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002078uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2079 DeclContext *DC) {
2080 if (DC->getPrimaryContext() != DC)
2081 return 0;
2082
2083 // Since there is no name lookup into functions or methods, don't bother to
2084 // build a visible-declarations table for these entities.
2085 if (DC->isFunctionOrMethod())
2086 return 0;
2087
2088 // If not in C++, we perform name lookup for the translation unit via the
2089 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2090 // FIXME: In C++ we need the visible declarations in order to "see" the
2091 // friend declarations, is there a way to do this without writing the table ?
2092 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2093 return 0;
2094
2095 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002096 if (DC->hasExternalVisibleStorage())
2097 DC->MaterializeVisibleDeclsFromExternalStorage();
2098 else
2099 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002100
2101 // Serialize the contents of the mapping used for lookup. Note that,
2102 // although we have two very different code paths, the serialized
2103 // representation is the same for both cases: a declaration name,
2104 // followed by a size, followed by references to the visible
2105 // declarations that have that name.
2106 uint64_t Offset = Stream.GetCurrentBitNo();
2107 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2108 if (!Map || Map->empty())
2109 return 0;
2110
2111 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2112 ASTDeclContextNameLookupTrait Trait(*this);
2113
2114 // Create the on-disk hash table representation.
2115 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2116 D != DEnd; ++D) {
2117 DeclarationName Name = D->first;
2118 DeclContext::lookup_result Result = D->second.getLookupResult();
2119 Generator.insert(Name, Result, Trait);
2120 }
2121
2122 // Create the on-disk hash table in a buffer.
2123 llvm::SmallString<4096> LookupTable;
2124 uint32_t BucketOffset;
2125 {
2126 llvm::raw_svector_ostream Out(LookupTable);
2127 // Make sure that no bucket is at offset 0
2128 clang::io::Emit32(Out, 0);
2129 BucketOffset = Generator.Emit(Out, Trait);
2130 }
2131
2132 // Write the lookup table
2133 RecordData Record;
2134 Record.push_back(DECL_CONTEXT_VISIBLE);
2135 Record.push_back(BucketOffset);
2136 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2137 LookupTable.str());
2138
2139 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2140 ++NumVisibleDeclContexts;
2141 return Offset;
2142}
2143
Sebastian Redla4071b42010-08-24 00:50:09 +00002144/// \brief Write an UPDATE_VISIBLE block for the given context.
2145///
2146/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2147/// DeclContext in a dependent AST file. As such, they only exist for the TU
2148/// (in C++) and for namespaces.
2149void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002150 // Make the context build its lookup table, but don't make it load external
2151 // decls.
2152 DC->lookup(DeclarationName());
2153
2154 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2155 if (!Map || Map->empty())
2156 return;
2157
2158 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2159 ASTDeclContextNameLookupTrait Trait(*this);
2160
2161 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002162 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2163 D != DEnd; ++D) {
2164 DeclarationName Name = D->first;
2165 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002166 // For any name that appears in this table, the results are complete, i.e.
2167 // they overwrite results from previous PCHs. Merging is always a mess.
2168 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002169 }
2170
2171 // Create the on-disk hash table in a buffer.
2172 llvm::SmallString<4096> LookupTable;
2173 uint32_t BucketOffset;
2174 {
2175 llvm::raw_svector_ostream Out(LookupTable);
2176 // Make sure that no bucket is at offset 0
2177 clang::io::Emit32(Out, 0);
2178 BucketOffset = Generator.Emit(Out, Trait);
2179 }
2180
2181 // Write the lookup table
2182 RecordData Record;
2183 Record.push_back(UPDATE_VISIBLE);
2184 Record.push_back(getDeclID(cast<Decl>(DC)));
2185 Record.push_back(BucketOffset);
2186 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2187}
2188
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002189//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002190// General Serialization Routines
2191//===----------------------------------------------------------------------===//
2192
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002193/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002194void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002195 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002196 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2197 const Attr * A = *i;
2198 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2199 AddSourceLocation(A->getLocation(), Record);
2200 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002201
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002202#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002203
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002204 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002205}
2206
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002207void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002208 Record.push_back(Str.size());
2209 Record.insert(Record.end(), Str.begin(), Str.end());
2210}
2211
Douglas Gregore84a9da2009-04-20 20:36:09 +00002212/// \brief Note that the identifier II occurs at the given offset
2213/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002214void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002216 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002217 // up earlier in the chain and thus don't need an offset.
2218 if (ID >= FirstIdentID)
2219 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002220}
2221
Douglas Gregor95c13f52009-04-25 17:48:32 +00002222/// \brief Note that the selector Sel occurs at the given offset
2223/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002224void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002225 unsigned ID = SelectorIDs[Sel];
2226 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002227 // Don't record offsets for selectors that are also available in a different
2228 // file.
2229 if (ID < FirstSelectorID)
2230 return;
2231 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002232}
2233
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002234ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redld95a56e2010-08-04 18:21:41 +00002235 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002236 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002237 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002238 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2239 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002240 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002241 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2242 NextCXXBaseSpecifiersID(1)
2243{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002244}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002245
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002246void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002247 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002248 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002249 Stream.Emit((unsigned)'C', 8);
2250 Stream.Emit((unsigned)'P', 8);
2251 Stream.Emit((unsigned)'C', 8);
2252 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002253
Chris Lattner28fa4e62009-04-26 22:26:21 +00002254 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002255
Sebastian Redl143413f2010-07-12 22:02:52 +00002256 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002257 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002258 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002259 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002260}
2261
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002262void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002263 const char *isysroot) {
2264 using namespace llvm;
2265
2266 ASTContext &Context = SemaRef.Context;
2267 Preprocessor &PP = SemaRef.PP;
2268
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002269 // The translation unit is the first declaration we'll emit.
2270 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002271 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002272 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002273
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002274 // Make sure that we emit IdentifierInfos (and any attached
2275 // declarations) for builtins.
2276 {
2277 IdentifierTable &Table = PP.getIdentifierTable();
2278 llvm::SmallVector<const char *, 32> BuiltinNames;
2279 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2280 Context.getLangOptions().NoBuiltin);
2281 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2282 getIdentifierRef(&Table.get(BuiltinNames[I]));
2283 }
2284
Chris Lattner0c797362009-09-08 18:19:27 +00002285 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002286 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002287 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002288 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002289 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2290 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002291 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002292
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002293 // Build a record containing all of the file scoped decls in this file.
2294 RecordData UnusedFileScopedDecls;
2295 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2296 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002297
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002298 RecordData WeakUndeclaredIdentifiers;
2299 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2300 WeakUndeclaredIdentifiers.push_back(
2301 SemaRef.WeakUndeclaredIdentifiers.size());
2302 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2303 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2304 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2305 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2306 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2307 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2308 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2309 }
2310 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002311
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002312 // Build a record containing all of the locally-scoped external
2313 // declarations in this header file. Generally, this record will be
2314 // empty.
2315 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002316 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002317 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002318 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002319 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2320 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2321 TD != TDEnd; ++TD)
2322 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2323
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002324 // Build a record containing all of the ext_vector declarations.
2325 RecordData ExtVectorDecls;
2326 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2327 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2328
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002329 // Build a record containing all of the VTable uses information.
2330 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002331 if (!SemaRef.VTableUses.empty()) {
2332 VTableUses.push_back(SemaRef.VTableUses.size());
2333 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2334 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2335 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2336 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2337 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002338 }
2339
2340 // Build a record containing all of dynamic classes declarations.
2341 RecordData DynamicClasses;
2342 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2343 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2344
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002345 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002346 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002347 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002348 I = SemaRef.PendingInstantiations.begin(),
2349 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2350 AddDeclRef(I->first, PendingInstantiations);
2351 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002352 }
2353 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2354 "There are local ones at end of translation unit!");
2355
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002356 // Build a record containing some declaration references.
2357 RecordData SemaDeclRefs;
2358 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2359 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2360 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2361 }
2362
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002363 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002364 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002365 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002366 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002367 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002368 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002369 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002370 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002371 // Write the record of special types.
2372 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002373
Steve Naroffc277ad12009-07-18 15:33:26 +00002374 AddTypeRef(Context.getBuiltinVaListType(), Record);
2375 AddTypeRef(Context.getObjCIdType(), Record);
2376 AddTypeRef(Context.getObjCSelType(), Record);
2377 AddTypeRef(Context.getObjCProtoType(), Record);
2378 AddTypeRef(Context.getObjCClassType(), Record);
2379 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2380 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2381 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002382 AddTypeRef(Context.getjmp_bufType(), Record);
2383 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002384 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2385 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002386 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002387 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002388 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2389 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002390 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002391 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002392
Douglas Gregor1970d882009-04-26 03:49:13 +00002393 // Keep writing types and declarations until all types and
2394 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002395 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002396 WriteDeclsBlockAbbrevs();
2397 while (!DeclTypesToEmit.empty()) {
2398 DeclOrType DOT = DeclTypesToEmit.front();
2399 DeclTypesToEmit.pop();
2400 if (DOT.isType())
2401 WriteType(DOT.getType());
2402 else
2403 WriteDecl(Context, DOT.getDecl());
2404 }
2405 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002406
Douglas Gregor45053152009-10-17 17:25:45 +00002407 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002408 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002409 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002410 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002411
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002412 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002413 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002414
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002415 // Write the C++ base-specifier set offsets.
2416 if (!CXXBaseSpecifiersOffsets.empty()) {
2417 // Create a blob abbreviation for the C++ base specifiers offsets.
2418 using namespace llvm;
2419
2420 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2421 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2424 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2425
2426 // Write the selector offsets table.
2427 Record.clear();
2428 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2429 Record.push_back(CXXBaseSpecifiersOffsets.size());
2430 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2431 (const char *)CXXBaseSpecifiersOffsets.data(),
2432 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2433 }
2434
Douglas Gregord4df8652009-04-22 22:02:47 +00002435 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002436 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002437 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002438
2439 // Write the record containing tentative definitions.
2440 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002441 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002442
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002443 // Write the record containing unused file scoped decls.
2444 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002445 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002446
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002447 // Write the record containing weak undeclared identifiers.
2448 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002449 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002450 WeakUndeclaredIdentifiers);
2451
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002452 // Write the record containing locally-scoped external definitions.
2453 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002454 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002455 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002456
2457 // Write the record containing ext_vector type names.
2458 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002459 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002460
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002461 // Write the record containing VTable uses information.
2462 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002463 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002464
2465 // Write the record containing dynamic classes declarations.
2466 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002467 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002468
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002469 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002470 if (!PendingInstantiations.empty())
2471 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002472
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002473 // Write the record containing declaration references of Sema.
2474 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002475 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002476
Douglas Gregor08f01292009-04-17 22:13:46 +00002477 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002478 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002479 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002480 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002481 Record.push_back(NumLexicalDeclContexts);
2482 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002483 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002484 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002485}
2486
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002487void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002488 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002489 using namespace llvm;
2490
2491 ASTContext &Context = SemaRef.Context;
2492 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002493
Sebastian Redl143413f2010-07-12 22:02:52 +00002494 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002495 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002496 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002497 if (StatCalls && !isysroot)
2498 WriteStatCache(*StatCalls);
2499 // FIXME: Source manager block should only write new stuff, which could be
2500 // done by tracking the largest ID in the chain
2501 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002502
2503 // The special types are in the chained PCH.
2504
2505 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002506 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002507 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002508 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002509 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2510 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002511 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002512 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002513 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002514 else if ((*I)->isChangedSinceDeserialization())
2515 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002516 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002517 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002518 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002519 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002520 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2521 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2522 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002523 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002524 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2525 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002526 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002527 // And a visible updates block for the DeclContexts.
2528 Abv = new llvm::BitCodeAbbrev();
2529 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2530 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2531 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2532 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2533 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2534 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002535
Sebastian Redl98912122010-07-27 23:01:28 +00002536 // Build a record containing all of the new tentative definitions in this
2537 // file, in TentativeDefinitions order.
2538 RecordData TentativeDefinitions;
2539 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2540 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2541 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2542 }
2543
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002544 // Build a record containing all of the file scoped decls in this file.
2545 RecordData UnusedFileScopedDecls;
2546 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2547 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2548 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002549 }
2550
Sebastian Redl08aca90252010-08-05 18:21:25 +00002551 // We write the entire table, overwriting the tables from the chain.
2552 RecordData WeakUndeclaredIdentifiers;
2553 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2554 WeakUndeclaredIdentifiers.push_back(
2555 SemaRef.WeakUndeclaredIdentifiers.size());
2556 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2557 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2558 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2559 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2560 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2561 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2562 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2563 }
2564 }
2565
Sebastian Redl98912122010-07-27 23:01:28 +00002566 // Build a record containing all of the locally-scoped external
2567 // declarations in this header file. Generally, this record will be
2568 // empty.
2569 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002570 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002571 // nondeterminstic!
2572 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2573 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2574 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2575 TD != TDEnd; ++TD) {
2576 if (TD->second->getPCHLevel() == 0)
2577 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2578 }
2579
2580 // Build a record containing all of the ext_vector declarations.
2581 RecordData ExtVectorDecls;
2582 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2583 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2584 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2585 }
2586
Sebastian Redl08aca90252010-08-05 18:21:25 +00002587 // Build a record containing all of the VTable uses information.
2588 // We write everything here, because it's too hard to determine whether
2589 // a use is new to this part.
2590 RecordData VTableUses;
2591 if (!SemaRef.VTableUses.empty()) {
2592 VTableUses.push_back(SemaRef.VTableUses.size());
2593 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2594 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2595 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2596 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2597 }
2598 }
2599
2600 // Build a record containing all of dynamic classes declarations.
2601 RecordData DynamicClasses;
2602 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2603 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2604 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2605
2606 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002607 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002608 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002609 I = SemaRef.PendingInstantiations.begin(),
2610 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002611 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002612 AddDeclRef(I->first, PendingInstantiations);
2613 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002614 }
2615 }
2616 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2617 "There are local ones at end of translation unit!");
2618
2619 // Build a record containing some declaration references.
2620 // It's not worth the effort to avoid duplication here.
2621 RecordData SemaDeclRefs;
2622 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2623 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2624 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2625 }
2626
Sebastian Redl539c5062010-08-18 23:57:32 +00002627 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002628 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002629 for (DeclsToRewriteTy::iterator
2630 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2631 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002632 while (!DeclTypesToEmit.empty()) {
2633 DeclOrType DOT = DeclTypesToEmit.front();
2634 DeclTypesToEmit.pop();
2635 if (DOT.isType())
2636 WriteType(DOT.getType());
2637 else
2638 WriteDecl(Context, DOT.getDecl());
2639 }
2640 Stream.ExitBlock();
2641
Sebastian Redl98912122010-07-27 23:01:28 +00002642 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002643 WriteSelectors(SemaRef);
2644 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002645 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002646 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002647 // FIXME: For chained PCH only write the new mappings (we currently
2648 // write all of them again).
2649 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002650
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002651 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002652 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002653 RecordData FirstLatestDeclIDs;
2654 for (FirstLatestDeclMap::iterator
2655 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2656 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2657 "Expected first & second to be in different PCHs");
2658 AddDeclRef(I->first, FirstLatestDeclIDs);
2659 AddDeclRef(I->second, FirstLatestDeclIDs);
2660 }
2661 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002662 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002663
Sebastian Redl98912122010-07-27 23:01:28 +00002664 // Write the record containing external, unnamed definitions.
2665 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002666 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002667
2668 // Write the record containing tentative definitions.
2669 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002670 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002671
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002672 // Write the record containing unused file scoped decls.
2673 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002674 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002675
Sebastian Redl08aca90252010-08-05 18:21:25 +00002676 // Write the record containing weak undeclared identifiers.
2677 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002678 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002679 WeakUndeclaredIdentifiers);
2680
Sebastian Redl98912122010-07-27 23:01:28 +00002681 // Write the record containing locally-scoped external definitions.
2682 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002683 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002684 LocallyScopedExternalDecls);
2685
2686 // Write the record containing ext_vector type names.
2687 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002688 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002689
Sebastian Redl08aca90252010-08-05 18:21:25 +00002690 // Write the record containing VTable uses information.
2691 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002692 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002693
2694 // Write the record containing dynamic classes declarations.
2695 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002696 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002697
2698 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002699 if (!PendingInstantiations.empty())
2700 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002701
2702 // Write the record containing declaration references of Sema.
2703 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002704 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002705
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002706 // Write the updates to DeclContexts.
2707 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2708 I = UpdatedDeclContexts.begin(),
2709 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002710 I != E; ++I)
2711 WriteDeclContextVisibleUpdate(*I);
2712
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002713 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002714
Sebastian Redl98912122010-07-27 23:01:28 +00002715 Record.clear();
2716 Record.push_back(NumStatements);
2717 Record.push_back(NumMacros);
2718 Record.push_back(NumLexicalDeclContexts);
2719 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002720 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002721 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002722 Stream.ExitBlock();
2723}
2724
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002725void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002726 if (DeclUpdates.empty())
2727 return;
2728
2729 RecordData OffsetsRecord;
2730 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2731 for (DeclUpdateMap::iterator
2732 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2733 const Decl *D = I->first;
2734 UpdateRecord &URec = I->second;
2735
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002736 if (DeclsToRewrite.count(D))
2737 continue; // The decl will be written completely,no need to store updates.
2738
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002739 uint64_t Offset = Stream.GetCurrentBitNo();
2740 Stream.EmitRecord(DECL_UPDATES, URec);
2741
2742 OffsetsRecord.push_back(GetDeclRef(D));
2743 OffsetsRecord.push_back(Offset);
2744 }
2745 Stream.ExitBlock();
2746 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2747}
2748
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002749void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002750 if (ReplacedDecls.empty())
2751 return;
2752
2753 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002754 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002755 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2756 Record.push_back(I->first);
2757 Record.push_back(I->second);
2758 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002759 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002760}
2761
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002762void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002763 Record.push_back(Loc.getRawEncoding());
2764}
2765
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002766void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002767 AddSourceLocation(Range.getBegin(), Record);
2768 AddSourceLocation(Range.getEnd(), Record);
2769}
2770
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002771void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002772 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002773 const uint64_t *Words = Value.getRawData();
2774 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002775}
2776
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002777void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002778 Record.push_back(Value.isUnsigned());
2779 AddAPInt(Value, Record);
2780}
2781
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002782void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002783 AddAPInt(Value.bitcastToAPInt(), Record);
2784}
2785
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002786void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002787 Record.push_back(getIdentifierRef(II));
2788}
2789
Sebastian Redl539c5062010-08-18 23:57:32 +00002790IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002791 if (II == 0)
2792 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002793
Sebastian Redl539c5062010-08-18 23:57:32 +00002794 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002795 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002796 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002797 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002798}
2799
Sebastian Redl50e26582010-09-15 19:54:06 +00002800MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002801 if (MD == 0)
2802 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002803
2804 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002805 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002806 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002807 return ID;
2808}
2809
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002810void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002811 Record.push_back(getSelectorRef(SelRef));
2812}
2813
Sebastian Redl539c5062010-08-18 23:57:32 +00002814SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002815 if (Sel.getAsOpaquePtr() == 0) {
2816 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002817 }
2818
Sebastian Redl539c5062010-08-18 23:57:32 +00002819 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002820 if (SID == 0 && Chain) {
2821 // This might trigger a ReadSelector callback, which will set the ID for
2822 // this selector.
2823 Chain->LoadSelector(Sel);
2824 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002825 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002826 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002827 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002828 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002829}
2830
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002831void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002832 AddDeclRef(Temp->getDestructor(), Record);
2833}
2834
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002835void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2836 CXXBaseSpecifier const *BasesEnd,
2837 RecordDataImpl &Record) {
2838 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2839 CXXBaseSpecifiersToWrite.push_back(
2840 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2841 Bases, BasesEnd));
2842 Record.push_back(NextCXXBaseSpecifiersID++);
2843}
2844
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002845void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002846 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002847 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002848 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002849 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002850 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002851 break;
2852 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002853 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002854 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002855 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002856 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2857 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002858 break;
John McCall0ad16662009-10-29 08:12:44 +00002859 case TemplateArgument::Null:
2860 case TemplateArgument::Integral:
2861 case TemplateArgument::Declaration:
2862 case TemplateArgument::Pack:
2863 break;
2864 }
2865}
2866
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002867void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002868 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002869 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002870
2871 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2872 bool InfoHasSameExpr
2873 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2874 Record.push_back(InfoHasSameExpr);
2875 if (InfoHasSameExpr)
2876 return; // Avoid storing the same expr twice.
2877 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002878 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2879 Record);
2880}
2881
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002882void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002883 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002884 AddTypeRef(QualType(), Record);
2885 return;
2886 }
2887
John McCallbcd03502009-12-07 02:54:59 +00002888 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002889 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002890 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002891 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002892}
2893
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002894void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002895 Record.push_back(GetOrCreateTypeID(T));
2896}
2897
2898TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002899 return MakeTypeID(T,
2900 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2901}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002902
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002903TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002904 return MakeTypeID(T,
2905 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002906}
2907
2908TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2909 if (T.isNull())
2910 return TypeIdx();
2911 assert(!T.getLocalFastQualifiers());
2912
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002913 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002914 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002915 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002916 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002917 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002918 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002919 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002920 return Idx;
2921}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002922
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002923TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002924 if (T.isNull())
2925 return TypeIdx();
2926 assert(!T.getLocalFastQualifiers());
2927
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002928 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2929 assert(I != TypeIdxs.end() && "Type not emitted!");
2930 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002931}
2932
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002933void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002934 Record.push_back(GetDeclRef(D));
2935}
2936
Sebastian Redl539c5062010-08-18 23:57:32 +00002937DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002938 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002939 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002940 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002941 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00002942 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002943 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002944 // We haven't seen this declaration before. Give it a new ID and
2945 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002946 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002947 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002948 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2949 // We don't add it to the replacement collection here, because we don't
2950 // have the offset yet.
2951 DeclTypesToEmit.push(const_cast<Decl *>(D));
2952 // Reset the flag, so that we don't add this decl multiple times.
2953 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002954 }
2955
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002956 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002957}
2958
Sebastian Redl539c5062010-08-18 23:57:32 +00002959DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002960 if (D == 0)
2961 return 0;
2962
2963 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2964 return DeclIDs[D];
2965}
2966
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002967void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002968 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002969 Record.push_back(Name.getNameKind());
2970 switch (Name.getNameKind()) {
2971 case DeclarationName::Identifier:
2972 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2973 break;
2974
2975 case DeclarationName::ObjCZeroArgSelector:
2976 case DeclarationName::ObjCOneArgSelector:
2977 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002978 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002979 break;
2980
2981 case DeclarationName::CXXConstructorName:
2982 case DeclarationName::CXXDestructorName:
2983 case DeclarationName::CXXConversionFunctionName:
2984 AddTypeRef(Name.getCXXNameType(), Record);
2985 break;
2986
2987 case DeclarationName::CXXOperatorName:
2988 Record.push_back(Name.getCXXOverloadedOperator());
2989 break;
2990
Alexis Hunt3d221f22009-11-29 07:34:05 +00002991 case DeclarationName::CXXLiteralOperatorName:
2992 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2993 break;
2994
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002995 case DeclarationName::CXXUsingDirective:
2996 // No extra data to emit
2997 break;
2998 }
2999}
Chris Lattnerca025db2010-05-07 21:43:38 +00003000
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003001void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003002 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003003 switch (Name.getNameKind()) {
3004 case DeclarationName::CXXConstructorName:
3005 case DeclarationName::CXXDestructorName:
3006 case DeclarationName::CXXConversionFunctionName:
3007 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3008 break;
3009
3010 case DeclarationName::CXXOperatorName:
3011 AddSourceLocation(
3012 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3013 Record);
3014 AddSourceLocation(
3015 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3016 Record);
3017 break;
3018
3019 case DeclarationName::CXXLiteralOperatorName:
3020 AddSourceLocation(
3021 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3022 Record);
3023 break;
3024
3025 case DeclarationName::Identifier:
3026 case DeclarationName::ObjCZeroArgSelector:
3027 case DeclarationName::ObjCOneArgSelector:
3028 case DeclarationName::ObjCMultiArgSelector:
3029 case DeclarationName::CXXUsingDirective:
3030 break;
3031 }
3032}
3033
3034void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003035 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003036 AddDeclarationName(NameInfo.getName(), Record);
3037 AddSourceLocation(NameInfo.getLoc(), Record);
3038 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3039}
3040
3041void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003042 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003043 AddNestedNameSpecifier(Info.NNS, Record);
3044 AddSourceRange(Info.NNSRange, Record);
3045 Record.push_back(Info.NumTemplParamLists);
3046 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3047 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3048}
3049
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003050void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003051 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003052 // Nested name specifiers usually aren't too long. I think that 8 would
3053 // typically accomodate the vast majority.
3054 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3055
3056 // Push each of the NNS's onto a stack for serialization in reverse order.
3057 while (NNS) {
3058 NestedNames.push_back(NNS);
3059 NNS = NNS->getPrefix();
3060 }
3061
3062 Record.push_back(NestedNames.size());
3063 while(!NestedNames.empty()) {
3064 NNS = NestedNames.pop_back_val();
3065 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3066 Record.push_back(Kind);
3067 switch (Kind) {
3068 case NestedNameSpecifier::Identifier:
3069 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3070 break;
3071
3072 case NestedNameSpecifier::Namespace:
3073 AddDeclRef(NNS->getAsNamespace(), Record);
3074 break;
3075
3076 case NestedNameSpecifier::TypeSpec:
3077 case NestedNameSpecifier::TypeSpecWithTemplate:
3078 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3079 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3080 break;
3081
3082 case NestedNameSpecifier::Global:
3083 // Don't need to write an associated value.
3084 break;
3085 }
3086 }
3087}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003088
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003089void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003090 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003091 Record.push_back(Kind);
3092 switch (Kind) {
3093 case TemplateName::Template:
3094 AddDeclRef(Name.getAsTemplateDecl(), Record);
3095 break;
3096
3097 case TemplateName::OverloadedTemplate: {
3098 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3099 Record.push_back(OvT->size());
3100 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3101 I != E; ++I)
3102 AddDeclRef(*I, Record);
3103 break;
3104 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003105
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003106 case TemplateName::QualifiedTemplate: {
3107 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3108 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3109 Record.push_back(QualT->hasTemplateKeyword());
3110 AddDeclRef(QualT->getTemplateDecl(), Record);
3111 break;
3112 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003113
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003114 case TemplateName::DependentTemplate: {
3115 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3116 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3117 Record.push_back(DepT->isIdentifier());
3118 if (DepT->isIdentifier())
3119 AddIdentifierRef(DepT->getIdentifier(), Record);
3120 else
3121 Record.push_back(DepT->getOperator());
3122 break;
3123 }
3124 }
3125}
3126
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003127void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003128 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003129 Record.push_back(Arg.getKind());
3130 switch (Arg.getKind()) {
3131 case TemplateArgument::Null:
3132 break;
3133 case TemplateArgument::Type:
3134 AddTypeRef(Arg.getAsType(), Record);
3135 break;
3136 case TemplateArgument::Declaration:
3137 AddDeclRef(Arg.getAsDecl(), Record);
3138 break;
3139 case TemplateArgument::Integral:
3140 AddAPSInt(*Arg.getAsIntegral(), Record);
3141 AddTypeRef(Arg.getIntegralType(), Record);
3142 break;
3143 case TemplateArgument::Template:
3144 AddTemplateName(Arg.getAsTemplate(), Record);
3145 break;
3146 case TemplateArgument::Expression:
3147 AddStmt(Arg.getAsExpr());
3148 break;
3149 case TemplateArgument::Pack:
3150 Record.push_back(Arg.pack_size());
3151 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3152 I != E; ++I)
3153 AddTemplateArgument(*I, Record);
3154 break;
3155 }
3156}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003157
3158void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003159ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003160 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003161 assert(TemplateParams && "No TemplateParams!");
3162 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3163 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3164 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3165 Record.push_back(TemplateParams->size());
3166 for (TemplateParameterList::const_iterator
3167 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3168 P != PEnd; ++P)
3169 AddDeclRef(*P, Record);
3170}
3171
3172/// \brief Emit a template argument list.
3173void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003174ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003175 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003176 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003177 Record.push_back(TemplateArgs->size());
3178 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003179 AddTemplateArgument(TemplateArgs->get(i), Record);
3180}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003181
3182
3183void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003184ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003185 Record.push_back(Set.size());
3186 for (UnresolvedSetImpl::const_iterator
3187 I = Set.begin(), E = Set.end(); I != E; ++I) {
3188 AddDeclRef(I.getDecl(), Record);
3189 Record.push_back(I.getAccess());
3190 }
3191}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003192
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003193void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003194 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003195 Record.push_back(Base.isVirtual());
3196 Record.push_back(Base.isBaseOfClass());
3197 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003198 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003199 AddSourceRange(Base.getSourceRange(), Record);
3200}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003201
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003202void ASTWriter::FlushCXXBaseSpecifiers() {
3203 RecordData Record;
3204 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3205 Record.clear();
3206
3207 // Record the offset of this base-specifier set.
3208 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3209 if (Index == CXXBaseSpecifiersOffsets.size())
3210 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3211 else {
3212 if (Index > CXXBaseSpecifiersOffsets.size())
3213 CXXBaseSpecifiersOffsets.resize(Index + 1);
3214 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3215 }
3216
3217 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3218 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3219 Record.push_back(BEnd - B);
3220 for (; B != BEnd; ++B)
3221 AddCXXBaseSpecifier(*B, Record);
3222 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003223
3224 // Flush any expressions that were written as part of the base specifiers.
3225 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003226 }
3227
3228 CXXBaseSpecifiersToWrite.clear();
3229}
3230
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003231void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003232 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003233 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003234 Record.push_back(NumBaseOrMembers);
3235 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3236 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3237
3238 Record.push_back(Init->isBaseInitializer());
3239 if (Init->isBaseInitializer()) {
3240 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3241 Record.push_back(Init->isBaseVirtual());
3242 } else {
3243 AddDeclRef(Init->getMember(), Record);
3244 }
3245 AddSourceLocation(Init->getMemberLocation(), Record);
3246 AddStmt(Init->getInit());
3247 AddDeclRef(Init->getAnonUnionMember(), Record);
3248 AddSourceLocation(Init->getLParenLoc(), Record);
3249 AddSourceLocation(Init->getRParenLoc(), Record);
3250 Record.push_back(Init->isWritten());
3251 if (Init->isWritten()) {
3252 Record.push_back(Init->getSourceOrder());
3253 } else {
3254 Record.push_back(Init->getNumArrayIndices());
3255 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3256 AddDeclRef(Init->getArrayIndex(i), Record);
3257 }
3258 }
3259}
3260
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003261void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3262 assert(D->DefinitionData);
3263 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3264 Record.push_back(Data.UserDeclaredConstructor);
3265 Record.push_back(Data.UserDeclaredCopyConstructor);
3266 Record.push_back(Data.UserDeclaredCopyAssignment);
3267 Record.push_back(Data.UserDeclaredDestructor);
3268 Record.push_back(Data.Aggregate);
3269 Record.push_back(Data.PlainOldData);
3270 Record.push_back(Data.Empty);
3271 Record.push_back(Data.Polymorphic);
3272 Record.push_back(Data.Abstract);
3273 Record.push_back(Data.HasTrivialConstructor);
3274 Record.push_back(Data.HasTrivialCopyConstructor);
3275 Record.push_back(Data.HasTrivialCopyAssignment);
3276 Record.push_back(Data.HasTrivialDestructor);
3277 Record.push_back(Data.ComputedVisibleConversions);
3278 Record.push_back(Data.DeclaredDefaultConstructor);
3279 Record.push_back(Data.DeclaredCopyConstructor);
3280 Record.push_back(Data.DeclaredCopyAssignment);
3281 Record.push_back(Data.DeclaredDestructor);
3282
3283 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003284 if (Data.NumBases > 0)
3285 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3286 Record);
3287
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003288 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3289 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003290 if (Data.NumVBases > 0)
3291 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3292 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003293
3294 AddUnresolvedSet(Data.Conversions, Record);
3295 AddUnresolvedSet(Data.VisibleConversions, Record);
3296 // Data.Definition is the owning decl, no need to write it.
3297 AddDeclRef(Data.FirstFriend, Record);
3298}
3299
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003300void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003301 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003302 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003303 assert(FirstDeclID == NextDeclID &&
3304 FirstTypeID == NextTypeID &&
3305 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003306 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003307 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003308 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003309 "Setting chain after writing has started.");
3310 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003311
3312 FirstDeclID += Chain->getTotalNumDecls();
3313 FirstTypeID += Chain->getTotalNumTypes();
3314 FirstIdentID += Chain->getTotalNumIdentifiers();
3315 FirstSelectorID += Chain->getTotalNumSelectors();
3316 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003317 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003318 NextDeclID = FirstDeclID;
3319 NextTypeID = FirstTypeID;
3320 NextIdentID = FirstIdentID;
3321 NextSelectorID = FirstSelectorID;
3322 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003323 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003324}
3325
Sebastian Redl539c5062010-08-18 23:57:32 +00003326void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003327 IdentifierIDs[II] = ID;
3328}
3329
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003330void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003331 // Always take the highest-numbered type index. This copes with an interesting
3332 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003333 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003334 // keep the higher-numbered entry so that we can properly write it out to
3335 // the AST file.
3336 TypeIdx &StoredIdx = TypeIdxs[T];
3337 if (Idx.getIndex() >= StoredIdx.getIndex())
3338 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003339}
3340
Sebastian Redl539c5062010-08-18 23:57:32 +00003341void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003342 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003343}
Sebastian Redl834bb972010-08-04 17:20:04 +00003344
Sebastian Redl539c5062010-08-18 23:57:32 +00003345void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003346 SelectorIDs[S] = ID;
3347}
Douglas Gregor91096292010-10-02 19:29:26 +00003348
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003349void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003350 MacroDefinition *MD) {
3351 MacroDefinitions[MD] = ID;
3352}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003353
3354void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3355 assert(D->isDefinition());
3356 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3357 // We are interested when a PCH decl is modified.
3358 if (RD->getPCHLevel() > 0) {
3359 // A forward reference was mutated into a definition. Rewrite it.
3360 // FIXME: This happens during template instantiation, should we
3361 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003362 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003363 }
3364
3365 for (CXXRecordDecl::redecl_iterator
3366 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3367 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3368 if (Redecl == RD)
3369 continue;
3370
3371 // We are interested when a PCH decl is modified.
3372 if (Redecl->getPCHLevel() > 0) {
3373 UpdateRecord &Record = DeclUpdates[Redecl];
3374 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3375 assert(Redecl->DefinitionData);
3376 assert(Redecl->DefinitionData->Definition == D);
3377 AddDeclRef(D, Record); // the DefinitionDecl
3378 }
3379 }
3380 }
3381}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003382void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3383 // TU and namespaces are handled elsewhere.
3384 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3385 return;
3386
3387 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3388 return; // Not a source decl added to a DeclContext from PCH.
3389
3390 AddUpdatedDeclContext(DC);
3391}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003392
3393void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3394 assert(D->isImplicit());
3395 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3396 return; // Not a source member added to a class from PCH.
3397 if (!isa<CXXMethodDecl>(D))
3398 return; // We are interested in lazily declared implicit methods.
3399
3400 // A decl coming from PCH was modified.
3401 assert(RD->isDefinition());
3402 UpdateRecord &Record = DeclUpdates[RD];
3403 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3404 AddDeclRef(D, Record);
3405}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003406
3407void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3408 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003409 // The specializations set is kept in the canonical template.
3410 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003411 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3412 return; // Not a source specialization added to a template from PCH.
3413
3414 UpdateRecord &Record = DeclUpdates[TD];
3415 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3416 AddDeclRef(D, Record);
3417}