blob: b715d6ce3f62e975a9e0af13f279858474d31c29 [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 Gregor56ca8a92012-01-17 19:21:53 +0000242 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), 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 Gregor56ca8a92012-01-17 19:21:53 +0000372 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), 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);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000781 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000782 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 Gregorcff9f262012-01-27 01:47:08 +0000801 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000802 RECORD(FILE_SORTED_DECLS);
803 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000804 RECORD(MERGED_DECLARATIONS);
805 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000806 RECORD(OBJC_CATEGORIES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000807
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000808 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000809 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000810 RECORD(SM_SLOC_FILE_ENTRY);
811 RECORD(SM_SLOC_BUFFER_ENTRY);
812 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000813 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000815 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000816 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000817 RECORD(PP_MACRO_OBJECT_LIKE);
818 RECORD(PP_MACRO_FUNCTION_LIKE);
819 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000820
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000821 // Decls and Types block.
822 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000823 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000824 RECORD(TYPE_COMPLEX);
825 RECORD(TYPE_POINTER);
826 RECORD(TYPE_BLOCK_POINTER);
827 RECORD(TYPE_LVALUE_REFERENCE);
828 RECORD(TYPE_RVALUE_REFERENCE);
829 RECORD(TYPE_MEMBER_POINTER);
830 RECORD(TYPE_CONSTANT_ARRAY);
831 RECORD(TYPE_INCOMPLETE_ARRAY);
832 RECORD(TYPE_VARIABLE_ARRAY);
833 RECORD(TYPE_VECTOR);
834 RECORD(TYPE_EXT_VECTOR);
835 RECORD(TYPE_FUNCTION_PROTO);
836 RECORD(TYPE_FUNCTION_NO_PROTO);
837 RECORD(TYPE_TYPEDEF);
838 RECORD(TYPE_TYPEOF_EXPR);
839 RECORD(TYPE_TYPEOF);
840 RECORD(TYPE_RECORD);
841 RECORD(TYPE_ENUM);
842 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000843 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000844 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000845 RECORD(TYPE_DECLTYPE);
846 RECORD(TYPE_ELABORATED);
847 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
848 RECORD(TYPE_UNRESOLVED_USING);
849 RECORD(TYPE_INJECTED_CLASS_NAME);
850 RECORD(TYPE_OBJC_OBJECT);
851 RECORD(TYPE_TEMPLATE_TYPE_PARM);
852 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
853 RECORD(TYPE_DEPENDENT_NAME);
854 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
855 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
856 RECORD(TYPE_PAREN);
857 RECORD(TYPE_PACK_EXPANSION);
858 RECORD(TYPE_ATTRIBUTED);
859 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000860 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000861 RECORD(DECL_TYPEDEF);
862 RECORD(DECL_ENUM);
863 RECORD(DECL_RECORD);
864 RECORD(DECL_ENUM_CONSTANT);
865 RECORD(DECL_FUNCTION);
866 RECORD(DECL_OBJC_METHOD);
867 RECORD(DECL_OBJC_INTERFACE);
868 RECORD(DECL_OBJC_PROTOCOL);
869 RECORD(DECL_OBJC_IVAR);
870 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000871 RECORD(DECL_OBJC_CATEGORY);
872 RECORD(DECL_OBJC_CATEGORY_IMPL);
873 RECORD(DECL_OBJC_IMPLEMENTATION);
874 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
875 RECORD(DECL_OBJC_PROPERTY);
876 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000877 RECORD(DECL_FIELD);
878 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000879 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000880 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000881 RECORD(DECL_FILE_SCOPE_ASM);
882 RECORD(DECL_BLOCK);
883 RECORD(DECL_CONTEXT_LEXICAL);
884 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000885 RECORD(DECL_NAMESPACE);
886 RECORD(DECL_NAMESPACE_ALIAS);
887 RECORD(DECL_USING);
888 RECORD(DECL_USING_SHADOW);
889 RECORD(DECL_USING_DIRECTIVE);
890 RECORD(DECL_UNRESOLVED_USING_VALUE);
891 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
892 RECORD(DECL_LINKAGE_SPEC);
893 RECORD(DECL_CXX_RECORD);
894 RECORD(DECL_CXX_METHOD);
895 RECORD(DECL_CXX_CONSTRUCTOR);
896 RECORD(DECL_CXX_DESTRUCTOR);
897 RECORD(DECL_CXX_CONVERSION);
898 RECORD(DECL_ACCESS_SPEC);
899 RECORD(DECL_FRIEND);
900 RECORD(DECL_FRIEND_TEMPLATE);
901 RECORD(DECL_CLASS_TEMPLATE);
902 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
903 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
904 RECORD(DECL_FUNCTION_TEMPLATE);
905 RECORD(DECL_TEMPLATE_TYPE_PARM);
906 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
907 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
908 RECORD(DECL_STATIC_ASSERT);
909 RECORD(DECL_CXX_BASE_SPECIFIERS);
910 RECORD(DECL_INDIRECTFIELD);
911 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
912
Douglas Gregora72d8c42011-06-03 02:27:19 +0000913 // Statements and Exprs can occur in the Decls and Types block.
914 AddStmtsExprs(Stream, Record);
915
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000916 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000917 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000918 RECORD(PPD_MACRO_DEFINITION);
919 RECORD(PPD_INCLUSION_DIRECTIVE);
920
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000921#undef RECORD
922#undef BLOCK
923 Stream.ExitBlock();
924}
925
Douglas Gregore650c8c2009-07-07 00:12:59 +0000926/// \brief Adjusts the given filename to only write out the portion of the
927/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000928///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000929/// \param Filename the file name to adjust.
930///
931/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
932/// the returned filename will be adjusted by this system root.
933///
934/// \returns either the original filename (if it needs no adjustment) or the
935/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000936static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000937adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000938 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Douglas Gregor832d6202011-07-22 16:35:34 +0000940 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000941 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943 // Verify that the filename and the system root have the same prefix.
944 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000945 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000946 if (Filename[Pos] != isysroot[Pos])
947 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Douglas Gregore650c8c2009-07-07 00:12:59 +0000949 // We hit the end of the filename before we hit the end of the system root.
950 if (!Filename[Pos])
951 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Douglas Gregore650c8c2009-07-07 00:12:59 +0000953 // If the file name has a '/' at the current position, skip over the '/'.
954 // We distinguish sysroot-based includes from absolute includes by the
955 // absence of '/' at the beginning of sysroot-based includes.
956 if (Filename[Pos] == '/')
957 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Douglas Gregore650c8c2009-07-07 00:12:59 +0000959 return Filename + Pos;
960}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000961
Sebastian Redl3397c552010-08-18 23:56:27 +0000962/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000963void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000964 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000965 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000966
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000968 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000970 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000971 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
972 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
974 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
975 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregore95b9192011-08-17 21:07:30 +0000976 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000980 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000981 Record.push_back(VERSION_MAJOR);
982 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 Record.push_back(CLANG_VERSION_MAJOR);
984 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000985 Record.push_back(!isysroot.empty());
Douglas Gregore95b9192011-08-17 21:07:30 +0000986 const std::string &Triple = Target.getTriple().getTriple();
987 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
988
989 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +0000990 serialization::ModuleManager &Mgr = Chain->getModuleManager();
991 llvm::SmallVector<char, 128> ModulePaths;
992 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +0000993
994 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
995 M != MEnd; ++M) {
996 // Skip modules that weren't directly imported.
997 if (!(*M)->isDirectlyImported())
998 continue;
999
1000 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1001 // FIXME: Write import location, once it matters.
1002 // FIXME: This writes the absolute path for AST files we depend on.
1003 const std::string &FileName = (*M)->FileName;
1004 Record.push_back(FileName.size());
1005 Record.append(FileName.begin(), FileName.end());
1006 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001007 Stream.EmitRecord(IMPORTS, Record);
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregor31d375f2011-05-06 21:43:30 +00001010 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001011 SourceManager &SM = Context.getSourceManager();
1012 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1013 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001014 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001015 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1016 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1017
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001018 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001020 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001021
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001022 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001023 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001024 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001025 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001026 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001027 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001028
1029 Record.clear();
1030 Record.push_back(SM.getMainFileID().getOpaqueValue());
1031 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001032 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001033
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001034 // Original PCH directory
1035 if (!OutputFile.empty() && OutputFile != "-") {
1036 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1037 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1039 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1040
1041 llvm::SmallString<128> OutputPath(OutputFile);
1042
1043 llvm::sys::fs::make_absolute(OutputPath);
1044 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1045
1046 RecordData Record;
1047 Record.push_back(ORIGINAL_PCH_DIR);
1048 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1049 }
1050
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001051 // Repository branch/version information.
1052 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001053 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001054 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1055 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001056 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001057 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001058 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1059 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001060}
1061
1062/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001063void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001064 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001065#define LANGOPT(Name, Bits, Default, Description) \
1066 Record.push_back(LangOpts.Name);
1067#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1068 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1069#include "clang/Basic/LangOptions.def"
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001070
1071 Record.push_back(LangOpts.CurrentModule.size());
1072 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001073 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001074}
1075
Douglas Gregor14f79002009-04-10 03:52:48 +00001076//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001077// stat cache Serialization
1078//===----------------------------------------------------------------------===//
1079
1080namespace {
1081// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001082class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001083public:
1084 typedef const char * key_type;
1085 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattner74e976b2010-11-23 19:28:12 +00001087 typedef struct stat data_type;
1088 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001089
1090 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001091 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
1094 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001095 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096 data_type_ref Data) {
1097 unsigned StrLen = strlen(path);
1098 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001099 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001100 clang::io::Emit8(Out, DataLen);
1101 return std::make_pair(StrLen + 1, DataLen);
1102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Chris Lattner5f9e2722011-07-23 10:55:15 +00001104 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001105 Out.write(path, KeyLen);
1106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Chris Lattner5f9e2722011-07-23 10:55:15 +00001108 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001109 data_type_ref Data, unsigned DataLen) {
1110 using namespace clang::io;
1111 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Chris Lattner74e976b2010-11-23 19:28:12 +00001113 Emit32(Out, (uint32_t) Data.st_ino);
1114 Emit32(Out, (uint32_t) Data.st_dev);
1115 Emit16(Out, (uint16_t) Data.st_mode);
1116 Emit64(Out, (uint64_t) Data.st_mtime);
1117 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001118
1119 assert(Out.tell() - Start == DataLen && "Wrong data length");
1120 }
1121};
1122} // end anonymous namespace
1123
Sebastian Redl3397c552010-08-18 23:56:27 +00001124/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001125void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001126 // Build the on-disk hash table containing information about every
1127 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001128 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001129 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001130 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001131 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001132 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001133 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001134 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001137 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001138 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001139 uint32_t BucketOffset;
1140 {
1141 llvm::raw_svector_ostream Out(StatCacheData);
1142 // Make sure that no bucket is at offset 0
1143 clang::io::Emit32(Out, 0);
1144 BucketOffset = Generator.Emit(Out);
1145 }
1146
1147 // Create a blob abbreviation
1148 using namespace llvm;
1149 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001150 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1154 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1155
1156 // Write the stat cache
1157 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001158 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001159 Record.push_back(BucketOffset);
1160 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001161 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001162}
1163
1164//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001165// Source Manager Serialization
1166//===----------------------------------------------------------------------===//
1167
1168/// \brief Create an abbreviation for the SLocEntry that refers to a
1169/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001170static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001171 using namespace llvm;
1172 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001173 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001178 // FileEntry fields.
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001186 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001187}
1188
1189/// \brief Create an abbreviation for the SLocEntry that refers to a
1190/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001191static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001192 using namespace llvm;
1193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001194 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001200 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001201}
1202
1203/// \brief Create an abbreviation for the SLocEntry that refers to a
1204/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001205static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001206 using namespace llvm;
1207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001208 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001210 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001211}
1212
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001213/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1214/// expansion.
1215static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001216 using namespace llvm;
1217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001218 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001224 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001225}
1226
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001227namespace {
1228 // Trait used for the on-disk hash table of header search information.
1229 class HeaderFileInfoTrait {
1230 ASTWriter &Writer;
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001231 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001232
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001233 // Keep track of the framework names we've used during serialization.
1234 SmallVector<char, 128> FrameworkStringData;
1235 llvm::StringMap<unsigned> FrameworkNameOffset;
1236
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001237 public:
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001238 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001239 : Writer(Writer), HS(HS) { }
1240
1241 typedef const char *key_type;
1242 typedef key_type key_type_ref;
1243
1244 typedef HeaderFileInfo data_type;
1245 typedef const data_type &data_type_ref;
1246
1247 static unsigned ComputeHash(const char *path) {
1248 // The hash is based only on the filename portion of the key, so that the
1249 // reader can match based on filenames when symlinking or excess path
1250 // elements ("foo/../", "../") change the form of the name. However,
1251 // complete path is still the key.
1252 return llvm::HashString(llvm::sys::path::filename(path));
1253 }
1254
1255 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001256 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001257 data_type_ref Data) {
1258 unsigned StrLen = strlen(path);
1259 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001260 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001261 clang::io::Emit8(Out, DataLen);
1262 return std::make_pair(StrLen + 1, DataLen);
1263 }
1264
Chris Lattner5f9e2722011-07-23 10:55:15 +00001265 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001266 Out.write(path, KeyLen);
1267 }
1268
Chris Lattner5f9e2722011-07-23 10:55:15 +00001269 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001270 data_type_ref Data, unsigned DataLen) {
1271 using namespace clang::io;
1272 uint64_t Start = Out.tell(); (void)Start;
1273
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001274 unsigned char Flags = (Data.isImport << 5)
1275 | (Data.isPragmaOnce << 4)
1276 | (Data.DirInfo << 2)
1277 | (Data.Resolved << 1)
1278 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001279 Emit8(Out, (uint8_t)Flags);
1280 Emit16(Out, (uint16_t) Data.NumIncludes);
1281
1282 if (!Data.ControllingMacro)
1283 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1284 else
1285 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001286
1287 unsigned Offset = 0;
1288 if (!Data.Framework.empty()) {
1289 // If this header refers into a framework, save the framework name.
1290 llvm::StringMap<unsigned>::iterator Pos
1291 = FrameworkNameOffset.find(Data.Framework);
1292 if (Pos == FrameworkNameOffset.end()) {
1293 Offset = FrameworkStringData.size() + 1;
1294 FrameworkStringData.append(Data.Framework.begin(),
1295 Data.Framework.end());
1296 FrameworkStringData.push_back(0);
1297
1298 FrameworkNameOffset[Data.Framework] = Offset;
1299 } else
1300 Offset = Pos->second;
1301 }
1302 Emit32(Out, Offset);
1303
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001304 assert(Out.tell() - Start == DataLen && "Wrong data length");
1305 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001306
1307 const char *strings_begin() const { return FrameworkStringData.begin(); }
1308 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001309 };
1310} // end anonymous namespace
1311
1312/// \brief Write the header search block for the list of files that
1313///
1314/// \param HS The header search structure to save.
1315///
1316/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001317void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001318 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001319 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1320
1321 if (FilesByUID.size() > HS.header_file_size())
1322 FilesByUID.resize(HS.header_file_size());
1323
1324 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1325 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001326 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001327 unsigned NumHeaderSearchEntries = 0;
1328 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1329 const FileEntry *File = FilesByUID[UID];
1330 if (!File)
1331 continue;
1332
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001333 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1334 // from the external source if it was not provided already.
1335 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001336 if (HFI.External && Chain)
1337 continue;
1338
1339 // Turn the file name into an absolute path, if it isn't already.
1340 const char *Filename = File->getName();
1341 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1342
1343 // If we performed any translation on the file name at all, we need to
1344 // save this string, since the generator will refer to it later.
1345 if (Filename != File->getName()) {
1346 Filename = strdup(Filename);
1347 SavedStrings.push_back(Filename);
1348 }
1349
1350 Generator.insert(Filename, HFI, GeneratorTrait);
1351 ++NumHeaderSearchEntries;
1352 }
1353
1354 // Create the on-disk hash table in a buffer.
1355 llvm::SmallString<4096> TableData;
1356 uint32_t BucketOffset;
1357 {
1358 llvm::raw_svector_ostream Out(TableData);
1359 // Make sure that no bucket is at offset 0
1360 clang::io::Emit32(Out, 0);
1361 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1362 }
1363
1364 // Create a blob abbreviation
1365 using namespace llvm;
1366 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1367 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1372 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1373
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001374 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001375 RecordData Record;
1376 Record.push_back(HEADER_SEARCH_TABLE);
1377 Record.push_back(BucketOffset);
1378 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001379 Record.push_back(TableData.size());
1380 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001381 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1382
1383 // Free all of the strings we had to duplicate.
1384 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1385 free((void*)SavedStrings[I]);
1386}
1387
Douglas Gregor14f79002009-04-10 03:52:48 +00001388/// \brief Writes the block containing the serialized form of the
1389/// source manager.
1390///
1391/// TODO: We should probably use an on-disk hash table (stored in a
1392/// blob), indexed based on the file name, so that we only create
1393/// entries for files that we actually need. In the common case (no
1394/// errors), we probably won't have to create file entries for any of
1395/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001396void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001397 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001398 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001399 RecordData Record;
1400
Chris Lattnerf04ad692009-04-10 17:16:57 +00001401 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001402 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001403
1404 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001405 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1406 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1407 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001408 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001409
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001410 // Write out the source location entry table. We skip the first
1411 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001412 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001413 // Write out the offsets of only source location file entries.
1414 // We will go through them in ASTReader::validateFileEntries().
1415 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001416 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001417 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1418 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001419 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001420 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001421 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001422
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001423 // Record the offset of this source-location entry.
1424 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1425
1426 // Figure out which record code to use.
1427 unsigned Code;
1428 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001429 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1430 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001431 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001432 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1433 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001434 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001435 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001436 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001437 Record.clear();
1438 Record.push_back(Code);
1439
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001440 // Starting offset of this entry within this module, so skip the dummy.
1441 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001442 if (SLoc->isFile()) {
1443 const SrcMgr::FileInfo &File = SLoc->getFile();
1444 Record.push_back(File.getIncludeLoc().getRawEncoding());
1445 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1446 Record.push_back(File.hasLineDirectives());
1447
1448 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001449 if (Content->OrigEntry) {
1450 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001451 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001452
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001453 // The source location entry is a file. The blob associated
1454 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregor2d52be52010-03-21 22:49:54 +00001456 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001457 Record.push_back(Content->OrigEntry->getSize());
1458 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001459 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001460 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001461
1462 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1463 if (FDI != FileDeclIDs.end()) {
1464 Record.push_back(FDI->second->FirstDeclIndex);
1465 Record.push_back(FDI->second->DeclIDs.size());
1466 } else {
1467 Record.push_back(0);
1468 Record.push_back(0);
1469 }
Douglas Gregora081da52011-11-16 20:05:18 +00001470
Douglas Gregore650c8c2009-07-07 00:12:59 +00001471 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001472 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001473 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001474
1475 // Ask the file manager to fixup the relative path for us. This will
1476 // honor the working directory.
1477 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1478
1479 // FIXME: This call to make_absolute shouldn't be necessary, the
1480 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001481 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001482 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregore650c8c2009-07-07 00:12:59 +00001484 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001485 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001486
1487 if (Content->BufferOverridden) {
1488 Record.clear();
1489 Record.push_back(SM_SLOC_BUFFER_BLOB);
1490 const llvm::MemoryBuffer *Buffer
1491 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1492 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1493 StringRef(Buffer->getBufferStart(),
1494 Buffer->getBufferSize() + 1));
1495 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001496 } else {
1497 // The source location entry is a buffer. The blob associated
1498 // with this entry contains the contents of the buffer.
1499
1500 // We add one to the size so that we capture the trailing NULL
1501 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1502 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001503 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001504 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001505 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001506 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001507 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001508 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001509 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001510 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001511 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001512 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001513
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001514 if (strcmp(Name, "<built-in>") == 0) {
1515 PreloadSLocs.push_back(SLocEntryOffsets.size());
1516 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001517 }
1518 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001519 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001520 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001521 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1522 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001523 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1524 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001525
1526 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001527 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001528 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001529 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001530 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001531 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 }
1533 }
1534
Douglas Gregorc9490c02009-04-16 22:23:12 +00001535 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001536
1537 if (SLocEntryOffsets.empty())
1538 return;
1539
Sebastian Redl3397c552010-08-18 23:56:27 +00001540 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001541 // table is used for lazily loading source-location information.
1542 using namespace llvm;
1543 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001544 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1548 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001550 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001551 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001553 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001554 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001555
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001556 Abbrev = new BitCodeAbbrev();
1557 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1560 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1561
1562 Record.clear();
1563 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1564 Record.push_back(SLocFileEntryOffsets.size());
1565 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1566 data(SLocFileEntryOffsets));
1567
Sebastian Redl3397c552010-08-18 23:56:27 +00001568 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001569 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001570 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001571
1572 // Write the line table. It depends on remapping working, so it must come
1573 // after the source location offsets.
1574 if (SourceMgr.hasLineTable()) {
1575 LineTableInfo &LineTable = SourceMgr.getLineTable();
1576
1577 Record.clear();
1578 // Emit the file names
1579 Record.push_back(LineTable.getNumFilenames());
1580 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1581 // Emit the file name
1582 const char *Filename = LineTable.getFilename(I);
1583 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1584 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1585 Record.push_back(FilenameLen);
1586 if (FilenameLen)
1587 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1588 }
1589
1590 // Emit the line entries
1591 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1592 L != LEnd; ++L) {
1593 // Only emit entries for local files.
1594 if (L->first < 0)
1595 continue;
1596
1597 // Emit the file ID
1598 Record.push_back(L->first);
1599
1600 // Emit the line entries
1601 Record.push_back(L->second.size());
1602 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1603 LEEnd = L->second.end();
1604 LE != LEEnd; ++LE) {
1605 Record.push_back(LE->FileOffset);
1606 Record.push_back(LE->LineNo);
1607 Record.push_back(LE->FilenameID);
1608 Record.push_back((unsigned)LE->FileKind);
1609 Record.push_back(LE->IncludeOffset);
1610 }
1611 }
1612 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1613 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001614}
1615
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001616//===----------------------------------------------------------------------===//
1617// Preprocessor Serialization
1618//===----------------------------------------------------------------------===//
1619
Douglas Gregor9c736102011-02-10 18:20:09 +00001620static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1621 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1622 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1623 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1624 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1625 return X.first->getName().compare(Y.first->getName());
1626}
1627
Chris Lattner0b1fb982009-04-10 17:15:23 +00001628/// \brief Writes the block containing the serialized form of the
1629/// preprocessor.
1630///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001631void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001632 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1633 if (PPRec)
1634 WritePreprocessorDetail(*PPRec);
1635
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001636 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001637
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001638 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1639 if (PP.getCounterValue() != 0) {
1640 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001641 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001642 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001643 }
1644
1645 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001646 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Sebastian Redl3397c552010-08-18 23:56:27 +00001648 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001649 // FIXME: use diagnostics subsystem for localization etc.
1650 if (PP.SawDateOrTime())
1651 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Douglas Gregorecdcb882010-10-20 22:00:55 +00001653
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001654 // Loop over all the macro definitions that are live at the end of the file,
1655 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001656
Douglas Gregor9c736102011-02-10 18:20:09 +00001657 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001658 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001659 MacrosToEmit;
1660 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001661 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1662 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001663 I != E; ++I) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001664 const IdentifierInfo *Name = I->first;
Douglas Gregoraa93a872011-10-17 15:32:29 +00001665 if (!IsModule || I->second->isPublic()) {
Douglas Gregor1d4c1132011-12-20 22:06:13 +00001666 MacroDefinitionsSeen.insert(Name);
Douglas Gregor7143aab2011-09-01 17:04:32 +00001667 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1668 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001669 }
1670
1671 // Sort the set of macro definitions that need to be serialized by the
1672 // name of the macro, to provide a stable ordering.
1673 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1674 &compareMacroDefinitions);
1675
Douglas Gregor040a8042011-02-11 00:26:14 +00001676 // Resolve any identifiers that defined macros at the time they were
1677 // deserialized, adding them to the list of macros to emit (if appropriate).
1678 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1679 IdentifierInfo *Name
1680 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1681 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1682 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1683 }
1684
Douglas Gregor9c736102011-02-10 18:20:09 +00001685 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1686 const IdentifierInfo *Name = MacrosToEmit[I].first;
1687 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001688 if (!MI)
1689 continue;
1690
Sebastian Redl3397c552010-08-18 23:56:27 +00001691 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001692 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001693 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001694
1695 // FIXME: There is a (probably minor) optimization we could do here, if
1696 // the macro comes from the original PCH but the identifier comes from a
1697 // chained PCH, by storing the offset into the original PCH rather than
1698 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001699 if (MI->isBuiltinMacro() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00001700 (Chain &&
1701 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1702 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001703 continue;
1704
Douglas Gregor9c736102011-02-10 18:20:09 +00001705 AddIdentifierRef(Name, Record);
1706 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001707 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1708 Record.push_back(MI->isUsed());
Douglas Gregoraa93a872011-10-17 15:32:29 +00001709 Record.push_back(MI->isPublic());
1710 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001711 unsigned Code;
1712 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001713 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001714 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001715 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001717 Record.push_back(MI->isC99Varargs());
1718 Record.push_back(MI->isGNUVarargs());
1719 Record.push_back(MI->getNumArgs());
1720 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1721 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001722 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001723 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001724
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001725 // If we have a detailed preprocessing record, record the macro definition
1726 // ID that corresponds to this macro.
1727 if (PPRec)
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001728 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001729
Douglas Gregorc9490c02009-04-16 22:23:12 +00001730 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001731 Record.clear();
1732
Chris Lattnerdf961c22009-04-10 18:08:30 +00001733 // Emit the tokens array.
1734 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1735 // Note that we know that the preprocessor does not have any annotation
1736 // tokens in it because they are created by the parser, and thus can't be
1737 // in a macro definition.
1738 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Chris Lattnerdf961c22009-04-10 18:08:30 +00001740 Record.push_back(Tok.getLocation().getRawEncoding());
1741 Record.push_back(Tok.getLength());
1742
Chris Lattnerdf961c22009-04-10 18:08:30 +00001743 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1744 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001745 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001746 // FIXME: Should translate token kind to a stable encoding.
1747 Record.push_back(Tok.getKind());
1748 // FIXME: Should translate token flags to a stable encoding.
1749 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001751 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001752 Record.clear();
1753 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001754 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001755 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001756 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001757}
1758
1759void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001760 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001761 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001762
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001763 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001764
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001765 // Enter the preprocessor block.
1766 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001767
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001768 // If the preprocessor has a preprocessing record, emit it.
1769 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001770 using namespace llvm;
1771
1772 // Set up the abbreviation for
1773 unsigned InclusionAbbrev = 0;
1774 {
1775 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1776 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1781 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1782 }
1783
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001784 unsigned FirstPreprocessorEntityID
1785 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1786 + NUM_PREDEF_PP_ENTITY_IDS;
1787 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001788 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001789 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1790 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001791 E != EEnd;
1792 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001793 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001794
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001795 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1796 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001797
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001798 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001799 // Record this macro definition's ID.
1800 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001801
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001802 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001803 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1804 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001805 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001806
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001807 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001808 Record.push_back(ME->isBuiltinMacro());
1809 if (ME->isBuiltinMacro())
1810 AddIdentifierRef(ME->getName(), Record);
1811 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001812 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001813 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001814 continue;
1815 }
1816
1817 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1818 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001819 Record.push_back(ID->getFileName().size());
1820 Record.push_back(ID->wasInQuotes());
1821 Record.push_back(static_cast<unsigned>(ID->getKind()));
1822 llvm::SmallString<64> Buffer;
1823 Buffer += ID->getFileName();
1824 Buffer += ID->getFile()->getName();
1825 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1826 continue;
1827 }
1828
1829 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1830 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001831 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001832
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001833 // Write the offsets table for the preprocessing record.
1834 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001835 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1836
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001837 // Write the offsets table for identifier IDs.
1838 using namespace llvm;
1839 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001840 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001843 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001844
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001845 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001846 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001847 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001848 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1849 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001850 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001851}
1852
Douglas Gregore209e502011-12-06 01:10:29 +00001853unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1854 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1855 if (Known != SubmoduleIDs.end())
1856 return Known->second;
1857
1858 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1859}
1860
Douglas Gregor26ced122011-12-01 00:59:36 +00001861/// \brief Compute the number of modules within the given tree (including the
1862/// given module).
1863static unsigned getNumberOfModules(Module *Mod) {
1864 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001865 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1866 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001867 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001868 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001869
1870 return ChildModules + 1;
1871}
1872
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001873void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001874 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001875 // FIXME: This feels like it belongs somewhere else, but there are no
1876 // other consumers of this information.
1877 SourceManager &SrcMgr = PP->getSourceManager();
1878 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1879 for (ASTContext::import_iterator I = Context->local_import_begin(),
1880 IEnd = Context->local_import_end();
1881 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001882 if (Module *ImportedFrom
1883 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1884 SrcMgr))) {
1885 ImportedFrom->Imports.push_back(I->getImportedModule());
1886 }
1887 }
1888
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001889 // Enter the submodule description block.
1890 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1891
1892 // Write the abbreviations needed for the submodules block.
1893 using namespace llvm;
1894 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1895 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1905 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1906
1907 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001908 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1910 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1911
1912 Abbrev = new BitCodeAbbrev();
1913 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1915 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001916
1917 Abbrev = new BitCodeAbbrev();
1918 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1920 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1921
Douglas Gregor51f564f2011-12-31 04:05:44 +00001922 Abbrev = new BitCodeAbbrev();
1923 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1924 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1925 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1926
Douglas Gregor26ced122011-12-01 00:59:36 +00001927 // Write the submodule metadata block.
1928 RecordData Record;
1929 Record.push_back(getNumberOfModules(WritingModule));
1930 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1931 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1932
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001933 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001934 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001935 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001936 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001937 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001938 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001939 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001940
1941 // Emit the definition of the block.
1942 Record.clear();
1943 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001944 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001945 if (Mod->Parent) {
1946 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1947 Record.push_back(SubmoduleIDs[Mod->Parent]);
1948 } else {
1949 Record.push_back(0);
1950 }
1951 Record.push_back(Mod->IsFramework);
1952 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001953 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00001954 Record.push_back(Mod->InferSubmodules);
1955 Record.push_back(Mod->InferExplicitSubmodules);
1956 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001957 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1958
Douglas Gregor51f564f2011-12-31 04:05:44 +00001959 // Emit the requirements.
1960 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1961 Record.clear();
1962 Record.push_back(SUBMODULE_REQUIRES);
1963 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1964 Mod->Requires[I].data(),
1965 Mod->Requires[I].size());
1966 }
1967
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001968 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00001969 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001970 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001971 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001972 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00001973 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00001974 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1975 Record.clear();
1976 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1977 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1978 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001979 }
1980
1981 // Emit the headers.
1982 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1983 Record.clear();
1984 Record.push_back(SUBMODULE_HEADER);
1985 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1986 Mod->Headers[I]->getName());
1987 }
Douglas Gregor55988682011-12-05 16:33:54 +00001988
1989 // Emit the imports.
1990 if (!Mod->Imports.empty()) {
1991 Record.clear();
1992 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00001993 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00001994 assert(ImportedID && "Unknown submodule!");
1995 Record.push_back(ImportedID);
1996 }
1997 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
1998 }
1999
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002000 // Emit the exports.
2001 if (!Mod->Exports.empty()) {
2002 Record.clear();
2003 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002004 if (Module *Exported = Mod->Exports[I].getPointer()) {
2005 unsigned ExportedID = SubmoduleIDs[Exported];
2006 assert(ExportedID > 0 && "Unknown submodule ID?");
2007 Record.push_back(ExportedID);
2008 } else {
2009 Record.push_back(0);
2010 }
2011
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002012 Record.push_back(Mod->Exports[I].getInt());
2013 }
2014 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2015 }
2016
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002017 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002018 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2019 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002020 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002021 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002022 }
2023
2024 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002025
2026 assert((NextSubmoduleID - FirstSubmoduleID
2027 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002028}
2029
Douglas Gregor185dbd72011-12-01 02:07:58 +00002030serialization::SubmoduleID
2031ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002032 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002033 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002034
2035 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002036 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002037 Module *OwningMod
2038 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002039 if (!OwningMod)
2040 return 0;
2041
Douglas Gregore209e502011-12-06 01:10:29 +00002042 // Check whether this submodule is part of our own module.
2043 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002044 return 0;
2045
Douglas Gregore209e502011-12-06 01:10:29 +00002046 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002047}
2048
David Blaikied6471f72011-09-25 23:23:43 +00002049void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002050 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002051 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002052 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2053 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002054 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002055 if (point.Loc.isInvalid())
2056 continue;
2057
2058 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002059 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002060 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002061 if (I->second.isPragma()) {
2062 Record.push_back(I->first);
2063 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002064 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002065 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002066 Record.push_back(-1); // mark the end of the diag/map pairs for this
2067 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002068 }
2069
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002070 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002071 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002072}
2073
Anders Carlssonc8505782011-03-06 18:41:18 +00002074void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2075 if (CXXBaseSpecifiersOffsets.empty())
2076 return;
2077
2078 RecordData Record;
2079
2080 // Create a blob abbreviation for the C++ base specifiers offsets.
2081 using namespace llvm;
2082
2083 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2084 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2087 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2088
Douglas Gregore92b8a12011-08-04 00:01:48 +00002089 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002090 Record.clear();
2091 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2092 Record.push_back(CXXBaseSpecifiersOffsets.size());
2093 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002094 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002095}
2096
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002097//===----------------------------------------------------------------------===//
2098// Type Serialization
2099//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002100
Sebastian Redl3397c552010-08-18 23:56:27 +00002101/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002102void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002103 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002104 if (Idx.getIndex() == 0) // we haven't seen this type before.
2105 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002106
Douglas Gregor97475832010-10-05 18:37:06 +00002107 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002108
Douglas Gregor2cf26342009-04-09 22:27:44 +00002109 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002110 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002111 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002112 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002113 else if (TypeOffsets.size() < Index) {
2114 TypeOffsets.resize(Index + 1);
2115 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002116 }
2117
2118 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Douglas Gregor2cf26342009-04-09 22:27:44 +00002120 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002121 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002122
Douglas Gregora4923eb2009-11-16 21:35:15 +00002123 if (T.hasLocalNonFastQualifiers()) {
2124 Qualifiers Qs = T.getLocalQualifiers();
2125 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002126 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002127 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002128 } else {
2129 switch (T->getTypeClass()) {
2130 // For all of the concrete, non-dependent types, call the
2131 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002132#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002133 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002134#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002135#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002136 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002137 }
2138
2139 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002140 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002141
2142 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002143 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002144}
2145
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002146//===----------------------------------------------------------------------===//
2147// Declaration Serialization
2148//===----------------------------------------------------------------------===//
2149
Douglas Gregor2cf26342009-04-09 22:27:44 +00002150/// \brief Write the block containing all of the declaration IDs
2151/// lexically declared within the given DeclContext.
2152///
2153/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2154/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002155uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002156 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002157 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002158 return 0;
2159
Douglas Gregorc9490c02009-04-16 22:23:12 +00002160 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002161 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002162 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002163 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002164 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2165 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002166 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002167
Douglas Gregor25123082009-04-22 22:34:57 +00002168 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002169 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002170 return Offset;
2171}
2172
Sebastian Redla4232eb2010-08-18 23:56:21 +00002173void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002174 using namespace llvm;
2175 RecordData Record;
2176
2177 // Write the type offsets array
2178 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002179 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2183 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2184 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002185 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002186 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002187 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002188 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002189
2190 // Write the declaration offsets array
2191 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002192 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2196 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2197 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002198 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002199 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002200 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002201 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002202}
2203
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002204void ASTWriter::WriteFileDeclIDsMap() {
2205 using namespace llvm;
2206 RecordData Record;
2207
2208 // Join the vectors of DeclIDs from all files.
2209 SmallVector<DeclID, 256> FileSortedIDs;
2210 for (FileDeclIDsTy::iterator
2211 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2212 DeclIDInFileInfo &Info = *FI->second;
2213 Info.FirstDeclIndex = FileSortedIDs.size();
2214 for (LocDeclIDsTy::iterator
2215 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2216 FileSortedIDs.push_back(DI->second);
2217 }
2218
2219 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2220 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2222 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2223 Record.push_back(FILE_SORTED_DECLS);
2224 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2225}
2226
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002227//===----------------------------------------------------------------------===//
2228// Global Method Pool and Selector Serialization
2229//===----------------------------------------------------------------------===//
2230
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002231namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002232// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002233class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002234 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002235
2236public:
2237 typedef Selector key_type;
2238 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002239
Sebastian Redl5d050072010-08-04 17:20:04 +00002240 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002241 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002242 ObjCMethodList Instance, Factory;
2243 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002244 typedef const data_type& data_type_ref;
2245
Sebastian Redl3397c552010-08-18 23:56:27 +00002246 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002248 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002249 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002250 }
Mike Stump1eb44332009-09-09 15:08:12 +00002251
2252 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002253 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002254 data_type_ref Methods) {
2255 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2256 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002257 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2258 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002259 Method = Method->Next)
2260 if (Method->Method)
2261 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002262 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002263 Method = Method->Next)
2264 if (Method->Method)
2265 DataLen += 4;
2266 clang::io::Emit16(Out, DataLen);
2267 return std::make_pair(KeyLen, DataLen);
2268 }
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Chris Lattner5f9e2722011-07-23 10:55:15 +00002270 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002271 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002272 assert((Start >> 32) == 0 && "Selector key offset too large");
2273 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002274 unsigned N = Sel.getNumArgs();
2275 clang::io::Emit16(Out, N);
2276 if (N == 0)
2277 N = 1;
2278 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002279 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002280 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2281 }
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Chris Lattner5f9e2722011-07-23 10:55:15 +00002283 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002284 data_type_ref Methods, unsigned DataLen) {
2285 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002286 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002287 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002288 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002289 Method = Method->Next)
2290 if (Method->Method)
2291 ++NumInstanceMethods;
2292
2293 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002294 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002295 Method = Method->Next)
2296 if (Method->Method)
2297 ++NumFactoryMethods;
2298
2299 clang::io::Emit16(Out, NumInstanceMethods);
2300 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002301 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002302 Method = Method->Next)
2303 if (Method->Method)
2304 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002305 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002306 Method = Method->Next)
2307 if (Method->Method)
2308 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002309
2310 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002311 }
2312};
2313} // end anonymous namespace
2314
Sebastian Redl059612d2010-08-03 21:58:15 +00002315/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002316///
2317/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002318/// in an on-disk hash table indexed by the selector. The hash table also
2319/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002320void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002321 using namespace llvm;
2322
Sebastian Redl059612d2010-08-03 21:58:15 +00002323 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002324 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002325 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002326 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002327 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002328 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002329 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002330 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Sebastian Redl059612d2010-08-03 21:58:15 +00002332 // Create the on-disk hash table representation. We walk through every
2333 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002334 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002335 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002336 I = SelectorIDs.begin(), E = SelectorIDs.end();
2337 I != E; ++I) {
2338 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002339 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002340 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002341 I->second,
2342 ObjCMethodList(),
2343 ObjCMethodList()
2344 };
2345 if (F != SemaRef.MethodPool.end()) {
2346 Data.Instance = F->second.first;
2347 Data.Factory = F->second.second;
2348 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002349 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002350 // changed.
2351 if (Chain && I->second < FirstSelectorID) {
2352 // Selector already exists. Did it change?
2353 bool changed = false;
2354 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2355 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002356 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002357 changed = true;
2358 }
2359 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2360 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002361 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002362 changed = true;
2363 }
2364 if (!changed)
2365 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002366 } else if (Data.Instance.Method || Data.Factory.Method) {
2367 // A new method pool entry.
2368 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002369 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002370 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002371 }
2372
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002373 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002374 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002375 uint32_t BucketOffset;
2376 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002377 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002378 llvm::raw_svector_ostream Out(MethodPool);
2379 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002380 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002381 BucketOffset = Generator.Emit(Out, Trait);
2382 }
2383
2384 // Create a blob abbreviation
2385 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002386 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2390 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2391
Douglas Gregor83941df2009-04-25 17:48:32 +00002392 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002393 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002394 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002395 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002396 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002397 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002398
2399 // Create a blob abbreviation for the selector table offsets.
2400 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002401 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2405 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2406
2407 // Write the selector offsets table.
2408 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002409 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002410 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002411 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002412 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002413 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002414 }
2415}
2416
Sebastian Redl3397c552010-08-18 23:56:27 +00002417/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002418void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002419 using namespace llvm;
2420 if (SemaRef.ReferencedSelectors.empty())
2421 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002422
Fariborz Jahanian32019832010-07-23 19:11:11 +00002423 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002424
Sebastian Redl3397c552010-08-18 23:56:27 +00002425 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002426 // very tricky to fix, and given that @selector shouldn't really appear in
2427 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002428 for (DenseMap<Selector, SourceLocation>::iterator S =
2429 SemaRef.ReferencedSelectors.begin(),
2430 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2431 Selector Sel = (*S).first;
2432 SourceLocation Loc = (*S).second;
2433 AddSelectorRef(Sel, Record);
2434 AddSourceLocation(Loc, Record);
2435 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002436 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002437}
2438
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002439//===----------------------------------------------------------------------===//
2440// Identifier Table Serialization
2441//===----------------------------------------------------------------------===//
2442
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002443namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002444class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002445 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002446 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002447 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002448 bool IsModule;
2449
Douglas Gregora92193e2009-04-28 21:18:29 +00002450 /// \brief Determines whether this is an "interesting" identifier
2451 /// that needs a full IdentifierInfo structure written into the hash
2452 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002453 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002454 if (II->isPoisoned() ||
2455 II->isExtensionToken() ||
2456 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002457 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002458 II->getFETokenInfo<void>())
2459 return true;
2460
Douglas Gregorce835df2011-09-14 22:14:14 +00002461 return hasMacroDefinition(II, Macro);
2462 }
2463
2464 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002465 if (!II->hasMacroDefinition())
2466 return false;
2467
Douglas Gregorce835df2011-09-14 22:14:14 +00002468 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002469 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor7143aab2011-09-01 17:04:32 +00002470
Douglas Gregorce835df2011-09-14 22:14:14 +00002471 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002472 }
2473
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002474public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002475 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002476 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002477
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002478 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002479 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002480
Douglas Gregoreee242f2011-10-27 09:33:13 +00002481 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2482 IdentifierResolver &IdResolver, bool IsModule)
2483 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002484
2485 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002486 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002487 }
Mike Stump1eb44332009-09-09 15:08:12 +00002488
2489 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002490 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002491 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002492 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002493 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002494 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002495 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregorce835df2011-09-14 22:14:14 +00002496 if (hasMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002497 DataLen += 8;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002498
2499 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2500 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002501 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002502 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002503 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002504 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002505 // We emit the key length after the data length so that every
2506 // string is preceded by a 16-bit length. This matches the PTH
2507 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002508 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002509 return std::make_pair(KeyLen, DataLen);
2510 }
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Chris Lattner5f9e2722011-07-23 10:55:15 +00002512 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002513 unsigned KeyLen) {
2514 // Record the location of the key data. This is used when generating
2515 // the mapping from persistent IDs to strings.
2516 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002517 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002518 }
Mike Stump1eb44332009-09-09 15:08:12 +00002519
Douglas Gregor7143aab2011-09-01 17:04:32 +00002520 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002521 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002522 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002523 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002524 clang::io::Emit32(Out, ID << 1);
2525 return;
2526 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002527
Douglas Gregora92193e2009-04-28 21:18:29 +00002528 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002529 uint32_t Bits = 0;
Douglas Gregorce835df2011-09-14 22:14:14 +00002530 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregor5998da52009-04-28 21:32:13 +00002531 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topper925be542011-12-19 05:04:33 +00002532 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregorce835df2011-09-14 22:14:14 +00002533 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002534 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2535 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002536 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002537 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002538 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002539
Douglas Gregor13292642011-12-02 15:45:10 +00002540 if (HasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002541 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor13292642011-12-02 15:45:10 +00002542 clang::io::Emit32(Out,
2543 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2544 }
2545
Douglas Gregor668c1a42009-04-21 22:25:48 +00002546 // Emit the declaration IDs in reverse order, because the
2547 // IdentifierResolver provides the declarations as they would be
2548 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002549 // "stat"), but the ASTReader adds declarations to the end of the list
2550 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002551 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002552 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2553 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002554 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002555 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002556 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002557 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002558 }
2559};
2560} // end anonymous namespace
2561
Sebastian Redl3397c552010-08-18 23:56:27 +00002562/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002563///
2564/// The identifier table consists of a blob containing string data
2565/// (the actual identifiers themselves) and a separate "offsets" index
2566/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002567void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2568 IdentifierResolver &IdResolver,
2569 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002570 using namespace llvm;
2571
2572 // Create and write out the blob that contains the identifier
2573 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002574 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002575 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002576 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002577
Douglas Gregor92b059e2009-04-28 20:33:11 +00002578 // Look for any identifiers that were named while processing the
2579 // headers, but are otherwise not needed. We add these to the hash
2580 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002581 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002582 // file.
2583 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2584 IDEnd = PP.getIdentifierTable().end();
2585 ID != IDEnd; ++ID)
2586 getIdentifierRef(ID->second);
2587
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002588 // Create the on-disk hash table representation. We only store offsets
2589 // for identifiers that appear here for the first time.
2590 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002591 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002592 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2593 ID != IDEnd; ++ID) {
2594 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002595 if (!Chain || !ID->first->isFromAST() ||
2596 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002597 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2598 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002599 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002600
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002601 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002602 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002603 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002604 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002605 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002606 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002607 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002608 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002609 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002610 }
2611
2612 // Create a blob abbreviation
2613 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002614 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002617 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002618
2619 // Write the identifier table
2620 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002621 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002622 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002623 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002624 }
2625
2626 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002627 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002628 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2632 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2633
2634 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002635 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002636 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002637 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002638 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002639 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002640}
2641
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002642//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002643// DeclContext's Name Lookup Table Serialization
2644//===----------------------------------------------------------------------===//
2645
2646namespace {
2647// Trait used for the on-disk hash table used in the method pool.
2648class ASTDeclContextNameLookupTrait {
2649 ASTWriter &Writer;
2650
2651public:
2652 typedef DeclarationName key_type;
2653 typedef key_type key_type_ref;
2654
2655 typedef DeclContext::lookup_result data_type;
2656 typedef const data_type& data_type_ref;
2657
2658 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2659
2660 unsigned ComputeHash(DeclarationName Name) {
2661 llvm::FoldingSetNodeID ID;
2662 ID.AddInteger(Name.getNameKind());
2663
2664 switch (Name.getNameKind()) {
2665 case DeclarationName::Identifier:
2666 ID.AddString(Name.getAsIdentifierInfo()->getName());
2667 break;
2668 case DeclarationName::ObjCZeroArgSelector:
2669 case DeclarationName::ObjCOneArgSelector:
2670 case DeclarationName::ObjCMultiArgSelector:
2671 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2672 break;
2673 case DeclarationName::CXXConstructorName:
2674 case DeclarationName::CXXDestructorName:
2675 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002676 break;
2677 case DeclarationName::CXXOperatorName:
2678 ID.AddInteger(Name.getCXXOverloadedOperator());
2679 break;
2680 case DeclarationName::CXXLiteralOperatorName:
2681 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2682 case DeclarationName::CXXUsingDirective:
2683 break;
2684 }
2685
2686 return ID.ComputeHash();
2687 }
2688
2689 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002690 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002691 data_type_ref Lookup) {
2692 unsigned KeyLen = 1;
2693 switch (Name.getNameKind()) {
2694 case DeclarationName::Identifier:
2695 case DeclarationName::ObjCZeroArgSelector:
2696 case DeclarationName::ObjCOneArgSelector:
2697 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002698 case DeclarationName::CXXLiteralOperatorName:
2699 KeyLen += 4;
2700 break;
2701 case DeclarationName::CXXOperatorName:
2702 KeyLen += 1;
2703 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002704 case DeclarationName::CXXConstructorName:
2705 case DeclarationName::CXXDestructorName:
2706 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002707 case DeclarationName::CXXUsingDirective:
2708 break;
2709 }
2710 clang::io::Emit16(Out, KeyLen);
2711
2712 // 2 bytes for num of decls and 4 for each DeclID.
2713 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2714 clang::io::Emit16(Out, DataLen);
2715
2716 return std::make_pair(KeyLen, DataLen);
2717 }
2718
Chris Lattner5f9e2722011-07-23 10:55:15 +00002719 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002720 using namespace clang::io;
2721
2722 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2723 Emit8(Out, Name.getNameKind());
2724 switch (Name.getNameKind()) {
2725 case DeclarationName::Identifier:
2726 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2727 break;
2728 case DeclarationName::ObjCZeroArgSelector:
2729 case DeclarationName::ObjCOneArgSelector:
2730 case DeclarationName::ObjCMultiArgSelector:
2731 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2732 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002733 case DeclarationName::CXXOperatorName:
2734 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2735 Emit8(Out, Name.getCXXOverloadedOperator());
2736 break;
2737 case DeclarationName::CXXLiteralOperatorName:
2738 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2739 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002740 case DeclarationName::CXXConstructorName:
2741 case DeclarationName::CXXDestructorName:
2742 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002743 case DeclarationName::CXXUsingDirective:
2744 break;
2745 }
2746 }
2747
Chris Lattner5f9e2722011-07-23 10:55:15 +00002748 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002749 data_type Lookup, unsigned DataLen) {
2750 uint64_t Start = Out.tell(); (void)Start;
2751 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2752 for (; Lookup.first != Lookup.second; ++Lookup.first)
2753 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2754
2755 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2756 }
2757};
2758} // end anonymous namespace
2759
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002760/// \brief Write the block containing all of the declaration IDs
2761/// visible from the given DeclContext.
2762///
2763/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002764/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002765uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2766 DeclContext *DC) {
2767 if (DC->getPrimaryContext() != DC)
2768 return 0;
2769
2770 // Since there is no name lookup into functions or methods, don't bother to
2771 // build a visible-declarations table for these entities.
2772 if (DC->isFunctionOrMethod())
2773 return 0;
2774
2775 // If not in C++, we perform name lookup for the translation unit via the
2776 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2777 // FIXME: In C++ we need the visible declarations in order to "see" the
2778 // friend declarations, is there a way to do this without writing the table ?
2779 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2780 return 0;
2781
2782 // Force the DeclContext to build a its name-lookup table.
Douglas Gregorc266de92011-08-24 21:56:08 +00002783 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002784 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002785
2786 // Serialize the contents of the mapping used for lookup. Note that,
2787 // although we have two very different code paths, the serialized
2788 // representation is the same for both cases: a declaration name,
2789 // followed by a size, followed by references to the visible
2790 // declarations that have that name.
2791 uint64_t Offset = Stream.GetCurrentBitNo();
2792 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2793 if (!Map || Map->empty())
2794 return 0;
2795
2796 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2797 ASTDeclContextNameLookupTrait Trait(*this);
2798
2799 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002800 DeclarationName ConversionName;
2801 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002802 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2803 D != DEnd; ++D) {
2804 DeclarationName Name = D->first;
2805 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002806 if (Result.first != Result.second) {
2807 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2808 // Hash all conversion function names to the same name. The actual
2809 // type information in conversion function name is not used in the
2810 // key (since such type information is not stable across different
2811 // modules), so the intended effect is to coalesce all of the conversion
2812 // functions under a single key.
2813 if (!ConversionName)
2814 ConversionName = Name;
2815 ConversionDecls.append(Result.first, Result.second);
2816 continue;
2817 }
2818
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002819 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002820 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002821 }
2822
Douglas Gregore5a54b62011-08-30 20:49:19 +00002823 // Add the conversion functions
2824 if (!ConversionDecls.empty()) {
2825 Generator.insert(ConversionName,
2826 DeclContext::lookup_result(ConversionDecls.begin(),
2827 ConversionDecls.end()),
2828 Trait);
2829 }
2830
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002831 // Create the on-disk hash table in a buffer.
2832 llvm::SmallString<4096> LookupTable;
2833 uint32_t BucketOffset;
2834 {
2835 llvm::raw_svector_ostream Out(LookupTable);
2836 // Make sure that no bucket is at offset 0
2837 clang::io::Emit32(Out, 0);
2838 BucketOffset = Generator.Emit(Out, Trait);
2839 }
2840
2841 // Write the lookup table
2842 RecordData Record;
2843 Record.push_back(DECL_CONTEXT_VISIBLE);
2844 Record.push_back(BucketOffset);
2845 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2846 LookupTable.str());
2847
2848 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2849 ++NumVisibleDeclContexts;
2850 return Offset;
2851}
2852
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002853/// \brief Write an UPDATE_VISIBLE block for the given context.
2854///
2855/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2856/// DeclContext in a dependent AST file. As such, they only exist for the TU
2857/// (in C++) and for namespaces.
2858void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002859 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2860 if (!Map || Map->empty())
2861 return;
2862
2863 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2864 ASTDeclContextNameLookupTrait Trait(*this);
2865
2866 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002867 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2868 D != DEnd; ++D) {
2869 DeclarationName Name = D->first;
2870 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002871 // For any name that appears in this table, the results are complete, i.e.
2872 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002873 if (Result.first != Result.second)
2874 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002875 }
2876
2877 // Create the on-disk hash table in a buffer.
2878 llvm::SmallString<4096> LookupTable;
2879 uint32_t BucketOffset;
2880 {
2881 llvm::raw_svector_ostream Out(LookupTable);
2882 // Make sure that no bucket is at offset 0
2883 clang::io::Emit32(Out, 0);
2884 BucketOffset = Generator.Emit(Out, Trait);
2885 }
2886
2887 // Write the lookup table
2888 RecordData Record;
2889 Record.push_back(UPDATE_VISIBLE);
2890 Record.push_back(getDeclID(cast<Decl>(DC)));
2891 Record.push_back(BucketOffset);
2892 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2893}
2894
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002895/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2896void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2897 RecordData Record;
2898 Record.push_back(Opts.fp_contract);
2899 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2900}
2901
2902/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2903void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2904 if (!SemaRef.Context.getLangOptions().OpenCL)
2905 return;
2906
2907 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2908 RecordData Record;
2909#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2910#include "clang/Basic/OpenCLExtensions.def"
2911 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2912}
2913
Douglas Gregor2171bf12012-01-15 16:58:34 +00002914void ASTWriter::WriteRedeclarations() {
2915 RecordData LocalRedeclChains;
2916 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2917
2918 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2919 Decl *First = Redeclarations[I];
2920 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2921
2922 Decl *MostRecent = First->getMostRecentDecl();
2923
2924 // If we only have a single declaration, there is no point in storing
2925 // a redeclaration chain.
2926 if (First == MostRecent)
2927 continue;
2928
2929 unsigned Offset = LocalRedeclChains.size();
2930 unsigned Size = 0;
2931 LocalRedeclChains.push_back(0); // Placeholder for the size.
2932
2933 // Collect the set of local redeclarations of this declaration.
2934 for (Decl *Prev = MostRecent; Prev != First;
2935 Prev = Prev->getPreviousDecl()) {
2936 if (!Prev->isFromASTFile()) {
2937 AddDeclRef(Prev, LocalRedeclChains);
2938 ++Size;
2939 }
2940 }
2941 LocalRedeclChains[Offset] = Size;
2942
2943 // Reverse the set of local redeclarations, so that we store them in
2944 // order (since we found them in reverse order).
2945 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2946
2947 // Add the mapping from the first ID to the set of local declarations.
2948 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2949 LocalRedeclsMap.push_back(Info);
2950
2951 assert(N == Redeclarations.size() &&
2952 "Deserialized a declaration we shouldn't have");
2953 }
2954
2955 if (LocalRedeclChains.empty())
2956 return;
2957
2958 // Sort the local redeclarations map by the first declaration ID,
2959 // since the reader will be performing binary searches on this information.
2960 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2961
2962 // Emit the local redeclarations map.
2963 using namespace llvm;
2964 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2965 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2968 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
2969
2970 RecordData Record;
2971 Record.push_back(LOCAL_REDECLARATIONS_MAP);
2972 Record.push_back(LocalRedeclsMap.size());
2973 Stream.EmitRecordWithBlob(AbbrevID, Record,
2974 reinterpret_cast<char*>(LocalRedeclsMap.data()),
2975 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
2976
2977 // Emit the redeclaration chains.
2978 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
2979}
2980
Douglas Gregorcff9f262012-01-27 01:47:08 +00002981void ASTWriter::WriteObjCCategories() {
2982 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
2983 RecordData Categories;
2984
2985 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
2986 unsigned Size = 0;
2987 unsigned StartIndex = Categories.size();
2988
2989 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
2990
2991 // Allocate space for the size.
2992 Categories.push_back(0);
2993
2994 // Add the categories.
2995 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
2996 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
2997 assert(getDeclID(Cat) != 0 && "Bogus category");
2998 AddDeclRef(Cat, Categories);
2999 }
3000
3001 // Update the size.
3002 Categories[StartIndex] = Size;
3003
3004 // Record this interface -> category map.
3005 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3006 CategoriesMap.push_back(CatInfo);
3007 }
3008
3009 // Sort the categories map by the definition ID, since the reader will be
3010 // performing binary searches on this information.
3011 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3012
3013 // Emit the categories map.
3014 using namespace llvm;
3015 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3016 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3019 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3020
3021 RecordData Record;
3022 Record.push_back(OBJC_CATEGORIES_MAP);
3023 Record.push_back(CategoriesMap.size());
3024 Stream.EmitRecordWithBlob(AbbrevID, Record,
3025 reinterpret_cast<char*>(CategoriesMap.data()),
3026 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3027
3028 // Emit the category lists.
3029 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3030}
3031
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003032void ASTWriter::WriteMergedDecls() {
3033 if (!Chain || Chain->MergedDecls.empty())
3034 return;
3035
3036 RecordData Record;
3037 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3038 IEnd = Chain->MergedDecls.end();
3039 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003040 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003041 : getDeclID(I->first);
3042 assert(CanonID && "Merged declaration not known?");
3043
3044 Record.push_back(CanonID);
3045 Record.push_back(I->second.size());
3046 Record.append(I->second.begin(), I->second.end());
3047 }
3048 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3049}
3050
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003051//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003052// General Serialization Routines
3053//===----------------------------------------------------------------------===//
3054
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003055/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003056void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003057 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00003058 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
3059 const Attr * A = *i;
3060 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003061 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003062
Sean Huntcf807c42010-08-18 23:23:40 +00003063#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003064
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003065 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003066}
3067
Chris Lattner5f9e2722011-07-23 10:55:15 +00003068void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003069 Record.push_back(Str.size());
3070 Record.insert(Record.end(), Str.begin(), Str.end());
3071}
3072
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003073void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3074 RecordDataImpl &Record) {
3075 Record.push_back(Version.getMajor());
3076 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3077 Record.push_back(*Minor + 1);
3078 else
3079 Record.push_back(0);
3080 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3081 Record.push_back(*Subminor + 1);
3082 else
3083 Record.push_back(0);
3084}
3085
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003086/// \brief Note that the identifier II occurs at the given offset
3087/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003088void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003089 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003090 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003091 // up earlier in the chain and thus don't need an offset.
3092 if (ID >= FirstIdentID)
3093 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003094}
3095
Douglas Gregor83941df2009-04-25 17:48:32 +00003096/// \brief Note that the selector Sel occurs at the given offset
3097/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003098void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003099 unsigned ID = SelectorIDs[Sel];
3100 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003101 // Don't record offsets for selectors that are also available in a different
3102 // file.
3103 if (ID < FirstSelectorID)
3104 return;
3105 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003106}
3107
Sebastian Redla4232eb2010-08-18 23:56:21 +00003108ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003109 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
3110 WritingAST(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003111 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003112 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003113 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003114 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3115 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003116 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003117 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003118 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003119 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003120 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003121 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003122 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3123 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3124 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003125 DeclTypedefAbbrev(0),
3126 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3127 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003128{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003129}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003130
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003131ASTWriter::~ASTWriter() {
3132 for (FileDeclIDsTy::iterator
3133 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3134 delete I->second;
3135}
3136
Sebastian Redla4232eb2010-08-18 23:56:21 +00003137void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003138 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003139 Module *WritingModule, StringRef isysroot) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003140 WritingAST = true;
3141
Douglas Gregor2cf26342009-04-09 22:27:44 +00003142 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003143 Stream.Emit((unsigned)'C', 8);
3144 Stream.Emit((unsigned)'P', 8);
3145 Stream.Emit((unsigned)'C', 8);
3146 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003147
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003148 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003149
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003150 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003151 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003152 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003153 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003154 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003155 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003156 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003157
3158 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003159}
3160
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003161template<typename Vector>
3162static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3163 ASTWriter::RecordData &Record) {
3164 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3165 I != E; ++I) {
3166 Writer.AddDeclRef(*I, Record);
3167 }
3168}
3169
Sebastian Redla4232eb2010-08-18 23:56:21 +00003170void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003171 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003172 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003173 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003174 using namespace llvm;
3175
Douglas Gregorecc2c092011-12-01 22:20:10 +00003176 // Make sure that the AST reader knows to finalize itself.
3177 if (Chain)
3178 Chain->finalizeForWriting();
3179
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003180 ASTContext &Context = SemaRef.Context;
3181 Preprocessor &PP = SemaRef.PP;
3182
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003183 // Set up predefined declaration IDs.
3184 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003185 if (Context.ObjCIdDecl)
3186 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003187 if (Context.ObjCSelDecl)
3188 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003189 if (Context.ObjCClassDecl)
3190 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003191 if (Context.ObjCProtocolClassDecl)
3192 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003193 if (Context.Int128Decl)
3194 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3195 if (Context.UInt128Decl)
3196 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003197 if (Context.ObjCInstanceTypeDecl)
3198 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003199
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003200 if (!Chain) {
3201 // Make sure that we emit IdentifierInfos (and any attached
3202 // declarations) for builtins. We don't need to do this when we're
3203 // emitting chained PCH files, because all of the builtins will be
3204 // in the original PCH file.
3205 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003206 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003207 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003208 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
3209 Context.getLangOptions().NoBuiltin);
3210 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3211 getIdentifierRef(&Table.get(BuiltinNames[I]));
3212 }
3213
Douglas Gregoreee242f2011-10-27 09:33:13 +00003214 // If there are any out-of-date identifiers, bring them up to date.
3215 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3216 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3217 IDEnd = PP.getIdentifierTable().end();
3218 ID != IDEnd; ++ID)
3219 if (ID->second->isOutOfDate())
3220 ExtSource->updateOutOfDateIdentifier(*ID->second);
3221 }
3222
Chris Lattner63d65f82009-09-08 18:19:27 +00003223 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003224 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003225 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003226 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003227 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003228
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003229 // Build a record containing all of the file scoped decls in this file.
3230 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003231 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3232 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003233
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003234 // Build a record containing all of the delegating constructors we still need
3235 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003236 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003237 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003238
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003239 // Write the set of weak, undeclared identifiers. We always write the
3240 // entire table, since later PCH files in a PCH chain are only interested in
3241 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003242 RecordData WeakUndeclaredIdentifiers;
3243 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003244 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003245 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3246 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3247 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3248 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3249 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3250 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3251 }
3252 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003253
Douglas Gregor14c22f22009-04-22 22:18:58 +00003254 // Build a record containing all of the locally-scoped external
3255 // declarations in this header file. Generally, this record will be
3256 // empty.
3257 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003258 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003259 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003260 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003261 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3262 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003263 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003264 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003265 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3266 }
3267
Douglas Gregorb81c1702009-04-27 20:06:05 +00003268 // Build a record containing all of the ext_vector declarations.
3269 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003270 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003271
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003272 // Build a record containing all of the VTable uses information.
3273 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003274 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003275 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3276 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3277 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3278 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3279 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003280 }
3281
3282 // Build a record containing all of dynamic classes declarations.
3283 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003284 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003285
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003286 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003287 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003288 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003289 I = SemaRef.PendingInstantiations.begin(),
3290 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3291 AddDeclRef(I->first, PendingInstantiations);
3292 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003293 }
3294 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3295 "There are local ones at end of translation unit!");
3296
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003297 // Build a record containing some declaration references.
3298 RecordData SemaDeclRefs;
3299 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3300 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3301 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3302 }
3303
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003304 RecordData CUDASpecialDeclRefs;
3305 if (Context.getcudaConfigureCallDecl()) {
3306 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3307 }
3308
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003309 // Build a record containing all of the known namespaces.
3310 RecordData KnownNamespaces;
3311 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3312 I = SemaRef.KnownNamespaces.begin(),
3313 IEnd = SemaRef.KnownNamespaces.end();
3314 I != IEnd; ++I) {
3315 if (!I->second)
3316 AddDeclRef(I->first, KnownNamespaces);
3317 }
3318
Sebastian Redl3397c552010-08-18 23:56:27 +00003319 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003320 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003321 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003322 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003323 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00003324 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003325 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003326
3327 // Create a lexical update block containing all of the declarations in the
3328 // translation unit that do not come from other AST files.
3329 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3330 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3331 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3332 E = TU->noload_decls_end();
3333 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003334 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003335 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003336 }
3337
3338 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3339 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3340 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3341 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3342 Record.clear();
3343 Record.push_back(TU_UPDATE_LEXICAL);
3344 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3345 data(NewGlobalDecls));
3346
3347 // And a visible updates block for the translation unit.
3348 Abv = new llvm::BitCodeAbbrev();
3349 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3350 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3351 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3352 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3353 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3354 WriteDeclContextVisibleUpdate(TU);
3355
3356 // If the translation unit has an anonymous namespace, and we don't already
3357 // have an update block for it, write it as an update block.
3358 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3359 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3360 if (Record.empty()) {
3361 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003362 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003363 }
3364 }
3365
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003366 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003367 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003368
Douglas Gregora119da02011-08-02 16:26:37 +00003369 // Form the record of special types.
3370 RecordData SpecialTypes;
3371 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003372 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003373 AddTypeRef(Context.getFILEType(), SpecialTypes);
3374 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3375 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3376 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3377 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003378 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003379 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003380
Douglas Gregor366809a2009-04-26 03:49:13 +00003381 // Keep writing types and declarations until all types and
3382 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003383 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003384 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003385 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3386 E = DeclsToRewrite.end();
3387 I != E; ++I)
3388 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003389 while (!DeclTypesToEmit.empty()) {
3390 DeclOrType DOT = DeclTypesToEmit.front();
3391 DeclTypesToEmit.pop();
3392 if (DOT.isType())
3393 WriteType(DOT.getType());
3394 else
3395 WriteDecl(Context, DOT.getDecl());
3396 }
3397 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003398
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003399 WriteFileDeclIDsMap();
3400 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3401
3402 if (Chain) {
3403 // Write the mapping information describing our module dependencies and how
3404 // each of those modules were mapped into our own offset/ID space, so that
3405 // the reader can build the appropriate mapping to its own offset/ID space.
3406 // The map consists solely of a blob with the following format:
3407 // *(module-name-len:i16 module-name:len*i8
3408 // source-location-offset:i32
3409 // identifier-id:i32
3410 // preprocessed-entity-id:i32
3411 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003412 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003413 // selector-id:i32
3414 // declaration-id:i32
3415 // c++-base-specifiers-id:i32
3416 // type-id:i32)
3417 //
3418 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3419 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3421 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
3422 llvm::SmallString<2048> Buffer;
3423 {
3424 llvm::raw_svector_ostream Out(Buffer);
3425 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003426 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003427 M != MEnd; ++M) {
3428 StringRef FileName = (*M)->FileName;
3429 io::Emit16(Out, FileName.size());
3430 Out.write(FileName.data(), FileName.size());
3431 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3432 io::Emit32(Out, (*M)->BaseIdentifierID);
3433 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003434 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003435 io::Emit32(Out, (*M)->BaseSelectorID);
3436 io::Emit32(Out, (*M)->BaseDeclID);
3437 io::Emit32(Out, (*M)->BaseTypeIndex);
3438 }
3439 }
3440 Record.clear();
3441 Record.push_back(MODULE_OFFSET_MAP);
3442 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3443 Buffer.data(), Buffer.size());
3444 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003445 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003446 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003447 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003448 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003449 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003450 WriteFPPragmaOptions(SemaRef.getFPOptions());
3451 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003452
Sebastian Redl1476ed42010-07-16 16:36:56 +00003453 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003454 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003455
Anders Carlssonc8505782011-03-06 18:41:18 +00003456 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003457
Douglas Gregore209e502011-12-06 01:10:29 +00003458 // If we're emitting a module, write out the submodule information.
3459 if (WritingModule)
3460 WriteSubmodules(WritingModule);
3461
Douglas Gregora119da02011-08-02 16:26:37 +00003462 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3463
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003464 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003465 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003466 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003467
3468 // Write the record containing tentative definitions.
3469 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003470 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003471
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003472 // Write the record containing unused file scoped decls.
3473 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003474 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003475
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003476 // Write the record containing weak undeclared identifiers.
3477 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003478 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003479 WeakUndeclaredIdentifiers);
3480
Douglas Gregor14c22f22009-04-22 22:18:58 +00003481 // Write the record containing locally-scoped external definitions.
3482 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003483 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003484 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003485
3486 // Write the record containing ext_vector type names.
3487 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003488 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003489
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003490 // Write the record containing VTable uses information.
3491 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003492 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003493
3494 // Write the record containing dynamic classes declarations.
3495 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003496 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003497
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003498 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003499 if (!PendingInstantiations.empty())
3500 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003501
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003502 // Write the record containing declaration references of Sema.
3503 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003504 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003505
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003506 // Write the record containing CUDA-specific declaration references.
3507 if (!CUDASpecialDeclRefs.empty())
3508 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003509
3510 // Write the delegating constructors.
3511 if (!DelegatingCtorDecls.empty())
3512 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003513
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003514 // Write the known namespaces.
3515 if (!KnownNamespaces.empty())
3516 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3517
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003518 // Write the visible updates to DeclContexts.
3519 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3520 I = UpdatedDeclContexts.begin(),
3521 E = UpdatedDeclContexts.end();
3522 I != E; ++I)
3523 WriteDeclContextVisibleUpdate(*I);
3524
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003525 if (!WritingModule) {
3526 // Write the submodules that were imported, if any.
3527 RecordData ImportedModules;
3528 for (ASTContext::import_iterator I = Context.local_import_begin(),
3529 IEnd = Context.local_import_end();
3530 I != IEnd; ++I) {
3531 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3532 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3533 }
3534 if (!ImportedModules.empty()) {
3535 // Sort module IDs.
3536 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3537
3538 // Unique module IDs.
3539 ImportedModules.erase(std::unique(ImportedModules.begin(),
3540 ImportedModules.end()),
3541 ImportedModules.end());
3542
3543 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3544 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003545 }
3546
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003547 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003548 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003549 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003550 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003551 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003552
Douglas Gregor3e1af842009-04-17 22:13:46 +00003553 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003554 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003555 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003556 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003557 Record.push_back(NumLexicalDeclContexts);
3558 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003559 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003560 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003561}
3562
Douglas Gregor61c5e342011-09-17 00:05:03 +00003563/// \brief Go through the declaration update blocks and resolve declaration
3564/// pointers into declaration IDs.
3565void ASTWriter::ResolveDeclUpdatesBlocks() {
3566 for (DeclUpdateMap::iterator
3567 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3568 const Decl *D = I->first;
3569 UpdateRecord &URec = I->second;
3570
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003571 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003572 continue; // The decl will be written completely
3573
3574 unsigned Idx = 0, N = URec.size();
3575 while (Idx < N) {
3576 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003577 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3578 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3579 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3580 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3581 ++Idx;
3582 break;
3583
3584 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3585 ++Idx;
3586 break;
3587 }
3588 }
3589 }
3590}
3591
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003592void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003593 if (DeclUpdates.empty())
3594 return;
3595
3596 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003597 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003598 for (DeclUpdateMap::iterator
3599 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3600 const Decl *D = I->first;
3601 UpdateRecord &URec = I->second;
3602
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003603 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003604 continue; // The decl will be written completely,no need to store updates.
3605
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003606 uint64_t Offset = Stream.GetCurrentBitNo();
3607 Stream.EmitRecord(DECL_UPDATES, URec);
3608
3609 OffsetsRecord.push_back(GetDeclRef(D));
3610 OffsetsRecord.push_back(Offset);
3611 }
3612 Stream.ExitBlock();
3613 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3614}
3615
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003616void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003617 if (ReplacedDecls.empty())
3618 return;
3619
3620 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003621 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003622 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003623 Record.push_back(I->ID);
3624 Record.push_back(I->Offset);
3625 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003626 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003627 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003628}
3629
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003630void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003631 Record.push_back(Loc.getRawEncoding());
3632}
3633
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003634void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003635 AddSourceLocation(Range.getBegin(), Record);
3636 AddSourceLocation(Range.getEnd(), Record);
3637}
3638
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003639void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003640 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003641 const uint64_t *Words = Value.getRawData();
3642 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003643}
3644
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003645void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003646 Record.push_back(Value.isUnsigned());
3647 AddAPInt(Value, Record);
3648}
3649
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003650void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003651 AddAPInt(Value.bitcastToAPInt(), Record);
3652}
3653
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003654void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003655 Record.push_back(getIdentifierRef(II));
3656}
3657
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003658IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003659 if (II == 0)
3660 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003661
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003662 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003663 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003664 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003665 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003666}
3667
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003668void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003669 Record.push_back(getSelectorRef(SelRef));
3670}
3671
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003672SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003673 if (Sel.getAsOpaquePtr() == 0) {
3674 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003675 }
3676
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003677 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003678 if (SID == 0 && Chain) {
3679 // This might trigger a ReadSelector callback, which will set the ID for
3680 // this selector.
3681 Chain->LoadSelector(Sel);
3682 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003683 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003684 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003685 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003686 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003687}
3688
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003689void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003690 AddDeclRef(Temp->getDestructor(), Record);
3691}
3692
Douglas Gregor7c789c12010-10-29 22:39:52 +00003693void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3694 CXXBaseSpecifier const *BasesEnd,
3695 RecordDataImpl &Record) {
3696 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3697 CXXBaseSpecifiersToWrite.push_back(
3698 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3699 Bases, BasesEnd));
3700 Record.push_back(NextCXXBaseSpecifiersID++);
3701}
3702
Sebastian Redla4232eb2010-08-18 23:56:21 +00003703void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003704 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003705 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003706 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003707 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003708 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003709 break;
3710 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003711 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003712 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003713 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003714 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003715 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003716 break;
3717 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003718 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003719 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003720 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003721 break;
John McCall833ca992009-10-29 08:12:44 +00003722 case TemplateArgument::Null:
3723 case TemplateArgument::Integral:
3724 case TemplateArgument::Declaration:
3725 case TemplateArgument::Pack:
3726 break;
3727 }
3728}
3729
Sebastian Redla4232eb2010-08-18 23:56:21 +00003730void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003731 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003732 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003733
3734 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3735 bool InfoHasSameExpr
3736 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3737 Record.push_back(InfoHasSameExpr);
3738 if (InfoHasSameExpr)
3739 return; // Avoid storing the same expr twice.
3740 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003741 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3742 Record);
3743}
3744
Douglas Gregordc355712011-02-25 00:36:19 +00003745void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3746 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003747 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003748 AddTypeRef(QualType(), Record);
3749 return;
3750 }
3751
Douglas Gregordc355712011-02-25 00:36:19 +00003752 AddTypeLoc(TInfo->getTypeLoc(), Record);
3753}
3754
3755void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3756 AddTypeRef(TL.getType(), Record);
3757
John McCalla1ee0c52009-10-16 21:56:05 +00003758 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003759 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003760 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003761}
3762
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003763void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003764 Record.push_back(GetOrCreateTypeID(T));
3765}
3766
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003767TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3768 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003769 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3770}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003771
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003772TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003773 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003774 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003775}
3776
3777TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3778 if (T.isNull())
3779 return TypeIdx();
3780 assert(!T.getLocalFastQualifiers());
3781
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003782 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003783 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003784 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003785 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003786 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003787 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003788 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003789 return Idx;
3790}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003791
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003792TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003793 if (T.isNull())
3794 return TypeIdx();
3795 assert(!T.getLocalFastQualifiers());
3796
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003797 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3798 assert(I != TypeIdxs.end() && "Type not emitted!");
3799 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003800}
3801
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003802void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003803 Record.push_back(GetDeclRef(D));
3804}
3805
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003806DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003807 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3808
Douglas Gregor2cf26342009-04-09 22:27:44 +00003809 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003810 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003811 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003812
3813 // If D comes from an AST file, its declaration ID is already known and
3814 // fixed.
3815 if (D->isFromASTFile())
3816 return D->getGlobalID();
3817
Douglas Gregor97475832010-10-05 18:37:06 +00003818 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003819 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003820 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003821 // We haven't seen this declaration before. Give it a new ID and
3822 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003823 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003824 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003825 }
3826
Sebastian Redl681d7232010-07-27 00:17:23 +00003827 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003828}
3829
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003830DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003831 if (D == 0)
3832 return 0;
3833
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003834 // If D comes from an AST file, its declaration ID is already known and
3835 // fixed.
3836 if (D->isFromASTFile())
3837 return D->getGlobalID();
3838
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003839 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3840 return DeclIDs[D];
3841}
3842
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003843static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3844 std::pair<unsigned, serialization::DeclID> R) {
3845 return L.first < R.first;
3846}
3847
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003848void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003849 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003850 assert(D);
3851
3852 SourceLocation Loc = D->getLocation();
3853 if (Loc.isInvalid())
3854 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003855
3856 // We only keep track of the file-level declarations of each file.
3857 if (!D->getLexicalDeclContext()->isFileContext())
3858 return;
3859
3860 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003861 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003862 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003863 FileID FID;
3864 unsigned Offset;
3865 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003866 if (FID.isInvalid())
3867 return;
3868 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3869 assert(Entry->isFile());
3870
3871 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3872 if (!Info)
3873 Info = new DeclIDInFileInfo();
3874
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003875 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003876 LocDeclIDsTy &Decls = Info->DeclIDs;
3877
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003878 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003879 Decls.push_back(LocDecl);
3880 return;
3881 }
3882
3883 LocDeclIDsTy::iterator
3884 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3885
3886 Decls.insert(I, LocDecl);
3887}
3888
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003889void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003890 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003891 Record.push_back(Name.getNameKind());
3892 switch (Name.getNameKind()) {
3893 case DeclarationName::Identifier:
3894 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3895 break;
3896
3897 case DeclarationName::ObjCZeroArgSelector:
3898 case DeclarationName::ObjCOneArgSelector:
3899 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003900 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003901 break;
3902
3903 case DeclarationName::CXXConstructorName:
3904 case DeclarationName::CXXDestructorName:
3905 case DeclarationName::CXXConversionFunctionName:
3906 AddTypeRef(Name.getCXXNameType(), Record);
3907 break;
3908
3909 case DeclarationName::CXXOperatorName:
3910 Record.push_back(Name.getCXXOverloadedOperator());
3911 break;
3912
Sean Hunt3e518bd2009-11-29 07:34:05 +00003913 case DeclarationName::CXXLiteralOperatorName:
3914 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3915 break;
3916
Douglas Gregor2cf26342009-04-09 22:27:44 +00003917 case DeclarationName::CXXUsingDirective:
3918 // No extra data to emit
3919 break;
3920 }
3921}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003922
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003923void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003924 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003925 switch (Name.getNameKind()) {
3926 case DeclarationName::CXXConstructorName:
3927 case DeclarationName::CXXDestructorName:
3928 case DeclarationName::CXXConversionFunctionName:
3929 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3930 break;
3931
3932 case DeclarationName::CXXOperatorName:
3933 AddSourceLocation(
3934 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3935 Record);
3936 AddSourceLocation(
3937 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3938 Record);
3939 break;
3940
3941 case DeclarationName::CXXLiteralOperatorName:
3942 AddSourceLocation(
3943 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3944 Record);
3945 break;
3946
3947 case DeclarationName::Identifier:
3948 case DeclarationName::ObjCZeroArgSelector:
3949 case DeclarationName::ObjCOneArgSelector:
3950 case DeclarationName::ObjCMultiArgSelector:
3951 case DeclarationName::CXXUsingDirective:
3952 break;
3953 }
3954}
3955
3956void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003957 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003958 AddDeclarationName(NameInfo.getName(), Record);
3959 AddSourceLocation(NameInfo.getLoc(), Record);
3960 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3961}
3962
3963void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003964 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003965 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003966 Record.push_back(Info.NumTemplParamLists);
3967 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3968 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3969}
3970
Sebastian Redla4232eb2010-08-18 23:56:21 +00003971void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003972 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003973 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003974 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003975 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003976
3977 // Push each of the NNS's onto a stack for serialization in reverse order.
3978 while (NNS) {
3979 NestedNames.push_back(NNS);
3980 NNS = NNS->getPrefix();
3981 }
3982
3983 Record.push_back(NestedNames.size());
3984 while(!NestedNames.empty()) {
3985 NNS = NestedNames.pop_back_val();
3986 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3987 Record.push_back(Kind);
3988 switch (Kind) {
3989 case NestedNameSpecifier::Identifier:
3990 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3991 break;
3992
3993 case NestedNameSpecifier::Namespace:
3994 AddDeclRef(NNS->getAsNamespace(), Record);
3995 break;
3996
Douglas Gregor14aba762011-02-24 02:36:08 +00003997 case NestedNameSpecifier::NamespaceAlias:
3998 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3999 break;
4000
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004001 case NestedNameSpecifier::TypeSpec:
4002 case NestedNameSpecifier::TypeSpecWithTemplate:
4003 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4004 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4005 break;
4006
4007 case NestedNameSpecifier::Global:
4008 // Don't need to write an associated value.
4009 break;
4010 }
4011 }
4012}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004013
Douglas Gregordc355712011-02-25 00:36:19 +00004014void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4015 RecordDataImpl &Record) {
4016 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004017 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004018 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004019
4020 // Push each of the nested-name-specifiers's onto a stack for
4021 // serialization in reverse order.
4022 while (NNS) {
4023 NestedNames.push_back(NNS);
4024 NNS = NNS.getPrefix();
4025 }
4026
4027 Record.push_back(NestedNames.size());
4028 while(!NestedNames.empty()) {
4029 NNS = NestedNames.pop_back_val();
4030 NestedNameSpecifier::SpecifierKind Kind
4031 = NNS.getNestedNameSpecifier()->getKind();
4032 Record.push_back(Kind);
4033 switch (Kind) {
4034 case NestedNameSpecifier::Identifier:
4035 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4036 AddSourceRange(NNS.getLocalSourceRange(), Record);
4037 break;
4038
4039 case NestedNameSpecifier::Namespace:
4040 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4041 AddSourceRange(NNS.getLocalSourceRange(), Record);
4042 break;
4043
4044 case NestedNameSpecifier::NamespaceAlias:
4045 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4046 AddSourceRange(NNS.getLocalSourceRange(), Record);
4047 break;
4048
4049 case NestedNameSpecifier::TypeSpec:
4050 case NestedNameSpecifier::TypeSpecWithTemplate:
4051 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4052 AddTypeLoc(NNS.getTypeLoc(), Record);
4053 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4054 break;
4055
4056 case NestedNameSpecifier::Global:
4057 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4058 break;
4059 }
4060 }
4061}
4062
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004063void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004064 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004065 Record.push_back(Kind);
4066 switch (Kind) {
4067 case TemplateName::Template:
4068 AddDeclRef(Name.getAsTemplateDecl(), Record);
4069 break;
4070
4071 case TemplateName::OverloadedTemplate: {
4072 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4073 Record.push_back(OvT->size());
4074 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4075 I != E; ++I)
4076 AddDeclRef(*I, Record);
4077 break;
4078 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004079
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004080 case TemplateName::QualifiedTemplate: {
4081 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4082 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4083 Record.push_back(QualT->hasTemplateKeyword());
4084 AddDeclRef(QualT->getTemplateDecl(), Record);
4085 break;
4086 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004087
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004088 case TemplateName::DependentTemplate: {
4089 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4090 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4091 Record.push_back(DepT->isIdentifier());
4092 if (DepT->isIdentifier())
4093 AddIdentifierRef(DepT->getIdentifier(), Record);
4094 else
4095 Record.push_back(DepT->getOperator());
4096 break;
4097 }
John McCall14606042011-06-30 08:33:18 +00004098
4099 case TemplateName::SubstTemplateTemplateParm: {
4100 SubstTemplateTemplateParmStorage *subst
4101 = Name.getAsSubstTemplateTemplateParm();
4102 AddDeclRef(subst->getParameter(), Record);
4103 AddTemplateName(subst->getReplacement(), Record);
4104 break;
4105 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004106
4107 case TemplateName::SubstTemplateTemplateParmPack: {
4108 SubstTemplateTemplateParmPackStorage *SubstPack
4109 = Name.getAsSubstTemplateTemplateParmPack();
4110 AddDeclRef(SubstPack->getParameterPack(), Record);
4111 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4112 break;
4113 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004114 }
4115}
4116
Michael J. Spencer20249a12010-10-21 03:16:25 +00004117void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004118 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004119 Record.push_back(Arg.getKind());
4120 switch (Arg.getKind()) {
4121 case TemplateArgument::Null:
4122 break;
4123 case TemplateArgument::Type:
4124 AddTypeRef(Arg.getAsType(), Record);
4125 break;
4126 case TemplateArgument::Declaration:
4127 AddDeclRef(Arg.getAsDecl(), Record);
4128 break;
4129 case TemplateArgument::Integral:
4130 AddAPSInt(*Arg.getAsIntegral(), Record);
4131 AddTypeRef(Arg.getIntegralType(), Record);
4132 break;
4133 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004134 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4135 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004136 case TemplateArgument::TemplateExpansion:
4137 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004138 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4139 Record.push_back(*NumExpansions + 1);
4140 else
4141 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004142 break;
4143 case TemplateArgument::Expression:
4144 AddStmt(Arg.getAsExpr());
4145 break;
4146 case TemplateArgument::Pack:
4147 Record.push_back(Arg.pack_size());
4148 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4149 I != E; ++I)
4150 AddTemplateArgument(*I, Record);
4151 break;
4152 }
4153}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004154
4155void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004156ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004157 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004158 assert(TemplateParams && "No TemplateParams!");
4159 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4160 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4161 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4162 Record.push_back(TemplateParams->size());
4163 for (TemplateParameterList::const_iterator
4164 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4165 P != PEnd; ++P)
4166 AddDeclRef(*P, Record);
4167}
4168
4169/// \brief Emit a template argument list.
4170void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004171ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004172 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004173 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004174 Record.push_back(TemplateArgs->size());
4175 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004176 AddTemplateArgument(TemplateArgs->get(i), Record);
4177}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004178
4179
4180void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004181ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004182 Record.push_back(Set.size());
4183 for (UnresolvedSetImpl::const_iterator
4184 I = Set.begin(), E = Set.end(); I != E; ++I) {
4185 AddDeclRef(I.getDecl(), Record);
4186 Record.push_back(I.getAccess());
4187 }
4188}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004189
Sebastian Redla4232eb2010-08-18 23:56:21 +00004190void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004191 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004192 Record.push_back(Base.isVirtual());
4193 Record.push_back(Base.isBaseOfClass());
4194 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004195 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004196 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004197 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004198 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4199 : SourceLocation(),
4200 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004201}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004202
Douglas Gregor7c789c12010-10-29 22:39:52 +00004203void ASTWriter::FlushCXXBaseSpecifiers() {
4204 RecordData Record;
4205 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4206 Record.clear();
4207
4208 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004209 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004210 if (Index == CXXBaseSpecifiersOffsets.size())
4211 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4212 else {
4213 if (Index > CXXBaseSpecifiersOffsets.size())
4214 CXXBaseSpecifiersOffsets.resize(Index + 1);
4215 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4216 }
4217
4218 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4219 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4220 Record.push_back(BEnd - B);
4221 for (; B != BEnd; ++B)
4222 AddCXXBaseSpecifier(*B, Record);
4223 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004224
4225 // Flush any expressions that were written as part of the base specifiers.
4226 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004227 }
4228
4229 CXXBaseSpecifiersToWrite.clear();
4230}
4231
Sean Huntcbb67482011-01-08 20:30:50 +00004232void ASTWriter::AddCXXCtorInitializers(
4233 const CXXCtorInitializer * const *CtorInitializers,
4234 unsigned NumCtorInitializers,
4235 RecordDataImpl &Record) {
4236 Record.push_back(NumCtorInitializers);
4237 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4238 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004239
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004240 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004241 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004242 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004243 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004244 } else if (Init->isDelegatingInitializer()) {
4245 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004246 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004247 } else if (Init->isMemberInitializer()){
4248 Record.push_back(CTOR_INITIALIZER_MEMBER);
4249 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004250 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004251 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4252 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004253 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004254
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004255 AddSourceLocation(Init->getMemberLocation(), Record);
4256 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004257 AddSourceLocation(Init->getLParenLoc(), Record);
4258 AddSourceLocation(Init->getRParenLoc(), Record);
4259 Record.push_back(Init->isWritten());
4260 if (Init->isWritten()) {
4261 Record.push_back(Init->getSourceOrder());
4262 } else {
4263 Record.push_back(Init->getNumArrayIndices());
4264 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4265 AddDeclRef(Init->getArrayIndex(i), Record);
4266 }
4267 }
4268}
4269
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004270void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4271 assert(D->DefinitionData);
4272 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
4273 Record.push_back(Data.UserDeclaredConstructor);
4274 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004275 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004276 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004277 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004278 Record.push_back(Data.UserDeclaredDestructor);
4279 Record.push_back(Data.Aggregate);
4280 Record.push_back(Data.PlainOldData);
4281 Record.push_back(Data.Empty);
4282 Record.push_back(Data.Polymorphic);
4283 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004284 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004285 Record.push_back(Data.HasNoNonEmptyBases);
4286 Record.push_back(Data.HasPrivateFields);
4287 Record.push_back(Data.HasProtectedFields);
4288 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004289 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00004290 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004291 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004292 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004293 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004294 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004295 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004296 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004297 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004298 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004299 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004300 Record.push_back(Data.DeclaredDefaultConstructor);
4301 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004302 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004303 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004304 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004305 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004306 Record.push_back(Data.FailedImplicitMoveConstructor);
4307 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004308
4309 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004310 if (Data.NumBases > 0)
4311 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4312 Record);
4313
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004314 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4315 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004316 if (Data.NumVBases > 0)
4317 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4318 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004319
4320 AddUnresolvedSet(Data.Conversions, Record);
4321 AddUnresolvedSet(Data.VisibleConversions, Record);
4322 // Data.Definition is the owning decl, no need to write it.
4323 AddDeclRef(Data.FirstFriend, Record);
4324}
4325
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004326void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004327 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004328 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004329 assert(FirstDeclID == NextDeclID &&
4330 FirstTypeID == NextTypeID &&
4331 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004332 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004333 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004334 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004335
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004336 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004337
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004338 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4339 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4340 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004341 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004342 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004343 NextDeclID = FirstDeclID;
4344 NextTypeID = FirstTypeID;
4345 NextIdentID = FirstIdentID;
4346 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004347 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004348}
4349
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004350void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004351 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004352 if (II->hasMacroDefinition())
4353 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004354}
4355
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004356void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004357 // Always take the highest-numbered type index. This copes with an interesting
4358 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004359 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004360 // keep the higher-numbered entry so that we can properly write it out to
4361 // the AST file.
4362 TypeIdx &StoredIdx = TypeIdxs[T];
4363 if (Idx.getIndex() >= StoredIdx.getIndex())
4364 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004365}
4366
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004367void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004368 SelectorIDs[S] = ID;
4369}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004370
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004371void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004372 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004373 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004374 MacroDefinitions[MD] = ID;
4375}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004376
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004377void ASTWriter::MacroVisible(IdentifierInfo *II) {
4378 DeserializedMacroNames.push_back(II);
4379}
4380
Douglas Gregora015cab2011-12-02 17:30:13 +00004381void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4382 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4383 SubmoduleIDs[Mod] = ID;
4384}
4385
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004386void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004387 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004388 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004389 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4390 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004391 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004392 // A forward reference was mutated into a definition. Rewrite it.
4393 // FIXME: This happens during template instantiation, should we
4394 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004395 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004396 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004397 }
4398}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004399void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004400 assert(!WritingAST && "Already writing the AST!");
4401
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004402 // TU and namespaces are handled elsewhere.
4403 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4404 return;
4405
Douglas Gregor919814d2011-09-09 23:01:35 +00004406 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004407 return; // Not a source decl added to a DeclContext from PCH.
4408
4409 AddUpdatedDeclContext(DC);
4410}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004411
4412void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004413 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004414 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004415 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004416 return; // Not a source member added to a class from PCH.
4417 if (!isa<CXXMethodDecl>(D))
4418 return; // We are interested in lazily declared implicit methods.
4419
4420 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004421 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004422 UpdateRecord &Record = DeclUpdates[RD];
4423 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004424 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004425}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004426
4427void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4428 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004429 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004430 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004431 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004432 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004433 return; // Not a source specialization added to a template from PCH.
4434
4435 UpdateRecord &Record = DeclUpdates[TD];
4436 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004437 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004438}
Douglas Gregor89d99802010-11-30 06:16:57 +00004439
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004440void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4441 const FunctionDecl *D) {
4442 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004443 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004444 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004445 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004446 return; // Not a source specialization added to a template from PCH.
4447
4448 UpdateRecord &Record = DeclUpdates[TD];
4449 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004450 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004451}
4452
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004453void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004454 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004455 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004456 return; // Declaration not imported from PCH.
4457
4458 // Implicit decl from a PCH was defined.
4459 // FIXME: Should implicit definition be a separate FunctionDecl?
4460 RewriteDecl(D);
4461}
4462
Sebastian Redlf79a7192011-04-29 08:19:30 +00004463void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004464 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004465 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004466 return;
4467
4468 // Since the actual instantiation is delayed, this really means that we need
4469 // to update the instantiation location.
4470 UpdateRecord &Record = DeclUpdates[D];
4471 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4472 AddSourceLocation(
4473 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4474}
4475
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004476void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4477 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004478 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004479 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004480 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004481
4482 assert(IFD->getDefinition() && "Category on a class without a definition?");
4483 ObjCClassesWithCategories.insert(
4484 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004485}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004486
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004487
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004488void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4489 const ObjCPropertyDecl *OrigProp,
4490 const ObjCCategoryDecl *ClassExt) {
4491 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4492 if (!D)
4493 return;
4494
4495 assert(!WritingAST && "Already writing the AST!");
4496 if (!D->isFromASTFile())
4497 return; // Declaration not imported from PCH.
4498
4499 RewriteDecl(D);
4500}
4501