blob: f3852af00ff038352e145ffb1ecca0c0d0121f66 [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
Douglas Gregorc5046832009-04-27 18:38:38 +0000907 typedef std::pair<int, struct stat> data_type;
908 typedef const data_type& data_type_ref;
909
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);
919 unsigned DataLen = 1; // result value
920 if (Data.first == 0)
921 DataLen += 4 + 4 + 2 + 8 + 8;
922 clang::io::Emit8(Out, DataLen);
923 return std::make_pair(StrLen + 1, DataLen);
924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Douglas Gregorc5046832009-04-27 18:38:38 +0000926 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
927 Out.write(path, KeyLen);
928 }
Mike Stump11289f42009-09-09 15:08:12 +0000929
Douglas Gregorc5046832009-04-27 18:38:38 +0000930 void EmitData(llvm::raw_ostream& Out, key_type_ref,
931 data_type_ref Data, unsigned DataLen) {
932 using namespace clang::io;
933 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000934
Douglas Gregorc5046832009-04-27 18:38:38 +0000935 // Result of stat()
936 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000937
Douglas Gregorc5046832009-04-27 18:38:38 +0000938 if (Data.first == 0) {
939 Emit32(Out, (uint32_t) Data.second.st_ino);
940 Emit32(Out, (uint32_t) Data.second.st_dev);
941 Emit16(Out, (uint16_t) Data.second.st_mode);
942 Emit64(Out, (uint64_t) Data.second.st_mtime);
943 Emit64(Out, (uint64_t) Data.second.st_size);
944 }
945
946 assert(Out.tell() - Start == DataLen && "Wrong data length");
947 }
948};
949} // end anonymous namespace
950
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000951/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000952void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000953 // Build the on-disk hash table containing information about every
954 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000955 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000956 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000957 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000958 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000959 Stat != StatEnd; ++Stat, ++NumStatEntries) {
960 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000961 Generator.insert(Filename, Stat->second);
962 }
Mike Stump11289f42009-09-09 15:08:12 +0000963
Douglas Gregorc5046832009-04-27 18:38:38 +0000964 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000965 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000966 uint32_t BucketOffset;
967 {
968 llvm::raw_svector_ostream Out(StatCacheData);
969 // Make sure that no bucket is at offset 0
970 clang::io::Emit32(Out, 0);
971 BucketOffset = Generator.Emit(Out);
972 }
973
974 // Create a blob abbreviation
975 using namespace llvm;
976 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000977 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
981 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
982
983 // Write the stat cache
984 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000985 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +0000986 Record.push_back(BucketOffset);
987 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000988 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000989}
990
991//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000992// Source Manager Serialization
993//===----------------------------------------------------------------------===//
994
995/// \brief Create an abbreviation for the SLocEntry that refers to a
996/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000997static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000998 using namespace llvm;
999 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001000 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001005 // FileEntry fields.
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001008 // HeaderFileInfo fields.
1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001014 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001015}
1016
1017/// \brief Create an abbreviation for the SLocEntry that refers to a
1018/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001019static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001020 using namespace llvm;
1021 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001022 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001028 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001029}
1030
1031/// \brief Create an abbreviation for the SLocEntry that refers to a
1032/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001033static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001034 using namespace llvm;
1035 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001036 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001038 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001039}
1040
1041/// \brief Create an abbreviation for the SLocEntry that refers to an
1042/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001043static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001044 using namespace llvm;
1045 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001046 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001052 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001053}
1054
1055/// \brief Writes the block containing the serialized form of the
1056/// source manager.
1057///
1058/// TODO: We should probably use an on-disk hash table (stored in a
1059/// blob), indexed based on the file name, so that we only create
1060/// entries for files that we actually need. In the common case (no
1061/// errors), we probably won't have to create file entries for any of
1062/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001063void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001064 const Preprocessor &PP,
1065 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001066 RecordData Record;
1067
Chris Lattner0910e3b2009-04-10 17:16:57 +00001068 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001069 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001070
1071 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001072 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1073 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1074 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1075 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001076
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001077 // Write the line table.
1078 if (SourceMgr.hasLineTable()) {
1079 LineTableInfo &LineTable = SourceMgr.getLineTable();
1080
1081 // Emit the file names
1082 Record.push_back(LineTable.getNumFilenames());
1083 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1084 // Emit the file name
1085 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001086 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001087 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1088 Record.push_back(FilenameLen);
1089 if (FilenameLen)
1090 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1091 }
Mike Stump11289f42009-09-09 15:08:12 +00001092
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001093 // Emit the line entries
1094 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1095 L != LEnd; ++L) {
1096 // Emit the file ID
1097 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001098
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001099 // Emit the line entries
1100 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001101 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001102 LEEnd = L->second.end();
1103 LE != LEEnd; ++LE) {
1104 Record.push_back(LE->FileOffset);
1105 Record.push_back(LE->LineNo);
1106 Record.push_back(LE->FilenameID);
1107 Record.push_back((unsigned)LE->FileKind);
1108 Record.push_back(LE->IncludeOffset);
1109 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001110 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001111 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001112 }
1113
Douglas Gregor258ae542009-04-27 06:38:32 +00001114 // Write out the source location entry table. We skip the first
1115 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001116 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001117 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001118 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1119 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1120 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1121 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001122 // Get this source location entry.
1123 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001124
Douglas Gregor258ae542009-04-27 06:38:32 +00001125 // Record the offset of this source-location entry.
1126 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1127
1128 // Figure out which record code to use.
1129 unsigned Code;
1130 if (SLoc->isFile()) {
1131 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001132 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001133 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001134 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001135 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001136 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001137 Record.clear();
1138 Record.push_back(Code);
1139
1140 Record.push_back(SLoc->getOffset());
1141 if (SLoc->isFile()) {
1142 const SrcMgr::FileInfo &File = SLoc->getFile();
1143 Record.push_back(File.getIncludeLoc().getRawEncoding());
1144 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1145 Record.push_back(File.hasLineDirectives());
1146
1147 const SrcMgr::ContentCache *Content = File.getContentCache();
1148 if (Content->Entry) {
1149 // The source location entry is a file. The blob associated
1150 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001151
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001152 // Emit size/modification time for this file.
1153 Record.push_back(Content->Entry->getSize());
1154 Record.push_back(Content->Entry->getModificationTime());
1155
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001156 // Emit header-search information associated with this file.
1157 HeaderFileInfo HFI;
1158 HeaderSearch &HS = PP.getHeaderSearchInfo();
1159 if (Content->Entry->getUID() < HS.header_file_size())
1160 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1161 Record.push_back(HFI.isImport);
1162 Record.push_back(HFI.DirInfo);
1163 Record.push_back(HFI.NumIncludes);
1164 AddIdentifierRef(HFI.ControllingMacro, Record);
1165
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001166 // Turn the file name into an absolute path, if it isn't already.
1167 const char *Filename = Content->Entry->getName();
1168 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001169 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001170 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001171
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001172 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001173 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001174
1175 // FIXME: For now, preload all file source locations, so that
1176 // we get the appropriate File entries in the reader. This is
1177 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001178 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001179 } else {
1180 // The source location entry is a buffer. The blob associated
1181 // with this entry contains the contents of the buffer.
1182
1183 // We add one to the size so that we capture the trailing NULL
1184 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1185 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001186 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001187 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001188 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001189 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1190 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001191 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001192 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001193 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001194 llvm::StringRef(Buffer->getBufferStart(),
1195 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001196
1197 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001198 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001199 }
1200 } else {
1201 // The source location entry is an instantiation.
1202 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1203 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1204 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1205 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1206
1207 // Compute the token length for this macro expansion.
1208 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001209 if (I + 1 != N)
1210 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001211 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1212 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1213 }
1214 }
1215
Douglas Gregor8f45df52009-04-16 22:23:12 +00001216 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001217
1218 if (SLocEntryOffsets.empty())
1219 return;
1220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001221 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001222 // table is used for lazily loading source-location information.
1223 using namespace llvm;
1224 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001225 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1229 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001230
Douglas Gregor258ae542009-04-27 06:38:32 +00001231 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001232 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001233 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001234 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1235 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001236 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001237 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001238 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001239
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001240 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001241 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001242 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001243}
1244
Douglas Gregorc5046832009-04-27 18:38:38 +00001245//===----------------------------------------------------------------------===//
1246// Preprocessor Serialization
1247//===----------------------------------------------------------------------===//
1248
Chris Lattnereeffaef2009-04-10 17:15:23 +00001249/// \brief Writes the block containing the serialized form of the
1250/// preprocessor.
1251///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001252void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001253 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001254
Chris Lattner0af3ba12009-04-13 01:29:17 +00001255 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1256 if (PP.getCounterValue() != 0) {
1257 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001258 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001259 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001260 }
1261
1262 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001263 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001265 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001266 // FIXME: use diagnostics subsystem for localization etc.
1267 if (PP.SawDateOrTime())
1268 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001269
Douglas Gregor796d76a2010-10-20 22:00:55 +00001270
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001271 // Loop over all the macro definitions that are live at the end of the file,
1272 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001273 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001274 unsigned InclusionAbbrev = 0;
1275 if (PPRec) {
1276 using namespace llvm;
1277 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1278 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001286 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001287 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001288
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001289 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1290 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001291 // FIXME: This emits macros in hash table order, we should do it in a stable
1292 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001293 MacroInfo *MI = I->second;
1294
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001295 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001296 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001297 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001298
1299 // FIXME: There is a (probably minor) optimization we could do here, if
1300 // the macro comes from the original PCH but the identifier comes from a
1301 // chained PCH, by storing the offset into the original PCH rather than
1302 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001303 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001304 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001305 continue;
1306
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001307 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001308 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001309 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1310 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001311
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001312 unsigned Code;
1313 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001314 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001315 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001316 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001317
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001318 Record.push_back(MI->isC99Varargs());
1319 Record.push_back(MI->isGNUVarargs());
1320 Record.push_back(MI->getNumArgs());
1321 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1322 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001323 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001324 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001325
Douglas Gregoraae92242010-03-19 21:51:54 +00001326 // If we have a detailed preprocessing record, record the macro definition
1327 // ID that corresponds to this macro.
1328 if (PPRec)
1329 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001330
Douglas Gregor8f45df52009-04-16 22:23:12 +00001331 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001332 Record.clear();
1333
Chris Lattner2199f5b2009-04-10 18:08:30 +00001334 // Emit the tokens array.
1335 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1336 // Note that we know that the preprocessor does not have any annotation
1337 // tokens in it because they are created by the parser, and thus can't be
1338 // in a macro definition.
1339 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001340
Chris Lattner2199f5b2009-04-10 18:08:30 +00001341 Record.push_back(Tok.getLocation().getRawEncoding());
1342 Record.push_back(Tok.getLength());
1343
Chris Lattner2199f5b2009-04-10 18:08:30 +00001344 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1345 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001346 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001347
Chris Lattner2199f5b2009-04-10 18:08:30 +00001348 // FIXME: Should translate token kind to a stable encoding.
1349 Record.push_back(Tok.getKind());
1350 // FIXME: Should translate token flags to a stable encoding.
1351 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001352
Sebastian Redl539c5062010-08-18 23:57:32 +00001353 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001354 Record.clear();
1355 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001356 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001357 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001358
Douglas Gregoraae92242010-03-19 21:51:54 +00001359 // If the preprocessor has a preprocessing record, emit it.
1360 unsigned NumPreprocessingRecords = 0;
1361 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001362 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001363 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1364 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001365 E != EEnd; ++E) {
1366 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001367
Douglas Gregoraae92242010-03-19 21:51:54 +00001368 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001369 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001370 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1371 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1372 AddIdentifierRef(MI->getName(), Record);
1373 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
Sebastian Redl539c5062010-08-18 23:57:32 +00001374 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001375 continue;
1376 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001377
Douglas Gregoraae92242010-03-19 21:51:54 +00001378 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1379 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001380 MacroID ID = getMacroDefinitionID(MD);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001381
Douglas Gregor91096292010-10-02 19:29:26 +00001382 // Don't write the macro definition if it is from another AST file.
1383 if (ID < FirstMacroID)
1384 continue;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001385
Douglas Gregor91096292010-10-02 19:29:26 +00001386 unsigned Position = ID - FirstMacroID;
1387 if (Position != MacroDefinitionOffsets.size()) {
1388 if (Position > MacroDefinitionOffsets.size())
1389 MacroDefinitionOffsets.resize(Position + 1);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001390
1391 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001392 } else
1393 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001394
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001395 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001396 Record.push_back(ID);
1397 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1398 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1399 AddIdentifierRef(MD->getName(), Record);
1400 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001401 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001402 continue;
1403 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001404
Douglas Gregor796d76a2010-10-20 22:00:55 +00001405 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1406 Record.push_back(PP_INCLUSION_DIRECTIVE);
1407 Record.push_back(IndexBase + NumPreprocessingRecords++);
1408 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1409 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1410 Record.push_back(ID->getFileName().size());
1411 Record.push_back(ID->wasInQuotes());
1412 Record.push_back(static_cast<unsigned>(ID->getKind()));
1413 llvm::SmallString<64> Buffer;
1414 Buffer += ID->getFileName();
1415 Buffer += ID->getFile()->getName();
1416 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1417 continue;
1418 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001419 }
1420 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001421
Douglas Gregor8f45df52009-04-16 22:23:12 +00001422 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001423
Douglas Gregoraae92242010-03-19 21:51:54 +00001424 // Write the offsets table for the preprocessing record.
1425 if (NumPreprocessingRecords > 0) {
1426 // Write the offsets table for identifier IDs.
1427 using namespace llvm;
1428 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001429 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1433 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001434
Douglas Gregoraae92242010-03-19 21:51:54 +00001435 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001436 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001437 Record.push_back(NumPreprocessingRecords);
1438 Record.push_back(MacroDefinitionOffsets.size());
1439 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001440 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001441 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1442 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001443}
1444
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001445void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1446 RecordData Record;
1447 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
1448 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i);
1449 if (Map & 0x8) { // user mapping.
1450 Record.push_back(i);
1451 Record.push_back(Map & 0x7);
1452 }
1453 }
1454
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001455 if (!Record.empty())
1456 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001457}
1458
Douglas Gregorc5046832009-04-27 18:38:38 +00001459//===----------------------------------------------------------------------===//
1460// Type Serialization
1461//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001462
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001463/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001464void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001465 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001466 if (Idx.getIndex() == 0) // we haven't seen this type before.
1467 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001468
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001469 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001470
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001471 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001472 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001473 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001474 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001475 else if (TypeOffsets.size() < Index) {
1476 TypeOffsets.resize(Index + 1);
1477 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001478 }
1479
1480 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001481
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001482 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001483 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001484
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001485 if (T.hasLocalNonFastQualifiers()) {
1486 Qualifiers Qs = T.getLocalQualifiers();
1487 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001488 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001489 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001490 } else {
1491 switch (T->getTypeClass()) {
1492 // For all of the concrete, non-dependent types, call the
1493 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001494#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001495 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001496#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001497#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001498 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001499 }
1500
1501 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001502 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001503
1504 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001505 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001506}
1507
Douglas Gregorc5046832009-04-27 18:38:38 +00001508//===----------------------------------------------------------------------===//
1509// Declaration Serialization
1510//===----------------------------------------------------------------------===//
1511
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001512/// \brief Write the block containing all of the declaration IDs
1513/// lexically declared within the given DeclContext.
1514///
1515/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1516/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001517uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001518 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001519 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001520 return 0;
1521
Douglas Gregor8f45df52009-04-16 22:23:12 +00001522 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001523 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001524 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001525 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001526 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1527 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001528 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001529
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001530 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001531 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001532 reinterpret_cast<char*>(Decls.data()),
1533 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001534 return Offset;
1535}
1536
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001537void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001538 using namespace llvm;
1539 RecordData Record;
1540
1541 // Write the type offsets array
1542 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001543 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1546 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1547 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001548 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001549 Record.push_back(TypeOffsets.size());
1550 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001551 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001552 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1553
1554 // Write the declaration offsets array
1555 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001556 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1559 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1560 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001561 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001562 Record.push_back(DeclOffsets.size());
1563 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001564 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001565 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1566}
1567
Douglas Gregorc5046832009-04-27 18:38:38 +00001568//===----------------------------------------------------------------------===//
1569// Global Method Pool and Selector Serialization
1570//===----------------------------------------------------------------------===//
1571
Douglas Gregore84a9da2009-04-20 20:36:09 +00001572namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001573// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001574class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001575 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001576
1577public:
1578 typedef Selector key_type;
1579 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001580
Sebastian Redl834bb972010-08-04 17:20:04 +00001581 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001582 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001583 ObjCMethodList Instance, Factory;
1584 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001585 typedef const data_type& data_type_ref;
1586
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001587 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001588
Douglas Gregorc78d3462009-04-24 21:10:55 +00001589 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001590 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001591 }
Mike Stump11289f42009-09-09 15:08:12 +00001592
1593 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001594 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1595 data_type_ref Methods) {
1596 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1597 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001598 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1599 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001600 Method = Method->Next)
1601 if (Method->Method)
1602 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001603 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001604 Method = Method->Next)
1605 if (Method->Method)
1606 DataLen += 4;
1607 clang::io::Emit16(Out, DataLen);
1608 return std::make_pair(KeyLen, DataLen);
1609 }
Mike Stump11289f42009-09-09 15:08:12 +00001610
Douglas Gregor95c13f52009-04-25 17:48:32 +00001611 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001612 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001613 assert((Start >> 32) == 0 && "Selector key offset too large");
1614 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001615 unsigned N = Sel.getNumArgs();
1616 clang::io::Emit16(Out, N);
1617 if (N == 0)
1618 N = 1;
1619 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001620 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001621 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1622 }
Mike Stump11289f42009-09-09 15:08:12 +00001623
Douglas Gregorc78d3462009-04-24 21:10:55 +00001624 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001625 data_type_ref Methods, unsigned DataLen) {
1626 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001627 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001628 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001629 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001630 Method = Method->Next)
1631 if (Method->Method)
1632 ++NumInstanceMethods;
1633
1634 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001635 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001636 Method = Method->Next)
1637 if (Method->Method)
1638 ++NumFactoryMethods;
1639
1640 clang::io::Emit16(Out, NumInstanceMethods);
1641 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001642 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001643 Method = Method->Next)
1644 if (Method->Method)
1645 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001646 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001647 Method = Method->Next)
1648 if (Method->Method)
1649 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001650
1651 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001652 }
1653};
1654} // end anonymous namespace
1655
Sebastian Redla19a67f2010-08-03 21:58:15 +00001656/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001657///
1658/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001659/// in an on-disk hash table indexed by the selector. The hash table also
1660/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001661void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001662 using namespace llvm;
1663
Sebastian Redla19a67f2010-08-03 21:58:15 +00001664 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001665 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001666 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001667 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001668 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001669 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001670 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001671 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001672
Sebastian Redla19a67f2010-08-03 21:58:15 +00001673 // Create the on-disk hash table representation. We walk through every
1674 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001675 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001676 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001677 I = SelectorIDs.begin(), E = SelectorIDs.end();
1678 I != E; ++I) {
1679 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001680 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001681 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001682 I->second,
1683 ObjCMethodList(),
1684 ObjCMethodList()
1685 };
1686 if (F != SemaRef.MethodPool.end()) {
1687 Data.Instance = F->second.first;
1688 Data.Factory = F->second.second;
1689 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001690 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001691 // changed.
1692 if (Chain && I->second < FirstSelectorID) {
1693 // Selector already exists. Did it change?
1694 bool changed = false;
1695 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1696 M = M->Next) {
1697 if (M->Method->getPCHLevel() == 0)
1698 changed = true;
1699 }
1700 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1701 M = M->Next) {
1702 if (M->Method->getPCHLevel() == 0)
1703 changed = true;
1704 }
1705 if (!changed)
1706 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001707 } else if (Data.Instance.Method || Data.Factory.Method) {
1708 // A new method pool entry.
1709 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001710 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001711 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001712 }
1713
Douglas Gregorc78d3462009-04-24 21:10:55 +00001714 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001715 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001716 uint32_t BucketOffset;
1717 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001718 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001719 llvm::raw_svector_ostream Out(MethodPool);
1720 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001721 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001722 BucketOffset = Generator.Emit(Out, Trait);
1723 }
1724
1725 // Create a blob abbreviation
1726 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1731 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1732
Douglas Gregor95c13f52009-04-25 17:48:32 +00001733 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001734 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001735 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001736 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001737 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001738 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001739
1740 // Create a blob abbreviation for the selector table offsets.
1741 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001742 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1745 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1746
1747 // Write the selector offsets table.
1748 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001749 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001750 Record.push_back(SelectorOffsets.size());
1751 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001752 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001753 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001754 }
1755}
1756
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001757/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001758void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001759 using namespace llvm;
1760 if (SemaRef.ReferencedSelectors.empty())
1761 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001762
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001763 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001764
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001765 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001766 // very tricky to fix, and given that @selector shouldn't really appear in
1767 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001768 for (DenseMap<Selector, SourceLocation>::iterator S =
1769 SemaRef.ReferencedSelectors.begin(),
1770 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1771 Selector Sel = (*S).first;
1772 SourceLocation Loc = (*S).second;
1773 AddSelectorRef(Sel, Record);
1774 AddSourceLocation(Loc, Record);
1775 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001776 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001777}
1778
Douglas Gregorc5046832009-04-27 18:38:38 +00001779//===----------------------------------------------------------------------===//
1780// Identifier Table Serialization
1781//===----------------------------------------------------------------------===//
1782
Douglas Gregorc78d3462009-04-24 21:10:55 +00001783namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001784class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001785 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001786 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001787
Douglas Gregor1d583f22009-04-28 21:18:29 +00001788 /// \brief Determines whether this is an "interesting" identifier
1789 /// that needs a full IdentifierInfo structure written into the hash
1790 /// table.
1791 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1792 return II->isPoisoned() ||
1793 II->isExtensionToken() ||
1794 II->hasMacroDefinition() ||
1795 II->getObjCOrBuiltinID() ||
1796 II->getFETokenInfo<void>();
1797 }
1798
Douglas Gregore84a9da2009-04-20 20:36:09 +00001799public:
1800 typedef const IdentifierInfo* key_type;
1801 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001802
Sebastian Redl539c5062010-08-18 23:57:32 +00001803 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001804 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001805
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001806 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001807 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001808
1809 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001810 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001811 }
Mike Stump11289f42009-09-09 15:08:12 +00001812
1813 std::pair<unsigned,unsigned>
1814 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001815 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001816 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001817 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1818 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001819 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001820 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001821 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001822 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001823 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1824 DEnd = IdentifierResolver::end();
1825 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001826 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001827 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001828 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001829 // We emit the key length after the data length so that every
1830 // string is preceded by a 16-bit length. This matches the PTH
1831 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001832 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001833 return std::make_pair(KeyLen, DataLen);
1834 }
Mike Stump11289f42009-09-09 15:08:12 +00001835
1836 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001837 unsigned KeyLen) {
1838 // Record the location of the key data. This is used when generating
1839 // the mapping from persistent IDs to strings.
1840 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001841 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001842 }
Mike Stump11289f42009-09-09 15:08:12 +00001843
1844 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001845 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001846 if (!isInterestingIdentifier(II)) {
1847 clang::io::Emit32(Out, ID << 1);
1848 return;
1849 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001850
Douglas Gregor1d583f22009-04-28 21:18:29 +00001851 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001852 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001853 bool hasMacroDefinition =
1854 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001855 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001856 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001857 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1858 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1859 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001860 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001861 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001862 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001863
Douglas Gregorc3366a52009-04-21 23:56:24 +00001864 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001865 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001866
Douglas Gregora868bbd2009-04-21 22:25:48 +00001867 // Emit the declaration IDs in reverse order, because the
1868 // IdentifierResolver provides the declarations as they would be
1869 // visible (e.g., the function "stat" would come before the struct
1870 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1871 // adds declarations to the end of the list (so we need to see the
1872 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001873 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001874 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001875 IdentifierResolver::end());
1876 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1877 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001878 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001879 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001880 }
1881};
1882} // end anonymous namespace
1883
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001884/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001885///
1886/// The identifier table consists of a blob containing string data
1887/// (the actual identifiers themselves) and a separate "offsets" index
1888/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001889void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001890 using namespace llvm;
1891
1892 // Create and write out the blob that contains the identifier
1893 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001894 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001895 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001896 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001897
Douglas Gregore6648fb2009-04-28 20:33:11 +00001898 // Look for any identifiers that were named while processing the
1899 // headers, but are otherwise not needed. We add these to the hash
1900 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001901 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001902 // file.
1903 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1904 IDEnd = PP.getIdentifierTable().end();
1905 ID != IDEnd; ++ID)
1906 getIdentifierRef(ID->second);
1907
Sebastian Redlff4a2952010-07-23 23:49:55 +00001908 // Create the on-disk hash table representation. We only store offsets
1909 // for identifiers that appear here for the first time.
1910 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001911 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001912 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1913 ID != IDEnd; ++ID) {
1914 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001915 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001916 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001917 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001918
Douglas Gregore84a9da2009-04-20 20:36:09 +00001919 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001920 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001921 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001922 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001923 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001924 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001925 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001926 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001927 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001928 }
1929
1930 // Create a blob abbreviation
1931 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001932 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001935 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001936
1937 // Write the identifier table
1938 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001939 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001940 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001941 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001942 }
1943
1944 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001945 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001946 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1949 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1950
1951 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001952 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001953 Record.push_back(IdentifierOffsets.size());
1954 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001955 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001956 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001957}
1958
Douglas Gregorc5046832009-04-27 18:38:38 +00001959//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001960// DeclContext's Name Lookup Table Serialization
1961//===----------------------------------------------------------------------===//
1962
1963namespace {
1964// Trait used for the on-disk hash table used in the method pool.
1965class ASTDeclContextNameLookupTrait {
1966 ASTWriter &Writer;
1967
1968public:
1969 typedef DeclarationName key_type;
1970 typedef key_type key_type_ref;
1971
1972 typedef DeclContext::lookup_result data_type;
1973 typedef const data_type& data_type_ref;
1974
1975 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1976
1977 unsigned ComputeHash(DeclarationName Name) {
1978 llvm::FoldingSetNodeID ID;
1979 ID.AddInteger(Name.getNameKind());
1980
1981 switch (Name.getNameKind()) {
1982 case DeclarationName::Identifier:
1983 ID.AddString(Name.getAsIdentifierInfo()->getName());
1984 break;
1985 case DeclarationName::ObjCZeroArgSelector:
1986 case DeclarationName::ObjCOneArgSelector:
1987 case DeclarationName::ObjCMultiArgSelector:
1988 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1989 break;
1990 case DeclarationName::CXXConstructorName:
1991 case DeclarationName::CXXDestructorName:
1992 case DeclarationName::CXXConversionFunctionName:
1993 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1994 break;
1995 case DeclarationName::CXXOperatorName:
1996 ID.AddInteger(Name.getCXXOverloadedOperator());
1997 break;
1998 case DeclarationName::CXXLiteralOperatorName:
1999 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2000 case DeclarationName::CXXUsingDirective:
2001 break;
2002 }
2003
2004 return ID.ComputeHash();
2005 }
2006
2007 std::pair<unsigned,unsigned>
2008 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2009 data_type_ref Lookup) {
2010 unsigned KeyLen = 1;
2011 switch (Name.getNameKind()) {
2012 case DeclarationName::Identifier:
2013 case DeclarationName::ObjCZeroArgSelector:
2014 case DeclarationName::ObjCOneArgSelector:
2015 case DeclarationName::ObjCMultiArgSelector:
2016 case DeclarationName::CXXConstructorName:
2017 case DeclarationName::CXXDestructorName:
2018 case DeclarationName::CXXConversionFunctionName:
2019 case DeclarationName::CXXLiteralOperatorName:
2020 KeyLen += 4;
2021 break;
2022 case DeclarationName::CXXOperatorName:
2023 KeyLen += 1;
2024 break;
2025 case DeclarationName::CXXUsingDirective:
2026 break;
2027 }
2028 clang::io::Emit16(Out, KeyLen);
2029
2030 // 2 bytes for num of decls and 4 for each DeclID.
2031 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2032 clang::io::Emit16(Out, DataLen);
2033
2034 return std::make_pair(KeyLen, DataLen);
2035 }
2036
2037 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2038 using namespace clang::io;
2039
2040 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2041 Emit8(Out, Name.getNameKind());
2042 switch (Name.getNameKind()) {
2043 case DeclarationName::Identifier:
2044 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2045 break;
2046 case DeclarationName::ObjCZeroArgSelector:
2047 case DeclarationName::ObjCOneArgSelector:
2048 case DeclarationName::ObjCMultiArgSelector:
2049 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2050 break;
2051 case DeclarationName::CXXConstructorName:
2052 case DeclarationName::CXXDestructorName:
2053 case DeclarationName::CXXConversionFunctionName:
2054 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2055 break;
2056 case DeclarationName::CXXOperatorName:
2057 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2058 Emit8(Out, Name.getCXXOverloadedOperator());
2059 break;
2060 case DeclarationName::CXXLiteralOperatorName:
2061 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2062 break;
2063 case DeclarationName::CXXUsingDirective:
2064 break;
2065 }
2066 }
2067
2068 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2069 data_type Lookup, unsigned DataLen) {
2070 uint64_t Start = Out.tell(); (void)Start;
2071 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2072 for (; Lookup.first != Lookup.second; ++Lookup.first)
2073 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2074
2075 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2076 }
2077};
2078} // end anonymous namespace
2079
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002080/// \brief Write the block containing all of the declaration IDs
2081/// visible from the given DeclContext.
2082///
2083/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002084/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002085uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2086 DeclContext *DC) {
2087 if (DC->getPrimaryContext() != DC)
2088 return 0;
2089
2090 // Since there is no name lookup into functions or methods, don't bother to
2091 // build a visible-declarations table for these entities.
2092 if (DC->isFunctionOrMethod())
2093 return 0;
2094
2095 // If not in C++, we perform name lookup for the translation unit via the
2096 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2097 // FIXME: In C++ we need the visible declarations in order to "see" the
2098 // friend declarations, is there a way to do this without writing the table ?
2099 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2100 return 0;
2101
2102 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002103 if (DC->hasExternalVisibleStorage())
2104 DC->MaterializeVisibleDeclsFromExternalStorage();
2105 else
2106 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002107
2108 // Serialize the contents of the mapping used for lookup. Note that,
2109 // although we have two very different code paths, the serialized
2110 // representation is the same for both cases: a declaration name,
2111 // followed by a size, followed by references to the visible
2112 // declarations that have that name.
2113 uint64_t Offset = Stream.GetCurrentBitNo();
2114 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2115 if (!Map || Map->empty())
2116 return 0;
2117
2118 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2119 ASTDeclContextNameLookupTrait Trait(*this);
2120
2121 // Create the on-disk hash table representation.
2122 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2123 D != DEnd; ++D) {
2124 DeclarationName Name = D->first;
2125 DeclContext::lookup_result Result = D->second.getLookupResult();
2126 Generator.insert(Name, Result, Trait);
2127 }
2128
2129 // Create the on-disk hash table in a buffer.
2130 llvm::SmallString<4096> LookupTable;
2131 uint32_t BucketOffset;
2132 {
2133 llvm::raw_svector_ostream Out(LookupTable);
2134 // Make sure that no bucket is at offset 0
2135 clang::io::Emit32(Out, 0);
2136 BucketOffset = Generator.Emit(Out, Trait);
2137 }
2138
2139 // Write the lookup table
2140 RecordData Record;
2141 Record.push_back(DECL_CONTEXT_VISIBLE);
2142 Record.push_back(BucketOffset);
2143 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2144 LookupTable.str());
2145
2146 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2147 ++NumVisibleDeclContexts;
2148 return Offset;
2149}
2150
Sebastian Redla4071b42010-08-24 00:50:09 +00002151/// \brief Write an UPDATE_VISIBLE block for the given context.
2152///
2153/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2154/// DeclContext in a dependent AST file. As such, they only exist for the TU
2155/// (in C++) and for namespaces.
2156void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002157 // Make the context build its lookup table, but don't make it load external
2158 // decls.
2159 DC->lookup(DeclarationName());
2160
2161 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2162 if (!Map || Map->empty())
2163 return;
2164
2165 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2166 ASTDeclContextNameLookupTrait Trait(*this);
2167
2168 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002169 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2170 D != DEnd; ++D) {
2171 DeclarationName Name = D->first;
2172 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002173 // For any name that appears in this table, the results are complete, i.e.
2174 // they overwrite results from previous PCHs. Merging is always a mess.
2175 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002176 }
2177
2178 // Create the on-disk hash table in a buffer.
2179 llvm::SmallString<4096> LookupTable;
2180 uint32_t BucketOffset;
2181 {
2182 llvm::raw_svector_ostream Out(LookupTable);
2183 // Make sure that no bucket is at offset 0
2184 clang::io::Emit32(Out, 0);
2185 BucketOffset = Generator.Emit(Out, Trait);
2186 }
2187
2188 // Write the lookup table
2189 RecordData Record;
2190 Record.push_back(UPDATE_VISIBLE);
2191 Record.push_back(getDeclID(cast<Decl>(DC)));
2192 Record.push_back(BucketOffset);
2193 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2194}
2195
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002196//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002197// General Serialization Routines
2198//===----------------------------------------------------------------------===//
2199
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002200/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002201void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002202 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002203 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2204 const Attr * A = *i;
2205 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2206 AddSourceLocation(A->getLocation(), Record);
2207 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002208
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002209#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002210
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002211 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002212}
2213
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002214void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002215 Record.push_back(Str.size());
2216 Record.insert(Record.end(), Str.begin(), Str.end());
2217}
2218
Douglas Gregore84a9da2009-04-20 20:36:09 +00002219/// \brief Note that the identifier II occurs at the given offset
2220/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002221void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002222 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002223 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002224 // up earlier in the chain and thus don't need an offset.
2225 if (ID >= FirstIdentID)
2226 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002227}
2228
Douglas Gregor95c13f52009-04-25 17:48:32 +00002229/// \brief Note that the selector Sel occurs at the given offset
2230/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002231void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002232 unsigned ID = SelectorIDs[Sel];
2233 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002234 // Don't record offsets for selectors that are also available in a different
2235 // file.
2236 if (ID < FirstSelectorID)
2237 return;
2238 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002239}
2240
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002241ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redld95a56e2010-08-04 18:21:41 +00002242 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002243 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002244 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002245 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2246 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002247 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002248 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2249 NextCXXBaseSpecifiersID(1)
2250{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002251}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002252
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002253void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002254 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002255 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002256 Stream.Emit((unsigned)'C', 8);
2257 Stream.Emit((unsigned)'P', 8);
2258 Stream.Emit((unsigned)'C', 8);
2259 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002260
Chris Lattner28fa4e62009-04-26 22:26:21 +00002261 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002262
Sebastian Redl143413f2010-07-12 22:02:52 +00002263 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002264 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002265 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002266 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002267}
2268
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002269void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002270 const char *isysroot) {
2271 using namespace llvm;
2272
2273 ASTContext &Context = SemaRef.Context;
2274 Preprocessor &PP = SemaRef.PP;
2275
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002276 // The translation unit is the first declaration we'll emit.
2277 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002278 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002279 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002280
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002281 // Make sure that we emit IdentifierInfos (and any attached
2282 // declarations) for builtins.
2283 {
2284 IdentifierTable &Table = PP.getIdentifierTable();
2285 llvm::SmallVector<const char *, 32> BuiltinNames;
2286 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2287 Context.getLangOptions().NoBuiltin);
2288 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2289 getIdentifierRef(&Table.get(BuiltinNames[I]));
2290 }
2291
Chris Lattner0c797362009-09-08 18:19:27 +00002292 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002293 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002294 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002295 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002296 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2297 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002298 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002299
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002300 // Build a record containing all of the file scoped decls in this file.
2301 RecordData UnusedFileScopedDecls;
2302 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2303 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002304
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002305 RecordData WeakUndeclaredIdentifiers;
2306 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2307 WeakUndeclaredIdentifiers.push_back(
2308 SemaRef.WeakUndeclaredIdentifiers.size());
2309 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2310 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2311 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2312 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2313 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2314 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2315 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2316 }
2317 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002318
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002319 // Build a record containing all of the locally-scoped external
2320 // declarations in this header file. Generally, this record will be
2321 // empty.
2322 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002323 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002324 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002325 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002326 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2327 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2328 TD != TDEnd; ++TD)
2329 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2330
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002331 // Build a record containing all of the ext_vector declarations.
2332 RecordData ExtVectorDecls;
2333 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2334 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2335
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002336 // Build a record containing all of the VTable uses information.
2337 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002338 if (!SemaRef.VTableUses.empty()) {
2339 VTableUses.push_back(SemaRef.VTableUses.size());
2340 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2341 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2342 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2343 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2344 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002345 }
2346
2347 // Build a record containing all of dynamic classes declarations.
2348 RecordData DynamicClasses;
2349 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2350 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2351
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002352 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002353 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002354 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002355 I = SemaRef.PendingInstantiations.begin(),
2356 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2357 AddDeclRef(I->first, PendingInstantiations);
2358 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002359 }
2360 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2361 "There are local ones at end of translation unit!");
2362
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002363 // Build a record containing some declaration references.
2364 RecordData SemaDeclRefs;
2365 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2366 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2367 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2368 }
2369
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002370 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002371 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002372 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002373 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002374 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002375 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002376 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002377 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002378 // Write the record of special types.
2379 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002380
Steve Naroffc277ad12009-07-18 15:33:26 +00002381 AddTypeRef(Context.getBuiltinVaListType(), Record);
2382 AddTypeRef(Context.getObjCIdType(), Record);
2383 AddTypeRef(Context.getObjCSelType(), Record);
2384 AddTypeRef(Context.getObjCProtoType(), Record);
2385 AddTypeRef(Context.getObjCClassType(), Record);
2386 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2387 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2388 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002389 AddTypeRef(Context.getjmp_bufType(), Record);
2390 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002391 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2392 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002393 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002394 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002395 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2396 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002397 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002398 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002399
Douglas Gregor1970d882009-04-26 03:49:13 +00002400 // Keep writing types and declarations until all types and
2401 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002402 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002403 WriteDeclsBlockAbbrevs();
2404 while (!DeclTypesToEmit.empty()) {
2405 DeclOrType DOT = DeclTypesToEmit.front();
2406 DeclTypesToEmit.pop();
2407 if (DOT.isType())
2408 WriteType(DOT.getType());
2409 else
2410 WriteDecl(Context, DOT.getDecl());
2411 }
2412 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002413
Douglas Gregor45053152009-10-17 17:25:45 +00002414 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002415 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002416 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002417 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002418
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002419 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002420 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002421
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002422 // Write the C++ base-specifier set offsets.
2423 if (!CXXBaseSpecifiersOffsets.empty()) {
2424 // Create a blob abbreviation for the C++ base specifiers offsets.
2425 using namespace llvm;
2426
2427 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2428 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2431 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2432
2433 // Write the selector offsets table.
2434 Record.clear();
2435 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2436 Record.push_back(CXXBaseSpecifiersOffsets.size());
2437 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2438 (const char *)CXXBaseSpecifiersOffsets.data(),
2439 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2440 }
2441
Douglas Gregord4df8652009-04-22 22:02:47 +00002442 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002443 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002444 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002445
2446 // Write the record containing tentative definitions.
2447 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002448 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002449
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002450 // Write the record containing unused file scoped decls.
2451 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002452 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002453
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002454 // Write the record containing weak undeclared identifiers.
2455 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002456 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002457 WeakUndeclaredIdentifiers);
2458
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002459 // Write the record containing locally-scoped external definitions.
2460 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002461 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002462 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002463
2464 // Write the record containing ext_vector type names.
2465 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002466 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002467
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002468 // Write the record containing VTable uses information.
2469 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002470 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002471
2472 // Write the record containing dynamic classes declarations.
2473 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002474 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002475
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002476 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002477 if (!PendingInstantiations.empty())
2478 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002479
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002480 // Write the record containing declaration references of Sema.
2481 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002482 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002483
Douglas Gregor08f01292009-04-17 22:13:46 +00002484 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002485 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002486 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002487 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002488 Record.push_back(NumLexicalDeclContexts);
2489 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002490 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002491 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002492}
2493
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002494void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002495 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002496 using namespace llvm;
2497
2498 ASTContext &Context = SemaRef.Context;
2499 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002500
Sebastian Redl143413f2010-07-12 22:02:52 +00002501 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002502 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002503 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002504 if (StatCalls && !isysroot)
2505 WriteStatCache(*StatCalls);
2506 // FIXME: Source manager block should only write new stuff, which could be
2507 // done by tracking the largest ID in the chain
2508 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002509
2510 // The special types are in the chained PCH.
2511
2512 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002513 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002514 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002515 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002516 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2517 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002518 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002519 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002520 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002521 else if ((*I)->isChangedSinceDeserialization())
2522 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002523 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002524 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002525 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002526 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002527 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2528 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2529 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002530 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002531 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2532 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002533 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002534 // And a visible updates block for the DeclContexts.
2535 Abv = new llvm::BitCodeAbbrev();
2536 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2537 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2538 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2539 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2540 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2541 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002542
Sebastian Redl98912122010-07-27 23:01:28 +00002543 // Build a record containing all of the new tentative definitions in this
2544 // file, in TentativeDefinitions order.
2545 RecordData TentativeDefinitions;
2546 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2547 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2548 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2549 }
2550
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002551 // Build a record containing all of the file scoped decls in this file.
2552 RecordData UnusedFileScopedDecls;
2553 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2554 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2555 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002556 }
2557
Sebastian Redl08aca90252010-08-05 18:21:25 +00002558 // We write the entire table, overwriting the tables from the chain.
2559 RecordData WeakUndeclaredIdentifiers;
2560 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2561 WeakUndeclaredIdentifiers.push_back(
2562 SemaRef.WeakUndeclaredIdentifiers.size());
2563 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2564 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2565 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2566 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2567 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2568 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2569 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2570 }
2571 }
2572
Sebastian Redl98912122010-07-27 23:01:28 +00002573 // Build a record containing all of the locally-scoped external
2574 // declarations in this header file. Generally, this record will be
2575 // empty.
2576 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002577 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002578 // nondeterminstic!
2579 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2580 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2581 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2582 TD != TDEnd; ++TD) {
2583 if (TD->second->getPCHLevel() == 0)
2584 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2585 }
2586
2587 // Build a record containing all of the ext_vector declarations.
2588 RecordData ExtVectorDecls;
2589 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2590 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2591 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2592 }
2593
Sebastian Redl08aca90252010-08-05 18:21:25 +00002594 // Build a record containing all of the VTable uses information.
2595 // We write everything here, because it's too hard to determine whether
2596 // a use is new to this part.
2597 RecordData VTableUses;
2598 if (!SemaRef.VTableUses.empty()) {
2599 VTableUses.push_back(SemaRef.VTableUses.size());
2600 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2601 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2602 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2603 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2604 }
2605 }
2606
2607 // Build a record containing all of dynamic classes declarations.
2608 RecordData DynamicClasses;
2609 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2610 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2611 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2612
2613 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002614 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002615 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002616 I = SemaRef.PendingInstantiations.begin(),
2617 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002618 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002619 AddDeclRef(I->first, PendingInstantiations);
2620 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002621 }
2622 }
2623 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2624 "There are local ones at end of translation unit!");
2625
2626 // Build a record containing some declaration references.
2627 // It's not worth the effort to avoid duplication here.
2628 RecordData SemaDeclRefs;
2629 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2630 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2631 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2632 }
2633
Sebastian Redl539c5062010-08-18 23:57:32 +00002634 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002635 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002636 for (DeclsToRewriteTy::iterator
2637 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2638 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002639 while (!DeclTypesToEmit.empty()) {
2640 DeclOrType DOT = DeclTypesToEmit.front();
2641 DeclTypesToEmit.pop();
2642 if (DOT.isType())
2643 WriteType(DOT.getType());
2644 else
2645 WriteDecl(Context, DOT.getDecl());
2646 }
2647 Stream.ExitBlock();
2648
Sebastian Redl98912122010-07-27 23:01:28 +00002649 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002650 WriteSelectors(SemaRef);
2651 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002652 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002653 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002654 // FIXME: For chained PCH only write the new mappings (we currently
2655 // write all of them again).
2656 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002657
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002658 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002659 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002660 RecordData FirstLatestDeclIDs;
2661 for (FirstLatestDeclMap::iterator
2662 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2663 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2664 "Expected first & second to be in different PCHs");
2665 AddDeclRef(I->first, FirstLatestDeclIDs);
2666 AddDeclRef(I->second, FirstLatestDeclIDs);
2667 }
2668 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002669 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002670
Sebastian Redl98912122010-07-27 23:01:28 +00002671 // Write the record containing external, unnamed definitions.
2672 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002673 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002674
2675 // Write the record containing tentative definitions.
2676 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002677 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002678
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002679 // Write the record containing unused file scoped decls.
2680 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002681 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002682
Sebastian Redl08aca90252010-08-05 18:21:25 +00002683 // Write the record containing weak undeclared identifiers.
2684 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002685 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002686 WeakUndeclaredIdentifiers);
2687
Sebastian Redl98912122010-07-27 23:01:28 +00002688 // Write the record containing locally-scoped external definitions.
2689 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002690 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002691 LocallyScopedExternalDecls);
2692
2693 // Write the record containing ext_vector type names.
2694 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002695 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002696
Sebastian Redl08aca90252010-08-05 18:21:25 +00002697 // Write the record containing VTable uses information.
2698 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002699 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002700
2701 // Write the record containing dynamic classes declarations.
2702 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002703 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002704
2705 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002706 if (!PendingInstantiations.empty())
2707 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002708
2709 // Write the record containing declaration references of Sema.
2710 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002711 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002712
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002713 // Write the updates to DeclContexts.
2714 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2715 I = UpdatedDeclContexts.begin(),
2716 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002717 I != E; ++I)
2718 WriteDeclContextVisibleUpdate(*I);
2719
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002720 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002721
Sebastian Redl98912122010-07-27 23:01:28 +00002722 Record.clear();
2723 Record.push_back(NumStatements);
2724 Record.push_back(NumMacros);
2725 Record.push_back(NumLexicalDeclContexts);
2726 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002727 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002728 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002729 Stream.ExitBlock();
2730}
2731
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002732void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002733 if (DeclUpdates.empty())
2734 return;
2735
2736 RecordData OffsetsRecord;
2737 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2738 for (DeclUpdateMap::iterator
2739 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2740 const Decl *D = I->first;
2741 UpdateRecord &URec = I->second;
2742
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002743 if (DeclsToRewrite.count(D))
2744 continue; // The decl will be written completely,no need to store updates.
2745
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002746 uint64_t Offset = Stream.GetCurrentBitNo();
2747 Stream.EmitRecord(DECL_UPDATES, URec);
2748
2749 OffsetsRecord.push_back(GetDeclRef(D));
2750 OffsetsRecord.push_back(Offset);
2751 }
2752 Stream.ExitBlock();
2753 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2754}
2755
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002756void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002757 if (ReplacedDecls.empty())
2758 return;
2759
2760 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002761 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002762 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2763 Record.push_back(I->first);
2764 Record.push_back(I->second);
2765 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002766 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002767}
2768
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002769void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002770 Record.push_back(Loc.getRawEncoding());
2771}
2772
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002773void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002774 AddSourceLocation(Range.getBegin(), Record);
2775 AddSourceLocation(Range.getEnd(), Record);
2776}
2777
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002778void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002779 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002780 const uint64_t *Words = Value.getRawData();
2781 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002782}
2783
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002784void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002785 Record.push_back(Value.isUnsigned());
2786 AddAPInt(Value, Record);
2787}
2788
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002789void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002790 AddAPInt(Value.bitcastToAPInt(), Record);
2791}
2792
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002793void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002794 Record.push_back(getIdentifierRef(II));
2795}
2796
Sebastian Redl539c5062010-08-18 23:57:32 +00002797IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002798 if (II == 0)
2799 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002800
Sebastian Redl539c5062010-08-18 23:57:32 +00002801 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002802 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002803 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002804 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002805}
2806
Sebastian Redl50e26582010-09-15 19:54:06 +00002807MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002808 if (MD == 0)
2809 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002810
2811 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002812 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002813 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002814 return ID;
2815}
2816
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002817void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002818 Record.push_back(getSelectorRef(SelRef));
2819}
2820
Sebastian Redl539c5062010-08-18 23:57:32 +00002821SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002822 if (Sel.getAsOpaquePtr() == 0) {
2823 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002824 }
2825
Sebastian Redl539c5062010-08-18 23:57:32 +00002826 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002827 if (SID == 0 && Chain) {
2828 // This might trigger a ReadSelector callback, which will set the ID for
2829 // this selector.
2830 Chain->LoadSelector(Sel);
2831 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002832 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002833 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002834 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002835 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002836}
2837
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002838void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002839 AddDeclRef(Temp->getDestructor(), Record);
2840}
2841
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002842void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2843 CXXBaseSpecifier const *BasesEnd,
2844 RecordDataImpl &Record) {
2845 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2846 CXXBaseSpecifiersToWrite.push_back(
2847 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2848 Bases, BasesEnd));
2849 Record.push_back(NextCXXBaseSpecifiersID++);
2850}
2851
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002852void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002853 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002854 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002855 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002856 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002857 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002858 break;
2859 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002860 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002861 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002862 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002863 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2864 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002865 break;
John McCall0ad16662009-10-29 08:12:44 +00002866 case TemplateArgument::Null:
2867 case TemplateArgument::Integral:
2868 case TemplateArgument::Declaration:
2869 case TemplateArgument::Pack:
2870 break;
2871 }
2872}
2873
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002874void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002875 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002876 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002877
2878 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2879 bool InfoHasSameExpr
2880 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2881 Record.push_back(InfoHasSameExpr);
2882 if (InfoHasSameExpr)
2883 return; // Avoid storing the same expr twice.
2884 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002885 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2886 Record);
2887}
2888
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002889void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002890 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002891 AddTypeRef(QualType(), Record);
2892 return;
2893 }
2894
John McCallbcd03502009-12-07 02:54:59 +00002895 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002896 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002897 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002898 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002899}
2900
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002901void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002902 Record.push_back(GetOrCreateTypeID(T));
2903}
2904
2905TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002906 return MakeTypeID(T,
2907 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2908}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002909
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002910TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002911 return MakeTypeID(T,
2912 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002913}
2914
2915TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2916 if (T.isNull())
2917 return TypeIdx();
2918 assert(!T.getLocalFastQualifiers());
2919
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002920 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002921 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002922 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002923 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002924 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002925 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002926 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002927 return Idx;
2928}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002929
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002930TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002931 if (T.isNull())
2932 return TypeIdx();
2933 assert(!T.getLocalFastQualifiers());
2934
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002935 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2936 assert(I != TypeIdxs.end() && "Type not emitted!");
2937 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002938}
2939
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002940void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002941 Record.push_back(GetDeclRef(D));
2942}
2943
Sebastian Redl539c5062010-08-18 23:57:32 +00002944DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002945 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002946 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002947 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002948 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00002949 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002950 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002951 // We haven't seen this declaration before. Give it a new ID and
2952 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002953 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002954 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002955 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2956 // We don't add it to the replacement collection here, because we don't
2957 // have the offset yet.
2958 DeclTypesToEmit.push(const_cast<Decl *>(D));
2959 // Reset the flag, so that we don't add this decl multiple times.
2960 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002961 }
2962
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002963 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002964}
2965
Sebastian Redl539c5062010-08-18 23:57:32 +00002966DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002967 if (D == 0)
2968 return 0;
2969
2970 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2971 return DeclIDs[D];
2972}
2973
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002974void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002975 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002976 Record.push_back(Name.getNameKind());
2977 switch (Name.getNameKind()) {
2978 case DeclarationName::Identifier:
2979 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2980 break;
2981
2982 case DeclarationName::ObjCZeroArgSelector:
2983 case DeclarationName::ObjCOneArgSelector:
2984 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002985 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002986 break;
2987
2988 case DeclarationName::CXXConstructorName:
2989 case DeclarationName::CXXDestructorName:
2990 case DeclarationName::CXXConversionFunctionName:
2991 AddTypeRef(Name.getCXXNameType(), Record);
2992 break;
2993
2994 case DeclarationName::CXXOperatorName:
2995 Record.push_back(Name.getCXXOverloadedOperator());
2996 break;
2997
Alexis Hunt3d221f22009-11-29 07:34:05 +00002998 case DeclarationName::CXXLiteralOperatorName:
2999 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3000 break;
3001
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003002 case DeclarationName::CXXUsingDirective:
3003 // No extra data to emit
3004 break;
3005 }
3006}
Chris Lattnerca025db2010-05-07 21:43:38 +00003007
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003008void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003009 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003010 switch (Name.getNameKind()) {
3011 case DeclarationName::CXXConstructorName:
3012 case DeclarationName::CXXDestructorName:
3013 case DeclarationName::CXXConversionFunctionName:
3014 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3015 break;
3016
3017 case DeclarationName::CXXOperatorName:
3018 AddSourceLocation(
3019 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3020 Record);
3021 AddSourceLocation(
3022 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3023 Record);
3024 break;
3025
3026 case DeclarationName::CXXLiteralOperatorName:
3027 AddSourceLocation(
3028 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3029 Record);
3030 break;
3031
3032 case DeclarationName::Identifier:
3033 case DeclarationName::ObjCZeroArgSelector:
3034 case DeclarationName::ObjCOneArgSelector:
3035 case DeclarationName::ObjCMultiArgSelector:
3036 case DeclarationName::CXXUsingDirective:
3037 break;
3038 }
3039}
3040
3041void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003042 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003043 AddDeclarationName(NameInfo.getName(), Record);
3044 AddSourceLocation(NameInfo.getLoc(), Record);
3045 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3046}
3047
3048void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003049 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003050 AddNestedNameSpecifier(Info.NNS, Record);
3051 AddSourceRange(Info.NNSRange, Record);
3052 Record.push_back(Info.NumTemplParamLists);
3053 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3054 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3055}
3056
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003057void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003058 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003059 // Nested name specifiers usually aren't too long. I think that 8 would
3060 // typically accomodate the vast majority.
3061 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3062
3063 // Push each of the NNS's onto a stack for serialization in reverse order.
3064 while (NNS) {
3065 NestedNames.push_back(NNS);
3066 NNS = NNS->getPrefix();
3067 }
3068
3069 Record.push_back(NestedNames.size());
3070 while(!NestedNames.empty()) {
3071 NNS = NestedNames.pop_back_val();
3072 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3073 Record.push_back(Kind);
3074 switch (Kind) {
3075 case NestedNameSpecifier::Identifier:
3076 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3077 break;
3078
3079 case NestedNameSpecifier::Namespace:
3080 AddDeclRef(NNS->getAsNamespace(), Record);
3081 break;
3082
3083 case NestedNameSpecifier::TypeSpec:
3084 case NestedNameSpecifier::TypeSpecWithTemplate:
3085 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3086 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3087 break;
3088
3089 case NestedNameSpecifier::Global:
3090 // Don't need to write an associated value.
3091 break;
3092 }
3093 }
3094}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003095
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003096void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003097 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003098 Record.push_back(Kind);
3099 switch (Kind) {
3100 case TemplateName::Template:
3101 AddDeclRef(Name.getAsTemplateDecl(), Record);
3102 break;
3103
3104 case TemplateName::OverloadedTemplate: {
3105 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3106 Record.push_back(OvT->size());
3107 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3108 I != E; ++I)
3109 AddDeclRef(*I, Record);
3110 break;
3111 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003112
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003113 case TemplateName::QualifiedTemplate: {
3114 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3115 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3116 Record.push_back(QualT->hasTemplateKeyword());
3117 AddDeclRef(QualT->getTemplateDecl(), Record);
3118 break;
3119 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003120
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003121 case TemplateName::DependentTemplate: {
3122 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3123 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3124 Record.push_back(DepT->isIdentifier());
3125 if (DepT->isIdentifier())
3126 AddIdentifierRef(DepT->getIdentifier(), Record);
3127 else
3128 Record.push_back(DepT->getOperator());
3129 break;
3130 }
3131 }
3132}
3133
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003134void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003135 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003136 Record.push_back(Arg.getKind());
3137 switch (Arg.getKind()) {
3138 case TemplateArgument::Null:
3139 break;
3140 case TemplateArgument::Type:
3141 AddTypeRef(Arg.getAsType(), Record);
3142 break;
3143 case TemplateArgument::Declaration:
3144 AddDeclRef(Arg.getAsDecl(), Record);
3145 break;
3146 case TemplateArgument::Integral:
3147 AddAPSInt(*Arg.getAsIntegral(), Record);
3148 AddTypeRef(Arg.getIntegralType(), Record);
3149 break;
3150 case TemplateArgument::Template:
3151 AddTemplateName(Arg.getAsTemplate(), Record);
3152 break;
3153 case TemplateArgument::Expression:
3154 AddStmt(Arg.getAsExpr());
3155 break;
3156 case TemplateArgument::Pack:
3157 Record.push_back(Arg.pack_size());
3158 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3159 I != E; ++I)
3160 AddTemplateArgument(*I, Record);
3161 break;
3162 }
3163}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003164
3165void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003166ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003167 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003168 assert(TemplateParams && "No TemplateParams!");
3169 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3170 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3171 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3172 Record.push_back(TemplateParams->size());
3173 for (TemplateParameterList::const_iterator
3174 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3175 P != PEnd; ++P)
3176 AddDeclRef(*P, Record);
3177}
3178
3179/// \brief Emit a template argument list.
3180void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003181ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003182 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003183 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003184 Record.push_back(TemplateArgs->size());
3185 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003186 AddTemplateArgument(TemplateArgs->get(i), Record);
3187}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003188
3189
3190void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003191ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003192 Record.push_back(Set.size());
3193 for (UnresolvedSetImpl::const_iterator
3194 I = Set.begin(), E = Set.end(); I != E; ++I) {
3195 AddDeclRef(I.getDecl(), Record);
3196 Record.push_back(I.getAccess());
3197 }
3198}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003199
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003200void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003201 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003202 Record.push_back(Base.isVirtual());
3203 Record.push_back(Base.isBaseOfClass());
3204 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003205 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003206 AddSourceRange(Base.getSourceRange(), Record);
3207}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003208
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003209void ASTWriter::FlushCXXBaseSpecifiers() {
3210 RecordData Record;
3211 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3212 Record.clear();
3213
3214 // Record the offset of this base-specifier set.
3215 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3216 if (Index == CXXBaseSpecifiersOffsets.size())
3217 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3218 else {
3219 if (Index > CXXBaseSpecifiersOffsets.size())
3220 CXXBaseSpecifiersOffsets.resize(Index + 1);
3221 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3222 }
3223
3224 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3225 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3226 Record.push_back(BEnd - B);
3227 for (; B != BEnd; ++B)
3228 AddCXXBaseSpecifier(*B, Record);
3229 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003230
3231 // Flush any expressions that were written as part of the base specifiers.
3232 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003233 }
3234
3235 CXXBaseSpecifiersToWrite.clear();
3236}
3237
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003238void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003239 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003240 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003241 Record.push_back(NumBaseOrMembers);
3242 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3243 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3244
3245 Record.push_back(Init->isBaseInitializer());
3246 if (Init->isBaseInitializer()) {
3247 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3248 Record.push_back(Init->isBaseVirtual());
3249 } else {
3250 AddDeclRef(Init->getMember(), Record);
3251 }
3252 AddSourceLocation(Init->getMemberLocation(), Record);
3253 AddStmt(Init->getInit());
3254 AddDeclRef(Init->getAnonUnionMember(), Record);
3255 AddSourceLocation(Init->getLParenLoc(), Record);
3256 AddSourceLocation(Init->getRParenLoc(), Record);
3257 Record.push_back(Init->isWritten());
3258 if (Init->isWritten()) {
3259 Record.push_back(Init->getSourceOrder());
3260 } else {
3261 Record.push_back(Init->getNumArrayIndices());
3262 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3263 AddDeclRef(Init->getArrayIndex(i), Record);
3264 }
3265 }
3266}
3267
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003268void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3269 assert(D->DefinitionData);
3270 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3271 Record.push_back(Data.UserDeclaredConstructor);
3272 Record.push_back(Data.UserDeclaredCopyConstructor);
3273 Record.push_back(Data.UserDeclaredCopyAssignment);
3274 Record.push_back(Data.UserDeclaredDestructor);
3275 Record.push_back(Data.Aggregate);
3276 Record.push_back(Data.PlainOldData);
3277 Record.push_back(Data.Empty);
3278 Record.push_back(Data.Polymorphic);
3279 Record.push_back(Data.Abstract);
3280 Record.push_back(Data.HasTrivialConstructor);
3281 Record.push_back(Data.HasTrivialCopyConstructor);
3282 Record.push_back(Data.HasTrivialCopyAssignment);
3283 Record.push_back(Data.HasTrivialDestructor);
3284 Record.push_back(Data.ComputedVisibleConversions);
3285 Record.push_back(Data.DeclaredDefaultConstructor);
3286 Record.push_back(Data.DeclaredCopyConstructor);
3287 Record.push_back(Data.DeclaredCopyAssignment);
3288 Record.push_back(Data.DeclaredDestructor);
3289
3290 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003291 if (Data.NumBases > 0)
3292 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3293 Record);
3294
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003295 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3296 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003297 if (Data.NumVBases > 0)
3298 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3299 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003300
3301 AddUnresolvedSet(Data.Conversions, Record);
3302 AddUnresolvedSet(Data.VisibleConversions, Record);
3303 // Data.Definition is the owning decl, no need to write it.
3304 AddDeclRef(Data.FirstFriend, Record);
3305}
3306
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003307void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003308 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003309 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003310 assert(FirstDeclID == NextDeclID &&
3311 FirstTypeID == NextTypeID &&
3312 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003313 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003314 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003315 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003316 "Setting chain after writing has started.");
3317 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003318
3319 FirstDeclID += Chain->getTotalNumDecls();
3320 FirstTypeID += Chain->getTotalNumTypes();
3321 FirstIdentID += Chain->getTotalNumIdentifiers();
3322 FirstSelectorID += Chain->getTotalNumSelectors();
3323 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003324 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003325 NextDeclID = FirstDeclID;
3326 NextTypeID = FirstTypeID;
3327 NextIdentID = FirstIdentID;
3328 NextSelectorID = FirstSelectorID;
3329 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003330 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003331}
3332
Sebastian Redl539c5062010-08-18 23:57:32 +00003333void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003334 IdentifierIDs[II] = ID;
3335}
3336
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003337void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003338 // Always take the highest-numbered type index. This copes with an interesting
3339 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003340 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003341 // keep the higher-numbered entry so that we can properly write it out to
3342 // the AST file.
3343 TypeIdx &StoredIdx = TypeIdxs[T];
3344 if (Idx.getIndex() >= StoredIdx.getIndex())
3345 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003346}
3347
Sebastian Redl539c5062010-08-18 23:57:32 +00003348void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003349 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003350}
Sebastian Redl834bb972010-08-04 17:20:04 +00003351
Sebastian Redl539c5062010-08-18 23:57:32 +00003352void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003353 SelectorIDs[S] = ID;
3354}
Douglas Gregor91096292010-10-02 19:29:26 +00003355
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003356void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003357 MacroDefinition *MD) {
3358 MacroDefinitions[MD] = ID;
3359}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003360
3361void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3362 assert(D->isDefinition());
3363 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3364 // We are interested when a PCH decl is modified.
3365 if (RD->getPCHLevel() > 0) {
3366 // A forward reference was mutated into a definition. Rewrite it.
3367 // FIXME: This happens during template instantiation, should we
3368 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003369 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003370 }
3371
3372 for (CXXRecordDecl::redecl_iterator
3373 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3374 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3375 if (Redecl == RD)
3376 continue;
3377
3378 // We are interested when a PCH decl is modified.
3379 if (Redecl->getPCHLevel() > 0) {
3380 UpdateRecord &Record = DeclUpdates[Redecl];
3381 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3382 assert(Redecl->DefinitionData);
3383 assert(Redecl->DefinitionData->Definition == D);
3384 AddDeclRef(D, Record); // the DefinitionDecl
3385 }
3386 }
3387 }
3388}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003389void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3390 // TU and namespaces are handled elsewhere.
3391 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3392 return;
3393
3394 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3395 return; // Not a source decl added to a DeclContext from PCH.
3396
3397 AddUpdatedDeclContext(DC);
3398}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003399
3400void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3401 assert(D->isImplicit());
3402 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3403 return; // Not a source member added to a class from PCH.
3404 if (!isa<CXXMethodDecl>(D))
3405 return; // We are interested in lazily declared implicit methods.
3406
3407 // A decl coming from PCH was modified.
3408 assert(RD->isDefinition());
3409 UpdateRecord &Record = DeclUpdates[RD];
3410 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3411 AddDeclRef(D, Record);
3412}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003413
3414void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3415 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003416 // The specializations set is kept in the canonical template.
3417 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003418 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3419 return; // Not a source specialization added to a template from PCH.
3420
3421 UpdateRecord &Record = DeclUpdates[TD];
3422 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3423 AddDeclRef(D, Record);
3424}