blob: 9b534c91654130bbda73e12a464fe37bee03ba50 [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
John McCalla1ee0c52009-10-16 21:56:05 +0000391namespace {
392
393class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000394 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000395 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000396
397public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000398 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000399 : Writer(Writer), Record(Record) { }
400
John McCall51bd8032009-10-18 01:05:36 +0000401#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000402#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000403 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000404#include "clang/AST/TypeLocNodes.def"
405
John McCall51bd8032009-10-18 01:05:36 +0000406 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
407 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000408};
409
410}
411
John McCall51bd8032009-10-18 01:05:36 +0000412void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
413 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000414}
John McCall51bd8032009-10-18 01:05:36 +0000415void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000416 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
417 if (TL.needsExtraLocalData()) {
418 Record.push_back(TL.getWrittenTypeSpec());
419 Record.push_back(TL.getWrittenSignSpec());
420 Record.push_back(TL.getWrittenWidthSpec());
421 Record.push_back(TL.hasModeAttr());
422 }
John McCalla1ee0c52009-10-16 21:56:05 +0000423}
John McCall51bd8032009-10-18 01:05:36 +0000424void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
425 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000426}
John McCall51bd8032009-10-18 01:05:36 +0000427void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
428 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000429}
John McCall51bd8032009-10-18 01:05:36 +0000430void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000432}
John McCall51bd8032009-10-18 01:05:36 +0000433void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000435}
John McCall51bd8032009-10-18 01:05:36 +0000436void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000438}
John McCall51bd8032009-10-18 01:05:36 +0000439void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000441 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000442}
John McCall51bd8032009-10-18 01:05:36 +0000443void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
445 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
446 Record.push_back(TL.getSizeExpr() ? 1 : 0);
447 if (TL.getSizeExpr())
448 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
451 VisitArrayTypeLoc(TL);
452}
453void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
454 VisitArrayTypeLoc(TL);
455}
456void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
457 VisitArrayTypeLoc(TL);
458}
459void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
460 DependentSizedArrayTypeLoc TL) {
461 VisitArrayTypeLoc(TL);
462}
463void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
464 DependentSizedExtVectorTypeLoc TL) {
465 Writer.AddSourceLocation(TL.getNameLoc(), Record);
466}
467void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
468 Writer.AddSourceLocation(TL.getNameLoc(), Record);
469}
470void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
473void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000474 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
475 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000476 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000477 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
478 Writer.AddDeclRef(TL.getArg(i), Record);
479}
480void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
481 VisitFunctionTypeLoc(TL);
482}
483void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
484 VisitFunctionTypeLoc(TL);
485}
John McCalled976492009-12-04 22:46:56 +0000486void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getNameLoc(), Record);
488}
John McCall51bd8032009-10-18 01:05:36 +0000489void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
491}
492void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000493 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
494 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
495 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000496}
497void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000498 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
499 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
500 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
501 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000502}
503void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
Sean Huntca63c202011-05-24 22:41:36 +0000506void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getKWLoc(), Record);
508 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
509 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
510 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
511}
Richard Smith34b41d92011-02-20 03:19:35 +0000512void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
513 Writer.AddSourceLocation(TL.getNameLoc(), Record);
514}
John McCall51bd8032009-10-18 01:05:36 +0000515void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
516 Writer.AddSourceLocation(TL.getNameLoc(), Record);
517}
518void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
John McCall9d156a72011-01-06 01:58:22 +0000521void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
523 if (TL.hasAttrOperand()) {
524 SourceRange range = TL.getAttrOperandParensRange();
525 Writer.AddSourceLocation(range.getBegin(), Record);
526 Writer.AddSourceLocation(range.getEnd(), Record);
527 }
528 if (TL.hasAttrExprOperand()) {
529 Expr *operand = TL.getAttrExprOperand();
530 Record.push_back(operand ? 1 : 0);
531 if (operand) Writer.AddStmt(operand);
532 } else if (TL.hasAttrEnumOperand()) {
533 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
534 }
535}
John McCall51bd8032009-10-18 01:05:36 +0000536void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getNameLoc(), Record);
538}
John McCall49a832b2009-10-18 09:09:24 +0000539void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
540 SubstTemplateTypeParmTypeLoc TL) {
541 Writer.AddSourceLocation(TL.getNameLoc(), Record);
542}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000543void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
544 SubstTemplateTypeParmPackTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
John McCall51bd8032009-10-18 01:05:36 +0000547void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
548 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000549 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
550 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
551 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
552 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000553 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
554 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000555}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000556void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
558 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
559}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000560void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000561 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000562 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000563}
John McCall3cb0ebd2010-03-10 03:28:59 +0000564void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
565 Writer.AddSourceLocation(TL.getNameLoc(), Record);
566}
Douglas Gregor4714c122010-03-31 17:34:00 +0000567void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000568 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000569 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000570 Writer.AddSourceLocation(TL.getNameLoc(), Record);
571}
John McCall33500952010-06-11 00:33:02 +0000572void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
573 DependentTemplateSpecializationTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000575 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000576 Writer.AddSourceLocation(TL.getNameLoc(), Record);
577 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
578 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
579 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000580 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
581 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000582}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000583void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
585}
John McCall51bd8032009-10-18 01:05:36 +0000586void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
587 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000588}
589void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
590 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000591 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
592 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
593 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
594 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000595}
John McCall54e14c42009-10-22 22:37:11 +0000596void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
597 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000598}
John McCalla1ee0c52009-10-16 21:56:05 +0000599
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000600//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000601// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000602//===----------------------------------------------------------------------===//
603
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000604static void EmitBlockID(unsigned ID, const char *Name,
605 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000606 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000607 Record.clear();
608 Record.push_back(ID);
609 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
610
611 // Emit the block name if present.
612 if (Name == 0 || Name[0] == 0) return;
613 Record.clear();
614 while (*Name)
615 Record.push_back(*Name++);
616 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
617}
618
619static void EmitRecordID(unsigned ID, const char *Name,
620 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000621 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000622 Record.clear();
623 Record.push_back(ID);
624 while (*Name)
625 Record.push_back(*Name++);
626 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000627}
628
629static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000630 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000631#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000632 RECORD(STMT_STOP);
633 RECORD(STMT_NULL_PTR);
634 RECORD(STMT_NULL);
635 RECORD(STMT_COMPOUND);
636 RECORD(STMT_CASE);
637 RECORD(STMT_DEFAULT);
638 RECORD(STMT_LABEL);
639 RECORD(STMT_IF);
640 RECORD(STMT_SWITCH);
641 RECORD(STMT_WHILE);
642 RECORD(STMT_DO);
643 RECORD(STMT_FOR);
644 RECORD(STMT_GOTO);
645 RECORD(STMT_INDIRECT_GOTO);
646 RECORD(STMT_CONTINUE);
647 RECORD(STMT_BREAK);
648 RECORD(STMT_RETURN);
649 RECORD(STMT_DECL);
650 RECORD(STMT_ASM);
651 RECORD(EXPR_PREDEFINED);
652 RECORD(EXPR_DECL_REF);
653 RECORD(EXPR_INTEGER_LITERAL);
654 RECORD(EXPR_FLOATING_LITERAL);
655 RECORD(EXPR_IMAGINARY_LITERAL);
656 RECORD(EXPR_STRING_LITERAL);
657 RECORD(EXPR_CHARACTER_LITERAL);
658 RECORD(EXPR_PAREN);
659 RECORD(EXPR_UNARY_OPERATOR);
660 RECORD(EXPR_SIZEOF_ALIGN_OF);
661 RECORD(EXPR_ARRAY_SUBSCRIPT);
662 RECORD(EXPR_CALL);
663 RECORD(EXPR_MEMBER);
664 RECORD(EXPR_BINARY_OPERATOR);
665 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
666 RECORD(EXPR_CONDITIONAL_OPERATOR);
667 RECORD(EXPR_IMPLICIT_CAST);
668 RECORD(EXPR_CSTYLE_CAST);
669 RECORD(EXPR_COMPOUND_LITERAL);
670 RECORD(EXPR_EXT_VECTOR_ELEMENT);
671 RECORD(EXPR_INIT_LIST);
672 RECORD(EXPR_DESIGNATED_INIT);
673 RECORD(EXPR_IMPLICIT_VALUE_INIT);
674 RECORD(EXPR_VA_ARG);
675 RECORD(EXPR_ADDR_LABEL);
676 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000677 RECORD(EXPR_CHOOSE);
678 RECORD(EXPR_GNU_NULL);
679 RECORD(EXPR_SHUFFLE_VECTOR);
680 RECORD(EXPR_BLOCK);
681 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000682 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000683 RECORD(EXPR_OBJC_STRING_LITERAL);
684 RECORD(EXPR_OBJC_ENCODE);
685 RECORD(EXPR_OBJC_SELECTOR_EXPR);
686 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
687 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
688 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
689 RECORD(EXPR_OBJC_KVC_REF_EXPR);
690 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000691 RECORD(STMT_OBJC_FOR_COLLECTION);
692 RECORD(STMT_OBJC_CATCH);
693 RECORD(STMT_OBJC_FINALLY);
694 RECORD(STMT_OBJC_AT_TRY);
695 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
696 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000697 RECORD(EXPR_CXX_OPERATOR_CALL);
698 RECORD(EXPR_CXX_CONSTRUCT);
699 RECORD(EXPR_CXX_STATIC_CAST);
700 RECORD(EXPR_CXX_DYNAMIC_CAST);
701 RECORD(EXPR_CXX_REINTERPRET_CAST);
702 RECORD(EXPR_CXX_CONST_CAST);
703 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
704 RECORD(EXPR_CXX_BOOL_LITERAL);
705 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000706 RECORD(EXPR_CXX_TYPEID_EXPR);
707 RECORD(EXPR_CXX_TYPEID_TYPE);
708 RECORD(EXPR_CXX_UUIDOF_EXPR);
709 RECORD(EXPR_CXX_UUIDOF_TYPE);
710 RECORD(EXPR_CXX_THIS);
711 RECORD(EXPR_CXX_THROW);
712 RECORD(EXPR_CXX_DEFAULT_ARG);
713 RECORD(EXPR_CXX_BIND_TEMPORARY);
714 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
715 RECORD(EXPR_CXX_NEW);
716 RECORD(EXPR_CXX_DELETE);
717 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
718 RECORD(EXPR_EXPR_WITH_CLEANUPS);
719 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
720 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
721 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
722 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
723 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
724 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
725 RECORD(EXPR_CXX_NOEXCEPT);
726 RECORD(EXPR_OPAQUE_VALUE);
727 RECORD(EXPR_BINARY_TYPE_TRAIT);
728 RECORD(EXPR_PACK_EXPANSION);
729 RECORD(EXPR_SIZEOF_PACK);
730 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000731 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000732#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000733}
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Sebastian Redla4232eb2010-08-18 23:56:21 +0000735void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000736 RecordData Record;
737 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000739#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
740#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Sebastian Redl3397c552010-08-18 23:56:27 +0000742 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000743 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000744 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000745 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000746 RECORD(TYPE_OFFSET);
747 RECORD(DECL_OFFSET);
748 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000749 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000750 RECORD(IDENTIFIER_OFFSET);
751 RECORD(IDENTIFIER_TABLE);
752 RECORD(EXTERNAL_DEFINITIONS);
753 RECORD(SPECIAL_TYPES);
754 RECORD(STATISTICS);
755 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000756 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000757 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
758 RECORD(SELECTOR_OFFSETS);
759 RECORD(METHOD_POOL);
760 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000761 RECORD(SOURCE_LOCATION_OFFSETS);
762 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000763 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000764 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000765 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000766 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000767 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000768 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000769 RECORD(TU_UPDATE_LEXICAL);
770 RECORD(REDECLS_UPDATE_LATEST);
771 RECORD(SEMA_DECL_REFS);
772 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
773 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
774 RECORD(DECL_REPLACEMENTS);
775 RECORD(UPDATE_VISIBLE);
776 RECORD(DECL_UPDATE_OFFSETS);
777 RECORD(DECL_UPDATES);
778 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
779 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000780 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000781 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000782 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000783 RECORD(FP_PRAGMA_OPTIONS);
784 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000785 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000786 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
787 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000788 RECORD(MODULE_OFFSET_MAP);
789 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000790
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000791 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000792 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000793 RECORD(SM_SLOC_FILE_ENTRY);
794 RECORD(SM_SLOC_BUFFER_ENTRY);
795 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000796 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000798 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000799 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000800 RECORD(PP_MACRO_OBJECT_LIKE);
801 RECORD(PP_MACRO_FUNCTION_LIKE);
802 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000803
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000804 // Decls and Types block.
805 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000806 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000807 RECORD(TYPE_COMPLEX);
808 RECORD(TYPE_POINTER);
809 RECORD(TYPE_BLOCK_POINTER);
810 RECORD(TYPE_LVALUE_REFERENCE);
811 RECORD(TYPE_RVALUE_REFERENCE);
812 RECORD(TYPE_MEMBER_POINTER);
813 RECORD(TYPE_CONSTANT_ARRAY);
814 RECORD(TYPE_INCOMPLETE_ARRAY);
815 RECORD(TYPE_VARIABLE_ARRAY);
816 RECORD(TYPE_VECTOR);
817 RECORD(TYPE_EXT_VECTOR);
818 RECORD(TYPE_FUNCTION_PROTO);
819 RECORD(TYPE_FUNCTION_NO_PROTO);
820 RECORD(TYPE_TYPEDEF);
821 RECORD(TYPE_TYPEOF_EXPR);
822 RECORD(TYPE_TYPEOF);
823 RECORD(TYPE_RECORD);
824 RECORD(TYPE_ENUM);
825 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000826 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000827 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000828 RECORD(TYPE_DECLTYPE);
829 RECORD(TYPE_ELABORATED);
830 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
831 RECORD(TYPE_UNRESOLVED_USING);
832 RECORD(TYPE_INJECTED_CLASS_NAME);
833 RECORD(TYPE_OBJC_OBJECT);
834 RECORD(TYPE_TEMPLATE_TYPE_PARM);
835 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
836 RECORD(TYPE_DEPENDENT_NAME);
837 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
838 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
839 RECORD(TYPE_PAREN);
840 RECORD(TYPE_PACK_EXPANSION);
841 RECORD(TYPE_ATTRIBUTED);
842 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000843 RECORD(DECL_TYPEDEF);
844 RECORD(DECL_ENUM);
845 RECORD(DECL_RECORD);
846 RECORD(DECL_ENUM_CONSTANT);
847 RECORD(DECL_FUNCTION);
848 RECORD(DECL_OBJC_METHOD);
849 RECORD(DECL_OBJC_INTERFACE);
850 RECORD(DECL_OBJC_PROTOCOL);
851 RECORD(DECL_OBJC_IVAR);
852 RECORD(DECL_OBJC_AT_DEFS_FIELD);
853 RECORD(DECL_OBJC_CLASS);
854 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
855 RECORD(DECL_OBJC_CATEGORY);
856 RECORD(DECL_OBJC_CATEGORY_IMPL);
857 RECORD(DECL_OBJC_IMPLEMENTATION);
858 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
859 RECORD(DECL_OBJC_PROPERTY);
860 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000861 RECORD(DECL_FIELD);
862 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000863 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000864 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000865 RECORD(DECL_FILE_SCOPE_ASM);
866 RECORD(DECL_BLOCK);
867 RECORD(DECL_CONTEXT_LEXICAL);
868 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000869 RECORD(DECL_NAMESPACE);
870 RECORD(DECL_NAMESPACE_ALIAS);
871 RECORD(DECL_USING);
872 RECORD(DECL_USING_SHADOW);
873 RECORD(DECL_USING_DIRECTIVE);
874 RECORD(DECL_UNRESOLVED_USING_VALUE);
875 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
876 RECORD(DECL_LINKAGE_SPEC);
877 RECORD(DECL_CXX_RECORD);
878 RECORD(DECL_CXX_METHOD);
879 RECORD(DECL_CXX_CONSTRUCTOR);
880 RECORD(DECL_CXX_DESTRUCTOR);
881 RECORD(DECL_CXX_CONVERSION);
882 RECORD(DECL_ACCESS_SPEC);
883 RECORD(DECL_FRIEND);
884 RECORD(DECL_FRIEND_TEMPLATE);
885 RECORD(DECL_CLASS_TEMPLATE);
886 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
887 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
888 RECORD(DECL_FUNCTION_TEMPLATE);
889 RECORD(DECL_TEMPLATE_TYPE_PARM);
890 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
891 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
892 RECORD(DECL_STATIC_ASSERT);
893 RECORD(DECL_CXX_BASE_SPECIFIERS);
894 RECORD(DECL_INDIRECTFIELD);
895 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
896
Douglas Gregora72d8c42011-06-03 02:27:19 +0000897 // Statements and Exprs can occur in the Decls and Types block.
898 AddStmtsExprs(Stream, Record);
899
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000900 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000901 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000902 RECORD(PPD_MACRO_DEFINITION);
903 RECORD(PPD_INCLUSION_DIRECTIVE);
904
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000905#undef RECORD
906#undef BLOCK
907 Stream.ExitBlock();
908}
909
Douglas Gregore650c8c2009-07-07 00:12:59 +0000910/// \brief Adjusts the given filename to only write out the portion of the
911/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000912///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000913/// \param Filename the file name to adjust.
914///
915/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
916/// the returned filename will be adjusted by this system root.
917///
918/// \returns either the original filename (if it needs no adjustment) or the
919/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000920static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000921adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000922 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Douglas Gregor832d6202011-07-22 16:35:34 +0000924 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000925 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Douglas Gregore650c8c2009-07-07 00:12:59 +0000927 // Verify that the filename and the system root have the same prefix.
928 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000929 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000930 if (Filename[Pos] != isysroot[Pos])
931 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Douglas Gregore650c8c2009-07-07 00:12:59 +0000933 // We hit the end of the filename before we hit the end of the system root.
934 if (!Filename[Pos])
935 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Douglas Gregore650c8c2009-07-07 00:12:59 +0000937 // If the file name has a '/' at the current position, skip over the '/'.
938 // We distinguish sysroot-based includes from absolute includes by the
939 // absence of '/' at the beginning of sysroot-based includes.
940 if (Filename[Pos] == '/')
941 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943 return Filename + Pos;
944}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000945
Sebastian Redl3397c552010-08-18 23:56:27 +0000946/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000947void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000948 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000949 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000950
Douglas Gregore650c8c2009-07-07 00:12:59 +0000951 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000952 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000953 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000954 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000955 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
956 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
959 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000960 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregore650c8c2009-07-07 00:12:59 +0000963 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000964 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000965 Record.push_back(VERSION_MAJOR);
966 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 Record.push_back(CLANG_VERSION_MAJOR);
968 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000969 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000970 const std::string &Triple = Target.getTriple().getTriple();
971 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
972
973 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000974 serialization::ModuleManager &Mgr = Chain->getModuleManager();
975 llvm::SmallVector<char, 128> ModulePaths;
976 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000977
978 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
979 M != MEnd; ++M) {
980 // Skip modules that weren't directly imported.
981 if (!(*M)->isDirectlyImported())
982 continue;
983
984 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
985 // FIXME: Write import location, once it matters.
986 // FIXME: This writes the absolute path for AST files we depend on.
987 const std::string &FileName = (*M)->FileName;
988 Record.push_back(FileName.size());
989 Record.append(FileName.begin(), FileName.end());
990 }
Douglas Gregore95b9192011-08-17 21:07:30 +0000991 Stream.EmitRecord(IMPORTS, Record);
992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregor31d375f2011-05-06 21:43:30 +0000994 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +0000995 SourceManager &SM = Context.getSourceManager();
996 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
997 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000998 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000999 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1000 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1001
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001002 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001004 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001005
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001006 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001007 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001008 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001009 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001010 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001011 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001012
1013 Record.clear();
1014 Record.push_back(SM.getMainFileID().getOpaqueValue());
1015 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001016 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001017
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001018 // Original PCH directory
1019 if (!OutputFile.empty() && OutputFile != "-") {
1020 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1021 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1023 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1024
1025 llvm::SmallString<128> OutputPath(OutputFile);
1026
1027 llvm::sys::fs::make_absolute(OutputPath);
1028 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1029
1030 RecordData Record;
1031 Record.push_back(ORIGINAL_PCH_DIR);
1032 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1033 }
1034
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001035 // Repository branch/version information.
1036 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001037 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001038 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1039 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001040 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001041 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001042 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1043 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001044}
1045
1046/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001047void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001048 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001049#define LANGOPT(Name, Bits, Default, Description) \
1050 Record.push_back(LangOpts.Name);
1051#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1052 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1053#include "clang/Basic/LangOptions.def"
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001054 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001055}
1056
Douglas Gregor14f79002009-04-10 03:52:48 +00001057//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001058// stat cache Serialization
1059//===----------------------------------------------------------------------===//
1060
1061namespace {
1062// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001063class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001064public:
1065 typedef const char * key_type;
1066 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Chris Lattner74e976b2010-11-23 19:28:12 +00001068 typedef struct stat data_type;
1069 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001070
1071 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001072 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001073 }
Mike Stump1eb44332009-09-09 15:08:12 +00001074
1075 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001076 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001077 data_type_ref Data) {
1078 unsigned StrLen = strlen(path);
1079 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001080 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081 clang::io::Emit8(Out, DataLen);
1082 return std::make_pair(StrLen + 1, DataLen);
1083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattner5f9e2722011-07-23 10:55:15 +00001085 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001086 Out.write(path, KeyLen);
1087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner5f9e2722011-07-23 10:55:15 +00001089 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001090 data_type_ref Data, unsigned DataLen) {
1091 using namespace clang::io;
1092 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Chris Lattner74e976b2010-11-23 19:28:12 +00001094 Emit32(Out, (uint32_t) Data.st_ino);
1095 Emit32(Out, (uint32_t) Data.st_dev);
1096 Emit16(Out, (uint16_t) Data.st_mode);
1097 Emit64(Out, (uint64_t) Data.st_mtime);
1098 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099
1100 assert(Out.tell() - Start == DataLen && "Wrong data length");
1101 }
1102};
1103} // end anonymous namespace
1104
Sebastian Redl3397c552010-08-18 23:56:27 +00001105/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001106void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001107 // Build the on-disk hash table containing information about every
1108 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001109 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001110 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001111 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001112 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001113 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001114 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001115 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001118 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001119 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001120 uint32_t BucketOffset;
1121 {
1122 llvm::raw_svector_ostream Out(StatCacheData);
1123 // Make sure that no bucket is at offset 0
1124 clang::io::Emit32(Out, 0);
1125 BucketOffset = Generator.Emit(Out);
1126 }
1127
1128 // Create a blob abbreviation
1129 using namespace llvm;
1130 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001131 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1135 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1136
1137 // Write the stat cache
1138 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001139 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001140 Record.push_back(BucketOffset);
1141 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001142 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001143}
1144
1145//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001146// Source Manager Serialization
1147//===----------------------------------------------------------------------===//
1148
1149/// \brief Create an abbreviation for the SLocEntry that refers to a
1150/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001151static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001152 using namespace llvm;
1153 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001154 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1158 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001159 // FileEntry fields.
1160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Douglas Gregor14f79002009-04-10 03:52:48 +00001163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001164 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001165}
1166
1167/// \brief Create an abbreviation for the SLocEntry that refers to a
1168/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001169static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001170 using namespace llvm;
1171 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001172 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001178 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001179}
1180
1181/// \brief Create an abbreviation for the SLocEntry that refers to a
1182/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001183static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001184 using namespace llvm;
1185 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001186 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001188 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001189}
1190
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001191/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1192/// expansion.
1193static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001194 using namespace llvm;
1195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001196 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001202 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001203}
1204
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001205namespace {
1206 // Trait used for the on-disk hash table of header search information.
1207 class HeaderFileInfoTrait {
1208 ASTWriter &Writer;
1209 HeaderSearch &HS;
1210
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001211 // Keep track of the framework names we've used during serialization.
1212 SmallVector<char, 128> FrameworkStringData;
1213 llvm::StringMap<unsigned> FrameworkNameOffset;
1214
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001215 public:
1216 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1217 : Writer(Writer), HS(HS) { }
1218
1219 typedef const char *key_type;
1220 typedef key_type key_type_ref;
1221
1222 typedef HeaderFileInfo data_type;
1223 typedef const data_type &data_type_ref;
1224
1225 static unsigned ComputeHash(const char *path) {
1226 // The hash is based only on the filename portion of the key, so that the
1227 // reader can match based on filenames when symlinking or excess path
1228 // elements ("foo/../", "../") change the form of the name. However,
1229 // complete path is still the key.
1230 return llvm::HashString(llvm::sys::path::filename(path));
1231 }
1232
1233 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001234 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001235 data_type_ref Data) {
1236 unsigned StrLen = strlen(path);
1237 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001238 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001239 clang::io::Emit8(Out, DataLen);
1240 return std::make_pair(StrLen + 1, DataLen);
1241 }
1242
Chris Lattner5f9e2722011-07-23 10:55:15 +00001243 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001244 Out.write(path, KeyLen);
1245 }
1246
Chris Lattner5f9e2722011-07-23 10:55:15 +00001247 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001248 data_type_ref Data, unsigned DataLen) {
1249 using namespace clang::io;
1250 uint64_t Start = Out.tell(); (void)Start;
1251
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001252 unsigned char Flags = (Data.isImport << 5)
1253 | (Data.isPragmaOnce << 4)
1254 | (Data.DirInfo << 2)
1255 | (Data.Resolved << 1)
1256 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001257 Emit8(Out, (uint8_t)Flags);
1258 Emit16(Out, (uint16_t) Data.NumIncludes);
1259
1260 if (!Data.ControllingMacro)
1261 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1262 else
1263 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001264
1265 unsigned Offset = 0;
1266 if (!Data.Framework.empty()) {
1267 // If this header refers into a framework, save the framework name.
1268 llvm::StringMap<unsigned>::iterator Pos
1269 = FrameworkNameOffset.find(Data.Framework);
1270 if (Pos == FrameworkNameOffset.end()) {
1271 Offset = FrameworkStringData.size() + 1;
1272 FrameworkStringData.append(Data.Framework.begin(),
1273 Data.Framework.end());
1274 FrameworkStringData.push_back(0);
1275
1276 FrameworkNameOffset[Data.Framework] = Offset;
1277 } else
1278 Offset = Pos->second;
1279 }
1280 Emit32(Out, Offset);
1281
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001282 assert(Out.tell() - Start == DataLen && "Wrong data length");
1283 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001284
1285 const char *strings_begin() const { return FrameworkStringData.begin(); }
1286 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001287 };
1288} // end anonymous namespace
1289
1290/// \brief Write the header search block for the list of files that
1291///
1292/// \param HS The header search structure to save.
1293///
1294/// \param Chain Whether we're creating a chained AST file.
Douglas Gregor832d6202011-07-22 16:35:34 +00001295void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001296 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001297 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1298
1299 if (FilesByUID.size() > HS.header_file_size())
1300 FilesByUID.resize(HS.header_file_size());
1301
1302 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1303 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001304 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001305 unsigned NumHeaderSearchEntries = 0;
1306 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1307 const FileEntry *File = FilesByUID[UID];
1308 if (!File)
1309 continue;
1310
1311 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1312 if (HFI.External && Chain)
1313 continue;
1314
1315 // Turn the file name into an absolute path, if it isn't already.
1316 const char *Filename = File->getName();
1317 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1318
1319 // If we performed any translation on the file name at all, we need to
1320 // save this string, since the generator will refer to it later.
1321 if (Filename != File->getName()) {
1322 Filename = strdup(Filename);
1323 SavedStrings.push_back(Filename);
1324 }
1325
1326 Generator.insert(Filename, HFI, GeneratorTrait);
1327 ++NumHeaderSearchEntries;
1328 }
1329
1330 // Create the on-disk hash table in a buffer.
1331 llvm::SmallString<4096> TableData;
1332 uint32_t BucketOffset;
1333 {
1334 llvm::raw_svector_ostream Out(TableData);
1335 // Make sure that no bucket is at offset 0
1336 clang::io::Emit32(Out, 0);
1337 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1338 }
1339
1340 // Create a blob abbreviation
1341 using namespace llvm;
1342 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1343 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1344 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1348 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1349
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001350 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001351 RecordData Record;
1352 Record.push_back(HEADER_SEARCH_TABLE);
1353 Record.push_back(BucketOffset);
1354 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001355 Record.push_back(TableData.size());
1356 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001357 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1358
1359 // Free all of the strings we had to duplicate.
1360 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1361 free((void*)SavedStrings[I]);
1362}
1363
Douglas Gregor14f79002009-04-10 03:52:48 +00001364/// \brief Writes the block containing the serialized form of the
1365/// source manager.
1366///
1367/// TODO: We should probably use an on-disk hash table (stored in a
1368/// blob), indexed based on the file name, so that we only create
1369/// entries for files that we actually need. In the common case (no
1370/// errors), we probably won't have to create file entries for any of
1371/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001372void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001373 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001374 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001375 RecordData Record;
1376
Chris Lattnerf04ad692009-04-10 17:16:57 +00001377 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001378 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001379
1380 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001381 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1382 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1383 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001384 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001385
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001386 // Write out the source location entry table. We skip the first
1387 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001388 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001389 // Write out the offsets of only source location file entries.
1390 // We will go through them in ASTReader::validateFileEntries().
1391 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001392 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001393 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1394 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001395 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001396 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001397 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001398
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001399 // Record the offset of this source-location entry.
1400 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1401
1402 // Figure out which record code to use.
1403 unsigned Code;
1404 if (SLoc->isFile()) {
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001405 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001406 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001407 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1408 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001409 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001410 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001411 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001412 Record.clear();
1413 Record.push_back(Code);
1414
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001415 // Starting offset of this entry within this module, so skip the dummy.
1416 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001417 if (SLoc->isFile()) {
1418 const SrcMgr::FileInfo &File = SLoc->getFile();
1419 Record.push_back(File.getIncludeLoc().getRawEncoding());
1420 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1421 Record.push_back(File.hasLineDirectives());
1422
1423 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001424 if (Content->OrigEntry) {
1425 assert(Content->OrigEntry == Content->ContentsEntry &&
1426 "Writing to AST an overriden file is not supported");
1427
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001428 // The source location entry is a file. The blob associated
1429 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Douglas Gregor2d52be52010-03-21 22:49:54 +00001431 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001432 Record.push_back(Content->OrigEntry->getSize());
1433 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001434
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001435 Record.push_back(File.NumCreatedFIDs);
1436
Douglas Gregore650c8c2009-07-07 00:12:59 +00001437 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001438 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001439 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001440
1441 // Ask the file manager to fixup the relative path for us. This will
1442 // honor the working directory.
1443 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1444
1445 // FIXME: This call to make_absolute shouldn't be necessary, the
1446 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001447 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001448 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Douglas Gregore650c8c2009-07-07 00:12:59 +00001450 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001451 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001452 } else {
1453 // The source location entry is a buffer. The blob associated
1454 // with this entry contains the contents of the buffer.
1455
1456 // We add one to the size so that we capture the trailing NULL
1457 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1458 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001459 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001460 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001461 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001462 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001463 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001464 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001465 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001466 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001467 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001468 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001469
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001470 if (strcmp(Name, "<built-in>") == 0) {
1471 PreloadSLocs.push_back(SLocEntryOffsets.size());
1472 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001473 }
1474 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001475 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001476 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001477 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1478 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001479 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1480 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001481
1482 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001483 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001484 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001485 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001486 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001487 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001488 }
1489 }
1490
Douglas Gregorc9490c02009-04-16 22:23:12 +00001491 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001492
1493 if (SLocEntryOffsets.empty())
1494 return;
1495
Sebastian Redl3397c552010-08-18 23:56:27 +00001496 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001497 // table is used for lazily loading source-location information.
1498 using namespace llvm;
1499 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001500 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1504 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001506 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001507 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001508 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001509 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001510 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001511
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001512 Abbrev = new BitCodeAbbrev();
1513 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1515 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1516 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1517
1518 Record.clear();
1519 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1520 Record.push_back(SLocFileEntryOffsets.size());
1521 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1522 data(SLocFileEntryOffsets));
1523
Sebastian Redl3397c552010-08-18 23:56:27 +00001524 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001525 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001526 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001527
1528 // Write the line table. It depends on remapping working, so it must come
1529 // after the source location offsets.
1530 if (SourceMgr.hasLineTable()) {
1531 LineTableInfo &LineTable = SourceMgr.getLineTable();
1532
1533 Record.clear();
1534 // Emit the file names
1535 Record.push_back(LineTable.getNumFilenames());
1536 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1537 // Emit the file name
1538 const char *Filename = LineTable.getFilename(I);
1539 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1540 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1541 Record.push_back(FilenameLen);
1542 if (FilenameLen)
1543 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1544 }
1545
1546 // Emit the line entries
1547 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1548 L != LEnd; ++L) {
1549 // Only emit entries for local files.
1550 if (L->first < 0)
1551 continue;
1552
1553 // Emit the file ID
1554 Record.push_back(L->first);
1555
1556 // Emit the line entries
1557 Record.push_back(L->second.size());
1558 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1559 LEEnd = L->second.end();
1560 LE != LEEnd; ++LE) {
1561 Record.push_back(LE->FileOffset);
1562 Record.push_back(LE->LineNo);
1563 Record.push_back(LE->FilenameID);
1564 Record.push_back((unsigned)LE->FileKind);
1565 Record.push_back(LE->IncludeOffset);
1566 }
1567 }
1568 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1569 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001570}
1571
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001572//===----------------------------------------------------------------------===//
1573// Preprocessor Serialization
1574//===----------------------------------------------------------------------===//
1575
Douglas Gregor9c736102011-02-10 18:20:09 +00001576static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1577 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1578 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1579 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1580 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1581 return X.first->getName().compare(Y.first->getName());
1582}
1583
Chris Lattner0b1fb982009-04-10 17:15:23 +00001584/// \brief Writes the block containing the serialized form of the
1585/// preprocessor.
1586///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001587void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001588 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1589 if (PPRec)
1590 WritePreprocessorDetail(*PPRec);
1591
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001592 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001593
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001594 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1595 if (PP.getCounterValue() != 0) {
1596 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001597 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001598 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001599 }
1600
1601 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001602 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Sebastian Redl3397c552010-08-18 23:56:27 +00001604 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001605 // FIXME: use diagnostics subsystem for localization etc.
1606 if (PP.SawDateOrTime())
1607 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Douglas Gregorecdcb882010-10-20 22:00:55 +00001609
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001610 // Loop over all the macro definitions that are live at the end of the file,
1611 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001612
Douglas Gregor9c736102011-02-10 18:20:09 +00001613 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001614 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001615 MacrosToEmit;
1616 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001617 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1618 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001619 I != E; ++I) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00001620 if (!IsModule || I->second->isExported()) {
1621 MacroDefinitionsSeen.insert(I->first);
1622 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1623 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001624 }
1625
1626 // Sort the set of macro definitions that need to be serialized by the
1627 // name of the macro, to provide a stable ordering.
1628 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1629 &compareMacroDefinitions);
1630
Douglas Gregor040a8042011-02-11 00:26:14 +00001631 // Resolve any identifiers that defined macros at the time they were
1632 // deserialized, adding them to the list of macros to emit (if appropriate).
1633 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1634 IdentifierInfo *Name
1635 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1636 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1637 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1638 }
1639
Douglas Gregor9c736102011-02-10 18:20:09 +00001640 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1641 const IdentifierInfo *Name = MacrosToEmit[I].first;
1642 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001643 if (!MI)
1644 continue;
1645
Sebastian Redl3397c552010-08-18 23:56:27 +00001646 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001647 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001648 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001649
1650 // FIXME: There is a (probably minor) optimization we could do here, if
1651 // the macro comes from the original PCH but the identifier comes from a
1652 // chained PCH, by storing the offset into the original PCH rather than
1653 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001654 if (MI->isBuiltinMacro() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00001655 (Chain && Name->isFromAST() && MI->isFromAST() &&
1656 !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001657 continue;
1658
Douglas Gregor9c736102011-02-10 18:20:09 +00001659 AddIdentifierRef(Name, Record);
1660 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001661 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1662 Record.push_back(MI->isUsed());
Douglas Gregor7143aab2011-09-01 17:04:32 +00001663 AddSourceLocation(MI->getExportLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001664 unsigned Code;
1665 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001666 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001667 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001668 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001670 Record.push_back(MI->isC99Varargs());
1671 Record.push_back(MI->isGNUVarargs());
1672 Record.push_back(MI->getNumArgs());
1673 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1674 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001675 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001676 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001677
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001678 // If we have a detailed preprocessing record, record the macro definition
1679 // ID that corresponds to this macro.
1680 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001681 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001682
Douglas Gregorc9490c02009-04-16 22:23:12 +00001683 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001684 Record.clear();
1685
Chris Lattnerdf961c22009-04-10 18:08:30 +00001686 // Emit the tokens array.
1687 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1688 // Note that we know that the preprocessor does not have any annotation
1689 // tokens in it because they are created by the parser, and thus can't be
1690 // in a macro definition.
1691 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Chris Lattnerdf961c22009-04-10 18:08:30 +00001693 Record.push_back(Tok.getLocation().getRawEncoding());
1694 Record.push_back(Tok.getLength());
1695
Chris Lattnerdf961c22009-04-10 18:08:30 +00001696 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1697 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001698 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001699 // FIXME: Should translate token kind to a stable encoding.
1700 Record.push_back(Tok.getKind());
1701 // FIXME: Should translate token flags to a stable encoding.
1702 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001704 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001705 Record.clear();
1706 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001707 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001708 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001709 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001710}
1711
1712void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001713 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001714 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001715
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001716 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001717
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001718 // Enter the preprocessor block.
1719 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001720
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001721 // If the preprocessor has a preprocessing record, emit it.
1722 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001723 using namespace llvm;
1724
1725 // Set up the abbreviation for
1726 unsigned InclusionAbbrev = 0;
1727 {
1728 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1729 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1731 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1734 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1735 }
1736
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001737 unsigned FirstPreprocessorEntityID
1738 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1739 + NUM_PREDEF_PP_ENTITY_IDS;
1740 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001741 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001742 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1743 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001744 E != EEnd;
1745 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001746 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001747
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001748 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1749 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001750
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001751 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001752 // Record this macro definition's ID.
1753 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001754
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001755 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001756 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1757 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001758 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001759
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001760 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001761 Record.push_back(ME->isBuiltinMacro());
1762 if (ME->isBuiltinMacro())
1763 AddIdentifierRef(ME->getName(), Record);
1764 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001765 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001766 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001767 continue;
1768 }
1769
1770 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1771 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001772 Record.push_back(ID->getFileName().size());
1773 Record.push_back(ID->wasInQuotes());
1774 Record.push_back(static_cast<unsigned>(ID->getKind()));
1775 llvm::SmallString<64> Buffer;
1776 Buffer += ID->getFileName();
1777 Buffer += ID->getFile()->getName();
1778 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1779 continue;
1780 }
1781
1782 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1783 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001784 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001785
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001786 // Write the offsets table for the preprocessing record.
1787 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001788 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1789
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001790 // Write the offsets table for identifier IDs.
1791 using namespace llvm;
1792 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001793 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001796 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001797
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001798 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001799 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001800 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001801 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1802 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001803 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001804}
1805
David Blaikied6471f72011-09-25 23:23:43 +00001806void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001807 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00001808 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001809 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1810 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00001811 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001812 if (point.Loc.isInvalid())
1813 continue;
1814
1815 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00001816 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001817 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00001818 if (I->second.isPragma()) {
1819 Record.push_back(I->first);
1820 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001821 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001822 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001823 Record.push_back(-1); // mark the end of the diag/map pairs for this
1824 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001825 }
1826
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001827 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001828 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001829}
1830
Anders Carlssonc8505782011-03-06 18:41:18 +00001831void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1832 if (CXXBaseSpecifiersOffsets.empty())
1833 return;
1834
1835 RecordData Record;
1836
1837 // Create a blob abbreviation for the C++ base specifiers offsets.
1838 using namespace llvm;
1839
1840 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1841 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1844 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1845
Douglas Gregore92b8a12011-08-04 00:01:48 +00001846 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00001847 Record.clear();
1848 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1849 Record.push_back(CXXBaseSpecifiersOffsets.size());
1850 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001851 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00001852}
1853
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001854//===----------------------------------------------------------------------===//
1855// Type Serialization
1856//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001857
Sebastian Redl3397c552010-08-18 23:56:27 +00001858/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001859void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001860 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001861 if (Idx.getIndex() == 0) // we haven't seen this type before.
1862 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Douglas Gregor97475832010-10-05 18:37:06 +00001864 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001865
Douglas Gregor2cf26342009-04-09 22:27:44 +00001866 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001867 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001868 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001869 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001870 else if (TypeOffsets.size() < Index) {
1871 TypeOffsets.resize(Index + 1);
1872 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001873 }
1874
1875 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Douglas Gregor2cf26342009-04-09 22:27:44 +00001877 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001878 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001879
Douglas Gregora4923eb2009-11-16 21:35:15 +00001880 if (T.hasLocalNonFastQualifiers()) {
1881 Qualifiers Qs = T.getLocalQualifiers();
1882 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001883 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001884 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001885 } else {
1886 switch (T->getTypeClass()) {
1887 // For all of the concrete, non-dependent types, call the
1888 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001889#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001890 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001891#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001892#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001893 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001894 }
1895
1896 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001897 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001898
1899 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001900 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001901}
1902
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001903//===----------------------------------------------------------------------===//
1904// Declaration Serialization
1905//===----------------------------------------------------------------------===//
1906
Douglas Gregor2cf26342009-04-09 22:27:44 +00001907/// \brief Write the block containing all of the declaration IDs
1908/// lexically declared within the given DeclContext.
1909///
1910/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1911/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001912uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001913 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001914 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001915 return 0;
1916
Douglas Gregorc9490c02009-04-16 22:23:12 +00001917 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001918 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001919 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001920 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001921 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1922 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001923 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001924
Douglas Gregor25123082009-04-22 22:34:57 +00001925 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001926 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001927 return Offset;
1928}
1929
Sebastian Redla4232eb2010-08-18 23:56:21 +00001930void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001931 using namespace llvm;
1932 RecordData Record;
1933
1934 // Write the type offsets array
1935 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001936 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00001938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00001939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1940 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1941 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001942 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001943 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00001944 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001945 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001946
1947 // Write the declaration offsets array
1948 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001949 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00001951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00001952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1953 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1954 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001955 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001956 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00001957 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001958 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001959}
1960
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001961//===----------------------------------------------------------------------===//
1962// Global Method Pool and Selector Serialization
1963//===----------------------------------------------------------------------===//
1964
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001965namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001966// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001967class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001968 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001969
1970public:
1971 typedef Selector key_type;
1972 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Sebastian Redl5d050072010-08-04 17:20:04 +00001974 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001975 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001976 ObjCMethodList Instance, Factory;
1977 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001978 typedef const data_type& data_type_ref;
1979
Sebastian Redl3397c552010-08-18 23:56:27 +00001980 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001982 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001983 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001984 }
Mike Stump1eb44332009-09-09 15:08:12 +00001985
1986 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001987 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001988 data_type_ref Methods) {
1989 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1990 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001991 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1992 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001993 Method = Method->Next)
1994 if (Method->Method)
1995 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001996 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001997 Method = Method->Next)
1998 if (Method->Method)
1999 DataLen += 4;
2000 clang::io::Emit16(Out, DataLen);
2001 return std::make_pair(KeyLen, DataLen);
2002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Chris Lattner5f9e2722011-07-23 10:55:15 +00002004 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002005 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002006 assert((Start >> 32) == 0 && "Selector key offset too large");
2007 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002008 unsigned N = Sel.getNumArgs();
2009 clang::io::Emit16(Out, N);
2010 if (N == 0)
2011 N = 1;
2012 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002013 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002014 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2015 }
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Chris Lattner5f9e2722011-07-23 10:55:15 +00002017 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002018 data_type_ref Methods, unsigned DataLen) {
2019 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002020 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002021 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002022 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002023 Method = Method->Next)
2024 if (Method->Method)
2025 ++NumInstanceMethods;
2026
2027 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002028 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002029 Method = Method->Next)
2030 if (Method->Method)
2031 ++NumFactoryMethods;
2032
2033 clang::io::Emit16(Out, NumInstanceMethods);
2034 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002035 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002036 Method = Method->Next)
2037 if (Method->Method)
2038 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002039 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002040 Method = Method->Next)
2041 if (Method->Method)
2042 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002043
2044 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002045 }
2046};
2047} // end anonymous namespace
2048
Sebastian Redl059612d2010-08-03 21:58:15 +00002049/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002050///
2051/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002052/// in an on-disk hash table indexed by the selector. The hash table also
2053/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002054void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002055 using namespace llvm;
2056
Sebastian Redl059612d2010-08-03 21:58:15 +00002057 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002058 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002059 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002060 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002061 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002062 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002063 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002064 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Sebastian Redl059612d2010-08-03 21:58:15 +00002066 // Create the on-disk hash table representation. We walk through every
2067 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002068 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002069 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002070 I = SelectorIDs.begin(), E = SelectorIDs.end();
2071 I != E; ++I) {
2072 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002073 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002074 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002075 I->second,
2076 ObjCMethodList(),
2077 ObjCMethodList()
2078 };
2079 if (F != SemaRef.MethodPool.end()) {
2080 Data.Instance = F->second.first;
2081 Data.Factory = F->second.second;
2082 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002083 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002084 // changed.
2085 if (Chain && I->second < FirstSelectorID) {
2086 // Selector already exists. Did it change?
2087 bool changed = false;
2088 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2089 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002090 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002091 changed = true;
2092 }
2093 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2094 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002095 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002096 changed = true;
2097 }
2098 if (!changed)
2099 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002100 } else if (Data.Instance.Method || Data.Factory.Method) {
2101 // A new method pool entry.
2102 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002103 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002104 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002105 }
2106
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002107 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002108 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002109 uint32_t BucketOffset;
2110 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002111 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002112 llvm::raw_svector_ostream Out(MethodPool);
2113 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002114 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002115 BucketOffset = Generator.Emit(Out, Trait);
2116 }
2117
2118 // Create a blob abbreviation
2119 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002120 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2124 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2125
Douglas Gregor83941df2009-04-25 17:48:32 +00002126 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002127 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002128 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002129 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002130 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002131 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002132
2133 // Create a blob abbreviation for the selector table offsets.
2134 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002135 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2139 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2140
2141 // Write the selector offsets table.
2142 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002143 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002144 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002145 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002146 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002147 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002148 }
2149}
2150
Sebastian Redl3397c552010-08-18 23:56:27 +00002151/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002152void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002153 using namespace llvm;
2154 if (SemaRef.ReferencedSelectors.empty())
2155 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002156
Fariborz Jahanian32019832010-07-23 19:11:11 +00002157 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002158
Sebastian Redl3397c552010-08-18 23:56:27 +00002159 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002160 // very tricky to fix, and given that @selector shouldn't really appear in
2161 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002162 for (DenseMap<Selector, SourceLocation>::iterator S =
2163 SemaRef.ReferencedSelectors.begin(),
2164 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2165 Selector Sel = (*S).first;
2166 SourceLocation Loc = (*S).second;
2167 AddSelectorRef(Sel, Record);
2168 AddSourceLocation(Loc, Record);
2169 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002170 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002171}
2172
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002173//===----------------------------------------------------------------------===//
2174// Identifier Table Serialization
2175//===----------------------------------------------------------------------===//
2176
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002177namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002178class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002179 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002180 Preprocessor &PP;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002181 bool IsModule;
2182
Douglas Gregora92193e2009-04-28 21:18:29 +00002183 /// \brief Determines whether this is an "interesting" identifier
2184 /// that needs a full IdentifierInfo structure written into the hash
2185 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002186 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002187 if (II->isPoisoned() ||
2188 II->isExtensionToken() ||
2189 II->getObjCOrBuiltinID() ||
2190 II->getFETokenInfo<void>())
2191 return true;
2192
Douglas Gregorce835df2011-09-14 22:14:14 +00002193 return hasMacroDefinition(II, Macro);
2194 }
2195
2196 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002197 if (!II->hasMacroDefinition())
2198 return false;
2199
Douglas Gregorce835df2011-09-14 22:14:14 +00002200 if (Macro || (Macro = PP.getMacroInfo(II)))
2201 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isExported());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002202
Douglas Gregorce835df2011-09-14 22:14:14 +00002203 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002204 }
2205
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002206public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002207 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002208 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002209
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002210 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002211 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Douglas Gregor7143aab2011-09-01 17:04:32 +00002213 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP, bool IsModule)
2214 : Writer(Writer), PP(PP), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002215
2216 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002217 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002218 }
Mike Stump1eb44332009-09-09 15:08:12 +00002219
2220 std::pair<unsigned,unsigned>
Douglas Gregor7143aab2011-09-01 17:04:32 +00002221 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002222 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002223 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002224 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002225 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002226 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002227 if (hasMacroDefinition(II, Macro))
Douglas Gregor5998da52009-04-28 21:32:13 +00002228 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002229 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2230 DEnd = IdentifierResolver::end();
2231 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002232 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002233 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002234 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002235 // We emit the key length after the data length so that every
2236 // string is preceded by a 16-bit length. This matches the PTH
2237 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002238 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002239 return std::make_pair(KeyLen, DataLen);
2240 }
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Chris Lattner5f9e2722011-07-23 10:55:15 +00002242 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002243 unsigned KeyLen) {
2244 // Record the location of the key data. This is used when generating
2245 // the mapping from persistent IDs to strings.
2246 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002247 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002248 }
Mike Stump1eb44332009-09-09 15:08:12 +00002249
Douglas Gregor7143aab2011-09-01 17:04:32 +00002250 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002251 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002252 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002253 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002254 clang::io::Emit32(Out, ID << 1);
2255 return;
2256 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002257
Douglas Gregora92193e2009-04-28 21:18:29 +00002258 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002259 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002260 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002261 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregorce835df2011-09-14 22:14:14 +00002262 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002263 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2264 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002265 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002266 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002267 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002268
Douglas Gregorce835df2011-09-14 22:14:14 +00002269 if (HasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002270 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002271
Douglas Gregor668c1a42009-04-21 22:25:48 +00002272 // Emit the declaration IDs in reverse order, because the
2273 // IdentifierResolver provides the declarations as they would be
2274 // visible (e.g., the function "stat" would come before the struct
2275 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2276 // adds declarations to the end of the list (so we need to see the
2277 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002278 // Only emit declarations that aren't from a chained PCH, though.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002279 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002280 IdentifierResolver::end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002281 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002282 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002283 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002284 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002285 }
2286};
2287} // end anonymous namespace
2288
Sebastian Redl3397c552010-08-18 23:56:27 +00002289/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002290///
2291/// The identifier table consists of a blob containing string data
2292/// (the actual identifiers themselves) and a separate "offsets" index
2293/// that maps identifier IDs to locations within the blob.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002294void ASTWriter::WriteIdentifierTable(Preprocessor &PP, bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002295 using namespace llvm;
2296
2297 // Create and write out the blob that contains the identifier
2298 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002299 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002300 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002301 ASTIdentifierTableTrait Trait(*this, PP, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Douglas Gregor92b059e2009-04-28 20:33:11 +00002303 // Look for any identifiers that were named while processing the
2304 // headers, but are otherwise not needed. We add these to the hash
2305 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002306 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002307 // file.
2308 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2309 IDEnd = PP.getIdentifierTable().end();
2310 ID != IDEnd; ++ID)
2311 getIdentifierRef(ID->second);
2312
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002313 // Create the on-disk hash table representation. We only store offsets
2314 // for identifiers that appear here for the first time.
2315 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002316 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002317 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2318 ID != IDEnd; ++ID) {
2319 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002320 if (!Chain || !ID->first->isFromAST())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002321 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2322 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002323 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002324
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002325 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002326 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002327 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002328 {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002329 ASTIdentifierTableTrait Trait(*this, PP, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002330 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002331 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002332 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002333 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002334 }
2335
2336 // Create a blob abbreviation
2337 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002338 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002341 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002342
2343 // Write the identifier table
2344 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002345 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002346 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002347 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002348 }
2349
2350 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002351 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002352 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2356 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2357
2358 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002359 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002360 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002361 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002362 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002363 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002364}
2365
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002366//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002367// DeclContext's Name Lookup Table Serialization
2368//===----------------------------------------------------------------------===//
2369
2370namespace {
2371// Trait used for the on-disk hash table used in the method pool.
2372class ASTDeclContextNameLookupTrait {
2373 ASTWriter &Writer;
2374
2375public:
2376 typedef DeclarationName key_type;
2377 typedef key_type key_type_ref;
2378
2379 typedef DeclContext::lookup_result data_type;
2380 typedef const data_type& data_type_ref;
2381
2382 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2383
2384 unsigned ComputeHash(DeclarationName Name) {
2385 llvm::FoldingSetNodeID ID;
2386 ID.AddInteger(Name.getNameKind());
2387
2388 switch (Name.getNameKind()) {
2389 case DeclarationName::Identifier:
2390 ID.AddString(Name.getAsIdentifierInfo()->getName());
2391 break;
2392 case DeclarationName::ObjCZeroArgSelector:
2393 case DeclarationName::ObjCOneArgSelector:
2394 case DeclarationName::ObjCMultiArgSelector:
2395 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2396 break;
2397 case DeclarationName::CXXConstructorName:
2398 case DeclarationName::CXXDestructorName:
2399 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002400 break;
2401 case DeclarationName::CXXOperatorName:
2402 ID.AddInteger(Name.getCXXOverloadedOperator());
2403 break;
2404 case DeclarationName::CXXLiteralOperatorName:
2405 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2406 case DeclarationName::CXXUsingDirective:
2407 break;
2408 }
2409
2410 return ID.ComputeHash();
2411 }
2412
2413 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002414 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002415 data_type_ref Lookup) {
2416 unsigned KeyLen = 1;
2417 switch (Name.getNameKind()) {
2418 case DeclarationName::Identifier:
2419 case DeclarationName::ObjCZeroArgSelector:
2420 case DeclarationName::ObjCOneArgSelector:
2421 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002422 case DeclarationName::CXXLiteralOperatorName:
2423 KeyLen += 4;
2424 break;
2425 case DeclarationName::CXXOperatorName:
2426 KeyLen += 1;
2427 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002428 case DeclarationName::CXXConstructorName:
2429 case DeclarationName::CXXDestructorName:
2430 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002431 case DeclarationName::CXXUsingDirective:
2432 break;
2433 }
2434 clang::io::Emit16(Out, KeyLen);
2435
2436 // 2 bytes for num of decls and 4 for each DeclID.
2437 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2438 clang::io::Emit16(Out, DataLen);
2439
2440 return std::make_pair(KeyLen, DataLen);
2441 }
2442
Chris Lattner5f9e2722011-07-23 10:55:15 +00002443 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002444 using namespace clang::io;
2445
2446 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2447 Emit8(Out, Name.getNameKind());
2448 switch (Name.getNameKind()) {
2449 case DeclarationName::Identifier:
2450 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2451 break;
2452 case DeclarationName::ObjCZeroArgSelector:
2453 case DeclarationName::ObjCOneArgSelector:
2454 case DeclarationName::ObjCMultiArgSelector:
2455 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2456 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002457 case DeclarationName::CXXOperatorName:
2458 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2459 Emit8(Out, Name.getCXXOverloadedOperator());
2460 break;
2461 case DeclarationName::CXXLiteralOperatorName:
2462 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2463 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002464 case DeclarationName::CXXConstructorName:
2465 case DeclarationName::CXXDestructorName:
2466 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002467 case DeclarationName::CXXUsingDirective:
2468 break;
2469 }
2470 }
2471
Chris Lattner5f9e2722011-07-23 10:55:15 +00002472 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002473 data_type Lookup, unsigned DataLen) {
2474 uint64_t Start = Out.tell(); (void)Start;
2475 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2476 for (; Lookup.first != Lookup.second; ++Lookup.first)
2477 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2478
2479 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2480 }
2481};
2482} // end anonymous namespace
2483
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002484/// \brief Write the block containing all of the declaration IDs
2485/// visible from the given DeclContext.
2486///
2487/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002488/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002489uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2490 DeclContext *DC) {
2491 if (DC->getPrimaryContext() != DC)
2492 return 0;
2493
2494 // Since there is no name lookup into functions or methods, don't bother to
2495 // build a visible-declarations table for these entities.
2496 if (DC->isFunctionOrMethod())
2497 return 0;
2498
2499 // If not in C++, we perform name lookup for the translation unit via the
2500 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2501 // FIXME: In C++ we need the visible declarations in order to "see" the
2502 // friend declarations, is there a way to do this without writing the table ?
2503 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2504 return 0;
2505
2506 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002507 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002508 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002509
2510 // Serialize the contents of the mapping used for lookup. Note that,
2511 // although we have two very different code paths, the serialized
2512 // representation is the same for both cases: a declaration name,
2513 // followed by a size, followed by references to the visible
2514 // declarations that have that name.
2515 uint64_t Offset = Stream.GetCurrentBitNo();
2516 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2517 if (!Map || Map->empty())
2518 return 0;
2519
2520 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2521 ASTDeclContextNameLookupTrait Trait(*this);
2522
2523 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002524 DeclarationName ConversionName;
2525 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002526 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2527 D != DEnd; ++D) {
2528 DeclarationName Name = D->first;
2529 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002530 if (Result.first != Result.second) {
2531 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2532 // Hash all conversion function names to the same name. The actual
2533 // type information in conversion function name is not used in the
2534 // key (since such type information is not stable across different
2535 // modules), so the intended effect is to coalesce all of the conversion
2536 // functions under a single key.
2537 if (!ConversionName)
2538 ConversionName = Name;
2539 ConversionDecls.append(Result.first, Result.second);
2540 continue;
2541 }
2542
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002543 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002544 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002545 }
2546
Douglas Gregore5a54b62011-08-30 20:49:19 +00002547 // Add the conversion functions
2548 if (!ConversionDecls.empty()) {
2549 Generator.insert(ConversionName,
2550 DeclContext::lookup_result(ConversionDecls.begin(),
2551 ConversionDecls.end()),
2552 Trait);
2553 }
2554
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002555 // Create the on-disk hash table in a buffer.
2556 llvm::SmallString<4096> LookupTable;
2557 uint32_t BucketOffset;
2558 {
2559 llvm::raw_svector_ostream Out(LookupTable);
2560 // Make sure that no bucket is at offset 0
2561 clang::io::Emit32(Out, 0);
2562 BucketOffset = Generator.Emit(Out, Trait);
2563 }
2564
2565 // Write the lookup table
2566 RecordData Record;
2567 Record.push_back(DECL_CONTEXT_VISIBLE);
2568 Record.push_back(BucketOffset);
2569 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2570 LookupTable.str());
2571
2572 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2573 ++NumVisibleDeclContexts;
2574 return Offset;
2575}
2576
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002577/// \brief Write an UPDATE_VISIBLE block for the given context.
2578///
2579/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2580/// DeclContext in a dependent AST file. As such, they only exist for the TU
2581/// (in C++) and for namespaces.
2582void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002583 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2584 if (!Map || Map->empty())
2585 return;
2586
2587 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2588 ASTDeclContextNameLookupTrait Trait(*this);
2589
2590 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002591 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2592 D != DEnd; ++D) {
2593 DeclarationName Name = D->first;
2594 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002595 // For any name that appears in this table, the results are complete, i.e.
2596 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002597 if (Result.first != Result.second)
2598 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002599 }
2600
2601 // Create the on-disk hash table in a buffer.
2602 llvm::SmallString<4096> LookupTable;
2603 uint32_t BucketOffset;
2604 {
2605 llvm::raw_svector_ostream Out(LookupTable);
2606 // Make sure that no bucket is at offset 0
2607 clang::io::Emit32(Out, 0);
2608 BucketOffset = Generator.Emit(Out, Trait);
2609 }
2610
2611 // Write the lookup table
2612 RecordData Record;
2613 Record.push_back(UPDATE_VISIBLE);
2614 Record.push_back(getDeclID(cast<Decl>(DC)));
2615 Record.push_back(BucketOffset);
2616 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2617}
2618
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002619/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2620void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2621 RecordData Record;
2622 Record.push_back(Opts.fp_contract);
2623 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2624}
2625
2626/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2627void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2628 if (!SemaRef.Context.getLangOptions().OpenCL)
2629 return;
2630
2631 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2632 RecordData Record;
2633#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2634#include "clang/Basic/OpenCLExtensions.def"
2635 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2636}
2637
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002638//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002639// General Serialization Routines
2640//===----------------------------------------------------------------------===//
2641
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002642/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002643void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002644 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002645 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2646 const Attr * A = *i;
2647 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002648 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002649
Sean Huntcf807c42010-08-18 23:23:40 +00002650#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002651
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002652 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002653}
2654
Chris Lattner5f9e2722011-07-23 10:55:15 +00002655void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002656 Record.push_back(Str.size());
2657 Record.insert(Record.end(), Str.begin(), Str.end());
2658}
2659
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002660void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2661 RecordDataImpl &Record) {
2662 Record.push_back(Version.getMajor());
2663 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2664 Record.push_back(*Minor + 1);
2665 else
2666 Record.push_back(0);
2667 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2668 Record.push_back(*Subminor + 1);
2669 else
2670 Record.push_back(0);
2671}
2672
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002673/// \brief Note that the identifier II occurs at the given offset
2674/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002675void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002676 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002677 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002678 // up earlier in the chain and thus don't need an offset.
2679 if (ID >= FirstIdentID)
2680 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002681}
2682
Douglas Gregor83941df2009-04-25 17:48:32 +00002683/// \brief Note that the selector Sel occurs at the given offset
2684/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002685void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002686 unsigned ID = SelectorIDs[Sel];
2687 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002688 // Don't record offsets for selectors that are also available in a different
2689 // file.
2690 if (ID < FirstSelectorID)
2691 return;
2692 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002693}
2694
Sebastian Redla4232eb2010-08-18 23:56:21 +00002695ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Argyrios Kyrtzidis92ddef12011-09-19 20:40:48 +00002696 : Stream(Stream), Context(0), Chain(0), WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002697 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002698 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002699 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002700 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002701 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002702 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002703 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00002704 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002705 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002706 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2707 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2708 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002709 DeclTypedefAbbrev(0),
2710 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2711 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00002712{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002713}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002714
Sebastian Redla4232eb2010-08-18 23:56:21 +00002715void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002716 const std::string &OutputFile,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002717 bool IsModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00002718 WritingAST = true;
2719
Douglas Gregor2cf26342009-04-09 22:27:44 +00002720 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002721 Stream.Emit((unsigned)'C', 8);
2722 Stream.Emit((unsigned)'P', 8);
2723 Stream.Emit((unsigned)'C', 8);
2724 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002725
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002726 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002727
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002728 Context = &SemaRef.Context;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002729 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, IsModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002730 Context = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00002731
2732 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002733}
2734
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002735template<typename Vector>
2736static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2737 ASTWriter::RecordData &Record) {
2738 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2739 I != E; ++I) {
2740 Writer.AddDeclRef(*I, Record);
2741 }
2742}
2743
Sebastian Redla4232eb2010-08-18 23:56:21 +00002744void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00002745 StringRef isysroot,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002746 const std::string &OutputFile, bool IsModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002747 using namespace llvm;
2748
2749 ASTContext &Context = SemaRef.Context;
2750 Preprocessor &PP = SemaRef.PP;
2751
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002752 // Set up predefined declaration IDs.
2753 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00002754 if (Context.ObjCIdDecl)
2755 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00002756 if (Context.ObjCSelDecl)
2757 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00002758 if (Context.ObjCClassDecl)
2759 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00002760 if (Context.Int128Decl)
2761 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
2762 if (Context.UInt128Decl)
2763 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00002764 if (Context.ObjCInstanceTypeDecl)
2765 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00002766
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002767 if (!Chain) {
2768 // Make sure that we emit IdentifierInfos (and any attached
2769 // declarations) for builtins. We don't need to do this when we're
2770 // emitting chained PCH files, because all of the builtins will be
2771 // in the original PCH file.
2772 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00002773 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002774 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002775 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2776 Context.getLangOptions().NoBuiltin);
2777 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2778 getIdentifierRef(&Table.get(BuiltinNames[I]));
2779 }
2780
Chris Lattner63d65f82009-09-08 18:19:27 +00002781 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002782 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002783 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002784 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002785 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00002786
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002787 // Build a record containing all of the file scoped decls in this file.
2788 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002789 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
2790 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002791
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002792 // Build a record containing all of the delegating constructors we still need
2793 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00002794 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00002795 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00002796
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002797 // Write the set of weak, undeclared identifiers. We always write the
2798 // entire table, since later PCH files in a PCH chain are only interested in
2799 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002800 RecordData WeakUndeclaredIdentifiers;
2801 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00002802 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002803 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2804 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2805 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2806 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2807 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2808 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2809 }
2810 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002811
Douglas Gregor14c22f22009-04-22 22:18:58 +00002812 // Build a record containing all of the locally-scoped external
2813 // declarations in this header file. Generally, this record will be
2814 // empty.
2815 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002816 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002817 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002818 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002819 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2820 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00002821 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002822 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00002823 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2824 }
2825
Douglas Gregorb81c1702009-04-27 20:06:05 +00002826 // Build a record containing all of the ext_vector declarations.
2827 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00002828 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002829
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002830 // Build a record containing all of the VTable uses information.
2831 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002832 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002833 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2834 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2835 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2836 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2837 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002838 }
2839
2840 // Build a record containing all of dynamic classes declarations.
2841 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00002842 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002843
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002844 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002845 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002846 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002847 I = SemaRef.PendingInstantiations.begin(),
2848 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2849 AddDeclRef(I->first, PendingInstantiations);
2850 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002851 }
2852 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2853 "There are local ones at end of translation unit!");
2854
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002855 // Build a record containing some declaration references.
2856 RecordData SemaDeclRefs;
2857 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2858 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2859 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2860 }
2861
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002862 RecordData CUDASpecialDeclRefs;
2863 if (Context.getcudaConfigureCallDecl()) {
2864 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2865 }
2866
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002867 // Build a record containing all of the known namespaces.
2868 RecordData KnownNamespaces;
2869 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2870 I = SemaRef.KnownNamespaces.begin(),
2871 IEnd = SemaRef.KnownNamespaces.end();
2872 I != IEnd; ++I) {
2873 if (!I->second)
2874 AddDeclRef(I->first, KnownNamespaces);
2875 }
2876
Sebastian Redl3397c552010-08-18 23:56:27 +00002877 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002878 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002879 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002880 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002881 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00002882 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00002883 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002884 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Douglas Gregor69a9e012011-08-01 16:54:33 +00002885
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002886 if (Chain) {
2887 // Write the mapping information describing our module dependencies and how
2888 // each of those modules were mapped into our own offset/ID space, so that
2889 // the reader can build the appropriate mapping to its own offset/ID space.
2890 // The map consists solely of a blob with the following format:
2891 // *(module-name-len:i16 module-name:len*i8
2892 // source-location-offset:i32
2893 // identifier-id:i32
2894 // preprocessed-entity-id:i32
2895 // macro-definition-id:i32
2896 // selector-id:i32
2897 // declaration-id:i32
2898 // c++-base-specifiers-id:i32
2899 // type-id:i32)
2900 //
2901 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2902 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
2903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2904 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
2905 llvm::SmallString<2048> Buffer;
2906 {
2907 llvm::raw_svector_ostream Out(Buffer);
2908 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
2909 MEnd = Chain->ModuleMgr.end();
2910 M != MEnd; ++M) {
2911 StringRef FileName = (*M)->FileName;
2912 io::Emit16(Out, FileName.size());
2913 Out.write(FileName.data(), FileName.size());
2914 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
2915 io::Emit32(Out, (*M)->BaseIdentifierID);
2916 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002917 io::Emit32(Out, (*M)->BaseSelectorID);
2918 io::Emit32(Out, (*M)->BaseDeclID);
2919 io::Emit32(Out, (*M)->BaseTypeIndex);
2920 }
2921 }
2922 Record.clear();
2923 Record.push_back(MODULE_OFFSET_MAP);
2924 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
2925 Buffer.data(), Buffer.size());
2926 }
2927
2928 // Create a lexical update block containing all of the declarations in the
2929 // translation unit that do not come from other AST files.
2930 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
2931 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
2932 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2933 E = TU->noload_decls_end();
2934 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002935 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002936 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
2937 else if ((*I)->isChangedSinceDeserialization())
2938 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
2939 }
2940
2941 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
2942 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
2943 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2944 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2945 Record.clear();
2946 Record.push_back(TU_UPDATE_LEXICAL);
2947 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2948 data(NewGlobalDecls));
2949
2950 // And a visible updates block for the translation unit.
2951 Abv = new llvm::BitCodeAbbrev();
2952 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2953 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2954 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2955 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2956 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2957 WriteDeclContextVisibleUpdate(TU);
2958
2959 // If the translation unit has an anonymous namespace, and we don't already
2960 // have an update block for it, write it as an update block.
2961 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
2962 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
2963 if (Record.empty()) {
2964 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00002965 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002966 }
2967 }
2968
Douglas Gregor61c5e342011-09-17 00:05:03 +00002969 // Resolve any declaration pointers within the declaration updates block and
2970 // chained Objective-C categories block to declaration IDs.
2971 ResolveDeclUpdatesBlocks();
2972 ResolveChainedObjCCategories();
2973
Douglas Gregora119da02011-08-02 16:26:37 +00002974 // Form the record of special types.
2975 RecordData SpecialTypes;
2976 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor30403a62011-08-11 22:04:35 +00002977 AddTypeRef(Context.ObjCProtoType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00002978 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00002979 AddTypeRef(Context.getFILEType(), SpecialTypes);
2980 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
2981 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
2982 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
2983 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00002984 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002985
Douglas Gregor366809a2009-04-26 03:49:13 +00002986 // Keep writing types and declarations until all types and
2987 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00002988 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002989 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002990 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
2991 E = DeclsToRewrite.end();
2992 I != E; ++I)
2993 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002994 while (!DeclTypesToEmit.empty()) {
2995 DeclOrType DOT = DeclTypesToEmit.front();
2996 DeclTypesToEmit.pop();
2997 if (DOT.isType())
2998 WriteType(DOT.getType());
2999 else
3000 WriteDecl(Context, DOT.getDecl());
3001 }
3002 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003003
Douglas Gregor7143aab2011-09-01 17:04:32 +00003004 WritePreprocessor(PP, IsModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003005 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003006 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003007 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor7143aab2011-09-01 17:04:32 +00003008 WriteIdentifierTable(PP, IsModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003009 WriteFPPragmaOptions(SemaRef.getFPOptions());
3010 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003011
Sebastian Redl1476ed42010-07-16 16:36:56 +00003012 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003013 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003014
Anders Carlssonc8505782011-03-06 18:41:18 +00003015 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003016
Douglas Gregora119da02011-08-02 16:26:37 +00003017 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3018
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003019 /// Build a record containing first declarations from a chained PCH and the
3020 /// most recent declarations in this AST that they point to.
3021 RecordData FirstLatestDeclIDs;
3022 for (FirstLatestDeclMap::iterator I = FirstLatestDecls.begin(),
3023 E = FirstLatestDecls.end();
3024 I != E; ++I) {
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003025 AddDeclRef(I->first, FirstLatestDeclIDs);
3026 AddDeclRef(I->second, FirstLatestDeclIDs);
3027 }
3028
3029 if (!FirstLatestDeclIDs.empty())
3030 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
3031
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003032 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003033 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003034 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003035
3036 // Write the record containing tentative definitions.
3037 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003038 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003039
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003040 // Write the record containing unused file scoped decls.
3041 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003042 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003043
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003044 // Write the record containing weak undeclared identifiers.
3045 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003046 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003047 WeakUndeclaredIdentifiers);
3048
Douglas Gregor14c22f22009-04-22 22:18:58 +00003049 // Write the record containing locally-scoped external definitions.
3050 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003051 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003052 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003053
3054 // Write the record containing ext_vector type names.
3055 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003056 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003057
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003058 // Write the record containing VTable uses information.
3059 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003060 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003061
3062 // Write the record containing dynamic classes declarations.
3063 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003064 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003065
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003066 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003067 if (!PendingInstantiations.empty())
3068 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003069
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003070 // Write the record containing declaration references of Sema.
3071 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003072 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003073
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003074 // Write the record containing CUDA-specific declaration references.
3075 if (!CUDASpecialDeclRefs.empty())
3076 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003077
3078 // Write the delegating constructors.
3079 if (!DelegatingCtorDecls.empty())
3080 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003081
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003082 // Write the known namespaces.
3083 if (!KnownNamespaces.empty())
3084 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3085
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003086 // Write the visible updates to DeclContexts.
3087 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3088 I = UpdatedDeclContexts.begin(),
3089 E = UpdatedDeclContexts.end();
3090 I != E; ++I)
3091 WriteDeclContextVisibleUpdate(*I);
3092
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003093 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003094 WriteDeclReplacementsBlock();
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003095 WriteChainedObjCCategories();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003096
Douglas Gregor3e1af842009-04-17 22:13:46 +00003097 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003098 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003099 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003100 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003101 Record.push_back(NumLexicalDeclContexts);
3102 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003103 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003104 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003105}
3106
Douglas Gregor61c5e342011-09-17 00:05:03 +00003107/// \brief Go through the declaration update blocks and resolve declaration
3108/// pointers into declaration IDs.
3109void ASTWriter::ResolveDeclUpdatesBlocks() {
3110 for (DeclUpdateMap::iterator
3111 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3112 const Decl *D = I->first;
3113 UpdateRecord &URec = I->second;
3114
3115 if (DeclsToRewrite.count(D))
3116 continue; // The decl will be written completely
3117
3118 unsigned Idx = 0, N = URec.size();
3119 while (Idx < N) {
3120 switch ((DeclUpdateKind)URec[Idx++]) {
3121 case UPD_CXX_SET_DEFINITIONDATA:
3122 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3123 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3124 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3125 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3126 ++Idx;
3127 break;
3128
3129 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3130 ++Idx;
3131 break;
3132 }
3133 }
3134 }
3135}
3136
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003137void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003138 if (DeclUpdates.empty())
3139 return;
3140
3141 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003142 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003143 for (DeclUpdateMap::iterator
3144 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3145 const Decl *D = I->first;
3146 UpdateRecord &URec = I->second;
3147
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003148 if (DeclsToRewrite.count(D))
3149 continue; // The decl will be written completely,no need to store updates.
3150
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003151 uint64_t Offset = Stream.GetCurrentBitNo();
3152 Stream.EmitRecord(DECL_UPDATES, URec);
3153
3154 OffsetsRecord.push_back(GetDeclRef(D));
3155 OffsetsRecord.push_back(Offset);
3156 }
3157 Stream.ExitBlock();
3158 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3159}
3160
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003161void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003162 if (ReplacedDecls.empty())
3163 return;
3164
3165 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003166 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003167 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3168 Record.push_back(I->first);
3169 Record.push_back(I->second);
3170 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003171 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003172}
3173
Douglas Gregor61c5e342011-09-17 00:05:03 +00003174void ASTWriter::ResolveChainedObjCCategories() {
3175 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3176 I = LocalChainedObjCCategories.begin(),
3177 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3178 ChainedObjCCategoriesData &Data = *I;
3179 Data.InterfaceID = GetDeclRef(Data.Interface);
3180 Data.TailCategoryID = GetDeclRef(Data.TailCategory);
3181 }
3182
3183}
3184
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003185void ASTWriter::WriteChainedObjCCategories() {
3186 if (LocalChainedObjCCategories.empty())
3187 return;
3188
3189 RecordData Record;
3190 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3191 I = LocalChainedObjCCategories.begin(),
3192 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3193 ChainedObjCCategoriesData &Data = *I;
3194 serialization::DeclID
3195 HeadCatID = getDeclID(Data.Interface->getCategoryList());
3196 assert(HeadCatID != 0 && "Category not written ?");
3197
3198 Record.push_back(Data.InterfaceID);
3199 Record.push_back(HeadCatID);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003200 Record.push_back(Data.TailCategoryID);
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003201 }
3202 Stream.EmitRecord(OBJC_CHAINED_CATEGORIES, Record);
3203}
3204
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003205void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003206 Record.push_back(Loc.getRawEncoding());
3207}
3208
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003209void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003210 AddSourceLocation(Range.getBegin(), Record);
3211 AddSourceLocation(Range.getEnd(), Record);
3212}
3213
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003214void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003215 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003216 const uint64_t *Words = Value.getRawData();
3217 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003218}
3219
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003220void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003221 Record.push_back(Value.isUnsigned());
3222 AddAPInt(Value, Record);
3223}
3224
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003225void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003226 AddAPInt(Value.bitcastToAPInt(), Record);
3227}
3228
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003229void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003230 Record.push_back(getIdentifierRef(II));
3231}
3232
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003233IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003234 if (II == 0)
3235 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003236
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003237 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003238 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003239 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003240 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003241}
3242
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003243void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003244 Record.push_back(getSelectorRef(SelRef));
3245}
3246
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003247SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003248 if (Sel.getAsOpaquePtr() == 0) {
3249 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003250 }
3251
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003252 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003253 if (SID == 0 && Chain) {
3254 // This might trigger a ReadSelector callback, which will set the ID for
3255 // this selector.
3256 Chain->LoadSelector(Sel);
3257 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003258 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003259 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003260 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003261 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003262}
3263
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003264void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003265 AddDeclRef(Temp->getDestructor(), Record);
3266}
3267
Douglas Gregor7c789c12010-10-29 22:39:52 +00003268void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3269 CXXBaseSpecifier const *BasesEnd,
3270 RecordDataImpl &Record) {
3271 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3272 CXXBaseSpecifiersToWrite.push_back(
3273 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3274 Bases, BasesEnd));
3275 Record.push_back(NextCXXBaseSpecifiersID++);
3276}
3277
Sebastian Redla4232eb2010-08-18 23:56:21 +00003278void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003279 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003280 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003281 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003282 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003283 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003284 break;
3285 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003286 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003287 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003288 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003289 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003290 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003291 break;
3292 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003293 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003294 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003295 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003296 break;
John McCall833ca992009-10-29 08:12:44 +00003297 case TemplateArgument::Null:
3298 case TemplateArgument::Integral:
3299 case TemplateArgument::Declaration:
3300 case TemplateArgument::Pack:
3301 break;
3302 }
3303}
3304
Sebastian Redla4232eb2010-08-18 23:56:21 +00003305void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003306 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003307 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003308
3309 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3310 bool InfoHasSameExpr
3311 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3312 Record.push_back(InfoHasSameExpr);
3313 if (InfoHasSameExpr)
3314 return; // Avoid storing the same expr twice.
3315 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003316 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3317 Record);
3318}
3319
Douglas Gregordc355712011-02-25 00:36:19 +00003320void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3321 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003322 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003323 AddTypeRef(QualType(), Record);
3324 return;
3325 }
3326
Douglas Gregordc355712011-02-25 00:36:19 +00003327 AddTypeLoc(TInfo->getTypeLoc(), Record);
3328}
3329
3330void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3331 AddTypeRef(TL.getType(), Record);
3332
John McCalla1ee0c52009-10-16 21:56:05 +00003333 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003334 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003335 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003336}
3337
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003338void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003339 Record.push_back(GetOrCreateTypeID(T));
3340}
3341
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003342TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3343 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003344 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3345}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003346
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003347TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003348 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003349 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003350}
3351
3352TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3353 if (T.isNull())
3354 return TypeIdx();
3355 assert(!T.getLocalFastQualifiers());
3356
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003357 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003358 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003359 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003360 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003361 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003362 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003363 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003364 return Idx;
3365}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003366
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003367TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003368 if (T.isNull())
3369 return TypeIdx();
3370 assert(!T.getLocalFastQualifiers());
3371
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003372 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3373 assert(I != TypeIdxs.end() && "Type not emitted!");
3374 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003375}
3376
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003377void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003378 Record.push_back(GetDeclRef(D));
3379}
3380
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003381DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003382 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3383
Douglas Gregor2cf26342009-04-09 22:27:44 +00003384 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003385 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003386 }
Douglas Gregor97475832010-10-05 18:37:06 +00003387 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003388 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003389 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003390 // We haven't seen this declaration before. Give it a new ID and
3391 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003392 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003393 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003394 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3395 // We don't add it to the replacement collection here, because we don't
3396 // have the offset yet.
3397 DeclTypesToEmit.push(const_cast<Decl *>(D));
3398 // Reset the flag, so that we don't add this decl multiple times.
3399 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003400 }
3401
Sebastian Redl681d7232010-07-27 00:17:23 +00003402 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003403}
3404
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003405DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003406 if (D == 0)
3407 return 0;
3408
3409 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3410 return DeclIDs[D];
3411}
3412
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003413void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003414 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003415 Record.push_back(Name.getNameKind());
3416 switch (Name.getNameKind()) {
3417 case DeclarationName::Identifier:
3418 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3419 break;
3420
3421 case DeclarationName::ObjCZeroArgSelector:
3422 case DeclarationName::ObjCOneArgSelector:
3423 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003424 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003425 break;
3426
3427 case DeclarationName::CXXConstructorName:
3428 case DeclarationName::CXXDestructorName:
3429 case DeclarationName::CXXConversionFunctionName:
3430 AddTypeRef(Name.getCXXNameType(), Record);
3431 break;
3432
3433 case DeclarationName::CXXOperatorName:
3434 Record.push_back(Name.getCXXOverloadedOperator());
3435 break;
3436
Sean Hunt3e518bd2009-11-29 07:34:05 +00003437 case DeclarationName::CXXLiteralOperatorName:
3438 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3439 break;
3440
Douglas Gregor2cf26342009-04-09 22:27:44 +00003441 case DeclarationName::CXXUsingDirective:
3442 // No extra data to emit
3443 break;
3444 }
3445}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003446
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003447void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003448 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003449 switch (Name.getNameKind()) {
3450 case DeclarationName::CXXConstructorName:
3451 case DeclarationName::CXXDestructorName:
3452 case DeclarationName::CXXConversionFunctionName:
3453 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3454 break;
3455
3456 case DeclarationName::CXXOperatorName:
3457 AddSourceLocation(
3458 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3459 Record);
3460 AddSourceLocation(
3461 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3462 Record);
3463 break;
3464
3465 case DeclarationName::CXXLiteralOperatorName:
3466 AddSourceLocation(
3467 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3468 Record);
3469 break;
3470
3471 case DeclarationName::Identifier:
3472 case DeclarationName::ObjCZeroArgSelector:
3473 case DeclarationName::ObjCOneArgSelector:
3474 case DeclarationName::ObjCMultiArgSelector:
3475 case DeclarationName::CXXUsingDirective:
3476 break;
3477 }
3478}
3479
3480void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003481 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003482 AddDeclarationName(NameInfo.getName(), Record);
3483 AddSourceLocation(NameInfo.getLoc(), Record);
3484 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3485}
3486
3487void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003488 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003489 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003490 Record.push_back(Info.NumTemplParamLists);
3491 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3492 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3493}
3494
Sebastian Redla4232eb2010-08-18 23:56:21 +00003495void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003496 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003497 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003498 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003499 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003500
3501 // Push each of the NNS's onto a stack for serialization in reverse order.
3502 while (NNS) {
3503 NestedNames.push_back(NNS);
3504 NNS = NNS->getPrefix();
3505 }
3506
3507 Record.push_back(NestedNames.size());
3508 while(!NestedNames.empty()) {
3509 NNS = NestedNames.pop_back_val();
3510 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3511 Record.push_back(Kind);
3512 switch (Kind) {
3513 case NestedNameSpecifier::Identifier:
3514 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3515 break;
3516
3517 case NestedNameSpecifier::Namespace:
3518 AddDeclRef(NNS->getAsNamespace(), Record);
3519 break;
3520
Douglas Gregor14aba762011-02-24 02:36:08 +00003521 case NestedNameSpecifier::NamespaceAlias:
3522 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3523 break;
3524
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003525 case NestedNameSpecifier::TypeSpec:
3526 case NestedNameSpecifier::TypeSpecWithTemplate:
3527 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3528 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3529 break;
3530
3531 case NestedNameSpecifier::Global:
3532 // Don't need to write an associated value.
3533 break;
3534 }
3535 }
3536}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003537
Douglas Gregordc355712011-02-25 00:36:19 +00003538void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3539 RecordDataImpl &Record) {
3540 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003541 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003542 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00003543
3544 // Push each of the nested-name-specifiers's onto a stack for
3545 // serialization in reverse order.
3546 while (NNS) {
3547 NestedNames.push_back(NNS);
3548 NNS = NNS.getPrefix();
3549 }
3550
3551 Record.push_back(NestedNames.size());
3552 while(!NestedNames.empty()) {
3553 NNS = NestedNames.pop_back_val();
3554 NestedNameSpecifier::SpecifierKind Kind
3555 = NNS.getNestedNameSpecifier()->getKind();
3556 Record.push_back(Kind);
3557 switch (Kind) {
3558 case NestedNameSpecifier::Identifier:
3559 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3560 AddSourceRange(NNS.getLocalSourceRange(), Record);
3561 break;
3562
3563 case NestedNameSpecifier::Namespace:
3564 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3565 AddSourceRange(NNS.getLocalSourceRange(), Record);
3566 break;
3567
3568 case NestedNameSpecifier::NamespaceAlias:
3569 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3570 AddSourceRange(NNS.getLocalSourceRange(), Record);
3571 break;
3572
3573 case NestedNameSpecifier::TypeSpec:
3574 case NestedNameSpecifier::TypeSpecWithTemplate:
3575 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3576 AddTypeLoc(NNS.getTypeLoc(), Record);
3577 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3578 break;
3579
3580 case NestedNameSpecifier::Global:
3581 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3582 break;
3583 }
3584 }
3585}
3586
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003587void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003588 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003589 Record.push_back(Kind);
3590 switch (Kind) {
3591 case TemplateName::Template:
3592 AddDeclRef(Name.getAsTemplateDecl(), Record);
3593 break;
3594
3595 case TemplateName::OverloadedTemplate: {
3596 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3597 Record.push_back(OvT->size());
3598 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3599 I != E; ++I)
3600 AddDeclRef(*I, Record);
3601 break;
3602 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003603
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003604 case TemplateName::QualifiedTemplate: {
3605 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3606 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3607 Record.push_back(QualT->hasTemplateKeyword());
3608 AddDeclRef(QualT->getTemplateDecl(), Record);
3609 break;
3610 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003611
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003612 case TemplateName::DependentTemplate: {
3613 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3614 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3615 Record.push_back(DepT->isIdentifier());
3616 if (DepT->isIdentifier())
3617 AddIdentifierRef(DepT->getIdentifier(), Record);
3618 else
3619 Record.push_back(DepT->getOperator());
3620 break;
3621 }
John McCall14606042011-06-30 08:33:18 +00003622
3623 case TemplateName::SubstTemplateTemplateParm: {
3624 SubstTemplateTemplateParmStorage *subst
3625 = Name.getAsSubstTemplateTemplateParm();
3626 AddDeclRef(subst->getParameter(), Record);
3627 AddTemplateName(subst->getReplacement(), Record);
3628 break;
3629 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003630
3631 case TemplateName::SubstTemplateTemplateParmPack: {
3632 SubstTemplateTemplateParmPackStorage *SubstPack
3633 = Name.getAsSubstTemplateTemplateParmPack();
3634 AddDeclRef(SubstPack->getParameterPack(), Record);
3635 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3636 break;
3637 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003638 }
3639}
3640
Michael J. Spencer20249a12010-10-21 03:16:25 +00003641void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003642 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003643 Record.push_back(Arg.getKind());
3644 switch (Arg.getKind()) {
3645 case TemplateArgument::Null:
3646 break;
3647 case TemplateArgument::Type:
3648 AddTypeRef(Arg.getAsType(), Record);
3649 break;
3650 case TemplateArgument::Declaration:
3651 AddDeclRef(Arg.getAsDecl(), Record);
3652 break;
3653 case TemplateArgument::Integral:
3654 AddAPSInt(*Arg.getAsIntegral(), Record);
3655 AddTypeRef(Arg.getIntegralType(), Record);
3656 break;
3657 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003658 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3659 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003660 case TemplateArgument::TemplateExpansion:
3661 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003662 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3663 Record.push_back(*NumExpansions + 1);
3664 else
3665 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003666 break;
3667 case TemplateArgument::Expression:
3668 AddStmt(Arg.getAsExpr());
3669 break;
3670 case TemplateArgument::Pack:
3671 Record.push_back(Arg.pack_size());
3672 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3673 I != E; ++I)
3674 AddTemplateArgument(*I, Record);
3675 break;
3676 }
3677}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003678
3679void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003680ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003681 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003682 assert(TemplateParams && "No TemplateParams!");
3683 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3684 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3685 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3686 Record.push_back(TemplateParams->size());
3687 for (TemplateParameterList::const_iterator
3688 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3689 P != PEnd; ++P)
3690 AddDeclRef(*P, Record);
3691}
3692
3693/// \brief Emit a template argument list.
3694void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003695ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003696 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003697 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003698 Record.push_back(TemplateArgs->size());
3699 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003700 AddTemplateArgument(TemplateArgs->get(i), Record);
3701}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003702
3703
3704void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003705ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003706 Record.push_back(Set.size());
3707 for (UnresolvedSetImpl::const_iterator
3708 I = Set.begin(), E = Set.end(); I != E; ++I) {
3709 AddDeclRef(I.getDecl(), Record);
3710 Record.push_back(I.getAccess());
3711 }
3712}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003713
Sebastian Redla4232eb2010-08-18 23:56:21 +00003714void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003715 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003716 Record.push_back(Base.isVirtual());
3717 Record.push_back(Base.isBaseOfClass());
3718 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003719 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003720 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003721 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003722 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3723 : SourceLocation(),
3724 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003725}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003726
Douglas Gregor7c789c12010-10-29 22:39:52 +00003727void ASTWriter::FlushCXXBaseSpecifiers() {
3728 RecordData Record;
3729 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3730 Record.clear();
3731
3732 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00003733 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003734 if (Index == CXXBaseSpecifiersOffsets.size())
3735 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3736 else {
3737 if (Index > CXXBaseSpecifiersOffsets.size())
3738 CXXBaseSpecifiersOffsets.resize(Index + 1);
3739 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3740 }
3741
3742 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3743 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3744 Record.push_back(BEnd - B);
3745 for (; B != BEnd; ++B)
3746 AddCXXBaseSpecifier(*B, Record);
3747 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003748
3749 // Flush any expressions that were written as part of the base specifiers.
3750 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003751 }
3752
3753 CXXBaseSpecifiersToWrite.clear();
3754}
3755
Sean Huntcbb67482011-01-08 20:30:50 +00003756void ASTWriter::AddCXXCtorInitializers(
3757 const CXXCtorInitializer * const *CtorInitializers,
3758 unsigned NumCtorInitializers,
3759 RecordDataImpl &Record) {
3760 Record.push_back(NumCtorInitializers);
3761 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3762 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003763
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003764 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00003765 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003766 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3767 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00003768 } else if (Init->isDelegatingInitializer()) {
3769 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3770 AddDeclRef(Init->getTargetConstructor(), Record);
3771 } else if (Init->isMemberInitializer()){
3772 Record.push_back(CTOR_INITIALIZER_MEMBER);
3773 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003774 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00003775 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3776 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003777 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003778
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003779 AddSourceLocation(Init->getMemberLocation(), Record);
3780 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003781 AddSourceLocation(Init->getLParenLoc(), Record);
3782 AddSourceLocation(Init->getRParenLoc(), Record);
3783 Record.push_back(Init->isWritten());
3784 if (Init->isWritten()) {
3785 Record.push_back(Init->getSourceOrder());
3786 } else {
3787 Record.push_back(Init->getNumArrayIndices());
3788 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3789 AddDeclRef(Init->getArrayIndex(i), Record);
3790 }
3791 }
3792}
3793
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003794void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3795 assert(D->DefinitionData);
3796 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3797 Record.push_back(Data.UserDeclaredConstructor);
3798 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00003799 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003800 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00003801 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003802 Record.push_back(Data.UserDeclaredDestructor);
3803 Record.push_back(Data.Aggregate);
3804 Record.push_back(Data.PlainOldData);
3805 Record.push_back(Data.Empty);
3806 Record.push_back(Data.Polymorphic);
3807 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00003808 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00003809 Record.push_back(Data.HasNoNonEmptyBases);
3810 Record.push_back(Data.HasPrivateFields);
3811 Record.push_back(Data.HasProtectedFields);
3812 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00003813 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00003814 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00003815 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003816 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003817 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003818 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003819 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003820 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003821 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003822 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00003823 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003824 Record.push_back(Data.DeclaredDefaultConstructor);
3825 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00003826 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003827 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00003828 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003829 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00003830 Record.push_back(Data.FailedImplicitMoveConstructor);
3831 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003832
3833 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003834 if (Data.NumBases > 0)
3835 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3836 Record);
3837
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003838 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3839 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003840 if (Data.NumVBases > 0)
3841 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3842 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003843
3844 AddUnresolvedSet(Data.Conversions, Record);
3845 AddUnresolvedSet(Data.VisibleConversions, Record);
3846 // Data.Definition is the owning decl, no need to write it.
3847 AddDeclRef(Data.FirstFriend, Record);
3848}
3849
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003850void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003851 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00003852 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003853 assert(FirstDeclID == NextDeclID &&
3854 FirstTypeID == NextTypeID &&
3855 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003856 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003857 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003858
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003859 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003860
Douglas Gregor10bc00f2011-08-18 04:12:04 +00003861 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
3862 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
3863 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
3864 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003865 NextDeclID = FirstDeclID;
3866 NextTypeID = FirstTypeID;
3867 NextIdentID = FirstIdentID;
3868 NextSelectorID = FirstSelectorID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003869}
3870
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003871void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003872 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003873 if (II->hasMacroDefinition())
3874 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003875}
3876
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003877void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003878 // Always take the highest-numbered type index. This copes with an interesting
3879 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003880 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003881 // keep the higher-numbered entry so that we can properly write it out to
3882 // the AST file.
3883 TypeIdx &StoredIdx = TypeIdxs[T];
3884 if (Idx.getIndex() >= StoredIdx.getIndex())
3885 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003886}
3887
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003888void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003889 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003890}
Sebastian Redl5d050072010-08-04 17:20:04 +00003891
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003892void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003893 SelectorIDs[S] = ID;
3894}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003895
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003896void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003897 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003898 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00003899 MacroDefinitions[MD] = ID;
3900}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003901
3902void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3903 assert(D->isDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00003904 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003905 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3906 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00003907 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003908 // A forward reference was mutated into a definition. Rewrite it.
3909 // FIXME: This happens during template instantiation, should we
3910 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003911 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003912 }
3913
3914 for (CXXRecordDecl::redecl_iterator
3915 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3916 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3917 if (Redecl == RD)
3918 continue;
3919
3920 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00003921 if (Redecl->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003922 UpdateRecord &Record = DeclUpdates[Redecl];
3923 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3924 assert(Redecl->DefinitionData);
3925 assert(Redecl->DefinitionData->Definition == D);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003926 Record.push_back(reinterpret_cast<uint64_t>(D)); // the DefinitionDecl
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003927 }
3928 }
3929 }
3930}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003931void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003932 assert(!WritingAST && "Already writing the AST!");
3933
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003934 // TU and namespaces are handled elsewhere.
3935 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3936 return;
3937
Douglas Gregor919814d2011-09-09 23:01:35 +00003938 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003939 return; // Not a source decl added to a DeclContext from PCH.
3940
3941 AddUpdatedDeclContext(DC);
3942}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003943
3944void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003945 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003946 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00003947 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003948 return; // Not a source member added to a class from PCH.
3949 if (!isa<CXXMethodDecl>(D))
3950 return; // We are interested in lazily declared implicit methods.
3951
3952 // A decl coming from PCH was modified.
3953 assert(RD->isDefinition());
3954 UpdateRecord &Record = DeclUpdates[RD];
3955 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003956 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003957}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003958
3959void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3960 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003961 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003962 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003963 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00003964 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003965 return; // Not a source specialization added to a template from PCH.
3966
3967 UpdateRecord &Record = DeclUpdates[TD];
3968 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003969 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003970}
Douglas Gregor89d99802010-11-30 06:16:57 +00003971
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003972void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
3973 const FunctionDecl *D) {
3974 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003975 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003976 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00003977 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003978 return; // Not a source specialization added to a template from PCH.
3979
3980 UpdateRecord &Record = DeclUpdates[TD];
3981 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003982 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003983}
3984
Sebastian Redl58a2cd82011-04-24 16:28:06 +00003985void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003986 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00003987 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00003988 return; // Declaration not imported from PCH.
3989
3990 // Implicit decl from a PCH was defined.
3991 // FIXME: Should implicit definition be a separate FunctionDecl?
3992 RewriteDecl(D);
3993}
3994
Sebastian Redlf79a7192011-04-29 08:19:30 +00003995void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003996 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00003997 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00003998 return;
3999
4000 // Since the actual instantiation is delayed, this really means that we need
4001 // to update the instantiation location.
4002 UpdateRecord &Record = DeclUpdates[D];
4003 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4004 AddSourceLocation(
4005 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4006}
4007
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004008void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4009 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004010 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004011 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004012 return; // Declaration not imported from PCH.
4013 if (CatD->getNextClassCategory() &&
Douglas Gregor919814d2011-09-09 23:01:35 +00004014 !CatD->getNextClassCategory()->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004015 return; // We already recorded that the tail of a category chain should be
4016 // attached to an interface.
4017
Douglas Gregor61c5e342011-09-17 00:05:03 +00004018 ChainedObjCCategoriesData Data = { IFD, CatD, 0, 0 };
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004019 LocalChainedObjCCategories.push_back(Data);
4020}