blob: 2df14937e3ee9b265f28451cd04ba5812d464364 [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 Gregor0a0d2b12011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000050using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000051using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000052
Sebastian Redlade50002010-07-30 17:03:48 +000053template <typename T, typename Allocator>
Benjamin Kramer6e089c62011-04-24 17:44:50 +000054static llvm::StringRef data(const std::vector<T, Allocator> &v) {
55 if (v.empty()) return llvm::StringRef();
56 return llvm::StringRef(reinterpret_cast<const char*>(&v[0]),
57 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000058}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000059
60template <typename T>
61static llvm::StringRef data(const llvm::SmallVectorImpl<T> &v) {
62 return llvm::StringRef(reinterpret_cast<const char*>(v.data()),
63 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000064}
65
Douglas Gregor2cf26342009-04-09 22:27:44 +000066//===----------------------------------------------------------------------===//
67// Type serialization
68//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000069
Douglas Gregor2cf26342009-04-09 22:27:44 +000070namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000071 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000072 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000073 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000074
75 public:
76 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000077 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000078
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000079 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000080 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000081
82 void VisitArrayType(const ArrayType *T);
83 void VisitFunctionType(const FunctionType *T);
84 void VisitTagType(const TagType *T);
85
86#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
87#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000088#include "clang/AST/TypeNodes.def"
89 };
90}
91
Sebastian Redl3397c552010-08-18 23:56:27 +000092void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000093 assert(false && "Built-in types are never serialized");
94}
95
Sebastian Redl3397c552010-08-18 23:56:27 +000096void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000098 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000112 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
113 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000114 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000115}
116
Sebastian Redl3397c552010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000118 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000123 Writer.AddTypeRef(T->getPointeeType(), Record);
124 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000125 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126}
127
Sebastian Redl3397c552010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000131 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132}
133
Sebastian Redl3397c552010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135 VisitArrayType(T);
136 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000137 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000138}
139
Sebastian Redl3397c552010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000141 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000147 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
148 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000149 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000150 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151}
152
Sebastian Redl3397c552010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154 Writer.AddTypeRef(T->getElementType(), Record);
155 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000156 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000157 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000158}
159
Sebastian Redl3397c552010-08-18 23:56:27 +0000160void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000161 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000162 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163}
164
Sebastian Redl3397c552010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000167 FunctionType::ExtInfo C = T->getExtInfo();
168 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000169 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000170 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000171 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000172 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000173 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000174}
175
Sebastian Redl3397c552010-08-18 23:56:27 +0000176void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000177 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000178 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179}
180
Sebastian Redl3397c552010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182 VisitFunctionType(T);
183 Record.push_back(T->getNumArgs());
184 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
185 Writer.AddTypeRef(T->getArgType(I), Record);
186 Record.push_back(T->isVariadic());
187 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000188 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000189 Record.push_back(T->getExceptionSpecType());
190 if (T->getExceptionSpecType() == EST_Dynamic) {
191 Record.push_back(T->getNumExceptions());
192 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
193 Writer.AddTypeRef(T->getExceptionType(I), Record);
194 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
195 Writer.AddStmt(T->getNoexceptExpr());
196 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000197 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198}
199
Sebastian Redl3397c552010-08-18 23:56:27 +0000200void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000201 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000202 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000203}
John McCalled976492009-12-04 22:46:56 +0000204
Sebastian Redl3397c552010-08-18 23:56:27 +0000205void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000207 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
208 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000209 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000210}
211
Sebastian Redl3397c552010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000213 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000214 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000215}
216
Sebastian Redl3397c552010-08-18 23:56:27 +0000217void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000218 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000219 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220}
221
Sebastian Redl3397c552010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000223 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000224 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000225}
226
Sean Huntca63c202011-05-24 22:41:36 +0000227void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
228 Writer.AddTypeRef(T->getBaseType(), Record);
229 Writer.AddTypeRef(T->getUnderlyingType(), Record);
230 Record.push_back(T->getUTTKind());
231 Code = TYPE_UNARY_TRANSFORM;
232}
233
Richard Smith34b41d92011-02-20 03:19:35 +0000234void ASTTypeWriter::VisitAutoType(const AutoType *T) {
235 Writer.AddTypeRef(T->getDeducedType(), Record);
236 Code = TYPE_AUTO;
237}
238
Sebastian Redl3397c552010-08-18 23:56:27 +0000239void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000240 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000241 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000242 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000243 "Cannot serialize in the middle of a type definition");
244}
245
Sebastian Redl3397c552010-08-18 23:56:27 +0000246void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000247 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000248 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249}
250
Sebastian Redl3397c552010-08-18 23:56:27 +0000251void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000252 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000253 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254}
255
John McCall9d156a72011-01-06 01:58:22 +0000256void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
257 Writer.AddTypeRef(T->getModifiedType(), Record);
258 Writer.AddTypeRef(T->getEquivalentType(), Record);
259 Record.push_back(T->getAttrKind());
260 Code = TYPE_ATTRIBUTED;
261}
262
Mike Stump1eb44332009-09-09 15:08:12 +0000263void
Sebastian Redl3397c552010-08-18 23:56:27 +0000264ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000265 const SubstTemplateTypeParmType *T) {
266 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
267 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000268 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000269}
270
271void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000272ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
273 const SubstTemplateTypeParmPackType *T) {
274 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
275 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
276 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
277}
278
279void
Sebastian Redl3397c552010-08-18 23:56:27 +0000280ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000281 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000282 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000283 Writer.AddTemplateName(T->getTemplateName(), Record);
284 Record.push_back(T->getNumArgs());
285 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
286 ArgI != ArgE; ++ArgI)
287 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000288 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
289 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000290 : T->getCanonicalTypeInternal(),
291 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000292 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000293}
294
295void
Sebastian Redl3397c552010-08-18 23:56:27 +0000296ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000297 VisitArrayType(T);
298 Writer.AddStmt(T->getSizeExpr());
299 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000300 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000301}
302
303void
Sebastian Redl3397c552010-08-18 23:56:27 +0000304ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000305 const DependentSizedExtVectorType *T) {
306 // FIXME: Serialize this type (C++ only)
307 assert(false && "Cannot serialize dependent sized extended vector types");
308}
309
310void
Sebastian Redl3397c552010-08-18 23:56:27 +0000311ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000312 Record.push_back(T->getDepth());
313 Record.push_back(T->getIndex());
314 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000315 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000316 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317}
318
319void
Sebastian Redl3397c552010-08-18 23:56:27 +0000320ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000321 Record.push_back(T->getKeyword());
322 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
323 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000324 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
325 : T->getCanonicalTypeInternal(),
326 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000327 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000328}
329
330void
Sebastian Redl3397c552010-08-18 23:56:27 +0000331ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000332 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000333 Record.push_back(T->getKeyword());
334 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
335 Writer.AddIdentifierRef(T->getIdentifier(), Record);
336 Record.push_back(T->getNumArgs());
337 for (DependentTemplateSpecializationType::iterator
338 I = T->begin(), E = T->end(); I != E; ++I)
339 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000340 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000341}
342
Douglas Gregor7536dd52010-12-20 02:24:11 +0000343void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
344 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000345 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
346 Record.push_back(*NumExpansions + 1);
347 else
348 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000349 Code = TYPE_PACK_EXPANSION;
350}
351
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000352void ASTTypeWriter::VisitParenType(const ParenType *T) {
353 Writer.AddTypeRef(T->getInnerType(), Record);
354 Code = TYPE_PAREN;
355}
356
Sebastian Redl3397c552010-08-18 23:56:27 +0000357void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000358 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000359 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
360 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000361 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000362}
363
Sebastian Redl3397c552010-08-18 23:56:27 +0000364void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000365 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000366 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000367 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000368}
369
Sebastian Redl3397c552010-08-18 23:56:27 +0000370void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000371 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000372 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000373}
374
Sebastian Redl3397c552010-08-18 23:56:27 +0000375void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000376 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000377 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000378 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000379 E = T->qual_end(); I != E; ++I)
380 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000381 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000382}
383
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000384void
Sebastian Redl3397c552010-08-18 23:56:27 +0000385ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000386 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000387 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000388}
389
John McCalla1ee0c52009-10-16 21:56:05 +0000390namespace {
391
392class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000393 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000394 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000395
396public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000397 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000398 : Writer(Writer), Record(Record) { }
399
John McCall51bd8032009-10-18 01:05:36 +0000400#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000401#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000402 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000403#include "clang/AST/TypeLocNodes.def"
404
John McCall51bd8032009-10-18 01:05:36 +0000405 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
406 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000407};
408
409}
410
John McCall51bd8032009-10-18 01:05:36 +0000411void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
412 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000413}
John McCall51bd8032009-10-18 01:05:36 +0000414void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000415 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
416 if (TL.needsExtraLocalData()) {
417 Record.push_back(TL.getWrittenTypeSpec());
418 Record.push_back(TL.getWrittenSignSpec());
419 Record.push_back(TL.getWrittenWidthSpec());
420 Record.push_back(TL.hasModeAttr());
421 }
John McCalla1ee0c52009-10-16 21:56:05 +0000422}
John McCall51bd8032009-10-18 01:05:36 +0000423void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000425}
John McCall51bd8032009-10-18 01:05:36 +0000426void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000428}
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
430 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000431}
John McCall51bd8032009-10-18 01:05:36 +0000432void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
433 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000434}
John McCall51bd8032009-10-18 01:05:36 +0000435void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
436 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000437}
John McCall51bd8032009-10-18 01:05:36 +0000438void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000440 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000441}
John McCall51bd8032009-10-18 01:05:36 +0000442void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
444 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
445 Record.push_back(TL.getSizeExpr() ? 1 : 0);
446 if (TL.getSizeExpr())
447 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
450 VisitArrayTypeLoc(TL);
451}
452void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
453 VisitArrayTypeLoc(TL);
454}
455void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
456 VisitArrayTypeLoc(TL);
457}
458void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
459 DependentSizedArrayTypeLoc TL) {
460 VisitArrayTypeLoc(TL);
461}
462void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
463 DependentSizedExtVectorTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getNameLoc(), Record);
465}
466void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getNameLoc(), Record);
468}
469void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getNameLoc(), Record);
471}
472void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000473 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
474 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000475 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000476 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
477 Writer.AddDeclRef(TL.getArg(i), Record);
478}
479void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
480 VisitFunctionTypeLoc(TL);
481}
482void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
483 VisitFunctionTypeLoc(TL);
484}
John McCalled976492009-12-04 22:46:56 +0000485void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
487}
John McCall51bd8032009-10-18 01:05:36 +0000488void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
490}
491void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000492 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
493 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
494 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000495}
496void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000497 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
498 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
499 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
500 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000501}
502void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
503 Writer.AddSourceLocation(TL.getNameLoc(), Record);
504}
Sean Huntca63c202011-05-24 22:41:36 +0000505void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getKWLoc(), Record);
507 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
508 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
509 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
510}
Richard Smith34b41d92011-02-20 03:19:35 +0000511void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
513}
John McCall51bd8032009-10-18 01:05:36 +0000514void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getNameLoc(), Record);
516}
517void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
518 Writer.AddSourceLocation(TL.getNameLoc(), Record);
519}
John McCall9d156a72011-01-06 01:58:22 +0000520void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
522 if (TL.hasAttrOperand()) {
523 SourceRange range = TL.getAttrOperandParensRange();
524 Writer.AddSourceLocation(range.getBegin(), Record);
525 Writer.AddSourceLocation(range.getEnd(), Record);
526 }
527 if (TL.hasAttrExprOperand()) {
528 Expr *operand = TL.getAttrExprOperand();
529 Record.push_back(operand ? 1 : 0);
530 if (operand) Writer.AddStmt(operand);
531 } else if (TL.hasAttrEnumOperand()) {
532 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
533 }
534}
John McCall51bd8032009-10-18 01:05:36 +0000535void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getNameLoc(), Record);
537}
John McCall49a832b2009-10-18 09:09:24 +0000538void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
539 SubstTemplateTypeParmTypeLoc TL) {
540 Writer.AddSourceLocation(TL.getNameLoc(), Record);
541}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000542void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
543 SubstTemplateTypeParmPackTypeLoc TL) {
544 Writer.AddSourceLocation(TL.getNameLoc(), Record);
545}
John McCall51bd8032009-10-18 01:05:36 +0000546void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
547 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000548 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
549 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
550 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
551 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000552 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
553 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000554}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000555void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
557 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
558}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000559void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000560 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000561 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000562}
John McCall3cb0ebd2010-03-10 03:28:59 +0000563void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
564 Writer.AddSourceLocation(TL.getNameLoc(), Record);
565}
Douglas Gregor4714c122010-03-31 17:34:00 +0000566void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000567 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000568 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000569 Writer.AddSourceLocation(TL.getNameLoc(), Record);
570}
John McCall33500952010-06-11 00:33:02 +0000571void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
572 DependentTemplateSpecializationTypeLoc TL) {
573 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000574 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000575 Writer.AddSourceLocation(TL.getNameLoc(), Record);
576 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
577 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
578 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000579 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
580 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000581}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000582void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
584}
John McCall51bd8032009-10-18 01:05:36 +0000585void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
586 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000587}
588void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
589 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000590 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
591 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
592 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
593 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000594}
John McCall54e14c42009-10-22 22:37:11 +0000595void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
596 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000597}
John McCalla1ee0c52009-10-16 21:56:05 +0000598
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000599//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000600// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000601//===----------------------------------------------------------------------===//
602
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000603static void EmitBlockID(unsigned ID, const char *Name,
604 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000605 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000606 Record.clear();
607 Record.push_back(ID);
608 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
609
610 // Emit the block name if present.
611 if (Name == 0 || Name[0] == 0) return;
612 Record.clear();
613 while (*Name)
614 Record.push_back(*Name++);
615 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
616}
617
618static void EmitRecordID(unsigned ID, const char *Name,
619 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000620 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000621 Record.clear();
622 Record.push_back(ID);
623 while (*Name)
624 Record.push_back(*Name++);
625 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000626}
627
628static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000629 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000630#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000631 RECORD(STMT_STOP);
632 RECORD(STMT_NULL_PTR);
633 RECORD(STMT_NULL);
634 RECORD(STMT_COMPOUND);
635 RECORD(STMT_CASE);
636 RECORD(STMT_DEFAULT);
637 RECORD(STMT_LABEL);
638 RECORD(STMT_IF);
639 RECORD(STMT_SWITCH);
640 RECORD(STMT_WHILE);
641 RECORD(STMT_DO);
642 RECORD(STMT_FOR);
643 RECORD(STMT_GOTO);
644 RECORD(STMT_INDIRECT_GOTO);
645 RECORD(STMT_CONTINUE);
646 RECORD(STMT_BREAK);
647 RECORD(STMT_RETURN);
648 RECORD(STMT_DECL);
649 RECORD(STMT_ASM);
650 RECORD(EXPR_PREDEFINED);
651 RECORD(EXPR_DECL_REF);
652 RECORD(EXPR_INTEGER_LITERAL);
653 RECORD(EXPR_FLOATING_LITERAL);
654 RECORD(EXPR_IMAGINARY_LITERAL);
655 RECORD(EXPR_STRING_LITERAL);
656 RECORD(EXPR_CHARACTER_LITERAL);
657 RECORD(EXPR_PAREN);
658 RECORD(EXPR_UNARY_OPERATOR);
659 RECORD(EXPR_SIZEOF_ALIGN_OF);
660 RECORD(EXPR_ARRAY_SUBSCRIPT);
661 RECORD(EXPR_CALL);
662 RECORD(EXPR_MEMBER);
663 RECORD(EXPR_BINARY_OPERATOR);
664 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
665 RECORD(EXPR_CONDITIONAL_OPERATOR);
666 RECORD(EXPR_IMPLICIT_CAST);
667 RECORD(EXPR_CSTYLE_CAST);
668 RECORD(EXPR_COMPOUND_LITERAL);
669 RECORD(EXPR_EXT_VECTOR_ELEMENT);
670 RECORD(EXPR_INIT_LIST);
671 RECORD(EXPR_DESIGNATED_INIT);
672 RECORD(EXPR_IMPLICIT_VALUE_INIT);
673 RECORD(EXPR_VA_ARG);
674 RECORD(EXPR_ADDR_LABEL);
675 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000676 RECORD(EXPR_CHOOSE);
677 RECORD(EXPR_GNU_NULL);
678 RECORD(EXPR_SHUFFLE_VECTOR);
679 RECORD(EXPR_BLOCK);
680 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000681 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000682 RECORD(EXPR_OBJC_STRING_LITERAL);
683 RECORD(EXPR_OBJC_ENCODE);
684 RECORD(EXPR_OBJC_SELECTOR_EXPR);
685 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
686 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
687 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
688 RECORD(EXPR_OBJC_KVC_REF_EXPR);
689 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000690 RECORD(STMT_OBJC_FOR_COLLECTION);
691 RECORD(STMT_OBJC_CATCH);
692 RECORD(STMT_OBJC_FINALLY);
693 RECORD(STMT_OBJC_AT_TRY);
694 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
695 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000696 RECORD(EXPR_CXX_OPERATOR_CALL);
697 RECORD(EXPR_CXX_CONSTRUCT);
698 RECORD(EXPR_CXX_STATIC_CAST);
699 RECORD(EXPR_CXX_DYNAMIC_CAST);
700 RECORD(EXPR_CXX_REINTERPRET_CAST);
701 RECORD(EXPR_CXX_CONST_CAST);
702 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
703 RECORD(EXPR_CXX_BOOL_LITERAL);
704 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000705 RECORD(EXPR_CXX_TYPEID_EXPR);
706 RECORD(EXPR_CXX_TYPEID_TYPE);
707 RECORD(EXPR_CXX_UUIDOF_EXPR);
708 RECORD(EXPR_CXX_UUIDOF_TYPE);
709 RECORD(EXPR_CXX_THIS);
710 RECORD(EXPR_CXX_THROW);
711 RECORD(EXPR_CXX_DEFAULT_ARG);
712 RECORD(EXPR_CXX_BIND_TEMPORARY);
713 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
714 RECORD(EXPR_CXX_NEW);
715 RECORD(EXPR_CXX_DELETE);
716 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
717 RECORD(EXPR_EXPR_WITH_CLEANUPS);
718 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
719 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
720 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
721 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
722 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
723 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
724 RECORD(EXPR_CXX_NOEXCEPT);
725 RECORD(EXPR_OPAQUE_VALUE);
726 RECORD(EXPR_BINARY_TYPE_TRAIT);
727 RECORD(EXPR_PACK_EXPANSION);
728 RECORD(EXPR_SIZEOF_PACK);
729 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000730 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000731#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000732}
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Sebastian Redla4232eb2010-08-18 23:56:21 +0000734void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000735 RecordData Record;
736 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000738#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
739#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Sebastian Redl3397c552010-08-18 23:56:27 +0000741 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000742 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000743 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000744 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000745 RECORD(TYPE_OFFSET);
746 RECORD(DECL_OFFSET);
747 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000748 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000749 RECORD(IDENTIFIER_OFFSET);
750 RECORD(IDENTIFIER_TABLE);
751 RECORD(EXTERNAL_DEFINITIONS);
752 RECORD(SPECIAL_TYPES);
753 RECORD(STATISTICS);
754 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000755 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000756 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
757 RECORD(SELECTOR_OFFSETS);
758 RECORD(METHOD_POOL);
759 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000760 RECORD(SOURCE_LOCATION_OFFSETS);
761 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000762 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000763 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000764 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000765 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000766 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000767 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000768 RECORD(TU_UPDATE_LEXICAL);
769 RECORD(REDECLS_UPDATE_LATEST);
770 RECORD(SEMA_DECL_REFS);
771 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
772 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
773 RECORD(DECL_REPLACEMENTS);
774 RECORD(UPDATE_VISIBLE);
775 RECORD(DECL_UPDATE_OFFSETS);
776 RECORD(DECL_UPDATES);
777 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
778 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000779 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000780 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000781 RECORD(FP_PRAGMA_OPTIONS);
782 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000783 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000784 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
785 RECORD(KNOWN_NAMESPACES);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000786
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000787 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000788 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000789 RECORD(SM_SLOC_FILE_ENTRY);
790 RECORD(SM_SLOC_BUFFER_ENTRY);
791 RECORD(SM_SLOC_BUFFER_BLOB);
792 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
793 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000795 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000796 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000797 RECORD(PP_MACRO_OBJECT_LIKE);
798 RECORD(PP_MACRO_FUNCTION_LIKE);
799 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000800
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000801 // Decls and Types block.
802 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000803 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000804 RECORD(TYPE_COMPLEX);
805 RECORD(TYPE_POINTER);
806 RECORD(TYPE_BLOCK_POINTER);
807 RECORD(TYPE_LVALUE_REFERENCE);
808 RECORD(TYPE_RVALUE_REFERENCE);
809 RECORD(TYPE_MEMBER_POINTER);
810 RECORD(TYPE_CONSTANT_ARRAY);
811 RECORD(TYPE_INCOMPLETE_ARRAY);
812 RECORD(TYPE_VARIABLE_ARRAY);
813 RECORD(TYPE_VECTOR);
814 RECORD(TYPE_EXT_VECTOR);
815 RECORD(TYPE_FUNCTION_PROTO);
816 RECORD(TYPE_FUNCTION_NO_PROTO);
817 RECORD(TYPE_TYPEDEF);
818 RECORD(TYPE_TYPEOF_EXPR);
819 RECORD(TYPE_TYPEOF);
820 RECORD(TYPE_RECORD);
821 RECORD(TYPE_ENUM);
822 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000823 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000824 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000825 RECORD(TYPE_DECLTYPE);
826 RECORD(TYPE_ELABORATED);
827 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
828 RECORD(TYPE_UNRESOLVED_USING);
829 RECORD(TYPE_INJECTED_CLASS_NAME);
830 RECORD(TYPE_OBJC_OBJECT);
831 RECORD(TYPE_TEMPLATE_TYPE_PARM);
832 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
833 RECORD(TYPE_DEPENDENT_NAME);
834 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
835 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
836 RECORD(TYPE_PAREN);
837 RECORD(TYPE_PACK_EXPANSION);
838 RECORD(TYPE_ATTRIBUTED);
839 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000840 RECORD(DECL_TRANSLATION_UNIT);
841 RECORD(DECL_TYPEDEF);
842 RECORD(DECL_ENUM);
843 RECORD(DECL_RECORD);
844 RECORD(DECL_ENUM_CONSTANT);
845 RECORD(DECL_FUNCTION);
846 RECORD(DECL_OBJC_METHOD);
847 RECORD(DECL_OBJC_INTERFACE);
848 RECORD(DECL_OBJC_PROTOCOL);
849 RECORD(DECL_OBJC_IVAR);
850 RECORD(DECL_OBJC_AT_DEFS_FIELD);
851 RECORD(DECL_OBJC_CLASS);
852 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
853 RECORD(DECL_OBJC_CATEGORY);
854 RECORD(DECL_OBJC_CATEGORY_IMPL);
855 RECORD(DECL_OBJC_IMPLEMENTATION);
856 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
857 RECORD(DECL_OBJC_PROPERTY);
858 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000859 RECORD(DECL_FIELD);
860 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000861 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000862 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000863 RECORD(DECL_FILE_SCOPE_ASM);
864 RECORD(DECL_BLOCK);
865 RECORD(DECL_CONTEXT_LEXICAL);
866 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000867 RECORD(DECL_NAMESPACE);
868 RECORD(DECL_NAMESPACE_ALIAS);
869 RECORD(DECL_USING);
870 RECORD(DECL_USING_SHADOW);
871 RECORD(DECL_USING_DIRECTIVE);
872 RECORD(DECL_UNRESOLVED_USING_VALUE);
873 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
874 RECORD(DECL_LINKAGE_SPEC);
875 RECORD(DECL_CXX_RECORD);
876 RECORD(DECL_CXX_METHOD);
877 RECORD(DECL_CXX_CONSTRUCTOR);
878 RECORD(DECL_CXX_DESTRUCTOR);
879 RECORD(DECL_CXX_CONVERSION);
880 RECORD(DECL_ACCESS_SPEC);
881 RECORD(DECL_FRIEND);
882 RECORD(DECL_FRIEND_TEMPLATE);
883 RECORD(DECL_CLASS_TEMPLATE);
884 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
885 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
886 RECORD(DECL_FUNCTION_TEMPLATE);
887 RECORD(DECL_TEMPLATE_TYPE_PARM);
888 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
889 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
890 RECORD(DECL_STATIC_ASSERT);
891 RECORD(DECL_CXX_BASE_SPECIFIERS);
892 RECORD(DECL_INDIRECTFIELD);
893 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
894
Douglas Gregora72d8c42011-06-03 02:27:19 +0000895 // Statements and Exprs can occur in the Decls and Types block.
896 AddStmtsExprs(Stream, Record);
897
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000898 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
899 RECORD(PPD_MACRO_INSTANTIATION);
900 RECORD(PPD_MACRO_DEFINITION);
901 RECORD(PPD_INCLUSION_DIRECTIVE);
902
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000903#undef RECORD
904#undef BLOCK
905 Stream.ExitBlock();
906}
907
Douglas Gregore650c8c2009-07-07 00:12:59 +0000908/// \brief Adjusts the given filename to only write out the portion of the
909/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000910///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000911/// \param Filename the file name to adjust.
912///
913/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
914/// the returned filename will be adjusted by this system root.
915///
916/// \returns either the original filename (if it needs no adjustment) or the
917/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000918static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000919adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
920 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Douglas Gregore650c8c2009-07-07 00:12:59 +0000922 if (!isysroot)
923 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Douglas Gregore650c8c2009-07-07 00:12:59 +0000925 // Verify that the filename and the system root have the same prefix.
926 unsigned Pos = 0;
927 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
928 if (Filename[Pos] != isysroot[Pos])
929 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Douglas Gregore650c8c2009-07-07 00:12:59 +0000931 // We hit the end of the filename before we hit the end of the system root.
932 if (!Filename[Pos])
933 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Douglas Gregore650c8c2009-07-07 00:12:59 +0000935 // If the file name has a '/' at the current position, skip over the '/'.
936 // We distinguish sysroot-based includes from absolute includes by the
937 // absence of '/' at the beginning of sysroot-based includes.
938 if (Filename[Pos] == '/')
939 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Douglas Gregore650c8c2009-07-07 00:12:59 +0000941 return Filename + Pos;
942}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000943
Sebastian Redl3397c552010-08-18 23:56:27 +0000944/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000945void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
946 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000947 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000948
Douglas Gregore650c8c2009-07-07 00:12:59 +0000949 // Metadata
950 const TargetInfo &Target = Context.Target;
951 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000952 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000953 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000954 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
955 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000956 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
957 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000959 // Target triple or chained PCH name
960 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregore650c8c2009-07-07 00:12:59 +0000963 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000964 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
965 Record.push_back(VERSION_MAJOR);
966 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 Record.push_back(CLANG_VERSION_MAJOR);
968 Record.push_back(CLANG_VERSION_MINOR);
969 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000970 // FIXME: This writes the absolute path for chained headers.
971 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
972 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Douglas Gregor31d375f2011-05-06 21:43:30 +0000974 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +0000975 SourceManager &SM = Context.getSourceManager();
976 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
977 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000978 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000979 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
980 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
981
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000982 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000984 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000985
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000986 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000987 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000988 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000989 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000990 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000991 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000992
993 Record.clear();
994 Record.push_back(SM.getMainFileID().getOpaqueValue());
995 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000996 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000997
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000998 // Original PCH directory
999 if (!OutputFile.empty() && OutputFile != "-") {
1000 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1001 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1003 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1004
1005 llvm::SmallString<128> OutputPath(OutputFile);
1006
1007 llvm::sys::fs::make_absolute(OutputPath);
1008 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1009
1010 RecordData Record;
1011 Record.push_back(ORIGINAL_PCH_DIR);
1012 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1013 }
1014
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001015 // Repository branch/version information.
1016 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001017 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001018 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1019 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001020 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001021 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001022 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1023 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001024}
1025
1026/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001027void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001028 RecordData Record;
1029 Record.push_back(LangOpts.Trigraphs);
1030 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1031 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1032 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1033 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001034 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001035 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1036 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1037 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1038 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00001039 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001040 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001041 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1042 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001043 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1044 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001045 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001047 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1048 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001049 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001050 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001051 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001052 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001053 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001054 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1055 // properties enabled.
Douglas Gregor74da19f2011-06-14 23:20:43 +00001056 Record.push_back(LangOpts.ObjCInferRelatedResultType);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001057 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001059 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001060 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1061 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001062 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001063 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001064 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001065 Record.push_back(LangOpts.CXXExceptions);
1066 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001067
Douglas Gregor6f755502011-02-01 15:15:22 +00001068 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001069 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1070 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1071 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1072
Chris Lattnerea5ce472009-04-27 07:35:58 +00001073 // Whether static initializers are protected by locks.
1074 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001075 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001076 Record.push_back(LangOpts.Blocks); // block extension to C
1077 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1078 // they are unused.
1079 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1080 // (modulo the platform support).
1081
Chris Lattnera4d71452010-06-26 21:25:03 +00001082 Record.push_back(LangOpts.getSignedOverflowBehavior());
1083 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001084
1085 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001086 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001087 // defined.
1088 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1089 // opposed to __DYNAMIC__).
1090 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1091
1092 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1093 // used (instead of C99 semantics).
1094 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00001095 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001096 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1097 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001098 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1099 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001100 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001101 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1102 // to the smallest integer type with
1103 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001104 Record.push_back(LangOpts.getGCMode());
1105 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001106 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001107 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001108 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001109 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001110 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001111 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001112 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001113 Record.push_back(LangOpts.SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00001114 Record.push_back(LangOpts.MRTD);
John McCallf85e1932011-06-15 23:02:42 +00001115 Record.push_back(LangOpts.ObjCAutoRefCount);
1116 Record.push_back(LangOpts.ObjCInferRelatedReturnType);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001117 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001118}
1119
Douglas Gregor14f79002009-04-10 03:52:48 +00001120//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001121// stat cache Serialization
1122//===----------------------------------------------------------------------===//
1123
1124namespace {
1125// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001126class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001127public:
1128 typedef const char * key_type;
1129 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Chris Lattner74e976b2010-11-23 19:28:12 +00001131 typedef struct stat data_type;
1132 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001133
1134 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001135 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001136 }
Mike Stump1eb44332009-09-09 15:08:12 +00001137
1138 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001139 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1140 data_type_ref Data) {
1141 unsigned StrLen = strlen(path);
1142 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001143 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001144 clang::io::Emit8(Out, DataLen);
1145 return std::make_pair(StrLen + 1, DataLen);
1146 }
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001148 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1149 Out.write(path, KeyLen);
1150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Chris Lattner74e976b2010-11-23 19:28:12 +00001152 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001153 data_type_ref Data, unsigned DataLen) {
1154 using namespace clang::io;
1155 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattner74e976b2010-11-23 19:28:12 +00001157 Emit32(Out, (uint32_t) Data.st_ino);
1158 Emit32(Out, (uint32_t) Data.st_dev);
1159 Emit16(Out, (uint16_t) Data.st_mode);
1160 Emit64(Out, (uint64_t) Data.st_mtime);
1161 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001162
1163 assert(Out.tell() - Start == DataLen && "Wrong data length");
1164 }
1165};
1166} // end anonymous namespace
1167
Sebastian Redl3397c552010-08-18 23:56:27 +00001168/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001169void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001170 // Build the on-disk hash table containing information about every
1171 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001172 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001173 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001174 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001175 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001176 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1177 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001178 Generator.insert(Filename, Stat->second);
1179 }
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001181 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001182 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001183 uint32_t BucketOffset;
1184 {
1185 llvm::raw_svector_ostream Out(StatCacheData);
1186 // Make sure that no bucket is at offset 0
1187 clang::io::Emit32(Out, 0);
1188 BucketOffset = Generator.Emit(Out);
1189 }
1190
1191 // Create a blob abbreviation
1192 using namespace llvm;
1193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001194 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1198 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1199
1200 // Write the stat cache
1201 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001202 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001203 Record.push_back(BucketOffset);
1204 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001205 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001206}
1207
1208//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001209// Source Manager Serialization
1210//===----------------------------------------------------------------------===//
1211
1212/// \brief Create an abbreviation for the SLocEntry that refers to a
1213/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001214static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001215 using namespace llvm;
1216 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001217 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001222 // FileEntry fields.
1223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001226 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001227}
1228
1229/// \brief Create an abbreviation for the SLocEntry that refers to a
1230/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001231static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001232 using namespace llvm;
1233 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001234 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001240 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001241}
1242
1243/// \brief Create an abbreviation for the SLocEntry that refers to a
1244/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001245static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001246 using namespace llvm;
1247 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001248 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001250 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001251}
1252
1253/// \brief Create an abbreviation for the SLocEntry that refers to an
1254/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001255static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001256 using namespace llvm;
1257 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001258 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001264 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001265}
1266
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001267namespace {
1268 // Trait used for the on-disk hash table of header search information.
1269 class HeaderFileInfoTrait {
1270 ASTWriter &Writer;
1271 HeaderSearch &HS;
1272
1273 public:
1274 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1275 : Writer(Writer), HS(HS) { }
1276
1277 typedef const char *key_type;
1278 typedef key_type key_type_ref;
1279
1280 typedef HeaderFileInfo data_type;
1281 typedef const data_type &data_type_ref;
1282
1283 static unsigned ComputeHash(const char *path) {
1284 // The hash is based only on the filename portion of the key, so that the
1285 // reader can match based on filenames when symlinking or excess path
1286 // elements ("foo/../", "../") change the form of the name. However,
1287 // complete path is still the key.
1288 return llvm::HashString(llvm::sys::path::filename(path));
1289 }
1290
1291 std::pair<unsigned,unsigned>
1292 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1293 data_type_ref Data) {
1294 unsigned StrLen = strlen(path);
1295 clang::io::Emit16(Out, StrLen);
1296 unsigned DataLen = 1 + 2 + 4;
1297 clang::io::Emit8(Out, DataLen);
1298 return std::make_pair(StrLen + 1, DataLen);
1299 }
1300
1301 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1302 Out.write(path, KeyLen);
1303 }
1304
1305 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1306 data_type_ref Data, unsigned DataLen) {
1307 using namespace clang::io;
1308 uint64_t Start = Out.tell(); (void)Start;
1309
Douglas Gregordd3e5542011-05-04 00:14:37 +00001310 unsigned char Flags = (Data.isImport << 4)
1311 | (Data.isPragmaOnce << 3)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001312 | (Data.DirInfo << 1)
1313 | Data.Resolved;
1314 Emit8(Out, (uint8_t)Flags);
1315 Emit16(Out, (uint16_t) Data.NumIncludes);
1316
1317 if (!Data.ControllingMacro)
1318 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1319 else
1320 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1321 assert(Out.tell() - Start == DataLen && "Wrong data length");
1322 }
1323 };
1324} // end anonymous namespace
1325
1326/// \brief Write the header search block for the list of files that
1327///
1328/// \param HS The header search structure to save.
1329///
1330/// \param Chain Whether we're creating a chained AST file.
1331void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1332 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1333 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1334
1335 if (FilesByUID.size() > HS.header_file_size())
1336 FilesByUID.resize(HS.header_file_size());
1337
1338 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1339 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1340 llvm::SmallVector<const char *, 4> SavedStrings;
1341 unsigned NumHeaderSearchEntries = 0;
1342 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1343 const FileEntry *File = FilesByUID[UID];
1344 if (!File)
1345 continue;
1346
1347 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1348 if (HFI.External && Chain)
1349 continue;
1350
1351 // Turn the file name into an absolute path, if it isn't already.
1352 const char *Filename = File->getName();
1353 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1354
1355 // If we performed any translation on the file name at all, we need to
1356 // save this string, since the generator will refer to it later.
1357 if (Filename != File->getName()) {
1358 Filename = strdup(Filename);
1359 SavedStrings.push_back(Filename);
1360 }
1361
1362 Generator.insert(Filename, HFI, GeneratorTrait);
1363 ++NumHeaderSearchEntries;
1364 }
1365
1366 // Create the on-disk hash table in a buffer.
1367 llvm::SmallString<4096> TableData;
1368 uint32_t BucketOffset;
1369 {
1370 llvm::raw_svector_ostream Out(TableData);
1371 // Make sure that no bucket is at offset 0
1372 clang::io::Emit32(Out, 0);
1373 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1374 }
1375
1376 // Create a blob abbreviation
1377 using namespace llvm;
1378 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1379 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1383 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1384
1385 // Write the stat cache
1386 RecordData Record;
1387 Record.push_back(HEADER_SEARCH_TABLE);
1388 Record.push_back(BucketOffset);
1389 Record.push_back(NumHeaderSearchEntries);
1390 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1391
1392 // Free all of the strings we had to duplicate.
1393 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1394 free((void*)SavedStrings[I]);
1395}
1396
Douglas Gregor14f79002009-04-10 03:52:48 +00001397/// \brief Writes the block containing the serialized form of the
1398/// source manager.
1399///
1400/// TODO: We should probably use an on-disk hash table (stored in a
1401/// blob), indexed based on the file name, so that we only create
1402/// entries for files that we actually need. In the common case (no
1403/// errors), we probably won't have to create file entries for any of
1404/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001405void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001406 const Preprocessor &PP,
1407 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001408 RecordData Record;
1409
Chris Lattnerf04ad692009-04-10 17:16:57 +00001410 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001411 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001412
1413 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001414 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1415 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1416 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1417 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001418
Douglas Gregorbd945002009-04-13 16:31:14 +00001419 // Write the line table.
1420 if (SourceMgr.hasLineTable()) {
1421 LineTableInfo &LineTable = SourceMgr.getLineTable();
1422
1423 // Emit the file names
1424 Record.push_back(LineTable.getNumFilenames());
1425 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1426 // Emit the file name
1427 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001428 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001429 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1430 Record.push_back(FilenameLen);
1431 if (FilenameLen)
1432 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Douglas Gregorbd945002009-04-13 16:31:14 +00001435 // Emit the line entries
1436 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1437 L != LEnd; ++L) {
1438 // Emit the file ID
1439 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregorbd945002009-04-13 16:31:14 +00001441 // Emit the line entries
1442 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001443 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001444 LEEnd = L->second.end();
1445 LE != LEEnd; ++LE) {
1446 Record.push_back(LE->FileOffset);
1447 Record.push_back(LE->LineNo);
1448 Record.push_back(LE->FilenameID);
1449 Record.push_back((unsigned)LE->FileKind);
1450 Record.push_back(LE->IncludeOffset);
1451 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001452 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001453 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001454 }
1455
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001456 // Write out the source location entry table. We skip the first
1457 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001458 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001459 // Write out the offsets of only source location file entries.
1460 // We will go through them in ASTReader::validateFileEntries().
1461 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001462 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001463 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1464 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1465 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1466 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001467 // Get this source location entry.
1468 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001469
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001470 // Record the offset of this source-location entry.
1471 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1472
1473 // Figure out which record code to use.
1474 unsigned Code;
1475 if (SLoc->isFile()) {
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001476 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001477 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001478 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1479 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001480 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001481 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001482 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001483 Record.clear();
1484 Record.push_back(Code);
1485
1486 Record.push_back(SLoc->getOffset());
1487 if (SLoc->isFile()) {
1488 const SrcMgr::FileInfo &File = SLoc->getFile();
1489 Record.push_back(File.getIncludeLoc().getRawEncoding());
1490 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1491 Record.push_back(File.hasLineDirectives());
1492
1493 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001494 if (Content->OrigEntry) {
1495 assert(Content->OrigEntry == Content->ContentsEntry &&
1496 "Writing to AST an overriden file is not supported");
1497
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001498 // The source location entry is a file. The blob associated
1499 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Douglas Gregor2d52be52010-03-21 22:49:54 +00001501 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001502 Record.push_back(Content->OrigEntry->getSize());
1503 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001504
Douglas Gregore650c8c2009-07-07 00:12:59 +00001505 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001506 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001507 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001508
1509 // Ask the file manager to fixup the relative path for us. This will
1510 // honor the working directory.
1511 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1512
1513 // FIXME: This call to make_absolute shouldn't be necessary, the
1514 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001515 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001516 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregore650c8c2009-07-07 00:12:59 +00001518 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001519 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001520 } else {
1521 // The source location entry is a buffer. The blob associated
1522 // with this entry contains the contents of the buffer.
1523
1524 // We add one to the size so that we capture the trailing NULL
1525 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1526 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001527 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001528 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001529 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001530 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1531 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001533 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001534 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001535 llvm::StringRef(Buffer->getBufferStart(),
1536 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001537
1538 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001539 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001540 }
1541 } else {
1542 // The source location entry is an instantiation.
1543 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1544 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1545 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1546 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1547
1548 // Compute the token length for this macro expansion.
1549 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001550 if (I + 1 != N)
1551 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1553 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1554 }
1555 }
1556
Douglas Gregorc9490c02009-04-16 22:23:12 +00001557 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001558
1559 if (SLocEntryOffsets.empty())
1560 return;
1561
Sebastian Redl3397c552010-08-18 23:56:27 +00001562 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001563 // table is used for lazily loading source-location information.
1564 using namespace llvm;
1565 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001566 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1570 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001572 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001573 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001574 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001575 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1576 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001577 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001578
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001579 Abbrev = new BitCodeAbbrev();
1580 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1583 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1584
1585 Record.clear();
1586 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1587 Record.push_back(SLocFileEntryOffsets.size());
1588 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1589 data(SLocFileEntryOffsets));
1590
Sebastian Redl3397c552010-08-18 23:56:27 +00001591 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001592 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001593 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001594}
1595
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001596//===----------------------------------------------------------------------===//
1597// Preprocessor Serialization
1598//===----------------------------------------------------------------------===//
1599
Douglas Gregor9c736102011-02-10 18:20:09 +00001600static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1601 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1602 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1603 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1604 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1605 return X.first->getName().compare(Y.first->getName());
1606}
1607
Chris Lattner0b1fb982009-04-10 17:15:23 +00001608/// \brief Writes the block containing the serialized form of the
1609/// preprocessor.
1610///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001611void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001612 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001613
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001614 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1615 if (PP.getCounterValue() != 0) {
1616 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001617 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001618 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001619 }
1620
1621 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001622 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Sebastian Redl3397c552010-08-18 23:56:27 +00001624 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001625 // FIXME: use diagnostics subsystem for localization etc.
1626 if (PP.SawDateOrTime())
1627 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Douglas Gregorecdcb882010-10-20 22:00:55 +00001629
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001630 // Loop over all the macro definitions that are live at the end of the file,
1631 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001632 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001633
Douglas Gregor9c736102011-02-10 18:20:09 +00001634 // Construct the list of macro definitions that need to be serialized.
1635 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1636 MacrosToEmit;
1637 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001638 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1639 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001640 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001641 MacroDefinitionsSeen.insert(I->first);
1642 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1643 }
1644
1645 // Sort the set of macro definitions that need to be serialized by the
1646 // name of the macro, to provide a stable ordering.
1647 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1648 &compareMacroDefinitions);
1649
Douglas Gregor040a8042011-02-11 00:26:14 +00001650 // Resolve any identifiers that defined macros at the time they were
1651 // deserialized, adding them to the list of macros to emit (if appropriate).
1652 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1653 IdentifierInfo *Name
1654 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1655 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1656 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1657 }
1658
Douglas Gregor9c736102011-02-10 18:20:09 +00001659 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1660 const IdentifierInfo *Name = MacrosToEmit[I].first;
1661 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001662 if (!MI)
1663 continue;
1664
Sebastian Redl3397c552010-08-18 23:56:27 +00001665 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001666 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001667 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001668
1669 // FIXME: There is a (probably minor) optimization we could do here, if
1670 // the macro comes from the original PCH but the identifier comes from a
1671 // chained PCH, by storing the offset into the original PCH rather than
1672 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001673 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001674 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001675 continue;
1676
Douglas Gregor9c736102011-02-10 18:20:09 +00001677 AddIdentifierRef(Name, Record);
1678 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001679 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1680 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001682 unsigned Code;
1683 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001684 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001685 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001686 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001688 Record.push_back(MI->isC99Varargs());
1689 Record.push_back(MI->isGNUVarargs());
1690 Record.push_back(MI->getNumArgs());
1691 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1692 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001693 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001694 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001695
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001696 // If we have a detailed preprocessing record, record the macro definition
1697 // ID that corresponds to this macro.
1698 if (PPRec)
1699 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001700
Douglas Gregorc9490c02009-04-16 22:23:12 +00001701 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001702 Record.clear();
1703
Chris Lattnerdf961c22009-04-10 18:08:30 +00001704 // Emit the tokens array.
1705 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1706 // Note that we know that the preprocessor does not have any annotation
1707 // tokens in it because they are created by the parser, and thus can't be
1708 // in a macro definition.
1709 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Chris Lattnerdf961c22009-04-10 18:08:30 +00001711 Record.push_back(Tok.getLocation().getRawEncoding());
1712 Record.push_back(Tok.getLength());
1713
Chris Lattnerdf961c22009-04-10 18:08:30 +00001714 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1715 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001716 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001717 // FIXME: Should translate token kind to a stable encoding.
1718 Record.push_back(Tok.getKind());
1719 // FIXME: Should translate token flags to a stable encoding.
1720 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001722 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001723 Record.clear();
1724 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001725 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001726 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001727 Stream.ExitBlock();
1728
1729 if (PPRec)
1730 WritePreprocessorDetail(*PPRec);
1731}
1732
1733void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1734 if (PPRec.begin(Chain) == PPRec.end(Chain))
1735 return;
1736
1737 // Enter the preprocessor block.
1738 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001739
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001740 // If the preprocessor has a preprocessing record, emit it.
1741 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001742 using namespace llvm;
1743
1744 // Set up the abbreviation for
1745 unsigned InclusionAbbrev = 0;
1746 {
1747 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1748 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1753 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1754 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1755 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1756 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1757 }
1758
1759 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1760 RecordData Record;
1761 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1762 EEnd = PPRec.end(Chain);
1763 E != EEnd; ++E) {
1764 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001765
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001766 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1767 // Record this macro definition's location.
1768 MacroID ID = getMacroDefinitionID(MD);
1769
1770 // Don't write the macro definition if it is from another AST file.
1771 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001772 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001773
Douglas Gregor89d99802010-11-30 06:16:57 +00001774 // Notify the serialization listener that we're serializing this entity.
1775 if (SerializationListener)
1776 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001777 Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001778
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001779 unsigned Position = ID - FirstMacroID;
1780 if (Position != MacroDefinitionOffsets.size()) {
1781 if (Position > MacroDefinitionOffsets.size())
1782 MacroDefinitionOffsets.resize(Position + 1);
1783
1784 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1785 } else
1786 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001787
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001788 Record.push_back(IndexBase + NumPreprocessingRecords++);
1789 Record.push_back(ID);
1790 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1791 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1792 AddIdentifierRef(MD->getName(), Record);
1793 AddSourceLocation(MD->getLocation(), Record);
1794 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1795 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001796 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001797
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001798 // Notify the serialization listener that we're serializing this entity.
1799 if (SerializationListener)
1800 SerializationListener->SerializedPreprocessedEntity(*E,
1801 Stream.GetCurrentBitNo());
1802
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001803 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001804 Record.push_back(IndexBase + NumPreprocessingRecords++);
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001805 AddSourceLocation(ME->getSourceRange().getBegin(), Record);
1806 AddSourceLocation(ME->getSourceRange().getEnd(), Record);
1807 AddIdentifierRef(ME->getName(), Record);
1808 Record.push_back(getMacroDefinitionID(ME->getDefinition()));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001809 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1810 continue;
1811 }
1812
1813 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1814 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1815 Record.push_back(IndexBase + NumPreprocessingRecords++);
1816 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1817 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1818 Record.push_back(ID->getFileName().size());
1819 Record.push_back(ID->wasInQuotes());
1820 Record.push_back(static_cast<unsigned>(ID->getKind()));
1821 llvm::SmallString<64> Buffer;
1822 Buffer += ID->getFileName();
1823 Buffer += ID->getFile()->getName();
1824 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1825 continue;
1826 }
1827
1828 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1829 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001830 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001831
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001832 // Write the offsets table for the preprocessing record.
1833 if (NumPreprocessingRecords > 0) {
1834 // Write the offsets table for identifier IDs.
1835 using namespace llvm;
1836 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001837 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1841 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001842
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001843 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001844 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001845 Record.push_back(NumPreprocessingRecords);
1846 Record.push_back(MacroDefinitionOffsets.size());
1847 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001848 data(MacroDefinitionOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001849 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001850}
1851
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001852void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001853 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001854 for (Diagnostic::DiagStatePointsTy::const_iterator
1855 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1856 I != E; ++I) {
1857 const Diagnostic::DiagStatePoint &point = *I;
1858 if (point.Loc.isInvalid())
1859 continue;
1860
1861 Record.push_back(point.Loc.getRawEncoding());
1862 for (Diagnostic::DiagState::iterator
1863 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1864 unsigned diag = I->first, map = I->second;
1865 if (map & 0x10) { // mapping from a diagnostic pragma.
1866 Record.push_back(diag);
1867 Record.push_back(map & 0x7);
1868 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001869 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001870 Record.push_back(-1); // mark the end of the diag/map pairs for this
1871 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001872 }
1873
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001874 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001875 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001876}
1877
Anders Carlssonc8505782011-03-06 18:41:18 +00001878void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1879 if (CXXBaseSpecifiersOffsets.empty())
1880 return;
1881
1882 RecordData Record;
1883
1884 // Create a blob abbreviation for the C++ base specifiers offsets.
1885 using namespace llvm;
1886
1887 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1888 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1891 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1892
1893 // Write the selector offsets table.
1894 Record.clear();
1895 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1896 Record.push_back(CXXBaseSpecifiersOffsets.size());
1897 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001898 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00001899}
1900
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001901//===----------------------------------------------------------------------===//
1902// Type Serialization
1903//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001904
Sebastian Redl3397c552010-08-18 23:56:27 +00001905/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001906void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001907 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001908 if (Idx.getIndex() == 0) // we haven't seen this type before.
1909 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Douglas Gregor97475832010-10-05 18:37:06 +00001911 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001912
Douglas Gregor2cf26342009-04-09 22:27:44 +00001913 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001914 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001915 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001916 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001917 else if (TypeOffsets.size() < Index) {
1918 TypeOffsets.resize(Index + 1);
1919 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001920 }
1921
1922 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Douglas Gregor2cf26342009-04-09 22:27:44 +00001924 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001925 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001926
Douglas Gregora4923eb2009-11-16 21:35:15 +00001927 if (T.hasLocalNonFastQualifiers()) {
1928 Qualifiers Qs = T.getLocalQualifiers();
1929 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001930 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001931 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001932 } else {
1933 switch (T->getTypeClass()) {
1934 // For all of the concrete, non-dependent types, call the
1935 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001936#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001937 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001938#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001939#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001940 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001941 }
1942
1943 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001944 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001945
1946 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001947 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001948}
1949
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001950//===----------------------------------------------------------------------===//
1951// Declaration Serialization
1952//===----------------------------------------------------------------------===//
1953
Douglas Gregor2cf26342009-04-09 22:27:44 +00001954/// \brief Write the block containing all of the declaration IDs
1955/// lexically declared within the given DeclContext.
1956///
1957/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1958/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001959uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001960 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001961 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001962 return 0;
1963
Douglas Gregorc9490c02009-04-16 22:23:12 +00001964 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001965 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001966 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001967 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001968 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1969 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001970 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001971
Douglas Gregor25123082009-04-22 22:34:57 +00001972 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001973 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001974 return Offset;
1975}
1976
Sebastian Redla4232eb2010-08-18 23:56:21 +00001977void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001978 using namespace llvm;
1979 RecordData Record;
1980
1981 // Write the type offsets array
1982 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001983 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1986 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1987 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001988 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001989 Record.push_back(TypeOffsets.size());
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001990 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001991
1992 // Write the declaration offsets array
1993 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001994 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1997 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1998 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001999 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002000 Record.push_back(DeclOffsets.size());
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002001 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002002}
2003
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002004//===----------------------------------------------------------------------===//
2005// Global Method Pool and Selector Serialization
2006//===----------------------------------------------------------------------===//
2007
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002008namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002009// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002010class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002011 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002012
2013public:
2014 typedef Selector key_type;
2015 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Sebastian Redl5d050072010-08-04 17:20:04 +00002017 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002018 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002019 ObjCMethodList Instance, Factory;
2020 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002021 typedef const data_type& data_type_ref;
2022
Sebastian Redl3397c552010-08-18 23:56:27 +00002023 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002025 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002026 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002027 }
Mike Stump1eb44332009-09-09 15:08:12 +00002028
2029 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002030 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
2031 data_type_ref Methods) {
2032 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2033 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002034 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2035 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002036 Method = Method->Next)
2037 if (Method->Method)
2038 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002039 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002040 Method = Method->Next)
2041 if (Method->Method)
2042 DataLen += 4;
2043 clang::io::Emit16(Out, DataLen);
2044 return std::make_pair(KeyLen, DataLen);
2045 }
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Douglas Gregor83941df2009-04-25 17:48:32 +00002047 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002048 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002049 assert((Start >> 32) == 0 && "Selector key offset too large");
2050 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002051 unsigned N = Sel.getNumArgs();
2052 clang::io::Emit16(Out, N);
2053 if (N == 0)
2054 N = 1;
2055 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002056 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002057 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2058 }
Mike Stump1eb44332009-09-09 15:08:12 +00002059
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002060 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002061 data_type_ref Methods, unsigned DataLen) {
2062 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002063 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002064 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002065 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002066 Method = Method->Next)
2067 if (Method->Method)
2068 ++NumInstanceMethods;
2069
2070 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002071 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002072 Method = Method->Next)
2073 if (Method->Method)
2074 ++NumFactoryMethods;
2075
2076 clang::io::Emit16(Out, NumInstanceMethods);
2077 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002078 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002079 Method = Method->Next)
2080 if (Method->Method)
2081 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002082 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002083 Method = Method->Next)
2084 if (Method->Method)
2085 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002086
2087 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002088 }
2089};
2090} // end anonymous namespace
2091
Sebastian Redl059612d2010-08-03 21:58:15 +00002092/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002093///
2094/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002095/// in an on-disk hash table indexed by the selector. The hash table also
2096/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002097void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002098 using namespace llvm;
2099
Sebastian Redl059612d2010-08-03 21:58:15 +00002100 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002101 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002102 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002103 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002104 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002105 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002106 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002107 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002108
Sebastian Redl059612d2010-08-03 21:58:15 +00002109 // Create the on-disk hash table representation. We walk through every
2110 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002111 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002112 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002113 I = SelectorIDs.begin(), E = SelectorIDs.end();
2114 I != E; ++I) {
2115 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002116 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002117 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002118 I->second,
2119 ObjCMethodList(),
2120 ObjCMethodList()
2121 };
2122 if (F != SemaRef.MethodPool.end()) {
2123 Data.Instance = F->second.first;
2124 Data.Factory = F->second.second;
2125 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002126 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002127 // changed.
2128 if (Chain && I->second < FirstSelectorID) {
2129 // Selector already exists. Did it change?
2130 bool changed = false;
2131 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2132 M = M->Next) {
2133 if (M->Method->getPCHLevel() == 0)
2134 changed = true;
2135 }
2136 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2137 M = M->Next) {
2138 if (M->Method->getPCHLevel() == 0)
2139 changed = true;
2140 }
2141 if (!changed)
2142 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002143 } else if (Data.Instance.Method || Data.Factory.Method) {
2144 // A new method pool entry.
2145 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002146 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002147 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002148 }
2149
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002150 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002151 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002152 uint32_t BucketOffset;
2153 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002154 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002155 llvm::raw_svector_ostream Out(MethodPool);
2156 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002157 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002158 BucketOffset = Generator.Emit(Out, Trait);
2159 }
2160
2161 // Create a blob abbreviation
2162 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002163 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2167 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2168
Douglas Gregor83941df2009-04-25 17:48:32 +00002169 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002170 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002171 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002172 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002173 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002174 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002175
2176 // Create a blob abbreviation for the selector table offsets.
2177 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002178 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00002180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2181 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2182
2183 // Write the selector offsets table.
2184 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002185 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002186 Record.push_back(SelectorOffsets.size());
2187 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002188 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002189 }
2190}
2191
Sebastian Redl3397c552010-08-18 23:56:27 +00002192/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002193void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002194 using namespace llvm;
2195 if (SemaRef.ReferencedSelectors.empty())
2196 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002197
Fariborz Jahanian32019832010-07-23 19:11:11 +00002198 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002199
Sebastian Redl3397c552010-08-18 23:56:27 +00002200 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002201 // very tricky to fix, and given that @selector shouldn't really appear in
2202 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002203 for (DenseMap<Selector, SourceLocation>::iterator S =
2204 SemaRef.ReferencedSelectors.begin(),
2205 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2206 Selector Sel = (*S).first;
2207 SourceLocation Loc = (*S).second;
2208 AddSelectorRef(Sel, Record);
2209 AddSourceLocation(Loc, Record);
2210 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002211 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002212}
2213
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002214//===----------------------------------------------------------------------===//
2215// Identifier Table Serialization
2216//===----------------------------------------------------------------------===//
2217
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002218namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002219class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002220 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002221 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002222
Douglas Gregora92193e2009-04-28 21:18:29 +00002223 /// \brief Determines whether this is an "interesting" identifier
2224 /// that needs a full IdentifierInfo structure written into the hash
2225 /// table.
2226 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2227 return II->isPoisoned() ||
2228 II->isExtensionToken() ||
2229 II->hasMacroDefinition() ||
2230 II->getObjCOrBuiltinID() ||
2231 II->getFETokenInfo<void>();
2232 }
2233
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002234public:
2235 typedef const IdentifierInfo* key_type;
2236 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002238 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002239 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Sebastian Redl3397c552010-08-18 23:56:27 +00002241 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002242 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002243
2244 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002245 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002246 }
Mike Stump1eb44332009-09-09 15:08:12 +00002247
2248 std::pair<unsigned,unsigned>
2249 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002250 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002251 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002252 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2253 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002254 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002255 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002256 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002257 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002258 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2259 DEnd = IdentifierResolver::end();
2260 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002261 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002262 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002263 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002264 // We emit the key length after the data length so that every
2265 // string is preceded by a 16-bit length. This matches the PTH
2266 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002267 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002268 return std::make_pair(KeyLen, DataLen);
2269 }
Mike Stump1eb44332009-09-09 15:08:12 +00002270
2271 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002272 unsigned KeyLen) {
2273 // Record the location of the key data. This is used when generating
2274 // the mapping from persistent IDs to strings.
2275 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002276 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002277 }
Mike Stump1eb44332009-09-09 15:08:12 +00002278
2279 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002280 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002281 if (!isInterestingIdentifier(II)) {
2282 clang::io::Emit32(Out, ID << 1);
2283 return;
2284 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002285
Douglas Gregora92193e2009-04-28 21:18:29 +00002286 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002287 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002288 bool hasMacroDefinition =
2289 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002290 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002291 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002292 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2293 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2294 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002295 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002296 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002297 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002298
Douglas Gregor37e26842009-04-21 23:56:24 +00002299 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002300 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002301
Douglas Gregor668c1a42009-04-21 22:25:48 +00002302 // Emit the declaration IDs in reverse order, because the
2303 // IdentifierResolver provides the declarations as they would be
2304 // visible (e.g., the function "stat" would come before the struct
2305 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2306 // adds declarations to the end of the list (so we need to see the
2307 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002308 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00002309 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002310 IdentifierResolver::end());
2311 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2312 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002313 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002314 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002315 }
2316};
2317} // end anonymous namespace
2318
Sebastian Redl3397c552010-08-18 23:56:27 +00002319/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002320///
2321/// The identifier table consists of a blob containing string data
2322/// (the actual identifiers themselves) and a separate "offsets" index
2323/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002324void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002325 using namespace llvm;
2326
2327 // Create and write out the blob that contains the identifier
2328 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002329 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002330 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002331 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002332
Douglas Gregor92b059e2009-04-28 20:33:11 +00002333 // Look for any identifiers that were named while processing the
2334 // headers, but are otherwise not needed. We add these to the hash
2335 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002336 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002337 // file.
2338 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2339 IDEnd = PP.getIdentifierTable().end();
2340 ID != IDEnd; ++ID)
2341 getIdentifierRef(ID->second);
2342
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002343 // Create the on-disk hash table representation. We only store offsets
2344 // for identifiers that appear here for the first time.
2345 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002346 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002347 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2348 ID != IDEnd; ++ID) {
2349 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002350 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002351 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002352 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002353
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002354 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002355 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002356 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002357 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002358 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002359 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002360 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002361 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002362 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002363 }
2364
2365 // Create a blob abbreviation
2366 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002367 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002370 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002371
2372 // Write the identifier table
2373 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002374 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002375 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002376 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002377 }
2378
2379 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002380 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002381 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2384 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2385
2386 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002387 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002388 Record.push_back(IdentifierOffsets.size());
2389 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002390 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002391}
2392
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002393//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002394// DeclContext's Name Lookup Table Serialization
2395//===----------------------------------------------------------------------===//
2396
2397namespace {
2398// Trait used for the on-disk hash table used in the method pool.
2399class ASTDeclContextNameLookupTrait {
2400 ASTWriter &Writer;
2401
2402public:
2403 typedef DeclarationName key_type;
2404 typedef key_type key_type_ref;
2405
2406 typedef DeclContext::lookup_result data_type;
2407 typedef const data_type& data_type_ref;
2408
2409 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2410
2411 unsigned ComputeHash(DeclarationName Name) {
2412 llvm::FoldingSetNodeID ID;
2413 ID.AddInteger(Name.getNameKind());
2414
2415 switch (Name.getNameKind()) {
2416 case DeclarationName::Identifier:
2417 ID.AddString(Name.getAsIdentifierInfo()->getName());
2418 break;
2419 case DeclarationName::ObjCZeroArgSelector:
2420 case DeclarationName::ObjCOneArgSelector:
2421 case DeclarationName::ObjCMultiArgSelector:
2422 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2423 break;
2424 case DeclarationName::CXXConstructorName:
2425 case DeclarationName::CXXDestructorName:
2426 case DeclarationName::CXXConversionFunctionName:
2427 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2428 break;
2429 case DeclarationName::CXXOperatorName:
2430 ID.AddInteger(Name.getCXXOverloadedOperator());
2431 break;
2432 case DeclarationName::CXXLiteralOperatorName:
2433 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2434 case DeclarationName::CXXUsingDirective:
2435 break;
2436 }
2437
2438 return ID.ComputeHash();
2439 }
2440
2441 std::pair<unsigned,unsigned>
2442 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2443 data_type_ref Lookup) {
2444 unsigned KeyLen = 1;
2445 switch (Name.getNameKind()) {
2446 case DeclarationName::Identifier:
2447 case DeclarationName::ObjCZeroArgSelector:
2448 case DeclarationName::ObjCOneArgSelector:
2449 case DeclarationName::ObjCMultiArgSelector:
2450 case DeclarationName::CXXConstructorName:
2451 case DeclarationName::CXXDestructorName:
2452 case DeclarationName::CXXConversionFunctionName:
2453 case DeclarationName::CXXLiteralOperatorName:
2454 KeyLen += 4;
2455 break;
2456 case DeclarationName::CXXOperatorName:
2457 KeyLen += 1;
2458 break;
2459 case DeclarationName::CXXUsingDirective:
2460 break;
2461 }
2462 clang::io::Emit16(Out, KeyLen);
2463
2464 // 2 bytes for num of decls and 4 for each DeclID.
2465 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2466 clang::io::Emit16(Out, DataLen);
2467
2468 return std::make_pair(KeyLen, DataLen);
2469 }
2470
2471 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2472 using namespace clang::io;
2473
2474 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2475 Emit8(Out, Name.getNameKind());
2476 switch (Name.getNameKind()) {
2477 case DeclarationName::Identifier:
2478 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2479 break;
2480 case DeclarationName::ObjCZeroArgSelector:
2481 case DeclarationName::ObjCOneArgSelector:
2482 case DeclarationName::ObjCMultiArgSelector:
2483 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2484 break;
2485 case DeclarationName::CXXConstructorName:
2486 case DeclarationName::CXXDestructorName:
2487 case DeclarationName::CXXConversionFunctionName:
2488 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2489 break;
2490 case DeclarationName::CXXOperatorName:
2491 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2492 Emit8(Out, Name.getCXXOverloadedOperator());
2493 break;
2494 case DeclarationName::CXXLiteralOperatorName:
2495 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2496 break;
2497 case DeclarationName::CXXUsingDirective:
2498 break;
2499 }
2500 }
2501
2502 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2503 data_type Lookup, unsigned DataLen) {
2504 uint64_t Start = Out.tell(); (void)Start;
2505 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2506 for (; Lookup.first != Lookup.second; ++Lookup.first)
2507 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2508
2509 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2510 }
2511};
2512} // end anonymous namespace
2513
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002514/// \brief Write the block containing all of the declaration IDs
2515/// visible from the given DeclContext.
2516///
2517/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002518/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002519uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2520 DeclContext *DC) {
2521 if (DC->getPrimaryContext() != DC)
2522 return 0;
2523
2524 // Since there is no name lookup into functions or methods, don't bother to
2525 // build a visible-declarations table for these entities.
2526 if (DC->isFunctionOrMethod())
2527 return 0;
2528
2529 // If not in C++, we perform name lookup for the translation unit via the
2530 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2531 // FIXME: In C++ we need the visible declarations in order to "see" the
2532 // friend declarations, is there a way to do this without writing the table ?
2533 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2534 return 0;
2535
2536 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002537 if (DC->hasExternalVisibleStorage())
2538 DC->MaterializeVisibleDeclsFromExternalStorage();
2539 else
2540 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002541
2542 // Serialize the contents of the mapping used for lookup. Note that,
2543 // although we have two very different code paths, the serialized
2544 // representation is the same for both cases: a declaration name,
2545 // followed by a size, followed by references to the visible
2546 // declarations that have that name.
2547 uint64_t Offset = Stream.GetCurrentBitNo();
2548 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2549 if (!Map || Map->empty())
2550 return 0;
2551
2552 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2553 ASTDeclContextNameLookupTrait Trait(*this);
2554
2555 // Create the on-disk hash table representation.
2556 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2557 D != DEnd; ++D) {
2558 DeclarationName Name = D->first;
2559 DeclContext::lookup_result Result = D->second.getLookupResult();
2560 Generator.insert(Name, Result, Trait);
2561 }
2562
2563 // Create the on-disk hash table in a buffer.
2564 llvm::SmallString<4096> LookupTable;
2565 uint32_t BucketOffset;
2566 {
2567 llvm::raw_svector_ostream Out(LookupTable);
2568 // Make sure that no bucket is at offset 0
2569 clang::io::Emit32(Out, 0);
2570 BucketOffset = Generator.Emit(Out, Trait);
2571 }
2572
2573 // Write the lookup table
2574 RecordData Record;
2575 Record.push_back(DECL_CONTEXT_VISIBLE);
2576 Record.push_back(BucketOffset);
2577 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2578 LookupTable.str());
2579
2580 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2581 ++NumVisibleDeclContexts;
2582 return Offset;
2583}
2584
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002585/// \brief Write an UPDATE_VISIBLE block for the given context.
2586///
2587/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2588/// DeclContext in a dependent AST file. As such, they only exist for the TU
2589/// (in C++) and for namespaces.
2590void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002591 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2592 if (!Map || Map->empty())
2593 return;
2594
2595 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2596 ASTDeclContextNameLookupTrait Trait(*this);
2597
2598 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002599 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2600 D != DEnd; ++D) {
2601 DeclarationName Name = D->first;
2602 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002603 // For any name that appears in this table, the results are complete, i.e.
2604 // they overwrite results from previous PCHs. Merging is always a mess.
2605 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002606 }
2607
2608 // Create the on-disk hash table in a buffer.
2609 llvm::SmallString<4096> LookupTable;
2610 uint32_t BucketOffset;
2611 {
2612 llvm::raw_svector_ostream Out(LookupTable);
2613 // Make sure that no bucket is at offset 0
2614 clang::io::Emit32(Out, 0);
2615 BucketOffset = Generator.Emit(Out, Trait);
2616 }
2617
2618 // Write the lookup table
2619 RecordData Record;
2620 Record.push_back(UPDATE_VISIBLE);
2621 Record.push_back(getDeclID(cast<Decl>(DC)));
2622 Record.push_back(BucketOffset);
2623 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2624}
2625
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002626/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2627void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2628 RecordData Record;
2629 Record.push_back(Opts.fp_contract);
2630 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2631}
2632
2633/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2634void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2635 if (!SemaRef.Context.getLangOptions().OpenCL)
2636 return;
2637
2638 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2639 RecordData Record;
2640#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2641#include "clang/Basic/OpenCLExtensions.def"
2642 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2643}
2644
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002645//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002646// General Serialization Routines
2647//===----------------------------------------------------------------------===//
2648
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002649/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002650void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002651 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002652 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2653 const Attr * A = *i;
2654 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2655 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002656
Sean Huntcf807c42010-08-18 23:23:40 +00002657#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002658
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002659 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002660}
2661
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002662void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002663 Record.push_back(Str.size());
2664 Record.insert(Record.end(), Str.begin(), Str.end());
2665}
2666
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002667void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2668 RecordDataImpl &Record) {
2669 Record.push_back(Version.getMajor());
2670 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2671 Record.push_back(*Minor + 1);
2672 else
2673 Record.push_back(0);
2674 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2675 Record.push_back(*Subminor + 1);
2676 else
2677 Record.push_back(0);
2678}
2679
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002680/// \brief Note that the identifier II occurs at the given offset
2681/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002682void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002683 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002684 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002685 // up earlier in the chain and thus don't need an offset.
2686 if (ID >= FirstIdentID)
2687 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002688}
2689
Douglas Gregor83941df2009-04-25 17:48:32 +00002690/// \brief Note that the selector Sel occurs at the given offset
2691/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002692void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002693 unsigned ID = SelectorIDs[Sel];
2694 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002695 // Don't record offsets for selectors that are also available in a different
2696 // file.
2697 if (ID < FirstSelectorID)
2698 return;
2699 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002700}
2701
Sebastian Redla4232eb2010-08-18 23:56:21 +00002702ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002703 : Stream(Stream), Chain(0), SerializationListener(0),
2704 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002705 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002706 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002707 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2708 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002709 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002710 NumVisibleDeclContexts(0),
2711 FirstCXXBaseSpecifiersID(1), NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002712 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002713 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2714 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2715 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002716 DeclTypedefAbbrev(0),
2717 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2718 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00002719{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002720}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002721
Sebastian Redla4232eb2010-08-18 23:56:21 +00002722void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002723 const std::string &OutputFile,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002724 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002725 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002726 Stream.Emit((unsigned)'C', 8);
2727 Stream.Emit((unsigned)'P', 8);
2728 Stream.Emit((unsigned)'C', 8);
2729 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002730
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002731 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002732
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002733 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002734 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002735 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002736 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002737}
2738
Sebastian Redla4232eb2010-08-18 23:56:21 +00002739void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002740 const char *isysroot,
2741 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002742 using namespace llvm;
2743
2744 ASTContext &Context = SemaRef.Context;
2745 Preprocessor &PP = SemaRef.PP;
2746
Douglas Gregor2cf26342009-04-09 22:27:44 +00002747 // The translation unit is the first declaration we'll emit.
2748 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002749 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002750 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002751
Douglas Gregor2deaea32009-04-22 18:49:13 +00002752 // Make sure that we emit IdentifierInfos (and any attached
2753 // declarations) for builtins.
2754 {
2755 IdentifierTable &Table = PP.getIdentifierTable();
2756 llvm::SmallVector<const char *, 32> BuiltinNames;
2757 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2758 Context.getLangOptions().NoBuiltin);
2759 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2760 getIdentifierRef(&Table.get(BuiltinNames[I]));
2761 }
2762
Chris Lattner63d65f82009-09-08 18:19:27 +00002763 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002764 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002765 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002766 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002767 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2768 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002769 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002770
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002771 // Build a record containing all of the file scoped decls in this file.
2772 RecordData UnusedFileScopedDecls;
2773 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2774 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002775
Sean Huntebcbe1d2011-05-04 23:29:54 +00002776 RecordData DelegatingCtorDecls;
2777 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i)
2778 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
2779
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002780 RecordData WeakUndeclaredIdentifiers;
2781 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2782 WeakUndeclaredIdentifiers.push_back(
2783 SemaRef.WeakUndeclaredIdentifiers.size());
2784 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2785 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2786 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2787 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2788 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2789 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2790 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2791 }
2792 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002793
Douglas Gregor14c22f22009-04-22 22:18:58 +00002794 // Build a record containing all of the locally-scoped external
2795 // declarations in this header file. Generally, this record will be
2796 // empty.
2797 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002798 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002799 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002800 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002801 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2802 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2803 TD != TDEnd; ++TD)
2804 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2805
Douglas Gregorb81c1702009-04-27 20:06:05 +00002806 // Build a record containing all of the ext_vector declarations.
2807 RecordData ExtVectorDecls;
2808 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2809 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2810
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002811 // Build a record containing all of the VTable uses information.
2812 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002813 if (!SemaRef.VTableUses.empty()) {
2814 VTableUses.push_back(SemaRef.VTableUses.size());
2815 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2816 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2817 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2818 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2819 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002820 }
2821
2822 // Build a record containing all of dynamic classes declarations.
2823 RecordData DynamicClasses;
2824 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2825 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2826
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002827 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002828 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002829 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002830 I = SemaRef.PendingInstantiations.begin(),
2831 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2832 AddDeclRef(I->first, PendingInstantiations);
2833 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002834 }
2835 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2836 "There are local ones at end of translation unit!");
2837
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002838 // Build a record containing some declaration references.
2839 RecordData SemaDeclRefs;
2840 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2841 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2842 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2843 }
2844
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002845 RecordData CUDASpecialDeclRefs;
2846 if (Context.getcudaConfigureCallDecl()) {
2847 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2848 }
2849
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002850 // Build a record containing all of the known namespaces.
2851 RecordData KnownNamespaces;
2852 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2853 I = SemaRef.KnownNamespaces.begin(),
2854 IEnd = SemaRef.KnownNamespaces.end();
2855 I != IEnd; ++I) {
2856 if (!I->second)
2857 AddDeclRef(I->first, KnownNamespaces);
2858 }
2859
Sebastian Redl3397c552010-08-18 23:56:27 +00002860 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002861 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002862 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002863 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002864 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002865 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002866 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002867 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002868 // Write the record of special types.
2869 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002871 AddTypeRef(Context.getBuiltinVaListType(), Record);
2872 AddTypeRef(Context.getObjCIdType(), Record);
2873 AddTypeRef(Context.getObjCSelType(), Record);
2874 AddTypeRef(Context.getObjCProtoType(), Record);
2875 AddTypeRef(Context.getObjCClassType(), Record);
2876 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2877 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2878 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002879 AddTypeRef(Context.getjmp_bufType(), Record);
2880 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002881 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2882 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002883 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002884 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002885 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2886 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002887 Record.push_back(Context.isInt128Installed());
Richard Smithad762fc2011-04-14 22:09:26 +00002888 AddTypeRef(Context.AutoDeductTy, Record);
2889 AddTypeRef(Context.AutoRRefDeductTy, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002890 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Douglas Gregor366809a2009-04-26 03:49:13 +00002892 // Keep writing types and declarations until all types and
2893 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00002894 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002895 WriteDeclsBlockAbbrevs();
2896 while (!DeclTypesToEmit.empty()) {
2897 DeclOrType DOT = DeclTypesToEmit.front();
2898 DeclTypesToEmit.pop();
2899 if (DOT.isType())
2900 WriteType(DOT.getType());
2901 else
2902 WriteDecl(Context, DOT.getDecl());
2903 }
2904 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002905
Douglas Gregor813a97b2009-10-17 17:25:45 +00002906 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002907 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002908 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002909 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002910 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002911 WriteFPPragmaOptions(SemaRef.getFPOptions());
2912 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002913
Sebastian Redl1476ed42010-07-16 16:36:56 +00002914 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002915 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002916
Anders Carlssonc8505782011-03-06 18:41:18 +00002917 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00002918
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002919 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002920 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002921 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002922
2923 // Write the record containing tentative definitions.
2924 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002925 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002926
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002927 // Write the record containing unused file scoped decls.
2928 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002929 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002930
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002931 // Write the record containing weak undeclared identifiers.
2932 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002933 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002934 WeakUndeclaredIdentifiers);
2935
Douglas Gregor14c22f22009-04-22 22:18:58 +00002936 // Write the record containing locally-scoped external definitions.
2937 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002938 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002939 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002940
2941 // Write the record containing ext_vector type names.
2942 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002943 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002944
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002945 // Write the record containing VTable uses information.
2946 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002947 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002948
2949 // Write the record containing dynamic classes declarations.
2950 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002951 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002952
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002953 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002954 if (!PendingInstantiations.empty())
2955 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002956
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002957 // Write the record containing declaration references of Sema.
2958 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002959 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002960
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002961 // Write the record containing CUDA-specific declaration references.
2962 if (!CUDASpecialDeclRefs.empty())
2963 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00002964
2965 // Write the delegating constructors.
2966 if (!DelegatingCtorDecls.empty())
2967 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002968
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002969 // Write the known namespaces.
2970 if (!KnownNamespaces.empty())
2971 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
2972
Douglas Gregor3e1af842009-04-17 22:13:46 +00002973 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002974 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002975 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002976 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002977 Record.push_back(NumLexicalDeclContexts);
2978 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002979 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002980 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002981}
2982
Sebastian Redla4232eb2010-08-18 23:56:21 +00002983void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002984 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002985 using namespace llvm;
2986
2987 ASTContext &Context = SemaRef.Context;
2988 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002989
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002990 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002991 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002992 WriteMetadata(Context, isysroot, "");
Sebastian Redl1476ed42010-07-16 16:36:56 +00002993 if (StatCalls && !isysroot)
2994 WriteStatCache(*StatCalls);
2995 // FIXME: Source manager block should only write new stuff, which could be
2996 // done by tracking the largest ID in the chain
2997 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002998
2999 // The special types are in the chained PCH.
3000
3001 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003002 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003003 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003004 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00003005 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3006 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003007 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00003008 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003009 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003010 else if ((*I)->isChangedSinceDeserialization())
3011 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003012 }
Sebastian Redl681d7232010-07-27 00:17:23 +00003013 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00003014 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003015 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00003016 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3017 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3018 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003019 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00003020 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003021 data(NewGlobalDecls));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003022 // And a visible updates block for the DeclContexts.
3023 Abv = new llvm::BitCodeAbbrev();
3024 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3025 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3026 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3027 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3028 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3029 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003030
Sebastian Redl083abdf2010-07-27 23:01:28 +00003031 // Build a record containing all of the new tentative definitions in this
3032 // file, in TentativeDefinitions order.
3033 RecordData TentativeDefinitions;
3034 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
3035 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
3036 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
3037 }
3038
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003039 // Build a record containing all of the file scoped decls in this file.
3040 RecordData UnusedFileScopedDecls;
3041 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
3042 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
3043 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003044 }
3045
Sean Huntebcbe1d2011-05-04 23:29:54 +00003046 // Build a record containing all of the delegating constructor decls in this
3047 // file.
3048 RecordData DelegatingCtorDecls;
3049 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i) {
3050 if (SemaRef.DelegatingCtorDecls[i]->getPCHLevel() == 0)
3051 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
3052 }
3053
Sebastian Redl40566802010-08-05 18:21:25 +00003054 // We write the entire table, overwriting the tables from the chain.
3055 RecordData WeakUndeclaredIdentifiers;
3056 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
3057 WeakUndeclaredIdentifiers.push_back(
3058 SemaRef.WeakUndeclaredIdentifiers.size());
3059 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
3060 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3061 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3062 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3063 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3064 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3065 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3066 }
3067 }
3068
Sebastian Redl083abdf2010-07-27 23:01:28 +00003069 // Build a record containing all of the locally-scoped external
3070 // declarations in this header file. Generally, this record will be
3071 // empty.
3072 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003073 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00003074 // nondeterminstic!
3075 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3076 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3077 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3078 TD != TDEnd; ++TD) {
3079 if (TD->second->getPCHLevel() == 0)
3080 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3081 }
3082
3083 // Build a record containing all of the ext_vector declarations.
3084 RecordData ExtVectorDecls;
3085 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3086 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3087 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3088 }
3089
Sebastian Redl40566802010-08-05 18:21:25 +00003090 // Build a record containing all of the VTable uses information.
3091 // We write everything here, because it's too hard to determine whether
3092 // a use is new to this part.
3093 RecordData VTableUses;
3094 if (!SemaRef.VTableUses.empty()) {
3095 VTableUses.push_back(SemaRef.VTableUses.size());
3096 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3097 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3098 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3099 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3100 }
3101 }
3102
3103 // Build a record containing all of dynamic classes declarations.
3104 RecordData DynamicClasses;
3105 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3106 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3107 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3108
3109 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003110 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00003111 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003112 I = SemaRef.PendingInstantiations.begin(),
3113 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00003114 AddDeclRef(I->first, PendingInstantiations);
3115 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003116 }
3117 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3118 "There are local ones at end of translation unit!");
3119
3120 // Build a record containing some declaration references.
3121 // It's not worth the effort to avoid duplication here.
3122 RecordData SemaDeclRefs;
3123 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3124 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3125 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3126 }
3127
Douglas Gregora72d8c42011-06-03 02:27:19 +00003128 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003129 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003130 for (DeclsToRewriteTy::iterator
3131 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3132 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003133 while (!DeclTypesToEmit.empty()) {
3134 DeclOrType DOT = DeclTypesToEmit.front();
3135 DeclTypesToEmit.pop();
3136 if (DOT.isType())
3137 WriteType(DOT.getType());
3138 else
3139 WriteDecl(Context, DOT.getDecl());
3140 }
3141 Stream.ExitBlock();
3142
Sebastian Redl083abdf2010-07-27 23:01:28 +00003143 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003144 WriteSelectors(SemaRef);
3145 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003146 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003147 WriteFPPragmaOptions(SemaRef.getFPOptions());
3148 WriteOpenCLExtensions(SemaRef);
3149
Sebastian Redl1476ed42010-07-16 16:36:56 +00003150 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003151 // FIXME: For chained PCH only write the new mappings (we currently
3152 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003153 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003154
Anders Carlssonc8505782011-03-06 18:41:18 +00003155 WriteCXXBaseSpecifiersOffsets();
3156
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003157 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003158 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003159 RecordData FirstLatestDeclIDs;
3160 for (FirstLatestDeclMap::iterator
3161 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3162 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3163 "Expected first & second to be in different PCHs");
3164 AddDeclRef(I->first, FirstLatestDeclIDs);
3165 AddDeclRef(I->second, FirstLatestDeclIDs);
3166 }
3167 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003168 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003169
Sebastian Redl083abdf2010-07-27 23:01:28 +00003170 // Write the record containing external, unnamed definitions.
3171 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003172 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003173
3174 // Write the record containing tentative definitions.
3175 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003176 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003177
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003178 // Write the record containing unused file scoped decls.
3179 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003180 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003181
Sebastian Redl40566802010-08-05 18:21:25 +00003182 // Write the record containing weak undeclared identifiers.
3183 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003184 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003185 WeakUndeclaredIdentifiers);
3186
Sebastian Redl083abdf2010-07-27 23:01:28 +00003187 // Write the record containing locally-scoped external definitions.
3188 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003189 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003190 LocallyScopedExternalDecls);
3191
3192 // Write the record containing ext_vector type names.
3193 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003194 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003195
Sebastian Redl40566802010-08-05 18:21:25 +00003196 // Write the record containing VTable uses information.
3197 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003198 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003199
3200 // Write the record containing dynamic classes declarations.
3201 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003202 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003203
3204 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003205 if (!PendingInstantiations.empty())
3206 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003207
3208 // Write the record containing declaration references of Sema.
3209 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003210 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003211
3212 // Write the delegating constructors.
3213 if (!DelegatingCtorDecls.empty())
3214 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003215
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003216 // Write the updates to DeclContexts.
3217 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3218 I = UpdatedDeclContexts.begin(),
3219 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003220 I != E; ++I)
3221 WriteDeclContextVisibleUpdate(*I);
3222
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003223 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003224
Sebastian Redl083abdf2010-07-27 23:01:28 +00003225 Record.clear();
3226 Record.push_back(NumStatements);
3227 Record.push_back(NumMacros);
3228 Record.push_back(NumLexicalDeclContexts);
3229 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003230 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003231 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003232 Stream.ExitBlock();
3233}
3234
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003235void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003236 if (DeclUpdates.empty())
3237 return;
3238
3239 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003240 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003241 for (DeclUpdateMap::iterator
3242 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3243 const Decl *D = I->first;
3244 UpdateRecord &URec = I->second;
3245
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003246 if (DeclsToRewrite.count(D))
3247 continue; // The decl will be written completely,no need to store updates.
3248
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003249 uint64_t Offset = Stream.GetCurrentBitNo();
3250 Stream.EmitRecord(DECL_UPDATES, URec);
3251
3252 OffsetsRecord.push_back(GetDeclRef(D));
3253 OffsetsRecord.push_back(Offset);
3254 }
3255 Stream.ExitBlock();
3256 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3257}
3258
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003259void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003260 if (ReplacedDecls.empty())
3261 return;
3262
3263 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003264 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003265 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3266 Record.push_back(I->first);
3267 Record.push_back(I->second);
3268 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003269 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003270}
3271
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003272void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003273 Record.push_back(Loc.getRawEncoding());
3274}
3275
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003276void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003277 AddSourceLocation(Range.getBegin(), Record);
3278 AddSourceLocation(Range.getEnd(), Record);
3279}
3280
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003281void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003282 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003283 const uint64_t *Words = Value.getRawData();
3284 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003285}
3286
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003287void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003288 Record.push_back(Value.isUnsigned());
3289 AddAPInt(Value, Record);
3290}
3291
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003292void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003293 AddAPInt(Value.bitcastToAPInt(), Record);
3294}
3295
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003296void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003297 Record.push_back(getIdentifierRef(II));
3298}
3299
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003300IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003301 if (II == 0)
3302 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003303
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003304 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003305 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003306 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003307 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003308}
3309
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003310MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003311 if (MD == 0)
3312 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003313
3314 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003315 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003316 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003317 return ID;
3318}
3319
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003320void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003321 Record.push_back(getSelectorRef(SelRef));
3322}
3323
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003324SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003325 if (Sel.getAsOpaquePtr() == 0) {
3326 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003327 }
3328
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003329 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003330 if (SID == 0 && Chain) {
3331 // This might trigger a ReadSelector callback, which will set the ID for
3332 // this selector.
3333 Chain->LoadSelector(Sel);
3334 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003335 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003336 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003337 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003338 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003339}
3340
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003341void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003342 AddDeclRef(Temp->getDestructor(), Record);
3343}
3344
Douglas Gregor7c789c12010-10-29 22:39:52 +00003345void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3346 CXXBaseSpecifier const *BasesEnd,
3347 RecordDataImpl &Record) {
3348 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3349 CXXBaseSpecifiersToWrite.push_back(
3350 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3351 Bases, BasesEnd));
3352 Record.push_back(NextCXXBaseSpecifiersID++);
3353}
3354
Sebastian Redla4232eb2010-08-18 23:56:21 +00003355void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003356 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003357 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003358 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003359 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003360 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003361 break;
3362 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003363 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003364 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003365 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003366 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003367 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003368 break;
3369 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003370 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003371 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003372 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003373 break;
John McCall833ca992009-10-29 08:12:44 +00003374 case TemplateArgument::Null:
3375 case TemplateArgument::Integral:
3376 case TemplateArgument::Declaration:
3377 case TemplateArgument::Pack:
3378 break;
3379 }
3380}
3381
Sebastian Redla4232eb2010-08-18 23:56:21 +00003382void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003383 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003384 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003385
3386 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3387 bool InfoHasSameExpr
3388 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3389 Record.push_back(InfoHasSameExpr);
3390 if (InfoHasSameExpr)
3391 return; // Avoid storing the same expr twice.
3392 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003393 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3394 Record);
3395}
3396
Douglas Gregordc355712011-02-25 00:36:19 +00003397void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3398 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003399 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003400 AddTypeRef(QualType(), Record);
3401 return;
3402 }
3403
Douglas Gregordc355712011-02-25 00:36:19 +00003404 AddTypeLoc(TInfo->getTypeLoc(), Record);
3405}
3406
3407void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3408 AddTypeRef(TL.getType(), Record);
3409
John McCalla1ee0c52009-10-16 21:56:05 +00003410 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003411 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003412 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003413}
3414
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003415void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003416 Record.push_back(GetOrCreateTypeID(T));
3417}
3418
3419TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003420 return MakeTypeID(T,
3421 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3422}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003423
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003424TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003425 return MakeTypeID(T,
3426 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003427}
3428
3429TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3430 if (T.isNull())
3431 return TypeIdx();
3432 assert(!T.getLocalFastQualifiers());
3433
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003434 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003435 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003436 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003437 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003438 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003439 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003440 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003441 return Idx;
3442}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003443
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003444TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003445 if (T.isNull())
3446 return TypeIdx();
3447 assert(!T.getLocalFastQualifiers());
3448
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003449 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3450 assert(I != TypeIdxs.end() && "Type not emitted!");
3451 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003452}
3453
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003454void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003455 Record.push_back(GetDeclRef(D));
3456}
3457
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003458DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003459 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003460 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003461 }
Douglas Gregor97475832010-10-05 18:37:06 +00003462 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003463 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003464 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003465 // We haven't seen this declaration before. Give it a new ID and
3466 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003467 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003468 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003469 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3470 // We don't add it to the replacement collection here, because we don't
3471 // have the offset yet.
3472 DeclTypesToEmit.push(const_cast<Decl *>(D));
3473 // Reset the flag, so that we don't add this decl multiple times.
3474 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003475 }
3476
Sebastian Redl681d7232010-07-27 00:17:23 +00003477 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003478}
3479
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003480DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003481 if (D == 0)
3482 return 0;
3483
3484 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3485 return DeclIDs[D];
3486}
3487
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003488void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003489 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003490 Record.push_back(Name.getNameKind());
3491 switch (Name.getNameKind()) {
3492 case DeclarationName::Identifier:
3493 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3494 break;
3495
3496 case DeclarationName::ObjCZeroArgSelector:
3497 case DeclarationName::ObjCOneArgSelector:
3498 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003499 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003500 break;
3501
3502 case DeclarationName::CXXConstructorName:
3503 case DeclarationName::CXXDestructorName:
3504 case DeclarationName::CXXConversionFunctionName:
3505 AddTypeRef(Name.getCXXNameType(), Record);
3506 break;
3507
3508 case DeclarationName::CXXOperatorName:
3509 Record.push_back(Name.getCXXOverloadedOperator());
3510 break;
3511
Sean Hunt3e518bd2009-11-29 07:34:05 +00003512 case DeclarationName::CXXLiteralOperatorName:
3513 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3514 break;
3515
Douglas Gregor2cf26342009-04-09 22:27:44 +00003516 case DeclarationName::CXXUsingDirective:
3517 // No extra data to emit
3518 break;
3519 }
3520}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003521
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003522void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003523 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003524 switch (Name.getNameKind()) {
3525 case DeclarationName::CXXConstructorName:
3526 case DeclarationName::CXXDestructorName:
3527 case DeclarationName::CXXConversionFunctionName:
3528 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3529 break;
3530
3531 case DeclarationName::CXXOperatorName:
3532 AddSourceLocation(
3533 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3534 Record);
3535 AddSourceLocation(
3536 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3537 Record);
3538 break;
3539
3540 case DeclarationName::CXXLiteralOperatorName:
3541 AddSourceLocation(
3542 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3543 Record);
3544 break;
3545
3546 case DeclarationName::Identifier:
3547 case DeclarationName::ObjCZeroArgSelector:
3548 case DeclarationName::ObjCOneArgSelector:
3549 case DeclarationName::ObjCMultiArgSelector:
3550 case DeclarationName::CXXUsingDirective:
3551 break;
3552 }
3553}
3554
3555void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003556 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003557 AddDeclarationName(NameInfo.getName(), Record);
3558 AddSourceLocation(NameInfo.getLoc(), Record);
3559 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3560}
3561
3562void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003563 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003564 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003565 Record.push_back(Info.NumTemplParamLists);
3566 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3567 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3568}
3569
Sebastian Redla4232eb2010-08-18 23:56:21 +00003570void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003571 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003572 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003573 // typically accommodate the vast majority.
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003574 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3575
3576 // Push each of the NNS's onto a stack for serialization in reverse order.
3577 while (NNS) {
3578 NestedNames.push_back(NNS);
3579 NNS = NNS->getPrefix();
3580 }
3581
3582 Record.push_back(NestedNames.size());
3583 while(!NestedNames.empty()) {
3584 NNS = NestedNames.pop_back_val();
3585 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3586 Record.push_back(Kind);
3587 switch (Kind) {
3588 case NestedNameSpecifier::Identifier:
3589 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3590 break;
3591
3592 case NestedNameSpecifier::Namespace:
3593 AddDeclRef(NNS->getAsNamespace(), Record);
3594 break;
3595
Douglas Gregor14aba762011-02-24 02:36:08 +00003596 case NestedNameSpecifier::NamespaceAlias:
3597 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3598 break;
3599
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003600 case NestedNameSpecifier::TypeSpec:
3601 case NestedNameSpecifier::TypeSpecWithTemplate:
3602 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3603 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3604 break;
3605
3606 case NestedNameSpecifier::Global:
3607 // Don't need to write an associated value.
3608 break;
3609 }
3610 }
3611}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003612
Douglas Gregordc355712011-02-25 00:36:19 +00003613void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3614 RecordDataImpl &Record) {
3615 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003616 // typically accommodate the vast majority.
Douglas Gregordc355712011-02-25 00:36:19 +00003617 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3618
3619 // Push each of the nested-name-specifiers's onto a stack for
3620 // serialization in reverse order.
3621 while (NNS) {
3622 NestedNames.push_back(NNS);
3623 NNS = NNS.getPrefix();
3624 }
3625
3626 Record.push_back(NestedNames.size());
3627 while(!NestedNames.empty()) {
3628 NNS = NestedNames.pop_back_val();
3629 NestedNameSpecifier::SpecifierKind Kind
3630 = NNS.getNestedNameSpecifier()->getKind();
3631 Record.push_back(Kind);
3632 switch (Kind) {
3633 case NestedNameSpecifier::Identifier:
3634 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3635 AddSourceRange(NNS.getLocalSourceRange(), Record);
3636 break;
3637
3638 case NestedNameSpecifier::Namespace:
3639 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3640 AddSourceRange(NNS.getLocalSourceRange(), Record);
3641 break;
3642
3643 case NestedNameSpecifier::NamespaceAlias:
3644 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3645 AddSourceRange(NNS.getLocalSourceRange(), Record);
3646 break;
3647
3648 case NestedNameSpecifier::TypeSpec:
3649 case NestedNameSpecifier::TypeSpecWithTemplate:
3650 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3651 AddTypeLoc(NNS.getTypeLoc(), Record);
3652 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3653 break;
3654
3655 case NestedNameSpecifier::Global:
3656 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3657 break;
3658 }
3659 }
3660}
3661
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003662void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003663 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003664 Record.push_back(Kind);
3665 switch (Kind) {
3666 case TemplateName::Template:
3667 AddDeclRef(Name.getAsTemplateDecl(), Record);
3668 break;
3669
3670 case TemplateName::OverloadedTemplate: {
3671 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3672 Record.push_back(OvT->size());
3673 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3674 I != E; ++I)
3675 AddDeclRef(*I, Record);
3676 break;
3677 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003678
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003679 case TemplateName::QualifiedTemplate: {
3680 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3681 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3682 Record.push_back(QualT->hasTemplateKeyword());
3683 AddDeclRef(QualT->getTemplateDecl(), Record);
3684 break;
3685 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003686
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003687 case TemplateName::DependentTemplate: {
3688 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3689 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3690 Record.push_back(DepT->isIdentifier());
3691 if (DepT->isIdentifier())
3692 AddIdentifierRef(DepT->getIdentifier(), Record);
3693 else
3694 Record.push_back(DepT->getOperator());
3695 break;
3696 }
John McCall14606042011-06-30 08:33:18 +00003697
3698 case TemplateName::SubstTemplateTemplateParm: {
3699 SubstTemplateTemplateParmStorage *subst
3700 = Name.getAsSubstTemplateTemplateParm();
3701 AddDeclRef(subst->getParameter(), Record);
3702 AddTemplateName(subst->getReplacement(), Record);
3703 break;
3704 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003705
3706 case TemplateName::SubstTemplateTemplateParmPack: {
3707 SubstTemplateTemplateParmPackStorage *SubstPack
3708 = Name.getAsSubstTemplateTemplateParmPack();
3709 AddDeclRef(SubstPack->getParameterPack(), Record);
3710 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3711 break;
3712 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003713 }
3714}
3715
Michael J. Spencer20249a12010-10-21 03:16:25 +00003716void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003717 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003718 Record.push_back(Arg.getKind());
3719 switch (Arg.getKind()) {
3720 case TemplateArgument::Null:
3721 break;
3722 case TemplateArgument::Type:
3723 AddTypeRef(Arg.getAsType(), Record);
3724 break;
3725 case TemplateArgument::Declaration:
3726 AddDeclRef(Arg.getAsDecl(), Record);
3727 break;
3728 case TemplateArgument::Integral:
3729 AddAPSInt(*Arg.getAsIntegral(), Record);
3730 AddTypeRef(Arg.getIntegralType(), Record);
3731 break;
3732 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003733 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3734 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003735 case TemplateArgument::TemplateExpansion:
3736 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003737 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3738 Record.push_back(*NumExpansions + 1);
3739 else
3740 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003741 break;
3742 case TemplateArgument::Expression:
3743 AddStmt(Arg.getAsExpr());
3744 break;
3745 case TemplateArgument::Pack:
3746 Record.push_back(Arg.pack_size());
3747 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3748 I != E; ++I)
3749 AddTemplateArgument(*I, Record);
3750 break;
3751 }
3752}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003753
3754void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003755ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003756 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003757 assert(TemplateParams && "No TemplateParams!");
3758 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3759 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3760 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3761 Record.push_back(TemplateParams->size());
3762 for (TemplateParameterList::const_iterator
3763 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3764 P != PEnd; ++P)
3765 AddDeclRef(*P, Record);
3766}
3767
3768/// \brief Emit a template argument list.
3769void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003770ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003771 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003772 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003773 Record.push_back(TemplateArgs->size());
3774 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003775 AddTemplateArgument(TemplateArgs->get(i), Record);
3776}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003777
3778
3779void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003780ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003781 Record.push_back(Set.size());
3782 for (UnresolvedSetImpl::const_iterator
3783 I = Set.begin(), E = Set.end(); I != E; ++I) {
3784 AddDeclRef(I.getDecl(), Record);
3785 Record.push_back(I.getAccess());
3786 }
3787}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003788
Sebastian Redla4232eb2010-08-18 23:56:21 +00003789void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003790 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003791 Record.push_back(Base.isVirtual());
3792 Record.push_back(Base.isBaseOfClass());
3793 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003794 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003795 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003796 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003797 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3798 : SourceLocation(),
3799 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003800}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003801
Douglas Gregor7c789c12010-10-29 22:39:52 +00003802void ASTWriter::FlushCXXBaseSpecifiers() {
3803 RecordData Record;
3804 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3805 Record.clear();
3806
3807 // Record the offset of this base-specifier set.
3808 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3809 if (Index == CXXBaseSpecifiersOffsets.size())
3810 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3811 else {
3812 if (Index > CXXBaseSpecifiersOffsets.size())
3813 CXXBaseSpecifiersOffsets.resize(Index + 1);
3814 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3815 }
3816
3817 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3818 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3819 Record.push_back(BEnd - B);
3820 for (; B != BEnd; ++B)
3821 AddCXXBaseSpecifier(*B, Record);
3822 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003823
3824 // Flush any expressions that were written as part of the base specifiers.
3825 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003826 }
3827
3828 CXXBaseSpecifiersToWrite.clear();
3829}
3830
Sean Huntcbb67482011-01-08 20:30:50 +00003831void ASTWriter::AddCXXCtorInitializers(
3832 const CXXCtorInitializer * const *CtorInitializers,
3833 unsigned NumCtorInitializers,
3834 RecordDataImpl &Record) {
3835 Record.push_back(NumCtorInitializers);
3836 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3837 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003838
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003839 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00003840 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003841 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3842 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00003843 } else if (Init->isDelegatingInitializer()) {
3844 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3845 AddDeclRef(Init->getTargetConstructor(), Record);
3846 } else if (Init->isMemberInitializer()){
3847 Record.push_back(CTOR_INITIALIZER_MEMBER);
3848 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003849 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00003850 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3851 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003852 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003853
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003854 AddSourceLocation(Init->getMemberLocation(), Record);
3855 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003856 AddSourceLocation(Init->getLParenLoc(), Record);
3857 AddSourceLocation(Init->getRParenLoc(), Record);
3858 Record.push_back(Init->isWritten());
3859 if (Init->isWritten()) {
3860 Record.push_back(Init->getSourceOrder());
3861 } else {
3862 Record.push_back(Init->getNumArrayIndices());
3863 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3864 AddDeclRef(Init->getArrayIndex(i), Record);
3865 }
3866 }
3867}
3868
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003869void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3870 assert(D->DefinitionData);
3871 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3872 Record.push_back(Data.UserDeclaredConstructor);
3873 Record.push_back(Data.UserDeclaredCopyConstructor);
3874 Record.push_back(Data.UserDeclaredCopyAssignment);
3875 Record.push_back(Data.UserDeclaredDestructor);
3876 Record.push_back(Data.Aggregate);
3877 Record.push_back(Data.PlainOldData);
3878 Record.push_back(Data.Empty);
3879 Record.push_back(Data.Polymorphic);
3880 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00003881 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00003882 Record.push_back(Data.HasNoNonEmptyBases);
3883 Record.push_back(Data.HasPrivateFields);
3884 Record.push_back(Data.HasProtectedFields);
3885 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00003886 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00003887 Record.push_back(Data.HasTrivialDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003888 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003889 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003890 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003891 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003892 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003893 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003894 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003895 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00003896 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003897 Record.push_back(Data.DeclaredDefaultConstructor);
3898 Record.push_back(Data.DeclaredCopyConstructor);
3899 Record.push_back(Data.DeclaredCopyAssignment);
3900 Record.push_back(Data.DeclaredDestructor);
3901
3902 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003903 if (Data.NumBases > 0)
3904 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3905 Record);
3906
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003907 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3908 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003909 if (Data.NumVBases > 0)
3910 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3911 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003912
3913 AddUnresolvedSet(Data.Conversions, Record);
3914 AddUnresolvedSet(Data.VisibleConversions, Record);
3915 // Data.Definition is the owning decl, no need to write it.
3916 AddDeclRef(Data.FirstFriend, Record);
3917}
3918
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003919void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003920 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003921 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003922 assert(FirstDeclID == NextDeclID &&
3923 FirstTypeID == NextTypeID &&
3924 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003925 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003926 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003927 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003928 "Setting chain after writing has started.");
3929 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003930
3931 FirstDeclID += Chain->getTotalNumDecls();
3932 FirstTypeID += Chain->getTotalNumTypes();
3933 FirstIdentID += Chain->getTotalNumIdentifiers();
3934 FirstSelectorID += Chain->getTotalNumSelectors();
3935 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003936 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003937 NextDeclID = FirstDeclID;
3938 NextTypeID = FirstTypeID;
3939 NextIdentID = FirstIdentID;
3940 NextSelectorID = FirstSelectorID;
3941 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003942 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003943}
3944
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003945void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003946 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003947 if (II->hasMacroDefinition())
3948 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003949}
3950
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003951void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003952 // Always take the highest-numbered type index. This copes with an interesting
3953 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003954 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003955 // keep the higher-numbered entry so that we can properly write it out to
3956 // the AST file.
3957 TypeIdx &StoredIdx = TypeIdxs[T];
3958 if (Idx.getIndex() >= StoredIdx.getIndex())
3959 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003960}
3961
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003962void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003963 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003964}
Sebastian Redl5d050072010-08-04 17:20:04 +00003965
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003966void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003967 SelectorIDs[S] = ID;
3968}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003969
Michael J. Spencer20249a12010-10-21 03:16:25 +00003970void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003971 MacroDefinition *MD) {
3972 MacroDefinitions[MD] = ID;
3973}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003974
3975void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3976 assert(D->isDefinition());
3977 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3978 // We are interested when a PCH decl is modified.
3979 if (RD->getPCHLevel() > 0) {
3980 // A forward reference was mutated into a definition. Rewrite it.
3981 // FIXME: This happens during template instantiation, should we
3982 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003983 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003984 }
3985
3986 for (CXXRecordDecl::redecl_iterator
3987 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3988 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3989 if (Redecl == RD)
3990 continue;
3991
3992 // We are interested when a PCH decl is modified.
3993 if (Redecl->getPCHLevel() > 0) {
3994 UpdateRecord &Record = DeclUpdates[Redecl];
3995 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3996 assert(Redecl->DefinitionData);
3997 assert(Redecl->DefinitionData->Definition == D);
3998 AddDeclRef(D, Record); // the DefinitionDecl
3999 }
4000 }
4001 }
4002}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004003void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
4004 // TU and namespaces are handled elsewhere.
4005 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4006 return;
4007
4008 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
4009 return; // Not a source decl added to a DeclContext from PCH.
4010
4011 AddUpdatedDeclContext(DC);
4012}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004013
4014void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
4015 assert(D->isImplicit());
4016 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
4017 return; // Not a source member added to a class from PCH.
4018 if (!isa<CXXMethodDecl>(D))
4019 return; // We are interested in lazily declared implicit methods.
4020
4021 // A decl coming from PCH was modified.
4022 assert(RD->isDefinition());
4023 UpdateRecord &Record = DeclUpdates[RD];
4024 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
4025 AddDeclRef(D, Record);
4026}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004027
4028void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4029 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004030 // The specializations set is kept in the canonical template.
4031 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004032 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4033 return; // Not a source specialization added to a template from PCH.
4034
4035 UpdateRecord &Record = DeclUpdates[TD];
4036 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4037 AddDeclRef(D, Record);
4038}
Douglas Gregor89d99802010-11-30 06:16:57 +00004039
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004040void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4041 const FunctionDecl *D) {
4042 // The specializations set is kept in the canonical template.
4043 TD = TD->getCanonicalDecl();
4044 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4045 return; // Not a source specialization added to a template from PCH.
4046
4047 UpdateRecord &Record = DeclUpdates[TD];
4048 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4049 AddDeclRef(D, Record);
4050}
4051
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004052void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4053 if (D->getPCHLevel() == 0)
4054 return; // Declaration not imported from PCH.
4055
4056 // Implicit decl from a PCH was defined.
4057 // FIXME: Should implicit definition be a separate FunctionDecl?
4058 RewriteDecl(D);
4059}
4060
Sebastian Redlf79a7192011-04-29 08:19:30 +00004061void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4062 if (D->getPCHLevel() == 0)
4063 return;
4064
4065 // Since the actual instantiation is delayed, this really means that we need
4066 // to update the instantiation location.
4067 UpdateRecord &Record = DeclUpdates[D];
4068 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4069 AddSourceLocation(
4070 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4071}
4072
Douglas Gregor89d99802010-11-30 06:16:57 +00004073ASTSerializationListener::~ASTSerializationListener() { }