blob: b31262d375feeed89271fa269f31447732524b20 [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"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Sebastian Redlade50002010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000059}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
66
Douglas Gregor2cf26342009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000070
Douglas Gregor2cf26342009-04-09 22:27:44 +000071namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl3397c552010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000116}
117
Sebastian Redl3397c552010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159}
160
Sebastian Redl3397c552010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175}
176
Sebastian Redl3397c552010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
188 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000189 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000190 Record.push_back(T->getExceptionSpecType());
191 if (T->getExceptionSpecType() == EST_Dynamic) {
192 Record.push_back(T->getNumExceptions());
193 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
194 Writer.AddTypeRef(T->getExceptionType(I), Record);
195 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
196 Writer.AddStmt(T->getNoexceptExpr());
197 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000198 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199}
200
Sebastian Redl3397c552010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000202 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000203 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000204}
John McCalled976492009-12-04 22:46:56 +0000205
Sebastian Redl3397c552010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000207 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000208 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
209 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000210 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000211}
212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000214 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000215 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000216}
217
Sebastian Redl3397c552010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000226}
227
Sean Huntca63c202011-05-24 22:41:36 +0000228void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
229 Writer.AddTypeRef(T->getBaseType(), Record);
230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
231 Record.push_back(T->getUTTKind());
232 Code = TYPE_UNARY_TRANSFORM;
233}
234
Richard Smith34b41d92011-02-20 03:19:35 +0000235void ASTTypeWriter::VisitAutoType(const AutoType *T) {
236 Writer.AddTypeRef(T->getDeducedType(), Record);
237 Code = TYPE_AUTO;
238}
239
Sebastian Redl3397c552010-08-18 23:56:27 +0000240void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000241 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000242 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000243 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000244 "Cannot serialize in the middle of a type definition");
245}
246
Sebastian Redl3397c552010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000249 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000250}
251
Sebastian Redl3397c552010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000254 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255}
256
John McCall9d156a72011-01-06 01:58:22 +0000257void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
258 Writer.AddTypeRef(T->getModifiedType(), Record);
259 Writer.AddTypeRef(T->getEquivalentType(), Record);
260 Record.push_back(T->getAttrKind());
261 Code = TYPE_ATTRIBUTED;
262}
263
Mike Stump1eb44332009-09-09 15:08:12 +0000264void
Sebastian Redl3397c552010-08-18 23:56:27 +0000265ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000266 const SubstTemplateTypeParmType *T) {
267 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
268 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000269 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000270}
271
272void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
274 const SubstTemplateTypeParmPackType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
278}
279
280void
Sebastian Redl3397c552010-08-18 23:56:27 +0000281ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000282 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000283 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000284 Writer.AddTemplateName(T->getTemplateName(), Record);
285 Record.push_back(T->getNumArgs());
286 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
287 ArgI != ArgE; ++ArgI)
288 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000289 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
290 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000291 : T->getCanonicalTypeInternal(),
292 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000293 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000294}
295
296void
Sebastian Redl3397c552010-08-18 23:56:27 +0000297ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000298 VisitArrayType(T);
299 Writer.AddStmt(T->getSizeExpr());
300 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl3397c552010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000306 const DependentSizedExtVectorType *T) {
307 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000308 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl3397c552010-08-18 23:56:27 +0000312ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313 Record.push_back(T->getDepth());
314 Record.push_back(T->getIndex());
315 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000316 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000317 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000318}
319
320void
Sebastian Redl3397c552010-08-18 23:56:27 +0000321ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000322 Record.push_back(T->getKeyword());
323 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
324 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000325 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
326 : T->getCanonicalTypeInternal(),
327 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000333 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000334 Record.push_back(T->getKeyword());
335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddIdentifierRef(T->getIdentifier(), Record);
337 Record.push_back(T->getNumArgs());
338 for (DependentTemplateSpecializationType::iterator
339 I = T->begin(), E = T->end(); I != E; ++I)
340 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000341 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000342}
343
Douglas Gregor7536dd52010-12-20 02:24:11 +0000344void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
345 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000346 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
347 Record.push_back(*NumExpansions + 1);
348 else
349 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000350 Code = TYPE_PACK_EXPANSION;
351}
352
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000353void ASTTypeWriter::VisitParenType(const ParenType *T) {
354 Writer.AddTypeRef(T->getInnerType(), Record);
355 Code = TYPE_PAREN;
356}
357
Sebastian Redl3397c552010-08-18 23:56:27 +0000358void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000359 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000360 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
361 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000362 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000363}
364
Sebastian Redl3397c552010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000366 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000367 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000368 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000369}
370
Sebastian Redl3397c552010-08-18 23:56:27 +0000371void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000372 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000374}
375
Sebastian Redl3397c552010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000377 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000378 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000379 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000380 E = T->qual_end(); I != E; ++I)
381 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000382 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000383}
384
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000385void
Sebastian Redl3397c552010-08-18 23:56:27 +0000386ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000387 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000388 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000389}
390
Eli Friedmanb001de72011-10-06 23:00:33 +0000391void
392ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
393 Writer.AddTypeRef(T->getValueType(), Record);
394 Code = TYPE_ATOMIC;
395}
396
John McCalla1ee0c52009-10-16 21:56:05 +0000397namespace {
398
399class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000400 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000401 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000402
403public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000404 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000405 : Writer(Writer), Record(Record) { }
406
John McCall51bd8032009-10-18 01:05:36 +0000407#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000408#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000409 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000410#include "clang/AST/TypeLocNodes.def"
411
John McCall51bd8032009-10-18 01:05:36 +0000412 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
413 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000414};
415
416}
417
John McCall51bd8032009-10-18 01:05:36 +0000418void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
419 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000420}
John McCall51bd8032009-10-18 01:05:36 +0000421void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000422 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
423 if (TL.needsExtraLocalData()) {
424 Record.push_back(TL.getWrittenTypeSpec());
425 Record.push_back(TL.getWrittenSignSpec());
426 Record.push_back(TL.getWrittenWidthSpec());
427 Record.push_back(TL.hasModeAttr());
428 }
John McCalla1ee0c52009-10-16 21:56:05 +0000429}
John McCall51bd8032009-10-18 01:05:36 +0000430void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000432}
John McCall51bd8032009-10-18 01:05:36 +0000433void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000435}
John McCall51bd8032009-10-18 01:05:36 +0000436void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000438}
John McCall51bd8032009-10-18 01:05:36 +0000439void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000441}
John McCall51bd8032009-10-18 01:05:36 +0000442void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000444}
John McCall51bd8032009-10-18 01:05:36 +0000445void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000447 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
451 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
452 Record.push_back(TL.getSizeExpr() ? 1 : 0);
453 if (TL.getSizeExpr())
454 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
457 VisitArrayTypeLoc(TL);
458}
459void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
460 VisitArrayTypeLoc(TL);
461}
462void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
463 VisitArrayTypeLoc(TL);
464}
465void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
466 DependentSizedArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
470 DependentSizedExtVectorTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
473void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
475}
476void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
479void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000480 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
481 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000482 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000483 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
484 Writer.AddDeclRef(TL.getArg(i), Record);
485}
486void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
487 VisitFunctionTypeLoc(TL);
488}
489void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
490 VisitFunctionTypeLoc(TL);
491}
John McCalled976492009-12-04 22:46:56 +0000492void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
John McCall51bd8032009-10-18 01:05:36 +0000495void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getNameLoc(), Record);
497}
498void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
500 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
501 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000502}
503void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000504 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
505 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
506 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
507 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000508}
509void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
Sean Huntca63c202011-05-24 22:41:36 +0000512void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
513 Writer.AddSourceLocation(TL.getKWLoc(), Record);
514 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
515 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
516 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
517}
Richard Smith34b41d92011-02-20 03:19:35 +0000518void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
John McCall51bd8032009-10-18 01:05:36 +0000521void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall9d156a72011-01-06 01:58:22 +0000527void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
529 if (TL.hasAttrOperand()) {
530 SourceRange range = TL.getAttrOperandParensRange();
531 Writer.AddSourceLocation(range.getBegin(), Record);
532 Writer.AddSourceLocation(range.getEnd(), Record);
533 }
534 if (TL.hasAttrExprOperand()) {
535 Expr *operand = TL.getAttrExprOperand();
536 Record.push_back(operand ? 1 : 0);
537 if (operand) Writer.AddStmt(operand);
538 } else if (TL.hasAttrEnumOperand()) {
539 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
540 }
541}
John McCall51bd8032009-10-18 01:05:36 +0000542void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCall49a832b2009-10-18 09:09:24 +0000545void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
546 SubstTemplateTypeParmTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getNameLoc(), Record);
548}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000549void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
550 SubstTemplateTypeParmPackTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall51bd8032009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
554 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000555 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000559 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
560 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000561}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000562void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
564 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
565}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000566void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000567 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000568 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000569}
John McCall3cb0ebd2010-03-10 03:28:59 +0000570void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
Douglas Gregor4714c122010-03-31 17:34:00 +0000573void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000574 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000575 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000576 Writer.AddSourceLocation(TL.getNameLoc(), Record);
577}
John McCall33500952010-06-11 00:33:02 +0000578void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
579 DependentTemplateSpecializationTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
584 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
585 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000586 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
587 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000588}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000589void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
590 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
591}
John McCall51bd8032009-10-18 01:05:36 +0000592void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
593 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000594}
595void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
596 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
600 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000601}
John McCall54e14c42009-10-22 22:37:11 +0000602void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000604}
Eli Friedmanb001de72011-10-06 23:00:33 +0000605void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getKWLoc(), Record);
607 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
608 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
609}
John McCalla1ee0c52009-10-16 21:56:05 +0000610
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000611//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000612// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000613//===----------------------------------------------------------------------===//
614
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000615static void EmitBlockID(unsigned ID, const char *Name,
616 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000617 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000618 Record.clear();
619 Record.push_back(ID);
620 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
621
622 // Emit the block name if present.
623 if (Name == 0 || Name[0] == 0) return;
624 Record.clear();
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
628}
629
630static void EmitRecordID(unsigned ID, const char *Name,
631 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000632 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 Record.clear();
634 Record.push_back(ID);
635 while (*Name)
636 Record.push_back(*Name++);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000638}
639
640static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000641 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000642#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000643 RECORD(STMT_STOP);
644 RECORD(STMT_NULL_PTR);
645 RECORD(STMT_NULL);
646 RECORD(STMT_COMPOUND);
647 RECORD(STMT_CASE);
648 RECORD(STMT_DEFAULT);
649 RECORD(STMT_LABEL);
650 RECORD(STMT_IF);
651 RECORD(STMT_SWITCH);
652 RECORD(STMT_WHILE);
653 RECORD(STMT_DO);
654 RECORD(STMT_FOR);
655 RECORD(STMT_GOTO);
656 RECORD(STMT_INDIRECT_GOTO);
657 RECORD(STMT_CONTINUE);
658 RECORD(STMT_BREAK);
659 RECORD(STMT_RETURN);
660 RECORD(STMT_DECL);
661 RECORD(STMT_ASM);
662 RECORD(EXPR_PREDEFINED);
663 RECORD(EXPR_DECL_REF);
664 RECORD(EXPR_INTEGER_LITERAL);
665 RECORD(EXPR_FLOATING_LITERAL);
666 RECORD(EXPR_IMAGINARY_LITERAL);
667 RECORD(EXPR_STRING_LITERAL);
668 RECORD(EXPR_CHARACTER_LITERAL);
669 RECORD(EXPR_PAREN);
670 RECORD(EXPR_UNARY_OPERATOR);
671 RECORD(EXPR_SIZEOF_ALIGN_OF);
672 RECORD(EXPR_ARRAY_SUBSCRIPT);
673 RECORD(EXPR_CALL);
674 RECORD(EXPR_MEMBER);
675 RECORD(EXPR_BINARY_OPERATOR);
676 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
677 RECORD(EXPR_CONDITIONAL_OPERATOR);
678 RECORD(EXPR_IMPLICIT_CAST);
679 RECORD(EXPR_CSTYLE_CAST);
680 RECORD(EXPR_COMPOUND_LITERAL);
681 RECORD(EXPR_EXT_VECTOR_ELEMENT);
682 RECORD(EXPR_INIT_LIST);
683 RECORD(EXPR_DESIGNATED_INIT);
684 RECORD(EXPR_IMPLICIT_VALUE_INIT);
685 RECORD(EXPR_VA_ARG);
686 RECORD(EXPR_ADDR_LABEL);
687 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000688 RECORD(EXPR_CHOOSE);
689 RECORD(EXPR_GNU_NULL);
690 RECORD(EXPR_SHUFFLE_VECTOR);
691 RECORD(EXPR_BLOCK);
692 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000693 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000694 RECORD(EXPR_OBJC_STRING_LITERAL);
695 RECORD(EXPR_OBJC_ENCODE);
696 RECORD(EXPR_OBJC_SELECTOR_EXPR);
697 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
698 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
699 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
700 RECORD(EXPR_OBJC_KVC_REF_EXPR);
701 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000702 RECORD(STMT_OBJC_FOR_COLLECTION);
703 RECORD(STMT_OBJC_CATCH);
704 RECORD(STMT_OBJC_FINALLY);
705 RECORD(STMT_OBJC_AT_TRY);
706 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
707 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000708 RECORD(EXPR_CXX_OPERATOR_CALL);
709 RECORD(EXPR_CXX_CONSTRUCT);
710 RECORD(EXPR_CXX_STATIC_CAST);
711 RECORD(EXPR_CXX_DYNAMIC_CAST);
712 RECORD(EXPR_CXX_REINTERPRET_CAST);
713 RECORD(EXPR_CXX_CONST_CAST);
714 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
715 RECORD(EXPR_CXX_BOOL_LITERAL);
716 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000717 RECORD(EXPR_CXX_TYPEID_EXPR);
718 RECORD(EXPR_CXX_TYPEID_TYPE);
719 RECORD(EXPR_CXX_UUIDOF_EXPR);
720 RECORD(EXPR_CXX_UUIDOF_TYPE);
721 RECORD(EXPR_CXX_THIS);
722 RECORD(EXPR_CXX_THROW);
723 RECORD(EXPR_CXX_DEFAULT_ARG);
724 RECORD(EXPR_CXX_BIND_TEMPORARY);
725 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
726 RECORD(EXPR_CXX_NEW);
727 RECORD(EXPR_CXX_DELETE);
728 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
729 RECORD(EXPR_EXPR_WITH_CLEANUPS);
730 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
731 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
732 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
733 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
734 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
735 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
736 RECORD(EXPR_CXX_NOEXCEPT);
737 RECORD(EXPR_OPAQUE_VALUE);
738 RECORD(EXPR_BINARY_TYPE_TRAIT);
739 RECORD(EXPR_PACK_EXPANSION);
740 RECORD(EXPR_SIZEOF_PACK);
741 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000742 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000743#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000744}
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Sebastian Redla4232eb2010-08-18 23:56:21 +0000746void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000747 RecordData Record;
748 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000750#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
751#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Sebastian Redl3397c552010-08-18 23:56:27 +0000753 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000754 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000755 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000756 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000757 RECORD(TYPE_OFFSET);
758 RECORD(DECL_OFFSET);
759 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000760 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761 RECORD(IDENTIFIER_OFFSET);
762 RECORD(IDENTIFIER_TABLE);
763 RECORD(EXTERNAL_DEFINITIONS);
764 RECORD(SPECIAL_TYPES);
765 RECORD(STATISTICS);
766 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000767 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000768 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
769 RECORD(SELECTOR_OFFSETS);
770 RECORD(METHOD_POOL);
771 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000772 RECORD(SOURCE_LOCATION_OFFSETS);
773 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000774 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000775 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000776 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000777 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000778 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000779 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000780 RECORD(TU_UPDATE_LEXICAL);
781 RECORD(REDECLS_UPDATE_LATEST);
782 RECORD(SEMA_DECL_REFS);
783 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
784 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
785 RECORD(DECL_REPLACEMENTS);
786 RECORD(UPDATE_VISIBLE);
787 RECORD(DECL_UPDATE_OFFSETS);
788 RECORD(DECL_UPDATES);
789 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
790 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000791 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000792 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000793 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000794 RECORD(FP_PRAGMA_OPTIONS);
795 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000796 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000797 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
798 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000799 RECORD(MODULE_OFFSET_MAP);
800 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000801
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000802 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000803 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000804 RECORD(SM_SLOC_FILE_ENTRY);
805 RECORD(SM_SLOC_BUFFER_ENTRY);
806 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000807 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000809 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000810 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000811 RECORD(PP_MACRO_OBJECT_LIKE);
812 RECORD(PP_MACRO_FUNCTION_LIKE);
813 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000814
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000815 // Decls and Types block.
816 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000817 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000818 RECORD(TYPE_COMPLEX);
819 RECORD(TYPE_POINTER);
820 RECORD(TYPE_BLOCK_POINTER);
821 RECORD(TYPE_LVALUE_REFERENCE);
822 RECORD(TYPE_RVALUE_REFERENCE);
823 RECORD(TYPE_MEMBER_POINTER);
824 RECORD(TYPE_CONSTANT_ARRAY);
825 RECORD(TYPE_INCOMPLETE_ARRAY);
826 RECORD(TYPE_VARIABLE_ARRAY);
827 RECORD(TYPE_VECTOR);
828 RECORD(TYPE_EXT_VECTOR);
829 RECORD(TYPE_FUNCTION_PROTO);
830 RECORD(TYPE_FUNCTION_NO_PROTO);
831 RECORD(TYPE_TYPEDEF);
832 RECORD(TYPE_TYPEOF_EXPR);
833 RECORD(TYPE_TYPEOF);
834 RECORD(TYPE_RECORD);
835 RECORD(TYPE_ENUM);
836 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000837 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000838 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000839 RECORD(TYPE_DECLTYPE);
840 RECORD(TYPE_ELABORATED);
841 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
842 RECORD(TYPE_UNRESOLVED_USING);
843 RECORD(TYPE_INJECTED_CLASS_NAME);
844 RECORD(TYPE_OBJC_OBJECT);
845 RECORD(TYPE_TEMPLATE_TYPE_PARM);
846 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
847 RECORD(TYPE_DEPENDENT_NAME);
848 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
849 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
850 RECORD(TYPE_PAREN);
851 RECORD(TYPE_PACK_EXPANSION);
852 RECORD(TYPE_ATTRIBUTED);
853 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000854 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000855 RECORD(DECL_TYPEDEF);
856 RECORD(DECL_ENUM);
857 RECORD(DECL_RECORD);
858 RECORD(DECL_ENUM_CONSTANT);
859 RECORD(DECL_FUNCTION);
860 RECORD(DECL_OBJC_METHOD);
861 RECORD(DECL_OBJC_INTERFACE);
862 RECORD(DECL_OBJC_PROTOCOL);
863 RECORD(DECL_OBJC_IVAR);
864 RECORD(DECL_OBJC_AT_DEFS_FIELD);
865 RECORD(DECL_OBJC_CLASS);
866 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
867 RECORD(DECL_OBJC_CATEGORY);
868 RECORD(DECL_OBJC_CATEGORY_IMPL);
869 RECORD(DECL_OBJC_IMPLEMENTATION);
870 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
871 RECORD(DECL_OBJC_PROPERTY);
872 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000873 RECORD(DECL_FIELD);
874 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000875 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000876 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000877 RECORD(DECL_FILE_SCOPE_ASM);
878 RECORD(DECL_BLOCK);
879 RECORD(DECL_CONTEXT_LEXICAL);
880 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000881 RECORD(DECL_NAMESPACE);
882 RECORD(DECL_NAMESPACE_ALIAS);
883 RECORD(DECL_USING);
884 RECORD(DECL_USING_SHADOW);
885 RECORD(DECL_USING_DIRECTIVE);
886 RECORD(DECL_UNRESOLVED_USING_VALUE);
887 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
888 RECORD(DECL_LINKAGE_SPEC);
889 RECORD(DECL_CXX_RECORD);
890 RECORD(DECL_CXX_METHOD);
891 RECORD(DECL_CXX_CONSTRUCTOR);
892 RECORD(DECL_CXX_DESTRUCTOR);
893 RECORD(DECL_CXX_CONVERSION);
894 RECORD(DECL_ACCESS_SPEC);
895 RECORD(DECL_FRIEND);
896 RECORD(DECL_FRIEND_TEMPLATE);
897 RECORD(DECL_CLASS_TEMPLATE);
898 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
899 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
900 RECORD(DECL_FUNCTION_TEMPLATE);
901 RECORD(DECL_TEMPLATE_TYPE_PARM);
902 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
903 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
904 RECORD(DECL_STATIC_ASSERT);
905 RECORD(DECL_CXX_BASE_SPECIFIERS);
906 RECORD(DECL_INDIRECTFIELD);
907 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
908
Douglas Gregora72d8c42011-06-03 02:27:19 +0000909 // Statements and Exprs can occur in the Decls and Types block.
910 AddStmtsExprs(Stream, Record);
911
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000912 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000913 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000914 RECORD(PPD_MACRO_DEFINITION);
915 RECORD(PPD_INCLUSION_DIRECTIVE);
916
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000917#undef RECORD
918#undef BLOCK
919 Stream.ExitBlock();
920}
921
Douglas Gregore650c8c2009-07-07 00:12:59 +0000922/// \brief Adjusts the given filename to only write out the portion of the
923/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000924///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000925/// \param Filename the file name to adjust.
926///
927/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
928/// the returned filename will be adjusted by this system root.
929///
930/// \returns either the original filename (if it needs no adjustment) or the
931/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000932static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000933adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000934 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregor832d6202011-07-22 16:35:34 +0000936 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000937 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Douglas Gregore650c8c2009-07-07 00:12:59 +0000939 // Verify that the filename and the system root have the same prefix.
940 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000941 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000942 if (Filename[Pos] != isysroot[Pos])
943 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945 // We hit the end of the filename before we hit the end of the system root.
946 if (!Filename[Pos])
947 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Douglas Gregore650c8c2009-07-07 00:12:59 +0000949 // If the file name has a '/' at the current position, skip over the '/'.
950 // We distinguish sysroot-based includes from absolute includes by the
951 // absence of '/' at the beginning of sysroot-based includes.
952 if (Filename[Pos] == '/')
953 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955 return Filename + Pos;
956}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000957
Sebastian Redl3397c552010-08-18 23:56:27 +0000958/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000959void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000960 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000961 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000962
Douglas Gregore650c8c2009-07-07 00:12:59 +0000963 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000964 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000965 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000966 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000967 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
968 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
970 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
971 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000972 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000976 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000977 Record.push_back(VERSION_MAJOR);
978 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 Record.push_back(CLANG_VERSION_MAJOR);
980 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000981 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000982 const std::string &Triple = Target.getTriple().getTriple();
983 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
984
985 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000986 serialization::ModuleManager &Mgr = Chain->getModuleManager();
987 llvm::SmallVector<char, 128> ModulePaths;
988 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000989
990 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
991 M != MEnd; ++M) {
992 // Skip modules that weren't directly imported.
993 if (!(*M)->isDirectlyImported())
994 continue;
995
996 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
997 // FIXME: Write import location, once it matters.
998 // FIXME: This writes the absolute path for AST files we depend on.
999 const std::string &FileName = (*M)->FileName;
1000 Record.push_back(FileName.size());
1001 Record.append(FileName.begin(), FileName.end());
1002 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001003 Stream.EmitRecord(IMPORTS, Record);
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Douglas Gregor31d375f2011-05-06 21:43:30 +00001006 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001007 SourceManager &SM = Context.getSourceManager();
1008 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1009 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001010 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001011 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1012 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1013
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001014 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001016 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001017
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001018 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001019 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001020 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001021 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001022 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001023 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001024
1025 Record.clear();
1026 Record.push_back(SM.getMainFileID().getOpaqueValue());
1027 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001028 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001029
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001030 // Original PCH directory
1031 if (!OutputFile.empty() && OutputFile != "-") {
1032 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1033 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1035 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1036
1037 llvm::SmallString<128> OutputPath(OutputFile);
1038
1039 llvm::sys::fs::make_absolute(OutputPath);
1040 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1041
1042 RecordData Record;
1043 Record.push_back(ORIGINAL_PCH_DIR);
1044 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1045 }
1046
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001047 // Repository branch/version information.
1048 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001049 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001050 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1051 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001052 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001053 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001054 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1055 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001056}
1057
1058/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001059void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001060 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001061#define LANGOPT(Name, Bits, Default, Description) \
1062 Record.push_back(LangOpts.Name);
1063#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1064 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1065#include "clang/Basic/LangOptions.def"
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001066 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001067}
1068
Douglas Gregor14f79002009-04-10 03:52:48 +00001069//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001070// stat cache Serialization
1071//===----------------------------------------------------------------------===//
1072
1073namespace {
1074// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001075class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001076public:
1077 typedef const char * key_type;
1078 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Chris Lattner74e976b2010-11-23 19:28:12 +00001080 typedef struct stat data_type;
1081 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001082
1083 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001084 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
1087 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001088 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001089 data_type_ref Data) {
1090 unsigned StrLen = strlen(path);
1091 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001092 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001093 clang::io::Emit8(Out, DataLen);
1094 return std::make_pair(StrLen + 1, DataLen);
1095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Chris Lattner5f9e2722011-07-23 10:55:15 +00001097 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001098 Out.write(path, KeyLen);
1099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Chris Lattner5f9e2722011-07-23 10:55:15 +00001101 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102 data_type_ref Data, unsigned DataLen) {
1103 using namespace clang::io;
1104 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner74e976b2010-11-23 19:28:12 +00001106 Emit32(Out, (uint32_t) Data.st_ino);
1107 Emit32(Out, (uint32_t) Data.st_dev);
1108 Emit16(Out, (uint16_t) Data.st_mode);
1109 Emit64(Out, (uint64_t) Data.st_mtime);
1110 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001111
1112 assert(Out.tell() - Start == DataLen && "Wrong data length");
1113 }
1114};
1115} // end anonymous namespace
1116
Sebastian Redl3397c552010-08-18 23:56:27 +00001117/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001118void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001119 // Build the on-disk hash table containing information about every
1120 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001121 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001122 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001123 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001124 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001125 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001126 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001127 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001128 }
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001130 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001131 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001132 uint32_t BucketOffset;
1133 {
1134 llvm::raw_svector_ostream Out(StatCacheData);
1135 // Make sure that no bucket is at offset 0
1136 clang::io::Emit32(Out, 0);
1137 BucketOffset = Generator.Emit(Out);
1138 }
1139
1140 // Create a blob abbreviation
1141 using namespace llvm;
1142 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001143 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1147 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1148
1149 // Write the stat cache
1150 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001151 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001152 Record.push_back(BucketOffset);
1153 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001154 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001155}
1156
1157//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001158// Source Manager Serialization
1159//===----------------------------------------------------------------------===//
1160
1161/// \brief Create an abbreviation for the SLocEntry that refers to a
1162/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001163static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001164 using namespace llvm;
1165 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001166 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1169 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001171 // FileEntry fields.
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Douglas Gregor14f79002009-04-10 03:52:48 +00001175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001176 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001177}
1178
1179/// \brief Create an abbreviation for the SLocEntry that refers to a
1180/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001181static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001182 using namespace llvm;
1183 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001184 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001190 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001191}
1192
1193/// \brief Create an abbreviation for the SLocEntry that refers to a
1194/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001195static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001196 using namespace llvm;
1197 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001198 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001200 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001201}
1202
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001203/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1204/// expansion.
1205static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001206 using namespace llvm;
1207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001208 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001214 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001215}
1216
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001217namespace {
1218 // Trait used for the on-disk hash table of header search information.
1219 class HeaderFileInfoTrait {
1220 ASTWriter &Writer;
1221 HeaderSearch &HS;
1222
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001223 // Keep track of the framework names we've used during serialization.
1224 SmallVector<char, 128> FrameworkStringData;
1225 llvm::StringMap<unsigned> FrameworkNameOffset;
1226
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001227 public:
1228 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1229 : Writer(Writer), HS(HS) { }
1230
1231 typedef const char *key_type;
1232 typedef key_type key_type_ref;
1233
1234 typedef HeaderFileInfo data_type;
1235 typedef const data_type &data_type_ref;
1236
1237 static unsigned ComputeHash(const char *path) {
1238 // The hash is based only on the filename portion of the key, so that the
1239 // reader can match based on filenames when symlinking or excess path
1240 // elements ("foo/../", "../") change the form of the name. However,
1241 // complete path is still the key.
1242 return llvm::HashString(llvm::sys::path::filename(path));
1243 }
1244
1245 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001246 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001247 data_type_ref Data) {
1248 unsigned StrLen = strlen(path);
1249 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001250 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001251 clang::io::Emit8(Out, DataLen);
1252 return std::make_pair(StrLen + 1, DataLen);
1253 }
1254
Chris Lattner5f9e2722011-07-23 10:55:15 +00001255 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001256 Out.write(path, KeyLen);
1257 }
1258
Chris Lattner5f9e2722011-07-23 10:55:15 +00001259 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001260 data_type_ref Data, unsigned DataLen) {
1261 using namespace clang::io;
1262 uint64_t Start = Out.tell(); (void)Start;
1263
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001264 unsigned char Flags = (Data.isImport << 5)
1265 | (Data.isPragmaOnce << 4)
1266 | (Data.DirInfo << 2)
1267 | (Data.Resolved << 1)
1268 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001269 Emit8(Out, (uint8_t)Flags);
1270 Emit16(Out, (uint16_t) Data.NumIncludes);
1271
1272 if (!Data.ControllingMacro)
1273 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1274 else
1275 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001276
1277 unsigned Offset = 0;
1278 if (!Data.Framework.empty()) {
1279 // If this header refers into a framework, save the framework name.
1280 llvm::StringMap<unsigned>::iterator Pos
1281 = FrameworkNameOffset.find(Data.Framework);
1282 if (Pos == FrameworkNameOffset.end()) {
1283 Offset = FrameworkStringData.size() + 1;
1284 FrameworkStringData.append(Data.Framework.begin(),
1285 Data.Framework.end());
1286 FrameworkStringData.push_back(0);
1287
1288 FrameworkNameOffset[Data.Framework] = Offset;
1289 } else
1290 Offset = Pos->second;
1291 }
1292 Emit32(Out, Offset);
1293
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001294 assert(Out.tell() - Start == DataLen && "Wrong data length");
1295 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001296
1297 const char *strings_begin() const { return FrameworkStringData.begin(); }
1298 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001299 };
1300} // end anonymous namespace
1301
1302/// \brief Write the header search block for the list of files that
1303///
1304/// \param HS The header search structure to save.
1305///
1306/// \param Chain Whether we're creating a chained AST file.
Douglas Gregor832d6202011-07-22 16:35:34 +00001307void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001308 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001309 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1310
1311 if (FilesByUID.size() > HS.header_file_size())
1312 FilesByUID.resize(HS.header_file_size());
1313
1314 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1315 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001316 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001317 unsigned NumHeaderSearchEntries = 0;
1318 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1319 const FileEntry *File = FilesByUID[UID];
1320 if (!File)
1321 continue;
1322
1323 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1324 if (HFI.External && Chain)
1325 continue;
1326
1327 // Turn the file name into an absolute path, if it isn't already.
1328 const char *Filename = File->getName();
1329 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1330
1331 // If we performed any translation on the file name at all, we need to
1332 // save this string, since the generator will refer to it later.
1333 if (Filename != File->getName()) {
1334 Filename = strdup(Filename);
1335 SavedStrings.push_back(Filename);
1336 }
1337
1338 Generator.insert(Filename, HFI, GeneratorTrait);
1339 ++NumHeaderSearchEntries;
1340 }
1341
1342 // Create the on-disk hash table in a buffer.
1343 llvm::SmallString<4096> TableData;
1344 uint32_t BucketOffset;
1345 {
1346 llvm::raw_svector_ostream Out(TableData);
1347 // Make sure that no bucket is at offset 0
1348 clang::io::Emit32(Out, 0);
1349 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1350 }
1351
1352 // Create a blob abbreviation
1353 using namespace llvm;
1354 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1355 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1360 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1361
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001362 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001363 RecordData Record;
1364 Record.push_back(HEADER_SEARCH_TABLE);
1365 Record.push_back(BucketOffset);
1366 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001367 Record.push_back(TableData.size());
1368 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001369 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1370
1371 // Free all of the strings we had to duplicate.
1372 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1373 free((void*)SavedStrings[I]);
1374}
1375
Douglas Gregor14f79002009-04-10 03:52:48 +00001376/// \brief Writes the block containing the serialized form of the
1377/// source manager.
1378///
1379/// TODO: We should probably use an on-disk hash table (stored in a
1380/// blob), indexed based on the file name, so that we only create
1381/// entries for files that we actually need. In the common case (no
1382/// errors), we probably won't have to create file entries for any of
1383/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001384void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001385 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001386 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001387 RecordData Record;
1388
Chris Lattnerf04ad692009-04-10 17:16:57 +00001389 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001390 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001391
1392 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001393 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1394 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1395 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001396 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001397
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001398 // Write out the source location entry table. We skip the first
1399 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001400 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001401 // Write out the offsets of only source location file entries.
1402 // We will go through them in ASTReader::validateFileEntries().
1403 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001404 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001405 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1406 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001407 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001408 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001409 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001410
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001411 // Record the offset of this source-location entry.
1412 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1413
1414 // Figure out which record code to use.
1415 unsigned Code;
1416 if (SLoc->isFile()) {
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001417 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001418 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001419 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1420 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001421 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001422 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001423 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001424 Record.clear();
1425 Record.push_back(Code);
1426
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001427 // Starting offset of this entry within this module, so skip the dummy.
1428 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001429 if (SLoc->isFile()) {
1430 const SrcMgr::FileInfo &File = SLoc->getFile();
1431 Record.push_back(File.getIncludeLoc().getRawEncoding());
1432 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1433 Record.push_back(File.hasLineDirectives());
1434
1435 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001436 if (Content->OrigEntry) {
1437 assert(Content->OrigEntry == Content->ContentsEntry &&
1438 "Writing to AST an overriden file is not supported");
1439
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001440 // The source location entry is a file. The blob associated
1441 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Douglas Gregor2d52be52010-03-21 22:49:54 +00001443 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001444 Record.push_back(Content->OrigEntry->getSize());
1445 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001446
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001447 Record.push_back(File.NumCreatedFIDs);
1448
Douglas Gregore650c8c2009-07-07 00:12:59 +00001449 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001450 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001451 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001452
1453 // Ask the file manager to fixup the relative path for us. This will
1454 // honor the working directory.
1455 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1456
1457 // FIXME: This call to make_absolute shouldn't be necessary, the
1458 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001459 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001460 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregore650c8c2009-07-07 00:12:59 +00001462 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001463 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001464 } else {
1465 // The source location entry is a buffer. The blob associated
1466 // with this entry contains the contents of the buffer.
1467
1468 // We add one to the size so that we capture the trailing NULL
1469 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1470 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001471 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001472 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001473 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001474 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001475 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001476 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001477 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001478 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001479 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001480 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001481
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001482 if (strcmp(Name, "<built-in>") == 0) {
1483 PreloadSLocs.push_back(SLocEntryOffsets.size());
1484 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001485 }
1486 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001487 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001488 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001489 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1490 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001491 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1492 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001493
1494 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001495 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001496 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001497 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001498 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001499 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001500 }
1501 }
1502
Douglas Gregorc9490c02009-04-16 22:23:12 +00001503 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001504
1505 if (SLocEntryOffsets.empty())
1506 return;
1507
Sebastian Redl3397c552010-08-18 23:56:27 +00001508 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001509 // table is used for lazily loading source-location information.
1510 using namespace llvm;
1511 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001512 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001515 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1516 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001518 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001519 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001520 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001521 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001522 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001524 Abbrev = new BitCodeAbbrev();
1525 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1528 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1529
1530 Record.clear();
1531 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1532 Record.push_back(SLocFileEntryOffsets.size());
1533 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1534 data(SLocFileEntryOffsets));
1535
Sebastian Redl3397c552010-08-18 23:56:27 +00001536 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001537 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001538 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001539
1540 // Write the line table. It depends on remapping working, so it must come
1541 // after the source location offsets.
1542 if (SourceMgr.hasLineTable()) {
1543 LineTableInfo &LineTable = SourceMgr.getLineTable();
1544
1545 Record.clear();
1546 // Emit the file names
1547 Record.push_back(LineTable.getNumFilenames());
1548 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1549 // Emit the file name
1550 const char *Filename = LineTable.getFilename(I);
1551 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1552 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1553 Record.push_back(FilenameLen);
1554 if (FilenameLen)
1555 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1556 }
1557
1558 // Emit the line entries
1559 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1560 L != LEnd; ++L) {
1561 // Only emit entries for local files.
1562 if (L->first < 0)
1563 continue;
1564
1565 // Emit the file ID
1566 Record.push_back(L->first);
1567
1568 // Emit the line entries
1569 Record.push_back(L->second.size());
1570 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1571 LEEnd = L->second.end();
1572 LE != LEEnd; ++LE) {
1573 Record.push_back(LE->FileOffset);
1574 Record.push_back(LE->LineNo);
1575 Record.push_back(LE->FilenameID);
1576 Record.push_back((unsigned)LE->FileKind);
1577 Record.push_back(LE->IncludeOffset);
1578 }
1579 }
1580 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1581 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001582}
1583
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001584//===----------------------------------------------------------------------===//
1585// Preprocessor Serialization
1586//===----------------------------------------------------------------------===//
1587
Douglas Gregor9c736102011-02-10 18:20:09 +00001588static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1589 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1590 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1591 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1592 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1593 return X.first->getName().compare(Y.first->getName());
1594}
1595
Chris Lattner0b1fb982009-04-10 17:15:23 +00001596/// \brief Writes the block containing the serialized form of the
1597/// preprocessor.
1598///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001599void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001600 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1601 if (PPRec)
1602 WritePreprocessorDetail(*PPRec);
1603
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001604 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001605
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001606 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1607 if (PP.getCounterValue() != 0) {
1608 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001609 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001610 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001611 }
1612
1613 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001614 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Sebastian Redl3397c552010-08-18 23:56:27 +00001616 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001617 // FIXME: use diagnostics subsystem for localization etc.
1618 if (PP.SawDateOrTime())
1619 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Douglas Gregorecdcb882010-10-20 22:00:55 +00001621
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001622 // Loop over all the macro definitions that are live at the end of the file,
1623 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001624
Douglas Gregor9c736102011-02-10 18:20:09 +00001625 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001626 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001627 MacrosToEmit;
1628 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001629 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1630 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001631 I != E; ++I) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00001632 if (!IsModule || I->second->isExported()) {
1633 MacroDefinitionsSeen.insert(I->first);
1634 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1635 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001636 }
1637
1638 // Sort the set of macro definitions that need to be serialized by the
1639 // name of the macro, to provide a stable ordering.
1640 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1641 &compareMacroDefinitions);
1642
Douglas Gregor040a8042011-02-11 00:26:14 +00001643 // Resolve any identifiers that defined macros at the time they were
1644 // deserialized, adding them to the list of macros to emit (if appropriate).
1645 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1646 IdentifierInfo *Name
1647 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1648 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1649 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1650 }
1651
Douglas Gregor9c736102011-02-10 18:20:09 +00001652 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1653 const IdentifierInfo *Name = MacrosToEmit[I].first;
1654 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001655 if (!MI)
1656 continue;
1657
Sebastian Redl3397c552010-08-18 23:56:27 +00001658 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001659 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001660 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001661
1662 // FIXME: There is a (probably minor) optimization we could do here, if
1663 // the macro comes from the original PCH but the identifier comes from a
1664 // chained PCH, by storing the offset into the original PCH rather than
1665 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001666 if (MI->isBuiltinMacro() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00001667 (Chain && Name->isFromAST() && MI->isFromAST() &&
1668 !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001669 continue;
1670
Douglas Gregor9c736102011-02-10 18:20:09 +00001671 AddIdentifierRef(Name, Record);
1672 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001673 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1674 Record.push_back(MI->isUsed());
Douglas Gregor7143aab2011-09-01 17:04:32 +00001675 AddSourceLocation(MI->getExportLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001676 unsigned Code;
1677 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001678 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001679 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001680 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001682 Record.push_back(MI->isC99Varargs());
1683 Record.push_back(MI->isGNUVarargs());
1684 Record.push_back(MI->getNumArgs());
1685 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1686 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001687 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001688 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001689
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001690 // If we have a detailed preprocessing record, record the macro definition
1691 // ID that corresponds to this macro.
1692 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001693 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001694
Douglas Gregorc9490c02009-04-16 22:23:12 +00001695 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001696 Record.clear();
1697
Chris Lattnerdf961c22009-04-10 18:08:30 +00001698 // Emit the tokens array.
1699 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1700 // Note that we know that the preprocessor does not have any annotation
1701 // tokens in it because they are created by the parser, and thus can't be
1702 // in a macro definition.
1703 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Chris Lattnerdf961c22009-04-10 18:08:30 +00001705 Record.push_back(Tok.getLocation().getRawEncoding());
1706 Record.push_back(Tok.getLength());
1707
Chris Lattnerdf961c22009-04-10 18:08:30 +00001708 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1709 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001710 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001711 // FIXME: Should translate token kind to a stable encoding.
1712 Record.push_back(Tok.getKind());
1713 // FIXME: Should translate token flags to a stable encoding.
1714 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001716 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001717 Record.clear();
1718 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001719 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001720 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001721 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001722}
1723
1724void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001725 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001726 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001727
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001728 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001729
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001730 // Enter the preprocessor block.
1731 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001732
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001733 // If the preprocessor has a preprocessing record, emit it.
1734 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001735 using namespace llvm;
1736
1737 // Set up the abbreviation for
1738 unsigned InclusionAbbrev = 0;
1739 {
1740 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1741 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1746 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1747 }
1748
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001749 unsigned FirstPreprocessorEntityID
1750 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1751 + NUM_PREDEF_PP_ENTITY_IDS;
1752 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001753 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001754 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1755 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001756 E != EEnd;
1757 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001758 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001759
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001760 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1761 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001762
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001763 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001764 // Record this macro definition's ID.
1765 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001766
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001767 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001768 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1769 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001770 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001771
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001772 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001773 Record.push_back(ME->isBuiltinMacro());
1774 if (ME->isBuiltinMacro())
1775 AddIdentifierRef(ME->getName(), Record);
1776 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001777 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001778 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001779 continue;
1780 }
1781
1782 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1783 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001784 Record.push_back(ID->getFileName().size());
1785 Record.push_back(ID->wasInQuotes());
1786 Record.push_back(static_cast<unsigned>(ID->getKind()));
1787 llvm::SmallString<64> Buffer;
1788 Buffer += ID->getFileName();
1789 Buffer += ID->getFile()->getName();
1790 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1791 continue;
1792 }
1793
1794 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1795 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001796 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001797
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001798 // Write the offsets table for the preprocessing record.
1799 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001800 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1801
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001802 // Write the offsets table for identifier IDs.
1803 using namespace llvm;
1804 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001805 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001808 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001809
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001810 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001811 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001812 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001813 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1814 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001815 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001816}
1817
David Blaikied6471f72011-09-25 23:23:43 +00001818void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001819 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00001820 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001821 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1822 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00001823 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001824 if (point.Loc.isInvalid())
1825 continue;
1826
1827 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00001828 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001829 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00001830 if (I->second.isPragma()) {
1831 Record.push_back(I->first);
1832 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001833 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001834 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001835 Record.push_back(-1); // mark the end of the diag/map pairs for this
1836 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001837 }
1838
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001839 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001840 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001841}
1842
Anders Carlssonc8505782011-03-06 18:41:18 +00001843void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1844 if (CXXBaseSpecifiersOffsets.empty())
1845 return;
1846
1847 RecordData Record;
1848
1849 // Create a blob abbreviation for the C++ base specifiers offsets.
1850 using namespace llvm;
1851
1852 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1853 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1854 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1855 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1856 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1857
Douglas Gregore92b8a12011-08-04 00:01:48 +00001858 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00001859 Record.clear();
1860 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1861 Record.push_back(CXXBaseSpecifiersOffsets.size());
1862 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001863 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00001864}
1865
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001866//===----------------------------------------------------------------------===//
1867// Type Serialization
1868//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001869
Sebastian Redl3397c552010-08-18 23:56:27 +00001870/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001871void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001872 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001873 if (Idx.getIndex() == 0) // we haven't seen this type before.
1874 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Douglas Gregor97475832010-10-05 18:37:06 +00001876 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001877
Douglas Gregor2cf26342009-04-09 22:27:44 +00001878 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001879 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001880 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001881 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001882 else if (TypeOffsets.size() < Index) {
1883 TypeOffsets.resize(Index + 1);
1884 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001885 }
1886
1887 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Douglas Gregor2cf26342009-04-09 22:27:44 +00001889 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001890 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001891
Douglas Gregora4923eb2009-11-16 21:35:15 +00001892 if (T.hasLocalNonFastQualifiers()) {
1893 Qualifiers Qs = T.getLocalQualifiers();
1894 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001895 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001896 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001897 } else {
1898 switch (T->getTypeClass()) {
1899 // For all of the concrete, non-dependent types, call the
1900 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001901#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001902 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001903#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001904#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001905 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001906 }
1907
1908 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001909 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001910
1911 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001912 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001913}
1914
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001915//===----------------------------------------------------------------------===//
1916// Declaration Serialization
1917//===----------------------------------------------------------------------===//
1918
Douglas Gregor2cf26342009-04-09 22:27:44 +00001919/// \brief Write the block containing all of the declaration IDs
1920/// lexically declared within the given DeclContext.
1921///
1922/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1923/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001924uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001925 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001926 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001927 return 0;
1928
Douglas Gregorc9490c02009-04-16 22:23:12 +00001929 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001930 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001931 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001932 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001933 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1934 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001935 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001936
Douglas Gregor25123082009-04-22 22:34:57 +00001937 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001938 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001939 return Offset;
1940}
1941
Sebastian Redla4232eb2010-08-18 23:56:21 +00001942void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001943 using namespace llvm;
1944 RecordData Record;
1945
1946 // Write the type offsets array
1947 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001948 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00001950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00001951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1952 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1953 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001954 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001955 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00001956 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001957 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001958
1959 // Write the declaration offsets array
1960 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001961 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00001963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00001964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1965 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1966 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001967 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001968 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00001969 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001970 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001971}
1972
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001973//===----------------------------------------------------------------------===//
1974// Global Method Pool and Selector Serialization
1975//===----------------------------------------------------------------------===//
1976
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001977namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001978// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001979class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001980 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001981
1982public:
1983 typedef Selector key_type;
1984 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Sebastian Redl5d050072010-08-04 17:20:04 +00001986 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001987 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001988 ObjCMethodList Instance, Factory;
1989 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001990 typedef const data_type& data_type_ref;
1991
Sebastian Redl3397c552010-08-18 23:56:27 +00001992 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001994 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001995 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001996 }
Mike Stump1eb44332009-09-09 15:08:12 +00001997
1998 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001999 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002000 data_type_ref Methods) {
2001 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2002 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002003 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2004 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002005 Method = Method->Next)
2006 if (Method->Method)
2007 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002008 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002009 Method = Method->Next)
2010 if (Method->Method)
2011 DataLen += 4;
2012 clang::io::Emit16(Out, DataLen);
2013 return std::make_pair(KeyLen, DataLen);
2014 }
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Chris Lattner5f9e2722011-07-23 10:55:15 +00002016 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002017 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002018 assert((Start >> 32) == 0 && "Selector key offset too large");
2019 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002020 unsigned N = Sel.getNumArgs();
2021 clang::io::Emit16(Out, N);
2022 if (N == 0)
2023 N = 1;
2024 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002025 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002026 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2027 }
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Chris Lattner5f9e2722011-07-23 10:55:15 +00002029 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002030 data_type_ref Methods, unsigned DataLen) {
2031 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002032 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002033 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002034 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002035 Method = Method->Next)
2036 if (Method->Method)
2037 ++NumInstanceMethods;
2038
2039 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002040 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002041 Method = Method->Next)
2042 if (Method->Method)
2043 ++NumFactoryMethods;
2044
2045 clang::io::Emit16(Out, NumInstanceMethods);
2046 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002047 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002048 Method = Method->Next)
2049 if (Method->Method)
2050 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002051 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002052 Method = Method->Next)
2053 if (Method->Method)
2054 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002055
2056 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002057 }
2058};
2059} // end anonymous namespace
2060
Sebastian Redl059612d2010-08-03 21:58:15 +00002061/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002062///
2063/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002064/// in an on-disk hash table indexed by the selector. The hash table also
2065/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002066void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002067 using namespace llvm;
2068
Sebastian Redl059612d2010-08-03 21:58:15 +00002069 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002070 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002071 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002072 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002073 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002074 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002075 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002076 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Sebastian Redl059612d2010-08-03 21:58:15 +00002078 // Create the on-disk hash table representation. We walk through every
2079 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002080 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002081 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002082 I = SelectorIDs.begin(), E = SelectorIDs.end();
2083 I != E; ++I) {
2084 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002085 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002086 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002087 I->second,
2088 ObjCMethodList(),
2089 ObjCMethodList()
2090 };
2091 if (F != SemaRef.MethodPool.end()) {
2092 Data.Instance = F->second.first;
2093 Data.Factory = F->second.second;
2094 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002095 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002096 // changed.
2097 if (Chain && I->second < FirstSelectorID) {
2098 // Selector already exists. Did it change?
2099 bool changed = false;
2100 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2101 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002102 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002103 changed = true;
2104 }
2105 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2106 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002107 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002108 changed = true;
2109 }
2110 if (!changed)
2111 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002112 } else if (Data.Instance.Method || Data.Factory.Method) {
2113 // A new method pool entry.
2114 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002115 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002116 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002117 }
2118
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002119 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002120 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002121 uint32_t BucketOffset;
2122 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002123 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002124 llvm::raw_svector_ostream Out(MethodPool);
2125 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002126 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002127 BucketOffset = Generator.Emit(Out, Trait);
2128 }
2129
2130 // Create a blob abbreviation
2131 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002132 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2136 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2137
Douglas Gregor83941df2009-04-25 17:48:32 +00002138 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002139 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002141 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002142 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002143 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002144
2145 // Create a blob abbreviation for the selector table offsets.
2146 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002147 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2151 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2152
2153 // Write the selector offsets table.
2154 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002155 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002156 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002157 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002158 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002159 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002160 }
2161}
2162
Sebastian Redl3397c552010-08-18 23:56:27 +00002163/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002164void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002165 using namespace llvm;
2166 if (SemaRef.ReferencedSelectors.empty())
2167 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002168
Fariborz Jahanian32019832010-07-23 19:11:11 +00002169 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002170
Sebastian Redl3397c552010-08-18 23:56:27 +00002171 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002172 // very tricky to fix, and given that @selector shouldn't really appear in
2173 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002174 for (DenseMap<Selector, SourceLocation>::iterator S =
2175 SemaRef.ReferencedSelectors.begin(),
2176 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2177 Selector Sel = (*S).first;
2178 SourceLocation Loc = (*S).second;
2179 AddSelectorRef(Sel, Record);
2180 AddSourceLocation(Loc, Record);
2181 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002182 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002183}
2184
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002185//===----------------------------------------------------------------------===//
2186// Identifier Table Serialization
2187//===----------------------------------------------------------------------===//
2188
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002189namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002190class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002191 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002192 Preprocessor &PP;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002193 bool IsModule;
2194
Douglas Gregora92193e2009-04-28 21:18:29 +00002195 /// \brief Determines whether this is an "interesting" identifier
2196 /// that needs a full IdentifierInfo structure written into the hash
2197 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002198 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002199 if (II->isPoisoned() ||
2200 II->isExtensionToken() ||
2201 II->getObjCOrBuiltinID() ||
2202 II->getFETokenInfo<void>())
2203 return true;
2204
Douglas Gregorce835df2011-09-14 22:14:14 +00002205 return hasMacroDefinition(II, Macro);
2206 }
2207
2208 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002209 if (!II->hasMacroDefinition())
2210 return false;
2211
Douglas Gregorce835df2011-09-14 22:14:14 +00002212 if (Macro || (Macro = PP.getMacroInfo(II)))
2213 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isExported());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002214
Douglas Gregorce835df2011-09-14 22:14:14 +00002215 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002216 }
2217
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002218public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002219 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002220 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002221
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002222 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002223 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002224
Douglas Gregor7143aab2011-09-01 17:04:32 +00002225 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP, bool IsModule)
2226 : Writer(Writer), PP(PP), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002227
2228 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002229 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002230 }
Mike Stump1eb44332009-09-09 15:08:12 +00002231
2232 std::pair<unsigned,unsigned>
Douglas Gregor7143aab2011-09-01 17:04:32 +00002233 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002234 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002235 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002236 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002237 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002238 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002239 if (hasMacroDefinition(II, Macro))
Douglas Gregor5998da52009-04-28 21:32:13 +00002240 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002241 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2242 DEnd = IdentifierResolver::end();
2243 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002244 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002245 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002246 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002247 // We emit the key length after the data length so that every
2248 // string is preceded by a 16-bit length. This matches the PTH
2249 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002250 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002251 return std::make_pair(KeyLen, DataLen);
2252 }
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Chris Lattner5f9e2722011-07-23 10:55:15 +00002254 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002255 unsigned KeyLen) {
2256 // Record the location of the key data. This is used when generating
2257 // the mapping from persistent IDs to strings.
2258 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002259 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002260 }
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Douglas Gregor7143aab2011-09-01 17:04:32 +00002262 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002263 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002264 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002265 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002266 clang::io::Emit32(Out, ID << 1);
2267 return;
2268 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002269
Douglas Gregora92193e2009-04-28 21:18:29 +00002270 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002271 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002272 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002273 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregorce835df2011-09-14 22:14:14 +00002274 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002275 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2276 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002277 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002278 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002279 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002280
Douglas Gregorce835df2011-09-14 22:14:14 +00002281 if (HasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002282 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002283
Douglas Gregor668c1a42009-04-21 22:25:48 +00002284 // Emit the declaration IDs in reverse order, because the
2285 // IdentifierResolver provides the declarations as they would be
2286 // visible (e.g., the function "stat" would come before the struct
2287 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2288 // adds declarations to the end of the list (so we need to see the
2289 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002290 // Only emit declarations that aren't from a chained PCH, though.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002291 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002292 IdentifierResolver::end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002293 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002294 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002295 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002296 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002297 }
2298};
2299} // end anonymous namespace
2300
Sebastian Redl3397c552010-08-18 23:56:27 +00002301/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002302///
2303/// The identifier table consists of a blob containing string data
2304/// (the actual identifiers themselves) and a separate "offsets" index
2305/// that maps identifier IDs to locations within the blob.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002306void ASTWriter::WriteIdentifierTable(Preprocessor &PP, bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002307 using namespace llvm;
2308
2309 // Create and write out the blob that contains the identifier
2310 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002311 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002312 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002313 ASTIdentifierTableTrait Trait(*this, PP, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Douglas Gregor92b059e2009-04-28 20:33:11 +00002315 // Look for any identifiers that were named while processing the
2316 // headers, but are otherwise not needed. We add these to the hash
2317 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002318 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002319 // file.
2320 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2321 IDEnd = PP.getIdentifierTable().end();
2322 ID != IDEnd; ++ID)
2323 getIdentifierRef(ID->second);
2324
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002325 // Create the on-disk hash table representation. We only store offsets
2326 // for identifiers that appear here for the first time.
2327 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002328 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002329 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2330 ID != IDEnd; ++ID) {
2331 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002332 if (!Chain || !ID->first->isFromAST())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002333 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2334 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002335 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002336
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002337 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002338 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002339 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002340 {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002341 ASTIdentifierTableTrait Trait(*this, PP, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002342 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002343 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002344 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002345 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002346 }
2347
2348 // Create a blob abbreviation
2349 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002350 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002353 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002354
2355 // Write the identifier table
2356 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002357 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002358 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002359 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002360 }
2361
2362 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002363 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002364 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2368 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2369
2370 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002371 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002372 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002373 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002374 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002375 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002376}
2377
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002378//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002379// DeclContext's Name Lookup Table Serialization
2380//===----------------------------------------------------------------------===//
2381
2382namespace {
2383// Trait used for the on-disk hash table used in the method pool.
2384class ASTDeclContextNameLookupTrait {
2385 ASTWriter &Writer;
2386
2387public:
2388 typedef DeclarationName key_type;
2389 typedef key_type key_type_ref;
2390
2391 typedef DeclContext::lookup_result data_type;
2392 typedef const data_type& data_type_ref;
2393
2394 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2395
2396 unsigned ComputeHash(DeclarationName Name) {
2397 llvm::FoldingSetNodeID ID;
2398 ID.AddInteger(Name.getNameKind());
2399
2400 switch (Name.getNameKind()) {
2401 case DeclarationName::Identifier:
2402 ID.AddString(Name.getAsIdentifierInfo()->getName());
2403 break;
2404 case DeclarationName::ObjCZeroArgSelector:
2405 case DeclarationName::ObjCOneArgSelector:
2406 case DeclarationName::ObjCMultiArgSelector:
2407 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2408 break;
2409 case DeclarationName::CXXConstructorName:
2410 case DeclarationName::CXXDestructorName:
2411 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002412 break;
2413 case DeclarationName::CXXOperatorName:
2414 ID.AddInteger(Name.getCXXOverloadedOperator());
2415 break;
2416 case DeclarationName::CXXLiteralOperatorName:
2417 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2418 case DeclarationName::CXXUsingDirective:
2419 break;
2420 }
2421
2422 return ID.ComputeHash();
2423 }
2424
2425 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002426 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002427 data_type_ref Lookup) {
2428 unsigned KeyLen = 1;
2429 switch (Name.getNameKind()) {
2430 case DeclarationName::Identifier:
2431 case DeclarationName::ObjCZeroArgSelector:
2432 case DeclarationName::ObjCOneArgSelector:
2433 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002434 case DeclarationName::CXXLiteralOperatorName:
2435 KeyLen += 4;
2436 break;
2437 case DeclarationName::CXXOperatorName:
2438 KeyLen += 1;
2439 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002440 case DeclarationName::CXXConstructorName:
2441 case DeclarationName::CXXDestructorName:
2442 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002443 case DeclarationName::CXXUsingDirective:
2444 break;
2445 }
2446 clang::io::Emit16(Out, KeyLen);
2447
2448 // 2 bytes for num of decls and 4 for each DeclID.
2449 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2450 clang::io::Emit16(Out, DataLen);
2451
2452 return std::make_pair(KeyLen, DataLen);
2453 }
2454
Chris Lattner5f9e2722011-07-23 10:55:15 +00002455 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002456 using namespace clang::io;
2457
2458 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2459 Emit8(Out, Name.getNameKind());
2460 switch (Name.getNameKind()) {
2461 case DeclarationName::Identifier:
2462 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2463 break;
2464 case DeclarationName::ObjCZeroArgSelector:
2465 case DeclarationName::ObjCOneArgSelector:
2466 case DeclarationName::ObjCMultiArgSelector:
2467 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2468 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002469 case DeclarationName::CXXOperatorName:
2470 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2471 Emit8(Out, Name.getCXXOverloadedOperator());
2472 break;
2473 case DeclarationName::CXXLiteralOperatorName:
2474 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2475 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002476 case DeclarationName::CXXConstructorName:
2477 case DeclarationName::CXXDestructorName:
2478 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002479 case DeclarationName::CXXUsingDirective:
2480 break;
2481 }
2482 }
2483
Chris Lattner5f9e2722011-07-23 10:55:15 +00002484 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002485 data_type Lookup, unsigned DataLen) {
2486 uint64_t Start = Out.tell(); (void)Start;
2487 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2488 for (; Lookup.first != Lookup.second; ++Lookup.first)
2489 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2490
2491 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2492 }
2493};
2494} // end anonymous namespace
2495
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002496/// \brief Write the block containing all of the declaration IDs
2497/// visible from the given DeclContext.
2498///
2499/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002500/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002501uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2502 DeclContext *DC) {
2503 if (DC->getPrimaryContext() != DC)
2504 return 0;
2505
2506 // Since there is no name lookup into functions or methods, don't bother to
2507 // build a visible-declarations table for these entities.
2508 if (DC->isFunctionOrMethod())
2509 return 0;
2510
2511 // If not in C++, we perform name lookup for the translation unit via the
2512 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2513 // FIXME: In C++ we need the visible declarations in order to "see" the
2514 // friend declarations, is there a way to do this without writing the table ?
2515 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2516 return 0;
2517
2518 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002519 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002520 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002521
2522 // Serialize the contents of the mapping used for lookup. Note that,
2523 // although we have two very different code paths, the serialized
2524 // representation is the same for both cases: a declaration name,
2525 // followed by a size, followed by references to the visible
2526 // declarations that have that name.
2527 uint64_t Offset = Stream.GetCurrentBitNo();
2528 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2529 if (!Map || Map->empty())
2530 return 0;
2531
2532 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2533 ASTDeclContextNameLookupTrait Trait(*this);
2534
2535 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002536 DeclarationName ConversionName;
2537 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002538 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2539 D != DEnd; ++D) {
2540 DeclarationName Name = D->first;
2541 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002542 if (Result.first != Result.second) {
2543 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2544 // Hash all conversion function names to the same name. The actual
2545 // type information in conversion function name is not used in the
2546 // key (since such type information is not stable across different
2547 // modules), so the intended effect is to coalesce all of the conversion
2548 // functions under a single key.
2549 if (!ConversionName)
2550 ConversionName = Name;
2551 ConversionDecls.append(Result.first, Result.second);
2552 continue;
2553 }
2554
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002555 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002556 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002557 }
2558
Douglas Gregore5a54b62011-08-30 20:49:19 +00002559 // Add the conversion functions
2560 if (!ConversionDecls.empty()) {
2561 Generator.insert(ConversionName,
2562 DeclContext::lookup_result(ConversionDecls.begin(),
2563 ConversionDecls.end()),
2564 Trait);
2565 }
2566
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002567 // Create the on-disk hash table in a buffer.
2568 llvm::SmallString<4096> LookupTable;
2569 uint32_t BucketOffset;
2570 {
2571 llvm::raw_svector_ostream Out(LookupTable);
2572 // Make sure that no bucket is at offset 0
2573 clang::io::Emit32(Out, 0);
2574 BucketOffset = Generator.Emit(Out, Trait);
2575 }
2576
2577 // Write the lookup table
2578 RecordData Record;
2579 Record.push_back(DECL_CONTEXT_VISIBLE);
2580 Record.push_back(BucketOffset);
2581 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2582 LookupTable.str());
2583
2584 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2585 ++NumVisibleDeclContexts;
2586 return Offset;
2587}
2588
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002589/// \brief Write an UPDATE_VISIBLE block for the given context.
2590///
2591/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2592/// DeclContext in a dependent AST file. As such, they only exist for the TU
2593/// (in C++) and for namespaces.
2594void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002595 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2596 if (!Map || Map->empty())
2597 return;
2598
2599 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2600 ASTDeclContextNameLookupTrait Trait(*this);
2601
2602 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002603 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2604 D != DEnd; ++D) {
2605 DeclarationName Name = D->first;
2606 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002607 // For any name that appears in this table, the results are complete, i.e.
2608 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002609 if (Result.first != Result.second)
2610 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002611 }
2612
2613 // Create the on-disk hash table in a buffer.
2614 llvm::SmallString<4096> LookupTable;
2615 uint32_t BucketOffset;
2616 {
2617 llvm::raw_svector_ostream Out(LookupTable);
2618 // Make sure that no bucket is at offset 0
2619 clang::io::Emit32(Out, 0);
2620 BucketOffset = Generator.Emit(Out, Trait);
2621 }
2622
2623 // Write the lookup table
2624 RecordData Record;
2625 Record.push_back(UPDATE_VISIBLE);
2626 Record.push_back(getDeclID(cast<Decl>(DC)));
2627 Record.push_back(BucketOffset);
2628 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2629}
2630
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002631/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2632void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2633 RecordData Record;
2634 Record.push_back(Opts.fp_contract);
2635 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2636}
2637
2638/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2639void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2640 if (!SemaRef.Context.getLangOptions().OpenCL)
2641 return;
2642
2643 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2644 RecordData Record;
2645#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2646#include "clang/Basic/OpenCLExtensions.def"
2647 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2648}
2649
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002650//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002651// General Serialization Routines
2652//===----------------------------------------------------------------------===//
2653
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002654/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002655void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002656 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002657 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2658 const Attr * A = *i;
2659 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002660 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002661
Sean Huntcf807c42010-08-18 23:23:40 +00002662#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002663
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002664 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002665}
2666
Chris Lattner5f9e2722011-07-23 10:55:15 +00002667void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002668 Record.push_back(Str.size());
2669 Record.insert(Record.end(), Str.begin(), Str.end());
2670}
2671
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002672void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2673 RecordDataImpl &Record) {
2674 Record.push_back(Version.getMajor());
2675 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2676 Record.push_back(*Minor + 1);
2677 else
2678 Record.push_back(0);
2679 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2680 Record.push_back(*Subminor + 1);
2681 else
2682 Record.push_back(0);
2683}
2684
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002685/// \brief Note that the identifier II occurs at the given offset
2686/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002687void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002688 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002689 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002690 // up earlier in the chain and thus don't need an offset.
2691 if (ID >= FirstIdentID)
2692 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002693}
2694
Douglas Gregor83941df2009-04-25 17:48:32 +00002695/// \brief Note that the selector Sel occurs at the given offset
2696/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002697void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002698 unsigned ID = SelectorIDs[Sel];
2699 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002700 // Don't record offsets for selectors that are also available in a different
2701 // file.
2702 if (ID < FirstSelectorID)
2703 return;
2704 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002705}
2706
Sebastian Redla4232eb2010-08-18 23:56:21 +00002707ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Argyrios Kyrtzidis92ddef12011-09-19 20:40:48 +00002708 : Stream(Stream), Context(0), Chain(0), WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002709 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002710 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002711 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002712 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002713 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002714 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002715 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00002716 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002717 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002718 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2719 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2720 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002721 DeclTypedefAbbrev(0),
2722 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2723 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00002724{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002725}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002726
Sebastian Redla4232eb2010-08-18 23:56:21 +00002727void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002728 const std::string &OutputFile,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002729 bool IsModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00002730 WritingAST = true;
2731
Douglas Gregor2cf26342009-04-09 22:27:44 +00002732 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002733 Stream.Emit((unsigned)'C', 8);
2734 Stream.Emit((unsigned)'P', 8);
2735 Stream.Emit((unsigned)'C', 8);
2736 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002737
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002738 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002739
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002740 Context = &SemaRef.Context;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002741 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, IsModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002742 Context = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00002743
2744 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002745}
2746
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002747template<typename Vector>
2748static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2749 ASTWriter::RecordData &Record) {
2750 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2751 I != E; ++I) {
2752 Writer.AddDeclRef(*I, Record);
2753 }
2754}
2755
Sebastian Redla4232eb2010-08-18 23:56:21 +00002756void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00002757 StringRef isysroot,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002758 const std::string &OutputFile, bool IsModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002759 using namespace llvm;
2760
2761 ASTContext &Context = SemaRef.Context;
2762 Preprocessor &PP = SemaRef.PP;
2763
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002764 // Set up predefined declaration IDs.
2765 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00002766 if (Context.ObjCIdDecl)
2767 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00002768 if (Context.ObjCSelDecl)
2769 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00002770 if (Context.ObjCClassDecl)
2771 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00002772 if (Context.Int128Decl)
2773 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
2774 if (Context.UInt128Decl)
2775 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00002776 if (Context.ObjCInstanceTypeDecl)
2777 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00002778
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002779 if (!Chain) {
2780 // Make sure that we emit IdentifierInfos (and any attached
2781 // declarations) for builtins. We don't need to do this when we're
2782 // emitting chained PCH files, because all of the builtins will be
2783 // in the original PCH file.
2784 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00002785 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002786 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002787 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2788 Context.getLangOptions().NoBuiltin);
2789 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2790 getIdentifierRef(&Table.get(BuiltinNames[I]));
2791 }
2792
Chris Lattner63d65f82009-09-08 18:19:27 +00002793 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002794 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002795 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002796 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002797 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00002798
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002799 // Build a record containing all of the file scoped decls in this file.
2800 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002801 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
2802 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002803
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002804 // Build a record containing all of the delegating constructors we still need
2805 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00002806 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00002807 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00002808
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002809 // Write the set of weak, undeclared identifiers. We always write the
2810 // entire table, since later PCH files in a PCH chain are only interested in
2811 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002812 RecordData WeakUndeclaredIdentifiers;
2813 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00002814 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002815 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2816 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2817 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2818 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2819 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2820 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2821 }
2822 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002823
Douglas Gregor14c22f22009-04-22 22:18:58 +00002824 // Build a record containing all of the locally-scoped external
2825 // declarations in this header file. Generally, this record will be
2826 // empty.
2827 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002828 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002829 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002830 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002831 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2832 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00002833 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002834 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00002835 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2836 }
2837
Douglas Gregorb81c1702009-04-27 20:06:05 +00002838 // Build a record containing all of the ext_vector declarations.
2839 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00002840 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002841
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002842 // Build a record containing all of the VTable uses information.
2843 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002844 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002845 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2846 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2847 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2848 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2849 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002850 }
2851
2852 // Build a record containing all of dynamic classes declarations.
2853 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00002854 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002855
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002856 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002857 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002858 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002859 I = SemaRef.PendingInstantiations.begin(),
2860 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2861 AddDeclRef(I->first, PendingInstantiations);
2862 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002863 }
2864 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2865 "There are local ones at end of translation unit!");
2866
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002867 // Build a record containing some declaration references.
2868 RecordData SemaDeclRefs;
2869 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2870 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2871 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2872 }
2873
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002874 RecordData CUDASpecialDeclRefs;
2875 if (Context.getcudaConfigureCallDecl()) {
2876 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2877 }
2878
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002879 // Build a record containing all of the known namespaces.
2880 RecordData KnownNamespaces;
2881 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2882 I = SemaRef.KnownNamespaces.begin(),
2883 IEnd = SemaRef.KnownNamespaces.end();
2884 I != IEnd; ++I) {
2885 if (!I->second)
2886 AddDeclRef(I->first, KnownNamespaces);
2887 }
2888
Sebastian Redl3397c552010-08-18 23:56:27 +00002889 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002890 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002891 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002892 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002893 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00002894 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00002895 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002896 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Douglas Gregor69a9e012011-08-01 16:54:33 +00002897
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002898 if (Chain) {
2899 // Write the mapping information describing our module dependencies and how
2900 // each of those modules were mapped into our own offset/ID space, so that
2901 // the reader can build the appropriate mapping to its own offset/ID space.
2902 // The map consists solely of a blob with the following format:
2903 // *(module-name-len:i16 module-name:len*i8
2904 // source-location-offset:i32
2905 // identifier-id:i32
2906 // preprocessed-entity-id:i32
2907 // macro-definition-id:i32
2908 // selector-id:i32
2909 // declaration-id:i32
2910 // c++-base-specifiers-id:i32
2911 // type-id:i32)
2912 //
2913 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2914 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
2915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2916 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
2917 llvm::SmallString<2048> Buffer;
2918 {
2919 llvm::raw_svector_ostream Out(Buffer);
2920 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
2921 MEnd = Chain->ModuleMgr.end();
2922 M != MEnd; ++M) {
2923 StringRef FileName = (*M)->FileName;
2924 io::Emit16(Out, FileName.size());
2925 Out.write(FileName.data(), FileName.size());
2926 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
2927 io::Emit32(Out, (*M)->BaseIdentifierID);
2928 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002929 io::Emit32(Out, (*M)->BaseSelectorID);
2930 io::Emit32(Out, (*M)->BaseDeclID);
2931 io::Emit32(Out, (*M)->BaseTypeIndex);
2932 }
2933 }
2934 Record.clear();
2935 Record.push_back(MODULE_OFFSET_MAP);
2936 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
2937 Buffer.data(), Buffer.size());
2938 }
2939
2940 // Create a lexical update block containing all of the declarations in the
2941 // translation unit that do not come from other AST files.
2942 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
2943 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
2944 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2945 E = TU->noload_decls_end();
2946 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002947 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002948 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
2949 else if ((*I)->isChangedSinceDeserialization())
2950 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
2951 }
2952
2953 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
2954 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
2955 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2956 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2957 Record.clear();
2958 Record.push_back(TU_UPDATE_LEXICAL);
2959 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2960 data(NewGlobalDecls));
2961
2962 // And a visible updates block for the translation unit.
2963 Abv = new llvm::BitCodeAbbrev();
2964 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2965 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2966 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2967 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2968 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2969 WriteDeclContextVisibleUpdate(TU);
2970
2971 // If the translation unit has an anonymous namespace, and we don't already
2972 // have an update block for it, write it as an update block.
2973 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
2974 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
2975 if (Record.empty()) {
2976 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00002977 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002978 }
2979 }
2980
Douglas Gregor61c5e342011-09-17 00:05:03 +00002981 // Resolve any declaration pointers within the declaration updates block and
2982 // chained Objective-C categories block to declaration IDs.
2983 ResolveDeclUpdatesBlocks();
2984 ResolveChainedObjCCategories();
2985
Douglas Gregora119da02011-08-02 16:26:37 +00002986 // Form the record of special types.
2987 RecordData SpecialTypes;
2988 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor30403a62011-08-11 22:04:35 +00002989 AddTypeRef(Context.ObjCProtoType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00002990 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00002991 AddTypeRef(Context.getFILEType(), SpecialTypes);
2992 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
2993 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
2994 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
2995 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00002996 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002997
Douglas Gregor366809a2009-04-26 03:49:13 +00002998 // Keep writing types and declarations until all types and
2999 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003000 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003001 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003002 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3003 E = DeclsToRewrite.end();
3004 I != E; ++I)
3005 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003006 while (!DeclTypesToEmit.empty()) {
3007 DeclOrType DOT = DeclTypesToEmit.front();
3008 DeclTypesToEmit.pop();
3009 if (DOT.isType())
3010 WriteType(DOT.getType());
3011 else
3012 WriteDecl(Context, DOT.getDecl());
3013 }
3014 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003015
Douglas Gregor7143aab2011-09-01 17:04:32 +00003016 WritePreprocessor(PP, IsModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003017 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003018 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003019 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor7143aab2011-09-01 17:04:32 +00003020 WriteIdentifierTable(PP, IsModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003021 WriteFPPragmaOptions(SemaRef.getFPOptions());
3022 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003023
Sebastian Redl1476ed42010-07-16 16:36:56 +00003024 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003025 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003026
Anders Carlssonc8505782011-03-06 18:41:18 +00003027 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003028
Douglas Gregora119da02011-08-02 16:26:37 +00003029 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3030
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003031 /// Build a record containing first declarations from a chained PCH and the
3032 /// most recent declarations in this AST that they point to.
3033 RecordData FirstLatestDeclIDs;
3034 for (FirstLatestDeclMap::iterator I = FirstLatestDecls.begin(),
3035 E = FirstLatestDecls.end();
3036 I != E; ++I) {
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003037 AddDeclRef(I->first, FirstLatestDeclIDs);
3038 AddDeclRef(I->second, FirstLatestDeclIDs);
3039 }
3040
3041 if (!FirstLatestDeclIDs.empty())
3042 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
3043
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003044 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003045 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003046 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003047
3048 // Write the record containing tentative definitions.
3049 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003050 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003051
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003052 // Write the record containing unused file scoped decls.
3053 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003054 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003055
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003056 // Write the record containing weak undeclared identifiers.
3057 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003058 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003059 WeakUndeclaredIdentifiers);
3060
Douglas Gregor14c22f22009-04-22 22:18:58 +00003061 // Write the record containing locally-scoped external definitions.
3062 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003063 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003064 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003065
3066 // Write the record containing ext_vector type names.
3067 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003068 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003069
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003070 // Write the record containing VTable uses information.
3071 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003072 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003073
3074 // Write the record containing dynamic classes declarations.
3075 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003076 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003077
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003078 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003079 if (!PendingInstantiations.empty())
3080 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003081
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003082 // Write the record containing declaration references of Sema.
3083 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003084 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003085
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003086 // Write the record containing CUDA-specific declaration references.
3087 if (!CUDASpecialDeclRefs.empty())
3088 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003089
3090 // Write the delegating constructors.
3091 if (!DelegatingCtorDecls.empty())
3092 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003093
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003094 // Write the known namespaces.
3095 if (!KnownNamespaces.empty())
3096 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3097
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003098 // Write the visible updates to DeclContexts.
3099 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3100 I = UpdatedDeclContexts.begin(),
3101 E = UpdatedDeclContexts.end();
3102 I != E; ++I)
3103 WriteDeclContextVisibleUpdate(*I);
3104
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003105 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003106 WriteDeclReplacementsBlock();
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003107 WriteChainedObjCCategories();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003108
Douglas Gregor3e1af842009-04-17 22:13:46 +00003109 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003110 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003111 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003112 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003113 Record.push_back(NumLexicalDeclContexts);
3114 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003115 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003116 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003117}
3118
Douglas Gregor61c5e342011-09-17 00:05:03 +00003119/// \brief Go through the declaration update blocks and resolve declaration
3120/// pointers into declaration IDs.
3121void ASTWriter::ResolveDeclUpdatesBlocks() {
3122 for (DeclUpdateMap::iterator
3123 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3124 const Decl *D = I->first;
3125 UpdateRecord &URec = I->second;
3126
3127 if (DeclsToRewrite.count(D))
3128 continue; // The decl will be written completely
3129
3130 unsigned Idx = 0, N = URec.size();
3131 while (Idx < N) {
3132 switch ((DeclUpdateKind)URec[Idx++]) {
3133 case UPD_CXX_SET_DEFINITIONDATA:
3134 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3135 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3136 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3137 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3138 ++Idx;
3139 break;
3140
3141 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3142 ++Idx;
3143 break;
3144 }
3145 }
3146 }
3147}
3148
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003149void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003150 if (DeclUpdates.empty())
3151 return;
3152
3153 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003154 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003155 for (DeclUpdateMap::iterator
3156 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3157 const Decl *D = I->first;
3158 UpdateRecord &URec = I->second;
3159
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003160 if (DeclsToRewrite.count(D))
3161 continue; // The decl will be written completely,no need to store updates.
3162
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003163 uint64_t Offset = Stream.GetCurrentBitNo();
3164 Stream.EmitRecord(DECL_UPDATES, URec);
3165
3166 OffsetsRecord.push_back(GetDeclRef(D));
3167 OffsetsRecord.push_back(Offset);
3168 }
3169 Stream.ExitBlock();
3170 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3171}
3172
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003173void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003174 if (ReplacedDecls.empty())
3175 return;
3176
3177 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003178 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003179 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3180 Record.push_back(I->first);
3181 Record.push_back(I->second);
3182 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003183 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003184}
3185
Douglas Gregor61c5e342011-09-17 00:05:03 +00003186void ASTWriter::ResolveChainedObjCCategories() {
3187 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3188 I = LocalChainedObjCCategories.begin(),
3189 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3190 ChainedObjCCategoriesData &Data = *I;
3191 Data.InterfaceID = GetDeclRef(Data.Interface);
3192 Data.TailCategoryID = GetDeclRef(Data.TailCategory);
3193 }
3194
3195}
3196
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003197void ASTWriter::WriteChainedObjCCategories() {
3198 if (LocalChainedObjCCategories.empty())
3199 return;
3200
3201 RecordData Record;
3202 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3203 I = LocalChainedObjCCategories.begin(),
3204 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3205 ChainedObjCCategoriesData &Data = *I;
3206 serialization::DeclID
3207 HeadCatID = getDeclID(Data.Interface->getCategoryList());
3208 assert(HeadCatID != 0 && "Category not written ?");
3209
3210 Record.push_back(Data.InterfaceID);
3211 Record.push_back(HeadCatID);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003212 Record.push_back(Data.TailCategoryID);
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003213 }
3214 Stream.EmitRecord(OBJC_CHAINED_CATEGORIES, Record);
3215}
3216
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003217void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003218 Record.push_back(Loc.getRawEncoding());
3219}
3220
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003221void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003222 AddSourceLocation(Range.getBegin(), Record);
3223 AddSourceLocation(Range.getEnd(), Record);
3224}
3225
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003226void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003227 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003228 const uint64_t *Words = Value.getRawData();
3229 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003230}
3231
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003232void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003233 Record.push_back(Value.isUnsigned());
3234 AddAPInt(Value, Record);
3235}
3236
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003237void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003238 AddAPInt(Value.bitcastToAPInt(), Record);
3239}
3240
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003241void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003242 Record.push_back(getIdentifierRef(II));
3243}
3244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003245IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003246 if (II == 0)
3247 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003248
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003249 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003250 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003251 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003252 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003253}
3254
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003255void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003256 Record.push_back(getSelectorRef(SelRef));
3257}
3258
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003259SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003260 if (Sel.getAsOpaquePtr() == 0) {
3261 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003262 }
3263
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003264 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003265 if (SID == 0 && Chain) {
3266 // This might trigger a ReadSelector callback, which will set the ID for
3267 // this selector.
3268 Chain->LoadSelector(Sel);
3269 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003270 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003271 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003272 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003273 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003274}
3275
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003276void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003277 AddDeclRef(Temp->getDestructor(), Record);
3278}
3279
Douglas Gregor7c789c12010-10-29 22:39:52 +00003280void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3281 CXXBaseSpecifier const *BasesEnd,
3282 RecordDataImpl &Record) {
3283 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3284 CXXBaseSpecifiersToWrite.push_back(
3285 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3286 Bases, BasesEnd));
3287 Record.push_back(NextCXXBaseSpecifiersID++);
3288}
3289
Sebastian Redla4232eb2010-08-18 23:56:21 +00003290void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003291 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003292 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003293 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003294 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003295 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003296 break;
3297 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003298 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003299 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003300 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003301 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003302 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003303 break;
3304 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003305 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003306 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003307 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003308 break;
John McCall833ca992009-10-29 08:12:44 +00003309 case TemplateArgument::Null:
3310 case TemplateArgument::Integral:
3311 case TemplateArgument::Declaration:
3312 case TemplateArgument::Pack:
3313 break;
3314 }
3315}
3316
Sebastian Redla4232eb2010-08-18 23:56:21 +00003317void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003318 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003319 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003320
3321 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3322 bool InfoHasSameExpr
3323 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3324 Record.push_back(InfoHasSameExpr);
3325 if (InfoHasSameExpr)
3326 return; // Avoid storing the same expr twice.
3327 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003328 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3329 Record);
3330}
3331
Douglas Gregordc355712011-02-25 00:36:19 +00003332void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3333 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003334 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003335 AddTypeRef(QualType(), Record);
3336 return;
3337 }
3338
Douglas Gregordc355712011-02-25 00:36:19 +00003339 AddTypeLoc(TInfo->getTypeLoc(), Record);
3340}
3341
3342void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3343 AddTypeRef(TL.getType(), Record);
3344
John McCalla1ee0c52009-10-16 21:56:05 +00003345 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003346 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003347 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003348}
3349
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003350void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003351 Record.push_back(GetOrCreateTypeID(T));
3352}
3353
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003354TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3355 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003356 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3357}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003358
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003359TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003360 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003361 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003362}
3363
3364TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3365 if (T.isNull())
3366 return TypeIdx();
3367 assert(!T.getLocalFastQualifiers());
3368
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003369 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003370 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003371 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003372 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003373 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003374 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003375 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003376 return Idx;
3377}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003378
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003379TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003380 if (T.isNull())
3381 return TypeIdx();
3382 assert(!T.getLocalFastQualifiers());
3383
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003384 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3385 assert(I != TypeIdxs.end() && "Type not emitted!");
3386 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003387}
3388
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003389void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003390 Record.push_back(GetDeclRef(D));
3391}
3392
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003393DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003394 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3395
Douglas Gregor2cf26342009-04-09 22:27:44 +00003396 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003397 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003398 }
Douglas Gregor97475832010-10-05 18:37:06 +00003399 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003400 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003401 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003402 // We haven't seen this declaration before. Give it a new ID and
3403 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003404 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003405 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003406 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3407 // We don't add it to the replacement collection here, because we don't
3408 // have the offset yet.
3409 DeclTypesToEmit.push(const_cast<Decl *>(D));
3410 // Reset the flag, so that we don't add this decl multiple times.
3411 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003412 }
3413
Sebastian Redl681d7232010-07-27 00:17:23 +00003414 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003415}
3416
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003417DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003418 if (D == 0)
3419 return 0;
3420
3421 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3422 return DeclIDs[D];
3423}
3424
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003425void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003426 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003427 Record.push_back(Name.getNameKind());
3428 switch (Name.getNameKind()) {
3429 case DeclarationName::Identifier:
3430 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3431 break;
3432
3433 case DeclarationName::ObjCZeroArgSelector:
3434 case DeclarationName::ObjCOneArgSelector:
3435 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003436 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003437 break;
3438
3439 case DeclarationName::CXXConstructorName:
3440 case DeclarationName::CXXDestructorName:
3441 case DeclarationName::CXXConversionFunctionName:
3442 AddTypeRef(Name.getCXXNameType(), Record);
3443 break;
3444
3445 case DeclarationName::CXXOperatorName:
3446 Record.push_back(Name.getCXXOverloadedOperator());
3447 break;
3448
Sean Hunt3e518bd2009-11-29 07:34:05 +00003449 case DeclarationName::CXXLiteralOperatorName:
3450 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3451 break;
3452
Douglas Gregor2cf26342009-04-09 22:27:44 +00003453 case DeclarationName::CXXUsingDirective:
3454 // No extra data to emit
3455 break;
3456 }
3457}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003458
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003459void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003460 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003461 switch (Name.getNameKind()) {
3462 case DeclarationName::CXXConstructorName:
3463 case DeclarationName::CXXDestructorName:
3464 case DeclarationName::CXXConversionFunctionName:
3465 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3466 break;
3467
3468 case DeclarationName::CXXOperatorName:
3469 AddSourceLocation(
3470 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3471 Record);
3472 AddSourceLocation(
3473 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3474 Record);
3475 break;
3476
3477 case DeclarationName::CXXLiteralOperatorName:
3478 AddSourceLocation(
3479 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3480 Record);
3481 break;
3482
3483 case DeclarationName::Identifier:
3484 case DeclarationName::ObjCZeroArgSelector:
3485 case DeclarationName::ObjCOneArgSelector:
3486 case DeclarationName::ObjCMultiArgSelector:
3487 case DeclarationName::CXXUsingDirective:
3488 break;
3489 }
3490}
3491
3492void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003493 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003494 AddDeclarationName(NameInfo.getName(), Record);
3495 AddSourceLocation(NameInfo.getLoc(), Record);
3496 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3497}
3498
3499void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003500 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003501 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003502 Record.push_back(Info.NumTemplParamLists);
3503 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3504 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3505}
3506
Sebastian Redla4232eb2010-08-18 23:56:21 +00003507void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003508 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003509 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003510 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003511 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003512
3513 // Push each of the NNS's onto a stack for serialization in reverse order.
3514 while (NNS) {
3515 NestedNames.push_back(NNS);
3516 NNS = NNS->getPrefix();
3517 }
3518
3519 Record.push_back(NestedNames.size());
3520 while(!NestedNames.empty()) {
3521 NNS = NestedNames.pop_back_val();
3522 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3523 Record.push_back(Kind);
3524 switch (Kind) {
3525 case NestedNameSpecifier::Identifier:
3526 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3527 break;
3528
3529 case NestedNameSpecifier::Namespace:
3530 AddDeclRef(NNS->getAsNamespace(), Record);
3531 break;
3532
Douglas Gregor14aba762011-02-24 02:36:08 +00003533 case NestedNameSpecifier::NamespaceAlias:
3534 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3535 break;
3536
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003537 case NestedNameSpecifier::TypeSpec:
3538 case NestedNameSpecifier::TypeSpecWithTemplate:
3539 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3540 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3541 break;
3542
3543 case NestedNameSpecifier::Global:
3544 // Don't need to write an associated value.
3545 break;
3546 }
3547 }
3548}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003549
Douglas Gregordc355712011-02-25 00:36:19 +00003550void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3551 RecordDataImpl &Record) {
3552 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003553 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003554 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00003555
3556 // Push each of the nested-name-specifiers's onto a stack for
3557 // serialization in reverse order.
3558 while (NNS) {
3559 NestedNames.push_back(NNS);
3560 NNS = NNS.getPrefix();
3561 }
3562
3563 Record.push_back(NestedNames.size());
3564 while(!NestedNames.empty()) {
3565 NNS = NestedNames.pop_back_val();
3566 NestedNameSpecifier::SpecifierKind Kind
3567 = NNS.getNestedNameSpecifier()->getKind();
3568 Record.push_back(Kind);
3569 switch (Kind) {
3570 case NestedNameSpecifier::Identifier:
3571 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3572 AddSourceRange(NNS.getLocalSourceRange(), Record);
3573 break;
3574
3575 case NestedNameSpecifier::Namespace:
3576 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3577 AddSourceRange(NNS.getLocalSourceRange(), Record);
3578 break;
3579
3580 case NestedNameSpecifier::NamespaceAlias:
3581 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3582 AddSourceRange(NNS.getLocalSourceRange(), Record);
3583 break;
3584
3585 case NestedNameSpecifier::TypeSpec:
3586 case NestedNameSpecifier::TypeSpecWithTemplate:
3587 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3588 AddTypeLoc(NNS.getTypeLoc(), Record);
3589 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3590 break;
3591
3592 case NestedNameSpecifier::Global:
3593 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3594 break;
3595 }
3596 }
3597}
3598
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003599void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003600 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003601 Record.push_back(Kind);
3602 switch (Kind) {
3603 case TemplateName::Template:
3604 AddDeclRef(Name.getAsTemplateDecl(), Record);
3605 break;
3606
3607 case TemplateName::OverloadedTemplate: {
3608 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3609 Record.push_back(OvT->size());
3610 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3611 I != E; ++I)
3612 AddDeclRef(*I, Record);
3613 break;
3614 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003615
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003616 case TemplateName::QualifiedTemplate: {
3617 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3618 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3619 Record.push_back(QualT->hasTemplateKeyword());
3620 AddDeclRef(QualT->getTemplateDecl(), Record);
3621 break;
3622 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003623
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003624 case TemplateName::DependentTemplate: {
3625 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3626 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3627 Record.push_back(DepT->isIdentifier());
3628 if (DepT->isIdentifier())
3629 AddIdentifierRef(DepT->getIdentifier(), Record);
3630 else
3631 Record.push_back(DepT->getOperator());
3632 break;
3633 }
John McCall14606042011-06-30 08:33:18 +00003634
3635 case TemplateName::SubstTemplateTemplateParm: {
3636 SubstTemplateTemplateParmStorage *subst
3637 = Name.getAsSubstTemplateTemplateParm();
3638 AddDeclRef(subst->getParameter(), Record);
3639 AddTemplateName(subst->getReplacement(), Record);
3640 break;
3641 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003642
3643 case TemplateName::SubstTemplateTemplateParmPack: {
3644 SubstTemplateTemplateParmPackStorage *SubstPack
3645 = Name.getAsSubstTemplateTemplateParmPack();
3646 AddDeclRef(SubstPack->getParameterPack(), Record);
3647 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3648 break;
3649 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003650 }
3651}
3652
Michael J. Spencer20249a12010-10-21 03:16:25 +00003653void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003654 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003655 Record.push_back(Arg.getKind());
3656 switch (Arg.getKind()) {
3657 case TemplateArgument::Null:
3658 break;
3659 case TemplateArgument::Type:
3660 AddTypeRef(Arg.getAsType(), Record);
3661 break;
3662 case TemplateArgument::Declaration:
3663 AddDeclRef(Arg.getAsDecl(), Record);
3664 break;
3665 case TemplateArgument::Integral:
3666 AddAPSInt(*Arg.getAsIntegral(), Record);
3667 AddTypeRef(Arg.getIntegralType(), Record);
3668 break;
3669 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003670 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3671 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003672 case TemplateArgument::TemplateExpansion:
3673 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003674 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3675 Record.push_back(*NumExpansions + 1);
3676 else
3677 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003678 break;
3679 case TemplateArgument::Expression:
3680 AddStmt(Arg.getAsExpr());
3681 break;
3682 case TemplateArgument::Pack:
3683 Record.push_back(Arg.pack_size());
3684 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3685 I != E; ++I)
3686 AddTemplateArgument(*I, Record);
3687 break;
3688 }
3689}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003690
3691void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003692ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003693 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003694 assert(TemplateParams && "No TemplateParams!");
3695 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3696 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3697 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3698 Record.push_back(TemplateParams->size());
3699 for (TemplateParameterList::const_iterator
3700 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3701 P != PEnd; ++P)
3702 AddDeclRef(*P, Record);
3703}
3704
3705/// \brief Emit a template argument list.
3706void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003707ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003708 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003709 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003710 Record.push_back(TemplateArgs->size());
3711 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003712 AddTemplateArgument(TemplateArgs->get(i), Record);
3713}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003714
3715
3716void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003717ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003718 Record.push_back(Set.size());
3719 for (UnresolvedSetImpl::const_iterator
3720 I = Set.begin(), E = Set.end(); I != E; ++I) {
3721 AddDeclRef(I.getDecl(), Record);
3722 Record.push_back(I.getAccess());
3723 }
3724}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003725
Sebastian Redla4232eb2010-08-18 23:56:21 +00003726void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003727 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003728 Record.push_back(Base.isVirtual());
3729 Record.push_back(Base.isBaseOfClass());
3730 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003731 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003732 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003733 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003734 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3735 : SourceLocation(),
3736 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003737}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003738
Douglas Gregor7c789c12010-10-29 22:39:52 +00003739void ASTWriter::FlushCXXBaseSpecifiers() {
3740 RecordData Record;
3741 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3742 Record.clear();
3743
3744 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00003745 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003746 if (Index == CXXBaseSpecifiersOffsets.size())
3747 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3748 else {
3749 if (Index > CXXBaseSpecifiersOffsets.size())
3750 CXXBaseSpecifiersOffsets.resize(Index + 1);
3751 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3752 }
3753
3754 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3755 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3756 Record.push_back(BEnd - B);
3757 for (; B != BEnd; ++B)
3758 AddCXXBaseSpecifier(*B, Record);
3759 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003760
3761 // Flush any expressions that were written as part of the base specifiers.
3762 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003763 }
3764
3765 CXXBaseSpecifiersToWrite.clear();
3766}
3767
Sean Huntcbb67482011-01-08 20:30:50 +00003768void ASTWriter::AddCXXCtorInitializers(
3769 const CXXCtorInitializer * const *CtorInitializers,
3770 unsigned NumCtorInitializers,
3771 RecordDataImpl &Record) {
3772 Record.push_back(NumCtorInitializers);
3773 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3774 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003775
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003776 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00003777 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003778 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3779 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00003780 } else if (Init->isDelegatingInitializer()) {
3781 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3782 AddDeclRef(Init->getTargetConstructor(), Record);
3783 } else if (Init->isMemberInitializer()){
3784 Record.push_back(CTOR_INITIALIZER_MEMBER);
3785 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003786 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00003787 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3788 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003789 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003790
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003791 AddSourceLocation(Init->getMemberLocation(), Record);
3792 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003793 AddSourceLocation(Init->getLParenLoc(), Record);
3794 AddSourceLocation(Init->getRParenLoc(), Record);
3795 Record.push_back(Init->isWritten());
3796 if (Init->isWritten()) {
3797 Record.push_back(Init->getSourceOrder());
3798 } else {
3799 Record.push_back(Init->getNumArrayIndices());
3800 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3801 AddDeclRef(Init->getArrayIndex(i), Record);
3802 }
3803 }
3804}
3805
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003806void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3807 assert(D->DefinitionData);
3808 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3809 Record.push_back(Data.UserDeclaredConstructor);
3810 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00003811 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003812 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00003813 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003814 Record.push_back(Data.UserDeclaredDestructor);
3815 Record.push_back(Data.Aggregate);
3816 Record.push_back(Data.PlainOldData);
3817 Record.push_back(Data.Empty);
3818 Record.push_back(Data.Polymorphic);
3819 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00003820 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00003821 Record.push_back(Data.HasNoNonEmptyBases);
3822 Record.push_back(Data.HasPrivateFields);
3823 Record.push_back(Data.HasProtectedFields);
3824 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00003825 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00003826 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00003827 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003828 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003829 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003830 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003831 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003832 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003833 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003834 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00003835 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003836 Record.push_back(Data.DeclaredDefaultConstructor);
3837 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00003838 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003839 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00003840 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003841 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00003842 Record.push_back(Data.FailedImplicitMoveConstructor);
3843 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003844
3845 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003846 if (Data.NumBases > 0)
3847 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3848 Record);
3849
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003850 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3851 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003852 if (Data.NumVBases > 0)
3853 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3854 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003855
3856 AddUnresolvedSet(Data.Conversions, Record);
3857 AddUnresolvedSet(Data.VisibleConversions, Record);
3858 // Data.Definition is the owning decl, no need to write it.
3859 AddDeclRef(Data.FirstFriend, Record);
3860}
3861
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003862void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003863 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00003864 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003865 assert(FirstDeclID == NextDeclID &&
3866 FirstTypeID == NextTypeID &&
3867 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003868 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003869 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003870
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003871 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003872
Douglas Gregor10bc00f2011-08-18 04:12:04 +00003873 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
3874 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
3875 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
3876 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003877 NextDeclID = FirstDeclID;
3878 NextTypeID = FirstTypeID;
3879 NextIdentID = FirstIdentID;
3880 NextSelectorID = FirstSelectorID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003881}
3882
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003883void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003884 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003885 if (II->hasMacroDefinition())
3886 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003887}
3888
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003889void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003890 // Always take the highest-numbered type index. This copes with an interesting
3891 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003892 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003893 // keep the higher-numbered entry so that we can properly write it out to
3894 // the AST file.
3895 TypeIdx &StoredIdx = TypeIdxs[T];
3896 if (Idx.getIndex() >= StoredIdx.getIndex())
3897 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003898}
3899
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003900void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003901 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003902}
Sebastian Redl5d050072010-08-04 17:20:04 +00003903
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003904void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003905 SelectorIDs[S] = ID;
3906}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003907
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003908void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003909 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003910 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00003911 MacroDefinitions[MD] = ID;
3912}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003913
3914void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00003915 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00003916 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003917 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3918 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00003919 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003920 // A forward reference was mutated into a definition. Rewrite it.
3921 // FIXME: This happens during template instantiation, should we
3922 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003923 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003924 }
3925
3926 for (CXXRecordDecl::redecl_iterator
3927 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3928 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3929 if (Redecl == RD)
3930 continue;
3931
3932 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00003933 if (Redecl->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003934 UpdateRecord &Record = DeclUpdates[Redecl];
3935 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3936 assert(Redecl->DefinitionData);
3937 assert(Redecl->DefinitionData->Definition == D);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003938 Record.push_back(reinterpret_cast<uint64_t>(D)); // the DefinitionDecl
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003939 }
3940 }
3941 }
3942}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003943void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003944 assert(!WritingAST && "Already writing the AST!");
3945
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003946 // TU and namespaces are handled elsewhere.
3947 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3948 return;
3949
Douglas Gregor919814d2011-09-09 23:01:35 +00003950 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003951 return; // Not a source decl added to a DeclContext from PCH.
3952
3953 AddUpdatedDeclContext(DC);
3954}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003955
3956void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003957 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003958 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00003959 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003960 return; // Not a source member added to a class from PCH.
3961 if (!isa<CXXMethodDecl>(D))
3962 return; // We are interested in lazily declared implicit methods.
3963
3964 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00003965 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003966 UpdateRecord &Record = DeclUpdates[RD];
3967 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003968 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003969}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003970
3971void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3972 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003973 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003974 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003975 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00003976 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003977 return; // Not a source specialization added to a template from PCH.
3978
3979 UpdateRecord &Record = DeclUpdates[TD];
3980 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003981 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003982}
Douglas Gregor89d99802010-11-30 06:16:57 +00003983
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003984void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
3985 const FunctionDecl *D) {
3986 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003987 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003988 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00003989 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003990 return; // Not a source specialization added to a template from PCH.
3991
3992 UpdateRecord &Record = DeclUpdates[TD];
3993 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003994 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003995}
3996
Sebastian Redl58a2cd82011-04-24 16:28:06 +00003997void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003998 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00003999 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004000 return; // Declaration not imported from PCH.
4001
4002 // Implicit decl from a PCH was defined.
4003 // FIXME: Should implicit definition be a separate FunctionDecl?
4004 RewriteDecl(D);
4005}
4006
Sebastian Redlf79a7192011-04-29 08:19:30 +00004007void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004008 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004009 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004010 return;
4011
4012 // Since the actual instantiation is delayed, this really means that we need
4013 // to update the instantiation location.
4014 UpdateRecord &Record = DeclUpdates[D];
4015 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4016 AddSourceLocation(
4017 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4018}
4019
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004020void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4021 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004022 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004023 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004024 return; // Declaration not imported from PCH.
4025 if (CatD->getNextClassCategory() &&
Douglas Gregor919814d2011-09-09 23:01:35 +00004026 !CatD->getNextClassCategory()->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004027 return; // We already recorded that the tail of a category chain should be
4028 // attached to an interface.
4029
Douglas Gregor61c5e342011-09-17 00:05:03 +00004030 ChainedObjCCategoriesData Data = { IFD, CatD, 0, 0 };
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004031 LocalChainedObjCCategories.push_back(Data);
4032}