blob: b6198184cca92f0c770904d4c1a26d3d161d46ec [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Sebastian Redlade50002010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000059}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
66
Douglas Gregor2cf26342009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000070
Douglas Gregor2cf26342009-04-09 22:27:44 +000071namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl3397c552010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000116}
117
Sebastian Redl3397c552010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159}
160
Sebastian Redl3397c552010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175}
176
Sebastian Redl3397c552010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
188 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000189 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000190 Record.push_back(T->getExceptionSpecType());
191 if (T->getExceptionSpecType() == EST_Dynamic) {
192 Record.push_back(T->getNumExceptions());
193 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
194 Writer.AddTypeRef(T->getExceptionType(I), Record);
195 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
196 Writer.AddStmt(T->getNoexceptExpr());
197 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000198 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199}
200
Sebastian Redl3397c552010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000202 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000203 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000204}
John McCalled976492009-12-04 22:46:56 +0000205
Sebastian Redl3397c552010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000207 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000208 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
209 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000210 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000211}
212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000214 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000215 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000216}
217
Sebastian Redl3397c552010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000226}
227
Sean Huntca63c202011-05-24 22:41:36 +0000228void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
229 Writer.AddTypeRef(T->getBaseType(), Record);
230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
231 Record.push_back(T->getUTTKind());
232 Code = TYPE_UNARY_TRANSFORM;
233}
234
Richard Smith34b41d92011-02-20 03:19:35 +0000235void ASTTypeWriter::VisitAutoType(const AutoType *T) {
236 Writer.AddTypeRef(T->getDeducedType(), Record);
237 Code = TYPE_AUTO;
238}
239
Sebastian Redl3397c552010-08-18 23:56:27 +0000240void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000241 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000242 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000243 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000244 "Cannot serialize in the middle of a type definition");
245}
246
Sebastian Redl3397c552010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000249 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000250}
251
Sebastian Redl3397c552010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000254 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255}
256
John McCall9d156a72011-01-06 01:58:22 +0000257void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
258 Writer.AddTypeRef(T->getModifiedType(), Record);
259 Writer.AddTypeRef(T->getEquivalentType(), Record);
260 Record.push_back(T->getAttrKind());
261 Code = TYPE_ATTRIBUTED;
262}
263
Mike Stump1eb44332009-09-09 15:08:12 +0000264void
Sebastian Redl3397c552010-08-18 23:56:27 +0000265ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000266 const SubstTemplateTypeParmType *T) {
267 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
268 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000269 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000270}
271
272void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
274 const SubstTemplateTypeParmPackType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
278}
279
280void
Sebastian Redl3397c552010-08-18 23:56:27 +0000281ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000282 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000283 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000284 Writer.AddTemplateName(T->getTemplateName(), Record);
285 Record.push_back(T->getNumArgs());
286 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
287 ArgI != ArgE; ++ArgI)
288 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000289 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
290 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000291 : T->getCanonicalTypeInternal(),
292 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000293 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000294}
295
296void
Sebastian Redl3397c552010-08-18 23:56:27 +0000297ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000298 VisitArrayType(T);
299 Writer.AddStmt(T->getSizeExpr());
300 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl3397c552010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000306 const DependentSizedExtVectorType *T) {
307 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000308 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl3397c552010-08-18 23:56:27 +0000312ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313 Record.push_back(T->getDepth());
314 Record.push_back(T->getIndex());
315 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000316 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000317 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000318}
319
320void
Sebastian Redl3397c552010-08-18 23:56:27 +0000321ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000322 Record.push_back(T->getKeyword());
323 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
324 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000325 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
326 : T->getCanonicalTypeInternal(),
327 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000333 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000334 Record.push_back(T->getKeyword());
335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddIdentifierRef(T->getIdentifier(), Record);
337 Record.push_back(T->getNumArgs());
338 for (DependentTemplateSpecializationType::iterator
339 I = T->begin(), E = T->end(); I != E; ++I)
340 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000341 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000342}
343
Douglas Gregor7536dd52010-12-20 02:24:11 +0000344void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
345 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000346 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
347 Record.push_back(*NumExpansions + 1);
348 else
349 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000350 Code = TYPE_PACK_EXPANSION;
351}
352
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000353void ASTTypeWriter::VisitParenType(const ParenType *T) {
354 Writer.AddTypeRef(T->getInnerType(), Record);
355 Code = TYPE_PAREN;
356}
357
Sebastian Redl3397c552010-08-18 23:56:27 +0000358void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000359 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000360 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
361 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000362 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000363}
364
Sebastian Redl3397c552010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000366 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000367 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000368 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000369}
370
Sebastian Redl3397c552010-08-18 23:56:27 +0000371void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000372 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000374}
375
Sebastian Redl3397c552010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000377 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000378 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000379 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000380 E = T->qual_end(); I != E; ++I)
381 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000382 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000383}
384
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000385void
Sebastian Redl3397c552010-08-18 23:56:27 +0000386ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000387 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000388 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000389}
390
Eli Friedmanb001de72011-10-06 23:00:33 +0000391void
392ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
393 Writer.AddTypeRef(T->getValueType(), Record);
394 Code = TYPE_ATOMIC;
395}
396
John McCalla1ee0c52009-10-16 21:56:05 +0000397namespace {
398
399class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000400 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000401 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000402
403public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000404 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000405 : Writer(Writer), Record(Record) { }
406
John McCall51bd8032009-10-18 01:05:36 +0000407#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000408#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000409 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000410#include "clang/AST/TypeLocNodes.def"
411
John McCall51bd8032009-10-18 01:05:36 +0000412 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
413 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000414};
415
416}
417
John McCall51bd8032009-10-18 01:05:36 +0000418void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
419 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000420}
John McCall51bd8032009-10-18 01:05:36 +0000421void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000422 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
423 if (TL.needsExtraLocalData()) {
424 Record.push_back(TL.getWrittenTypeSpec());
425 Record.push_back(TL.getWrittenSignSpec());
426 Record.push_back(TL.getWrittenWidthSpec());
427 Record.push_back(TL.hasModeAttr());
428 }
John McCalla1ee0c52009-10-16 21:56:05 +0000429}
John McCall51bd8032009-10-18 01:05:36 +0000430void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000432}
John McCall51bd8032009-10-18 01:05:36 +0000433void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000435}
John McCall51bd8032009-10-18 01:05:36 +0000436void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000438}
John McCall51bd8032009-10-18 01:05:36 +0000439void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000441}
John McCall51bd8032009-10-18 01:05:36 +0000442void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000444}
John McCall51bd8032009-10-18 01:05:36 +0000445void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000447 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
451 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
452 Record.push_back(TL.getSizeExpr() ? 1 : 0);
453 if (TL.getSizeExpr())
454 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
457 VisitArrayTypeLoc(TL);
458}
459void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
460 VisitArrayTypeLoc(TL);
461}
462void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
463 VisitArrayTypeLoc(TL);
464}
465void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
466 DependentSizedArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
470 DependentSizedExtVectorTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
473void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
475}
476void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
479void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000480 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
481 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000482 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000483 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
484 Writer.AddDeclRef(TL.getArg(i), Record);
485}
486void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
487 VisitFunctionTypeLoc(TL);
488}
489void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
490 VisitFunctionTypeLoc(TL);
491}
John McCalled976492009-12-04 22:46:56 +0000492void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
John McCall51bd8032009-10-18 01:05:36 +0000495void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getNameLoc(), Record);
497}
498void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
500 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
501 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000502}
503void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000504 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
505 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
506 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
507 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000508}
509void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
Sean Huntca63c202011-05-24 22:41:36 +0000512void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
513 Writer.AddSourceLocation(TL.getKWLoc(), Record);
514 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
515 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
516 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
517}
Richard Smith34b41d92011-02-20 03:19:35 +0000518void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
John McCall51bd8032009-10-18 01:05:36 +0000521void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall9d156a72011-01-06 01:58:22 +0000527void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
529 if (TL.hasAttrOperand()) {
530 SourceRange range = TL.getAttrOperandParensRange();
531 Writer.AddSourceLocation(range.getBegin(), Record);
532 Writer.AddSourceLocation(range.getEnd(), Record);
533 }
534 if (TL.hasAttrExprOperand()) {
535 Expr *operand = TL.getAttrExprOperand();
536 Record.push_back(operand ? 1 : 0);
537 if (operand) Writer.AddStmt(operand);
538 } else if (TL.hasAttrEnumOperand()) {
539 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
540 }
541}
John McCall51bd8032009-10-18 01:05:36 +0000542void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCall49a832b2009-10-18 09:09:24 +0000545void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
546 SubstTemplateTypeParmTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getNameLoc(), Record);
548}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000549void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
550 SubstTemplateTypeParmPackTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall51bd8032009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
554 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000555 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000559 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
560 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000561}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000562void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
564 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
565}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000566void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000567 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000568 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000569}
John McCall3cb0ebd2010-03-10 03:28:59 +0000570void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
Douglas Gregor4714c122010-03-31 17:34:00 +0000573void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000574 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000575 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000576 Writer.AddSourceLocation(TL.getNameLoc(), Record);
577}
John McCall33500952010-06-11 00:33:02 +0000578void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
579 DependentTemplateSpecializationTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
584 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
585 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000586 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
587 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000588}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000589void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
590 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
591}
John McCall51bd8032009-10-18 01:05:36 +0000592void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
593 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000594}
595void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
596 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000597 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
598 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
599 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
600 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000601}
John McCall54e14c42009-10-22 22:37:11 +0000602void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000604}
Eli Friedmanb001de72011-10-06 23:00:33 +0000605void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getKWLoc(), Record);
607 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
608 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
609}
John McCalla1ee0c52009-10-16 21:56:05 +0000610
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000611//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000612// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000613//===----------------------------------------------------------------------===//
614
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000615static void EmitBlockID(unsigned ID, const char *Name,
616 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000617 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000618 Record.clear();
619 Record.push_back(ID);
620 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
621
622 // Emit the block name if present.
623 if (Name == 0 || Name[0] == 0) return;
624 Record.clear();
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
628}
629
630static void EmitRecordID(unsigned ID, const char *Name,
631 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000632 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 Record.clear();
634 Record.push_back(ID);
635 while (*Name)
636 Record.push_back(*Name++);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000638}
639
640static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000641 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000642#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000643 RECORD(STMT_STOP);
644 RECORD(STMT_NULL_PTR);
645 RECORD(STMT_NULL);
646 RECORD(STMT_COMPOUND);
647 RECORD(STMT_CASE);
648 RECORD(STMT_DEFAULT);
649 RECORD(STMT_LABEL);
650 RECORD(STMT_IF);
651 RECORD(STMT_SWITCH);
652 RECORD(STMT_WHILE);
653 RECORD(STMT_DO);
654 RECORD(STMT_FOR);
655 RECORD(STMT_GOTO);
656 RECORD(STMT_INDIRECT_GOTO);
657 RECORD(STMT_CONTINUE);
658 RECORD(STMT_BREAK);
659 RECORD(STMT_RETURN);
660 RECORD(STMT_DECL);
661 RECORD(STMT_ASM);
662 RECORD(EXPR_PREDEFINED);
663 RECORD(EXPR_DECL_REF);
664 RECORD(EXPR_INTEGER_LITERAL);
665 RECORD(EXPR_FLOATING_LITERAL);
666 RECORD(EXPR_IMAGINARY_LITERAL);
667 RECORD(EXPR_STRING_LITERAL);
668 RECORD(EXPR_CHARACTER_LITERAL);
669 RECORD(EXPR_PAREN);
670 RECORD(EXPR_UNARY_OPERATOR);
671 RECORD(EXPR_SIZEOF_ALIGN_OF);
672 RECORD(EXPR_ARRAY_SUBSCRIPT);
673 RECORD(EXPR_CALL);
674 RECORD(EXPR_MEMBER);
675 RECORD(EXPR_BINARY_OPERATOR);
676 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
677 RECORD(EXPR_CONDITIONAL_OPERATOR);
678 RECORD(EXPR_IMPLICIT_CAST);
679 RECORD(EXPR_CSTYLE_CAST);
680 RECORD(EXPR_COMPOUND_LITERAL);
681 RECORD(EXPR_EXT_VECTOR_ELEMENT);
682 RECORD(EXPR_INIT_LIST);
683 RECORD(EXPR_DESIGNATED_INIT);
684 RECORD(EXPR_IMPLICIT_VALUE_INIT);
685 RECORD(EXPR_VA_ARG);
686 RECORD(EXPR_ADDR_LABEL);
687 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000688 RECORD(EXPR_CHOOSE);
689 RECORD(EXPR_GNU_NULL);
690 RECORD(EXPR_SHUFFLE_VECTOR);
691 RECORD(EXPR_BLOCK);
692 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000693 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000694 RECORD(EXPR_OBJC_STRING_LITERAL);
695 RECORD(EXPR_OBJC_ENCODE);
696 RECORD(EXPR_OBJC_SELECTOR_EXPR);
697 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
698 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
699 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
700 RECORD(EXPR_OBJC_KVC_REF_EXPR);
701 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000702 RECORD(STMT_OBJC_FOR_COLLECTION);
703 RECORD(STMT_OBJC_CATCH);
704 RECORD(STMT_OBJC_FINALLY);
705 RECORD(STMT_OBJC_AT_TRY);
706 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
707 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000708 RECORD(EXPR_CXX_OPERATOR_CALL);
709 RECORD(EXPR_CXX_CONSTRUCT);
710 RECORD(EXPR_CXX_STATIC_CAST);
711 RECORD(EXPR_CXX_DYNAMIC_CAST);
712 RECORD(EXPR_CXX_REINTERPRET_CAST);
713 RECORD(EXPR_CXX_CONST_CAST);
714 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
715 RECORD(EXPR_CXX_BOOL_LITERAL);
716 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000717 RECORD(EXPR_CXX_TYPEID_EXPR);
718 RECORD(EXPR_CXX_TYPEID_TYPE);
719 RECORD(EXPR_CXX_UUIDOF_EXPR);
720 RECORD(EXPR_CXX_UUIDOF_TYPE);
721 RECORD(EXPR_CXX_THIS);
722 RECORD(EXPR_CXX_THROW);
723 RECORD(EXPR_CXX_DEFAULT_ARG);
724 RECORD(EXPR_CXX_BIND_TEMPORARY);
725 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
726 RECORD(EXPR_CXX_NEW);
727 RECORD(EXPR_CXX_DELETE);
728 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
729 RECORD(EXPR_EXPR_WITH_CLEANUPS);
730 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
731 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
732 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
733 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
734 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
735 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
736 RECORD(EXPR_CXX_NOEXCEPT);
737 RECORD(EXPR_OPAQUE_VALUE);
738 RECORD(EXPR_BINARY_TYPE_TRAIT);
739 RECORD(EXPR_PACK_EXPANSION);
740 RECORD(EXPR_SIZEOF_PACK);
741 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000742 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000743#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000744}
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Sebastian Redla4232eb2010-08-18 23:56:21 +0000746void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000747 RecordData Record;
748 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000750#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
751#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Sebastian Redl3397c552010-08-18 23:56:27 +0000753 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000754 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000755 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000756 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000757 RECORD(TYPE_OFFSET);
758 RECORD(DECL_OFFSET);
759 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000760 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761 RECORD(IDENTIFIER_OFFSET);
762 RECORD(IDENTIFIER_TABLE);
763 RECORD(EXTERNAL_DEFINITIONS);
764 RECORD(SPECIAL_TYPES);
765 RECORD(STATISTICS);
766 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000767 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000768 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
769 RECORD(SELECTOR_OFFSETS);
770 RECORD(METHOD_POOL);
771 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000772 RECORD(SOURCE_LOCATION_OFFSETS);
773 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000774 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000775 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000776 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000777 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000778 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000779 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000780 RECORD(TU_UPDATE_LEXICAL);
781 RECORD(REDECLS_UPDATE_LATEST);
782 RECORD(SEMA_DECL_REFS);
783 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
784 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
785 RECORD(DECL_REPLACEMENTS);
786 RECORD(UPDATE_VISIBLE);
787 RECORD(DECL_UPDATE_OFFSETS);
788 RECORD(DECL_UPDATES);
789 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
790 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000791 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000792 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000793 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000794 RECORD(FP_PRAGMA_OPTIONS);
795 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000796 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000797 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
798 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000799 RECORD(MODULE_OFFSET_MAP);
800 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000801
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000802 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000803 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000804 RECORD(SM_SLOC_FILE_ENTRY);
805 RECORD(SM_SLOC_BUFFER_ENTRY);
806 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000807 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000809 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000810 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000811 RECORD(PP_MACRO_OBJECT_LIKE);
812 RECORD(PP_MACRO_FUNCTION_LIKE);
813 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000814
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000815 // Decls and Types block.
816 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000817 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000818 RECORD(TYPE_COMPLEX);
819 RECORD(TYPE_POINTER);
820 RECORD(TYPE_BLOCK_POINTER);
821 RECORD(TYPE_LVALUE_REFERENCE);
822 RECORD(TYPE_RVALUE_REFERENCE);
823 RECORD(TYPE_MEMBER_POINTER);
824 RECORD(TYPE_CONSTANT_ARRAY);
825 RECORD(TYPE_INCOMPLETE_ARRAY);
826 RECORD(TYPE_VARIABLE_ARRAY);
827 RECORD(TYPE_VECTOR);
828 RECORD(TYPE_EXT_VECTOR);
829 RECORD(TYPE_FUNCTION_PROTO);
830 RECORD(TYPE_FUNCTION_NO_PROTO);
831 RECORD(TYPE_TYPEDEF);
832 RECORD(TYPE_TYPEOF_EXPR);
833 RECORD(TYPE_TYPEOF);
834 RECORD(TYPE_RECORD);
835 RECORD(TYPE_ENUM);
836 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000837 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000838 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000839 RECORD(TYPE_DECLTYPE);
840 RECORD(TYPE_ELABORATED);
841 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
842 RECORD(TYPE_UNRESOLVED_USING);
843 RECORD(TYPE_INJECTED_CLASS_NAME);
844 RECORD(TYPE_OBJC_OBJECT);
845 RECORD(TYPE_TEMPLATE_TYPE_PARM);
846 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
847 RECORD(TYPE_DEPENDENT_NAME);
848 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
849 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
850 RECORD(TYPE_PAREN);
851 RECORD(TYPE_PACK_EXPANSION);
852 RECORD(TYPE_ATTRIBUTED);
853 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000854 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000855 RECORD(DECL_TYPEDEF);
856 RECORD(DECL_ENUM);
857 RECORD(DECL_RECORD);
858 RECORD(DECL_ENUM_CONSTANT);
859 RECORD(DECL_FUNCTION);
860 RECORD(DECL_OBJC_METHOD);
861 RECORD(DECL_OBJC_INTERFACE);
862 RECORD(DECL_OBJC_PROTOCOL);
863 RECORD(DECL_OBJC_IVAR);
864 RECORD(DECL_OBJC_AT_DEFS_FIELD);
865 RECORD(DECL_OBJC_CLASS);
866 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
867 RECORD(DECL_OBJC_CATEGORY);
868 RECORD(DECL_OBJC_CATEGORY_IMPL);
869 RECORD(DECL_OBJC_IMPLEMENTATION);
870 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
871 RECORD(DECL_OBJC_PROPERTY);
872 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000873 RECORD(DECL_FIELD);
874 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000875 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000876 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000877 RECORD(DECL_FILE_SCOPE_ASM);
878 RECORD(DECL_BLOCK);
879 RECORD(DECL_CONTEXT_LEXICAL);
880 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000881 RECORD(DECL_NAMESPACE);
882 RECORD(DECL_NAMESPACE_ALIAS);
883 RECORD(DECL_USING);
884 RECORD(DECL_USING_SHADOW);
885 RECORD(DECL_USING_DIRECTIVE);
886 RECORD(DECL_UNRESOLVED_USING_VALUE);
887 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
888 RECORD(DECL_LINKAGE_SPEC);
889 RECORD(DECL_CXX_RECORD);
890 RECORD(DECL_CXX_METHOD);
891 RECORD(DECL_CXX_CONSTRUCTOR);
892 RECORD(DECL_CXX_DESTRUCTOR);
893 RECORD(DECL_CXX_CONVERSION);
894 RECORD(DECL_ACCESS_SPEC);
895 RECORD(DECL_FRIEND);
896 RECORD(DECL_FRIEND_TEMPLATE);
897 RECORD(DECL_CLASS_TEMPLATE);
898 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
899 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
900 RECORD(DECL_FUNCTION_TEMPLATE);
901 RECORD(DECL_TEMPLATE_TYPE_PARM);
902 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
903 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
904 RECORD(DECL_STATIC_ASSERT);
905 RECORD(DECL_CXX_BASE_SPECIFIERS);
906 RECORD(DECL_INDIRECTFIELD);
907 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
908
Douglas Gregora72d8c42011-06-03 02:27:19 +0000909 // Statements and Exprs can occur in the Decls and Types block.
910 AddStmtsExprs(Stream, Record);
911
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000912 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000913 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000914 RECORD(PPD_MACRO_DEFINITION);
915 RECORD(PPD_INCLUSION_DIRECTIVE);
916
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000917#undef RECORD
918#undef BLOCK
919 Stream.ExitBlock();
920}
921
Douglas Gregore650c8c2009-07-07 00:12:59 +0000922/// \brief Adjusts the given filename to only write out the portion of the
923/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000924///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000925/// \param Filename the file name to adjust.
926///
927/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
928/// the returned filename will be adjusted by this system root.
929///
930/// \returns either the original filename (if it needs no adjustment) or the
931/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000932static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000933adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000934 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregor832d6202011-07-22 16:35:34 +0000936 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000937 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Douglas Gregore650c8c2009-07-07 00:12:59 +0000939 // Verify that the filename and the system root have the same prefix.
940 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000941 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000942 if (Filename[Pos] != isysroot[Pos])
943 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945 // We hit the end of the filename before we hit the end of the system root.
946 if (!Filename[Pos])
947 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Douglas Gregore650c8c2009-07-07 00:12:59 +0000949 // If the file name has a '/' at the current position, skip over the '/'.
950 // We distinguish sysroot-based includes from absolute includes by the
951 // absence of '/' at the beginning of sysroot-based includes.
952 if (Filename[Pos] == '/')
953 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955 return Filename + Pos;
956}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000957
Sebastian Redl3397c552010-08-18 23:56:27 +0000958/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000959void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000960 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000961 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000962
Douglas Gregore650c8c2009-07-07 00:12:59 +0000963 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000964 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000965 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000966 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000967 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
968 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
970 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
971 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000972 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000976 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000977 Record.push_back(VERSION_MAJOR);
978 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 Record.push_back(CLANG_VERSION_MAJOR);
980 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000981 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000982 const std::string &Triple = Target.getTriple().getTriple();
983 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
984
985 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000986 serialization::ModuleManager &Mgr = Chain->getModuleManager();
987 llvm::SmallVector<char, 128> ModulePaths;
988 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000989
990 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
991 M != MEnd; ++M) {
992 // Skip modules that weren't directly imported.
993 if (!(*M)->isDirectlyImported())
994 continue;
995
996 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
997 // FIXME: Write import location, once it matters.
998 // FIXME: This writes the absolute path for AST files we depend on.
999 const std::string &FileName = (*M)->FileName;
1000 Record.push_back(FileName.size());
1001 Record.append(FileName.begin(), FileName.end());
1002 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001003 Stream.EmitRecord(IMPORTS, Record);
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Douglas Gregor31d375f2011-05-06 21:43:30 +00001006 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001007 SourceManager &SM = Context.getSourceManager();
1008 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1009 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001010 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001011 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1012 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1013
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001014 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001016 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001017
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001018 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001019 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001020 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001021 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001022 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001023 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001024
1025 Record.clear();
1026 Record.push_back(SM.getMainFileID().getOpaqueValue());
1027 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001028 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001029
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001030 // Original PCH directory
1031 if (!OutputFile.empty() && OutputFile != "-") {
1032 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1033 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1035 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1036
1037 llvm::SmallString<128> OutputPath(OutputFile);
1038
1039 llvm::sys::fs::make_absolute(OutputPath);
1040 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1041
1042 RecordData Record;
1043 Record.push_back(ORIGINAL_PCH_DIR);
1044 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1045 }
1046
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001047 // Repository branch/version information.
1048 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001049 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001050 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1051 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001052 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001053 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001054 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1055 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001056}
1057
1058/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001059void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001060 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001061#define LANGOPT(Name, Bits, Default, Description) \
1062 Record.push_back(LangOpts.Name);
1063#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1064 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1065#include "clang/Basic/LangOptions.def"
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001066
1067 Record.push_back(LangOpts.CurrentModule.size());
1068 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001069 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001070}
1071
Douglas Gregor14f79002009-04-10 03:52:48 +00001072//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001073// stat cache Serialization
1074//===----------------------------------------------------------------------===//
1075
1076namespace {
1077// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001078class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001079public:
1080 typedef const char * key_type;
1081 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattner74e976b2010-11-23 19:28:12 +00001083 typedef struct stat data_type;
1084 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001085
1086 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001087 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
1090 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001091 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 data_type_ref Data) {
1093 unsigned StrLen = strlen(path);
1094 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001095 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096 clang::io::Emit8(Out, DataLen);
1097 return std::make_pair(StrLen + 1, DataLen);
1098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Chris Lattner5f9e2722011-07-23 10:55:15 +00001100 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001101 Out.write(path, KeyLen);
1102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Chris Lattner5f9e2722011-07-23 10:55:15 +00001104 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001105 data_type_ref Data, unsigned DataLen) {
1106 using namespace clang::io;
1107 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Chris Lattner74e976b2010-11-23 19:28:12 +00001109 Emit32(Out, (uint32_t) Data.st_ino);
1110 Emit32(Out, (uint32_t) Data.st_dev);
1111 Emit16(Out, (uint16_t) Data.st_mode);
1112 Emit64(Out, (uint64_t) Data.st_mtime);
1113 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001114
1115 assert(Out.tell() - Start == DataLen && "Wrong data length");
1116 }
1117};
1118} // end anonymous namespace
1119
Sebastian Redl3397c552010-08-18 23:56:27 +00001120/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001121void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001122 // Build the on-disk hash table containing information about every
1123 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001124 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001125 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001126 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001127 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001128 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001129 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001130 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001131 }
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001133 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001134 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001135 uint32_t BucketOffset;
1136 {
1137 llvm::raw_svector_ostream Out(StatCacheData);
1138 // Make sure that no bucket is at offset 0
1139 clang::io::Emit32(Out, 0);
1140 BucketOffset = Generator.Emit(Out);
1141 }
1142
1143 // Create a blob abbreviation
1144 using namespace llvm;
1145 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001146 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1150 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1151
1152 // Write the stat cache
1153 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001154 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001155 Record.push_back(BucketOffset);
1156 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001157 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001158}
1159
1160//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001161// Source Manager Serialization
1162//===----------------------------------------------------------------------===//
1163
1164/// \brief Create an abbreviation for the SLocEntry that refers to a
1165/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001166static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001167 using namespace llvm;
1168 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001169 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001174 // FileEntry fields.
1175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001182 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001183}
1184
1185/// \brief Create an abbreviation for the SLocEntry that refers to a
1186/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001187static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001188 using namespace llvm;
1189 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001190 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001196 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001197}
1198
1199/// \brief Create an abbreviation for the SLocEntry that refers to a
1200/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001201static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001202 using namespace llvm;
1203 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001204 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001206 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001207}
1208
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001209/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1210/// expansion.
1211static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001212 using namespace llvm;
1213 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001214 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001220 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001221}
1222
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001223namespace {
1224 // Trait used for the on-disk hash table of header search information.
1225 class HeaderFileInfoTrait {
1226 ASTWriter &Writer;
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001227 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001228
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001229 // Keep track of the framework names we've used during serialization.
1230 SmallVector<char, 128> FrameworkStringData;
1231 llvm::StringMap<unsigned> FrameworkNameOffset;
1232
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001233 public:
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001234 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001235 : Writer(Writer), HS(HS) { }
1236
1237 typedef const char *key_type;
1238 typedef key_type key_type_ref;
1239
1240 typedef HeaderFileInfo data_type;
1241 typedef const data_type &data_type_ref;
1242
1243 static unsigned ComputeHash(const char *path) {
1244 // The hash is based only on the filename portion of the key, so that the
1245 // reader can match based on filenames when symlinking or excess path
1246 // elements ("foo/../", "../") change the form of the name. However,
1247 // complete path is still the key.
1248 return llvm::HashString(llvm::sys::path::filename(path));
1249 }
1250
1251 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001252 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001253 data_type_ref Data) {
1254 unsigned StrLen = strlen(path);
1255 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001256 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001257 clang::io::Emit8(Out, DataLen);
1258 return std::make_pair(StrLen + 1, DataLen);
1259 }
1260
Chris Lattner5f9e2722011-07-23 10:55:15 +00001261 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001262 Out.write(path, KeyLen);
1263 }
1264
Chris Lattner5f9e2722011-07-23 10:55:15 +00001265 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001266 data_type_ref Data, unsigned DataLen) {
1267 using namespace clang::io;
1268 uint64_t Start = Out.tell(); (void)Start;
1269
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001270 unsigned char Flags = (Data.isImport << 5)
1271 | (Data.isPragmaOnce << 4)
1272 | (Data.DirInfo << 2)
1273 | (Data.Resolved << 1)
1274 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001275 Emit8(Out, (uint8_t)Flags);
1276 Emit16(Out, (uint16_t) Data.NumIncludes);
1277
1278 if (!Data.ControllingMacro)
1279 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1280 else
1281 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001282
1283 unsigned Offset = 0;
1284 if (!Data.Framework.empty()) {
1285 // If this header refers into a framework, save the framework name.
1286 llvm::StringMap<unsigned>::iterator Pos
1287 = FrameworkNameOffset.find(Data.Framework);
1288 if (Pos == FrameworkNameOffset.end()) {
1289 Offset = FrameworkStringData.size() + 1;
1290 FrameworkStringData.append(Data.Framework.begin(),
1291 Data.Framework.end());
1292 FrameworkStringData.push_back(0);
1293
1294 FrameworkNameOffset[Data.Framework] = Offset;
1295 } else
1296 Offset = Pos->second;
1297 }
1298 Emit32(Out, Offset);
1299
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001300 assert(Out.tell() - Start == DataLen && "Wrong data length");
1301 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001302
1303 const char *strings_begin() const { return FrameworkStringData.begin(); }
1304 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001305 };
1306} // end anonymous namespace
1307
1308/// \brief Write the header search block for the list of files that
1309///
1310/// \param HS The header search structure to save.
1311///
1312/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001313void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001314 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001315 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1316
1317 if (FilesByUID.size() > HS.header_file_size())
1318 FilesByUID.resize(HS.header_file_size());
1319
1320 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1321 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001322 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001323 unsigned NumHeaderSearchEntries = 0;
1324 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1325 const FileEntry *File = FilesByUID[UID];
1326 if (!File)
1327 continue;
1328
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001329 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1330 // from the external source if it was not provided already.
1331 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001332 if (HFI.External && Chain)
1333 continue;
1334
1335 // Turn the file name into an absolute path, if it isn't already.
1336 const char *Filename = File->getName();
1337 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1338
1339 // If we performed any translation on the file name at all, we need to
1340 // save this string, since the generator will refer to it later.
1341 if (Filename != File->getName()) {
1342 Filename = strdup(Filename);
1343 SavedStrings.push_back(Filename);
1344 }
1345
1346 Generator.insert(Filename, HFI, GeneratorTrait);
1347 ++NumHeaderSearchEntries;
1348 }
1349
1350 // Create the on-disk hash table in a buffer.
1351 llvm::SmallString<4096> TableData;
1352 uint32_t BucketOffset;
1353 {
1354 llvm::raw_svector_ostream Out(TableData);
1355 // Make sure that no bucket is at offset 0
1356 clang::io::Emit32(Out, 0);
1357 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1358 }
1359
1360 // Create a blob abbreviation
1361 using namespace llvm;
1362 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1363 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1368 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1369
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001370 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001371 RecordData Record;
1372 Record.push_back(HEADER_SEARCH_TABLE);
1373 Record.push_back(BucketOffset);
1374 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001375 Record.push_back(TableData.size());
1376 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001377 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1378
1379 // Free all of the strings we had to duplicate.
1380 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1381 free((void*)SavedStrings[I]);
1382}
1383
Douglas Gregor14f79002009-04-10 03:52:48 +00001384/// \brief Writes the block containing the serialized form of the
1385/// source manager.
1386///
1387/// TODO: We should probably use an on-disk hash table (stored in a
1388/// blob), indexed based on the file name, so that we only create
1389/// entries for files that we actually need. In the common case (no
1390/// errors), we probably won't have to create file entries for any of
1391/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001392void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001393 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001394 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001395 RecordData Record;
1396
Chris Lattnerf04ad692009-04-10 17:16:57 +00001397 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001398 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001399
1400 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001401 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1402 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1403 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001404 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001405
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001406 // Write out the source location entry table. We skip the first
1407 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001408 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001409 // Write out the offsets of only source location file entries.
1410 // We will go through them in ASTReader::validateFileEntries().
1411 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001412 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001413 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1414 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001415 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001416 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001417 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001418
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001419 // Record the offset of this source-location entry.
1420 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1421
1422 // Figure out which record code to use.
1423 unsigned Code;
1424 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001425 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1426 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001427 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001428 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1429 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001430 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001431 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001432 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001433 Record.clear();
1434 Record.push_back(Code);
1435
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001436 // Starting offset of this entry within this module, so skip the dummy.
1437 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001438 if (SLoc->isFile()) {
1439 const SrcMgr::FileInfo &File = SLoc->getFile();
1440 Record.push_back(File.getIncludeLoc().getRawEncoding());
1441 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1442 Record.push_back(File.hasLineDirectives());
1443
1444 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001445 if (Content->OrigEntry) {
1446 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001447 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001448
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001449 // The source location entry is a file. The blob associated
1450 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Douglas Gregor2d52be52010-03-21 22:49:54 +00001452 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001453 Record.push_back(Content->OrigEntry->getSize());
1454 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001455 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001456 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001457
1458 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1459 if (FDI != FileDeclIDs.end()) {
1460 Record.push_back(FDI->second->FirstDeclIndex);
1461 Record.push_back(FDI->second->DeclIDs.size());
1462 } else {
1463 Record.push_back(0);
1464 Record.push_back(0);
1465 }
Douglas Gregora081da52011-11-16 20:05:18 +00001466
Douglas Gregore650c8c2009-07-07 00:12:59 +00001467 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001468 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001469 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001470
1471 // Ask the file manager to fixup the relative path for us. This will
1472 // honor the working directory.
1473 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1474
1475 // FIXME: This call to make_absolute shouldn't be necessary, the
1476 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001477 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001478 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Douglas Gregore650c8c2009-07-07 00:12:59 +00001480 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001481 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001482
1483 if (Content->BufferOverridden) {
1484 Record.clear();
1485 Record.push_back(SM_SLOC_BUFFER_BLOB);
1486 const llvm::MemoryBuffer *Buffer
1487 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1488 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1489 StringRef(Buffer->getBufferStart(),
1490 Buffer->getBufferSize() + 1));
1491 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001492 } else {
1493 // The source location entry is a buffer. The blob associated
1494 // with this entry contains the contents of the buffer.
1495
1496 // We add one to the size so that we capture the trailing NULL
1497 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1498 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001499 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001500 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001501 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001502 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001503 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001504 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001505 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001506 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001507 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001508 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001509
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001510 if (strcmp(Name, "<built-in>") == 0) {
1511 PreloadSLocs.push_back(SLocEntryOffsets.size());
1512 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001513 }
1514 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001515 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001516 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001517 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1518 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001519 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1520 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001521
1522 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001523 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001524 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001525 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001526 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001527 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001528 }
1529 }
1530
Douglas Gregorc9490c02009-04-16 22:23:12 +00001531 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532
1533 if (SLocEntryOffsets.empty())
1534 return;
1535
Sebastian Redl3397c552010-08-18 23:56:27 +00001536 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001537 // table is used for lazily loading source-location information.
1538 using namespace llvm;
1539 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001540 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1544 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001546 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001547 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001548 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001549 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001550 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001551
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001552 Abbrev = new BitCodeAbbrev();
1553 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1556 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1557
1558 Record.clear();
1559 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1560 Record.push_back(SLocFileEntryOffsets.size());
1561 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1562 data(SLocFileEntryOffsets));
1563
Sebastian Redl3397c552010-08-18 23:56:27 +00001564 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001565 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001566 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001567
1568 // Write the line table. It depends on remapping working, so it must come
1569 // after the source location offsets.
1570 if (SourceMgr.hasLineTable()) {
1571 LineTableInfo &LineTable = SourceMgr.getLineTable();
1572
1573 Record.clear();
1574 // Emit the file names
1575 Record.push_back(LineTable.getNumFilenames());
1576 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1577 // Emit the file name
1578 const char *Filename = LineTable.getFilename(I);
1579 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1580 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1581 Record.push_back(FilenameLen);
1582 if (FilenameLen)
1583 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1584 }
1585
1586 // Emit the line entries
1587 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1588 L != LEnd; ++L) {
1589 // Only emit entries for local files.
1590 if (L->first < 0)
1591 continue;
1592
1593 // Emit the file ID
1594 Record.push_back(L->first);
1595
1596 // Emit the line entries
1597 Record.push_back(L->second.size());
1598 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1599 LEEnd = L->second.end();
1600 LE != LEEnd; ++LE) {
1601 Record.push_back(LE->FileOffset);
1602 Record.push_back(LE->LineNo);
1603 Record.push_back(LE->FilenameID);
1604 Record.push_back((unsigned)LE->FileKind);
1605 Record.push_back(LE->IncludeOffset);
1606 }
1607 }
1608 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1609 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001610}
1611
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001612//===----------------------------------------------------------------------===//
1613// Preprocessor Serialization
1614//===----------------------------------------------------------------------===//
1615
Douglas Gregor9c736102011-02-10 18:20:09 +00001616static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1617 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1618 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1619 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1620 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1621 return X.first->getName().compare(Y.first->getName());
1622}
1623
Chris Lattner0b1fb982009-04-10 17:15:23 +00001624/// \brief Writes the block containing the serialized form of the
1625/// preprocessor.
1626///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001627void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001628 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1629 if (PPRec)
1630 WritePreprocessorDetail(*PPRec);
1631
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001632 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001633
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001634 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1635 if (PP.getCounterValue() != 0) {
1636 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001637 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001638 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001639 }
1640
1641 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001642 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Sebastian Redl3397c552010-08-18 23:56:27 +00001644 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001645 // FIXME: use diagnostics subsystem for localization etc.
1646 if (PP.SawDateOrTime())
1647 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Douglas Gregorecdcb882010-10-20 22:00:55 +00001649
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001650 // Loop over all the macro definitions that are live at the end of the file,
1651 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001652
Douglas Gregor9c736102011-02-10 18:20:09 +00001653 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001654 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001655 MacrosToEmit;
1656 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001657 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1658 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001659 I != E; ++I) {
Douglas Gregoraa93a872011-10-17 15:32:29 +00001660 if (!IsModule || I->second->isPublic()) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00001661 MacroDefinitionsSeen.insert(I->first);
1662 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1663 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001664 }
1665
1666 // Sort the set of macro definitions that need to be serialized by the
1667 // name of the macro, to provide a stable ordering.
1668 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1669 &compareMacroDefinitions);
1670
Douglas Gregor040a8042011-02-11 00:26:14 +00001671 // Resolve any identifiers that defined macros at the time they were
1672 // deserialized, adding them to the list of macros to emit (if appropriate).
1673 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1674 IdentifierInfo *Name
1675 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1676 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1677 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1678 }
1679
Douglas Gregor9c736102011-02-10 18:20:09 +00001680 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1681 const IdentifierInfo *Name = MacrosToEmit[I].first;
1682 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001683 if (!MI)
1684 continue;
1685
Sebastian Redl3397c552010-08-18 23:56:27 +00001686 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001687 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001688 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001689
1690 // FIXME: There is a (probably minor) optimization we could do here, if
1691 // the macro comes from the original PCH but the identifier comes from a
1692 // chained PCH, by storing the offset into the original PCH rather than
1693 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001694 if (MI->isBuiltinMacro() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00001695 (Chain &&
1696 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1697 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001698 continue;
1699
Douglas Gregor9c736102011-02-10 18:20:09 +00001700 AddIdentifierRef(Name, Record);
1701 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001702 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1703 Record.push_back(MI->isUsed());
Douglas Gregoraa93a872011-10-17 15:32:29 +00001704 Record.push_back(MI->isPublic());
1705 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001706 unsigned Code;
1707 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001708 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001709 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001710 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001712 Record.push_back(MI->isC99Varargs());
1713 Record.push_back(MI->isGNUVarargs());
1714 Record.push_back(MI->getNumArgs());
1715 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1716 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001717 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001718 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001719
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001720 // If we have a detailed preprocessing record, record the macro definition
1721 // ID that corresponds to this macro.
1722 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001723 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001724
Douglas Gregorc9490c02009-04-16 22:23:12 +00001725 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001726 Record.clear();
1727
Chris Lattnerdf961c22009-04-10 18:08:30 +00001728 // Emit the tokens array.
1729 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1730 // Note that we know that the preprocessor does not have any annotation
1731 // tokens in it because they are created by the parser, and thus can't be
1732 // in a macro definition.
1733 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Chris Lattnerdf961c22009-04-10 18:08:30 +00001735 Record.push_back(Tok.getLocation().getRawEncoding());
1736 Record.push_back(Tok.getLength());
1737
Chris Lattnerdf961c22009-04-10 18:08:30 +00001738 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1739 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001740 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001741 // FIXME: Should translate token kind to a stable encoding.
1742 Record.push_back(Tok.getKind());
1743 // FIXME: Should translate token flags to a stable encoding.
1744 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001746 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001747 Record.clear();
1748 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001749 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001750 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001751 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001752}
1753
1754void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001755 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001756 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001757
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001758 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001759
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001760 // Enter the preprocessor block.
1761 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001762
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001763 // If the preprocessor has a preprocessing record, emit it.
1764 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001765 using namespace llvm;
1766
1767 // Set up the abbreviation for
1768 unsigned InclusionAbbrev = 0;
1769 {
1770 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1771 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1776 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1777 }
1778
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001779 unsigned FirstPreprocessorEntityID
1780 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1781 + NUM_PREDEF_PP_ENTITY_IDS;
1782 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001783 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001784 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1785 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001786 E != EEnd;
1787 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001788 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001789
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001790 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1791 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001792
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001793 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001794 // Record this macro definition's ID.
1795 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001796
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001797 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001798 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1799 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001800 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001801
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001802 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001803 Record.push_back(ME->isBuiltinMacro());
1804 if (ME->isBuiltinMacro())
1805 AddIdentifierRef(ME->getName(), Record);
1806 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001807 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001808 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001809 continue;
1810 }
1811
1812 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1813 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001814 Record.push_back(ID->getFileName().size());
1815 Record.push_back(ID->wasInQuotes());
1816 Record.push_back(static_cast<unsigned>(ID->getKind()));
1817 llvm::SmallString<64> Buffer;
1818 Buffer += ID->getFileName();
1819 Buffer += ID->getFile()->getName();
1820 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1821 continue;
1822 }
1823
1824 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1825 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001826 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001827
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001828 // Write the offsets table for the preprocessing record.
1829 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001830 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1831
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001832 // Write the offsets table for identifier IDs.
1833 using namespace llvm;
1834 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001835 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001837 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001838 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001839
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001840 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001841 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001842 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001843 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1844 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001845 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001846}
1847
Douglas Gregor26ced122011-12-01 00:59:36 +00001848/// \brief Compute the number of modules within the given tree (including the
1849/// given module).
1850static unsigned getNumberOfModules(Module *Mod) {
1851 unsigned ChildModules = 0;
1852 for (llvm::StringMap<Module *>::iterator Sub = Mod->SubModules.begin(),
1853 SubEnd = Mod->SubModules.end();
1854 Sub != SubEnd; ++Sub)
1855 ChildModules += getNumberOfModules(Sub->getValue());
1856
1857 return ChildModules + 1;
1858}
1859
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001860void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001861 // Enter the submodule description block.
1862 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1863
1864 // Write the abbreviations needed for the submodules block.
1865 using namespace llvm;
1866 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1867 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
1868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
1871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1872 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1873
1874 Abbrev = new BitCodeAbbrev();
1875 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA));
1876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1877 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1878
1879 Abbrev = new BitCodeAbbrev();
1880 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1882 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor26ced122011-12-01 00:59:36 +00001883
1884 // Write the submodule metadata block.
1885 RecordData Record;
1886 Record.push_back(getNumberOfModules(WritingModule));
1887 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1888 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1889
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001890 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001891 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001892 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001893 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001894 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001895 Q.pop();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00001896 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
Douglas Gregor26ced122011-12-01 00:59:36 +00001897 SubmoduleIDs[Mod] = NextSubmoduleID++;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001898
1899 // Emit the definition of the block.
1900 Record.clear();
1901 Record.push_back(SUBMODULE_DEFINITION);
1902 if (Mod->Parent) {
1903 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1904 Record.push_back(SubmoduleIDs[Mod->Parent]);
1905 } else {
1906 Record.push_back(0);
1907 }
1908 Record.push_back(Mod->IsFramework);
1909 Record.push_back(Mod->IsExplicit);
1910 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1911
1912 // Emit the umbrella header, if there is one.
1913 if (Mod->UmbrellaHeader) {
1914 Record.clear();
1915 Record.push_back(SUBMODULE_UMBRELLA);
1916 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
1917 Mod->UmbrellaHeader->getName());
1918 }
1919
1920 // Emit the headers.
1921 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1922 Record.clear();
1923 Record.push_back(SUBMODULE_HEADER);
1924 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1925 Mod->Headers[I]->getName());
1926 }
1927
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00001928 // Emit the exports.
1929 if (!Mod->Exports.empty()) {
1930 Record.clear();
1931 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
1932 unsigned ExportedID = SubmoduleIDs[Mod->Exports[I].getPointer()];
1933 assert(ExportedID && "Unknown submodule!");
1934 Record.push_back(ExportedID);
1935 Record.push_back(Mod->Exports[I].getInt());
1936 }
1937 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
1938 }
1939
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001940 // Queue up the submodules of this module.
1941 llvm::SmallVector<StringRef, 2> SubModules;
1942
1943 // Sort the submodules first, so we get a predictable ordering in the AST
1944 // file.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001945 for (llvm::StringMap<Module *>::iterator
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001946 Sub = Mod->SubModules.begin(),
1947 SubEnd = Mod->SubModules.end();
1948 Sub != SubEnd; ++Sub)
1949 SubModules.push_back(Sub->getKey());
1950 llvm::array_pod_sort(SubModules.begin(), SubModules.end());
1951
1952 for (unsigned I = 0, N = SubModules.size(); I != N; ++I)
1953 Q.push(Mod->SubModules[SubModules[I]]);
1954 }
1955
1956 Stream.ExitBlock();
1957}
1958
Douglas Gregor185dbd72011-12-01 02:07:58 +00001959serialization::SubmoduleID
1960ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
1961 if (Loc.isInvalid() || SubmoduleIDs.empty())
1962 return 0; // No submodule
1963
1964 // Use the expansion location to determine which module we're in.
1965 SourceManager &SrcMgr = PP->getSourceManager();
1966 SourceLocation ExpansionLoc = SrcMgr.getExpansionLoc(Loc);
1967 if (!ExpansionLoc.isFileID())
1968 return 0;
1969
1970
1971 FileID ExpansionFileID = SrcMgr.getFileID(ExpansionLoc);
1972 const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
1973 if (!ExpansionFile)
1974 return 0;
1975
1976 // Find the module that owns this header.
1977 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1978 Module *OwningMod = ModMap.findModuleForHeader(ExpansionFile);
1979 if (!OwningMod)
1980 return 0;
1981
1982 // Check whether we known about this submodule.
1983 llvm::DenseMap<Module *, unsigned>::iterator Known
1984 = SubmoduleIDs.find(OwningMod);
1985 if (Known == SubmoduleIDs.end())
1986 return 0;
1987
1988 return Known->second;
1989}
1990
David Blaikied6471f72011-09-25 23:23:43 +00001991void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001992 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00001993 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001994 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1995 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00001996 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001997 if (point.Loc.isInvalid())
1998 continue;
1999
2000 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002001 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002002 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002003 if (I->second.isPragma()) {
2004 Record.push_back(I->first);
2005 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002006 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002007 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002008 Record.push_back(-1); // mark the end of the diag/map pairs for this
2009 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002010 }
2011
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002012 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002013 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002014}
2015
Anders Carlssonc8505782011-03-06 18:41:18 +00002016void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2017 if (CXXBaseSpecifiersOffsets.empty())
2018 return;
2019
2020 RecordData Record;
2021
2022 // Create a blob abbreviation for the C++ base specifiers offsets.
2023 using namespace llvm;
2024
2025 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2026 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2029 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2030
Douglas Gregore92b8a12011-08-04 00:01:48 +00002031 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002032 Record.clear();
2033 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2034 Record.push_back(CXXBaseSpecifiersOffsets.size());
2035 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002036 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002037}
2038
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002039//===----------------------------------------------------------------------===//
2040// Type Serialization
2041//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002042
Sebastian Redl3397c552010-08-18 23:56:27 +00002043/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002044void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002045 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002046 if (Idx.getIndex() == 0) // we haven't seen this type before.
2047 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Douglas Gregor97475832010-10-05 18:37:06 +00002049 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002050
Douglas Gregor2cf26342009-04-09 22:27:44 +00002051 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002052 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002053 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002054 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002055 else if (TypeOffsets.size() < Index) {
2056 TypeOffsets.resize(Index + 1);
2057 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002058 }
2059
2060 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Douglas Gregor2cf26342009-04-09 22:27:44 +00002062 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002063 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002064
Douglas Gregora4923eb2009-11-16 21:35:15 +00002065 if (T.hasLocalNonFastQualifiers()) {
2066 Qualifiers Qs = T.getLocalQualifiers();
2067 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002068 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002069 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002070 } else {
2071 switch (T->getTypeClass()) {
2072 // For all of the concrete, non-dependent types, call the
2073 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002074#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002075 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002076#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002077#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002078 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002079 }
2080
2081 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002082 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002083
2084 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002085 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002086}
2087
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002088//===----------------------------------------------------------------------===//
2089// Declaration Serialization
2090//===----------------------------------------------------------------------===//
2091
Douglas Gregor2cf26342009-04-09 22:27:44 +00002092/// \brief Write the block containing all of the declaration IDs
2093/// lexically declared within the given DeclContext.
2094///
2095/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2096/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002097uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002098 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002099 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002100 return 0;
2101
Douglas Gregorc9490c02009-04-16 22:23:12 +00002102 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002103 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002104 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002105 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002106 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2107 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002108 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002109
Douglas Gregor25123082009-04-22 22:34:57 +00002110 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002111 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002112 return Offset;
2113}
2114
Sebastian Redla4232eb2010-08-18 23:56:21 +00002115void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002116 using namespace llvm;
2117 RecordData Record;
2118
2119 // Write the type offsets array
2120 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002121 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2125 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2126 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002127 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002128 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002129 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002130 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002131
2132 // Write the declaration offsets array
2133 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002134 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2138 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2139 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002141 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002142 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002143 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002144}
2145
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002146void ASTWriter::WriteFileDeclIDsMap() {
2147 using namespace llvm;
2148 RecordData Record;
2149
2150 // Join the vectors of DeclIDs from all files.
2151 SmallVector<DeclID, 256> FileSortedIDs;
2152 for (FileDeclIDsTy::iterator
2153 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2154 DeclIDInFileInfo &Info = *FI->second;
2155 Info.FirstDeclIndex = FileSortedIDs.size();
2156 for (LocDeclIDsTy::iterator
2157 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2158 FileSortedIDs.push_back(DI->second);
2159 }
2160
2161 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2162 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2164 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2165 Record.push_back(FILE_SORTED_DECLS);
2166 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2167}
2168
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002169//===----------------------------------------------------------------------===//
2170// Global Method Pool and Selector Serialization
2171//===----------------------------------------------------------------------===//
2172
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002173namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002174// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002175class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002176 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002177
2178public:
2179 typedef Selector key_type;
2180 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Sebastian Redl5d050072010-08-04 17:20:04 +00002182 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002183 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002184 ObjCMethodList Instance, Factory;
2185 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002186 typedef const data_type& data_type_ref;
2187
Sebastian Redl3397c552010-08-18 23:56:27 +00002188 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002189
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002190 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002191 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002192 }
Mike Stump1eb44332009-09-09 15:08:12 +00002193
2194 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002195 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002196 data_type_ref Methods) {
2197 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2198 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002199 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2200 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002201 Method = Method->Next)
2202 if (Method->Method)
2203 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002204 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002205 Method = Method->Next)
2206 if (Method->Method)
2207 DataLen += 4;
2208 clang::io::Emit16(Out, DataLen);
2209 return std::make_pair(KeyLen, DataLen);
2210 }
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Chris Lattner5f9e2722011-07-23 10:55:15 +00002212 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002213 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002214 assert((Start >> 32) == 0 && "Selector key offset too large");
2215 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002216 unsigned N = Sel.getNumArgs();
2217 clang::io::Emit16(Out, N);
2218 if (N == 0)
2219 N = 1;
2220 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002221 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002222 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2223 }
Mike Stump1eb44332009-09-09 15:08:12 +00002224
Chris Lattner5f9e2722011-07-23 10:55:15 +00002225 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002226 data_type_ref Methods, unsigned DataLen) {
2227 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002228 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002229 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002230 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002231 Method = Method->Next)
2232 if (Method->Method)
2233 ++NumInstanceMethods;
2234
2235 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002236 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002237 Method = Method->Next)
2238 if (Method->Method)
2239 ++NumFactoryMethods;
2240
2241 clang::io::Emit16(Out, NumInstanceMethods);
2242 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002243 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002244 Method = Method->Next)
2245 if (Method->Method)
2246 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002247 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002248 Method = Method->Next)
2249 if (Method->Method)
2250 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002251
2252 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002253 }
2254};
2255} // end anonymous namespace
2256
Sebastian Redl059612d2010-08-03 21:58:15 +00002257/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002258///
2259/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002260/// in an on-disk hash table indexed by the selector. The hash table also
2261/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002262void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002263 using namespace llvm;
2264
Sebastian Redl059612d2010-08-03 21:58:15 +00002265 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002266 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002267 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002268 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002269 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002270 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002271 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002272 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Sebastian Redl059612d2010-08-03 21:58:15 +00002274 // Create the on-disk hash table representation. We walk through every
2275 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002276 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002277 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002278 I = SelectorIDs.begin(), E = SelectorIDs.end();
2279 I != E; ++I) {
2280 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002281 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002282 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002283 I->second,
2284 ObjCMethodList(),
2285 ObjCMethodList()
2286 };
2287 if (F != SemaRef.MethodPool.end()) {
2288 Data.Instance = F->second.first;
2289 Data.Factory = F->second.second;
2290 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002291 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002292 // changed.
2293 if (Chain && I->second < FirstSelectorID) {
2294 // Selector already exists. Did it change?
2295 bool changed = false;
2296 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2297 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002298 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002299 changed = true;
2300 }
2301 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2302 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002303 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002304 changed = true;
2305 }
2306 if (!changed)
2307 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002308 } else if (Data.Instance.Method || Data.Factory.Method) {
2309 // A new method pool entry.
2310 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002311 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002312 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002313 }
2314
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002315 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002316 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002317 uint32_t BucketOffset;
2318 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002319 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002320 llvm::raw_svector_ostream Out(MethodPool);
2321 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002322 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002323 BucketOffset = Generator.Emit(Out, Trait);
2324 }
2325
2326 // Create a blob abbreviation
2327 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002328 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2332 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2333
Douglas Gregor83941df2009-04-25 17:48:32 +00002334 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002335 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002336 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002337 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002338 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002339 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002340
2341 // Create a blob abbreviation for the selector table offsets.
2342 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002343 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002344 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2347 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2348
2349 // Write the selector offsets table.
2350 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002351 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002352 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002353 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002354 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002355 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002356 }
2357}
2358
Sebastian Redl3397c552010-08-18 23:56:27 +00002359/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002360void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002361 using namespace llvm;
2362 if (SemaRef.ReferencedSelectors.empty())
2363 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002364
Fariborz Jahanian32019832010-07-23 19:11:11 +00002365 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002366
Sebastian Redl3397c552010-08-18 23:56:27 +00002367 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002368 // very tricky to fix, and given that @selector shouldn't really appear in
2369 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002370 for (DenseMap<Selector, SourceLocation>::iterator S =
2371 SemaRef.ReferencedSelectors.begin(),
2372 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2373 Selector Sel = (*S).first;
2374 SourceLocation Loc = (*S).second;
2375 AddSelectorRef(Sel, Record);
2376 AddSourceLocation(Loc, Record);
2377 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002378 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002379}
2380
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002381//===----------------------------------------------------------------------===//
2382// Identifier Table Serialization
2383//===----------------------------------------------------------------------===//
2384
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002385namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002386class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002387 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002388 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002389 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002390 bool IsModule;
2391
Douglas Gregora92193e2009-04-28 21:18:29 +00002392 /// \brief Determines whether this is an "interesting" identifier
2393 /// that needs a full IdentifierInfo structure written into the hash
2394 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002395 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002396 if (II->isPoisoned() ||
2397 II->isExtensionToken() ||
2398 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002399 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002400 II->getFETokenInfo<void>())
2401 return true;
2402
Douglas Gregorce835df2011-09-14 22:14:14 +00002403 return hasMacroDefinition(II, Macro);
2404 }
2405
2406 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002407 if (!II->hasMacroDefinition())
2408 return false;
2409
Douglas Gregorce835df2011-09-14 22:14:14 +00002410 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002411 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002412
Douglas Gregorce835df2011-09-14 22:14:14 +00002413 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002414 }
2415
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002416public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002417 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002418 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002420 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002421 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002422
Douglas Gregoreee242f2011-10-27 09:33:13 +00002423 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2424 IdentifierResolver &IdResolver, bool IsModule)
2425 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002426
2427 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002428 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002429 }
Mike Stump1eb44332009-09-09 15:08:12 +00002430
2431 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002432 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002433 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002434 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002435 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002436 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002437 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002438 if (hasMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002439 DataLen += 8;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002440
2441 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2442 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002443 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002444 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002445 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002446 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002447 // We emit the key length after the data length so that every
2448 // string is preceded by a 16-bit length. This matches the PTH
2449 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002450 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002451 return std::make_pair(KeyLen, DataLen);
2452 }
Mike Stump1eb44332009-09-09 15:08:12 +00002453
Chris Lattner5f9e2722011-07-23 10:55:15 +00002454 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002455 unsigned KeyLen) {
2456 // Record the location of the key data. This is used when generating
2457 // the mapping from persistent IDs to strings.
2458 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002459 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002460 }
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Douglas Gregor7143aab2011-09-01 17:04:32 +00002462 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002463 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002464 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002465 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002466 clang::io::Emit32(Out, ID << 1);
2467 return;
2468 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002469
Douglas Gregora92193e2009-04-28 21:18:29 +00002470 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002471 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002472 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002473 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregorce835df2011-09-14 22:14:14 +00002474 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002475 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2476 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002477 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002478 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002479 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002480
Douglas Gregor13292642011-12-02 15:45:10 +00002481 if (HasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002482 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor13292642011-12-02 15:45:10 +00002483 clang::io::Emit32(Out,
2484 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2485 }
2486
Douglas Gregor668c1a42009-04-21 22:25:48 +00002487 // Emit the declaration IDs in reverse order, because the
2488 // IdentifierResolver provides the declarations as they would be
2489 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002490 // "stat"), but the ASTReader adds declarations to the end of the list
2491 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002492 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002493 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2494 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002495 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002496 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002497 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002498 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002499 }
2500};
2501} // end anonymous namespace
2502
Sebastian Redl3397c552010-08-18 23:56:27 +00002503/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002504///
2505/// The identifier table consists of a blob containing string data
2506/// (the actual identifiers themselves) and a separate "offsets" index
2507/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002508void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2509 IdentifierResolver &IdResolver,
2510 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002511 using namespace llvm;
2512
2513 // Create and write out the blob that contains the identifier
2514 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002515 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002516 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002517 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002518
Douglas Gregor92b059e2009-04-28 20:33:11 +00002519 // Look for any identifiers that were named while processing the
2520 // headers, but are otherwise not needed. We add these to the hash
2521 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002522 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002523 // file.
2524 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2525 IDEnd = PP.getIdentifierTable().end();
2526 ID != IDEnd; ++ID)
2527 getIdentifierRef(ID->second);
2528
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002529 // Create the on-disk hash table representation. We only store offsets
2530 // for identifiers that appear here for the first time.
2531 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002532 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002533 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2534 ID != IDEnd; ++ID) {
2535 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002536 if (!Chain || !ID->first->isFromAST() ||
2537 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002538 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2539 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002540 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002541
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002542 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002543 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002544 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002545 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002546 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002547 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002548 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002549 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002550 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002551 }
2552
2553 // Create a blob abbreviation
2554 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002555 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002558 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002559
2560 // Write the identifier table
2561 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002562 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002563 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002564 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002565 }
2566
2567 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002568 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002569 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2573 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2574
2575 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002576 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002577 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002578 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002579 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002580 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002581}
2582
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002583//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002584// DeclContext's Name Lookup Table Serialization
2585//===----------------------------------------------------------------------===//
2586
2587namespace {
2588// Trait used for the on-disk hash table used in the method pool.
2589class ASTDeclContextNameLookupTrait {
2590 ASTWriter &Writer;
2591
2592public:
2593 typedef DeclarationName key_type;
2594 typedef key_type key_type_ref;
2595
2596 typedef DeclContext::lookup_result data_type;
2597 typedef const data_type& data_type_ref;
2598
2599 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2600
2601 unsigned ComputeHash(DeclarationName Name) {
2602 llvm::FoldingSetNodeID ID;
2603 ID.AddInteger(Name.getNameKind());
2604
2605 switch (Name.getNameKind()) {
2606 case DeclarationName::Identifier:
2607 ID.AddString(Name.getAsIdentifierInfo()->getName());
2608 break;
2609 case DeclarationName::ObjCZeroArgSelector:
2610 case DeclarationName::ObjCOneArgSelector:
2611 case DeclarationName::ObjCMultiArgSelector:
2612 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2613 break;
2614 case DeclarationName::CXXConstructorName:
2615 case DeclarationName::CXXDestructorName:
2616 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002617 break;
2618 case DeclarationName::CXXOperatorName:
2619 ID.AddInteger(Name.getCXXOverloadedOperator());
2620 break;
2621 case DeclarationName::CXXLiteralOperatorName:
2622 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2623 case DeclarationName::CXXUsingDirective:
2624 break;
2625 }
2626
2627 return ID.ComputeHash();
2628 }
2629
2630 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002631 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002632 data_type_ref Lookup) {
2633 unsigned KeyLen = 1;
2634 switch (Name.getNameKind()) {
2635 case DeclarationName::Identifier:
2636 case DeclarationName::ObjCZeroArgSelector:
2637 case DeclarationName::ObjCOneArgSelector:
2638 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002639 case DeclarationName::CXXLiteralOperatorName:
2640 KeyLen += 4;
2641 break;
2642 case DeclarationName::CXXOperatorName:
2643 KeyLen += 1;
2644 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002645 case DeclarationName::CXXConstructorName:
2646 case DeclarationName::CXXDestructorName:
2647 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002648 case DeclarationName::CXXUsingDirective:
2649 break;
2650 }
2651 clang::io::Emit16(Out, KeyLen);
2652
2653 // 2 bytes for num of decls and 4 for each DeclID.
2654 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2655 clang::io::Emit16(Out, DataLen);
2656
2657 return std::make_pair(KeyLen, DataLen);
2658 }
2659
Chris Lattner5f9e2722011-07-23 10:55:15 +00002660 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002661 using namespace clang::io;
2662
2663 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2664 Emit8(Out, Name.getNameKind());
2665 switch (Name.getNameKind()) {
2666 case DeclarationName::Identifier:
2667 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2668 break;
2669 case DeclarationName::ObjCZeroArgSelector:
2670 case DeclarationName::ObjCOneArgSelector:
2671 case DeclarationName::ObjCMultiArgSelector:
2672 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2673 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002674 case DeclarationName::CXXOperatorName:
2675 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2676 Emit8(Out, Name.getCXXOverloadedOperator());
2677 break;
2678 case DeclarationName::CXXLiteralOperatorName:
2679 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2680 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002681 case DeclarationName::CXXConstructorName:
2682 case DeclarationName::CXXDestructorName:
2683 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002684 case DeclarationName::CXXUsingDirective:
2685 break;
2686 }
2687 }
2688
Chris Lattner5f9e2722011-07-23 10:55:15 +00002689 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002690 data_type Lookup, unsigned DataLen) {
2691 uint64_t Start = Out.tell(); (void)Start;
2692 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2693 for (; Lookup.first != Lookup.second; ++Lookup.first)
2694 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2695
2696 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2697 }
2698};
2699} // end anonymous namespace
2700
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002701/// \brief Write the block containing all of the declaration IDs
2702/// visible from the given DeclContext.
2703///
2704/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002705/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002706uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2707 DeclContext *DC) {
2708 if (DC->getPrimaryContext() != DC)
2709 return 0;
2710
2711 // Since there is no name lookup into functions or methods, don't bother to
2712 // build a visible-declarations table for these entities.
2713 if (DC->isFunctionOrMethod())
2714 return 0;
2715
2716 // If not in C++, we perform name lookup for the translation unit via the
2717 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2718 // FIXME: In C++ we need the visible declarations in order to "see" the
2719 // friend declarations, is there a way to do this without writing the table ?
2720 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2721 return 0;
2722
2723 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002724 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002725 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002726
2727 // Serialize the contents of the mapping used for lookup. Note that,
2728 // although we have two very different code paths, the serialized
2729 // representation is the same for both cases: a declaration name,
2730 // followed by a size, followed by references to the visible
2731 // declarations that have that name.
2732 uint64_t Offset = Stream.GetCurrentBitNo();
2733 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2734 if (!Map || Map->empty())
2735 return 0;
2736
2737 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2738 ASTDeclContextNameLookupTrait Trait(*this);
2739
2740 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002741 DeclarationName ConversionName;
2742 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002743 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2744 D != DEnd; ++D) {
2745 DeclarationName Name = D->first;
2746 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002747 if (Result.first != Result.second) {
2748 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2749 // Hash all conversion function names to the same name. The actual
2750 // type information in conversion function name is not used in the
2751 // key (since such type information is not stable across different
2752 // modules), so the intended effect is to coalesce all of the conversion
2753 // functions under a single key.
2754 if (!ConversionName)
2755 ConversionName = Name;
2756 ConversionDecls.append(Result.first, Result.second);
2757 continue;
2758 }
2759
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002760 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002761 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002762 }
2763
Douglas Gregore5a54b62011-08-30 20:49:19 +00002764 // Add the conversion functions
2765 if (!ConversionDecls.empty()) {
2766 Generator.insert(ConversionName,
2767 DeclContext::lookup_result(ConversionDecls.begin(),
2768 ConversionDecls.end()),
2769 Trait);
2770 }
2771
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002772 // Create the on-disk hash table in a buffer.
2773 llvm::SmallString<4096> LookupTable;
2774 uint32_t BucketOffset;
2775 {
2776 llvm::raw_svector_ostream Out(LookupTable);
2777 // Make sure that no bucket is at offset 0
2778 clang::io::Emit32(Out, 0);
2779 BucketOffset = Generator.Emit(Out, Trait);
2780 }
2781
2782 // Write the lookup table
2783 RecordData Record;
2784 Record.push_back(DECL_CONTEXT_VISIBLE);
2785 Record.push_back(BucketOffset);
2786 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2787 LookupTable.str());
2788
2789 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2790 ++NumVisibleDeclContexts;
2791 return Offset;
2792}
2793
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002794/// \brief Write an UPDATE_VISIBLE block for the given context.
2795///
2796/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2797/// DeclContext in a dependent AST file. As such, they only exist for the TU
2798/// (in C++) and for namespaces.
2799void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002800 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2801 if (!Map || Map->empty())
2802 return;
2803
2804 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2805 ASTDeclContextNameLookupTrait Trait(*this);
2806
2807 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002808 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2809 D != DEnd; ++D) {
2810 DeclarationName Name = D->first;
2811 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002812 // For any name that appears in this table, the results are complete, i.e.
2813 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002814 if (Result.first != Result.second)
2815 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002816 }
2817
2818 // Create the on-disk hash table in a buffer.
2819 llvm::SmallString<4096> LookupTable;
2820 uint32_t BucketOffset;
2821 {
2822 llvm::raw_svector_ostream Out(LookupTable);
2823 // Make sure that no bucket is at offset 0
2824 clang::io::Emit32(Out, 0);
2825 BucketOffset = Generator.Emit(Out, Trait);
2826 }
2827
2828 // Write the lookup table
2829 RecordData Record;
2830 Record.push_back(UPDATE_VISIBLE);
2831 Record.push_back(getDeclID(cast<Decl>(DC)));
2832 Record.push_back(BucketOffset);
2833 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2834}
2835
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002836/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2837void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2838 RecordData Record;
2839 Record.push_back(Opts.fp_contract);
2840 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2841}
2842
2843/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2844void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2845 if (!SemaRef.Context.getLangOptions().OpenCL)
2846 return;
2847
2848 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2849 RecordData Record;
2850#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2851#include "clang/Basic/OpenCLExtensions.def"
2852 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2853}
2854
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002855//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002856// General Serialization Routines
2857//===----------------------------------------------------------------------===//
2858
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002859/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002860void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002861 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002862 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2863 const Attr * A = *i;
2864 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002865 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002866
Sean Huntcf807c42010-08-18 23:23:40 +00002867#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002868
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002869 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002870}
2871
Chris Lattner5f9e2722011-07-23 10:55:15 +00002872void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002873 Record.push_back(Str.size());
2874 Record.insert(Record.end(), Str.begin(), Str.end());
2875}
2876
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002877void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2878 RecordDataImpl &Record) {
2879 Record.push_back(Version.getMajor());
2880 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2881 Record.push_back(*Minor + 1);
2882 else
2883 Record.push_back(0);
2884 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2885 Record.push_back(*Subminor + 1);
2886 else
2887 Record.push_back(0);
2888}
2889
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002890/// \brief Note that the identifier II occurs at the given offset
2891/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002892void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002893 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002894 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002895 // up earlier in the chain and thus don't need an offset.
2896 if (ID >= FirstIdentID)
2897 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002898}
2899
Douglas Gregor83941df2009-04-25 17:48:32 +00002900/// \brief Note that the selector Sel occurs at the given offset
2901/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002902void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002903 unsigned ID = SelectorIDs[Sel];
2904 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002905 // Don't record offsets for selectors that are also available in a different
2906 // file.
2907 if (ID < FirstSelectorID)
2908 return;
2909 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002910}
2911
Sebastian Redla4232eb2010-08-18 23:56:21 +00002912ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002913 : Stream(Stream), Context(0), PP(0), Chain(0), WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002914 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002915 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002916 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00002917 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
2918 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002919 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002920 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002921 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002922 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00002923 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002924 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002925 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2926 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2927 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002928 DeclTypedefAbbrev(0),
2929 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2930 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00002931{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002932}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002933
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002934ASTWriter::~ASTWriter() {
2935 for (FileDeclIDsTy::iterator
2936 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
2937 delete I->second;
2938}
2939
Sebastian Redla4232eb2010-08-18 23:56:21 +00002940void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002941 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002942 Module *WritingModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00002943 WritingAST = true;
2944
Douglas Gregor2cf26342009-04-09 22:27:44 +00002945 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002946 Stream.Emit((unsigned)'C', 8);
2947 Stream.Emit((unsigned)'P', 8);
2948 Stream.Emit((unsigned)'C', 8);
2949 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002950
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002951 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002952
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002953 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00002954 PP = &SemaRef.PP;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00002955 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00002956 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00002957 PP = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00002958
2959 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002960}
2961
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002962template<typename Vector>
2963static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2964 ASTWriter::RecordData &Record) {
2965 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2966 I != E; ++I) {
2967 Writer.AddDeclRef(*I, Record);
2968 }
2969}
2970
Sebastian Redla4232eb2010-08-18 23:56:21 +00002971void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00002972 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00002973 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002974 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002975 using namespace llvm;
2976
Douglas Gregorecc2c092011-12-01 22:20:10 +00002977 // Make sure that the AST reader knows to finalize itself.
2978 if (Chain)
2979 Chain->finalizeForWriting();
2980
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002981 ASTContext &Context = SemaRef.Context;
2982 Preprocessor &PP = SemaRef.PP;
2983
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002984 // Set up predefined declaration IDs.
2985 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00002986 if (Context.ObjCIdDecl)
2987 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00002988 if (Context.ObjCSelDecl)
2989 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00002990 if (Context.ObjCClassDecl)
2991 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00002992 if (Context.Int128Decl)
2993 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
2994 if (Context.UInt128Decl)
2995 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00002996 if (Context.ObjCInstanceTypeDecl)
2997 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00002998
Douglas Gregorb7c324f2011-08-12 01:39:19 +00002999 if (!Chain) {
3000 // Make sure that we emit IdentifierInfos (and any attached
3001 // declarations) for builtins. We don't need to do this when we're
3002 // emitting chained PCH files, because all of the builtins will be
3003 // in the original PCH file.
3004 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003005 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003006 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003007 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
3008 Context.getLangOptions().NoBuiltin);
3009 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3010 getIdentifierRef(&Table.get(BuiltinNames[I]));
3011 }
3012
Douglas Gregoreee242f2011-10-27 09:33:13 +00003013 // If there are any out-of-date identifiers, bring them up to date.
3014 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3015 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3016 IDEnd = PP.getIdentifierTable().end();
3017 ID != IDEnd; ++ID)
3018 if (ID->second->isOutOfDate())
3019 ExtSource->updateOutOfDateIdentifier(*ID->second);
3020 }
3021
Chris Lattner63d65f82009-09-08 18:19:27 +00003022 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003023 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003024 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003025 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003026 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003027
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003028 // Build a record containing all of the file scoped decls in this file.
3029 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003030 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3031 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003032
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003033 // Build a record containing all of the delegating constructors we still need
3034 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003035 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003036 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003037
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003038 // Write the set of weak, undeclared identifiers. We always write the
3039 // entire table, since later PCH files in a PCH chain are only interested in
3040 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003041 RecordData WeakUndeclaredIdentifiers;
3042 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003043 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003044 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3045 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3046 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3047 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3048 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3049 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3050 }
3051 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003052
Douglas Gregor14c22f22009-04-22 22:18:58 +00003053 // Build a record containing all of the locally-scoped external
3054 // declarations in this header file. Generally, this record will be
3055 // empty.
3056 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003057 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003058 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003059 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003060 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3061 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003062 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003063 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003064 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3065 }
3066
Douglas Gregorb81c1702009-04-27 20:06:05 +00003067 // Build a record containing all of the ext_vector declarations.
3068 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003069 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003070
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003071 // Build a record containing all of the VTable uses information.
3072 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003073 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003074 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3075 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3076 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3077 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3078 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003079 }
3080
3081 // Build a record containing all of dynamic classes declarations.
3082 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003083 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003084
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003085 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003086 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003087 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003088 I = SemaRef.PendingInstantiations.begin(),
3089 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3090 AddDeclRef(I->first, PendingInstantiations);
3091 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003092 }
3093 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3094 "There are local ones at end of translation unit!");
3095
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003096 // Build a record containing some declaration references.
3097 RecordData SemaDeclRefs;
3098 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3099 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3100 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3101 }
3102
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003103 RecordData CUDASpecialDeclRefs;
3104 if (Context.getcudaConfigureCallDecl()) {
3105 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3106 }
3107
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003108 // Build a record containing all of the known namespaces.
3109 RecordData KnownNamespaces;
3110 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3111 I = SemaRef.KnownNamespaces.begin(),
3112 IEnd = SemaRef.KnownNamespaces.end();
3113 I != IEnd; ++I) {
3114 if (!I->second)
3115 AddDeclRef(I->first, KnownNamespaces);
3116 }
3117
Sebastian Redl3397c552010-08-18 23:56:27 +00003118 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003119 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003120 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003121 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003122 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00003123 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003124 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003125
3126 // Create a lexical update block containing all of the declarations in the
3127 // translation unit that do not come from other AST files.
3128 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3129 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3130 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3131 E = TU->noload_decls_end();
3132 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003133 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003134 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003135 }
3136
3137 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3138 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3139 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3140 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3141 Record.clear();
3142 Record.push_back(TU_UPDATE_LEXICAL);
3143 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3144 data(NewGlobalDecls));
3145
3146 // And a visible updates block for the translation unit.
3147 Abv = new llvm::BitCodeAbbrev();
3148 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3149 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3150 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3151 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3152 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3153 WriteDeclContextVisibleUpdate(TU);
3154
3155 // If the translation unit has an anonymous namespace, and we don't already
3156 // have an update block for it, write it as an update block.
3157 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3158 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3159 if (Record.empty()) {
3160 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003161 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003162 }
3163 }
3164
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003165 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003166 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003167
Douglas Gregora119da02011-08-02 16:26:37 +00003168 // Form the record of special types.
3169 RecordData SpecialTypes;
3170 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor30403a62011-08-11 22:04:35 +00003171 AddTypeRef(Context.ObjCProtoType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003172 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003173 AddTypeRef(Context.getFILEType(), SpecialTypes);
3174 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3175 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3176 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3177 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003178 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003179 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003180
3181 // If we're emitting a module, write out the submodule information.
3182 if (WritingModule)
3183 WriteSubmodules(WritingModule);
3184
Douglas Gregor366809a2009-04-26 03:49:13 +00003185 // Keep writing types and declarations until all types and
3186 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003187 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003188 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003189 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3190 E = DeclsToRewrite.end();
3191 I != E; ++I)
3192 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003193 while (!DeclTypesToEmit.empty()) {
3194 DeclOrType DOT = DeclTypesToEmit.front();
3195 DeclTypesToEmit.pop();
3196 if (DOT.isType())
3197 WriteType(DOT.getType());
3198 else
3199 WriteDecl(Context, DOT.getDecl());
3200 }
3201 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003202
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003203 WriteFileDeclIDsMap();
3204 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3205
3206 if (Chain) {
3207 // Write the mapping information describing our module dependencies and how
3208 // each of those modules were mapped into our own offset/ID space, so that
3209 // the reader can build the appropriate mapping to its own offset/ID space.
3210 // The map consists solely of a blob with the following format:
3211 // *(module-name-len:i16 module-name:len*i8
3212 // source-location-offset:i32
3213 // identifier-id:i32
3214 // preprocessed-entity-id:i32
3215 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003216 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003217 // selector-id:i32
3218 // declaration-id:i32
3219 // c++-base-specifiers-id:i32
3220 // type-id:i32)
3221 //
3222 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3223 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3225 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
3226 llvm::SmallString<2048> Buffer;
3227 {
3228 llvm::raw_svector_ostream Out(Buffer);
3229 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003230 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003231 M != MEnd; ++M) {
3232 StringRef FileName = (*M)->FileName;
3233 io::Emit16(Out, FileName.size());
3234 Out.write(FileName.data(), FileName.size());
3235 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3236 io::Emit32(Out, (*M)->BaseIdentifierID);
3237 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003238 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003239 io::Emit32(Out, (*M)->BaseSelectorID);
3240 io::Emit32(Out, (*M)->BaseDeclID);
3241 io::Emit32(Out, (*M)->BaseTypeIndex);
3242 }
3243 }
3244 Record.clear();
3245 Record.push_back(MODULE_OFFSET_MAP);
3246 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3247 Buffer.data(), Buffer.size());
3248 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003249 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003250 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003251 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003252 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003253 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003254 WriteFPPragmaOptions(SemaRef.getFPOptions());
3255 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003256
Sebastian Redl1476ed42010-07-16 16:36:56 +00003257 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003258 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003259
Anders Carlssonc8505782011-03-06 18:41:18 +00003260 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003261
Douglas Gregora119da02011-08-02 16:26:37 +00003262 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3263
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003264 /// Build a record containing first declarations from a chained PCH and the
3265 /// most recent declarations in this AST that they point to.
3266 RecordData FirstLatestDeclIDs;
3267 for (FirstLatestDeclMap::iterator I = FirstLatestDecls.begin(),
3268 E = FirstLatestDecls.end();
3269 I != E; ++I) {
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003270 AddDeclRef(I->first, FirstLatestDeclIDs);
3271 AddDeclRef(I->second, FirstLatestDeclIDs);
3272 }
3273
3274 if (!FirstLatestDeclIDs.empty())
3275 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
3276
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003277 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003278 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003279 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003280
3281 // Write the record containing tentative definitions.
3282 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003283 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003284
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003285 // Write the record containing unused file scoped decls.
3286 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003287 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003288
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003289 // Write the record containing weak undeclared identifiers.
3290 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003291 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003292 WeakUndeclaredIdentifiers);
3293
Douglas Gregor14c22f22009-04-22 22:18:58 +00003294 // Write the record containing locally-scoped external definitions.
3295 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003296 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003297 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003298
3299 // Write the record containing ext_vector type names.
3300 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003301 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003303 // Write the record containing VTable uses information.
3304 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003305 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003306
3307 // Write the record containing dynamic classes declarations.
3308 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003309 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003310
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003311 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003312 if (!PendingInstantiations.empty())
3313 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003314
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003315 // Write the record containing declaration references of Sema.
3316 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003317 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003318
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003319 // Write the record containing CUDA-specific declaration references.
3320 if (!CUDASpecialDeclRefs.empty())
3321 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003322
3323 // Write the delegating constructors.
3324 if (!DelegatingCtorDecls.empty())
3325 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003326
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003327 // Write the known namespaces.
3328 if (!KnownNamespaces.empty())
3329 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3330
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003331 // Write the visible updates to DeclContexts.
3332 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3333 I = UpdatedDeclContexts.begin(),
3334 E = UpdatedDeclContexts.end();
3335 I != E; ++I)
3336 WriteDeclContextVisibleUpdate(*I);
3337
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003338 if (!WritingModule) {
3339 // Write the submodules that were imported, if any.
3340 RecordData ImportedModules;
3341 for (ASTContext::import_iterator I = Context.local_import_begin(),
3342 IEnd = Context.local_import_end();
3343 I != IEnd; ++I) {
3344 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3345 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3346 }
3347 if (!ImportedModules.empty()) {
3348 // Sort module IDs.
3349 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3350
3351 // Unique module IDs.
3352 ImportedModules.erase(std::unique(ImportedModules.begin(),
3353 ImportedModules.end()),
3354 ImportedModules.end());
3355
3356 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3357 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003358 }
3359
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003360 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003361 WriteDeclReplacementsBlock();
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003362 WriteChainedObjCCategories();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003363
Douglas Gregor3e1af842009-04-17 22:13:46 +00003364 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003365 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003366 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003367 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003368 Record.push_back(NumLexicalDeclContexts);
3369 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003370 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003371 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003372}
3373
Douglas Gregor61c5e342011-09-17 00:05:03 +00003374/// \brief Go through the declaration update blocks and resolve declaration
3375/// pointers into declaration IDs.
3376void ASTWriter::ResolveDeclUpdatesBlocks() {
3377 for (DeclUpdateMap::iterator
3378 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3379 const Decl *D = I->first;
3380 UpdateRecord &URec = I->second;
3381
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003382 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003383 continue; // The decl will be written completely
3384
3385 unsigned Idx = 0, N = URec.size();
3386 while (Idx < N) {
3387 switch ((DeclUpdateKind)URec[Idx++]) {
3388 case UPD_CXX_SET_DEFINITIONDATA:
3389 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3390 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3391 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3392 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3393 ++Idx;
3394 break;
3395
3396 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3397 ++Idx;
3398 break;
3399 }
3400 }
3401 }
3402}
3403
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003404void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003405 if (DeclUpdates.empty())
3406 return;
3407
3408 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003409 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003410 for (DeclUpdateMap::iterator
3411 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3412 const Decl *D = I->first;
3413 UpdateRecord &URec = I->second;
3414
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003415 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003416 continue; // The decl will be written completely,no need to store updates.
3417
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003418 uint64_t Offset = Stream.GetCurrentBitNo();
3419 Stream.EmitRecord(DECL_UPDATES, URec);
3420
3421 OffsetsRecord.push_back(GetDeclRef(D));
3422 OffsetsRecord.push_back(Offset);
3423 }
3424 Stream.ExitBlock();
3425 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3426}
3427
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003428void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003429 if (ReplacedDecls.empty())
3430 return;
3431
3432 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003433 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003434 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003435 Record.push_back(I->ID);
3436 Record.push_back(I->Offset);
3437 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003438 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003439 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003440}
3441
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003442void ASTWriter::WriteChainedObjCCategories() {
3443 if (LocalChainedObjCCategories.empty())
3444 return;
3445
3446 RecordData Record;
3447 for (SmallVector<ChainedObjCCategoriesData, 16>::iterator
3448 I = LocalChainedObjCCategories.begin(),
3449 E = LocalChainedObjCCategories.end(); I != E; ++I) {
3450 ChainedObjCCategoriesData &Data = *I;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003451 if (isRewritten(Data.Interface))
3452 continue;
3453
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003454 assert(Data.Interface->getCategoryList());
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003455 serialization::DeclID
3456 HeadCatID = getDeclID(Data.Interface->getCategoryList());
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003457
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003458 Record.push_back(getDeclID(Data.Interface));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003459 Record.push_back(HeadCatID);
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003460 Record.push_back(getDeclID(Data.TailCategory));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00003461 }
3462 Stream.EmitRecord(OBJC_CHAINED_CATEGORIES, Record);
3463}
3464
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003465void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003466 Record.push_back(Loc.getRawEncoding());
3467}
3468
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003469void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003470 AddSourceLocation(Range.getBegin(), Record);
3471 AddSourceLocation(Range.getEnd(), Record);
3472}
3473
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003474void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003475 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003476 const uint64_t *Words = Value.getRawData();
3477 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003478}
3479
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003480void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003481 Record.push_back(Value.isUnsigned());
3482 AddAPInt(Value, Record);
3483}
3484
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003485void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003486 AddAPInt(Value.bitcastToAPInt(), Record);
3487}
3488
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003489void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003490 Record.push_back(getIdentifierRef(II));
3491}
3492
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003493IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003494 if (II == 0)
3495 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003496
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003497 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003498 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003499 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003500 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003501}
3502
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003503void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003504 Record.push_back(getSelectorRef(SelRef));
3505}
3506
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003507SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003508 if (Sel.getAsOpaquePtr() == 0) {
3509 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003510 }
3511
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003512 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003513 if (SID == 0 && Chain) {
3514 // This might trigger a ReadSelector callback, which will set the ID for
3515 // this selector.
3516 Chain->LoadSelector(Sel);
3517 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003518 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003519 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003520 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003521 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003522}
3523
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003524void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003525 AddDeclRef(Temp->getDestructor(), Record);
3526}
3527
Douglas Gregor7c789c12010-10-29 22:39:52 +00003528void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3529 CXXBaseSpecifier const *BasesEnd,
3530 RecordDataImpl &Record) {
3531 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3532 CXXBaseSpecifiersToWrite.push_back(
3533 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3534 Bases, BasesEnd));
3535 Record.push_back(NextCXXBaseSpecifiersID++);
3536}
3537
Sebastian Redla4232eb2010-08-18 23:56:21 +00003538void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003539 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003540 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003541 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003542 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003543 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003544 break;
3545 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003546 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003547 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003548 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003549 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003550 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003551 break;
3552 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003553 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003554 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003555 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003556 break;
John McCall833ca992009-10-29 08:12:44 +00003557 case TemplateArgument::Null:
3558 case TemplateArgument::Integral:
3559 case TemplateArgument::Declaration:
3560 case TemplateArgument::Pack:
3561 break;
3562 }
3563}
3564
Sebastian Redla4232eb2010-08-18 23:56:21 +00003565void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003566 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003567 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003568
3569 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3570 bool InfoHasSameExpr
3571 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3572 Record.push_back(InfoHasSameExpr);
3573 if (InfoHasSameExpr)
3574 return; // Avoid storing the same expr twice.
3575 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003576 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3577 Record);
3578}
3579
Douglas Gregordc355712011-02-25 00:36:19 +00003580void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3581 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003582 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003583 AddTypeRef(QualType(), Record);
3584 return;
3585 }
3586
Douglas Gregordc355712011-02-25 00:36:19 +00003587 AddTypeLoc(TInfo->getTypeLoc(), Record);
3588}
3589
3590void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3591 AddTypeRef(TL.getType(), Record);
3592
John McCalla1ee0c52009-10-16 21:56:05 +00003593 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003594 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003595 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003596}
3597
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003598void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003599 Record.push_back(GetOrCreateTypeID(T));
3600}
3601
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003602TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3603 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003604 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3605}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003606
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003607TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003608 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003609 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003610}
3611
3612TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3613 if (T.isNull())
3614 return TypeIdx();
3615 assert(!T.getLocalFastQualifiers());
3616
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003617 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003618 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003619 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003620 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003621 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003622 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003623 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003624 return Idx;
3625}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003626
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003627TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003628 if (T.isNull())
3629 return TypeIdx();
3630 assert(!T.getLocalFastQualifiers());
3631
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003632 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3633 assert(I != TypeIdxs.end() && "Type not emitted!");
3634 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003635}
3636
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003637void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003638 Record.push_back(GetDeclRef(D));
3639}
3640
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003641DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003642 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3643
Douglas Gregor2cf26342009-04-09 22:27:44 +00003644 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003645 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003646 }
Douglas Gregor97475832010-10-05 18:37:06 +00003647 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003648 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003649 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003650 // We haven't seen this declaration before. Give it a new ID and
3651 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003652 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003653 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003654 }
3655
Sebastian Redl681d7232010-07-27 00:17:23 +00003656 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003657}
3658
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003659DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003660 if (D == 0)
3661 return 0;
3662
3663 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3664 return DeclIDs[D];
3665}
3666
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003667static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3668 std::pair<unsigned, serialization::DeclID> R) {
3669 return L.first < R.first;
3670}
3671
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003672void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003673 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003674 assert(D);
3675
3676 SourceLocation Loc = D->getLocation();
3677 if (Loc.isInvalid())
3678 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003679
3680 // We only keep track of the file-level declarations of each file.
3681 if (!D->getLexicalDeclContext()->isFileContext())
3682 return;
3683
3684 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003685 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003686 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003687 FileID FID;
3688 unsigned Offset;
3689 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003690 if (FID.isInvalid())
3691 return;
3692 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3693 assert(Entry->isFile());
3694
3695 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3696 if (!Info)
3697 Info = new DeclIDInFileInfo();
3698
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003699 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003700 LocDeclIDsTy &Decls = Info->DeclIDs;
3701
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003702 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003703 Decls.push_back(LocDecl);
3704 return;
3705 }
3706
3707 LocDeclIDsTy::iterator
3708 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3709
3710 Decls.insert(I, LocDecl);
3711}
3712
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003713void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003714 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003715 Record.push_back(Name.getNameKind());
3716 switch (Name.getNameKind()) {
3717 case DeclarationName::Identifier:
3718 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3719 break;
3720
3721 case DeclarationName::ObjCZeroArgSelector:
3722 case DeclarationName::ObjCOneArgSelector:
3723 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003724 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003725 break;
3726
3727 case DeclarationName::CXXConstructorName:
3728 case DeclarationName::CXXDestructorName:
3729 case DeclarationName::CXXConversionFunctionName:
3730 AddTypeRef(Name.getCXXNameType(), Record);
3731 break;
3732
3733 case DeclarationName::CXXOperatorName:
3734 Record.push_back(Name.getCXXOverloadedOperator());
3735 break;
3736
Sean Hunt3e518bd2009-11-29 07:34:05 +00003737 case DeclarationName::CXXLiteralOperatorName:
3738 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3739 break;
3740
Douglas Gregor2cf26342009-04-09 22:27:44 +00003741 case DeclarationName::CXXUsingDirective:
3742 // No extra data to emit
3743 break;
3744 }
3745}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003746
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003747void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003748 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003749 switch (Name.getNameKind()) {
3750 case DeclarationName::CXXConstructorName:
3751 case DeclarationName::CXXDestructorName:
3752 case DeclarationName::CXXConversionFunctionName:
3753 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3754 break;
3755
3756 case DeclarationName::CXXOperatorName:
3757 AddSourceLocation(
3758 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3759 Record);
3760 AddSourceLocation(
3761 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3762 Record);
3763 break;
3764
3765 case DeclarationName::CXXLiteralOperatorName:
3766 AddSourceLocation(
3767 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3768 Record);
3769 break;
3770
3771 case DeclarationName::Identifier:
3772 case DeclarationName::ObjCZeroArgSelector:
3773 case DeclarationName::ObjCOneArgSelector:
3774 case DeclarationName::ObjCMultiArgSelector:
3775 case DeclarationName::CXXUsingDirective:
3776 break;
3777 }
3778}
3779
3780void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003781 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003782 AddDeclarationName(NameInfo.getName(), Record);
3783 AddSourceLocation(NameInfo.getLoc(), Record);
3784 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3785}
3786
3787void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003788 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003789 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003790 Record.push_back(Info.NumTemplParamLists);
3791 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3792 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3793}
3794
Sebastian Redla4232eb2010-08-18 23:56:21 +00003795void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003796 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003797 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003798 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003799 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003800
3801 // Push each of the NNS's onto a stack for serialization in reverse order.
3802 while (NNS) {
3803 NestedNames.push_back(NNS);
3804 NNS = NNS->getPrefix();
3805 }
3806
3807 Record.push_back(NestedNames.size());
3808 while(!NestedNames.empty()) {
3809 NNS = NestedNames.pop_back_val();
3810 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3811 Record.push_back(Kind);
3812 switch (Kind) {
3813 case NestedNameSpecifier::Identifier:
3814 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3815 break;
3816
3817 case NestedNameSpecifier::Namespace:
3818 AddDeclRef(NNS->getAsNamespace(), Record);
3819 break;
3820
Douglas Gregor14aba762011-02-24 02:36:08 +00003821 case NestedNameSpecifier::NamespaceAlias:
3822 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3823 break;
3824
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003825 case NestedNameSpecifier::TypeSpec:
3826 case NestedNameSpecifier::TypeSpecWithTemplate:
3827 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3828 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3829 break;
3830
3831 case NestedNameSpecifier::Global:
3832 // Don't need to write an associated value.
3833 break;
3834 }
3835 }
3836}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003837
Douglas Gregordc355712011-02-25 00:36:19 +00003838void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3839 RecordDataImpl &Record) {
3840 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003841 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003842 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00003843
3844 // Push each of the nested-name-specifiers's onto a stack for
3845 // serialization in reverse order.
3846 while (NNS) {
3847 NestedNames.push_back(NNS);
3848 NNS = NNS.getPrefix();
3849 }
3850
3851 Record.push_back(NestedNames.size());
3852 while(!NestedNames.empty()) {
3853 NNS = NestedNames.pop_back_val();
3854 NestedNameSpecifier::SpecifierKind Kind
3855 = NNS.getNestedNameSpecifier()->getKind();
3856 Record.push_back(Kind);
3857 switch (Kind) {
3858 case NestedNameSpecifier::Identifier:
3859 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3860 AddSourceRange(NNS.getLocalSourceRange(), Record);
3861 break;
3862
3863 case NestedNameSpecifier::Namespace:
3864 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3865 AddSourceRange(NNS.getLocalSourceRange(), Record);
3866 break;
3867
3868 case NestedNameSpecifier::NamespaceAlias:
3869 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3870 AddSourceRange(NNS.getLocalSourceRange(), Record);
3871 break;
3872
3873 case NestedNameSpecifier::TypeSpec:
3874 case NestedNameSpecifier::TypeSpecWithTemplate:
3875 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3876 AddTypeLoc(NNS.getTypeLoc(), Record);
3877 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3878 break;
3879
3880 case NestedNameSpecifier::Global:
3881 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3882 break;
3883 }
3884 }
3885}
3886
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003887void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003888 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003889 Record.push_back(Kind);
3890 switch (Kind) {
3891 case TemplateName::Template:
3892 AddDeclRef(Name.getAsTemplateDecl(), Record);
3893 break;
3894
3895 case TemplateName::OverloadedTemplate: {
3896 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3897 Record.push_back(OvT->size());
3898 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3899 I != E; ++I)
3900 AddDeclRef(*I, Record);
3901 break;
3902 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003903
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003904 case TemplateName::QualifiedTemplate: {
3905 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3906 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3907 Record.push_back(QualT->hasTemplateKeyword());
3908 AddDeclRef(QualT->getTemplateDecl(), Record);
3909 break;
3910 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003911
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003912 case TemplateName::DependentTemplate: {
3913 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3914 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3915 Record.push_back(DepT->isIdentifier());
3916 if (DepT->isIdentifier())
3917 AddIdentifierRef(DepT->getIdentifier(), Record);
3918 else
3919 Record.push_back(DepT->getOperator());
3920 break;
3921 }
John McCall14606042011-06-30 08:33:18 +00003922
3923 case TemplateName::SubstTemplateTemplateParm: {
3924 SubstTemplateTemplateParmStorage *subst
3925 = Name.getAsSubstTemplateTemplateParm();
3926 AddDeclRef(subst->getParameter(), Record);
3927 AddTemplateName(subst->getReplacement(), Record);
3928 break;
3929 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003930
3931 case TemplateName::SubstTemplateTemplateParmPack: {
3932 SubstTemplateTemplateParmPackStorage *SubstPack
3933 = Name.getAsSubstTemplateTemplateParmPack();
3934 AddDeclRef(SubstPack->getParameterPack(), Record);
3935 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3936 break;
3937 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003938 }
3939}
3940
Michael J. Spencer20249a12010-10-21 03:16:25 +00003941void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003942 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003943 Record.push_back(Arg.getKind());
3944 switch (Arg.getKind()) {
3945 case TemplateArgument::Null:
3946 break;
3947 case TemplateArgument::Type:
3948 AddTypeRef(Arg.getAsType(), Record);
3949 break;
3950 case TemplateArgument::Declaration:
3951 AddDeclRef(Arg.getAsDecl(), Record);
3952 break;
3953 case TemplateArgument::Integral:
3954 AddAPSInt(*Arg.getAsIntegral(), Record);
3955 AddTypeRef(Arg.getIntegralType(), Record);
3956 break;
3957 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003958 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3959 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003960 case TemplateArgument::TemplateExpansion:
3961 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003962 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3963 Record.push_back(*NumExpansions + 1);
3964 else
3965 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003966 break;
3967 case TemplateArgument::Expression:
3968 AddStmt(Arg.getAsExpr());
3969 break;
3970 case TemplateArgument::Pack:
3971 Record.push_back(Arg.pack_size());
3972 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3973 I != E; ++I)
3974 AddTemplateArgument(*I, Record);
3975 break;
3976 }
3977}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003978
3979void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003980ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003981 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003982 assert(TemplateParams && "No TemplateParams!");
3983 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3984 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3985 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3986 Record.push_back(TemplateParams->size());
3987 for (TemplateParameterList::const_iterator
3988 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3989 P != PEnd; ++P)
3990 AddDeclRef(*P, Record);
3991}
3992
3993/// \brief Emit a template argument list.
3994void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003995ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003996 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003997 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003998 Record.push_back(TemplateArgs->size());
3999 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004000 AddTemplateArgument(TemplateArgs->get(i), Record);
4001}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004002
4003
4004void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004005ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004006 Record.push_back(Set.size());
4007 for (UnresolvedSetImpl::const_iterator
4008 I = Set.begin(), E = Set.end(); I != E; ++I) {
4009 AddDeclRef(I.getDecl(), Record);
4010 Record.push_back(I.getAccess());
4011 }
4012}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004013
Sebastian Redla4232eb2010-08-18 23:56:21 +00004014void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004015 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004016 Record.push_back(Base.isVirtual());
4017 Record.push_back(Base.isBaseOfClass());
4018 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004019 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004020 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004021 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004022 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4023 : SourceLocation(),
4024 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004025}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004026
Douglas Gregor7c789c12010-10-29 22:39:52 +00004027void ASTWriter::FlushCXXBaseSpecifiers() {
4028 RecordData Record;
4029 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4030 Record.clear();
4031
4032 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004033 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004034 if (Index == CXXBaseSpecifiersOffsets.size())
4035 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4036 else {
4037 if (Index > CXXBaseSpecifiersOffsets.size())
4038 CXXBaseSpecifiersOffsets.resize(Index + 1);
4039 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4040 }
4041
4042 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4043 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4044 Record.push_back(BEnd - B);
4045 for (; B != BEnd; ++B)
4046 AddCXXBaseSpecifier(*B, Record);
4047 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004048
4049 // Flush any expressions that were written as part of the base specifiers.
4050 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004051 }
4052
4053 CXXBaseSpecifiersToWrite.clear();
4054}
4055
Sean Huntcbb67482011-01-08 20:30:50 +00004056void ASTWriter::AddCXXCtorInitializers(
4057 const CXXCtorInitializer * const *CtorInitializers,
4058 unsigned NumCtorInitializers,
4059 RecordDataImpl &Record) {
4060 Record.push_back(NumCtorInitializers);
4061 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4062 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004063
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004064 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004065 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004066 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004067 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004068 } else if (Init->isDelegatingInitializer()) {
4069 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004070 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004071 } else if (Init->isMemberInitializer()){
4072 Record.push_back(CTOR_INITIALIZER_MEMBER);
4073 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004074 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004075 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4076 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004077 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004078
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004079 AddSourceLocation(Init->getMemberLocation(), Record);
4080 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004081 AddSourceLocation(Init->getLParenLoc(), Record);
4082 AddSourceLocation(Init->getRParenLoc(), Record);
4083 Record.push_back(Init->isWritten());
4084 if (Init->isWritten()) {
4085 Record.push_back(Init->getSourceOrder());
4086 } else {
4087 Record.push_back(Init->getNumArrayIndices());
4088 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4089 AddDeclRef(Init->getArrayIndex(i), Record);
4090 }
4091 }
4092}
4093
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004094void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4095 assert(D->DefinitionData);
4096 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
4097 Record.push_back(Data.UserDeclaredConstructor);
4098 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004099 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004100 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004101 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004102 Record.push_back(Data.UserDeclaredDestructor);
4103 Record.push_back(Data.Aggregate);
4104 Record.push_back(Data.PlainOldData);
4105 Record.push_back(Data.Empty);
4106 Record.push_back(Data.Polymorphic);
4107 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004108 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004109 Record.push_back(Data.HasNoNonEmptyBases);
4110 Record.push_back(Data.HasPrivateFields);
4111 Record.push_back(Data.HasProtectedFields);
4112 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004113 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00004114 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004115 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004116 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004117 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004118 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004119 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004120 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004121 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004122 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004123 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004124 Record.push_back(Data.DeclaredDefaultConstructor);
4125 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004126 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004127 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004128 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004129 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004130 Record.push_back(Data.FailedImplicitMoveConstructor);
4131 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004132
4133 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004134 if (Data.NumBases > 0)
4135 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4136 Record);
4137
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004138 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4139 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004140 if (Data.NumVBases > 0)
4141 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4142 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004143
4144 AddUnresolvedSet(Data.Conversions, Record);
4145 AddUnresolvedSet(Data.VisibleConversions, Record);
4146 // Data.Definition is the owning decl, no need to write it.
4147 AddDeclRef(Data.FirstFriend, Record);
4148}
4149
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004150void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004151 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004152 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004153 assert(FirstDeclID == NextDeclID &&
4154 FirstTypeID == NextTypeID &&
4155 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004156 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004157 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004158 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004159
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004160 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004161
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004162 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4163 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4164 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004165 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004166 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004167 NextDeclID = FirstDeclID;
4168 NextTypeID = FirstTypeID;
4169 NextIdentID = FirstIdentID;
4170 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004171 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004172}
4173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004174void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004175 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004176 if (II->hasMacroDefinition())
4177 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004178}
4179
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004180void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004181 // Always take the highest-numbered type index. This copes with an interesting
4182 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004183 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004184 // keep the higher-numbered entry so that we can properly write it out to
4185 // the AST file.
4186 TypeIdx &StoredIdx = TypeIdxs[T];
4187 if (Idx.getIndex() >= StoredIdx.getIndex())
4188 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004189}
4190
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004191void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00004192 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004193}
Sebastian Redl5d050072010-08-04 17:20:04 +00004194
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004195void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004196 SelectorIDs[S] = ID;
4197}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004198
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004199void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004200 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004201 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004202 MacroDefinitions[MD] = ID;
4203}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004204
Douglas Gregora015cab2011-12-02 17:30:13 +00004205void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4206 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4207 SubmoduleIDs[Mod] = ID;
4208}
4209
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004210void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004211 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004212 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004213 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4214 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004215 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004216 // A forward reference was mutated into a definition. Rewrite it.
4217 // FIXME: This happens during template instantiation, should we
4218 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004219 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004220 }
4221
4222 for (CXXRecordDecl::redecl_iterator
4223 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
4224 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
4225 if (Redecl == RD)
4226 continue;
4227
4228 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004229 if (Redecl->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004230 UpdateRecord &Record = DeclUpdates[Redecl];
4231 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
4232 assert(Redecl->DefinitionData);
4233 assert(Redecl->DefinitionData->Definition == D);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004234 Record.push_back(reinterpret_cast<uint64_t>(D)); // the DefinitionDecl
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004235 }
4236 }
4237 }
4238}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004239void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004240 assert(!WritingAST && "Already writing the AST!");
4241
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004242 // TU and namespaces are handled elsewhere.
4243 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4244 return;
4245
Douglas Gregor919814d2011-09-09 23:01:35 +00004246 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004247 return; // Not a source decl added to a DeclContext from PCH.
4248
4249 AddUpdatedDeclContext(DC);
4250}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004251
4252void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004253 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004254 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004255 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004256 return; // Not a source member added to a class from PCH.
4257 if (!isa<CXXMethodDecl>(D))
4258 return; // We are interested in lazily declared implicit methods.
4259
4260 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004261 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004262 UpdateRecord &Record = DeclUpdates[RD];
4263 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004264 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004265}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004266
4267void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4268 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004269 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004270 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004271 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004272 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004273 return; // Not a source specialization added to a template from PCH.
4274
4275 UpdateRecord &Record = DeclUpdates[TD];
4276 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004277 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004278}
Douglas Gregor89d99802010-11-30 06:16:57 +00004279
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004280void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4281 const FunctionDecl *D) {
4282 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004283 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004284 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004285 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004286 return; // Not a source specialization added to a template from PCH.
4287
4288 UpdateRecord &Record = DeclUpdates[TD];
4289 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004290 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004291}
4292
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004293void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004294 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004295 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004296 return; // Declaration not imported from PCH.
4297
4298 // Implicit decl from a PCH was defined.
4299 // FIXME: Should implicit definition be a separate FunctionDecl?
4300 RewriteDecl(D);
4301}
4302
Sebastian Redlf79a7192011-04-29 08:19:30 +00004303void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004304 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004305 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004306 return;
4307
4308 // Since the actual instantiation is delayed, this really means that we need
4309 // to update the instantiation location.
4310 UpdateRecord &Record = DeclUpdates[D];
4311 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4312 AddSourceLocation(
4313 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4314}
4315
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004316void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4317 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004318 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004319 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004320 return; // Declaration not imported from PCH.
4321 if (CatD->getNextClassCategory() &&
Douglas Gregor919814d2011-09-09 23:01:35 +00004322 !CatD->getNextClassCategory()->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004323 return; // We already recorded that the tail of a category chain should be
4324 // attached to an interface.
4325
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004326 ChainedObjCCategoriesData Data = { IFD, CatD };
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004327 LocalChainedObjCCategories.push_back(Data);
4328}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004329
4330void ASTWriter::CompletedObjCForwardRef(const ObjCContainerDecl *D) {
4331 assert(!WritingAST && "Already writing the AST!");
4332 if (!D->isFromASTFile())
4333 return; // Declaration not imported from PCH.
4334
4335 RewriteDecl(D);
4336}
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004337
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004338void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4339 const ObjCPropertyDecl *OrigProp,
4340 const ObjCCategoryDecl *ClassExt) {
4341 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4342 if (!D)
4343 return;
4344
4345 assert(!WritingAST && "Already writing the AST!");
4346 if (!D->isFromASTFile())
4347 return; // Declaration not imported from PCH.
4348
4349 RewriteDecl(D);
4350}
4351
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004352void ASTWriter::UpdatedAttributeList(const Decl *D) {
4353 assert(!WritingAST && "Already writing the AST!");
4354 if (!D->isFromASTFile())
4355 return; // Declaration not imported from PCH.
4356
4357 RewriteDecl(D);
4358}