blob: 624cf1034909c8ea4d5fc7f545f361834881e6b7 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-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 Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregor89d99802010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000050
Sebastian Redlade50002010-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 Gregor2cf26342009-04-09 22:27:44 +000060//===----------------------------------------------------------------------===//
61// Type serialization
62//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000063
Douglas Gregor2cf26342009-04-09 22:27:44 +000064namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000065 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000066 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000067 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000068
69 public:
70 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000071 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000072
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000073 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000074 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-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 Gregor2cf26342009-04-09 22:27:44 +000082#include "clang/AST/TypeNodes.def"
83 };
84}
85
Sebastian Redl3397c552010-08-18 23:56:27 +000086void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000087 assert(false && "Built-in types are never serialized");
88}
89
Sebastian Redl3397c552010-08-18 23:56:27 +000090void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000091 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000092 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000093}
94
Sebastian Redl3397c552010-08-18 23:56:27 +000095void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000096 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000097 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000098}
99
Sebastian Redl3397c552010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000102 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103}
104
Sebastian Redl3397c552010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000107 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
Sebastian Redl3397c552010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000112 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113}
114
Sebastian Redl3397c552010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000116 Writer.AddTypeRef(T->getPointeeType(), Record);
117 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000118 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000119}
120
Sebastian Redl3397c552010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000122 Writer.AddTypeRef(T->getElementType(), Record);
123 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000124 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125}
126
Sebastian Redl3397c552010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128 VisitArrayType(T);
129 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000135 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136}
137
Sebastian Redl3397c552010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000140 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
141 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000142 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 Writer.AddTypeRef(T->getElementType(), Record);
148 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000149 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000150 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151}
152
Sebastian Redl3397c552010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000155 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156}
157
Sebastian Redl3397c552010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000160 FunctionType::ExtInfo C = T->getExtInfo();
161 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000162 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000163 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000164 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000169 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000170}
171
Sebastian Redl3397c552010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000173 VisitFunctionType(T);
174 Record.push_back(T->getNumArgs());
175 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
176 Writer.AddTypeRef(T->getArgType(I), Record);
177 Record.push_back(T->isVariadic());
178 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000179 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl465226e2009-05-27 22:11:52 +0000180 Record.push_back(T->hasExceptionSpec());
181 Record.push_back(T->hasAnyExceptionSpec());
182 Record.push_back(T->getNumExceptions());
183 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
184 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000185 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000186}
187
Sebastian Redl3397c552010-08-18 23:56:27 +0000188void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000189 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000190 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000191}
John McCalled976492009-12-04 22:46:56 +0000192
Sebastian Redl3397c552010-08-18 23:56:27 +0000193void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000194 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000195 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
196 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000197 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198}
199
Sebastian Redl3397c552010-08-18 23:56:27 +0000200void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000201 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000202 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000203}
204
Sebastian Redl3397c552010-08-18 23:56:27 +0000205void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000207 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208}
209
Sebastian Redl3397c552010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000211 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000212 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000213}
214
Sebastian Redl3397c552010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000216 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000218 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219 "Cannot serialize in the middle of a type definition");
220}
221
Sebastian Redl3397c552010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000224 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225}
226
Sebastian Redl3397c552010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000229 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000230}
231
John McCall9d156a72011-01-06 01:58:22 +0000232void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
233 Writer.AddTypeRef(T->getModifiedType(), Record);
234 Writer.AddTypeRef(T->getEquivalentType(), Record);
235 Record.push_back(T->getAttrKind());
236 Code = TYPE_ATTRIBUTED;
237}
238
Mike Stump1eb44332009-09-09 15:08:12 +0000239void
Sebastian Redl3397c552010-08-18 23:56:27 +0000240ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000241 const SubstTemplateTypeParmType *T) {
242 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
243 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000244 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000245}
246
247void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000248ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
249 const SubstTemplateTypeParmPackType *T) {
250 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
251 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
252 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
253}
254
255void
Sebastian Redl3397c552010-08-18 23:56:27 +0000256ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000258 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000259 Writer.AddTemplateName(T->getTemplateName(), Record);
260 Record.push_back(T->getNumArgs());
261 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
262 ArgI != ArgE; ++ArgI)
263 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000264 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
265 : T->getCanonicalTypeInternal(),
266 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000267 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000268}
269
270void
Sebastian Redl3397c552010-08-18 23:56:27 +0000271ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000272 VisitArrayType(T);
273 Writer.AddStmt(T->getSizeExpr());
274 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000275 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000276}
277
278void
Sebastian Redl3397c552010-08-18 23:56:27 +0000279ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000280 const DependentSizedExtVectorType *T) {
281 // FIXME: Serialize this type (C++ only)
282 assert(false && "Cannot serialize dependent sized extended vector types");
283}
284
285void
Sebastian Redl3397c552010-08-18 23:56:27 +0000286ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000287 Record.push_back(T->getDepth());
288 Record.push_back(T->getIndex());
289 Record.push_back(T->isParameterPack());
290 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000291 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000292}
293
294void
Sebastian Redl3397c552010-08-18 23:56:27 +0000295ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000296 Record.push_back(T->getKeyword());
297 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
298 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000299 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
300 : T->getCanonicalTypeInternal(),
301 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl3397c552010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000307 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000308 Record.push_back(T->getKeyword());
309 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
310 Writer.AddIdentifierRef(T->getIdentifier(), Record);
311 Record.push_back(T->getNumArgs());
312 for (DependentTemplateSpecializationType::iterator
313 I = T->begin(), E = T->end(); I != E; ++I)
314 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000315 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000316}
317
Douglas Gregor7536dd52010-12-20 02:24:11 +0000318void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
319 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000320 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
321 Record.push_back(*NumExpansions + 1);
322 else
323 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000324 Code = TYPE_PACK_EXPANSION;
325}
326
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000327void ASTTypeWriter::VisitParenType(const ParenType *T) {
328 Writer.AddTypeRef(T->getInnerType(), Record);
329 Code = TYPE_PAREN;
330}
331
Sebastian Redl3397c552010-08-18 23:56:27 +0000332void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000333 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000334 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
335 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000336 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000337}
338
Sebastian Redl3397c552010-08-18 23:56:27 +0000339void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000340 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000341 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000342 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000343}
344
Sebastian Redl3397c552010-08-18 23:56:27 +0000345void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000346 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000347 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000348}
349
Sebastian Redl3397c552010-08-18 23:56:27 +0000350void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000351 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000352 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000353 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000354 E = T->qual_end(); I != E; ++I)
355 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000356 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000357}
358
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000359void
Sebastian Redl3397c552010-08-18 23:56:27 +0000360ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000361 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000362 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000363}
364
John McCalla1ee0c52009-10-16 21:56:05 +0000365namespace {
366
367class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000368 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000369 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000370
371public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000372 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000373 : Writer(Writer), Record(Record) { }
374
John McCall51bd8032009-10-18 01:05:36 +0000375#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000376#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000377 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000378#include "clang/AST/TypeLocNodes.def"
379
John McCall51bd8032009-10-18 01:05:36 +0000380 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
381 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000382};
383
384}
385
John McCall51bd8032009-10-18 01:05:36 +0000386void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
387 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000388}
John McCall51bd8032009-10-18 01:05:36 +0000389void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000390 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
391 if (TL.needsExtraLocalData()) {
392 Record.push_back(TL.getWrittenTypeSpec());
393 Record.push_back(TL.getWrittenSignSpec());
394 Record.push_back(TL.getWrittenWidthSpec());
395 Record.push_back(TL.hasModeAttr());
396 }
John McCalla1ee0c52009-10-16 21:56:05 +0000397}
John McCall51bd8032009-10-18 01:05:36 +0000398void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
399 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000400}
John McCall51bd8032009-10-18 01:05:36 +0000401void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
402 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000403}
John McCall51bd8032009-10-18 01:05:36 +0000404void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000406}
John McCall51bd8032009-10-18 01:05:36 +0000407void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000409}
John McCall51bd8032009-10-18 01:05:36 +0000410void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000412}
John McCall51bd8032009-10-18 01:05:36 +0000413void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000415}
John McCall51bd8032009-10-18 01:05:36 +0000416void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
418 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
419 Record.push_back(TL.getSizeExpr() ? 1 : 0);
420 if (TL.getSizeExpr())
421 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000422}
John McCall51bd8032009-10-18 01:05:36 +0000423void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
424 VisitArrayTypeLoc(TL);
425}
426void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
427 VisitArrayTypeLoc(TL);
428}
429void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
430 VisitArrayTypeLoc(TL);
431}
432void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
433 DependentSizedArrayTypeLoc TL) {
434 VisitArrayTypeLoc(TL);
435}
436void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
437 DependentSizedExtVectorTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
439}
440void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
442}
443void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
446void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
448 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000449 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000450 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
451 Writer.AddDeclRef(TL.getArg(i), Record);
452}
453void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
454 VisitFunctionTypeLoc(TL);
455}
456void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
457 VisitFunctionTypeLoc(TL);
458}
John McCalled976492009-12-04 22:46:56 +0000459void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
461}
John McCall51bd8032009-10-18 01:05:36 +0000462void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
465void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000466 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
467 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
468 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000469}
470void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000471 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
472 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
473 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
474 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000475}
476void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
479void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
481}
482void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
484}
John McCall9d156a72011-01-06 01:58:22 +0000485void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
487 if (TL.hasAttrOperand()) {
488 SourceRange range = TL.getAttrOperandParensRange();
489 Writer.AddSourceLocation(range.getBegin(), Record);
490 Writer.AddSourceLocation(range.getEnd(), Record);
491 }
492 if (TL.hasAttrExprOperand()) {
493 Expr *operand = TL.getAttrExprOperand();
494 Record.push_back(operand ? 1 : 0);
495 if (operand) Writer.AddStmt(operand);
496 } else if (TL.hasAttrEnumOperand()) {
497 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
498 }
499}
John McCall51bd8032009-10-18 01:05:36 +0000500void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502}
John McCall49a832b2009-10-18 09:09:24 +0000503void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
504 SubstTemplateTypeParmTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000507void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
508 SubstTemplateTypeParmPackTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getNameLoc(), Record);
510}
John McCall51bd8032009-10-18 01:05:36 +0000511void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
512 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000513 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
514 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
515 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
516 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000517 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
518 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000519}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000520void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
522 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
523}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000524void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000525 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
526 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000527}
John McCall3cb0ebd2010-03-10 03:28:59 +0000528void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
529 Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
Douglas Gregor4714c122010-03-31 17:34:00 +0000531void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000532 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
533 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
John McCall33500952010-06-11 00:33:02 +0000536void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
537 DependentTemplateSpecializationTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
539 Writer.AddSourceRange(TL.getQualifierRange(), Record);
540 Writer.AddSourceLocation(TL.getNameLoc(), Record);
541 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
542 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
543 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000544 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
545 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000546}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000547void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
549}
John McCall51bd8032009-10-18 01:05:36 +0000550void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000552}
553void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
554 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000555 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
556 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
557 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
558 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000559}
John McCall54e14c42009-10-22 22:37:11 +0000560void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000562}
John McCalla1ee0c52009-10-16 21:56:05 +0000563
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000564//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000565// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000566//===----------------------------------------------------------------------===//
567
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000568static void EmitBlockID(unsigned ID, const char *Name,
569 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000570 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000571 Record.clear();
572 Record.push_back(ID);
573 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
574
575 // Emit the block name if present.
576 if (Name == 0 || Name[0] == 0) return;
577 Record.clear();
578 while (*Name)
579 Record.push_back(*Name++);
580 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
581}
582
583static void EmitRecordID(unsigned ID, const char *Name,
584 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000585 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000586 Record.clear();
587 Record.push_back(ID);
588 while (*Name)
589 Record.push_back(*Name++);
590 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000591}
592
593static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000594 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000595#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000596 RECORD(STMT_STOP);
597 RECORD(STMT_NULL_PTR);
598 RECORD(STMT_NULL);
599 RECORD(STMT_COMPOUND);
600 RECORD(STMT_CASE);
601 RECORD(STMT_DEFAULT);
602 RECORD(STMT_LABEL);
603 RECORD(STMT_IF);
604 RECORD(STMT_SWITCH);
605 RECORD(STMT_WHILE);
606 RECORD(STMT_DO);
607 RECORD(STMT_FOR);
608 RECORD(STMT_GOTO);
609 RECORD(STMT_INDIRECT_GOTO);
610 RECORD(STMT_CONTINUE);
611 RECORD(STMT_BREAK);
612 RECORD(STMT_RETURN);
613 RECORD(STMT_DECL);
614 RECORD(STMT_ASM);
615 RECORD(EXPR_PREDEFINED);
616 RECORD(EXPR_DECL_REF);
617 RECORD(EXPR_INTEGER_LITERAL);
618 RECORD(EXPR_FLOATING_LITERAL);
619 RECORD(EXPR_IMAGINARY_LITERAL);
620 RECORD(EXPR_STRING_LITERAL);
621 RECORD(EXPR_CHARACTER_LITERAL);
622 RECORD(EXPR_PAREN);
623 RECORD(EXPR_UNARY_OPERATOR);
624 RECORD(EXPR_SIZEOF_ALIGN_OF);
625 RECORD(EXPR_ARRAY_SUBSCRIPT);
626 RECORD(EXPR_CALL);
627 RECORD(EXPR_MEMBER);
628 RECORD(EXPR_BINARY_OPERATOR);
629 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
630 RECORD(EXPR_CONDITIONAL_OPERATOR);
631 RECORD(EXPR_IMPLICIT_CAST);
632 RECORD(EXPR_CSTYLE_CAST);
633 RECORD(EXPR_COMPOUND_LITERAL);
634 RECORD(EXPR_EXT_VECTOR_ELEMENT);
635 RECORD(EXPR_INIT_LIST);
636 RECORD(EXPR_DESIGNATED_INIT);
637 RECORD(EXPR_IMPLICIT_VALUE_INIT);
638 RECORD(EXPR_VA_ARG);
639 RECORD(EXPR_ADDR_LABEL);
640 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000641 RECORD(EXPR_CHOOSE);
642 RECORD(EXPR_GNU_NULL);
643 RECORD(EXPR_SHUFFLE_VECTOR);
644 RECORD(EXPR_BLOCK);
645 RECORD(EXPR_BLOCK_DECL_REF);
646 RECORD(EXPR_OBJC_STRING_LITERAL);
647 RECORD(EXPR_OBJC_ENCODE);
648 RECORD(EXPR_OBJC_SELECTOR_EXPR);
649 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
650 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
651 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
652 RECORD(EXPR_OBJC_KVC_REF_EXPR);
653 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000654 RECORD(STMT_OBJC_FOR_COLLECTION);
655 RECORD(STMT_OBJC_CATCH);
656 RECORD(STMT_OBJC_FINALLY);
657 RECORD(STMT_OBJC_AT_TRY);
658 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
659 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000660 RECORD(EXPR_CXX_OPERATOR_CALL);
661 RECORD(EXPR_CXX_CONSTRUCT);
662 RECORD(EXPR_CXX_STATIC_CAST);
663 RECORD(EXPR_CXX_DYNAMIC_CAST);
664 RECORD(EXPR_CXX_REINTERPRET_CAST);
665 RECORD(EXPR_CXX_CONST_CAST);
666 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
667 RECORD(EXPR_CXX_BOOL_LITERAL);
668 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000669 RECORD(EXPR_CXX_TYPEID_EXPR);
670 RECORD(EXPR_CXX_TYPEID_TYPE);
671 RECORD(EXPR_CXX_UUIDOF_EXPR);
672 RECORD(EXPR_CXX_UUIDOF_TYPE);
673 RECORD(EXPR_CXX_THIS);
674 RECORD(EXPR_CXX_THROW);
675 RECORD(EXPR_CXX_DEFAULT_ARG);
676 RECORD(EXPR_CXX_BIND_TEMPORARY);
677 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
678 RECORD(EXPR_CXX_NEW);
679 RECORD(EXPR_CXX_DELETE);
680 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
681 RECORD(EXPR_EXPR_WITH_CLEANUPS);
682 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
683 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
684 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
685 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
686 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
687 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
688 RECORD(EXPR_CXX_NOEXCEPT);
689 RECORD(EXPR_OPAQUE_VALUE);
690 RECORD(EXPR_BINARY_TYPE_TRAIT);
691 RECORD(EXPR_PACK_EXPANSION);
692 RECORD(EXPR_SIZEOF_PACK);
693 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000694 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000695#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000696}
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Sebastian Redla4232eb2010-08-18 23:56:21 +0000698void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000699 RecordData Record;
700 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000702#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
703#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Sebastian Redl3397c552010-08-18 23:56:27 +0000705 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000706 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000707 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000708 RECORD(TYPE_OFFSET);
709 RECORD(DECL_OFFSET);
710 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000711 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000712 RECORD(IDENTIFIER_OFFSET);
713 RECORD(IDENTIFIER_TABLE);
714 RECORD(EXTERNAL_DEFINITIONS);
715 RECORD(SPECIAL_TYPES);
716 RECORD(STATISTICS);
717 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000718 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000719 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
720 RECORD(SELECTOR_OFFSETS);
721 RECORD(METHOD_POOL);
722 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000723 RECORD(SOURCE_LOCATION_OFFSETS);
724 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000725 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000726 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000727 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000728 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000729 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000730 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000731 RECORD(TU_UPDATE_LEXICAL);
732 RECORD(REDECLS_UPDATE_LATEST);
733 RECORD(SEMA_DECL_REFS);
734 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
735 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
736 RECORD(DECL_REPLACEMENTS);
737 RECORD(UPDATE_VISIBLE);
738 RECORD(DECL_UPDATE_OFFSETS);
739 RECORD(DECL_UPDATES);
740 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
741 RECORD(DIAG_PRAGMA_MAPPINGS);
742
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000743 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000744 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000745 RECORD(SM_SLOC_FILE_ENTRY);
746 RECORD(SM_SLOC_BUFFER_ENTRY);
747 RECORD(SM_SLOC_BUFFER_BLOB);
748 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
749 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000751 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000752 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000753 RECORD(PP_MACRO_OBJECT_LIKE);
754 RECORD(PP_MACRO_FUNCTION_LIKE);
755 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000756
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000757 // Decls and Types block.
758 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000759 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000760 RECORD(TYPE_COMPLEX);
761 RECORD(TYPE_POINTER);
762 RECORD(TYPE_BLOCK_POINTER);
763 RECORD(TYPE_LVALUE_REFERENCE);
764 RECORD(TYPE_RVALUE_REFERENCE);
765 RECORD(TYPE_MEMBER_POINTER);
766 RECORD(TYPE_CONSTANT_ARRAY);
767 RECORD(TYPE_INCOMPLETE_ARRAY);
768 RECORD(TYPE_VARIABLE_ARRAY);
769 RECORD(TYPE_VECTOR);
770 RECORD(TYPE_EXT_VECTOR);
771 RECORD(TYPE_FUNCTION_PROTO);
772 RECORD(TYPE_FUNCTION_NO_PROTO);
773 RECORD(TYPE_TYPEDEF);
774 RECORD(TYPE_TYPEOF_EXPR);
775 RECORD(TYPE_TYPEOF);
776 RECORD(TYPE_RECORD);
777 RECORD(TYPE_ENUM);
778 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000779 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000780 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000781 RECORD(TYPE_DECLTYPE);
782 RECORD(TYPE_ELABORATED);
783 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
784 RECORD(TYPE_UNRESOLVED_USING);
785 RECORD(TYPE_INJECTED_CLASS_NAME);
786 RECORD(TYPE_OBJC_OBJECT);
787 RECORD(TYPE_TEMPLATE_TYPE_PARM);
788 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
789 RECORD(TYPE_DEPENDENT_NAME);
790 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
791 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
792 RECORD(TYPE_PAREN);
793 RECORD(TYPE_PACK_EXPANSION);
794 RECORD(TYPE_ATTRIBUTED);
795 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000796 RECORD(DECL_TRANSLATION_UNIT);
797 RECORD(DECL_TYPEDEF);
798 RECORD(DECL_ENUM);
799 RECORD(DECL_RECORD);
800 RECORD(DECL_ENUM_CONSTANT);
801 RECORD(DECL_FUNCTION);
802 RECORD(DECL_OBJC_METHOD);
803 RECORD(DECL_OBJC_INTERFACE);
804 RECORD(DECL_OBJC_PROTOCOL);
805 RECORD(DECL_OBJC_IVAR);
806 RECORD(DECL_OBJC_AT_DEFS_FIELD);
807 RECORD(DECL_OBJC_CLASS);
808 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
809 RECORD(DECL_OBJC_CATEGORY);
810 RECORD(DECL_OBJC_CATEGORY_IMPL);
811 RECORD(DECL_OBJC_IMPLEMENTATION);
812 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
813 RECORD(DECL_OBJC_PROPERTY);
814 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000815 RECORD(DECL_FIELD);
816 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000817 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000818 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000819 RECORD(DECL_FILE_SCOPE_ASM);
820 RECORD(DECL_BLOCK);
821 RECORD(DECL_CONTEXT_LEXICAL);
822 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000823 RECORD(DECL_NAMESPACE);
824 RECORD(DECL_NAMESPACE_ALIAS);
825 RECORD(DECL_USING);
826 RECORD(DECL_USING_SHADOW);
827 RECORD(DECL_USING_DIRECTIVE);
828 RECORD(DECL_UNRESOLVED_USING_VALUE);
829 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
830 RECORD(DECL_LINKAGE_SPEC);
831 RECORD(DECL_CXX_RECORD);
832 RECORD(DECL_CXX_METHOD);
833 RECORD(DECL_CXX_CONSTRUCTOR);
834 RECORD(DECL_CXX_DESTRUCTOR);
835 RECORD(DECL_CXX_CONVERSION);
836 RECORD(DECL_ACCESS_SPEC);
837 RECORD(DECL_FRIEND);
838 RECORD(DECL_FRIEND_TEMPLATE);
839 RECORD(DECL_CLASS_TEMPLATE);
840 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
841 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
842 RECORD(DECL_FUNCTION_TEMPLATE);
843 RECORD(DECL_TEMPLATE_TYPE_PARM);
844 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
845 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
846 RECORD(DECL_STATIC_ASSERT);
847 RECORD(DECL_CXX_BASE_SPECIFIERS);
848 RECORD(DECL_INDIRECTFIELD);
849 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
850
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000851 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
852 RECORD(PPD_MACRO_INSTANTIATION);
853 RECORD(PPD_MACRO_DEFINITION);
854 RECORD(PPD_INCLUSION_DIRECTIVE);
855
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000856 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000857 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000858#undef RECORD
859#undef BLOCK
860 Stream.ExitBlock();
861}
862
Douglas Gregore650c8c2009-07-07 00:12:59 +0000863/// \brief Adjusts the given filename to only write out the portion of the
864/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000865///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000866/// \param Filename the file name to adjust.
867///
868/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
869/// the returned filename will be adjusted by this system root.
870///
871/// \returns either the original filename (if it needs no adjustment) or the
872/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000873static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000874adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
875 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Douglas Gregore650c8c2009-07-07 00:12:59 +0000877 if (!isysroot)
878 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Douglas Gregore650c8c2009-07-07 00:12:59 +0000880 // Verify that the filename and the system root have the same prefix.
881 unsigned Pos = 0;
882 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
883 if (Filename[Pos] != isysroot[Pos])
884 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Douglas Gregore650c8c2009-07-07 00:12:59 +0000886 // We hit the end of the filename before we hit the end of the system root.
887 if (!Filename[Pos])
888 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Douglas Gregore650c8c2009-07-07 00:12:59 +0000890 // If the file name has a '/' at the current position, skip over the '/'.
891 // We distinguish sysroot-based includes from absolute includes by the
892 // absence of '/' at the beginning of sysroot-based includes.
893 if (Filename[Pos] == '/')
894 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Douglas Gregore650c8c2009-07-07 00:12:59 +0000896 return Filename + Pos;
897}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000898
Sebastian Redl3397c552010-08-18 23:56:27 +0000899/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000900void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000901 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000902
Douglas Gregore650c8c2009-07-07 00:12:59 +0000903 // Metadata
904 const TargetInfo &Target = Context.Target;
905 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000906 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000907 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000908 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
909 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000910 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
911 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
912 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000913 // Target triple or chained PCH name
914 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000915 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Douglas Gregore650c8c2009-07-07 00:12:59 +0000917 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000918 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
919 Record.push_back(VERSION_MAJOR);
920 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000921 Record.push_back(CLANG_VERSION_MAJOR);
922 Record.push_back(CLANG_VERSION_MINOR);
923 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000924 // FIXME: This writes the absolute path for chained headers.
925 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
926 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregorb64c1932009-05-12 01:31:05 +0000928 // Original file name
929 SourceManager &SM = Context.getSourceManager();
930 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
931 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000932 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000933 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
934 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
935
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000936 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000938 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000939
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000940 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000941 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000942 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000943 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000944 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000945 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000946 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000947
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000948 // Repository branch/version information.
949 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000950 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000951 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
952 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000953 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000954 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000955 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
956 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000957}
958
959/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000960void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000961 RecordData Record;
962 Record.push_back(LangOpts.Trigraphs);
963 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
964 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
965 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
966 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000967 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000968 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
969 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
970 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
971 Record.push_back(LangOpts.C99); // C99 Support
972 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +0000973 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
974 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000975 Record.push_back(LangOpts.CPlusPlus); // C++ Support
976 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000977 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000979 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
980 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000981 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000982 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000983 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000984 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000985 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +0000986 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
987 // properties enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000988 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000990 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000991 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
992 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000993 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000994 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000995 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000996
Douglas Gregor6f755502011-02-01 15:15:22 +0000997 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000998 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
999 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1000 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1001
Chris Lattnerea5ce472009-04-27 07:35:58 +00001002 // Whether static initializers are protected by locks.
1003 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001004 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001005 Record.push_back(LangOpts.Blocks); // block extension to C
1006 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1007 // they are unused.
1008 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1009 // (modulo the platform support).
1010
Chris Lattnera4d71452010-06-26 21:25:03 +00001011 Record.push_back(LangOpts.getSignedOverflowBehavior());
1012 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001013
1014 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001015 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001016 // defined.
1017 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1018 // opposed to __DYNAMIC__).
1019 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1020
1021 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1022 // used (instead of C99 semantics).
1023 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001024 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1025 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001026 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1027 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001028 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001029 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1030 // to the smallest integer type with
1031 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001032 Record.push_back(LangOpts.getGCMode());
1033 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001034 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001035 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001036 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001037 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001038 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +00001039 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001040 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001041 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001042}
1043
Douglas Gregor14f79002009-04-10 03:52:48 +00001044//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001045// stat cache Serialization
1046//===----------------------------------------------------------------------===//
1047
1048namespace {
1049// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001050class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001051public:
1052 typedef const char * key_type;
1053 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattner74e976b2010-11-23 19:28:12 +00001055 typedef struct stat data_type;
1056 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001057
1058 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001059 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
1062 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001063 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1064 data_type_ref Data) {
1065 unsigned StrLen = strlen(path);
1066 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001067 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001068 clang::io::Emit8(Out, DataLen);
1069 return std::make_pair(StrLen + 1, DataLen);
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001072 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1073 Out.write(path, KeyLen);
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Chris Lattner74e976b2010-11-23 19:28:12 +00001076 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001077 data_type_ref Data, unsigned DataLen) {
1078 using namespace clang::io;
1079 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Chris Lattner74e976b2010-11-23 19:28:12 +00001081 Emit32(Out, (uint32_t) Data.st_ino);
1082 Emit32(Out, (uint32_t) Data.st_dev);
1083 Emit16(Out, (uint16_t) Data.st_mode);
1084 Emit64(Out, (uint64_t) Data.st_mtime);
1085 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001086
1087 assert(Out.tell() - Start == DataLen && "Wrong data length");
1088 }
1089};
1090} // end anonymous namespace
1091
Sebastian Redl3397c552010-08-18 23:56:27 +00001092/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001093void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001094 // Build the on-disk hash table containing information about every
1095 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001096 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001097 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001098 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001100 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1101 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001102 Generator.insert(Filename, Stat->second);
1103 }
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001105 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001106 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001107 uint32_t BucketOffset;
1108 {
1109 llvm::raw_svector_ostream Out(StatCacheData);
1110 // Make sure that no bucket is at offset 0
1111 clang::io::Emit32(Out, 0);
1112 BucketOffset = Generator.Emit(Out);
1113 }
1114
1115 // Create a blob abbreviation
1116 using namespace llvm;
1117 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001118 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001119 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1120 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1122 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1123
1124 // Write the stat cache
1125 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001126 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001127 Record.push_back(BucketOffset);
1128 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001129 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001130}
1131
1132//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001133// Source Manager Serialization
1134//===----------------------------------------------------------------------===//
1135
1136/// \brief Create an abbreviation for the SLocEntry that refers to a
1137/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001138static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001139 using namespace llvm;
1140 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001141 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001142 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001146 // FileEntry fields.
1147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001149 // HeaderFileInfo fields.
1150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001155 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001156}
1157
1158/// \brief Create an abbreviation for the SLocEntry that refers to a
1159/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001160static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001161 using namespace llvm;
1162 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001163 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001169 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001170}
1171
1172/// \brief Create an abbreviation for the SLocEntry that refers to a
1173/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001174static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001175 using namespace llvm;
1176 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001177 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001179 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001180}
1181
1182/// \brief Create an abbreviation for the SLocEntry that refers to an
1183/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001184static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001185 using namespace llvm;
1186 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001187 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001193 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001194}
1195
1196/// \brief Writes the block containing the serialized form of the
1197/// source manager.
1198///
1199/// TODO: We should probably use an on-disk hash table (stored in a
1200/// blob), indexed based on the file name, so that we only create
1201/// entries for files that we actually need. In the common case (no
1202/// errors), we probably won't have to create file entries for any of
1203/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001204void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001205 const Preprocessor &PP,
1206 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207 RecordData Record;
1208
Chris Lattnerf04ad692009-04-10 17:16:57 +00001209 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001210 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001211
1212 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001213 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1214 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1215 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1216 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001217
Douglas Gregorbd945002009-04-13 16:31:14 +00001218 // Write the line table.
1219 if (SourceMgr.hasLineTable()) {
1220 LineTableInfo &LineTable = SourceMgr.getLineTable();
1221
1222 // Emit the file names
1223 Record.push_back(LineTable.getNumFilenames());
1224 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1225 // Emit the file name
1226 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001227 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001228 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1229 Record.push_back(FilenameLen);
1230 if (FilenameLen)
1231 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1232 }
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregorbd945002009-04-13 16:31:14 +00001234 // Emit the line entries
1235 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1236 L != LEnd; ++L) {
1237 // Emit the file ID
1238 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregorbd945002009-04-13 16:31:14 +00001240 // Emit the line entries
1241 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001242 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001243 LEEnd = L->second.end();
1244 LE != LEEnd; ++LE) {
1245 Record.push_back(LE->FileOffset);
1246 Record.push_back(LE->LineNo);
1247 Record.push_back(LE->FilenameID);
1248 Record.push_back((unsigned)LE->FileKind);
1249 Record.push_back(LE->IncludeOffset);
1250 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001251 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001252 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001253 }
1254
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001255 // Write out the source location entry table. We skip the first
1256 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001257 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001258 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001259 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1260 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1261 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1262 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001263 // Get this source location entry.
1264 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001265
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001266 // Record the offset of this source-location entry.
1267 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1268
1269 // Figure out which record code to use.
1270 unsigned Code;
1271 if (SLoc->isFile()) {
1272 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001273 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001274 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001275 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001276 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001277 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001278 Record.clear();
1279 Record.push_back(Code);
1280
1281 Record.push_back(SLoc->getOffset());
1282 if (SLoc->isFile()) {
1283 const SrcMgr::FileInfo &File = SLoc->getFile();
1284 Record.push_back(File.getIncludeLoc().getRawEncoding());
1285 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1286 Record.push_back(File.hasLineDirectives());
1287
1288 const SrcMgr::ContentCache *Content = File.getContentCache();
1289 if (Content->Entry) {
1290 // The source location entry is a file. The blob associated
1291 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Douglas Gregor2d52be52010-03-21 22:49:54 +00001293 // Emit size/modification time for this file.
1294 Record.push_back(Content->Entry->getSize());
1295 Record.push_back(Content->Entry->getModificationTime());
1296
Douglas Gregor12fab312010-03-16 16:35:32 +00001297 // Emit header-search information associated with this file.
1298 HeaderFileInfo HFI;
1299 HeaderSearch &HS = PP.getHeaderSearchInfo();
1300 if (Content->Entry->getUID() < HS.header_file_size())
1301 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1302 Record.push_back(HFI.isImport);
1303 Record.push_back(HFI.DirInfo);
1304 Record.push_back(HFI.NumIncludes);
1305 AddIdentifierRef(HFI.ControllingMacro, Record);
1306
Douglas Gregore650c8c2009-07-07 00:12:59 +00001307 // Turn the file name into an absolute path, if it isn't already.
1308 const char *Filename = Content->Entry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001309 llvm::SmallString<128> FilePath(Filename);
1310 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001311 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Douglas Gregore650c8c2009-07-07 00:12:59 +00001313 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001314 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001315
1316 // FIXME: For now, preload all file source locations, so that
1317 // we get the appropriate File entries in the reader. This is
1318 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001319 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001320 } else {
1321 // The source location entry is a buffer. The blob associated
1322 // with this entry contains the contents of the buffer.
1323
1324 // We add one to the size so that we capture the trailing NULL
1325 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1326 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001327 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001328 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001329 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001330 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1331 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001332 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001333 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001334 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001335 llvm::StringRef(Buffer->getBufferStart(),
1336 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001337
1338 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001339 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001340 }
1341 } else {
1342 // The source location entry is an instantiation.
1343 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1344 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1345 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1346 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1347
1348 // Compute the token length for this macro expansion.
1349 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001350 if (I + 1 != N)
1351 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001352 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1353 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1354 }
1355 }
1356
Douglas Gregorc9490c02009-04-16 22:23:12 +00001357 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001358
1359 if (SLocEntryOffsets.empty())
1360 return;
1361
Sebastian Redl3397c552010-08-18 23:56:27 +00001362 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001363 // table is used for lazily loading source-location information.
1364 using namespace llvm;
1365 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001366 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1370 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001372 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001373 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001374 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001375 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1376 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001377 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001378 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001379 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001380
Sebastian Redl3397c552010-08-18 23:56:27 +00001381 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001382 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001383 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001384}
1385
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001386//===----------------------------------------------------------------------===//
1387// Preprocessor Serialization
1388//===----------------------------------------------------------------------===//
1389
Chris Lattner0b1fb982009-04-10 17:15:23 +00001390/// \brief Writes the block containing the serialized form of the
1391/// preprocessor.
1392///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001393void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001394 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001395
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001396 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1397 if (PP.getCounterValue() != 0) {
1398 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001399 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001400 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001401 }
1402
1403 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001404 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Sebastian Redl3397c552010-08-18 23:56:27 +00001406 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001407 // FIXME: use diagnostics subsystem for localization etc.
1408 if (PP.SawDateOrTime())
1409 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Douglas Gregorecdcb882010-10-20 22:00:55 +00001411
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001412 // Loop over all the macro definitions that are live at the end of the file,
1413 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001414 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001415
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001416 // FIXME: If we are chaining, don't visit all of the macros!
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001417 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1418 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001419 // FIXME: This emits macros in hash table order, we should do it in a stable
1420 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001421 MacroInfo *MI = I->second;
1422
Sebastian Redl3397c552010-08-18 23:56:27 +00001423 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001424 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001425 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001426
1427 // FIXME: There is a (probably minor) optimization we could do here, if
1428 // the macro comes from the original PCH but the identifier comes from a
1429 // chained PCH, by storing the offset into the original PCH rather than
1430 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001431 if (MI->isBuiltinMacro() ||
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001432 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001433 continue;
1434
Chris Lattner7356a312009-04-11 21:15:38 +00001435 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001436 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001437 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1438 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001440 unsigned Code;
1441 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001442 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001443 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001444 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001446 Record.push_back(MI->isC99Varargs());
1447 Record.push_back(MI->isGNUVarargs());
1448 Record.push_back(MI->getNumArgs());
1449 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1450 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001451 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001452 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001453
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001454 // If we have a detailed preprocessing record, record the macro definition
1455 // ID that corresponds to this macro.
1456 if (PPRec)
1457 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001458
Douglas Gregorc9490c02009-04-16 22:23:12 +00001459 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001460 Record.clear();
1461
Chris Lattnerdf961c22009-04-10 18:08:30 +00001462 // Emit the tokens array.
1463 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1464 // Note that we know that the preprocessor does not have any annotation
1465 // tokens in it because they are created by the parser, and thus can't be
1466 // in a macro definition.
1467 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Chris Lattnerdf961c22009-04-10 18:08:30 +00001469 Record.push_back(Tok.getLocation().getRawEncoding());
1470 Record.push_back(Tok.getLength());
1471
Chris Lattnerdf961c22009-04-10 18:08:30 +00001472 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1473 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001474 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Chris Lattnerdf961c22009-04-10 18:08:30 +00001476 // FIXME: Should translate token kind to a stable encoding.
1477 Record.push_back(Tok.getKind());
1478 // FIXME: Should translate token flags to a stable encoding.
1479 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001481 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001482 Record.clear();
1483 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001484 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001485 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001486 Stream.ExitBlock();
1487
1488 if (PPRec)
1489 WritePreprocessorDetail(*PPRec);
1490}
1491
1492void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1493 if (PPRec.begin(Chain) == PPRec.end(Chain))
1494 return;
1495
1496 // Enter the preprocessor block.
1497 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001498
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001499 // If the preprocessor has a preprocessing record, emit it.
1500 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001501 using namespace llvm;
1502
1503 // Set up the abbreviation for
1504 unsigned InclusionAbbrev = 0;
1505 {
1506 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1507 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1508 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1515 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1516 }
1517
1518 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1519 RecordData Record;
1520 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1521 EEnd = PPRec.end(Chain);
1522 E != EEnd; ++E) {
1523 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001524
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001525 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1526 // Record this macro definition's location.
1527 MacroID ID = getMacroDefinitionID(MD);
1528
1529 // Don't write the macro definition if it is from another AST file.
1530 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001531 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001532
Douglas Gregor89d99802010-11-30 06:16:57 +00001533 // Notify the serialization listener that we're serializing this entity.
1534 if (SerializationListener)
1535 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001536 Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001537
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001538 unsigned Position = ID - FirstMacroID;
1539 if (Position != MacroDefinitionOffsets.size()) {
1540 if (Position > MacroDefinitionOffsets.size())
1541 MacroDefinitionOffsets.resize(Position + 1);
1542
1543 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1544 } else
1545 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001546
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001547 Record.push_back(IndexBase + NumPreprocessingRecords++);
1548 Record.push_back(ID);
1549 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1550 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1551 AddIdentifierRef(MD->getName(), Record);
1552 AddSourceLocation(MD->getLocation(), Record);
1553 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1554 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001555 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001556
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001557 // Notify the serialization listener that we're serializing this entity.
1558 if (SerializationListener)
1559 SerializationListener->SerializedPreprocessedEntity(*E,
1560 Stream.GetCurrentBitNo());
1561
1562 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1563 Record.push_back(IndexBase + NumPreprocessingRecords++);
1564 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1565 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1566 AddIdentifierRef(MI->getName(), Record);
1567 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1568 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1569 continue;
1570 }
1571
1572 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1573 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1574 Record.push_back(IndexBase + NumPreprocessingRecords++);
1575 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1576 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1577 Record.push_back(ID->getFileName().size());
1578 Record.push_back(ID->wasInQuotes());
1579 Record.push_back(static_cast<unsigned>(ID->getKind()));
1580 llvm::SmallString<64> Buffer;
1581 Buffer += ID->getFileName();
1582 Buffer += ID->getFile()->getName();
1583 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1584 continue;
1585 }
1586
1587 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1588 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001589 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001590
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001591 // Write the offsets table for the preprocessing record.
1592 if (NumPreprocessingRecords > 0) {
1593 // Write the offsets table for identifier IDs.
1594 using namespace llvm;
1595 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001596 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001597 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1599 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1600 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001601
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001602 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001603 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001604 Record.push_back(NumPreprocessingRecords);
1605 Record.push_back(MacroDefinitionOffsets.size());
1606 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001607 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001608 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1609 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001610}
1611
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001612void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001613 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001614 for (Diagnostic::DiagStatePointsTy::const_iterator
1615 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1616 I != E; ++I) {
1617 const Diagnostic::DiagStatePoint &point = *I;
1618 if (point.Loc.isInvalid())
1619 continue;
1620
1621 Record.push_back(point.Loc.getRawEncoding());
1622 for (Diagnostic::DiagState::iterator
1623 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1624 unsigned diag = I->first, map = I->second;
1625 if (map & 0x10) { // mapping from a diagnostic pragma.
1626 Record.push_back(diag);
1627 Record.push_back(map & 0x7);
1628 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001629 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001630 Record.push_back(-1); // mark the end of the diag/map pairs for this
1631 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001632 }
1633
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001634 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001635 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001636}
1637
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001638//===----------------------------------------------------------------------===//
1639// Type Serialization
1640//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001641
Sebastian Redl3397c552010-08-18 23:56:27 +00001642/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001643void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001644 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001645 if (Idx.getIndex() == 0) // we haven't seen this type before.
1646 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Douglas Gregor97475832010-10-05 18:37:06 +00001648 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001649
Douglas Gregor2cf26342009-04-09 22:27:44 +00001650 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001651 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001652 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001653 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001654 else if (TypeOffsets.size() < Index) {
1655 TypeOffsets.resize(Index + 1);
1656 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001657 }
1658
1659 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Douglas Gregor2cf26342009-04-09 22:27:44 +00001661 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001662 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001663
Douglas Gregora4923eb2009-11-16 21:35:15 +00001664 if (T.hasLocalNonFastQualifiers()) {
1665 Qualifiers Qs = T.getLocalQualifiers();
1666 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001667 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001668 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001669 } else {
1670 switch (T->getTypeClass()) {
1671 // For all of the concrete, non-dependent types, call the
1672 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001673#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001674 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001675#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001676#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001677 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001678 }
1679
1680 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001681 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001682
1683 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001684 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001685}
1686
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001687//===----------------------------------------------------------------------===//
1688// Declaration Serialization
1689//===----------------------------------------------------------------------===//
1690
Douglas Gregor2cf26342009-04-09 22:27:44 +00001691/// \brief Write the block containing all of the declaration IDs
1692/// lexically declared within the given DeclContext.
1693///
1694/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1695/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001696uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001697 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001698 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001699 return 0;
1700
Douglas Gregorc9490c02009-04-16 22:23:12 +00001701 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001702 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001703 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001704 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001705 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1706 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001707 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001708
Douglas Gregor25123082009-04-22 22:34:57 +00001709 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001710 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001711 reinterpret_cast<char*>(Decls.data()),
1712 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001713 return Offset;
1714}
1715
Sebastian Redla4232eb2010-08-18 23:56:21 +00001716void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001717 using namespace llvm;
1718 RecordData Record;
1719
1720 // Write the type offsets array
1721 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001722 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001723 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1724 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1725 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1726 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001727 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001728 Record.push_back(TypeOffsets.size());
1729 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001730 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001731 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1732
1733 // Write the declaration offsets array
1734 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001735 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1738 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1739 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001740 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001741 Record.push_back(DeclOffsets.size());
1742 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001743 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001744 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1745}
1746
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001747//===----------------------------------------------------------------------===//
1748// Global Method Pool and Selector Serialization
1749//===----------------------------------------------------------------------===//
1750
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001751namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001752// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001753class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001754 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001755
1756public:
1757 typedef Selector key_type;
1758 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Sebastian Redl5d050072010-08-04 17:20:04 +00001760 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001761 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001762 ObjCMethodList Instance, Factory;
1763 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001764 typedef const data_type& data_type_ref;
1765
Sebastian Redl3397c552010-08-18 23:56:27 +00001766 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001768 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001769 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001770 }
Mike Stump1eb44332009-09-09 15:08:12 +00001771
1772 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001773 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1774 data_type_ref Methods) {
1775 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1776 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001777 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1778 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001779 Method = Method->Next)
1780 if (Method->Method)
1781 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001782 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001783 Method = Method->Next)
1784 if (Method->Method)
1785 DataLen += 4;
1786 clang::io::Emit16(Out, DataLen);
1787 return std::make_pair(KeyLen, DataLen);
1788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Douglas Gregor83941df2009-04-25 17:48:32 +00001790 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001791 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001792 assert((Start >> 32) == 0 && "Selector key offset too large");
1793 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001794 unsigned N = Sel.getNumArgs();
1795 clang::io::Emit16(Out, N);
1796 if (N == 0)
1797 N = 1;
1798 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001799 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001800 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1801 }
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001803 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001804 data_type_ref Methods, unsigned DataLen) {
1805 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001806 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001807 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001808 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001809 Method = Method->Next)
1810 if (Method->Method)
1811 ++NumInstanceMethods;
1812
1813 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001814 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001815 Method = Method->Next)
1816 if (Method->Method)
1817 ++NumFactoryMethods;
1818
1819 clang::io::Emit16(Out, NumInstanceMethods);
1820 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001821 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001822 Method = Method->Next)
1823 if (Method->Method)
1824 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001825 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001826 Method = Method->Next)
1827 if (Method->Method)
1828 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001829
1830 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001831 }
1832};
1833} // end anonymous namespace
1834
Sebastian Redl059612d2010-08-03 21:58:15 +00001835/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001836///
1837/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001838/// in an on-disk hash table indexed by the selector. The hash table also
1839/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001840void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001841 using namespace llvm;
1842
Sebastian Redl059612d2010-08-03 21:58:15 +00001843 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001844 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001845 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001846 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001847 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001848 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001849 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001850 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001851
Sebastian Redl059612d2010-08-03 21:58:15 +00001852 // Create the on-disk hash table representation. We walk through every
1853 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001854 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001855 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00001856 I = SelectorIDs.begin(), E = SelectorIDs.end();
1857 I != E; ++I) {
1858 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001859 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001860 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001861 I->second,
1862 ObjCMethodList(),
1863 ObjCMethodList()
1864 };
1865 if (F != SemaRef.MethodPool.end()) {
1866 Data.Instance = F->second.first;
1867 Data.Factory = F->second.second;
1868 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001869 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001870 // changed.
1871 if (Chain && I->second < FirstSelectorID) {
1872 // Selector already exists. Did it change?
1873 bool changed = false;
1874 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1875 M = M->Next) {
1876 if (M->Method->getPCHLevel() == 0)
1877 changed = true;
1878 }
1879 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1880 M = M->Next) {
1881 if (M->Method->getPCHLevel() == 0)
1882 changed = true;
1883 }
1884 if (!changed)
1885 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001886 } else if (Data.Instance.Method || Data.Factory.Method) {
1887 // A new method pool entry.
1888 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001889 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001890 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001891 }
1892
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001893 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001894 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001895 uint32_t BucketOffset;
1896 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001897 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001898 llvm::raw_svector_ostream Out(MethodPool);
1899 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001900 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001901 BucketOffset = Generator.Emit(Out, Trait);
1902 }
1903
1904 // Create a blob abbreviation
1905 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001906 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1910 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1911
Douglas Gregor83941df2009-04-25 17:48:32 +00001912 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001913 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001914 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001915 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001916 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001917 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001918
1919 // Create a blob abbreviation for the selector table offsets.
1920 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001921 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00001922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00001923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1924 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1925
1926 // Write the selector offsets table.
1927 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001928 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00001929 Record.push_back(SelectorOffsets.size());
1930 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001931 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001932 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001933 }
1934}
1935
Sebastian Redl3397c552010-08-18 23:56:27 +00001936/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001937void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001938 using namespace llvm;
1939 if (SemaRef.ReferencedSelectors.empty())
1940 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001941
Fariborz Jahanian32019832010-07-23 19:11:11 +00001942 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001943
Sebastian Redl3397c552010-08-18 23:56:27 +00001944 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001945 // very tricky to fix, and given that @selector shouldn't really appear in
1946 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001947 for (DenseMap<Selector, SourceLocation>::iterator S =
1948 SemaRef.ReferencedSelectors.begin(),
1949 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1950 Selector Sel = (*S).first;
1951 SourceLocation Loc = (*S).second;
1952 AddSelectorRef(Sel, Record);
1953 AddSourceLocation(Loc, Record);
1954 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001955 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001956}
1957
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001958//===----------------------------------------------------------------------===//
1959// Identifier Table Serialization
1960//===----------------------------------------------------------------------===//
1961
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001962namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001963class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001964 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001965 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001966
Douglas Gregora92193e2009-04-28 21:18:29 +00001967 /// \brief Determines whether this is an "interesting" identifier
1968 /// that needs a full IdentifierInfo structure written into the hash
1969 /// table.
1970 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1971 return II->isPoisoned() ||
1972 II->isExtensionToken() ||
1973 II->hasMacroDefinition() ||
1974 II->getObjCOrBuiltinID() ||
1975 II->getFETokenInfo<void>();
1976 }
1977
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001978public:
1979 typedef const IdentifierInfo* key_type;
1980 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001982 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001983 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Sebastian Redl3397c552010-08-18 23:56:27 +00001985 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001986 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001987
1988 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001989 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001990 }
Mike Stump1eb44332009-09-09 15:08:12 +00001991
1992 std::pair<unsigned,unsigned>
1993 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001994 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001995 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001996 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1997 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001998 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001999 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002000 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002001 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002002 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2003 DEnd = IdentifierResolver::end();
2004 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002005 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002006 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002007 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002008 // We emit the key length after the data length so that every
2009 // string is preceded by a 16-bit length. This matches the PTH
2010 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002011 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002012 return std::make_pair(KeyLen, DataLen);
2013 }
Mike Stump1eb44332009-09-09 15:08:12 +00002014
2015 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002016 unsigned KeyLen) {
2017 // Record the location of the key data. This is used when generating
2018 // the mapping from persistent IDs to strings.
2019 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002020 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002021 }
Mike Stump1eb44332009-09-09 15:08:12 +00002022
2023 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002024 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002025 if (!isInterestingIdentifier(II)) {
2026 clang::io::Emit32(Out, ID << 1);
2027 return;
2028 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002029
Douglas Gregora92193e2009-04-28 21:18:29 +00002030 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002031 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002032 bool hasMacroDefinition =
2033 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002034 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002035 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002036 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2037 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2038 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002039 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002040 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002041 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002042
Douglas Gregor37e26842009-04-21 23:56:24 +00002043 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002044 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002045
Douglas Gregor668c1a42009-04-21 22:25:48 +00002046 // Emit the declaration IDs in reverse order, because the
2047 // IdentifierResolver provides the declarations as they would be
2048 // visible (e.g., the function "stat" would come before the struct
2049 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2050 // adds declarations to the end of the list (so we need to see the
2051 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002052 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00002053 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002054 IdentifierResolver::end());
2055 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2056 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002057 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002058 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002059 }
2060};
2061} // end anonymous namespace
2062
Sebastian Redl3397c552010-08-18 23:56:27 +00002063/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002064///
2065/// The identifier table consists of a blob containing string data
2066/// (the actual identifiers themselves) and a separate "offsets" index
2067/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002068void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002069 using namespace llvm;
2070
2071 // Create and write out the blob that contains the identifier
2072 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002073 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002074 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002075 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002076
Douglas Gregor92b059e2009-04-28 20:33:11 +00002077 // Look for any identifiers that were named while processing the
2078 // headers, but are otherwise not needed. We add these to the hash
2079 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002080 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002081 // file.
2082 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2083 IDEnd = PP.getIdentifierTable().end();
2084 ID != IDEnd; ++ID)
2085 getIdentifierRef(ID->second);
2086
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002087 // Create the on-disk hash table representation. We only store offsets
2088 // for identifiers that appear here for the first time.
2089 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002090 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002091 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2092 ID != IDEnd; ++ID) {
2093 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002094 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002095 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002096 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002097
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002098 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002099 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002100 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002101 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002102 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002103 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002104 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002105 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002106 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002107 }
2108
2109 // Create a blob abbreviation
2110 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002111 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002114 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002115
2116 // Write the identifier table
2117 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002118 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002119 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002120 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002121 }
2122
2123 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002124 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002125 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2128 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2129
2130 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002131 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002132 Record.push_back(IdentifierOffsets.size());
2133 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002134 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002135 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002136}
2137
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002138//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002139// DeclContext's Name Lookup Table Serialization
2140//===----------------------------------------------------------------------===//
2141
2142namespace {
2143// Trait used for the on-disk hash table used in the method pool.
2144class ASTDeclContextNameLookupTrait {
2145 ASTWriter &Writer;
2146
2147public:
2148 typedef DeclarationName key_type;
2149 typedef key_type key_type_ref;
2150
2151 typedef DeclContext::lookup_result data_type;
2152 typedef const data_type& data_type_ref;
2153
2154 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2155
2156 unsigned ComputeHash(DeclarationName Name) {
2157 llvm::FoldingSetNodeID ID;
2158 ID.AddInteger(Name.getNameKind());
2159
2160 switch (Name.getNameKind()) {
2161 case DeclarationName::Identifier:
2162 ID.AddString(Name.getAsIdentifierInfo()->getName());
2163 break;
2164 case DeclarationName::ObjCZeroArgSelector:
2165 case DeclarationName::ObjCOneArgSelector:
2166 case DeclarationName::ObjCMultiArgSelector:
2167 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2168 break;
2169 case DeclarationName::CXXConstructorName:
2170 case DeclarationName::CXXDestructorName:
2171 case DeclarationName::CXXConversionFunctionName:
2172 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2173 break;
2174 case DeclarationName::CXXOperatorName:
2175 ID.AddInteger(Name.getCXXOverloadedOperator());
2176 break;
2177 case DeclarationName::CXXLiteralOperatorName:
2178 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2179 case DeclarationName::CXXUsingDirective:
2180 break;
2181 }
2182
2183 return ID.ComputeHash();
2184 }
2185
2186 std::pair<unsigned,unsigned>
2187 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2188 data_type_ref Lookup) {
2189 unsigned KeyLen = 1;
2190 switch (Name.getNameKind()) {
2191 case DeclarationName::Identifier:
2192 case DeclarationName::ObjCZeroArgSelector:
2193 case DeclarationName::ObjCOneArgSelector:
2194 case DeclarationName::ObjCMultiArgSelector:
2195 case DeclarationName::CXXConstructorName:
2196 case DeclarationName::CXXDestructorName:
2197 case DeclarationName::CXXConversionFunctionName:
2198 case DeclarationName::CXXLiteralOperatorName:
2199 KeyLen += 4;
2200 break;
2201 case DeclarationName::CXXOperatorName:
2202 KeyLen += 1;
2203 break;
2204 case DeclarationName::CXXUsingDirective:
2205 break;
2206 }
2207 clang::io::Emit16(Out, KeyLen);
2208
2209 // 2 bytes for num of decls and 4 for each DeclID.
2210 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2211 clang::io::Emit16(Out, DataLen);
2212
2213 return std::make_pair(KeyLen, DataLen);
2214 }
2215
2216 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2217 using namespace clang::io;
2218
2219 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2220 Emit8(Out, Name.getNameKind());
2221 switch (Name.getNameKind()) {
2222 case DeclarationName::Identifier:
2223 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2224 break;
2225 case DeclarationName::ObjCZeroArgSelector:
2226 case DeclarationName::ObjCOneArgSelector:
2227 case DeclarationName::ObjCMultiArgSelector:
2228 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2229 break;
2230 case DeclarationName::CXXConstructorName:
2231 case DeclarationName::CXXDestructorName:
2232 case DeclarationName::CXXConversionFunctionName:
2233 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2234 break;
2235 case DeclarationName::CXXOperatorName:
2236 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2237 Emit8(Out, Name.getCXXOverloadedOperator());
2238 break;
2239 case DeclarationName::CXXLiteralOperatorName:
2240 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2241 break;
2242 case DeclarationName::CXXUsingDirective:
2243 break;
2244 }
2245 }
2246
2247 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2248 data_type Lookup, unsigned DataLen) {
2249 uint64_t Start = Out.tell(); (void)Start;
2250 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2251 for (; Lookup.first != Lookup.second; ++Lookup.first)
2252 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2253
2254 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2255 }
2256};
2257} // end anonymous namespace
2258
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002259/// \brief Write the block containing all of the declaration IDs
2260/// visible from the given DeclContext.
2261///
2262/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002263/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002264uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2265 DeclContext *DC) {
2266 if (DC->getPrimaryContext() != DC)
2267 return 0;
2268
2269 // Since there is no name lookup into functions or methods, don't bother to
2270 // build a visible-declarations table for these entities.
2271 if (DC->isFunctionOrMethod())
2272 return 0;
2273
2274 // If not in C++, we perform name lookup for the translation unit via the
2275 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2276 // FIXME: In C++ we need the visible declarations in order to "see" the
2277 // friend declarations, is there a way to do this without writing the table ?
2278 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2279 return 0;
2280
2281 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002282 if (DC->hasExternalVisibleStorage())
2283 DC->MaterializeVisibleDeclsFromExternalStorage();
2284 else
2285 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002286
2287 // Serialize the contents of the mapping used for lookup. Note that,
2288 // although we have two very different code paths, the serialized
2289 // representation is the same for both cases: a declaration name,
2290 // followed by a size, followed by references to the visible
2291 // declarations that have that name.
2292 uint64_t Offset = Stream.GetCurrentBitNo();
2293 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2294 if (!Map || Map->empty())
2295 return 0;
2296
2297 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2298 ASTDeclContextNameLookupTrait Trait(*this);
2299
2300 // Create the on-disk hash table representation.
2301 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2302 D != DEnd; ++D) {
2303 DeclarationName Name = D->first;
2304 DeclContext::lookup_result Result = D->second.getLookupResult();
2305 Generator.insert(Name, Result, Trait);
2306 }
2307
2308 // Create the on-disk hash table in a buffer.
2309 llvm::SmallString<4096> LookupTable;
2310 uint32_t BucketOffset;
2311 {
2312 llvm::raw_svector_ostream Out(LookupTable);
2313 // Make sure that no bucket is at offset 0
2314 clang::io::Emit32(Out, 0);
2315 BucketOffset = Generator.Emit(Out, Trait);
2316 }
2317
2318 // Write the lookup table
2319 RecordData Record;
2320 Record.push_back(DECL_CONTEXT_VISIBLE);
2321 Record.push_back(BucketOffset);
2322 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2323 LookupTable.str());
2324
2325 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2326 ++NumVisibleDeclContexts;
2327 return Offset;
2328}
2329
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002330/// \brief Write an UPDATE_VISIBLE block for the given context.
2331///
2332/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2333/// DeclContext in a dependent AST file. As such, they only exist for the TU
2334/// (in C++) and for namespaces.
2335void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002336 // Make the context build its lookup table, but don't make it load external
2337 // decls.
2338 DC->lookup(DeclarationName());
2339
2340 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2341 if (!Map || Map->empty())
2342 return;
2343
2344 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2345 ASTDeclContextNameLookupTrait Trait(*this);
2346
2347 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002348 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2349 D != DEnd; ++D) {
2350 DeclarationName Name = D->first;
2351 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002352 // For any name that appears in this table, the results are complete, i.e.
2353 // they overwrite results from previous PCHs. Merging is always a mess.
2354 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002355 }
2356
2357 // Create the on-disk hash table in a buffer.
2358 llvm::SmallString<4096> LookupTable;
2359 uint32_t BucketOffset;
2360 {
2361 llvm::raw_svector_ostream Out(LookupTable);
2362 // Make sure that no bucket is at offset 0
2363 clang::io::Emit32(Out, 0);
2364 BucketOffset = Generator.Emit(Out, Trait);
2365 }
2366
2367 // Write the lookup table
2368 RecordData Record;
2369 Record.push_back(UPDATE_VISIBLE);
2370 Record.push_back(getDeclID(cast<Decl>(DC)));
2371 Record.push_back(BucketOffset);
2372 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2373}
2374
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002375//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002376// General Serialization Routines
2377//===----------------------------------------------------------------------===//
2378
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002379/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002380void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002381 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002382 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2383 const Attr * A = *i;
2384 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2385 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002386
Sean Huntcf807c42010-08-18 23:23:40 +00002387#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002388
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002389 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002390}
2391
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002392void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002393 Record.push_back(Str.size());
2394 Record.insert(Record.end(), Str.begin(), Str.end());
2395}
2396
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002397/// \brief Note that the identifier II occurs at the given offset
2398/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002399void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002400 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002401 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002402 // up earlier in the chain and thus don't need an offset.
2403 if (ID >= FirstIdentID)
2404 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002405}
2406
Douglas Gregor83941df2009-04-25 17:48:32 +00002407/// \brief Note that the selector Sel occurs at the given offset
2408/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002409void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002410 unsigned ID = SelectorIDs[Sel];
2411 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002412 // Don't record offsets for selectors that are also available in a different
2413 // file.
2414 if (ID < FirstSelectorID)
2415 return;
2416 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002417}
2418
Sebastian Redla4232eb2010-08-18 23:56:21 +00002419ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002420 : Stream(Stream), Chain(0), SerializationListener(0),
2421 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002422 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002423 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002424 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2425 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002426 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002427 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2428 NextCXXBaseSpecifiersID(1)
2429{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002430}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002431
Sebastian Redla4232eb2010-08-18 23:56:21 +00002432void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002433 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002434 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002435 Stream.Emit((unsigned)'C', 8);
2436 Stream.Emit((unsigned)'P', 8);
2437 Stream.Emit((unsigned)'C', 8);
2438 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002440 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002441
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002442 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002443 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002444 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002445 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002446}
2447
Sebastian Redla4232eb2010-08-18 23:56:21 +00002448void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002449 const char *isysroot) {
2450 using namespace llvm;
2451
2452 ASTContext &Context = SemaRef.Context;
2453 Preprocessor &PP = SemaRef.PP;
2454
Douglas Gregor2cf26342009-04-09 22:27:44 +00002455 // The translation unit is the first declaration we'll emit.
2456 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002457 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002458 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002459
Douglas Gregor2deaea32009-04-22 18:49:13 +00002460 // Make sure that we emit IdentifierInfos (and any attached
2461 // declarations) for builtins.
2462 {
2463 IdentifierTable &Table = PP.getIdentifierTable();
2464 llvm::SmallVector<const char *, 32> BuiltinNames;
2465 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2466 Context.getLangOptions().NoBuiltin);
2467 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2468 getIdentifierRef(&Table.get(BuiltinNames[I]));
2469 }
2470
Chris Lattner63d65f82009-09-08 18:19:27 +00002471 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002472 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002473 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002474 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002475 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2476 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002477 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002478
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002479 // Build a record containing all of the file scoped decls in this file.
2480 RecordData UnusedFileScopedDecls;
2481 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2482 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002483
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002484 RecordData WeakUndeclaredIdentifiers;
2485 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2486 WeakUndeclaredIdentifiers.push_back(
2487 SemaRef.WeakUndeclaredIdentifiers.size());
2488 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2489 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2490 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2491 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2492 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2493 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2494 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2495 }
2496 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002497
Douglas Gregor14c22f22009-04-22 22:18:58 +00002498 // Build a record containing all of the locally-scoped external
2499 // declarations in this header file. Generally, this record will be
2500 // empty.
2501 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002502 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002503 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002504 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002505 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2506 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2507 TD != TDEnd; ++TD)
2508 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2509
Douglas Gregorb81c1702009-04-27 20:06:05 +00002510 // Build a record containing all of the ext_vector declarations.
2511 RecordData ExtVectorDecls;
2512 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2513 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2514
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002515 // Build a record containing all of the VTable uses information.
2516 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002517 if (!SemaRef.VTableUses.empty()) {
2518 VTableUses.push_back(SemaRef.VTableUses.size());
2519 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2520 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2521 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2522 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2523 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002524 }
2525
2526 // Build a record containing all of dynamic classes declarations.
2527 RecordData DynamicClasses;
2528 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2529 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2530
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002531 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002532 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002533 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002534 I = SemaRef.PendingInstantiations.begin(),
2535 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2536 AddDeclRef(I->first, PendingInstantiations);
2537 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002538 }
2539 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2540 "There are local ones at end of translation unit!");
2541
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002542 // Build a record containing some declaration references.
2543 RecordData SemaDeclRefs;
2544 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2545 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2546 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2547 }
2548
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002549 RecordData CUDASpecialDeclRefs;
2550 if (Context.getcudaConfigureCallDecl()) {
2551 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2552 }
2553
Sebastian Redl3397c552010-08-18 23:56:27 +00002554 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002555 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002556 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002557 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002558 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002559 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002560 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002561 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002562 // Write the record of special types.
2563 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002564
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002565 AddTypeRef(Context.getBuiltinVaListType(), Record);
2566 AddTypeRef(Context.getObjCIdType(), Record);
2567 AddTypeRef(Context.getObjCSelType(), Record);
2568 AddTypeRef(Context.getObjCProtoType(), Record);
2569 AddTypeRef(Context.getObjCClassType(), Record);
2570 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2571 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2572 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002573 AddTypeRef(Context.getjmp_bufType(), Record);
2574 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002575 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2576 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002577 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002578 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002579 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2580 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002581 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002582 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Douglas Gregor366809a2009-04-26 03:49:13 +00002584 // Keep writing types and declarations until all types and
2585 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002586 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002587 WriteDeclsBlockAbbrevs();
2588 while (!DeclTypesToEmit.empty()) {
2589 DeclOrType DOT = DeclTypesToEmit.front();
2590 DeclTypesToEmit.pop();
2591 if (DOT.isType())
2592 WriteType(DOT.getType());
2593 else
2594 WriteDecl(Context, DOT.getDecl());
2595 }
2596 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002597
Douglas Gregor813a97b2009-10-17 17:25:45 +00002598 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002599 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002600 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002601 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002602
Sebastian Redl1476ed42010-07-16 16:36:56 +00002603 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002604 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002605
Douglas Gregor7c789c12010-10-29 22:39:52 +00002606 // Write the C++ base-specifier set offsets.
2607 if (!CXXBaseSpecifiersOffsets.empty()) {
2608 // Create a blob abbreviation for the C++ base specifiers offsets.
2609 using namespace llvm;
2610
2611 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2612 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2615 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2616
2617 // Write the selector offsets table.
2618 Record.clear();
2619 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2620 Record.push_back(CXXBaseSpecifiersOffsets.size());
2621 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2622 (const char *)CXXBaseSpecifiersOffsets.data(),
2623 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2624 }
2625
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002626 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002627 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002628 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002629
2630 // Write the record containing tentative definitions.
2631 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002632 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002633
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002634 // Write the record containing unused file scoped decls.
2635 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002636 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002637
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002638 // Write the record containing weak undeclared identifiers.
2639 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002640 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002641 WeakUndeclaredIdentifiers);
2642
Douglas Gregor14c22f22009-04-22 22:18:58 +00002643 // Write the record containing locally-scoped external definitions.
2644 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002645 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002646 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002647
2648 // Write the record containing ext_vector type names.
2649 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002650 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002651
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002652 // Write the record containing VTable uses information.
2653 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002654 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002655
2656 // Write the record containing dynamic classes declarations.
2657 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002658 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002659
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002660 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002661 if (!PendingInstantiations.empty())
2662 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002663
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002664 // Write the record containing declaration references of Sema.
2665 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002666 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002667
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002668 // Write the record containing CUDA-specific declaration references.
2669 if (!CUDASpecialDeclRefs.empty())
2670 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2671
Douglas Gregor3e1af842009-04-17 22:13:46 +00002672 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002673 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002674 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002675 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002676 Record.push_back(NumLexicalDeclContexts);
2677 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002678 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002679 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002680}
2681
Sebastian Redla4232eb2010-08-18 23:56:21 +00002682void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002683 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002684 using namespace llvm;
2685
2686 ASTContext &Context = SemaRef.Context;
2687 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002688
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002689 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002690 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002691 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002692 if (StatCalls && !isysroot)
2693 WriteStatCache(*StatCalls);
2694 // FIXME: Source manager block should only write new stuff, which could be
2695 // done by tracking the largest ID in the chain
2696 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002697
2698 // The special types are in the chained PCH.
2699
2700 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002701 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002702 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002703 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002704 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2705 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002706 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002707 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002708 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002709 else if ((*I)->isChangedSinceDeserialization())
2710 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002711 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002712 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002713 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002714 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002715 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2716 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2717 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002718 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002719 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2720 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002721 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002722 // And a visible updates block for the DeclContexts.
2723 Abv = new llvm::BitCodeAbbrev();
2724 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2725 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2726 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2727 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2728 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2729 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002730
Sebastian Redl083abdf2010-07-27 23:01:28 +00002731 // Build a record containing all of the new tentative definitions in this
2732 // file, in TentativeDefinitions order.
2733 RecordData TentativeDefinitions;
2734 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2735 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2736 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2737 }
2738
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002739 // Build a record containing all of the file scoped decls in this file.
2740 RecordData UnusedFileScopedDecls;
2741 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2742 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2743 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002744 }
2745
Sebastian Redl40566802010-08-05 18:21:25 +00002746 // We write the entire table, overwriting the tables from the chain.
2747 RecordData WeakUndeclaredIdentifiers;
2748 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2749 WeakUndeclaredIdentifiers.push_back(
2750 SemaRef.WeakUndeclaredIdentifiers.size());
2751 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2752 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2753 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2754 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2755 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2756 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2757 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2758 }
2759 }
2760
Sebastian Redl083abdf2010-07-27 23:01:28 +00002761 // Build a record containing all of the locally-scoped external
2762 // declarations in this header file. Generally, this record will be
2763 // empty.
2764 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002765 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002766 // nondeterminstic!
2767 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2768 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2769 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2770 TD != TDEnd; ++TD) {
2771 if (TD->second->getPCHLevel() == 0)
2772 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2773 }
2774
2775 // Build a record containing all of the ext_vector declarations.
2776 RecordData ExtVectorDecls;
2777 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2778 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2779 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2780 }
2781
Sebastian Redl40566802010-08-05 18:21:25 +00002782 // Build a record containing all of the VTable uses information.
2783 // We write everything here, because it's too hard to determine whether
2784 // a use is new to this part.
2785 RecordData VTableUses;
2786 if (!SemaRef.VTableUses.empty()) {
2787 VTableUses.push_back(SemaRef.VTableUses.size());
2788 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2789 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2790 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2791 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2792 }
2793 }
2794
2795 // Build a record containing all of dynamic classes declarations.
2796 RecordData DynamicClasses;
2797 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2798 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2799 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2800
2801 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002802 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002803 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002804 I = SemaRef.PendingInstantiations.begin(),
2805 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00002806 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002807 AddDeclRef(I->first, PendingInstantiations);
2808 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002809 }
2810 }
2811 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2812 "There are local ones at end of translation unit!");
2813
2814 // Build a record containing some declaration references.
2815 // It's not worth the effort to avoid duplication here.
2816 RecordData SemaDeclRefs;
2817 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2818 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2819 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2820 }
2821
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002822 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002823 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00002824 for (DeclsToRewriteTy::iterator
2825 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2826 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002827 while (!DeclTypesToEmit.empty()) {
2828 DeclOrType DOT = DeclTypesToEmit.front();
2829 DeclTypesToEmit.pop();
2830 if (DOT.isType())
2831 WriteType(DOT.getType());
2832 else
2833 WriteDecl(Context, DOT.getDecl());
2834 }
2835 Stream.ExitBlock();
2836
Sebastian Redl083abdf2010-07-27 23:01:28 +00002837 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002838 WriteSelectors(SemaRef);
2839 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002840 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002841 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002842 // FIXME: For chained PCH only write the new mappings (we currently
2843 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002844 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00002845
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002846 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002847 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002848 RecordData FirstLatestDeclIDs;
2849 for (FirstLatestDeclMap::iterator
2850 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2851 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2852 "Expected first & second to be in different PCHs");
2853 AddDeclRef(I->first, FirstLatestDeclIDs);
2854 AddDeclRef(I->second, FirstLatestDeclIDs);
2855 }
2856 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002857 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002858
Sebastian Redl083abdf2010-07-27 23:01:28 +00002859 // Write the record containing external, unnamed definitions.
2860 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002861 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002862
2863 // Write the record containing tentative definitions.
2864 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002865 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002866
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002867 // Write the record containing unused file scoped decls.
2868 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002869 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002870
Sebastian Redl40566802010-08-05 18:21:25 +00002871 // Write the record containing weak undeclared identifiers.
2872 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002873 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00002874 WeakUndeclaredIdentifiers);
2875
Sebastian Redl083abdf2010-07-27 23:01:28 +00002876 // Write the record containing locally-scoped external definitions.
2877 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002878 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00002879 LocallyScopedExternalDecls);
2880
2881 // Write the record containing ext_vector type names.
2882 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002883 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002884
Sebastian Redl40566802010-08-05 18:21:25 +00002885 // Write the record containing VTable uses information.
2886 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002887 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00002888
2889 // Write the record containing dynamic classes declarations.
2890 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002891 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00002892
2893 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002894 if (!PendingInstantiations.empty())
2895 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002896
2897 // Write the record containing declaration references of Sema.
2898 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002899 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002900
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002901 // Write the updates to DeclContexts.
2902 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2903 I = UpdatedDeclContexts.begin(),
2904 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002905 I != E; ++I)
2906 WriteDeclContextVisibleUpdate(*I);
2907
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002908 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002909
Sebastian Redl083abdf2010-07-27 23:01:28 +00002910 Record.clear();
2911 Record.push_back(NumStatements);
2912 Record.push_back(NumMacros);
2913 Record.push_back(NumLexicalDeclContexts);
2914 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002915 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002916 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002917 Stream.ExitBlock();
2918}
2919
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002920void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002921 if (DeclUpdates.empty())
2922 return;
2923
2924 RecordData OffsetsRecord;
2925 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2926 for (DeclUpdateMap::iterator
2927 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2928 const Decl *D = I->first;
2929 UpdateRecord &URec = I->second;
2930
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00002931 if (DeclsToRewrite.count(D))
2932 continue; // The decl will be written completely,no need to store updates.
2933
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002934 uint64_t Offset = Stream.GetCurrentBitNo();
2935 Stream.EmitRecord(DECL_UPDATES, URec);
2936
2937 OffsetsRecord.push_back(GetDeclRef(D));
2938 OffsetsRecord.push_back(Offset);
2939 }
2940 Stream.ExitBlock();
2941 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2942}
2943
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002944void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002945 if (ReplacedDecls.empty())
2946 return;
2947
2948 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002949 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00002950 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2951 Record.push_back(I->first);
2952 Record.push_back(I->second);
2953 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002954 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002955}
2956
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002957void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002958 Record.push_back(Loc.getRawEncoding());
2959}
2960
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002961void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002962 AddSourceLocation(Range.getBegin(), Record);
2963 AddSourceLocation(Range.getEnd(), Record);
2964}
2965
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002966void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002967 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00002968 const uint64_t *Words = Value.getRawData();
2969 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002970}
2971
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002972void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002973 Record.push_back(Value.isUnsigned());
2974 AddAPInt(Value, Record);
2975}
2976
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002977void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002978 AddAPInt(Value.bitcastToAPInt(), Record);
2979}
2980
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002981void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002982 Record.push_back(getIdentifierRef(II));
2983}
2984
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002985IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002986 if (II == 0)
2987 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002988
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002989 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002990 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002991 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002992 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002993}
2994
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002995MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002996 if (MD == 0)
2997 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002998
2999 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003000 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003001 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003002 return ID;
3003}
3004
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003005void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003006 Record.push_back(getSelectorRef(SelRef));
3007}
3008
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003009SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003010 if (Sel.getAsOpaquePtr() == 0) {
3011 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003012 }
3013
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003014 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003015 if (SID == 0 && Chain) {
3016 // This might trigger a ReadSelector callback, which will set the ID for
3017 // this selector.
3018 Chain->LoadSelector(Sel);
3019 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003020 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003021 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003022 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003023 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003024}
3025
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003026void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003027 AddDeclRef(Temp->getDestructor(), Record);
3028}
3029
Douglas Gregor7c789c12010-10-29 22:39:52 +00003030void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3031 CXXBaseSpecifier const *BasesEnd,
3032 RecordDataImpl &Record) {
3033 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3034 CXXBaseSpecifiersToWrite.push_back(
3035 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3036 Bases, BasesEnd));
3037 Record.push_back(NextCXXBaseSpecifiersID++);
3038}
3039
Sebastian Redla4232eb2010-08-18 23:56:21 +00003040void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003041 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003042 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003043 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003044 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003045 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003046 break;
3047 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003048 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003049 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003050 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003051 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3052 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003053 break;
3054 case TemplateArgument::TemplateExpansion:
3055 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3056 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003057 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003058 break;
John McCall833ca992009-10-29 08:12:44 +00003059 case TemplateArgument::Null:
3060 case TemplateArgument::Integral:
3061 case TemplateArgument::Declaration:
3062 case TemplateArgument::Pack:
3063 break;
3064 }
3065}
3066
Sebastian Redla4232eb2010-08-18 23:56:21 +00003067void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003068 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003069 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003070
3071 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3072 bool InfoHasSameExpr
3073 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3074 Record.push_back(InfoHasSameExpr);
3075 if (InfoHasSameExpr)
3076 return; // Avoid storing the same expr twice.
3077 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003078 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3079 Record);
3080}
3081
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003082void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003083 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003084 AddTypeRef(QualType(), Record);
3085 return;
3086 }
3087
John McCalla93c9342009-12-07 02:54:59 +00003088 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00003089 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00003090 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003091 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003092}
3093
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003094void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003095 Record.push_back(GetOrCreateTypeID(T));
3096}
3097
3098TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003099 return MakeTypeID(T,
3100 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3101}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003102
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003103TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003104 return MakeTypeID(T,
3105 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003106}
3107
3108TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3109 if (T.isNull())
3110 return TypeIdx();
3111 assert(!T.getLocalFastQualifiers());
3112
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003113 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003114 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003115 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003116 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003117 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003118 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003119 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003120 return Idx;
3121}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003122
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003123TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003124 if (T.isNull())
3125 return TypeIdx();
3126 assert(!T.getLocalFastQualifiers());
3127
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003128 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3129 assert(I != TypeIdxs.end() && "Type not emitted!");
3130 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003131}
3132
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003133void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003134 Record.push_back(GetDeclRef(D));
3135}
3136
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003137DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003138 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003139 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003140 }
Douglas Gregor97475832010-10-05 18:37:06 +00003141 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003142 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003143 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003144 // We haven't seen this declaration before. Give it a new ID and
3145 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003146 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003147 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003148 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3149 // We don't add it to the replacement collection here, because we don't
3150 // have the offset yet.
3151 DeclTypesToEmit.push(const_cast<Decl *>(D));
3152 // Reset the flag, so that we don't add this decl multiple times.
3153 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003154 }
3155
Sebastian Redl681d7232010-07-27 00:17:23 +00003156 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003157}
3158
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003159DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003160 if (D == 0)
3161 return 0;
3162
3163 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3164 return DeclIDs[D];
3165}
3166
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003167void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003168 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003169 Record.push_back(Name.getNameKind());
3170 switch (Name.getNameKind()) {
3171 case DeclarationName::Identifier:
3172 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3173 break;
3174
3175 case DeclarationName::ObjCZeroArgSelector:
3176 case DeclarationName::ObjCOneArgSelector:
3177 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003178 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003179 break;
3180
3181 case DeclarationName::CXXConstructorName:
3182 case DeclarationName::CXXDestructorName:
3183 case DeclarationName::CXXConversionFunctionName:
3184 AddTypeRef(Name.getCXXNameType(), Record);
3185 break;
3186
3187 case DeclarationName::CXXOperatorName:
3188 Record.push_back(Name.getCXXOverloadedOperator());
3189 break;
3190
Sean Hunt3e518bd2009-11-29 07:34:05 +00003191 case DeclarationName::CXXLiteralOperatorName:
3192 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3193 break;
3194
Douglas Gregor2cf26342009-04-09 22:27:44 +00003195 case DeclarationName::CXXUsingDirective:
3196 // No extra data to emit
3197 break;
3198 }
3199}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003200
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003201void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003202 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003203 switch (Name.getNameKind()) {
3204 case DeclarationName::CXXConstructorName:
3205 case DeclarationName::CXXDestructorName:
3206 case DeclarationName::CXXConversionFunctionName:
3207 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3208 break;
3209
3210 case DeclarationName::CXXOperatorName:
3211 AddSourceLocation(
3212 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3213 Record);
3214 AddSourceLocation(
3215 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3216 Record);
3217 break;
3218
3219 case DeclarationName::CXXLiteralOperatorName:
3220 AddSourceLocation(
3221 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3222 Record);
3223 break;
3224
3225 case DeclarationName::Identifier:
3226 case DeclarationName::ObjCZeroArgSelector:
3227 case DeclarationName::ObjCOneArgSelector:
3228 case DeclarationName::ObjCMultiArgSelector:
3229 case DeclarationName::CXXUsingDirective:
3230 break;
3231 }
3232}
3233
3234void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003235 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003236 AddDeclarationName(NameInfo.getName(), Record);
3237 AddSourceLocation(NameInfo.getLoc(), Record);
3238 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3239}
3240
3241void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003242 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003243 AddNestedNameSpecifier(Info.NNS, Record);
3244 AddSourceRange(Info.NNSRange, Record);
3245 Record.push_back(Info.NumTemplParamLists);
3246 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3247 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3248}
3249
Sebastian Redla4232eb2010-08-18 23:56:21 +00003250void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003251 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003252 // Nested name specifiers usually aren't too long. I think that 8 would
3253 // typically accomodate the vast majority.
3254 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3255
3256 // Push each of the NNS's onto a stack for serialization in reverse order.
3257 while (NNS) {
3258 NestedNames.push_back(NNS);
3259 NNS = NNS->getPrefix();
3260 }
3261
3262 Record.push_back(NestedNames.size());
3263 while(!NestedNames.empty()) {
3264 NNS = NestedNames.pop_back_val();
3265 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3266 Record.push_back(Kind);
3267 switch (Kind) {
3268 case NestedNameSpecifier::Identifier:
3269 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3270 break;
3271
3272 case NestedNameSpecifier::Namespace:
3273 AddDeclRef(NNS->getAsNamespace(), Record);
3274 break;
3275
3276 case NestedNameSpecifier::TypeSpec:
3277 case NestedNameSpecifier::TypeSpecWithTemplate:
3278 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3279 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3280 break;
3281
3282 case NestedNameSpecifier::Global:
3283 // Don't need to write an associated value.
3284 break;
3285 }
3286 }
3287}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003288
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003289void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003290 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003291 Record.push_back(Kind);
3292 switch (Kind) {
3293 case TemplateName::Template:
3294 AddDeclRef(Name.getAsTemplateDecl(), Record);
3295 break;
3296
3297 case TemplateName::OverloadedTemplate: {
3298 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3299 Record.push_back(OvT->size());
3300 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3301 I != E; ++I)
3302 AddDeclRef(*I, Record);
3303 break;
3304 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003305
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003306 case TemplateName::QualifiedTemplate: {
3307 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3308 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3309 Record.push_back(QualT->hasTemplateKeyword());
3310 AddDeclRef(QualT->getTemplateDecl(), Record);
3311 break;
3312 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003313
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003314 case TemplateName::DependentTemplate: {
3315 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3316 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3317 Record.push_back(DepT->isIdentifier());
3318 if (DepT->isIdentifier())
3319 AddIdentifierRef(DepT->getIdentifier(), Record);
3320 else
3321 Record.push_back(DepT->getOperator());
3322 break;
3323 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003324
3325 case TemplateName::SubstTemplateTemplateParmPack: {
3326 SubstTemplateTemplateParmPackStorage *SubstPack
3327 = Name.getAsSubstTemplateTemplateParmPack();
3328 AddDeclRef(SubstPack->getParameterPack(), Record);
3329 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3330 break;
3331 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003332 }
3333}
3334
Michael J. Spencer20249a12010-10-21 03:16:25 +00003335void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003336 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003337 Record.push_back(Arg.getKind());
3338 switch (Arg.getKind()) {
3339 case TemplateArgument::Null:
3340 break;
3341 case TemplateArgument::Type:
3342 AddTypeRef(Arg.getAsType(), Record);
3343 break;
3344 case TemplateArgument::Declaration:
3345 AddDeclRef(Arg.getAsDecl(), Record);
3346 break;
3347 case TemplateArgument::Integral:
3348 AddAPSInt(*Arg.getAsIntegral(), Record);
3349 AddTypeRef(Arg.getIntegralType(), Record);
3350 break;
3351 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003352 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3353 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003354 case TemplateArgument::TemplateExpansion:
3355 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003356 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3357 Record.push_back(*NumExpansions + 1);
3358 else
3359 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003360 break;
3361 case TemplateArgument::Expression:
3362 AddStmt(Arg.getAsExpr());
3363 break;
3364 case TemplateArgument::Pack:
3365 Record.push_back(Arg.pack_size());
3366 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3367 I != E; ++I)
3368 AddTemplateArgument(*I, Record);
3369 break;
3370 }
3371}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003372
3373void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003374ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003375 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003376 assert(TemplateParams && "No TemplateParams!");
3377 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3378 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3379 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3380 Record.push_back(TemplateParams->size());
3381 for (TemplateParameterList::const_iterator
3382 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3383 P != PEnd; ++P)
3384 AddDeclRef(*P, Record);
3385}
3386
3387/// \brief Emit a template argument list.
3388void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003389ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003390 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003391 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003392 Record.push_back(TemplateArgs->size());
3393 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003394 AddTemplateArgument(TemplateArgs->get(i), Record);
3395}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003396
3397
3398void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003399ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003400 Record.push_back(Set.size());
3401 for (UnresolvedSetImpl::const_iterator
3402 I = Set.begin(), E = Set.end(); I != E; ++I) {
3403 AddDeclRef(I.getDecl(), Record);
3404 Record.push_back(I.getAccess());
3405 }
3406}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003407
Sebastian Redla4232eb2010-08-18 23:56:21 +00003408void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003409 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003410 Record.push_back(Base.isVirtual());
3411 Record.push_back(Base.isBaseOfClass());
3412 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003413 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003414 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003415 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003416 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3417 : SourceLocation(),
3418 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003419}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003420
Douglas Gregor7c789c12010-10-29 22:39:52 +00003421void ASTWriter::FlushCXXBaseSpecifiers() {
3422 RecordData Record;
3423 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3424 Record.clear();
3425
3426 // Record the offset of this base-specifier set.
3427 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3428 if (Index == CXXBaseSpecifiersOffsets.size())
3429 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3430 else {
3431 if (Index > CXXBaseSpecifiersOffsets.size())
3432 CXXBaseSpecifiersOffsets.resize(Index + 1);
3433 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3434 }
3435
3436 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3437 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3438 Record.push_back(BEnd - B);
3439 for (; B != BEnd; ++B)
3440 AddCXXBaseSpecifier(*B, Record);
3441 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003442
3443 // Flush any expressions that were written as part of the base specifiers.
3444 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003445 }
3446
3447 CXXBaseSpecifiersToWrite.clear();
3448}
3449
Sean Huntcbb67482011-01-08 20:30:50 +00003450void ASTWriter::AddCXXCtorInitializers(
3451 const CXXCtorInitializer * const *CtorInitializers,
3452 unsigned NumCtorInitializers,
3453 RecordDataImpl &Record) {
3454 Record.push_back(NumCtorInitializers);
3455 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3456 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003457
3458 Record.push_back(Init->isBaseInitializer());
3459 if (Init->isBaseInitializer()) {
3460 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3461 Record.push_back(Init->isBaseVirtual());
3462 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003463 Record.push_back(Init->isIndirectMemberInitializer());
3464 if (Init->isIndirectMemberInitializer())
3465 AddDeclRef(Init->getIndirectMember(), Record);
3466 else
3467 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003468 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003469
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003470 AddSourceLocation(Init->getMemberLocation(), Record);
3471 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003472 AddSourceLocation(Init->getLParenLoc(), Record);
3473 AddSourceLocation(Init->getRParenLoc(), Record);
3474 Record.push_back(Init->isWritten());
3475 if (Init->isWritten()) {
3476 Record.push_back(Init->getSourceOrder());
3477 } else {
3478 Record.push_back(Init->getNumArrayIndices());
3479 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3480 AddDeclRef(Init->getArrayIndex(i), Record);
3481 }
3482 }
3483}
3484
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003485void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3486 assert(D->DefinitionData);
3487 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3488 Record.push_back(Data.UserDeclaredConstructor);
3489 Record.push_back(Data.UserDeclaredCopyConstructor);
3490 Record.push_back(Data.UserDeclaredCopyAssignment);
3491 Record.push_back(Data.UserDeclaredDestructor);
3492 Record.push_back(Data.Aggregate);
3493 Record.push_back(Data.PlainOldData);
3494 Record.push_back(Data.Empty);
3495 Record.push_back(Data.Polymorphic);
3496 Record.push_back(Data.Abstract);
3497 Record.push_back(Data.HasTrivialConstructor);
3498 Record.push_back(Data.HasTrivialCopyConstructor);
3499 Record.push_back(Data.HasTrivialCopyAssignment);
3500 Record.push_back(Data.HasTrivialDestructor);
3501 Record.push_back(Data.ComputedVisibleConversions);
3502 Record.push_back(Data.DeclaredDefaultConstructor);
3503 Record.push_back(Data.DeclaredCopyConstructor);
3504 Record.push_back(Data.DeclaredCopyAssignment);
3505 Record.push_back(Data.DeclaredDestructor);
3506
3507 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003508 if (Data.NumBases > 0)
3509 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3510 Record);
3511
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003512 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3513 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003514 if (Data.NumVBases > 0)
3515 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3516 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003517
3518 AddUnresolvedSet(Data.Conversions, Record);
3519 AddUnresolvedSet(Data.VisibleConversions, Record);
3520 // Data.Definition is the owning decl, no need to write it.
3521 AddDeclRef(Data.FirstFriend, Record);
3522}
3523
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003524void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003525 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003526 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003527 assert(FirstDeclID == NextDeclID &&
3528 FirstTypeID == NextTypeID &&
3529 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003530 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003531 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003532 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003533 "Setting chain after writing has started.");
3534 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003535
3536 FirstDeclID += Chain->getTotalNumDecls();
3537 FirstTypeID += Chain->getTotalNumTypes();
3538 FirstIdentID += Chain->getTotalNumIdentifiers();
3539 FirstSelectorID += Chain->getTotalNumSelectors();
3540 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003541 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003542 NextDeclID = FirstDeclID;
3543 NextTypeID = FirstTypeID;
3544 NextIdentID = FirstIdentID;
3545 NextSelectorID = FirstSelectorID;
3546 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003547 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003548}
3549
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003550void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003551 IdentifierIDs[II] = ID;
3552}
3553
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003554void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003555 // Always take the highest-numbered type index. This copes with an interesting
3556 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003557 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003558 // keep the higher-numbered entry so that we can properly write it out to
3559 // the AST file.
3560 TypeIdx &StoredIdx = TypeIdxs[T];
3561 if (Idx.getIndex() >= StoredIdx.getIndex())
3562 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003563}
3564
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003565void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003566 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003567}
Sebastian Redl5d050072010-08-04 17:20:04 +00003568
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003569void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003570 SelectorIDs[S] = ID;
3571}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003572
Michael J. Spencer20249a12010-10-21 03:16:25 +00003573void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003574 MacroDefinition *MD) {
3575 MacroDefinitions[MD] = ID;
3576}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003577
3578void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3579 assert(D->isDefinition());
3580 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3581 // We are interested when a PCH decl is modified.
3582 if (RD->getPCHLevel() > 0) {
3583 // A forward reference was mutated into a definition. Rewrite it.
3584 // FIXME: This happens during template instantiation, should we
3585 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003586 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003587 }
3588
3589 for (CXXRecordDecl::redecl_iterator
3590 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3591 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3592 if (Redecl == RD)
3593 continue;
3594
3595 // We are interested when a PCH decl is modified.
3596 if (Redecl->getPCHLevel() > 0) {
3597 UpdateRecord &Record = DeclUpdates[Redecl];
3598 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3599 assert(Redecl->DefinitionData);
3600 assert(Redecl->DefinitionData->Definition == D);
3601 AddDeclRef(D, Record); // the DefinitionDecl
3602 }
3603 }
3604 }
3605}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003606void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3607 // TU and namespaces are handled elsewhere.
3608 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3609 return;
3610
3611 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3612 return; // Not a source decl added to a DeclContext from PCH.
3613
3614 AddUpdatedDeclContext(DC);
3615}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003616
3617void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3618 assert(D->isImplicit());
3619 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3620 return; // Not a source member added to a class from PCH.
3621 if (!isa<CXXMethodDecl>(D))
3622 return; // We are interested in lazily declared implicit methods.
3623
3624 // A decl coming from PCH was modified.
3625 assert(RD->isDefinition());
3626 UpdateRecord &Record = DeclUpdates[RD];
3627 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3628 AddDeclRef(D, Record);
3629}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003630
3631void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3632 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003633 // The specializations set is kept in the canonical template.
3634 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003635 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3636 return; // Not a source specialization added to a template from PCH.
3637
3638 UpdateRecord &Record = DeclUpdates[TD];
3639 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3640 AddDeclRef(D, Record);
3641}
Douglas Gregor89d99802010-11-30 06:16:57 +00003642
3643ASTSerializationListener::~ASTSerializationListener() { }