blob: 44f17e40b1a31c3ffad36ef809b444c19ae28845 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050
Sebastian Redl3df5a082010-07-30 17:03:48 +000051template <typename T, typename Allocator>
52T *data(std::vector<T, Allocator> &v) {
53 return v.empty() ? 0 : &v.front();
54}
55template <typename T, typename Allocator>
56const T *data(const std::vector<T, Allocator> &v) {
57 return v.empty() ? 0 : &v.front();
58}
59
Douglas Gregoref84c4b2009-04-09 22:27:44 +000060//===----------------------------------------------------------------------===//
61// Type serialization
62//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000063
Douglas Gregoref84c4b2009-04-09 22:27:44 +000064namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000065 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000066 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000067 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068
69 public:
70 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000071 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000073 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000074 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075
76 void VisitArrayType(const ArrayType *T);
77 void VisitFunctionType(const FunctionType *T);
78 void VisitTagType(const TagType *T);
79
80#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
81#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082#include "clang/AST/TypeNodes.def"
83 };
84}
85
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000086void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000087 assert(false && "Built-in types are never serialized");
88}
89
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000090void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000091 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000092 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093}
94
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000095void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000097 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098}
99
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000101 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000102 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103}
104
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000107 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108}
109
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000112 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000116 Writer.AddTypeRef(T->getPointeeType(), Record);
117 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000118 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119}
120
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122 Writer.AddTypeRef(T->getElementType(), Record);
123 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000124 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128 VisitArrayType(T);
129 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000140 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
141 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000142 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 Writer.AddTypeRef(T->getElementType(), Record);
148 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000149 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000150 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000155 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156}
157
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000160 FunctionType::ExtInfo C = T->getExtInfo();
161 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000162 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000163 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000164 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000169 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170}
171
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173 VisitFunctionType(T);
174 Record.push_back(T->getNumArgs());
175 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
176 Writer.AddTypeRef(T->getArgType(I), Record);
177 Record.push_back(T->isVariadic());
178 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000179 Record.push_back(T->hasExceptionSpec());
180 Record.push_back(T->hasAnyExceptionSpec());
181 Record.push_back(T->getNumExceptions());
182 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
183 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000184 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000188 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000189 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000190}
John McCallb96ec562009-12-04 22:46:56 +0000191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000194 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
195 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000200 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202}
203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000206 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207}
208
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000210 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000211 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000212}
213
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000215 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000217 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218 "Cannot serialize in the middle of a type definition");
219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224}
225
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000228 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229}
230
Mike Stump11289f42009-09-09 15:08:12 +0000231void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000232ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000233 const SubstTemplateTypeParmType *T) {
234 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
235 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000236 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000237}
238
239void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000240ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000242 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000243 Writer.AddTemplateName(T->getTemplateName(), Record);
244 Record.push_back(T->getNumArgs());
245 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
246 ArgI != ArgE; ++ArgI)
247 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000248 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
249 : T->getCanonicalTypeInternal(),
250 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000251 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000252}
253
254void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000255ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000256 VisitArrayType(T);
257 Writer.AddStmt(T->getSizeExpr());
258 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000259 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000260}
261
262void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000263ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000264 const DependentSizedExtVectorType *T) {
265 // FIXME: Serialize this type (C++ only)
266 assert(false && "Cannot serialize dependent sized extended vector types");
267}
268
269void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000271 Record.push_back(T->getDepth());
272 Record.push_back(T->getIndex());
273 Record.push_back(T->isParameterPack());
274 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000275 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000276}
277
278void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000279ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000280 Record.push_back(T->getKeyword());
281 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
282 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000283 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
284 : T->getCanonicalTypeInternal(),
285 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000286 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000287}
288
289void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000290ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000291 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000292 Record.push_back(T->getKeyword());
293 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
294 Writer.AddIdentifierRef(T->getIdentifier(), Record);
295 Record.push_back(T->getNumArgs());
296 for (DependentTemplateSpecializationType::iterator
297 I = T->begin(), E = T->end(); I != E; ++I)
298 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000299 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000300}
301
Douglas Gregord2fa7662010-12-20 02:24:11 +0000302void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
303 Writer.AddTypeRef(T->getPattern(), Record);
304 Code = TYPE_PACK_EXPANSION;
305}
306
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000307void ASTTypeWriter::VisitParenType(const ParenType *T) {
308 Writer.AddTypeRef(T->getInnerType(), Record);
309 Code = TYPE_PAREN;
310}
311
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000313 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000314 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
315 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000316 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000317}
318
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000319void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000320 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000321 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000322 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000323}
324
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000325void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000326 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000327 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000328}
329
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000331 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000332 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000333 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000334 E = T->qual_end(); I != E; ++I)
335 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000336 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000337}
338
Steve Narofffb4330f2009-06-17 22:40:22 +0000339void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000340ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000341 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000343}
344
John McCall8f115c62009-10-16 21:56:05 +0000345namespace {
346
347class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000348 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000349 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000350
351public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000352 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000353 : Writer(Writer), Record(Record) { }
354
John McCall17001972009-10-18 01:05:36 +0000355#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000356#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000357 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000358#include "clang/AST/TypeLocNodes.def"
359
John McCall17001972009-10-18 01:05:36 +0000360 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
361 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000362};
363
364}
365
John McCall17001972009-10-18 01:05:36 +0000366void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
367 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000368}
John McCall17001972009-10-18 01:05:36 +0000369void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000370 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
371 if (TL.needsExtraLocalData()) {
372 Record.push_back(TL.getWrittenTypeSpec());
373 Record.push_back(TL.getWrittenSignSpec());
374 Record.push_back(TL.getWrittenWidthSpec());
375 Record.push_back(TL.hasModeAttr());
376 }
John McCall8f115c62009-10-16 21:56:05 +0000377}
John McCall17001972009-10-18 01:05:36 +0000378void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000380}
John McCall17001972009-10-18 01:05:36 +0000381void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000383}
John McCall17001972009-10-18 01:05:36 +0000384void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
385 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000386}
John McCall17001972009-10-18 01:05:36 +0000387void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000389}
John McCall17001972009-10-18 01:05:36 +0000390void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000392}
John McCall17001972009-10-18 01:05:36 +0000393void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
394 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000395}
John McCall17001972009-10-18 01:05:36 +0000396void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
397 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
398 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
399 Record.push_back(TL.getSizeExpr() ? 1 : 0);
400 if (TL.getSizeExpr())
401 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000402}
John McCall17001972009-10-18 01:05:36 +0000403void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
404 VisitArrayTypeLoc(TL);
405}
406void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
407 VisitArrayTypeLoc(TL);
408}
409void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
410 VisitArrayTypeLoc(TL);
411}
412void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
413 DependentSizedArrayTypeLoc TL) {
414 VisitArrayTypeLoc(TL);
415}
416void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
417 DependentSizedExtVectorTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getNameLoc(), Record);
419}
420void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getNameLoc(), Record);
422}
423void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getNameLoc(), Record);
425}
426void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
428 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000429 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000430 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
431 Writer.AddDeclRef(TL.getArg(i), Record);
432}
433void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
434 VisitFunctionTypeLoc(TL);
435}
436void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
437 VisitFunctionTypeLoc(TL);
438}
John McCallb96ec562009-12-04 22:46:56 +0000439void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getNameLoc(), Record);
441}
John McCall17001972009-10-18 01:05:36 +0000442void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
445void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000446 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
447 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
448 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000449}
450void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000451 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
452 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
453 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
454 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000455}
456void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getNameLoc(), Record);
458}
459void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
461}
462void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
John McCall17001972009-10-18 01:05:36 +0000465void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
John McCallcebee162009-10-18 09:09:24 +0000468void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
469 SubstTemplateTypeParmTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getNameLoc(), Record);
471}
John McCall17001972009-10-18 01:05:36 +0000472void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
473 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000474 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
475 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
476 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
477 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000478 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
479 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000480}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000481void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
483 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
484}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000485void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000486 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
487 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000488}
John McCalle78aac42010-03-10 03:28:59 +0000489void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
491}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000492void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000493 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
494 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000495 Writer.AddSourceLocation(TL.getNameLoc(), Record);
496}
John McCallc392f372010-06-11 00:33:02 +0000497void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
498 DependentTemplateSpecializationTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
500 Writer.AddSourceRange(TL.getQualifierRange(), Record);
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
503 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
504 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000505 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
506 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000507}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000508void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
510}
John McCall17001972009-10-18 01:05:36 +0000511void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000513}
514void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
515 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000516 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
517 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
518 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
519 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000520}
John McCallfc93cf92009-10-22 22:37:11 +0000521void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000523}
John McCall8f115c62009-10-16 21:56:05 +0000524
Chris Lattner19cea4e2009-04-22 05:57:30 +0000525//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000526// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000527//===----------------------------------------------------------------------===//
528
Chris Lattner28fa4e62009-04-26 22:26:21 +0000529static void EmitBlockID(unsigned ID, const char *Name,
530 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000531 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000532 Record.clear();
533 Record.push_back(ID);
534 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
535
536 // Emit the block name if present.
537 if (Name == 0 || Name[0] == 0) return;
538 Record.clear();
539 while (*Name)
540 Record.push_back(*Name++);
541 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
542}
543
544static void EmitRecordID(unsigned ID, const char *Name,
545 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000546 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000547 Record.clear();
548 Record.push_back(ID);
549 while (*Name)
550 Record.push_back(*Name++);
551 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000552}
553
554static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000555 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000556#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000557 RECORD(STMT_STOP);
558 RECORD(STMT_NULL_PTR);
559 RECORD(STMT_NULL);
560 RECORD(STMT_COMPOUND);
561 RECORD(STMT_CASE);
562 RECORD(STMT_DEFAULT);
563 RECORD(STMT_LABEL);
564 RECORD(STMT_IF);
565 RECORD(STMT_SWITCH);
566 RECORD(STMT_WHILE);
567 RECORD(STMT_DO);
568 RECORD(STMT_FOR);
569 RECORD(STMT_GOTO);
570 RECORD(STMT_INDIRECT_GOTO);
571 RECORD(STMT_CONTINUE);
572 RECORD(STMT_BREAK);
573 RECORD(STMT_RETURN);
574 RECORD(STMT_DECL);
575 RECORD(STMT_ASM);
576 RECORD(EXPR_PREDEFINED);
577 RECORD(EXPR_DECL_REF);
578 RECORD(EXPR_INTEGER_LITERAL);
579 RECORD(EXPR_FLOATING_LITERAL);
580 RECORD(EXPR_IMAGINARY_LITERAL);
581 RECORD(EXPR_STRING_LITERAL);
582 RECORD(EXPR_CHARACTER_LITERAL);
583 RECORD(EXPR_PAREN);
584 RECORD(EXPR_UNARY_OPERATOR);
585 RECORD(EXPR_SIZEOF_ALIGN_OF);
586 RECORD(EXPR_ARRAY_SUBSCRIPT);
587 RECORD(EXPR_CALL);
588 RECORD(EXPR_MEMBER);
589 RECORD(EXPR_BINARY_OPERATOR);
590 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
591 RECORD(EXPR_CONDITIONAL_OPERATOR);
592 RECORD(EXPR_IMPLICIT_CAST);
593 RECORD(EXPR_CSTYLE_CAST);
594 RECORD(EXPR_COMPOUND_LITERAL);
595 RECORD(EXPR_EXT_VECTOR_ELEMENT);
596 RECORD(EXPR_INIT_LIST);
597 RECORD(EXPR_DESIGNATED_INIT);
598 RECORD(EXPR_IMPLICIT_VALUE_INIT);
599 RECORD(EXPR_VA_ARG);
600 RECORD(EXPR_ADDR_LABEL);
601 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000602 RECORD(EXPR_CHOOSE);
603 RECORD(EXPR_GNU_NULL);
604 RECORD(EXPR_SHUFFLE_VECTOR);
605 RECORD(EXPR_BLOCK);
606 RECORD(EXPR_BLOCK_DECL_REF);
607 RECORD(EXPR_OBJC_STRING_LITERAL);
608 RECORD(EXPR_OBJC_ENCODE);
609 RECORD(EXPR_OBJC_SELECTOR_EXPR);
610 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
611 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
612 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
613 RECORD(EXPR_OBJC_KVC_REF_EXPR);
614 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000615 RECORD(STMT_OBJC_FOR_COLLECTION);
616 RECORD(STMT_OBJC_CATCH);
617 RECORD(STMT_OBJC_FINALLY);
618 RECORD(STMT_OBJC_AT_TRY);
619 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
620 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000621 RECORD(EXPR_CXX_OPERATOR_CALL);
622 RECORD(EXPR_CXX_CONSTRUCT);
623 RECORD(EXPR_CXX_STATIC_CAST);
624 RECORD(EXPR_CXX_DYNAMIC_CAST);
625 RECORD(EXPR_CXX_REINTERPRET_CAST);
626 RECORD(EXPR_CXX_CONST_CAST);
627 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
628 RECORD(EXPR_CXX_BOOL_LITERAL);
629 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000630#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000631}
Mike Stump11289f42009-09-09 15:08:12 +0000632
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000633void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000634 RecordData Record;
635 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000636
Sebastian Redl539c5062010-08-18 23:57:32 +0000637#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
638#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000639
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000640 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000641 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000642 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000643 RECORD(TYPE_OFFSET);
644 RECORD(DECL_OFFSET);
645 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000646 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000647 RECORD(IDENTIFIER_OFFSET);
648 RECORD(IDENTIFIER_TABLE);
649 RECORD(EXTERNAL_DEFINITIONS);
650 RECORD(SPECIAL_TYPES);
651 RECORD(STATISTICS);
652 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000653 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000654 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
655 RECORD(SELECTOR_OFFSETS);
656 RECORD(METHOD_POOL);
657 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000658 RECORD(SOURCE_LOCATION_OFFSETS);
659 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000660 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000661 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000662 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000663 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000664 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000665 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000666
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000668 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000669 RECORD(SM_SLOC_FILE_ENTRY);
670 RECORD(SM_SLOC_BUFFER_ENTRY);
671 RECORD(SM_SLOC_BUFFER_BLOB);
672 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
673 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner28fa4e62009-04-26 22:26:21 +0000675 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000676 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000677 RECORD(PP_MACRO_OBJECT_LIKE);
678 RECORD(PP_MACRO_FUNCTION_LIKE);
679 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000680 RECORD(PP_MACRO_INSTANTIATION);
681 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000682
Douglas Gregor12bfa382009-10-17 00:13:19 +0000683 // Decls and Types block.
684 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000685 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000686 RECORD(TYPE_COMPLEX);
687 RECORD(TYPE_POINTER);
688 RECORD(TYPE_BLOCK_POINTER);
689 RECORD(TYPE_LVALUE_REFERENCE);
690 RECORD(TYPE_RVALUE_REFERENCE);
691 RECORD(TYPE_MEMBER_POINTER);
692 RECORD(TYPE_CONSTANT_ARRAY);
693 RECORD(TYPE_INCOMPLETE_ARRAY);
694 RECORD(TYPE_VARIABLE_ARRAY);
695 RECORD(TYPE_VECTOR);
696 RECORD(TYPE_EXT_VECTOR);
697 RECORD(TYPE_FUNCTION_PROTO);
698 RECORD(TYPE_FUNCTION_NO_PROTO);
699 RECORD(TYPE_TYPEDEF);
700 RECORD(TYPE_TYPEOF_EXPR);
701 RECORD(TYPE_TYPEOF);
702 RECORD(TYPE_RECORD);
703 RECORD(TYPE_ENUM);
704 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000705 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000706 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000707 RECORD(DECL_TRANSLATION_UNIT);
708 RECORD(DECL_TYPEDEF);
709 RECORD(DECL_ENUM);
710 RECORD(DECL_RECORD);
711 RECORD(DECL_ENUM_CONSTANT);
712 RECORD(DECL_FUNCTION);
713 RECORD(DECL_OBJC_METHOD);
714 RECORD(DECL_OBJC_INTERFACE);
715 RECORD(DECL_OBJC_PROTOCOL);
716 RECORD(DECL_OBJC_IVAR);
717 RECORD(DECL_OBJC_AT_DEFS_FIELD);
718 RECORD(DECL_OBJC_CLASS);
719 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
720 RECORD(DECL_OBJC_CATEGORY);
721 RECORD(DECL_OBJC_CATEGORY_IMPL);
722 RECORD(DECL_OBJC_IMPLEMENTATION);
723 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
724 RECORD(DECL_OBJC_PROPERTY);
725 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000726 RECORD(DECL_FIELD);
727 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000728 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000729 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000730 RECORD(DECL_FILE_SCOPE_ASM);
731 RECORD(DECL_BLOCK);
732 RECORD(DECL_CONTEXT_LEXICAL);
733 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000734 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000735 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000736#undef RECORD
737#undef BLOCK
738 Stream.ExitBlock();
739}
740
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000741/// \brief Adjusts the given filename to only write out the portion of the
742/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000743///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000744/// \param Filename the file name to adjust.
745///
746/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
747/// the returned filename will be adjusted by this system root.
748///
749/// \returns either the original filename (if it needs no adjustment) or the
750/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000751static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000752adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
753 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000754
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000755 if (!isysroot)
756 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000757
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000758 // Verify that the filename and the system root have the same prefix.
759 unsigned Pos = 0;
760 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
761 if (Filename[Pos] != isysroot[Pos])
762 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000763
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000764 // We hit the end of the filename before we hit the end of the system root.
765 if (!Filename[Pos])
766 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000767
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000768 // If the file name has a '/' at the current position, skip over the '/'.
769 // We distinguish sysroot-based includes from absolute includes by the
770 // absence of '/' at the beginning of sysroot-based includes.
771 if (Filename[Pos] == '/')
772 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000773
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000774 return Filename + Pos;
775}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000776
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000777/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000778void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000779 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000780
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000781 // Metadata
782 const TargetInfo &Target = Context.Target;
783 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000784 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000785 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000786 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
787 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000788 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
789 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
790 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000791 // Target triple or chained PCH name
792 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000793 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000794
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000795 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000796 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
797 Record.push_back(VERSION_MAJOR);
798 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000799 Record.push_back(CLANG_VERSION_MAJOR);
800 Record.push_back(CLANG_VERSION_MINOR);
801 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000802 // FIXME: This writes the absolute path for chained headers.
803 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
804 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000805
Douglas Gregor45fe0362009-05-12 01:31:05 +0000806 // Original file name
807 SourceManager &SM = Context.getSourceManager();
808 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
809 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000810 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000811 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
812 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
813
Michael J. Spencer740857f2010-12-21 16:45:57 +0000814 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000815
Michael J. Spencer740857f2010-12-21 16:45:57 +0000816 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000817
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000818 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000819 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000820 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000821 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000822 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000823 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000824 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000825
Ted Kremenek18e066f2010-01-22 22:12:47 +0000826 // Repository branch/version information.
827 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000828 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000829 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
830 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000831 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000832 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000833 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
834 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000835}
836
837/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000838void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000839 RecordData Record;
840 Record.push_back(LangOpts.Trigraphs);
841 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
842 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
843 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
844 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000845 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000846 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
847 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
848 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
849 Record.push_back(LangOpts.C99); // C99 Support
850 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000851 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
852 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000853 Record.push_back(LangOpts.CPlusPlus); // C++ Support
854 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000855 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000856
Douglas Gregor55abb232009-04-10 20:39:37 +0000857 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
858 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000859 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000860 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000861 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000862 // modern abi enabled.
Ted Kremenek1d56c9e2010-12-23 21:35:43 +0000863 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
864 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000865 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000866
Douglas Gregor55abb232009-04-10 20:39:37 +0000867 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000868 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
869 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000870 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000871 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000872 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000873
874 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
875 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
876 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
877
Chris Lattner258172e2009-04-27 07:35:58 +0000878 // Whether static initializers are protected by locks.
879 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000880 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000881 Record.push_back(LangOpts.Blocks); // block extension to C
882 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
883 // they are unused.
884 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
885 // (modulo the platform support).
886
Chris Lattner51924e512010-06-26 21:25:03 +0000887 Record.push_back(LangOpts.getSignedOverflowBehavior());
888 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000889
890 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000891 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000892 // defined.
893 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
894 // opposed to __DYNAMIC__).
895 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
896
897 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
898 // used (instead of C99 semantics).
899 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000900 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
901 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000902 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
903 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000904 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000905 Record.push_back(LangOpts.getGCMode());
906 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000907 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000908 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000909 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000910 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +0000911 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000912 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000913 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000914 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000915}
916
Douglas Gregora7f71a92009-04-10 03:52:48 +0000917//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000918// stat cache Serialization
919//===----------------------------------------------------------------------===//
920
921namespace {
922// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000923class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000924public:
925 typedef const char * key_type;
926 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000927
Chris Lattner2a6fa472010-11-23 19:28:12 +0000928 typedef struct stat data_type;
929 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000930
931 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000932 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000933 }
Mike Stump11289f42009-09-09 15:08:12 +0000934
935 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000936 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
937 data_type_ref Data) {
938 unsigned StrLen = strlen(path);
939 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000940 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000941 clang::io::Emit8(Out, DataLen);
942 return std::make_pair(StrLen + 1, DataLen);
943 }
Mike Stump11289f42009-09-09 15:08:12 +0000944
Douglas Gregorc5046832009-04-27 18:38:38 +0000945 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
946 Out.write(path, KeyLen);
947 }
Mike Stump11289f42009-09-09 15:08:12 +0000948
Chris Lattner2a6fa472010-11-23 19:28:12 +0000949 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000950 data_type_ref Data, unsigned DataLen) {
951 using namespace clang::io;
952 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000953
Chris Lattner2a6fa472010-11-23 19:28:12 +0000954 Emit32(Out, (uint32_t) Data.st_ino);
955 Emit32(Out, (uint32_t) Data.st_dev);
956 Emit16(Out, (uint16_t) Data.st_mode);
957 Emit64(Out, (uint64_t) Data.st_mtime);
958 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +0000959
960 assert(Out.tell() - Start == DataLen && "Wrong data length");
961 }
962};
963} // end anonymous namespace
964
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000965/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000966void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000967 // Build the on-disk hash table containing information about every
968 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000969 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000970 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000971 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000972 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000973 Stat != StatEnd; ++Stat, ++NumStatEntries) {
974 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000975 Generator.insert(Filename, Stat->second);
976 }
Mike Stump11289f42009-09-09 15:08:12 +0000977
Douglas Gregorc5046832009-04-27 18:38:38 +0000978 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000979 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000980 uint32_t BucketOffset;
981 {
982 llvm::raw_svector_ostream Out(StatCacheData);
983 // Make sure that no bucket is at offset 0
984 clang::io::Emit32(Out, 0);
985 BucketOffset = Generator.Emit(Out);
986 }
987
988 // Create a blob abbreviation
989 using namespace llvm;
990 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000991 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
995 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
996
997 // Write the stat cache
998 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000999 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001000 Record.push_back(BucketOffset);
1001 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001002 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001003}
1004
1005//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001006// Source Manager Serialization
1007//===----------------------------------------------------------------------===//
1008
1009/// \brief Create an abbreviation for the SLocEntry that refers to a
1010/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001011static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001012 using namespace llvm;
1013 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001014 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001019 // FileEntry fields.
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001022 // HeaderFileInfo fields.
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
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.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001033static unsigned CreateSLocBufferAbbrev(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_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001042 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001043}
1044
1045/// \brief Create an abbreviation for the SLocEntry that refers to a
1046/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001047static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001048 using namespace llvm;
1049 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001050 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001052 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001053}
1054
1055/// \brief Create an abbreviation for the SLocEntry that refers to an
1056/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001057static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001058 using namespace llvm;
1059 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001060 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001066 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001067}
1068
1069/// \brief Writes the block containing the serialized form of the
1070/// source manager.
1071///
1072/// TODO: We should probably use an on-disk hash table (stored in a
1073/// blob), indexed based on the file name, so that we only create
1074/// entries for files that we actually need. In the common case (no
1075/// errors), we probably won't have to create file entries for any of
1076/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001077void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001078 const Preprocessor &PP,
1079 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001080 RecordData Record;
1081
Chris Lattner0910e3b2009-04-10 17:16:57 +00001082 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001083 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001084
1085 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001086 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1087 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1088 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1089 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001090
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001091 // Write the line table.
1092 if (SourceMgr.hasLineTable()) {
1093 LineTableInfo &LineTable = SourceMgr.getLineTable();
1094
1095 // Emit the file names
1096 Record.push_back(LineTable.getNumFilenames());
1097 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1098 // Emit the file name
1099 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001100 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001101 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1102 Record.push_back(FilenameLen);
1103 if (FilenameLen)
1104 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001107 // Emit the line entries
1108 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1109 L != LEnd; ++L) {
1110 // Emit the file ID
1111 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001112
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001113 // Emit the line entries
1114 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001115 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001116 LEEnd = L->second.end();
1117 LE != LEEnd; ++LE) {
1118 Record.push_back(LE->FileOffset);
1119 Record.push_back(LE->LineNo);
1120 Record.push_back(LE->FilenameID);
1121 Record.push_back((unsigned)LE->FileKind);
1122 Record.push_back(LE->IncludeOffset);
1123 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001124 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001125 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001126 }
1127
Douglas Gregor258ae542009-04-27 06:38:32 +00001128 // Write out the source location entry table. We skip the first
1129 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001130 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001131 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001132 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1133 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1134 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1135 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001136 // Get this source location entry.
1137 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001138
Douglas Gregor258ae542009-04-27 06:38:32 +00001139 // Record the offset of this source-location entry.
1140 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1141
1142 // Figure out which record code to use.
1143 unsigned Code;
1144 if (SLoc->isFile()) {
1145 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001146 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001147 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001148 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001149 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001150 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001151 Record.clear();
1152 Record.push_back(Code);
1153
1154 Record.push_back(SLoc->getOffset());
1155 if (SLoc->isFile()) {
1156 const SrcMgr::FileInfo &File = SLoc->getFile();
1157 Record.push_back(File.getIncludeLoc().getRawEncoding());
1158 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1159 Record.push_back(File.hasLineDirectives());
1160
1161 const SrcMgr::ContentCache *Content = File.getContentCache();
1162 if (Content->Entry) {
1163 // The source location entry is a file. The blob associated
1164 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001165
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001166 // Emit size/modification time for this file.
1167 Record.push_back(Content->Entry->getSize());
1168 Record.push_back(Content->Entry->getModificationTime());
1169
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001170 // Emit header-search information associated with this file.
1171 HeaderFileInfo HFI;
1172 HeaderSearch &HS = PP.getHeaderSearchInfo();
1173 if (Content->Entry->getUID() < HS.header_file_size())
1174 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1175 Record.push_back(HFI.isImport);
1176 Record.push_back(HFI.DirInfo);
1177 Record.push_back(HFI.NumIncludes);
1178 AddIdentifierRef(HFI.ControllingMacro, Record);
1179
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001180 // Turn the file name into an absolute path, if it isn't already.
1181 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001182 llvm::SmallString<128> FilePath(Filename);
1183 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001184 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001185
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001186 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001187 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001188
1189 // FIXME: For now, preload all file source locations, so that
1190 // we get the appropriate File entries in the reader. This is
1191 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001192 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001193 } else {
1194 // The source location entry is a buffer. The blob associated
1195 // with this entry contains the contents of the buffer.
1196
1197 // We add one to the size so that we capture the trailing NULL
1198 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1199 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001200 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001201 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001202 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001203 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1204 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001205 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001206 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001207 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001208 llvm::StringRef(Buffer->getBufferStart(),
1209 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001210
1211 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001212 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001213 }
1214 } else {
1215 // The source location entry is an instantiation.
1216 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1217 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1218 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1219 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1220
1221 // Compute the token length for this macro expansion.
1222 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001223 if (I + 1 != N)
1224 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001225 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1226 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1227 }
1228 }
1229
Douglas Gregor8f45df52009-04-16 22:23:12 +00001230 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001231
1232 if (SLocEntryOffsets.empty())
1233 return;
1234
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001235 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001236 // table is used for lazily loading source-location information.
1237 using namespace llvm;
1238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001239 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1243 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001244
Douglas Gregor258ae542009-04-27 06:38:32 +00001245 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001246 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001247 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001248 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1249 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001250 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001251 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001252 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001253
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001254 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001255 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001256 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001257}
1258
Douglas Gregorc5046832009-04-27 18:38:38 +00001259//===----------------------------------------------------------------------===//
1260// Preprocessor Serialization
1261//===----------------------------------------------------------------------===//
1262
Chris Lattnereeffaef2009-04-10 17:15:23 +00001263/// \brief Writes the block containing the serialized form of the
1264/// preprocessor.
1265///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001266void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001267 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001268
Chris Lattner0af3ba12009-04-13 01:29:17 +00001269 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1270 if (PP.getCounterValue() != 0) {
1271 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001272 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001273 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001274 }
1275
1276 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001277 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001278
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001279 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001280 // FIXME: use diagnostics subsystem for localization etc.
1281 if (PP.SawDateOrTime())
1282 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001283
Douglas Gregor796d76a2010-10-20 22:00:55 +00001284
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001285 // Loop over all the macro definitions that are live at the end of the file,
1286 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001287 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001288 unsigned InclusionAbbrev = 0;
1289 if (PPRec) {
1290 using namespace llvm;
1291 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1292 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001300 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001301 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001302
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001303 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1304 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001305 // FIXME: This emits macros in hash table order, we should do it in a stable
1306 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001307 MacroInfo *MI = I->second;
1308
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001309 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001310 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001311 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001312
1313 // FIXME: There is a (probably minor) optimization we could do here, if
1314 // the macro comes from the original PCH but the identifier comes from a
1315 // chained PCH, by storing the offset into the original PCH rather than
1316 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001317 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001318 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001319 continue;
1320
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001321 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001322 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001323 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1324 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001325
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001326 unsigned Code;
1327 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001328 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001329 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001330 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001331
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001332 Record.push_back(MI->isC99Varargs());
1333 Record.push_back(MI->isGNUVarargs());
1334 Record.push_back(MI->getNumArgs());
1335 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1336 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001337 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001338 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001339
Douglas Gregoraae92242010-03-19 21:51:54 +00001340 // If we have a detailed preprocessing record, record the macro definition
1341 // ID that corresponds to this macro.
1342 if (PPRec)
1343 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001344
Douglas Gregor8f45df52009-04-16 22:23:12 +00001345 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001346 Record.clear();
1347
Chris Lattner2199f5b2009-04-10 18:08:30 +00001348 // Emit the tokens array.
1349 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1350 // Note that we know that the preprocessor does not have any annotation
1351 // tokens in it because they are created by the parser, and thus can't be
1352 // in a macro definition.
1353 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001354
Chris Lattner2199f5b2009-04-10 18:08:30 +00001355 Record.push_back(Tok.getLocation().getRawEncoding());
1356 Record.push_back(Tok.getLength());
1357
Chris Lattner2199f5b2009-04-10 18:08:30 +00001358 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1359 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001360 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001361
Chris Lattner2199f5b2009-04-10 18:08:30 +00001362 // FIXME: Should translate token kind to a stable encoding.
1363 Record.push_back(Tok.getKind());
1364 // FIXME: Should translate token flags to a stable encoding.
1365 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001366
Sebastian Redl539c5062010-08-18 23:57:32 +00001367 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001368 Record.clear();
1369 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001370 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001371 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001372
Douglas Gregoraae92242010-03-19 21:51:54 +00001373 // If the preprocessor has a preprocessing record, emit it.
1374 unsigned NumPreprocessingRecords = 0;
1375 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001376 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001377 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1378 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001379 E != EEnd; ++E) {
1380 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001381
Douglas Gregoraae92242010-03-19 21:51:54 +00001382 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1383 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001384 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001385
Douglas Gregor91096292010-10-02 19:29:26 +00001386 // Don't write the macro definition if it is from another AST file.
1387 if (ID < FirstMacroID)
1388 continue;
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001389
1390 // Notify the serialization listener that we're serializing this entity.
1391 if (SerializationListener)
1392 SerializationListener->SerializedPreprocessedEntity(*E,
1393 Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001394
Douglas Gregor91096292010-10-02 19:29:26 +00001395 unsigned Position = ID - FirstMacroID;
1396 if (Position != MacroDefinitionOffsets.size()) {
1397 if (Position > MacroDefinitionOffsets.size())
1398 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001399
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001400 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001401 } else
1402 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001403
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001404 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001405 Record.push_back(ID);
1406 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1407 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1408 AddIdentifierRef(MD->getName(), Record);
1409 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001410 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001411 continue;
1412 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001413
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001414 // Notify the serialization listener that we're serializing this entity.
1415 if (SerializationListener)
1416 SerializationListener->SerializedPreprocessedEntity(*E,
1417 Stream.GetCurrentBitNo());
1418
1419 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1420 Record.push_back(IndexBase + NumPreprocessingRecords++);
1421 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1422 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1423 AddIdentifierRef(MI->getName(), Record);
1424 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1425 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1426 continue;
1427 }
1428
Douglas Gregor796d76a2010-10-20 22:00:55 +00001429 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1430 Record.push_back(PP_INCLUSION_DIRECTIVE);
1431 Record.push_back(IndexBase + NumPreprocessingRecords++);
1432 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1433 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1434 Record.push_back(ID->getFileName().size());
1435 Record.push_back(ID->wasInQuotes());
1436 Record.push_back(static_cast<unsigned>(ID->getKind()));
1437 llvm::SmallString<64> Buffer;
1438 Buffer += ID->getFileName();
1439 Buffer += ID->getFile()->getName();
1440 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1441 continue;
1442 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001443
1444 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregoraae92242010-03-19 21:51:54 +00001445 }
1446 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001447
Douglas Gregor8f45df52009-04-16 22:23:12 +00001448 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001449
Douglas Gregoraae92242010-03-19 21:51:54 +00001450 // Write the offsets table for the preprocessing record.
1451 if (NumPreprocessingRecords > 0) {
1452 // Write the offsets table for identifier IDs.
1453 using namespace llvm;
1454 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001455 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1459 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001460
Douglas Gregoraae92242010-03-19 21:51:54 +00001461 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001462 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001463 Record.push_back(NumPreprocessingRecords);
1464 Record.push_back(MacroDefinitionOffsets.size());
1465 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001466 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001467 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1468 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001469}
1470
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001471void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1472 RecordData Record;
1473 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001474 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i,Diag.GetCurDiagState());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001475 if (Map & 0x8) { // user mapping.
1476 Record.push_back(i);
1477 Record.push_back(Map & 0x7);
1478 }
1479 }
1480
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001481 if (!Record.empty())
1482 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001483}
1484
Douglas Gregorc5046832009-04-27 18:38:38 +00001485//===----------------------------------------------------------------------===//
1486// Type Serialization
1487//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001488
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001489/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001490void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001491 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001492 if (Idx.getIndex() == 0) // we haven't seen this type before.
1493 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001494
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001495 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001496
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001497 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001498 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001499 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001500 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001501 else if (TypeOffsets.size() < Index) {
1502 TypeOffsets.resize(Index + 1);
1503 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001504 }
1505
1506 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001507
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001508 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001509 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001510
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001511 if (T.hasLocalNonFastQualifiers()) {
1512 Qualifiers Qs = T.getLocalQualifiers();
1513 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001514 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001515 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001516 } else {
1517 switch (T->getTypeClass()) {
1518 // For all of the concrete, non-dependent types, call the
1519 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001520#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001521 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001522#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001523#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001524 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001525 }
1526
1527 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001528 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001529
1530 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001531 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001532}
1533
Douglas Gregorc5046832009-04-27 18:38:38 +00001534//===----------------------------------------------------------------------===//
1535// Declaration Serialization
1536//===----------------------------------------------------------------------===//
1537
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001538/// \brief Write the block containing all of the declaration IDs
1539/// lexically declared within the given DeclContext.
1540///
1541/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1542/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001543uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001544 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001545 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001546 return 0;
1547
Douglas Gregor8f45df52009-04-16 22:23:12 +00001548 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001549 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001550 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001551 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001552 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1553 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001554 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001555
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001556 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001557 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001558 reinterpret_cast<char*>(Decls.data()),
1559 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001560 return Offset;
1561}
1562
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001563void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001564 using namespace llvm;
1565 RecordData Record;
1566
1567 // Write the type offsets array
1568 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001569 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1572 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1573 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001574 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001575 Record.push_back(TypeOffsets.size());
1576 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001577 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001578 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1579
1580 // Write the declaration offsets array
1581 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001582 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1585 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1586 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001587 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001588 Record.push_back(DeclOffsets.size());
1589 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001590 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001591 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1592}
1593
Douglas Gregorc5046832009-04-27 18:38:38 +00001594//===----------------------------------------------------------------------===//
1595// Global Method Pool and Selector Serialization
1596//===----------------------------------------------------------------------===//
1597
Douglas Gregore84a9da2009-04-20 20:36:09 +00001598namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001599// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001600class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001601 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001602
1603public:
1604 typedef Selector key_type;
1605 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001606
Sebastian Redl834bb972010-08-04 17:20:04 +00001607 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001608 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001609 ObjCMethodList Instance, Factory;
1610 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001611 typedef const data_type& data_type_ref;
1612
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001613 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001614
Douglas Gregorc78d3462009-04-24 21:10:55 +00001615 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001616 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001617 }
Mike Stump11289f42009-09-09 15:08:12 +00001618
1619 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001620 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1621 data_type_ref Methods) {
1622 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1623 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001624 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1625 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001626 Method = Method->Next)
1627 if (Method->Method)
1628 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001629 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001630 Method = Method->Next)
1631 if (Method->Method)
1632 DataLen += 4;
1633 clang::io::Emit16(Out, DataLen);
1634 return std::make_pair(KeyLen, DataLen);
1635 }
Mike Stump11289f42009-09-09 15:08:12 +00001636
Douglas Gregor95c13f52009-04-25 17:48:32 +00001637 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001638 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001639 assert((Start >> 32) == 0 && "Selector key offset too large");
1640 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001641 unsigned N = Sel.getNumArgs();
1642 clang::io::Emit16(Out, N);
1643 if (N == 0)
1644 N = 1;
1645 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001646 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001647 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1648 }
Mike Stump11289f42009-09-09 15:08:12 +00001649
Douglas Gregorc78d3462009-04-24 21:10:55 +00001650 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001651 data_type_ref Methods, unsigned DataLen) {
1652 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001653 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001654 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001655 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001656 Method = Method->Next)
1657 if (Method->Method)
1658 ++NumInstanceMethods;
1659
1660 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001661 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001662 Method = Method->Next)
1663 if (Method->Method)
1664 ++NumFactoryMethods;
1665
1666 clang::io::Emit16(Out, NumInstanceMethods);
1667 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001668 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001669 Method = Method->Next)
1670 if (Method->Method)
1671 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001672 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001673 Method = Method->Next)
1674 if (Method->Method)
1675 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001676
1677 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001678 }
1679};
1680} // end anonymous namespace
1681
Sebastian Redla19a67f2010-08-03 21:58:15 +00001682/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001683///
1684/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001685/// in an on-disk hash table indexed by the selector. The hash table also
1686/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001687void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001688 using namespace llvm;
1689
Sebastian Redla19a67f2010-08-03 21:58:15 +00001690 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001691 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001692 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001693 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001694 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001695 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001696 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001697 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001698
Sebastian Redla19a67f2010-08-03 21:58:15 +00001699 // Create the on-disk hash table representation. We walk through every
1700 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001701 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001702 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001703 I = SelectorIDs.begin(), E = SelectorIDs.end();
1704 I != E; ++I) {
1705 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001706 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001707 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001708 I->second,
1709 ObjCMethodList(),
1710 ObjCMethodList()
1711 };
1712 if (F != SemaRef.MethodPool.end()) {
1713 Data.Instance = F->second.first;
1714 Data.Factory = F->second.second;
1715 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001716 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001717 // changed.
1718 if (Chain && I->second < FirstSelectorID) {
1719 // Selector already exists. Did it change?
1720 bool changed = false;
1721 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1722 M = M->Next) {
1723 if (M->Method->getPCHLevel() == 0)
1724 changed = true;
1725 }
1726 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1727 M = M->Next) {
1728 if (M->Method->getPCHLevel() == 0)
1729 changed = true;
1730 }
1731 if (!changed)
1732 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001733 } else if (Data.Instance.Method || Data.Factory.Method) {
1734 // A new method pool entry.
1735 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001736 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001737 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001738 }
1739
Douglas Gregorc78d3462009-04-24 21:10:55 +00001740 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001741 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001742 uint32_t BucketOffset;
1743 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001744 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001745 llvm::raw_svector_ostream Out(MethodPool);
1746 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001747 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001748 BucketOffset = Generator.Emit(Out, Trait);
1749 }
1750
1751 // Create a blob abbreviation
1752 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001753 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001754 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001755 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1757 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1758
Douglas Gregor95c13f52009-04-25 17:48:32 +00001759 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001760 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001761 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001762 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001763 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001764 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001765
1766 // Create a blob abbreviation for the selector table offsets.
1767 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001768 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1771 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1772
1773 // Write the selector offsets table.
1774 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001775 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001776 Record.push_back(SelectorOffsets.size());
1777 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001778 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001779 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001780 }
1781}
1782
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001783/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001784void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001785 using namespace llvm;
1786 if (SemaRef.ReferencedSelectors.empty())
1787 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001788
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001789 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001790
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001791 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001792 // very tricky to fix, and given that @selector shouldn't really appear in
1793 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001794 for (DenseMap<Selector, SourceLocation>::iterator S =
1795 SemaRef.ReferencedSelectors.begin(),
1796 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1797 Selector Sel = (*S).first;
1798 SourceLocation Loc = (*S).second;
1799 AddSelectorRef(Sel, Record);
1800 AddSourceLocation(Loc, Record);
1801 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001802 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001803}
1804
Douglas Gregorc5046832009-04-27 18:38:38 +00001805//===----------------------------------------------------------------------===//
1806// Identifier Table Serialization
1807//===----------------------------------------------------------------------===//
1808
Douglas Gregorc78d3462009-04-24 21:10:55 +00001809namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001810class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001811 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001812 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001813
Douglas Gregor1d583f22009-04-28 21:18:29 +00001814 /// \brief Determines whether this is an "interesting" identifier
1815 /// that needs a full IdentifierInfo structure written into the hash
1816 /// table.
1817 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1818 return II->isPoisoned() ||
1819 II->isExtensionToken() ||
1820 II->hasMacroDefinition() ||
1821 II->getObjCOrBuiltinID() ||
1822 II->getFETokenInfo<void>();
1823 }
1824
Douglas Gregore84a9da2009-04-20 20:36:09 +00001825public:
1826 typedef const IdentifierInfo* key_type;
1827 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001828
Sebastian Redl539c5062010-08-18 23:57:32 +00001829 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001830 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001831
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001832 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001833 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001834
1835 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001836 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001837 }
Mike Stump11289f42009-09-09 15:08:12 +00001838
1839 std::pair<unsigned,unsigned>
1840 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001841 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001842 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001843 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1844 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001845 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001846 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001847 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001848 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001849 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1850 DEnd = IdentifierResolver::end();
1851 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001852 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001853 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001854 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001855 // We emit the key length after the data length so that every
1856 // string is preceded by a 16-bit length. This matches the PTH
1857 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001858 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001859 return std::make_pair(KeyLen, DataLen);
1860 }
Mike Stump11289f42009-09-09 15:08:12 +00001861
1862 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001863 unsigned KeyLen) {
1864 // Record the location of the key data. This is used when generating
1865 // the mapping from persistent IDs to strings.
1866 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001867 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001868 }
Mike Stump11289f42009-09-09 15:08:12 +00001869
1870 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001871 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001872 if (!isInterestingIdentifier(II)) {
1873 clang::io::Emit32(Out, ID << 1);
1874 return;
1875 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001876
Douglas Gregor1d583f22009-04-28 21:18:29 +00001877 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001878 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001879 bool hasMacroDefinition =
1880 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001881 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001882 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001883 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1884 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1885 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001886 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001887 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001888 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001889
Douglas Gregorc3366a52009-04-21 23:56:24 +00001890 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001891 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001892
Douglas Gregora868bbd2009-04-21 22:25:48 +00001893 // Emit the declaration IDs in reverse order, because the
1894 // IdentifierResolver provides the declarations as they would be
1895 // visible (e.g., the function "stat" would come before the struct
1896 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1897 // adds declarations to the end of the list (so we need to see the
1898 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001899 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001900 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001901 IdentifierResolver::end());
1902 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1903 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001904 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001905 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001906 }
1907};
1908} // end anonymous namespace
1909
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001910/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001911///
1912/// The identifier table consists of a blob containing string data
1913/// (the actual identifiers themselves) and a separate "offsets" index
1914/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001915void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001916 using namespace llvm;
1917
1918 // Create and write out the blob that contains the identifier
1919 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001920 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001921 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001922 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001923
Douglas Gregore6648fb2009-04-28 20:33:11 +00001924 // Look for any identifiers that were named while processing the
1925 // headers, but are otherwise not needed. We add these to the hash
1926 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001927 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001928 // file.
1929 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1930 IDEnd = PP.getIdentifierTable().end();
1931 ID != IDEnd; ++ID)
1932 getIdentifierRef(ID->second);
1933
Sebastian Redlff4a2952010-07-23 23:49:55 +00001934 // Create the on-disk hash table representation. We only store offsets
1935 // for identifiers that appear here for the first time.
1936 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001937 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001938 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1939 ID != IDEnd; ++ID) {
1940 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001941 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001942 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001943 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001944
Douglas Gregore84a9da2009-04-20 20:36:09 +00001945 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001946 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001947 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001948 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001949 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001950 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001951 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001952 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001953 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001954 }
1955
1956 // Create a blob abbreviation
1957 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001958 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001961 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001962
1963 // Write the identifier table
1964 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001965 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001966 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001967 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001968 }
1969
1970 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001971 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001972 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1975 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1976
1977 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001978 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001979 Record.push_back(IdentifierOffsets.size());
1980 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001981 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001982 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001983}
1984
Douglas Gregorc5046832009-04-27 18:38:38 +00001985//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001986// DeclContext's Name Lookup Table Serialization
1987//===----------------------------------------------------------------------===//
1988
1989namespace {
1990// Trait used for the on-disk hash table used in the method pool.
1991class ASTDeclContextNameLookupTrait {
1992 ASTWriter &Writer;
1993
1994public:
1995 typedef DeclarationName key_type;
1996 typedef key_type key_type_ref;
1997
1998 typedef DeclContext::lookup_result data_type;
1999 typedef const data_type& data_type_ref;
2000
2001 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2002
2003 unsigned ComputeHash(DeclarationName Name) {
2004 llvm::FoldingSetNodeID ID;
2005 ID.AddInteger(Name.getNameKind());
2006
2007 switch (Name.getNameKind()) {
2008 case DeclarationName::Identifier:
2009 ID.AddString(Name.getAsIdentifierInfo()->getName());
2010 break;
2011 case DeclarationName::ObjCZeroArgSelector:
2012 case DeclarationName::ObjCOneArgSelector:
2013 case DeclarationName::ObjCMultiArgSelector:
2014 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2015 break;
2016 case DeclarationName::CXXConstructorName:
2017 case DeclarationName::CXXDestructorName:
2018 case DeclarationName::CXXConversionFunctionName:
2019 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2020 break;
2021 case DeclarationName::CXXOperatorName:
2022 ID.AddInteger(Name.getCXXOverloadedOperator());
2023 break;
2024 case DeclarationName::CXXLiteralOperatorName:
2025 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2026 case DeclarationName::CXXUsingDirective:
2027 break;
2028 }
2029
2030 return ID.ComputeHash();
2031 }
2032
2033 std::pair<unsigned,unsigned>
2034 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2035 data_type_ref Lookup) {
2036 unsigned KeyLen = 1;
2037 switch (Name.getNameKind()) {
2038 case DeclarationName::Identifier:
2039 case DeclarationName::ObjCZeroArgSelector:
2040 case DeclarationName::ObjCOneArgSelector:
2041 case DeclarationName::ObjCMultiArgSelector:
2042 case DeclarationName::CXXConstructorName:
2043 case DeclarationName::CXXDestructorName:
2044 case DeclarationName::CXXConversionFunctionName:
2045 case DeclarationName::CXXLiteralOperatorName:
2046 KeyLen += 4;
2047 break;
2048 case DeclarationName::CXXOperatorName:
2049 KeyLen += 1;
2050 break;
2051 case DeclarationName::CXXUsingDirective:
2052 break;
2053 }
2054 clang::io::Emit16(Out, KeyLen);
2055
2056 // 2 bytes for num of decls and 4 for each DeclID.
2057 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2058 clang::io::Emit16(Out, DataLen);
2059
2060 return std::make_pair(KeyLen, DataLen);
2061 }
2062
2063 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2064 using namespace clang::io;
2065
2066 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2067 Emit8(Out, Name.getNameKind());
2068 switch (Name.getNameKind()) {
2069 case DeclarationName::Identifier:
2070 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2071 break;
2072 case DeclarationName::ObjCZeroArgSelector:
2073 case DeclarationName::ObjCOneArgSelector:
2074 case DeclarationName::ObjCMultiArgSelector:
2075 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2076 break;
2077 case DeclarationName::CXXConstructorName:
2078 case DeclarationName::CXXDestructorName:
2079 case DeclarationName::CXXConversionFunctionName:
2080 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2081 break;
2082 case DeclarationName::CXXOperatorName:
2083 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2084 Emit8(Out, Name.getCXXOverloadedOperator());
2085 break;
2086 case DeclarationName::CXXLiteralOperatorName:
2087 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2088 break;
2089 case DeclarationName::CXXUsingDirective:
2090 break;
2091 }
2092 }
2093
2094 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2095 data_type Lookup, unsigned DataLen) {
2096 uint64_t Start = Out.tell(); (void)Start;
2097 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2098 for (; Lookup.first != Lookup.second; ++Lookup.first)
2099 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2100
2101 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2102 }
2103};
2104} // end anonymous namespace
2105
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002106/// \brief Write the block containing all of the declaration IDs
2107/// visible from the given DeclContext.
2108///
2109/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002110/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002111uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2112 DeclContext *DC) {
2113 if (DC->getPrimaryContext() != DC)
2114 return 0;
2115
2116 // Since there is no name lookup into functions or methods, don't bother to
2117 // build a visible-declarations table for these entities.
2118 if (DC->isFunctionOrMethod())
2119 return 0;
2120
2121 // If not in C++, we perform name lookup for the translation unit via the
2122 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2123 // FIXME: In C++ we need the visible declarations in order to "see" the
2124 // friend declarations, is there a way to do this without writing the table ?
2125 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2126 return 0;
2127
2128 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002129 if (DC->hasExternalVisibleStorage())
2130 DC->MaterializeVisibleDeclsFromExternalStorage();
2131 else
2132 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002133
2134 // Serialize the contents of the mapping used for lookup. Note that,
2135 // although we have two very different code paths, the serialized
2136 // representation is the same for both cases: a declaration name,
2137 // followed by a size, followed by references to the visible
2138 // declarations that have that name.
2139 uint64_t Offset = Stream.GetCurrentBitNo();
2140 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2141 if (!Map || Map->empty())
2142 return 0;
2143
2144 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2145 ASTDeclContextNameLookupTrait Trait(*this);
2146
2147 // Create the on-disk hash table representation.
2148 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2149 D != DEnd; ++D) {
2150 DeclarationName Name = D->first;
2151 DeclContext::lookup_result Result = D->second.getLookupResult();
2152 Generator.insert(Name, Result, Trait);
2153 }
2154
2155 // Create the on-disk hash table in a buffer.
2156 llvm::SmallString<4096> LookupTable;
2157 uint32_t BucketOffset;
2158 {
2159 llvm::raw_svector_ostream Out(LookupTable);
2160 // Make sure that no bucket is at offset 0
2161 clang::io::Emit32(Out, 0);
2162 BucketOffset = Generator.Emit(Out, Trait);
2163 }
2164
2165 // Write the lookup table
2166 RecordData Record;
2167 Record.push_back(DECL_CONTEXT_VISIBLE);
2168 Record.push_back(BucketOffset);
2169 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2170 LookupTable.str());
2171
2172 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2173 ++NumVisibleDeclContexts;
2174 return Offset;
2175}
2176
Sebastian Redla4071b42010-08-24 00:50:09 +00002177/// \brief Write an UPDATE_VISIBLE block for the given context.
2178///
2179/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2180/// DeclContext in a dependent AST file. As such, they only exist for the TU
2181/// (in C++) and for namespaces.
2182void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002183 // Make the context build its lookup table, but don't make it load external
2184 // decls.
2185 DC->lookup(DeclarationName());
2186
2187 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2188 if (!Map || Map->empty())
2189 return;
2190
2191 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2192 ASTDeclContextNameLookupTrait Trait(*this);
2193
2194 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002195 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2196 D != DEnd; ++D) {
2197 DeclarationName Name = D->first;
2198 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002199 // For any name that appears in this table, the results are complete, i.e.
2200 // they overwrite results from previous PCHs. Merging is always a mess.
2201 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002202 }
2203
2204 // Create the on-disk hash table in a buffer.
2205 llvm::SmallString<4096> LookupTable;
2206 uint32_t BucketOffset;
2207 {
2208 llvm::raw_svector_ostream Out(LookupTable);
2209 // Make sure that no bucket is at offset 0
2210 clang::io::Emit32(Out, 0);
2211 BucketOffset = Generator.Emit(Out, Trait);
2212 }
2213
2214 // Write the lookup table
2215 RecordData Record;
2216 Record.push_back(UPDATE_VISIBLE);
2217 Record.push_back(getDeclID(cast<Decl>(DC)));
2218 Record.push_back(BucketOffset);
2219 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2220}
2221
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002222//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002223// General Serialization Routines
2224//===----------------------------------------------------------------------===//
2225
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002226/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002227void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002228 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002229 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2230 const Attr * A = *i;
2231 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2232 AddSourceLocation(A->getLocation(), Record);
2233 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002234
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002235#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002236
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002237 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002238}
2239
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002240void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002241 Record.push_back(Str.size());
2242 Record.insert(Record.end(), Str.begin(), Str.end());
2243}
2244
Douglas Gregore84a9da2009-04-20 20:36:09 +00002245/// \brief Note that the identifier II occurs at the given offset
2246/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002247void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002248 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002249 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002250 // up earlier in the chain and thus don't need an offset.
2251 if (ID >= FirstIdentID)
2252 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002253}
2254
Douglas Gregor95c13f52009-04-25 17:48:32 +00002255/// \brief Note that the selector Sel occurs at the given offset
2256/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002257void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002258 unsigned ID = SelectorIDs[Sel];
2259 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002260 // Don't record offsets for selectors that are also available in a different
2261 // file.
2262 if (ID < FirstSelectorID)
2263 return;
2264 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002265}
2266
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002267ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002268 : Stream(Stream), Chain(0), SerializationListener(0),
2269 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002270 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002271 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002272 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2273 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002274 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002275 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2276 NextCXXBaseSpecifiersID(1)
2277{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002278}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002279
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002280void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002281 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002282 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002283 Stream.Emit((unsigned)'C', 8);
2284 Stream.Emit((unsigned)'P', 8);
2285 Stream.Emit((unsigned)'C', 8);
2286 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002287
Chris Lattner28fa4e62009-04-26 22:26:21 +00002288 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002289
Sebastian Redl143413f2010-07-12 22:02:52 +00002290 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002291 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002292 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002293 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002294}
2295
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002296void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002297 const char *isysroot) {
2298 using namespace llvm;
2299
2300 ASTContext &Context = SemaRef.Context;
2301 Preprocessor &PP = SemaRef.PP;
2302
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002303 // The translation unit is the first declaration we'll emit.
2304 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002305 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002306 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002307
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002308 // Make sure that we emit IdentifierInfos (and any attached
2309 // declarations) for builtins.
2310 {
2311 IdentifierTable &Table = PP.getIdentifierTable();
2312 llvm::SmallVector<const char *, 32> BuiltinNames;
2313 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2314 Context.getLangOptions().NoBuiltin);
2315 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2316 getIdentifierRef(&Table.get(BuiltinNames[I]));
2317 }
2318
Chris Lattner0c797362009-09-08 18:19:27 +00002319 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002320 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002321 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002322 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002323 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2324 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002325 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002326
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002327 // Build a record containing all of the file scoped decls in this file.
2328 RecordData UnusedFileScopedDecls;
2329 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2330 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002331
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002332 RecordData WeakUndeclaredIdentifiers;
2333 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2334 WeakUndeclaredIdentifiers.push_back(
2335 SemaRef.WeakUndeclaredIdentifiers.size());
2336 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2337 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2338 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2339 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2340 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2341 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2342 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2343 }
2344 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002345
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002346 // Build a record containing all of the locally-scoped external
2347 // declarations in this header file. Generally, this record will be
2348 // empty.
2349 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002350 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002351 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002352 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002353 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2354 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2355 TD != TDEnd; ++TD)
2356 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2357
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002358 // Build a record containing all of the ext_vector declarations.
2359 RecordData ExtVectorDecls;
2360 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2361 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2362
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002363 // Build a record containing all of the VTable uses information.
2364 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002365 if (!SemaRef.VTableUses.empty()) {
2366 VTableUses.push_back(SemaRef.VTableUses.size());
2367 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2368 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2369 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2370 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2371 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002372 }
2373
2374 // Build a record containing all of dynamic classes declarations.
2375 RecordData DynamicClasses;
2376 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2377 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2378
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002379 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002380 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002381 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002382 I = SemaRef.PendingInstantiations.begin(),
2383 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2384 AddDeclRef(I->first, PendingInstantiations);
2385 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002386 }
2387 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2388 "There are local ones at end of translation unit!");
2389
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002390 // Build a record containing some declaration references.
2391 RecordData SemaDeclRefs;
2392 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2393 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2394 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2395 }
2396
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002397 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002398 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002399 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002400 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002401 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002402 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002403 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002404 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002405 // Write the record of special types.
2406 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002407
Steve Naroffc277ad12009-07-18 15:33:26 +00002408 AddTypeRef(Context.getBuiltinVaListType(), Record);
2409 AddTypeRef(Context.getObjCIdType(), Record);
2410 AddTypeRef(Context.getObjCSelType(), Record);
2411 AddTypeRef(Context.getObjCProtoType(), Record);
2412 AddTypeRef(Context.getObjCClassType(), Record);
2413 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2414 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2415 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002416 AddTypeRef(Context.getjmp_bufType(), Record);
2417 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002418 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2419 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002420 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002421 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002422 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2423 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002424 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002425 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002426
Douglas Gregor1970d882009-04-26 03:49:13 +00002427 // Keep writing types and declarations until all types and
2428 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002429 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002430 WriteDeclsBlockAbbrevs();
2431 while (!DeclTypesToEmit.empty()) {
2432 DeclOrType DOT = DeclTypesToEmit.front();
2433 DeclTypesToEmit.pop();
2434 if (DOT.isType())
2435 WriteType(DOT.getType());
2436 else
2437 WriteDecl(Context, DOT.getDecl());
2438 }
2439 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002440
Douglas Gregor45053152009-10-17 17:25:45 +00002441 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002442 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002443 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002444 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002445
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002446 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002447 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002448
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002449 // Write the C++ base-specifier set offsets.
2450 if (!CXXBaseSpecifiersOffsets.empty()) {
2451 // Create a blob abbreviation for the C++ base specifiers offsets.
2452 using namespace llvm;
2453
2454 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2455 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2458 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2459
2460 // Write the selector offsets table.
2461 Record.clear();
2462 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2463 Record.push_back(CXXBaseSpecifiersOffsets.size());
2464 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2465 (const char *)CXXBaseSpecifiersOffsets.data(),
2466 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2467 }
2468
Douglas Gregord4df8652009-04-22 22:02:47 +00002469 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002470 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002471 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002472
2473 // Write the record containing tentative definitions.
2474 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002475 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002476
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002477 // Write the record containing unused file scoped decls.
2478 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002479 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002480
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002481 // Write the record containing weak undeclared identifiers.
2482 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002483 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002484 WeakUndeclaredIdentifiers);
2485
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002486 // Write the record containing locally-scoped external definitions.
2487 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002488 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002489 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002490
2491 // Write the record containing ext_vector type names.
2492 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002493 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002494
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002495 // Write the record containing VTable uses information.
2496 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002497 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002498
2499 // Write the record containing dynamic classes declarations.
2500 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002501 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002502
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002503 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002504 if (!PendingInstantiations.empty())
2505 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002506
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002507 // Write the record containing declaration references of Sema.
2508 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002509 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002510
Douglas Gregor08f01292009-04-17 22:13:46 +00002511 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002512 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002513 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002514 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002515 Record.push_back(NumLexicalDeclContexts);
2516 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002517 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002518 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002519}
2520
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002521void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002522 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002523 using namespace llvm;
2524
2525 ASTContext &Context = SemaRef.Context;
2526 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002527
Sebastian Redl143413f2010-07-12 22:02:52 +00002528 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002529 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002530 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002531 if (StatCalls && !isysroot)
2532 WriteStatCache(*StatCalls);
2533 // FIXME: Source manager block should only write new stuff, which could be
2534 // done by tracking the largest ID in the chain
2535 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002536
2537 // The special types are in the chained PCH.
2538
2539 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002540 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002541 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002542 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002543 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2544 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002545 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002546 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002547 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002548 else if ((*I)->isChangedSinceDeserialization())
2549 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002550 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002551 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002552 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002553 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002554 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2555 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2556 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002557 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002558 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2559 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002560 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002561 // And a visible updates block for the DeclContexts.
2562 Abv = new llvm::BitCodeAbbrev();
2563 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2564 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2565 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2566 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2567 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2568 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002569
Sebastian Redl98912122010-07-27 23:01:28 +00002570 // Build a record containing all of the new tentative definitions in this
2571 // file, in TentativeDefinitions order.
2572 RecordData TentativeDefinitions;
2573 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2574 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2575 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2576 }
2577
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002578 // Build a record containing all of the file scoped decls in this file.
2579 RecordData UnusedFileScopedDecls;
2580 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2581 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2582 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002583 }
2584
Sebastian Redl08aca90252010-08-05 18:21:25 +00002585 // We write the entire table, overwriting the tables from the chain.
2586 RecordData WeakUndeclaredIdentifiers;
2587 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2588 WeakUndeclaredIdentifiers.push_back(
2589 SemaRef.WeakUndeclaredIdentifiers.size());
2590 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2591 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2592 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2593 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2594 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2595 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2596 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2597 }
2598 }
2599
Sebastian Redl98912122010-07-27 23:01:28 +00002600 // Build a record containing all of the locally-scoped external
2601 // declarations in this header file. Generally, this record will be
2602 // empty.
2603 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002604 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002605 // nondeterminstic!
2606 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2607 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2608 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2609 TD != TDEnd; ++TD) {
2610 if (TD->second->getPCHLevel() == 0)
2611 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2612 }
2613
2614 // Build a record containing all of the ext_vector declarations.
2615 RecordData ExtVectorDecls;
2616 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2617 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2618 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2619 }
2620
Sebastian Redl08aca90252010-08-05 18:21:25 +00002621 // Build a record containing all of the VTable uses information.
2622 // We write everything here, because it's too hard to determine whether
2623 // a use is new to this part.
2624 RecordData VTableUses;
2625 if (!SemaRef.VTableUses.empty()) {
2626 VTableUses.push_back(SemaRef.VTableUses.size());
2627 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2628 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2629 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2630 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2631 }
2632 }
2633
2634 // Build a record containing all of dynamic classes declarations.
2635 RecordData DynamicClasses;
2636 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2637 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2638 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2639
2640 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002641 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002642 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002643 I = SemaRef.PendingInstantiations.begin(),
2644 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002645 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002646 AddDeclRef(I->first, PendingInstantiations);
2647 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002648 }
2649 }
2650 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2651 "There are local ones at end of translation unit!");
2652
2653 // Build a record containing some declaration references.
2654 // It's not worth the effort to avoid duplication here.
2655 RecordData SemaDeclRefs;
2656 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2657 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2658 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2659 }
2660
Sebastian Redl539c5062010-08-18 23:57:32 +00002661 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002662 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002663 for (DeclsToRewriteTy::iterator
2664 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2665 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002666 while (!DeclTypesToEmit.empty()) {
2667 DeclOrType DOT = DeclTypesToEmit.front();
2668 DeclTypesToEmit.pop();
2669 if (DOT.isType())
2670 WriteType(DOT.getType());
2671 else
2672 WriteDecl(Context, DOT.getDecl());
2673 }
2674 Stream.ExitBlock();
2675
Sebastian Redl98912122010-07-27 23:01:28 +00002676 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002677 WriteSelectors(SemaRef);
2678 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002679 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002680 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002681 // FIXME: For chained PCH only write the new mappings (we currently
2682 // write all of them again).
2683 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002684
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002685 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002686 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002687 RecordData FirstLatestDeclIDs;
2688 for (FirstLatestDeclMap::iterator
2689 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2690 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2691 "Expected first & second to be in different PCHs");
2692 AddDeclRef(I->first, FirstLatestDeclIDs);
2693 AddDeclRef(I->second, FirstLatestDeclIDs);
2694 }
2695 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002696 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002697
Sebastian Redl98912122010-07-27 23:01:28 +00002698 // Write the record containing external, unnamed definitions.
2699 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002700 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002701
2702 // Write the record containing tentative definitions.
2703 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002704 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002705
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002706 // Write the record containing unused file scoped decls.
2707 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002708 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002709
Sebastian Redl08aca90252010-08-05 18:21:25 +00002710 // Write the record containing weak undeclared identifiers.
2711 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002712 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002713 WeakUndeclaredIdentifiers);
2714
Sebastian Redl98912122010-07-27 23:01:28 +00002715 // Write the record containing locally-scoped external definitions.
2716 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002717 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002718 LocallyScopedExternalDecls);
2719
2720 // Write the record containing ext_vector type names.
2721 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002722 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002723
Sebastian Redl08aca90252010-08-05 18:21:25 +00002724 // Write the record containing VTable uses information.
2725 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002726 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002727
2728 // Write the record containing dynamic classes declarations.
2729 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002730 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002731
2732 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002733 if (!PendingInstantiations.empty())
2734 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002735
2736 // Write the record containing declaration references of Sema.
2737 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002738 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002739
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002740 // Write the updates to DeclContexts.
2741 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2742 I = UpdatedDeclContexts.begin(),
2743 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002744 I != E; ++I)
2745 WriteDeclContextVisibleUpdate(*I);
2746
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002747 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002748
Sebastian Redl98912122010-07-27 23:01:28 +00002749 Record.clear();
2750 Record.push_back(NumStatements);
2751 Record.push_back(NumMacros);
2752 Record.push_back(NumLexicalDeclContexts);
2753 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002754 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002755 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002756 Stream.ExitBlock();
2757}
2758
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002759void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002760 if (DeclUpdates.empty())
2761 return;
2762
2763 RecordData OffsetsRecord;
2764 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2765 for (DeclUpdateMap::iterator
2766 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2767 const Decl *D = I->first;
2768 UpdateRecord &URec = I->second;
2769
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002770 if (DeclsToRewrite.count(D))
2771 continue; // The decl will be written completely,no need to store updates.
2772
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002773 uint64_t Offset = Stream.GetCurrentBitNo();
2774 Stream.EmitRecord(DECL_UPDATES, URec);
2775
2776 OffsetsRecord.push_back(GetDeclRef(D));
2777 OffsetsRecord.push_back(Offset);
2778 }
2779 Stream.ExitBlock();
2780 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2781}
2782
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002783void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002784 if (ReplacedDecls.empty())
2785 return;
2786
2787 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002788 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002789 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2790 Record.push_back(I->first);
2791 Record.push_back(I->second);
2792 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002793 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002794}
2795
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002796void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002797 Record.push_back(Loc.getRawEncoding());
2798}
2799
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002800void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002801 AddSourceLocation(Range.getBegin(), Record);
2802 AddSourceLocation(Range.getEnd(), Record);
2803}
2804
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002805void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002806 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002807 const uint64_t *Words = Value.getRawData();
2808 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002809}
2810
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002811void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002812 Record.push_back(Value.isUnsigned());
2813 AddAPInt(Value, Record);
2814}
2815
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002816void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002817 AddAPInt(Value.bitcastToAPInt(), Record);
2818}
2819
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002820void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002821 Record.push_back(getIdentifierRef(II));
2822}
2823
Sebastian Redl539c5062010-08-18 23:57:32 +00002824IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002825 if (II == 0)
2826 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002827
Sebastian Redl539c5062010-08-18 23:57:32 +00002828 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002829 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002830 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002831 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002832}
2833
Sebastian Redl50e26582010-09-15 19:54:06 +00002834MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002835 if (MD == 0)
2836 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002837
2838 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002839 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002840 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002841 return ID;
2842}
2843
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002844void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002845 Record.push_back(getSelectorRef(SelRef));
2846}
2847
Sebastian Redl539c5062010-08-18 23:57:32 +00002848SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002849 if (Sel.getAsOpaquePtr() == 0) {
2850 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002851 }
2852
Sebastian Redl539c5062010-08-18 23:57:32 +00002853 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002854 if (SID == 0 && Chain) {
2855 // This might trigger a ReadSelector callback, which will set the ID for
2856 // this selector.
2857 Chain->LoadSelector(Sel);
2858 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002859 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002860 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002861 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002862 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002863}
2864
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002865void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002866 AddDeclRef(Temp->getDestructor(), Record);
2867}
2868
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002869void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2870 CXXBaseSpecifier const *BasesEnd,
2871 RecordDataImpl &Record) {
2872 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2873 CXXBaseSpecifiersToWrite.push_back(
2874 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2875 Bases, BasesEnd));
2876 Record.push_back(NextCXXBaseSpecifiersID++);
2877}
2878
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002879void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002880 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002881 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002882 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002883 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002884 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002885 break;
2886 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002887 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002888 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002889 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002890 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2891 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00002892 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002893 break;
John McCall0ad16662009-10-29 08:12:44 +00002894 case TemplateArgument::Null:
2895 case TemplateArgument::Integral:
2896 case TemplateArgument::Declaration:
2897 case TemplateArgument::Pack:
2898 break;
2899 }
2900}
2901
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002902void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002903 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002904 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002905
2906 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2907 bool InfoHasSameExpr
2908 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2909 Record.push_back(InfoHasSameExpr);
2910 if (InfoHasSameExpr)
2911 return; // Avoid storing the same expr twice.
2912 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002913 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2914 Record);
2915}
2916
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002917void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002918 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002919 AddTypeRef(QualType(), Record);
2920 return;
2921 }
2922
John McCallbcd03502009-12-07 02:54:59 +00002923 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002924 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002925 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002926 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002927}
2928
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002929void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002930 Record.push_back(GetOrCreateTypeID(T));
2931}
2932
2933TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002934 return MakeTypeID(T,
2935 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2936}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002937
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002938TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002939 return MakeTypeID(T,
2940 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002941}
2942
2943TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2944 if (T.isNull())
2945 return TypeIdx();
2946 assert(!T.getLocalFastQualifiers());
2947
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002948 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002949 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002950 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002951 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002952 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002953 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002954 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002955 return Idx;
2956}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002957
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002958TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002959 if (T.isNull())
2960 return TypeIdx();
2961 assert(!T.getLocalFastQualifiers());
2962
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002963 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2964 assert(I != TypeIdxs.end() && "Type not emitted!");
2965 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002966}
2967
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002968void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002969 Record.push_back(GetDeclRef(D));
2970}
2971
Sebastian Redl539c5062010-08-18 23:57:32 +00002972DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002973 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002974 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002975 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002976 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00002977 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002978 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002979 // We haven't seen this declaration before. Give it a new ID and
2980 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002981 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002982 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002983 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2984 // We don't add it to the replacement collection here, because we don't
2985 // have the offset yet.
2986 DeclTypesToEmit.push(const_cast<Decl *>(D));
2987 // Reset the flag, so that we don't add this decl multiple times.
2988 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002989 }
2990
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002991 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002992}
2993
Sebastian Redl539c5062010-08-18 23:57:32 +00002994DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002995 if (D == 0)
2996 return 0;
2997
2998 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2999 return DeclIDs[D];
3000}
3001
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003002void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003003 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003004 Record.push_back(Name.getNameKind());
3005 switch (Name.getNameKind()) {
3006 case DeclarationName::Identifier:
3007 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3008 break;
3009
3010 case DeclarationName::ObjCZeroArgSelector:
3011 case DeclarationName::ObjCOneArgSelector:
3012 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003013 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003014 break;
3015
3016 case DeclarationName::CXXConstructorName:
3017 case DeclarationName::CXXDestructorName:
3018 case DeclarationName::CXXConversionFunctionName:
3019 AddTypeRef(Name.getCXXNameType(), Record);
3020 break;
3021
3022 case DeclarationName::CXXOperatorName:
3023 Record.push_back(Name.getCXXOverloadedOperator());
3024 break;
3025
Alexis Hunt3d221f22009-11-29 07:34:05 +00003026 case DeclarationName::CXXLiteralOperatorName:
3027 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3028 break;
3029
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003030 case DeclarationName::CXXUsingDirective:
3031 // No extra data to emit
3032 break;
3033 }
3034}
Chris Lattnerca025db2010-05-07 21:43:38 +00003035
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003036void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003037 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003038 switch (Name.getNameKind()) {
3039 case DeclarationName::CXXConstructorName:
3040 case DeclarationName::CXXDestructorName:
3041 case DeclarationName::CXXConversionFunctionName:
3042 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3043 break;
3044
3045 case DeclarationName::CXXOperatorName:
3046 AddSourceLocation(
3047 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3048 Record);
3049 AddSourceLocation(
3050 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3051 Record);
3052 break;
3053
3054 case DeclarationName::CXXLiteralOperatorName:
3055 AddSourceLocation(
3056 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3057 Record);
3058 break;
3059
3060 case DeclarationName::Identifier:
3061 case DeclarationName::ObjCZeroArgSelector:
3062 case DeclarationName::ObjCOneArgSelector:
3063 case DeclarationName::ObjCMultiArgSelector:
3064 case DeclarationName::CXXUsingDirective:
3065 break;
3066 }
3067}
3068
3069void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003070 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003071 AddDeclarationName(NameInfo.getName(), Record);
3072 AddSourceLocation(NameInfo.getLoc(), Record);
3073 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3074}
3075
3076void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003077 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003078 AddNestedNameSpecifier(Info.NNS, Record);
3079 AddSourceRange(Info.NNSRange, Record);
3080 Record.push_back(Info.NumTemplParamLists);
3081 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3082 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3083}
3084
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003085void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003086 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003087 // Nested name specifiers usually aren't too long. I think that 8 would
3088 // typically accomodate the vast majority.
3089 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3090
3091 // Push each of the NNS's onto a stack for serialization in reverse order.
3092 while (NNS) {
3093 NestedNames.push_back(NNS);
3094 NNS = NNS->getPrefix();
3095 }
3096
3097 Record.push_back(NestedNames.size());
3098 while(!NestedNames.empty()) {
3099 NNS = NestedNames.pop_back_val();
3100 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3101 Record.push_back(Kind);
3102 switch (Kind) {
3103 case NestedNameSpecifier::Identifier:
3104 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3105 break;
3106
3107 case NestedNameSpecifier::Namespace:
3108 AddDeclRef(NNS->getAsNamespace(), Record);
3109 break;
3110
3111 case NestedNameSpecifier::TypeSpec:
3112 case NestedNameSpecifier::TypeSpecWithTemplate:
3113 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3114 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3115 break;
3116
3117 case NestedNameSpecifier::Global:
3118 // Don't need to write an associated value.
3119 break;
3120 }
3121 }
3122}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003123
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003124void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003125 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003126 Record.push_back(Kind);
3127 switch (Kind) {
3128 case TemplateName::Template:
3129 AddDeclRef(Name.getAsTemplateDecl(), Record);
3130 break;
3131
3132 case TemplateName::OverloadedTemplate: {
3133 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3134 Record.push_back(OvT->size());
3135 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3136 I != E; ++I)
3137 AddDeclRef(*I, Record);
3138 break;
3139 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003140
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003141 case TemplateName::QualifiedTemplate: {
3142 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3143 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3144 Record.push_back(QualT->hasTemplateKeyword());
3145 AddDeclRef(QualT->getTemplateDecl(), Record);
3146 break;
3147 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003148
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003149 case TemplateName::DependentTemplate: {
3150 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3151 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3152 Record.push_back(DepT->isIdentifier());
3153 if (DepT->isIdentifier())
3154 AddIdentifierRef(DepT->getIdentifier(), Record);
3155 else
3156 Record.push_back(DepT->getOperator());
3157 break;
3158 }
3159 }
3160}
3161
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003162void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003163 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003164 Record.push_back(Arg.getKind());
3165 switch (Arg.getKind()) {
3166 case TemplateArgument::Null:
3167 break;
3168 case TemplateArgument::Type:
3169 AddTypeRef(Arg.getAsType(), Record);
3170 break;
3171 case TemplateArgument::Declaration:
3172 AddDeclRef(Arg.getAsDecl(), Record);
3173 break;
3174 case TemplateArgument::Integral:
3175 AddAPSInt(*Arg.getAsIntegral(), Record);
3176 AddTypeRef(Arg.getIntegralType(), Record);
3177 break;
3178 case TemplateArgument::Template:
3179 AddTemplateName(Arg.getAsTemplate(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003180 Record.push_back(Arg.isPackExpansion());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003181 break;
3182 case TemplateArgument::Expression:
3183 AddStmt(Arg.getAsExpr());
3184 break;
3185 case TemplateArgument::Pack:
3186 Record.push_back(Arg.pack_size());
3187 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3188 I != E; ++I)
3189 AddTemplateArgument(*I, Record);
3190 break;
3191 }
3192}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003193
3194void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003195ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003196 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003197 assert(TemplateParams && "No TemplateParams!");
3198 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3199 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3200 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3201 Record.push_back(TemplateParams->size());
3202 for (TemplateParameterList::const_iterator
3203 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3204 P != PEnd; ++P)
3205 AddDeclRef(*P, Record);
3206}
3207
3208/// \brief Emit a template argument list.
3209void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003210ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003211 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003212 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003213 Record.push_back(TemplateArgs->size());
3214 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003215 AddTemplateArgument(TemplateArgs->get(i), Record);
3216}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003217
3218
3219void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003220ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003221 Record.push_back(Set.size());
3222 for (UnresolvedSetImpl::const_iterator
3223 I = Set.begin(), E = Set.end(); I != E; ++I) {
3224 AddDeclRef(I.getDecl(), Record);
3225 Record.push_back(I.getAccess());
3226 }
3227}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003228
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003229void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003230 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003231 Record.push_back(Base.isVirtual());
3232 Record.push_back(Base.isBaseOfClass());
3233 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003234 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003235 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003236 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3237 : SourceLocation(),
3238 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003239}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003240
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003241void ASTWriter::FlushCXXBaseSpecifiers() {
3242 RecordData Record;
3243 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3244 Record.clear();
3245
3246 // Record the offset of this base-specifier set.
3247 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3248 if (Index == CXXBaseSpecifiersOffsets.size())
3249 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3250 else {
3251 if (Index > CXXBaseSpecifiersOffsets.size())
3252 CXXBaseSpecifiersOffsets.resize(Index + 1);
3253 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3254 }
3255
3256 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3257 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3258 Record.push_back(BEnd - B);
3259 for (; B != BEnd; ++B)
3260 AddCXXBaseSpecifier(*B, Record);
3261 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003262
3263 // Flush any expressions that were written as part of the base specifiers.
3264 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003265 }
3266
3267 CXXBaseSpecifiersToWrite.clear();
3268}
3269
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003270void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003271 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003272 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003273 Record.push_back(NumBaseOrMembers);
3274 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3275 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3276
3277 Record.push_back(Init->isBaseInitializer());
3278 if (Init->isBaseInitializer()) {
3279 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3280 Record.push_back(Init->isBaseVirtual());
3281 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003282 Record.push_back(Init->isIndirectMemberInitializer());
3283 if (Init->isIndirectMemberInitializer())
3284 AddDeclRef(Init->getIndirectMember(), Record);
3285 else
3286 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003287 }
Francois Pichetd583da02010-12-04 09:14:42 +00003288
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003289 AddSourceLocation(Init->getMemberLocation(), Record);
3290 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003291 AddSourceLocation(Init->getLParenLoc(), Record);
3292 AddSourceLocation(Init->getRParenLoc(), Record);
3293 Record.push_back(Init->isWritten());
3294 if (Init->isWritten()) {
3295 Record.push_back(Init->getSourceOrder());
3296 } else {
3297 Record.push_back(Init->getNumArrayIndices());
3298 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3299 AddDeclRef(Init->getArrayIndex(i), Record);
3300 }
3301 }
3302}
3303
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003304void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3305 assert(D->DefinitionData);
3306 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3307 Record.push_back(Data.UserDeclaredConstructor);
3308 Record.push_back(Data.UserDeclaredCopyConstructor);
3309 Record.push_back(Data.UserDeclaredCopyAssignment);
3310 Record.push_back(Data.UserDeclaredDestructor);
3311 Record.push_back(Data.Aggregate);
3312 Record.push_back(Data.PlainOldData);
3313 Record.push_back(Data.Empty);
3314 Record.push_back(Data.Polymorphic);
3315 Record.push_back(Data.Abstract);
3316 Record.push_back(Data.HasTrivialConstructor);
3317 Record.push_back(Data.HasTrivialCopyConstructor);
3318 Record.push_back(Data.HasTrivialCopyAssignment);
3319 Record.push_back(Data.HasTrivialDestructor);
3320 Record.push_back(Data.ComputedVisibleConversions);
3321 Record.push_back(Data.DeclaredDefaultConstructor);
3322 Record.push_back(Data.DeclaredCopyConstructor);
3323 Record.push_back(Data.DeclaredCopyAssignment);
3324 Record.push_back(Data.DeclaredDestructor);
3325
3326 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003327 if (Data.NumBases > 0)
3328 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3329 Record);
3330
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003331 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3332 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003333 if (Data.NumVBases > 0)
3334 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3335 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003336
3337 AddUnresolvedSet(Data.Conversions, Record);
3338 AddUnresolvedSet(Data.VisibleConversions, Record);
3339 // Data.Definition is the owning decl, no need to write it.
3340 AddDeclRef(Data.FirstFriend, Record);
3341}
3342
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003343void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003344 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003345 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003346 assert(FirstDeclID == NextDeclID &&
3347 FirstTypeID == NextTypeID &&
3348 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003349 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003350 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003351 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003352 "Setting chain after writing has started.");
3353 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003354
3355 FirstDeclID += Chain->getTotalNumDecls();
3356 FirstTypeID += Chain->getTotalNumTypes();
3357 FirstIdentID += Chain->getTotalNumIdentifiers();
3358 FirstSelectorID += Chain->getTotalNumSelectors();
3359 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003360 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003361 NextDeclID = FirstDeclID;
3362 NextTypeID = FirstTypeID;
3363 NextIdentID = FirstIdentID;
3364 NextSelectorID = FirstSelectorID;
3365 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003366 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003367}
3368
Sebastian Redl539c5062010-08-18 23:57:32 +00003369void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003370 IdentifierIDs[II] = ID;
3371}
3372
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003373void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003374 // Always take the highest-numbered type index. This copes with an interesting
3375 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003376 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003377 // keep the higher-numbered entry so that we can properly write it out to
3378 // the AST file.
3379 TypeIdx &StoredIdx = TypeIdxs[T];
3380 if (Idx.getIndex() >= StoredIdx.getIndex())
3381 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003382}
3383
Sebastian Redl539c5062010-08-18 23:57:32 +00003384void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003385 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003386}
Sebastian Redl834bb972010-08-04 17:20:04 +00003387
Sebastian Redl539c5062010-08-18 23:57:32 +00003388void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003389 SelectorIDs[S] = ID;
3390}
Douglas Gregor91096292010-10-02 19:29:26 +00003391
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003392void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003393 MacroDefinition *MD) {
3394 MacroDefinitions[MD] = ID;
3395}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003396
3397void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3398 assert(D->isDefinition());
3399 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3400 // We are interested when a PCH decl is modified.
3401 if (RD->getPCHLevel() > 0) {
3402 // A forward reference was mutated into a definition. Rewrite it.
3403 // FIXME: This happens during template instantiation, should we
3404 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003405 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003406 }
3407
3408 for (CXXRecordDecl::redecl_iterator
3409 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3410 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3411 if (Redecl == RD)
3412 continue;
3413
3414 // We are interested when a PCH decl is modified.
3415 if (Redecl->getPCHLevel() > 0) {
3416 UpdateRecord &Record = DeclUpdates[Redecl];
3417 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3418 assert(Redecl->DefinitionData);
3419 assert(Redecl->DefinitionData->Definition == D);
3420 AddDeclRef(D, Record); // the DefinitionDecl
3421 }
3422 }
3423 }
3424}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003425void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3426 // TU and namespaces are handled elsewhere.
3427 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3428 return;
3429
3430 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3431 return; // Not a source decl added to a DeclContext from PCH.
3432
3433 AddUpdatedDeclContext(DC);
3434}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003435
3436void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3437 assert(D->isImplicit());
3438 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3439 return; // Not a source member added to a class from PCH.
3440 if (!isa<CXXMethodDecl>(D))
3441 return; // We are interested in lazily declared implicit methods.
3442
3443 // A decl coming from PCH was modified.
3444 assert(RD->isDefinition());
3445 UpdateRecord &Record = DeclUpdates[RD];
3446 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3447 AddDeclRef(D, Record);
3448}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003449
3450void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3451 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003452 // The specializations set is kept in the canonical template.
3453 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003454 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3455 return; // Not a source specialization added to a template from PCH.
3456
3457 UpdateRecord &Record = DeclUpdates[TD];
3458 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3459 AddDeclRef(D, Record);
3460}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003461
3462ASTSerializationListener::~ASTSerializationListener() { }