blob: 5e2eb59ae5ea33432c077e1aecb2ca78ef9ded6b [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregor89d99802010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
Sebastian Redlade50002010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000056static StringRef data(const std::vector<T, Allocator> &v) {
57 if (v.empty()) return StringRef();
58 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000059 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000060}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000061
62template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000063static StringRef data(const SmallVectorImpl<T> &v) {
64 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000066}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000071
Douglas Gregor2cf26342009-04-09 22:27:44 +000072namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000080
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
84 void VisitArrayType(const ArrayType *T);
85 void VisitFunctionType(const FunctionType *T);
86 void VisitTagType(const TagType *T);
87
88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
89#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl3397c552010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000095 assert(false && "Built-in types are never serialized");
96}
97
Sebastian Redl3397c552010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101}
102
Sebastian Redl3397c552010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106}
107
Sebastian Redl3397c552010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111}
112
Sebastian Redl3397c552010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117}
118
Sebastian Redl3397c552010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000122}
123
Sebastian Redl3397c552010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128}
129
Sebastian Redl3397c552010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134}
135
Sebastian Redl3397c552010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140}
141
Sebastian Redl3397c552010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145}
146
Sebastian Redl3397c552010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153}
154
Sebastian Redl3397c552010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160}
161
Sebastian Redl3397c552010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176}
177
Sebastian Redl3397c552010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181}
182
Sebastian Redl3397c552010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184 VisitFunctionType(T);
185 Record.push_back(T->getNumArgs());
186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
187 Writer.AddTypeRef(T->getArgType(I), Record);
188 Record.push_back(T->isVariadic());
189 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
198 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000199 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200}
201
Sebastian Redl3397c552010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000203 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000204 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000205}
John McCalled976492009-12-04 22:46:56 +0000206
Sebastian Redl3397c552010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000211 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000212}
213
Sebastian Redl3397c552010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000215 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000216 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217}
218
Sebastian Redl3397c552010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222}
223
Sebastian Redl3397c552010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000225 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000227}
228
Sean Huntca63c202011-05-24 22:41:36 +0000229void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
230 Writer.AddTypeRef(T->getBaseType(), Record);
231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
232 Record.push_back(T->getUTTKind());
233 Code = TYPE_UNARY_TRANSFORM;
234}
235
Richard Smith34b41d92011-02-20 03:19:35 +0000236void ASTTypeWriter::VisitAutoType(const AutoType *T) {
237 Writer.AddTypeRef(T->getDeducedType(), Record);
238 Code = TYPE_AUTO;
239}
240
Sebastian Redl3397c552010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000242 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000243 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000244 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000245 "Cannot serialize in the middle of a type definition");
246}
247
Sebastian Redl3397c552010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000250 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251}
252
Sebastian Redl3397c552010-08-18 23:56:27 +0000253void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000255 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256}
257
John McCall9d156a72011-01-06 01:58:22 +0000258void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
259 Writer.AddTypeRef(T->getModifiedType(), Record);
260 Writer.AddTypeRef(T->getEquivalentType(), Record);
261 Record.push_back(T->getAttrKind());
262 Code = TYPE_ATTRIBUTED;
263}
264
Mike Stump1eb44332009-09-09 15:08:12 +0000265void
Sebastian Redl3397c552010-08-18 23:56:27 +0000266ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000267 const SubstTemplateTypeParmType *T) {
268 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
269 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000270 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000271}
272
273void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000274ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
275 const SubstTemplateTypeParmPackType *T) {
276 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
277 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
278 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
279}
280
281void
Sebastian Redl3397c552010-08-18 23:56:27 +0000282ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000283 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000284 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000285 Writer.AddTemplateName(T->getTemplateName(), Record);
286 Record.push_back(T->getNumArgs());
287 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
288 ArgI != ArgE; ++ArgI)
289 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000290 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
291 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000292 : T->getCanonicalTypeInternal(),
293 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000294 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000295}
296
297void
Sebastian Redl3397c552010-08-18 23:56:27 +0000298ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000299 VisitArrayType(T);
300 Writer.AddStmt(T->getSizeExpr());
301 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl3397c552010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000307 const DependentSizedExtVectorType *T) {
308 // FIXME: Serialize this type (C++ only)
309 assert(false && "Cannot serialize dependent sized extended vector types");
310}
311
312void
Sebastian Redl3397c552010-08-18 23:56:27 +0000313ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314 Record.push_back(T->getDepth());
315 Record.push_back(T->getIndex());
316 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000317 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000318 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl3397c552010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000323 Record.push_back(T->getKeyword());
324 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
325 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000326 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
327 : T->getCanonicalTypeInternal(),
328 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000329 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000330}
331
332void
Sebastian Redl3397c552010-08-18 23:56:27 +0000333ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000334 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000335 Record.push_back(T->getKeyword());
336 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
337 Writer.AddIdentifierRef(T->getIdentifier(), Record);
338 Record.push_back(T->getNumArgs());
339 for (DependentTemplateSpecializationType::iterator
340 I = T->begin(), E = T->end(); I != E; ++I)
341 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000342 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000343}
344
Douglas Gregor7536dd52010-12-20 02:24:11 +0000345void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
346 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000347 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
348 Record.push_back(*NumExpansions + 1);
349 else
350 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000351 Code = TYPE_PACK_EXPANSION;
352}
353
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000354void ASTTypeWriter::VisitParenType(const ParenType *T) {
355 Writer.AddTypeRef(T->getInnerType(), Record);
356 Code = TYPE_PAREN;
357}
358
Sebastian Redl3397c552010-08-18 23:56:27 +0000359void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000360 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000361 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
362 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000363 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000364}
365
Sebastian Redl3397c552010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000367 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000368 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000369 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000370}
371
Sebastian Redl3397c552010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000373 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000374 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000375}
376
Sebastian Redl3397c552010-08-18 23:56:27 +0000377void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000378 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000379 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000380 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000381 E = T->qual_end(); I != E; ++I)
382 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000384}
385
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000386void
Sebastian Redl3397c552010-08-18 23:56:27 +0000387ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000390}
391
John McCalla1ee0c52009-10-16 21:56:05 +0000392namespace {
393
394class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000395 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000396 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000397
398public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000399 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000400 : Writer(Writer), Record(Record) { }
401
John McCall51bd8032009-10-18 01:05:36 +0000402#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000403#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000404 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000405#include "clang/AST/TypeLocNodes.def"
406
John McCall51bd8032009-10-18 01:05:36 +0000407 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
408 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000409};
410
411}
412
John McCall51bd8032009-10-18 01:05:36 +0000413void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
414 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000415}
John McCall51bd8032009-10-18 01:05:36 +0000416void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000417 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
418 if (TL.needsExtraLocalData()) {
419 Record.push_back(TL.getWrittenTypeSpec());
420 Record.push_back(TL.getWrittenSignSpec());
421 Record.push_back(TL.getWrittenWidthSpec());
422 Record.push_back(TL.hasModeAttr());
423 }
John McCalla1ee0c52009-10-16 21:56:05 +0000424}
John McCall51bd8032009-10-18 01:05:36 +0000425void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000430}
John McCall51bd8032009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000433}
John McCall51bd8032009-10-18 01:05:36 +0000434void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000436}
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000442 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
446 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
447 Record.push_back(TL.getSizeExpr() ? 1 : 0);
448 if (TL.getSizeExpr())
449 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000450}
John McCall51bd8032009-10-18 01:05:36 +0000451void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
452 VisitArrayTypeLoc(TL);
453}
454void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
455 VisitArrayTypeLoc(TL);
456}
457void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
458 VisitArrayTypeLoc(TL);
459}
460void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
461 DependentSizedArrayTypeLoc TL) {
462 VisitArrayTypeLoc(TL);
463}
464void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
465 DependentSizedExtVectorTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
468void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
474void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000475 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
476 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000477 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000478 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
479 Writer.AddDeclRef(TL.getArg(i), Record);
480}
481void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
482 VisitFunctionTypeLoc(TL);
483}
484void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
485 VisitFunctionTypeLoc(TL);
486}
John McCalled976492009-12-04 22:46:56 +0000487void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
John McCall51bd8032009-10-18 01:05:36 +0000490void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getNameLoc(), Record);
492}
493void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000494 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
495 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
496 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000497}
498void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc 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);
502 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000503}
504void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
Sean Huntca63c202011-05-24 22:41:36 +0000507void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getKWLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
511 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
512}
Richard Smith34b41d92011-02-20 03:19:35 +0000513void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
John McCall51bd8032009-10-18 01:05:36 +0000516void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
519void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getNameLoc(), Record);
521}
John McCall9d156a72011-01-06 01:58:22 +0000522void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
523 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
524 if (TL.hasAttrOperand()) {
525 SourceRange range = TL.getAttrOperandParensRange();
526 Writer.AddSourceLocation(range.getBegin(), Record);
527 Writer.AddSourceLocation(range.getEnd(), Record);
528 }
529 if (TL.hasAttrExprOperand()) {
530 Expr *operand = TL.getAttrExprOperand();
531 Record.push_back(operand ? 1 : 0);
532 if (operand) Writer.AddStmt(operand);
533 } else if (TL.hasAttrEnumOperand()) {
534 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
535 }
536}
John McCall51bd8032009-10-18 01:05:36 +0000537void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
John McCall49a832b2009-10-18 09:09:24 +0000540void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
541 SubstTemplateTypeParmTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000544void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
545 SubstTemplateTypeParmPackTypeLoc TL) {
546 Writer.AddSourceLocation(TL.getNameLoc(), Record);
547}
John McCall51bd8032009-10-18 01:05:36 +0000548void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
549 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000550 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
551 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
552 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
553 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000554 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
555 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000556}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000557void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
559 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
560}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000561void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000562 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000563 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000564}
John McCall3cb0ebd2010-03-10 03:28:59 +0000565void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
567}
Douglas Gregor4714c122010-03-31 17:34:00 +0000568void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000569 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000570 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCall33500952010-06-11 00:33:02 +0000573void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
574 DependentTemplateSpecializationTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000576 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
579 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
580 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000581 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
582 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000583}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000584void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
586}
John McCall51bd8032009-10-18 01:05:36 +0000587void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000589}
590void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
591 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000592 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
593 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
594 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
595 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000596}
John McCall54e14c42009-10-22 22:37:11 +0000597void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000599}
John McCalla1ee0c52009-10-16 21:56:05 +0000600
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000601//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000602// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000603//===----------------------------------------------------------------------===//
604
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000605static void EmitBlockID(unsigned ID, const char *Name,
606 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000607 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000608 Record.clear();
609 Record.push_back(ID);
610 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
611
612 // Emit the block name if present.
613 if (Name == 0 || Name[0] == 0) return;
614 Record.clear();
615 while (*Name)
616 Record.push_back(*Name++);
617 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
618}
619
620static void EmitRecordID(unsigned ID, const char *Name,
621 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000622 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000623 Record.clear();
624 Record.push_back(ID);
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000628}
629
630static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000632#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000633 RECORD(STMT_STOP);
634 RECORD(STMT_NULL_PTR);
635 RECORD(STMT_NULL);
636 RECORD(STMT_COMPOUND);
637 RECORD(STMT_CASE);
638 RECORD(STMT_DEFAULT);
639 RECORD(STMT_LABEL);
640 RECORD(STMT_IF);
641 RECORD(STMT_SWITCH);
642 RECORD(STMT_WHILE);
643 RECORD(STMT_DO);
644 RECORD(STMT_FOR);
645 RECORD(STMT_GOTO);
646 RECORD(STMT_INDIRECT_GOTO);
647 RECORD(STMT_CONTINUE);
648 RECORD(STMT_BREAK);
649 RECORD(STMT_RETURN);
650 RECORD(STMT_DECL);
651 RECORD(STMT_ASM);
652 RECORD(EXPR_PREDEFINED);
653 RECORD(EXPR_DECL_REF);
654 RECORD(EXPR_INTEGER_LITERAL);
655 RECORD(EXPR_FLOATING_LITERAL);
656 RECORD(EXPR_IMAGINARY_LITERAL);
657 RECORD(EXPR_STRING_LITERAL);
658 RECORD(EXPR_CHARACTER_LITERAL);
659 RECORD(EXPR_PAREN);
660 RECORD(EXPR_UNARY_OPERATOR);
661 RECORD(EXPR_SIZEOF_ALIGN_OF);
662 RECORD(EXPR_ARRAY_SUBSCRIPT);
663 RECORD(EXPR_CALL);
664 RECORD(EXPR_MEMBER);
665 RECORD(EXPR_BINARY_OPERATOR);
666 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
667 RECORD(EXPR_CONDITIONAL_OPERATOR);
668 RECORD(EXPR_IMPLICIT_CAST);
669 RECORD(EXPR_CSTYLE_CAST);
670 RECORD(EXPR_COMPOUND_LITERAL);
671 RECORD(EXPR_EXT_VECTOR_ELEMENT);
672 RECORD(EXPR_INIT_LIST);
673 RECORD(EXPR_DESIGNATED_INIT);
674 RECORD(EXPR_IMPLICIT_VALUE_INIT);
675 RECORD(EXPR_VA_ARG);
676 RECORD(EXPR_ADDR_LABEL);
677 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000678 RECORD(EXPR_CHOOSE);
679 RECORD(EXPR_GNU_NULL);
680 RECORD(EXPR_SHUFFLE_VECTOR);
681 RECORD(EXPR_BLOCK);
682 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000683 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000684 RECORD(EXPR_OBJC_STRING_LITERAL);
685 RECORD(EXPR_OBJC_ENCODE);
686 RECORD(EXPR_OBJC_SELECTOR_EXPR);
687 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
688 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
689 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
690 RECORD(EXPR_OBJC_KVC_REF_EXPR);
691 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000692 RECORD(STMT_OBJC_FOR_COLLECTION);
693 RECORD(STMT_OBJC_CATCH);
694 RECORD(STMT_OBJC_FINALLY);
695 RECORD(STMT_OBJC_AT_TRY);
696 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
697 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000698 RECORD(EXPR_CXX_OPERATOR_CALL);
699 RECORD(EXPR_CXX_CONSTRUCT);
700 RECORD(EXPR_CXX_STATIC_CAST);
701 RECORD(EXPR_CXX_DYNAMIC_CAST);
702 RECORD(EXPR_CXX_REINTERPRET_CAST);
703 RECORD(EXPR_CXX_CONST_CAST);
704 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
705 RECORD(EXPR_CXX_BOOL_LITERAL);
706 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000707 RECORD(EXPR_CXX_TYPEID_EXPR);
708 RECORD(EXPR_CXX_TYPEID_TYPE);
709 RECORD(EXPR_CXX_UUIDOF_EXPR);
710 RECORD(EXPR_CXX_UUIDOF_TYPE);
711 RECORD(EXPR_CXX_THIS);
712 RECORD(EXPR_CXX_THROW);
713 RECORD(EXPR_CXX_DEFAULT_ARG);
714 RECORD(EXPR_CXX_BIND_TEMPORARY);
715 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
716 RECORD(EXPR_CXX_NEW);
717 RECORD(EXPR_CXX_DELETE);
718 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
719 RECORD(EXPR_EXPR_WITH_CLEANUPS);
720 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
721 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
722 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
723 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
724 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
725 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
726 RECORD(EXPR_CXX_NOEXCEPT);
727 RECORD(EXPR_OPAQUE_VALUE);
728 RECORD(EXPR_BINARY_TYPE_TRAIT);
729 RECORD(EXPR_PACK_EXPANSION);
730 RECORD(EXPR_SIZEOF_PACK);
731 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000732 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000733#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000734}
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Sebastian Redla4232eb2010-08-18 23:56:21 +0000736void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000737 RecordData Record;
738 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000740#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
741#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Sebastian Redl3397c552010-08-18 23:56:27 +0000743 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000744 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000745 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000746 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000747 RECORD(TYPE_OFFSET);
748 RECORD(DECL_OFFSET);
749 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000750 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000751 RECORD(IDENTIFIER_OFFSET);
752 RECORD(IDENTIFIER_TABLE);
753 RECORD(EXTERNAL_DEFINITIONS);
754 RECORD(SPECIAL_TYPES);
755 RECORD(STATISTICS);
756 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000757 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000758 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
759 RECORD(SELECTOR_OFFSETS);
760 RECORD(METHOD_POOL);
761 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000762 RECORD(SOURCE_LOCATION_OFFSETS);
763 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000764 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000765 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000766 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000767 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000768 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000769 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000770 RECORD(TU_UPDATE_LEXICAL);
771 RECORD(REDECLS_UPDATE_LATEST);
772 RECORD(SEMA_DECL_REFS);
773 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
774 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
775 RECORD(DECL_REPLACEMENTS);
776 RECORD(UPDATE_VISIBLE);
777 RECORD(DECL_UPDATE_OFFSETS);
778 RECORD(DECL_UPDATES);
779 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
780 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000781 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000782 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000783 RECORD(FP_PRAGMA_OPTIONS);
784 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000785 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000786 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
787 RECORD(KNOWN_NAMESPACES);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000788
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000789 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000790 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000791 RECORD(SM_SLOC_FILE_ENTRY);
792 RECORD(SM_SLOC_BUFFER_ENTRY);
793 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000794 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000796 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000797 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000798 RECORD(PP_MACRO_OBJECT_LIKE);
799 RECORD(PP_MACRO_FUNCTION_LIKE);
800 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000801
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000802 // Decls and Types block.
803 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000804 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000805 RECORD(TYPE_COMPLEX);
806 RECORD(TYPE_POINTER);
807 RECORD(TYPE_BLOCK_POINTER);
808 RECORD(TYPE_LVALUE_REFERENCE);
809 RECORD(TYPE_RVALUE_REFERENCE);
810 RECORD(TYPE_MEMBER_POINTER);
811 RECORD(TYPE_CONSTANT_ARRAY);
812 RECORD(TYPE_INCOMPLETE_ARRAY);
813 RECORD(TYPE_VARIABLE_ARRAY);
814 RECORD(TYPE_VECTOR);
815 RECORD(TYPE_EXT_VECTOR);
816 RECORD(TYPE_FUNCTION_PROTO);
817 RECORD(TYPE_FUNCTION_NO_PROTO);
818 RECORD(TYPE_TYPEDEF);
819 RECORD(TYPE_TYPEOF_EXPR);
820 RECORD(TYPE_TYPEOF);
821 RECORD(TYPE_RECORD);
822 RECORD(TYPE_ENUM);
823 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000824 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000825 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000826 RECORD(TYPE_DECLTYPE);
827 RECORD(TYPE_ELABORATED);
828 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
829 RECORD(TYPE_UNRESOLVED_USING);
830 RECORD(TYPE_INJECTED_CLASS_NAME);
831 RECORD(TYPE_OBJC_OBJECT);
832 RECORD(TYPE_TEMPLATE_TYPE_PARM);
833 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
834 RECORD(TYPE_DEPENDENT_NAME);
835 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
836 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
837 RECORD(TYPE_PAREN);
838 RECORD(TYPE_PACK_EXPANSION);
839 RECORD(TYPE_ATTRIBUTED);
840 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000841 RECORD(DECL_TRANSLATION_UNIT);
842 RECORD(DECL_TYPEDEF);
843 RECORD(DECL_ENUM);
844 RECORD(DECL_RECORD);
845 RECORD(DECL_ENUM_CONSTANT);
846 RECORD(DECL_FUNCTION);
847 RECORD(DECL_OBJC_METHOD);
848 RECORD(DECL_OBJC_INTERFACE);
849 RECORD(DECL_OBJC_PROTOCOL);
850 RECORD(DECL_OBJC_IVAR);
851 RECORD(DECL_OBJC_AT_DEFS_FIELD);
852 RECORD(DECL_OBJC_CLASS);
853 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
854 RECORD(DECL_OBJC_CATEGORY);
855 RECORD(DECL_OBJC_CATEGORY_IMPL);
856 RECORD(DECL_OBJC_IMPLEMENTATION);
857 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
858 RECORD(DECL_OBJC_PROPERTY);
859 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000860 RECORD(DECL_FIELD);
861 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000862 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000863 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000864 RECORD(DECL_FILE_SCOPE_ASM);
865 RECORD(DECL_BLOCK);
866 RECORD(DECL_CONTEXT_LEXICAL);
867 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000868 RECORD(DECL_NAMESPACE);
869 RECORD(DECL_NAMESPACE_ALIAS);
870 RECORD(DECL_USING);
871 RECORD(DECL_USING_SHADOW);
872 RECORD(DECL_USING_DIRECTIVE);
873 RECORD(DECL_UNRESOLVED_USING_VALUE);
874 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
875 RECORD(DECL_LINKAGE_SPEC);
876 RECORD(DECL_CXX_RECORD);
877 RECORD(DECL_CXX_METHOD);
878 RECORD(DECL_CXX_CONSTRUCTOR);
879 RECORD(DECL_CXX_DESTRUCTOR);
880 RECORD(DECL_CXX_CONVERSION);
881 RECORD(DECL_ACCESS_SPEC);
882 RECORD(DECL_FRIEND);
883 RECORD(DECL_FRIEND_TEMPLATE);
884 RECORD(DECL_CLASS_TEMPLATE);
885 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
886 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
887 RECORD(DECL_FUNCTION_TEMPLATE);
888 RECORD(DECL_TEMPLATE_TYPE_PARM);
889 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
890 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
891 RECORD(DECL_STATIC_ASSERT);
892 RECORD(DECL_CXX_BASE_SPECIFIERS);
893 RECORD(DECL_INDIRECTFIELD);
894 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
895
Douglas Gregora72d8c42011-06-03 02:27:19 +0000896 // Statements and Exprs can occur in the Decls and Types block.
897 AddStmtsExprs(Stream, Record);
898
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000899 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000900 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000901 RECORD(PPD_MACRO_DEFINITION);
902 RECORD(PPD_INCLUSION_DIRECTIVE);
903
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000904#undef RECORD
905#undef BLOCK
906 Stream.ExitBlock();
907}
908
Douglas Gregore650c8c2009-07-07 00:12:59 +0000909/// \brief Adjusts the given filename to only write out the portion of the
910/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000911///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000912/// \param Filename the file name to adjust.
913///
914/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
915/// the returned filename will be adjusted by this system root.
916///
917/// \returns either the original filename (if it needs no adjustment) or the
918/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000919static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000920adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000921 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregor832d6202011-07-22 16:35:34 +0000923 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000924 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Douglas Gregore650c8c2009-07-07 00:12:59 +0000926 // Verify that the filename and the system root have the same prefix.
927 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000928 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000929 if (Filename[Pos] != isysroot[Pos])
930 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Douglas Gregore650c8c2009-07-07 00:12:59 +0000932 // We hit the end of the filename before we hit the end of the system root.
933 if (!Filename[Pos])
934 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregore650c8c2009-07-07 00:12:59 +0000936 // If the file name has a '/' at the current position, skip over the '/'.
937 // We distinguish sysroot-based includes from absolute includes by the
938 // absence of '/' at the beginning of sysroot-based includes.
939 if (Filename[Pos] == '/')
940 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregore650c8c2009-07-07 00:12:59 +0000942 return Filename + Pos;
943}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000944
Sebastian Redl3397c552010-08-18 23:56:27 +0000945/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000946void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000947 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000948 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000949
Douglas Gregore650c8c2009-07-07 00:12:59 +0000950 // Metadata
951 const TargetInfo &Target = Context.Target;
952 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000953 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000954 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000955 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
956 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
959 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000960 // Target triple or chained PCH name
961 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000965 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
966 Record.push_back(VERSION_MAJOR);
967 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 Record.push_back(CLANG_VERSION_MAJOR);
969 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000970 Record.push_back(!isysroot.empty());
Sebastian Redl77f46032010-07-09 21:00:24 +0000971 // FIXME: This writes the absolute path for chained headers.
Jonathan D. Turner48d2c3f2011-07-26 18:21:30 +0000972 const std::string &BlobStr =
973 Chain ? Chain->getFileName() : Target.getTriple().getTriple();
Sebastian Redl77f46032010-07-09 21:00:24 +0000974 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregor31d375f2011-05-06 21:43:30 +0000976 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +0000977 SourceManager &SM = Context.getSourceManager();
978 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
979 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000980 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000981 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
982 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
983
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000984 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000986 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000987
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000988 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000989 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000990 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000991 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000992 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000993 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000994
995 Record.clear();
996 Record.push_back(SM.getMainFileID().getOpaqueValue());
997 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000998 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000999
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001000 // Original PCH directory
1001 if (!OutputFile.empty() && OutputFile != "-") {
1002 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1003 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1005 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1006
1007 llvm::SmallString<128> OutputPath(OutputFile);
1008
1009 llvm::sys::fs::make_absolute(OutputPath);
1010 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1011
1012 RecordData Record;
1013 Record.push_back(ORIGINAL_PCH_DIR);
1014 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1015 }
1016
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001017 // Repository branch/version information.
1018 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001019 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001020 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1021 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001022 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001023 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001024 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1025 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001026}
1027
1028/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001029void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001030 RecordData Record;
1031 Record.push_back(LangOpts.Trigraphs);
1032 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1033 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1034 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1035 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001036 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001037 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1038 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1039 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1040 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00001041 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001042 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001043 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1044 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001045 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1046 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001047 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001049 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1050 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001051 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001052 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001053 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001054 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001055 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001056 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1057 // properties enabled.
Douglas Gregor74da19f2011-06-14 23:20:43 +00001058 Record.push_back(LangOpts.ObjCInferRelatedResultType);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001059 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001061 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001062 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1063 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001064 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001065 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001066 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001067 Record.push_back(LangOpts.CXXExceptions);
1068 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001069
Douglas Gregor6f755502011-02-01 15:15:22 +00001070 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001071 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1072 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1073 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1074
Chris Lattnerea5ce472009-04-27 07:35:58 +00001075 // Whether static initializers are protected by locks.
1076 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001077 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001078 Record.push_back(LangOpts.Blocks); // block extension to C
1079 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1080 // they are unused.
1081 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1082 // (modulo the platform support).
1083
Chris Lattnera4d71452010-06-26 21:25:03 +00001084 Record.push_back(LangOpts.getSignedOverflowBehavior());
1085 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001086
1087 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001088 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001089 // defined.
1090 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1091 // opposed to __DYNAMIC__).
1092 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1093
1094 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1095 // used (instead of C99 semantics).
1096 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00001097 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001098 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1099 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001100 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1101 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001102 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001103 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1104 // to the smallest integer type with
1105 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001106 Record.push_back(LangOpts.getGCMode());
1107 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001108 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001109 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001110 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001111 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001112 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001113 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001114 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001115 Record.push_back(LangOpts.SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00001116 Record.push_back(LangOpts.MRTD);
John McCallf85e1932011-06-15 23:02:42 +00001117 Record.push_back(LangOpts.ObjCAutoRefCount);
1118 Record.push_back(LangOpts.ObjCInferRelatedReturnType);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001119 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001120}
1121
Douglas Gregor14f79002009-04-10 03:52:48 +00001122//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001123// stat cache Serialization
1124//===----------------------------------------------------------------------===//
1125
1126namespace {
1127// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001128class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001129public:
1130 typedef const char * key_type;
1131 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Chris Lattner74e976b2010-11-23 19:28:12 +00001133 typedef struct stat data_type;
1134 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001135
1136 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001137 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
1140 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001141 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001142 data_type_ref Data) {
1143 unsigned StrLen = strlen(path);
1144 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001145 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001146 clang::io::Emit8(Out, DataLen);
1147 return std::make_pair(StrLen + 1, DataLen);
1148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Chris Lattner5f9e2722011-07-23 10:55:15 +00001150 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001151 Out.write(path, KeyLen);
1152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Chris Lattner5f9e2722011-07-23 10:55:15 +00001154 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001155 data_type_ref Data, unsigned DataLen) {
1156 using namespace clang::io;
1157 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Chris Lattner74e976b2010-11-23 19:28:12 +00001159 Emit32(Out, (uint32_t) Data.st_ino);
1160 Emit32(Out, (uint32_t) Data.st_dev);
1161 Emit16(Out, (uint16_t) Data.st_mode);
1162 Emit64(Out, (uint64_t) Data.st_mtime);
1163 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001164
1165 assert(Out.tell() - Start == DataLen && "Wrong data length");
1166 }
1167};
1168} // end anonymous namespace
1169
Sebastian Redl3397c552010-08-18 23:56:27 +00001170/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001171void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001172 // Build the on-disk hash table containing information about every
1173 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001174 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001175 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001176 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001177 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001178 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001179 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001180 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001181 }
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001183 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001184 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001185 uint32_t BucketOffset;
1186 {
1187 llvm::raw_svector_ostream Out(StatCacheData);
1188 // Make sure that no bucket is at offset 0
1189 clang::io::Emit32(Out, 0);
1190 BucketOffset = Generator.Emit(Out);
1191 }
1192
1193 // Create a blob abbreviation
1194 using namespace llvm;
1195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001196 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1200 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1201
1202 // Write the stat cache
1203 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001204 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001205 Record.push_back(BucketOffset);
1206 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001207 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001208}
1209
1210//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001211// Source Manager Serialization
1212//===----------------------------------------------------------------------===//
1213
1214/// \brief Create an abbreviation for the SLocEntry that refers to a
1215/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001216static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001217 using namespace llvm;
1218 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001219 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001224 // FileEntry fields.
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001228 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001229}
1230
1231/// \brief Create an abbreviation for the SLocEntry that refers to a
1232/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001233static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001234 using namespace llvm;
1235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001236 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001242 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001243}
1244
1245/// \brief Create an abbreviation for the SLocEntry that refers to a
1246/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001247static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001248 using namespace llvm;
1249 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001250 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001252 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001253}
1254
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001255/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1256/// expansion.
1257static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001258 using namespace llvm;
1259 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001260 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001266 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001267}
1268
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001269namespace {
1270 // Trait used for the on-disk hash table of header search information.
1271 class HeaderFileInfoTrait {
1272 ASTWriter &Writer;
1273 HeaderSearch &HS;
1274
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001275 // Keep track of the framework names we've used during serialization.
1276 SmallVector<char, 128> FrameworkStringData;
1277 llvm::StringMap<unsigned> FrameworkNameOffset;
1278
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001279 public:
1280 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1281 : Writer(Writer), HS(HS) { }
1282
1283 typedef const char *key_type;
1284 typedef key_type key_type_ref;
1285
1286 typedef HeaderFileInfo data_type;
1287 typedef const data_type &data_type_ref;
1288
1289 static unsigned ComputeHash(const char *path) {
1290 // The hash is based only on the filename portion of the key, so that the
1291 // reader can match based on filenames when symlinking or excess path
1292 // elements ("foo/../", "../") change the form of the name. However,
1293 // complete path is still the key.
1294 return llvm::HashString(llvm::sys::path::filename(path));
1295 }
1296
1297 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001298 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001299 data_type_ref Data) {
1300 unsigned StrLen = strlen(path);
1301 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001302 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001303 clang::io::Emit8(Out, DataLen);
1304 return std::make_pair(StrLen + 1, DataLen);
1305 }
1306
Chris Lattner5f9e2722011-07-23 10:55:15 +00001307 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001308 Out.write(path, KeyLen);
1309 }
1310
Chris Lattner5f9e2722011-07-23 10:55:15 +00001311 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001312 data_type_ref Data, unsigned DataLen) {
1313 using namespace clang::io;
1314 uint64_t Start = Out.tell(); (void)Start;
1315
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001316 unsigned char Flags = (Data.isImport << 5)
1317 | (Data.isPragmaOnce << 4)
1318 | (Data.DirInfo << 2)
1319 | (Data.Resolved << 1)
1320 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001321 Emit8(Out, (uint8_t)Flags);
1322 Emit16(Out, (uint16_t) Data.NumIncludes);
1323
1324 if (!Data.ControllingMacro)
1325 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1326 else
1327 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001328
1329 unsigned Offset = 0;
1330 if (!Data.Framework.empty()) {
1331 // If this header refers into a framework, save the framework name.
1332 llvm::StringMap<unsigned>::iterator Pos
1333 = FrameworkNameOffset.find(Data.Framework);
1334 if (Pos == FrameworkNameOffset.end()) {
1335 Offset = FrameworkStringData.size() + 1;
1336 FrameworkStringData.append(Data.Framework.begin(),
1337 Data.Framework.end());
1338 FrameworkStringData.push_back(0);
1339
1340 FrameworkNameOffset[Data.Framework] = Offset;
1341 } else
1342 Offset = Pos->second;
1343 }
1344 Emit32(Out, Offset);
1345
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001346 assert(Out.tell() - Start == DataLen && "Wrong data length");
1347 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001348
1349 const char *strings_begin() const { return FrameworkStringData.begin(); }
1350 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001351 };
1352} // end anonymous namespace
1353
1354/// \brief Write the header search block for the list of files that
1355///
1356/// \param HS The header search structure to save.
1357///
1358/// \param Chain Whether we're creating a chained AST file.
Douglas Gregor832d6202011-07-22 16:35:34 +00001359void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001360 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001361 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1362
1363 if (FilesByUID.size() > HS.header_file_size())
1364 FilesByUID.resize(HS.header_file_size());
1365
1366 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1367 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001368 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001369 unsigned NumHeaderSearchEntries = 0;
1370 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1371 const FileEntry *File = FilesByUID[UID];
1372 if (!File)
1373 continue;
1374
1375 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1376 if (HFI.External && Chain)
1377 continue;
1378
1379 // Turn the file name into an absolute path, if it isn't already.
1380 const char *Filename = File->getName();
1381 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1382
1383 // If we performed any translation on the file name at all, we need to
1384 // save this string, since the generator will refer to it later.
1385 if (Filename != File->getName()) {
1386 Filename = strdup(Filename);
1387 SavedStrings.push_back(Filename);
1388 }
1389
1390 Generator.insert(Filename, HFI, GeneratorTrait);
1391 ++NumHeaderSearchEntries;
1392 }
1393
1394 // Create the on-disk hash table in a buffer.
1395 llvm::SmallString<4096> TableData;
1396 uint32_t BucketOffset;
1397 {
1398 llvm::raw_svector_ostream Out(TableData);
1399 // Make sure that no bucket is at offset 0
1400 clang::io::Emit32(Out, 0);
1401 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1402 }
1403
1404 // Create a blob abbreviation
1405 using namespace llvm;
1406 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1407 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1412 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1413
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001414 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001415 RecordData Record;
1416 Record.push_back(HEADER_SEARCH_TABLE);
1417 Record.push_back(BucketOffset);
1418 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001419 Record.push_back(TableData.size());
1420 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001421 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1422
1423 // Free all of the strings we had to duplicate.
1424 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1425 free((void*)SavedStrings[I]);
1426}
1427
Douglas Gregor14f79002009-04-10 03:52:48 +00001428/// \brief Writes the block containing the serialized form of the
1429/// source manager.
1430///
1431/// TODO: We should probably use an on-disk hash table (stored in a
1432/// blob), indexed based on the file name, so that we only create
1433/// entries for files that we actually need. In the common case (no
1434/// errors), we probably won't have to create file entries for any of
1435/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001436void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001437 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001438 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001439 RecordData Record;
1440
Chris Lattnerf04ad692009-04-10 17:16:57 +00001441 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001442 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001443
1444 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001445 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1446 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1447 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001448 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001449
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001450 // Write out the source location entry table. We skip the first
1451 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001452 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001453 // Write out the offsets of only source location file entries.
1454 // We will go through them in ASTReader::validateFileEntries().
1455 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001456 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001457 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1458 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001459 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001460 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001461 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001462
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001463 // Record the offset of this source-location entry.
1464 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1465
1466 // Figure out which record code to use.
1467 unsigned Code;
1468 if (SLoc->isFile()) {
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001469 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001470 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001471 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1472 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001473 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001474 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001475 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001476 Record.clear();
1477 Record.push_back(Code);
1478
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001479 // Starting offset of this entry within this module, so skip the dummy.
1480 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001481 if (SLoc->isFile()) {
1482 const SrcMgr::FileInfo &File = SLoc->getFile();
1483 Record.push_back(File.getIncludeLoc().getRawEncoding());
1484 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1485 Record.push_back(File.hasLineDirectives());
1486
1487 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001488 if (Content->OrigEntry) {
1489 assert(Content->OrigEntry == Content->ContentsEntry &&
1490 "Writing to AST an overriden file is not supported");
1491
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001492 // The source location entry is a file. The blob associated
1493 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregor2d52be52010-03-21 22:49:54 +00001495 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001496 Record.push_back(Content->OrigEntry->getSize());
1497 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001498
Douglas Gregore650c8c2009-07-07 00:12:59 +00001499 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001500 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001501 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001502
1503 // Ask the file manager to fixup the relative path for us. This will
1504 // honor the working directory.
1505 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1506
1507 // FIXME: This call to make_absolute shouldn't be necessary, the
1508 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001509 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001510 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Douglas Gregore650c8c2009-07-07 00:12:59 +00001512 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001513 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001514 } else {
1515 // The source location entry is a buffer. The blob associated
1516 // with this entry contains the contents of the buffer.
1517
1518 // We add one to the size so that we capture the trailing NULL
1519 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1520 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001521 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001522 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001524 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001525 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001526 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001527 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001528 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001529 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001530 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001531
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001532 if (strcmp(Name, "<built-in>") == 0) {
1533 PreloadSLocs.push_back(SLocEntryOffsets.size());
1534 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001535 }
1536 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001537 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001538 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001539 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1540 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
1541 Record.push_back(Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001542
1543 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001544 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001545 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001546 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001548 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001549 }
1550 }
1551
Douglas Gregorc9490c02009-04-16 22:23:12 +00001552 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001553
1554 if (SLocEntryOffsets.empty())
1555 return;
1556
Sebastian Redl3397c552010-08-18 23:56:27 +00001557 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001558 // table is used for lazily loading source-location information.
1559 using namespace llvm;
1560 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001561 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1565 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001567 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001568 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001569 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001570 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001571 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001572
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001573 Abbrev = new BitCodeAbbrev();
1574 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1577 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1578
1579 Record.clear();
1580 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1581 Record.push_back(SLocFileEntryOffsets.size());
1582 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1583 data(SLocFileEntryOffsets));
1584
Sebastian Redl3397c552010-08-18 23:56:27 +00001585 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001586 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001587 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001588
1589 // Write the line table. It depends on remapping working, so it must come
1590 // after the source location offsets.
1591 if (SourceMgr.hasLineTable()) {
1592 LineTableInfo &LineTable = SourceMgr.getLineTable();
1593
1594 Record.clear();
1595 // Emit the file names
1596 Record.push_back(LineTable.getNumFilenames());
1597 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1598 // Emit the file name
1599 const char *Filename = LineTable.getFilename(I);
1600 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1601 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1602 Record.push_back(FilenameLen);
1603 if (FilenameLen)
1604 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1605 }
1606
1607 // Emit the line entries
1608 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1609 L != LEnd; ++L) {
1610 // Only emit entries for local files.
1611 if (L->first < 0)
1612 continue;
1613
1614 // Emit the file ID
1615 Record.push_back(L->first);
1616
1617 // Emit the line entries
1618 Record.push_back(L->second.size());
1619 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1620 LEEnd = L->second.end();
1621 LE != LEEnd; ++LE) {
1622 Record.push_back(LE->FileOffset);
1623 Record.push_back(LE->LineNo);
1624 Record.push_back(LE->FilenameID);
1625 Record.push_back((unsigned)LE->FileKind);
1626 Record.push_back(LE->IncludeOffset);
1627 }
1628 }
1629 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1630 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001631}
1632
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001633//===----------------------------------------------------------------------===//
1634// Preprocessor Serialization
1635//===----------------------------------------------------------------------===//
1636
Douglas Gregor9c736102011-02-10 18:20:09 +00001637static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1638 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1639 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1640 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1641 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1642 return X.first->getName().compare(Y.first->getName());
1643}
1644
Chris Lattner0b1fb982009-04-10 17:15:23 +00001645/// \brief Writes the block containing the serialized form of the
1646/// preprocessor.
1647///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001648void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001649 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001650
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001651 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1652 if (PP.getCounterValue() != 0) {
1653 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001654 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001655 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001656 }
1657
1658 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001659 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Sebastian Redl3397c552010-08-18 23:56:27 +00001661 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001662 // FIXME: use diagnostics subsystem for localization etc.
1663 if (PP.SawDateOrTime())
1664 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Douglas Gregorecdcb882010-10-20 22:00:55 +00001666
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001667 // Loop over all the macro definitions that are live at the end of the file,
1668 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001669 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001670
Douglas Gregor9c736102011-02-10 18:20:09 +00001671 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001672 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001673 MacrosToEmit;
1674 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001675 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1676 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001677 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001678 MacroDefinitionsSeen.insert(I->first);
1679 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1680 }
1681
1682 // Sort the set of macro definitions that need to be serialized by the
1683 // name of the macro, to provide a stable ordering.
1684 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1685 &compareMacroDefinitions);
1686
Douglas Gregor040a8042011-02-11 00:26:14 +00001687 // Resolve any identifiers that defined macros at the time they were
1688 // deserialized, adding them to the list of macros to emit (if appropriate).
1689 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1690 IdentifierInfo *Name
1691 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1692 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1693 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1694 }
1695
Douglas Gregor9c736102011-02-10 18:20:09 +00001696 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1697 const IdentifierInfo *Name = MacrosToEmit[I].first;
1698 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001699 if (!MI)
1700 continue;
1701
Sebastian Redl3397c552010-08-18 23:56:27 +00001702 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001703 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001704 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001705
1706 // FIXME: There is a (probably minor) optimization we could do here, if
1707 // the macro comes from the original PCH but the identifier comes from a
1708 // chained PCH, by storing the offset into the original PCH rather than
1709 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001710 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001711 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001712 continue;
1713
Douglas Gregor9c736102011-02-10 18:20:09 +00001714 AddIdentifierRef(Name, Record);
1715 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001716 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1717 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001719 unsigned Code;
1720 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001721 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001722 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001723 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001725 Record.push_back(MI->isC99Varargs());
1726 Record.push_back(MI->isGNUVarargs());
1727 Record.push_back(MI->getNumArgs());
1728 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1729 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001730 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001731 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001732
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001733 // If we have a detailed preprocessing record, record the macro definition
1734 // ID that corresponds to this macro.
1735 if (PPRec)
1736 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001737
Douglas Gregorc9490c02009-04-16 22:23:12 +00001738 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001739 Record.clear();
1740
Chris Lattnerdf961c22009-04-10 18:08:30 +00001741 // Emit the tokens array.
1742 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1743 // Note that we know that the preprocessor does not have any annotation
1744 // tokens in it because they are created by the parser, and thus can't be
1745 // in a macro definition.
1746 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Chris Lattnerdf961c22009-04-10 18:08:30 +00001748 Record.push_back(Tok.getLocation().getRawEncoding());
1749 Record.push_back(Tok.getLength());
1750
Chris Lattnerdf961c22009-04-10 18:08:30 +00001751 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1752 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001753 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001754 // FIXME: Should translate token kind to a stable encoding.
1755 Record.push_back(Tok.getKind());
1756 // FIXME: Should translate token flags to a stable encoding.
1757 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001759 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001760 Record.clear();
1761 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001762 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001763 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001764 Stream.ExitBlock();
1765
1766 if (PPRec)
1767 WritePreprocessorDetail(*PPRec);
1768}
1769
1770void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1771 if (PPRec.begin(Chain) == PPRec.end(Chain))
1772 return;
1773
1774 // Enter the preprocessor block.
1775 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001776
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001777 // If the preprocessor has a preprocessing record, emit it.
1778 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001779 using namespace llvm;
1780
1781 // Set up the abbreviation for
1782 unsigned InclusionAbbrev = 0;
1783 {
1784 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1785 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1786 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1793 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1794 }
1795
Douglas Gregor4c30bb12011-07-21 00:47:40 +00001796 unsigned IndexBase = Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001797 RecordData Record;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001798 uint64_t BitsInChain = Chain? Chain->TotalModulesSizeInBits : 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001799 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1800 EEnd = PPRec.end(Chain);
1801 E != EEnd; ++E) {
1802 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001803
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001804 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1805 // Record this macro definition's location.
1806 MacroID ID = getMacroDefinitionID(MD);
1807
1808 // Don't write the macro definition if it is from another AST file.
1809 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001810 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001811
Douglas Gregor89d99802010-11-30 06:16:57 +00001812 // Notify the serialization listener that we're serializing this entity.
1813 if (SerializationListener)
1814 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001815 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001816
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001817 unsigned Position = ID - FirstMacroID;
1818 if (Position != MacroDefinitionOffsets.size()) {
1819 if (Position > MacroDefinitionOffsets.size())
1820 MacroDefinitionOffsets.resize(Position + 1);
1821
1822 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1823 } else
1824 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001825
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001826 Record.push_back(IndexBase + NumPreprocessingRecords++);
1827 Record.push_back(ID);
1828 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1829 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1830 AddIdentifierRef(MD->getName(), Record);
1831 AddSourceLocation(MD->getLocation(), Record);
1832 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1833 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001834 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001835
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001836 // Notify the serialization listener that we're serializing this entity.
1837 if (SerializationListener)
1838 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001839 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001840
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001841 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001842 Record.push_back(IndexBase + NumPreprocessingRecords++);
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001843 AddSourceLocation(ME->getSourceRange().getBegin(), Record);
1844 AddSourceLocation(ME->getSourceRange().getEnd(), Record);
1845 AddIdentifierRef(ME->getName(), Record);
1846 Record.push_back(getMacroDefinitionID(ME->getDefinition()));
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001847 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001848 continue;
1849 }
1850
1851 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1852 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1853 Record.push_back(IndexBase + NumPreprocessingRecords++);
1854 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1855 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1856 Record.push_back(ID->getFileName().size());
1857 Record.push_back(ID->wasInQuotes());
1858 Record.push_back(static_cast<unsigned>(ID->getKind()));
1859 llvm::SmallString<64> Buffer;
1860 Buffer += ID->getFileName();
1861 Buffer += ID->getFile()->getName();
1862 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1863 continue;
1864 }
1865
1866 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1867 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001868 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001869
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001870 // Write the offsets table for the preprocessing record.
1871 if (NumPreprocessingRecords > 0) {
1872 // Write the offsets table for identifier IDs.
1873 using namespace llvm;
1874 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001875 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1879 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001880
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001881 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001882 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001883 Record.push_back(NumPreprocessingRecords);
1884 Record.push_back(MacroDefinitionOffsets.size());
1885 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001886 data(MacroDefinitionOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001887 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001888}
1889
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001890void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001891 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001892 for (Diagnostic::DiagStatePointsTy::const_iterator
1893 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1894 I != E; ++I) {
1895 const Diagnostic::DiagStatePoint &point = *I;
1896 if (point.Loc.isInvalid())
1897 continue;
1898
1899 Record.push_back(point.Loc.getRawEncoding());
1900 for (Diagnostic::DiagState::iterator
1901 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1902 unsigned diag = I->first, map = I->second;
1903 if (map & 0x10) { // mapping from a diagnostic pragma.
1904 Record.push_back(diag);
1905 Record.push_back(map & 0x7);
1906 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001907 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001908 Record.push_back(-1); // mark the end of the diag/map pairs for this
1909 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001910 }
1911
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001912 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001913 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001914}
1915
Anders Carlssonc8505782011-03-06 18:41:18 +00001916void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1917 if (CXXBaseSpecifiersOffsets.empty())
1918 return;
1919
1920 RecordData Record;
1921
1922 // Create a blob abbreviation for the C++ base specifiers offsets.
1923 using namespace llvm;
1924
1925 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1926 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1929 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1930
Douglas Gregore92b8a12011-08-04 00:01:48 +00001931 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00001932 Record.clear();
1933 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1934 Record.push_back(CXXBaseSpecifiersOffsets.size());
1935 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001936 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00001937}
1938
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001939//===----------------------------------------------------------------------===//
1940// Type Serialization
1941//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001942
Sebastian Redl3397c552010-08-18 23:56:27 +00001943/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001944void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001945 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001946 if (Idx.getIndex() == 0) // we haven't seen this type before.
1947 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001948
Douglas Gregor97475832010-10-05 18:37:06 +00001949 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001950
Douglas Gregor2cf26342009-04-09 22:27:44 +00001951 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001952 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001953 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001954 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001955 else if (TypeOffsets.size() < Index) {
1956 TypeOffsets.resize(Index + 1);
1957 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001958 }
1959
1960 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Douglas Gregor2cf26342009-04-09 22:27:44 +00001962 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001963 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001964
Douglas Gregora4923eb2009-11-16 21:35:15 +00001965 if (T.hasLocalNonFastQualifiers()) {
1966 Qualifiers Qs = T.getLocalQualifiers();
1967 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001968 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001969 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001970 } else {
1971 switch (T->getTypeClass()) {
1972 // For all of the concrete, non-dependent types, call the
1973 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001974#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001975 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001976#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001977#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001978 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001979 }
1980
1981 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001982 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001983
1984 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001985 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001986}
1987
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001988//===----------------------------------------------------------------------===//
1989// Declaration Serialization
1990//===----------------------------------------------------------------------===//
1991
Douglas Gregor2cf26342009-04-09 22:27:44 +00001992/// \brief Write the block containing all of the declaration IDs
1993/// lexically declared within the given DeclContext.
1994///
1995/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1996/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001997uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001998 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001999 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002000 return 0;
2001
Douglas Gregorc9490c02009-04-16 22:23:12 +00002002 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002003 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002004 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002005 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002006 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2007 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002008 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002009
Douglas Gregor25123082009-04-22 22:34:57 +00002010 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002011 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002012 return Offset;
2013}
2014
Sebastian Redla4232eb2010-08-18 23:56:21 +00002015void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002016 using namespace llvm;
2017 RecordData Record;
2018
2019 // Write the type offsets array
2020 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002021 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2025 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2026 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002027 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002028 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002029 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002030 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002031
2032 // Write the declaration offsets array
2033 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002034 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2038 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2039 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002040 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002041 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002042 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002043 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002044}
2045
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002046//===----------------------------------------------------------------------===//
2047// Global Method Pool and Selector Serialization
2048//===----------------------------------------------------------------------===//
2049
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002050namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002051// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002052class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002053 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002054
2055public:
2056 typedef Selector key_type;
2057 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Sebastian Redl5d050072010-08-04 17:20:04 +00002059 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002060 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002061 ObjCMethodList Instance, Factory;
2062 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002063 typedef const data_type& data_type_ref;
2064
Sebastian Redl3397c552010-08-18 23:56:27 +00002065 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002066
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002067 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002068 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002069 }
Mike Stump1eb44332009-09-09 15:08:12 +00002070
2071 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002072 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002073 data_type_ref Methods) {
2074 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2075 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002076 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2077 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002078 Method = Method->Next)
2079 if (Method->Method)
2080 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002081 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002082 Method = Method->Next)
2083 if (Method->Method)
2084 DataLen += 4;
2085 clang::io::Emit16(Out, DataLen);
2086 return std::make_pair(KeyLen, DataLen);
2087 }
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Chris Lattner5f9e2722011-07-23 10:55:15 +00002089 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002090 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002091 assert((Start >> 32) == 0 && "Selector key offset too large");
2092 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002093 unsigned N = Sel.getNumArgs();
2094 clang::io::Emit16(Out, N);
2095 if (N == 0)
2096 N = 1;
2097 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002098 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002099 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2100 }
Mike Stump1eb44332009-09-09 15:08:12 +00002101
Chris Lattner5f9e2722011-07-23 10:55:15 +00002102 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002103 data_type_ref Methods, unsigned DataLen) {
2104 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002105 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002106 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002107 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002108 Method = Method->Next)
2109 if (Method->Method)
2110 ++NumInstanceMethods;
2111
2112 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002113 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002114 Method = Method->Next)
2115 if (Method->Method)
2116 ++NumFactoryMethods;
2117
2118 clang::io::Emit16(Out, NumInstanceMethods);
2119 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002120 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002121 Method = Method->Next)
2122 if (Method->Method)
2123 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002124 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002125 Method = Method->Next)
2126 if (Method->Method)
2127 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002128
2129 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002130 }
2131};
2132} // end anonymous namespace
2133
Sebastian Redl059612d2010-08-03 21:58:15 +00002134/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002135///
2136/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002137/// in an on-disk hash table indexed by the selector. The hash table also
2138/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002139void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002140 using namespace llvm;
2141
Sebastian Redl059612d2010-08-03 21:58:15 +00002142 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002143 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002144 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002145 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002146 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002147 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002148 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002149 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Sebastian Redl059612d2010-08-03 21:58:15 +00002151 // Create the on-disk hash table representation. We walk through every
2152 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002153 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002154 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002155 I = SelectorIDs.begin(), E = SelectorIDs.end();
2156 I != E; ++I) {
2157 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002158 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002159 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002160 I->second,
2161 ObjCMethodList(),
2162 ObjCMethodList()
2163 };
2164 if (F != SemaRef.MethodPool.end()) {
2165 Data.Instance = F->second.first;
2166 Data.Factory = F->second.second;
2167 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002168 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002169 // changed.
2170 if (Chain && I->second < FirstSelectorID) {
2171 // Selector already exists. Did it change?
2172 bool changed = false;
2173 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2174 M = M->Next) {
2175 if (M->Method->getPCHLevel() == 0)
2176 changed = true;
2177 }
2178 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2179 M = M->Next) {
2180 if (M->Method->getPCHLevel() == 0)
2181 changed = true;
2182 }
2183 if (!changed)
2184 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002185 } else if (Data.Instance.Method || Data.Factory.Method) {
2186 // A new method pool entry.
2187 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002188 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002189 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002190 }
2191
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002192 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002193 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002194 uint32_t BucketOffset;
2195 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002196 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002197 llvm::raw_svector_ostream Out(MethodPool);
2198 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002199 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002200 BucketOffset = Generator.Emit(Out, Trait);
2201 }
2202
2203 // Create a blob abbreviation
2204 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002205 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2209 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2210
Douglas Gregor83941df2009-04-25 17:48:32 +00002211 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002212 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002213 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002214 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002215 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002216 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002217
2218 // Create a blob abbreviation for the selector table offsets.
2219 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002220 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2224 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2225
2226 // Write the selector offsets table.
2227 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002228 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002229 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002230 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002231 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002232 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002233 }
2234}
2235
Sebastian Redl3397c552010-08-18 23:56:27 +00002236/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002237void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002238 using namespace llvm;
2239 if (SemaRef.ReferencedSelectors.empty())
2240 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002241
Fariborz Jahanian32019832010-07-23 19:11:11 +00002242 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002243
Sebastian Redl3397c552010-08-18 23:56:27 +00002244 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002245 // very tricky to fix, and given that @selector shouldn't really appear in
2246 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002247 for (DenseMap<Selector, SourceLocation>::iterator S =
2248 SemaRef.ReferencedSelectors.begin(),
2249 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2250 Selector Sel = (*S).first;
2251 SourceLocation Loc = (*S).second;
2252 AddSelectorRef(Sel, Record);
2253 AddSourceLocation(Loc, Record);
2254 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002255 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002256}
2257
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002258//===----------------------------------------------------------------------===//
2259// Identifier Table Serialization
2260//===----------------------------------------------------------------------===//
2261
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002262namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002263class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002264 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002265 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002266
Douglas Gregora92193e2009-04-28 21:18:29 +00002267 /// \brief Determines whether this is an "interesting" identifier
2268 /// that needs a full IdentifierInfo structure written into the hash
2269 /// table.
2270 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2271 return II->isPoisoned() ||
2272 II->isExtensionToken() ||
2273 II->hasMacroDefinition() ||
2274 II->getObjCOrBuiltinID() ||
2275 II->getFETokenInfo<void>();
2276 }
2277
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002278public:
2279 typedef const IdentifierInfo* key_type;
2280 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002282 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002283 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Sebastian Redl3397c552010-08-18 23:56:27 +00002285 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002286 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002287
2288 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002289 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002290 }
Mike Stump1eb44332009-09-09 15:08:12 +00002291
2292 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002293 EmitKeyDataLength(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002294 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002295 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002296 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2297 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002298 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002299 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002300 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002301 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002302 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2303 DEnd = IdentifierResolver::end();
2304 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002305 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002306 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002307 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002308 // We emit the key length after the data length so that every
2309 // string is preceded by a 16-bit length. This matches the PTH
2310 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002311 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002312 return std::make_pair(KeyLen, DataLen);
2313 }
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Chris Lattner5f9e2722011-07-23 10:55:15 +00002315 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002316 unsigned KeyLen) {
2317 // Record the location of the key data. This is used when generating
2318 // the mapping from persistent IDs to strings.
2319 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002320 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002321 }
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Chris Lattner5f9e2722011-07-23 10:55:15 +00002323 void EmitData(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002324 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002325 if (!isInterestingIdentifier(II)) {
2326 clang::io::Emit32(Out, ID << 1);
2327 return;
2328 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002329
Douglas Gregora92193e2009-04-28 21:18:29 +00002330 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002331 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002332 bool hasMacroDefinition =
2333 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002334 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002335 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002336 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2337 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2338 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002339 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002340 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002341 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002342
Douglas Gregor37e26842009-04-21 23:56:24 +00002343 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002344 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002345
Douglas Gregor668c1a42009-04-21 22:25:48 +00002346 // Emit the declaration IDs in reverse order, because the
2347 // IdentifierResolver provides the declarations as they would be
2348 // visible (e.g., the function "stat" would come before the struct
2349 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2350 // adds declarations to the end of the list (so we need to see the
2351 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002352 // Only emit declarations that aren't from a chained PCH, though.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002353 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002354 IdentifierResolver::end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002355 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002356 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002357 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002358 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002359 }
2360};
2361} // end anonymous namespace
2362
Sebastian Redl3397c552010-08-18 23:56:27 +00002363/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002364///
2365/// The identifier table consists of a blob containing string data
2366/// (the actual identifiers themselves) and a separate "offsets" index
2367/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002368void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002369 using namespace llvm;
2370
2371 // Create and write out the blob that contains the identifier
2372 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002373 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002374 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002375 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002376
Douglas Gregor92b059e2009-04-28 20:33:11 +00002377 // Look for any identifiers that were named while processing the
2378 // headers, but are otherwise not needed. We add these to the hash
2379 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002380 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002381 // file.
2382 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2383 IDEnd = PP.getIdentifierTable().end();
2384 ID != IDEnd; ++ID)
2385 getIdentifierRef(ID->second);
2386
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002387 // Create the on-disk hash table representation. We only store offsets
2388 // for identifiers that appear here for the first time.
2389 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002390 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002391 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2392 ID != IDEnd; ++ID) {
2393 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002394 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002395 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002396 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002397
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002398 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002399 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002400 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002401 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002402 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002403 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002404 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002405 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002406 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002407 }
2408
2409 // Create a blob abbreviation
2410 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002411 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002414 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002415
2416 // Write the identifier table
2417 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002418 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002419 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002420 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002421 }
2422
2423 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002424 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002425 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2429 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2430
2431 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002432 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002433 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002434 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002435 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002436 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002437}
2438
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002439//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002440// DeclContext's Name Lookup Table Serialization
2441//===----------------------------------------------------------------------===//
2442
2443namespace {
2444// Trait used for the on-disk hash table used in the method pool.
2445class ASTDeclContextNameLookupTrait {
2446 ASTWriter &Writer;
2447
2448public:
2449 typedef DeclarationName key_type;
2450 typedef key_type key_type_ref;
2451
2452 typedef DeclContext::lookup_result data_type;
2453 typedef const data_type& data_type_ref;
2454
2455 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2456
2457 unsigned ComputeHash(DeclarationName Name) {
2458 llvm::FoldingSetNodeID ID;
2459 ID.AddInteger(Name.getNameKind());
2460
2461 switch (Name.getNameKind()) {
2462 case DeclarationName::Identifier:
2463 ID.AddString(Name.getAsIdentifierInfo()->getName());
2464 break;
2465 case DeclarationName::ObjCZeroArgSelector:
2466 case DeclarationName::ObjCOneArgSelector:
2467 case DeclarationName::ObjCMultiArgSelector:
2468 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2469 break;
2470 case DeclarationName::CXXConstructorName:
2471 case DeclarationName::CXXDestructorName:
2472 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002473 break;
2474 case DeclarationName::CXXOperatorName:
2475 ID.AddInteger(Name.getCXXOverloadedOperator());
2476 break;
2477 case DeclarationName::CXXLiteralOperatorName:
2478 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2479 case DeclarationName::CXXUsingDirective:
2480 break;
2481 }
2482
2483 return ID.ComputeHash();
2484 }
2485
2486 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002487 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002488 data_type_ref Lookup) {
2489 unsigned KeyLen = 1;
2490 switch (Name.getNameKind()) {
2491 case DeclarationName::Identifier:
2492 case DeclarationName::ObjCZeroArgSelector:
2493 case DeclarationName::ObjCOneArgSelector:
2494 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002495 case DeclarationName::CXXLiteralOperatorName:
2496 KeyLen += 4;
2497 break;
2498 case DeclarationName::CXXOperatorName:
2499 KeyLen += 1;
2500 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002501 case DeclarationName::CXXConstructorName:
2502 case DeclarationName::CXXDestructorName:
2503 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002504 case DeclarationName::CXXUsingDirective:
2505 break;
2506 }
2507 clang::io::Emit16(Out, KeyLen);
2508
2509 // 2 bytes for num of decls and 4 for each DeclID.
2510 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2511 clang::io::Emit16(Out, DataLen);
2512
2513 return std::make_pair(KeyLen, DataLen);
2514 }
2515
Chris Lattner5f9e2722011-07-23 10:55:15 +00002516 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002517 using namespace clang::io;
2518
2519 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2520 Emit8(Out, Name.getNameKind());
2521 switch (Name.getNameKind()) {
2522 case DeclarationName::Identifier:
2523 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2524 break;
2525 case DeclarationName::ObjCZeroArgSelector:
2526 case DeclarationName::ObjCOneArgSelector:
2527 case DeclarationName::ObjCMultiArgSelector:
2528 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2529 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002530 case DeclarationName::CXXOperatorName:
2531 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2532 Emit8(Out, Name.getCXXOverloadedOperator());
2533 break;
2534 case DeclarationName::CXXLiteralOperatorName:
2535 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2536 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002537 case DeclarationName::CXXConstructorName:
2538 case DeclarationName::CXXDestructorName:
2539 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002540 case DeclarationName::CXXUsingDirective:
2541 break;
2542 }
2543 }
2544
Chris Lattner5f9e2722011-07-23 10:55:15 +00002545 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002546 data_type Lookup, unsigned DataLen) {
2547 uint64_t Start = Out.tell(); (void)Start;
2548 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2549 for (; Lookup.first != Lookup.second; ++Lookup.first)
2550 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2551
2552 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2553 }
2554};
2555} // end anonymous namespace
2556
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002557/// \brief Write the block containing all of the declaration IDs
2558/// visible from the given DeclContext.
2559///
2560/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002561/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002562uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2563 DeclContext *DC) {
2564 if (DC->getPrimaryContext() != DC)
2565 return 0;
2566
2567 // Since there is no name lookup into functions or methods, don't bother to
2568 // build a visible-declarations table for these entities.
2569 if (DC->isFunctionOrMethod())
2570 return 0;
2571
2572 // If not in C++, we perform name lookup for the translation unit via the
2573 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2574 // FIXME: In C++ we need the visible declarations in order to "see" the
2575 // friend declarations, is there a way to do this without writing the table ?
2576 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2577 return 0;
2578
2579 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002580 if (DC->hasExternalVisibleStorage())
2581 DC->MaterializeVisibleDeclsFromExternalStorage();
2582 else
2583 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002584
2585 // Serialize the contents of the mapping used for lookup. Note that,
2586 // although we have two very different code paths, the serialized
2587 // representation is the same for both cases: a declaration name,
2588 // followed by a size, followed by references to the visible
2589 // declarations that have that name.
2590 uint64_t Offset = Stream.GetCurrentBitNo();
2591 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2592 if (!Map || Map->empty())
2593 return 0;
2594
2595 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2596 ASTDeclContextNameLookupTrait Trait(*this);
2597
2598 // Create the on-disk hash table representation.
2599 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2600 D != DEnd; ++D) {
2601 DeclarationName Name = D->first;
2602 DeclContext::lookup_result Result = D->second.getLookupResult();
2603 Generator.insert(Name, Result, Trait);
2604 }
2605
2606 // Create the on-disk hash table in a buffer.
2607 llvm::SmallString<4096> LookupTable;
2608 uint32_t BucketOffset;
2609 {
2610 llvm::raw_svector_ostream Out(LookupTable);
2611 // Make sure that no bucket is at offset 0
2612 clang::io::Emit32(Out, 0);
2613 BucketOffset = Generator.Emit(Out, Trait);
2614 }
2615
2616 // Write the lookup table
2617 RecordData Record;
2618 Record.push_back(DECL_CONTEXT_VISIBLE);
2619 Record.push_back(BucketOffset);
2620 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2621 LookupTable.str());
2622
2623 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2624 ++NumVisibleDeclContexts;
2625 return Offset;
2626}
2627
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002628/// \brief Write an UPDATE_VISIBLE block for the given context.
2629///
2630/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2631/// DeclContext in a dependent AST file. As such, they only exist for the TU
2632/// (in C++) and for namespaces.
2633void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002634 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2635 if (!Map || Map->empty())
2636 return;
2637
2638 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2639 ASTDeclContextNameLookupTrait Trait(*this);
2640
2641 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002642 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2643 D != DEnd; ++D) {
2644 DeclarationName Name = D->first;
2645 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002646 // For any name that appears in this table, the results are complete, i.e.
2647 // they overwrite results from previous PCHs. Merging is always a mess.
2648 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002649 }
2650
2651 // Create the on-disk hash table in a buffer.
2652 llvm::SmallString<4096> LookupTable;
2653 uint32_t BucketOffset;
2654 {
2655 llvm::raw_svector_ostream Out(LookupTable);
2656 // Make sure that no bucket is at offset 0
2657 clang::io::Emit32(Out, 0);
2658 BucketOffset = Generator.Emit(Out, Trait);
2659 }
2660
2661 // Write the lookup table
2662 RecordData Record;
2663 Record.push_back(UPDATE_VISIBLE);
2664 Record.push_back(getDeclID(cast<Decl>(DC)));
2665 Record.push_back(BucketOffset);
2666 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2667}
2668
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002669/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2670void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2671 RecordData Record;
2672 Record.push_back(Opts.fp_contract);
2673 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2674}
2675
2676/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2677void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2678 if (!SemaRef.Context.getLangOptions().OpenCL)
2679 return;
2680
2681 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2682 RecordData Record;
2683#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2684#include "clang/Basic/OpenCLExtensions.def"
2685 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2686}
2687
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002688//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002689// General Serialization Routines
2690//===----------------------------------------------------------------------===//
2691
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002692/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002693void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002694 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002695 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2696 const Attr * A = *i;
2697 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2698 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002699
Sean Huntcf807c42010-08-18 23:23:40 +00002700#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002701
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002702 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002703}
2704
Chris Lattner5f9e2722011-07-23 10:55:15 +00002705void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002706 Record.push_back(Str.size());
2707 Record.insert(Record.end(), Str.begin(), Str.end());
2708}
2709
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002710void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2711 RecordDataImpl &Record) {
2712 Record.push_back(Version.getMajor());
2713 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2714 Record.push_back(*Minor + 1);
2715 else
2716 Record.push_back(0);
2717 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2718 Record.push_back(*Subminor + 1);
2719 else
2720 Record.push_back(0);
2721}
2722
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002723/// \brief Note that the identifier II occurs at the given offset
2724/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002725void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002726 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002727 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002728 // up earlier in the chain and thus don't need an offset.
2729 if (ID >= FirstIdentID)
2730 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002731}
2732
Douglas Gregor83941df2009-04-25 17:48:32 +00002733/// \brief Note that the selector Sel occurs at the given offset
2734/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002735void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002736 unsigned ID = SelectorIDs[Sel];
2737 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002738 // Don't record offsets for selectors that are also available in a different
2739 // file.
2740 if (ID < FirstSelectorID)
2741 return;
2742 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002743}
2744
Sebastian Redla4232eb2010-08-18 23:56:21 +00002745ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002746 : Stream(Stream), Chain(0), SerializationListener(0),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002747 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002748 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002749 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002750 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
2751 FirstMacroID(1), NextMacroID(FirstMacroID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002752 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002753 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002754 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00002755 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002756 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002757 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2758 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2759 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002760 DeclTypedefAbbrev(0),
2761 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2762 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00002763{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002764}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002765
Sebastian Redla4232eb2010-08-18 23:56:21 +00002766void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002767 const std::string &OutputFile,
Douglas Gregor832d6202011-07-22 16:35:34 +00002768 StringRef isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002769 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002770 Stream.Emit((unsigned)'C', 8);
2771 Stream.Emit((unsigned)'P', 8);
2772 Stream.Emit((unsigned)'C', 8);
2773 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002775 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002776
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002777 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002778 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002779 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002780 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002781}
2782
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002783template<typename Vector>
2784static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2785 ASTWriter::RecordData &Record) {
2786 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2787 I != E; ++I) {
2788 Writer.AddDeclRef(*I, Record);
2789 }
2790}
2791
Sebastian Redla4232eb2010-08-18 23:56:21 +00002792void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00002793 StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002794 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002795 using namespace llvm;
2796
2797 ASTContext &Context = SemaRef.Context;
2798 Preprocessor &PP = SemaRef.PP;
2799
Douglas Gregor2cf26342009-04-09 22:27:44 +00002800 // The translation unit is the first declaration we'll emit.
2801 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002802 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002803 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002804
Douglas Gregor2deaea32009-04-22 18:49:13 +00002805 // Make sure that we emit IdentifierInfos (and any attached
2806 // declarations) for builtins.
2807 {
2808 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002809 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002810 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2811 Context.getLangOptions().NoBuiltin);
2812 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2813 getIdentifierRef(&Table.get(BuiltinNames[I]));
2814 }
2815
Chris Lattner63d65f82009-09-08 18:19:27 +00002816 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002817 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002818 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002819 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002820 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00002821
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002822 // Build a record containing all of the file scoped decls in this file.
2823 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002824 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
2825 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002826
Sean Huntebcbe1d2011-05-04 23:29:54 +00002827 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00002828 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00002829
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002830 RecordData WeakUndeclaredIdentifiers;
2831 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00002832 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002833 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2834 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2835 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2836 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2837 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2838 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2839 }
2840 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002841
Douglas Gregor14c22f22009-04-22 22:18:58 +00002842 // Build a record containing all of the locally-scoped external
2843 // declarations in this header file. Generally, this record will be
2844 // empty.
2845 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002846 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002847 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002848 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002849 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2850 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00002851 TD != TDEnd; ++TD) {
2852 if (TD->second->getPCHLevel() == 0)
2853 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2854 }
2855
Douglas Gregorb81c1702009-04-27 20:06:05 +00002856 // Build a record containing all of the ext_vector declarations.
2857 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00002858 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002859
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002860 // Build a record containing all of the VTable uses information.
2861 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002862 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002863 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2864 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2865 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2866 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2867 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002868 }
2869
2870 // Build a record containing all of dynamic classes declarations.
2871 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00002872 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002873
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002874 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002875 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002876 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002877 I = SemaRef.PendingInstantiations.begin(),
2878 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2879 AddDeclRef(I->first, PendingInstantiations);
2880 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002881 }
2882 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2883 "There are local ones at end of translation unit!");
2884
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002885 // Build a record containing some declaration references.
2886 RecordData SemaDeclRefs;
2887 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2888 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2889 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2890 }
2891
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002892 RecordData CUDASpecialDeclRefs;
2893 if (Context.getcudaConfigureCallDecl()) {
2894 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2895 }
2896
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002897 // Build a record containing all of the known namespaces.
2898 RecordData KnownNamespaces;
2899 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2900 I = SemaRef.KnownNamespaces.begin(),
2901 IEnd = SemaRef.KnownNamespaces.end();
2902 I != IEnd; ++I) {
2903 if (!I->second)
2904 AddDeclRef(I->first, KnownNamespaces);
2905 }
2906
Sebastian Redl3397c552010-08-18 23:56:27 +00002907 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002908 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002909 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002910 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002911 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00002912 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00002913 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002914 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Douglas Gregor69a9e012011-08-01 16:54:33 +00002915
Douglas Gregora119da02011-08-02 16:26:37 +00002916 // Form the record of special types.
2917 RecordData SpecialTypes;
2918 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
2919 AddTypeRef(Context.getObjCIdType(), SpecialTypes);
2920 AddTypeRef(Context.getObjCSelType(), SpecialTypes);
2921 AddTypeRef(Context.getObjCProtoType(), SpecialTypes);
2922 AddTypeRef(Context.getObjCClassType(), SpecialTypes);
2923 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
2924 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), SpecialTypes);
2925 AddTypeRef(Context.getFILEType(), SpecialTypes);
2926 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
2927 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
2928 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
2929 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
2930 AddTypeRef(Context.getRawBlockdescriptorType(), SpecialTypes);
2931 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), SpecialTypes);
2932 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
2933 AddTypeRef(Context.getRawNSConstantStringType(), SpecialTypes);
2934 SpecialTypes.push_back(Context.isInt128Installed());
2935 AddTypeRef(Context.AutoDeductTy, SpecialTypes);
2936 AddTypeRef(Context.AutoRRefDeductTy, SpecialTypes);
Mike Stump1eb44332009-09-09 15:08:12 +00002937
Douglas Gregor366809a2009-04-26 03:49:13 +00002938 // Keep writing types and declarations until all types and
2939 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00002940 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002941 WriteDeclsBlockAbbrevs();
2942 while (!DeclTypesToEmit.empty()) {
2943 DeclOrType DOT = DeclTypesToEmit.front();
2944 DeclTypesToEmit.pop();
2945 if (DOT.isType())
2946 WriteType(DOT.getType());
2947 else
2948 WriteDecl(Context, DOT.getDecl());
2949 }
2950 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002951
Douglas Gregor813a97b2009-10-17 17:25:45 +00002952 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002953 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002954 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002955 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002956 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002957 WriteFPPragmaOptions(SemaRef.getFPOptions());
2958 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002959
Sebastian Redl1476ed42010-07-16 16:36:56 +00002960 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002961 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002962
Anders Carlssonc8505782011-03-06 18:41:18 +00002963 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00002964
Douglas Gregora119da02011-08-02 16:26:37 +00002965 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
2966
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002967 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002968 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002969 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002970
2971 // Write the record containing tentative definitions.
2972 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002973 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002974
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002975 // Write the record containing unused file scoped decls.
2976 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002977 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002978
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002979 // Write the record containing weak undeclared identifiers.
2980 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002981 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002982 WeakUndeclaredIdentifiers);
2983
Douglas Gregor14c22f22009-04-22 22:18:58 +00002984 // Write the record containing locally-scoped external definitions.
2985 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002986 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002987 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002988
2989 // Write the record containing ext_vector type names.
2990 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002991 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002992
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002993 // Write the record containing VTable uses information.
2994 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002995 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002996
2997 // Write the record containing dynamic classes declarations.
2998 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002999 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003000
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003001 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003002 if (!PendingInstantiations.empty())
3003 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003004
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003005 // Write the record containing declaration references of Sema.
3006 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003007 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003008
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003009 // Write the record containing CUDA-specific declaration references.
3010 if (!CUDASpecialDeclRefs.empty())
3011 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003012
3013 // Write the delegating constructors.
3014 if (!DelegatingCtorDecls.empty())
3015 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003016
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003017 // Write the known namespaces.
3018 if (!KnownNamespaces.empty())
3019 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3020
Douglas Gregor3e1af842009-04-17 22:13:46 +00003021 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003022 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003023 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003024 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003025 Record.push_back(NumLexicalDeclContexts);
3026 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003027 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003028 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003029}
3030
Sebastian Redla4232eb2010-08-18 23:56:21 +00003031void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003032 StringRef isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003033 using namespace llvm;
3034
3035 ASTContext &Context = SemaRef.Context;
3036 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00003037
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003038 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003039 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003040 WriteMetadata(Context, isysroot, "");
Douglas Gregor832d6202011-07-22 16:35:34 +00003041 if (StatCalls && isysroot.empty())
Sebastian Redl1476ed42010-07-16 16:36:56 +00003042 WriteStatCache(*StatCalls);
3043 // FIXME: Source manager block should only write new stuff, which could be
3044 // done by tracking the largest ID in the chain
3045 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003046
Douglas Gregor69a9e012011-08-01 16:54:33 +00003047 // Write the mapping information describing our module dependencies and how
3048 // each of those modules were mapped into our own offset/ID space, so that
3049 // the reader can build the appropriate mapping to its own offset/ID space.
Douglas Gregor69a9e012011-08-01 16:54:33 +00003050 // The map consists solely of a blob with the following format:
Douglas Gregorf33740e2011-08-02 10:56:51 +00003051 // *(module-name-len:i16 module-name:len*i8
3052 // source-location-offset:i32
3053 // identifier-id:i32
3054 // preprocessed-entity-id:i32
3055 // macro-definition-id:i32
3056 // selector-id:i32
3057 // declaration-id:i32
3058 // c++-base-specifiers-id:i32
3059 // type-id:i32)
3060 //
Douglas Gregor69a9e012011-08-01 16:54:33 +00003061 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3062 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorf33740e2011-08-02 10:56:51 +00003064 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor69a9e012011-08-01 16:54:33 +00003065 llvm::SmallString<2048> Buffer;
3066 {
3067 llvm::raw_svector_ostream Out(Buffer);
Douglas Gregorf33740e2011-08-02 10:56:51 +00003068 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
3069 MEnd = Chain->ModuleMgr.end();
3070 M != MEnd; ++M) {
3071 StringRef FileName = (*M)->FileName;
3072 io::Emit16(Out, FileName.size());
3073 Out.write(FileName.data(), FileName.size());
3074 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3075 io::Emit32(Out, (*M)->BaseIdentifierID);
3076 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
3077 io::Emit32(Out, (*M)->BaseMacroDefinitionID);
3078 io::Emit32(Out, (*M)->BaseSelectorID);
3079 io::Emit32(Out, (*M)->BaseDeclID);
Douglas Gregore3605012011-08-02 18:32:54 +00003080 io::Emit32(Out, (*M)->BaseTypeIndex);
Douglas Gregor69a9e012011-08-01 16:54:33 +00003081 }
3082 }
3083 Record.clear();
3084 Record.push_back(MODULE_OFFSET_MAP);
Douglas Gregorf33740e2011-08-02 10:56:51 +00003085 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
Douglas Gregor69a9e012011-08-01 16:54:33 +00003086 Buffer.data(), Buffer.size());
3087
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003088 // The special types are in the chained PCH.
3089
3090 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003091 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003092 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003093 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00003094 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3095 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003096 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00003097 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003098 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003099 else if ((*I)->isChangedSinceDeserialization())
3100 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003101 }
Sebastian Redl681d7232010-07-27 00:17:23 +00003102 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00003103 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003104 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00003105 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3106 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3107 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003108 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00003109 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003110 data(NewGlobalDecls));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003111 // And a visible updates block for the DeclContexts.
3112 Abv = new llvm::BitCodeAbbrev();
3113 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3114 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3115 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3116 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3117 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3118 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003119
Sebastian Redl083abdf2010-07-27 23:01:28 +00003120 // Build a record containing all of the new tentative definitions in this
3121 // file, in TentativeDefinitions order.
3122 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003123 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003124
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003125 // Build a record containing all of the file scoped decls in this file.
3126 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003127 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3128 UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003129
Sean Huntebcbe1d2011-05-04 23:29:54 +00003130 // Build a record containing all of the delegating constructor decls in this
3131 // file.
3132 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003133 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003134
Sebastian Redl40566802010-08-05 18:21:25 +00003135 // We write the entire table, overwriting the tables from the chain.
3136 RecordData WeakUndeclaredIdentifiers;
3137 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003138 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Sebastian Redl40566802010-08-05 18:21:25 +00003139 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3140 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3141 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3142 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3143 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3144 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3145 }
3146 }
3147
Sebastian Redl083abdf2010-07-27 23:01:28 +00003148 // Build a record containing all of the locally-scoped external
3149 // declarations in this header file. Generally, this record will be
3150 // empty.
3151 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003152 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00003153 // nondeterminstic!
3154 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3155 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3156 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3157 TD != TDEnd; ++TD) {
3158 if (TD->second->getPCHLevel() == 0)
3159 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3160 }
3161
3162 // Build a record containing all of the ext_vector declarations.
3163 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003164 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003165
Sebastian Redl40566802010-08-05 18:21:25 +00003166 // Build a record containing all of the VTable uses information.
3167 // We write everything here, because it's too hard to determine whether
3168 // a use is new to this part.
3169 RecordData VTableUses;
3170 if (!SemaRef.VTableUses.empty()) {
Sebastian Redl40566802010-08-05 18:21:25 +00003171 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3172 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3173 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3174 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3175 }
3176 }
3177
3178 // Build a record containing all of dynamic classes declarations.
3179 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003180 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003181
3182 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003183 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00003184 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003185 I = SemaRef.PendingInstantiations.begin(),
3186 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00003187 AddDeclRef(I->first, PendingInstantiations);
3188 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003189 }
3190 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3191 "There are local ones at end of translation unit!");
3192
3193 // Build a record containing some declaration references.
3194 // It's not worth the effort to avoid duplication here.
3195 RecordData SemaDeclRefs;
3196 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3197 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3198 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3199 }
3200
Douglas Gregora72d8c42011-06-03 02:27:19 +00003201 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003202 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003203 for (DeclsToRewriteTy::iterator
3204 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3205 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003206 while (!DeclTypesToEmit.empty()) {
3207 DeclOrType DOT = DeclTypesToEmit.front();
3208 DeclTypesToEmit.pop();
3209 if (DOT.isType())
3210 WriteType(DOT.getType());
3211 else
3212 WriteDecl(Context, DOT.getDecl());
3213 }
3214 Stream.ExitBlock();
3215
Sebastian Redl083abdf2010-07-27 23:01:28 +00003216 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003217 WriteSelectors(SemaRef);
3218 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003219 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003220 WriteFPPragmaOptions(SemaRef.getFPOptions());
3221 WriteOpenCLExtensions(SemaRef);
3222
Sebastian Redl1476ed42010-07-16 16:36:56 +00003223 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003224 // FIXME: For chained PCH only write the new mappings (we currently
3225 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003226 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003227
Anders Carlssonc8505782011-03-06 18:41:18 +00003228 WriteCXXBaseSpecifiersOffsets();
3229
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003230 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003231 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003232 RecordData FirstLatestDeclIDs;
3233 for (FirstLatestDeclMap::iterator
3234 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3235 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3236 "Expected first & second to be in different PCHs");
3237 AddDeclRef(I->first, FirstLatestDeclIDs);
3238 AddDeclRef(I->second, FirstLatestDeclIDs);
3239 }
3240 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003241 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003242
Sebastian Redl083abdf2010-07-27 23:01:28 +00003243 // Write the record containing external, unnamed definitions.
3244 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003245 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003246
3247 // Write the record containing tentative definitions.
3248 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003249 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003250
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003251 // Write the record containing unused file scoped decls.
3252 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003253 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003254
Sebastian Redl40566802010-08-05 18:21:25 +00003255 // Write the record containing weak undeclared identifiers.
3256 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003257 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003258 WeakUndeclaredIdentifiers);
3259
Sebastian Redl083abdf2010-07-27 23:01:28 +00003260 // Write the record containing locally-scoped external definitions.
3261 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003262 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003263 LocallyScopedExternalDecls);
3264
3265 // Write the record containing ext_vector type names.
3266 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003267 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003268
Sebastian Redl40566802010-08-05 18:21:25 +00003269 // Write the record containing VTable uses information.
3270 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003271 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003272
3273 // Write the record containing dynamic classes declarations.
3274 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003275 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003276
3277 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003278 if (!PendingInstantiations.empty())
3279 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003280
3281 // Write the record containing declaration references of Sema.
3282 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003283 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003284
3285 // Write the delegating constructors.
3286 if (!DelegatingCtorDecls.empty())
3287 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003288
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003289 // Write the updates to DeclContexts.
3290 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3291 I = UpdatedDeclContexts.begin(),
3292 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003293 I != E; ++I)
3294 WriteDeclContextVisibleUpdate(*I);
3295
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003296 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003297
Sebastian Redl083abdf2010-07-27 23:01:28 +00003298 Record.clear();
3299 Record.push_back(NumStatements);
3300 Record.push_back(NumMacros);
3301 Record.push_back(NumLexicalDeclContexts);
3302 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003303 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003304 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003305 Stream.ExitBlock();
3306}
3307
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003308void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003309 if (DeclUpdates.empty())
3310 return;
3311
3312 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003313 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003314 for (DeclUpdateMap::iterator
3315 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3316 const Decl *D = I->first;
3317 UpdateRecord &URec = I->second;
3318
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003319 if (DeclsToRewrite.count(D))
3320 continue; // The decl will be written completely,no need to store updates.
3321
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003322 uint64_t Offset = Stream.GetCurrentBitNo();
3323 Stream.EmitRecord(DECL_UPDATES, URec);
3324
3325 OffsetsRecord.push_back(GetDeclRef(D));
3326 OffsetsRecord.push_back(Offset);
3327 }
3328 Stream.ExitBlock();
3329 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3330}
3331
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003332void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003333 if (ReplacedDecls.empty())
3334 return;
3335
3336 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003337 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003338 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3339 Record.push_back(I->first);
3340 Record.push_back(I->second);
3341 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003342 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003343}
3344
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003345void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003346 Record.push_back(Loc.getRawEncoding());
3347}
3348
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003349void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003350 AddSourceLocation(Range.getBegin(), Record);
3351 AddSourceLocation(Range.getEnd(), Record);
3352}
3353
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003354void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003355 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003356 const uint64_t *Words = Value.getRawData();
3357 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003358}
3359
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003360void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003361 Record.push_back(Value.isUnsigned());
3362 AddAPInt(Value, Record);
3363}
3364
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003365void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003366 AddAPInt(Value.bitcastToAPInt(), Record);
3367}
3368
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003369void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003370 Record.push_back(getIdentifierRef(II));
3371}
3372
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003373IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003374 if (II == 0)
3375 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003376
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003377 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003378 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003379 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003380 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003381}
3382
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003383MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003384 if (MD == 0)
3385 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003386
3387 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003388 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003389 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003390 return ID;
3391}
3392
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003393void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003394 Record.push_back(getSelectorRef(SelRef));
3395}
3396
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003397SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003398 if (Sel.getAsOpaquePtr() == 0) {
3399 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003400 }
3401
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003402 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003403 if (SID == 0 && Chain) {
3404 // This might trigger a ReadSelector callback, which will set the ID for
3405 // this selector.
3406 Chain->LoadSelector(Sel);
3407 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003408 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003409 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003410 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003411 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003412}
3413
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003414void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003415 AddDeclRef(Temp->getDestructor(), Record);
3416}
3417
Douglas Gregor7c789c12010-10-29 22:39:52 +00003418void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3419 CXXBaseSpecifier const *BasesEnd,
3420 RecordDataImpl &Record) {
3421 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3422 CXXBaseSpecifiersToWrite.push_back(
3423 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3424 Bases, BasesEnd));
3425 Record.push_back(NextCXXBaseSpecifiersID++);
3426}
3427
Sebastian Redla4232eb2010-08-18 23:56:21 +00003428void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003429 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003430 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003431 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003432 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003433 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003434 break;
3435 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003436 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003437 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003438 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003439 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003440 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003441 break;
3442 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003443 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003444 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003445 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003446 break;
John McCall833ca992009-10-29 08:12:44 +00003447 case TemplateArgument::Null:
3448 case TemplateArgument::Integral:
3449 case TemplateArgument::Declaration:
3450 case TemplateArgument::Pack:
3451 break;
3452 }
3453}
3454
Sebastian Redla4232eb2010-08-18 23:56:21 +00003455void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003456 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003457 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003458
3459 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3460 bool InfoHasSameExpr
3461 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3462 Record.push_back(InfoHasSameExpr);
3463 if (InfoHasSameExpr)
3464 return; // Avoid storing the same expr twice.
3465 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003466 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3467 Record);
3468}
3469
Douglas Gregordc355712011-02-25 00:36:19 +00003470void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3471 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003472 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003473 AddTypeRef(QualType(), Record);
3474 return;
3475 }
3476
Douglas Gregordc355712011-02-25 00:36:19 +00003477 AddTypeLoc(TInfo->getTypeLoc(), Record);
3478}
3479
3480void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3481 AddTypeRef(TL.getType(), Record);
3482
John McCalla1ee0c52009-10-16 21:56:05 +00003483 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003484 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003485 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003486}
3487
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003488void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003489 Record.push_back(GetOrCreateTypeID(T));
3490}
3491
3492TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003493 return MakeTypeID(T,
3494 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3495}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003496
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003497TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003498 return MakeTypeID(T,
3499 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003500}
3501
3502TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3503 if (T.isNull())
3504 return TypeIdx();
3505 assert(!T.getLocalFastQualifiers());
3506
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003507 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003508 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003509 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003510 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003511 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003512 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003513 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003514 return Idx;
3515}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003516
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003517TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003518 if (T.isNull())
3519 return TypeIdx();
3520 assert(!T.getLocalFastQualifiers());
3521
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003522 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3523 assert(I != TypeIdxs.end() && "Type not emitted!");
3524 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003525}
3526
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003527void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003528 Record.push_back(GetDeclRef(D));
3529}
3530
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003531DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003532 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003533 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003534 }
Douglas Gregor97475832010-10-05 18:37:06 +00003535 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003536 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003537 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003538 // We haven't seen this declaration before. Give it a new ID and
3539 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003540 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003541 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003542 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3543 // We don't add it to the replacement collection here, because we don't
3544 // have the offset yet.
3545 DeclTypesToEmit.push(const_cast<Decl *>(D));
3546 // Reset the flag, so that we don't add this decl multiple times.
3547 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003548 }
3549
Sebastian Redl681d7232010-07-27 00:17:23 +00003550 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003551}
3552
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003553DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003554 if (D == 0)
3555 return 0;
3556
3557 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3558 return DeclIDs[D];
3559}
3560
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003561void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003562 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003563 Record.push_back(Name.getNameKind());
3564 switch (Name.getNameKind()) {
3565 case DeclarationName::Identifier:
3566 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3567 break;
3568
3569 case DeclarationName::ObjCZeroArgSelector:
3570 case DeclarationName::ObjCOneArgSelector:
3571 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003572 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003573 break;
3574
3575 case DeclarationName::CXXConstructorName:
3576 case DeclarationName::CXXDestructorName:
3577 case DeclarationName::CXXConversionFunctionName:
3578 AddTypeRef(Name.getCXXNameType(), Record);
3579 break;
3580
3581 case DeclarationName::CXXOperatorName:
3582 Record.push_back(Name.getCXXOverloadedOperator());
3583 break;
3584
Sean Hunt3e518bd2009-11-29 07:34:05 +00003585 case DeclarationName::CXXLiteralOperatorName:
3586 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3587 break;
3588
Douglas Gregor2cf26342009-04-09 22:27:44 +00003589 case DeclarationName::CXXUsingDirective:
3590 // No extra data to emit
3591 break;
3592 }
3593}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003594
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003595void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003596 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003597 switch (Name.getNameKind()) {
3598 case DeclarationName::CXXConstructorName:
3599 case DeclarationName::CXXDestructorName:
3600 case DeclarationName::CXXConversionFunctionName:
3601 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3602 break;
3603
3604 case DeclarationName::CXXOperatorName:
3605 AddSourceLocation(
3606 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3607 Record);
3608 AddSourceLocation(
3609 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3610 Record);
3611 break;
3612
3613 case DeclarationName::CXXLiteralOperatorName:
3614 AddSourceLocation(
3615 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3616 Record);
3617 break;
3618
3619 case DeclarationName::Identifier:
3620 case DeclarationName::ObjCZeroArgSelector:
3621 case DeclarationName::ObjCOneArgSelector:
3622 case DeclarationName::ObjCMultiArgSelector:
3623 case DeclarationName::CXXUsingDirective:
3624 break;
3625 }
3626}
3627
3628void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003629 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003630 AddDeclarationName(NameInfo.getName(), Record);
3631 AddSourceLocation(NameInfo.getLoc(), Record);
3632 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3633}
3634
3635void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003636 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003637 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003638 Record.push_back(Info.NumTemplParamLists);
3639 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3640 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3641}
3642
Sebastian Redla4232eb2010-08-18 23:56:21 +00003643void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003644 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003645 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003646 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003647 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003648
3649 // Push each of the NNS's onto a stack for serialization in reverse order.
3650 while (NNS) {
3651 NestedNames.push_back(NNS);
3652 NNS = NNS->getPrefix();
3653 }
3654
3655 Record.push_back(NestedNames.size());
3656 while(!NestedNames.empty()) {
3657 NNS = NestedNames.pop_back_val();
3658 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3659 Record.push_back(Kind);
3660 switch (Kind) {
3661 case NestedNameSpecifier::Identifier:
3662 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3663 break;
3664
3665 case NestedNameSpecifier::Namespace:
3666 AddDeclRef(NNS->getAsNamespace(), Record);
3667 break;
3668
Douglas Gregor14aba762011-02-24 02:36:08 +00003669 case NestedNameSpecifier::NamespaceAlias:
3670 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3671 break;
3672
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003673 case NestedNameSpecifier::TypeSpec:
3674 case NestedNameSpecifier::TypeSpecWithTemplate:
3675 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3676 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3677 break;
3678
3679 case NestedNameSpecifier::Global:
3680 // Don't need to write an associated value.
3681 break;
3682 }
3683 }
3684}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003685
Douglas Gregordc355712011-02-25 00:36:19 +00003686void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3687 RecordDataImpl &Record) {
3688 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003689 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003690 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00003691
3692 // Push each of the nested-name-specifiers's onto a stack for
3693 // serialization in reverse order.
3694 while (NNS) {
3695 NestedNames.push_back(NNS);
3696 NNS = NNS.getPrefix();
3697 }
3698
3699 Record.push_back(NestedNames.size());
3700 while(!NestedNames.empty()) {
3701 NNS = NestedNames.pop_back_val();
3702 NestedNameSpecifier::SpecifierKind Kind
3703 = NNS.getNestedNameSpecifier()->getKind();
3704 Record.push_back(Kind);
3705 switch (Kind) {
3706 case NestedNameSpecifier::Identifier:
3707 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3708 AddSourceRange(NNS.getLocalSourceRange(), Record);
3709 break;
3710
3711 case NestedNameSpecifier::Namespace:
3712 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3713 AddSourceRange(NNS.getLocalSourceRange(), Record);
3714 break;
3715
3716 case NestedNameSpecifier::NamespaceAlias:
3717 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3718 AddSourceRange(NNS.getLocalSourceRange(), Record);
3719 break;
3720
3721 case NestedNameSpecifier::TypeSpec:
3722 case NestedNameSpecifier::TypeSpecWithTemplate:
3723 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3724 AddTypeLoc(NNS.getTypeLoc(), Record);
3725 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3726 break;
3727
3728 case NestedNameSpecifier::Global:
3729 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3730 break;
3731 }
3732 }
3733}
3734
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003735void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003736 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003737 Record.push_back(Kind);
3738 switch (Kind) {
3739 case TemplateName::Template:
3740 AddDeclRef(Name.getAsTemplateDecl(), Record);
3741 break;
3742
3743 case TemplateName::OverloadedTemplate: {
3744 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3745 Record.push_back(OvT->size());
3746 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3747 I != E; ++I)
3748 AddDeclRef(*I, Record);
3749 break;
3750 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003751
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003752 case TemplateName::QualifiedTemplate: {
3753 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3754 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3755 Record.push_back(QualT->hasTemplateKeyword());
3756 AddDeclRef(QualT->getTemplateDecl(), Record);
3757 break;
3758 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003759
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003760 case TemplateName::DependentTemplate: {
3761 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3762 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3763 Record.push_back(DepT->isIdentifier());
3764 if (DepT->isIdentifier())
3765 AddIdentifierRef(DepT->getIdentifier(), Record);
3766 else
3767 Record.push_back(DepT->getOperator());
3768 break;
3769 }
John McCall14606042011-06-30 08:33:18 +00003770
3771 case TemplateName::SubstTemplateTemplateParm: {
3772 SubstTemplateTemplateParmStorage *subst
3773 = Name.getAsSubstTemplateTemplateParm();
3774 AddDeclRef(subst->getParameter(), Record);
3775 AddTemplateName(subst->getReplacement(), Record);
3776 break;
3777 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003778
3779 case TemplateName::SubstTemplateTemplateParmPack: {
3780 SubstTemplateTemplateParmPackStorage *SubstPack
3781 = Name.getAsSubstTemplateTemplateParmPack();
3782 AddDeclRef(SubstPack->getParameterPack(), Record);
3783 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3784 break;
3785 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003786 }
3787}
3788
Michael J. Spencer20249a12010-10-21 03:16:25 +00003789void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003790 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003791 Record.push_back(Arg.getKind());
3792 switch (Arg.getKind()) {
3793 case TemplateArgument::Null:
3794 break;
3795 case TemplateArgument::Type:
3796 AddTypeRef(Arg.getAsType(), Record);
3797 break;
3798 case TemplateArgument::Declaration:
3799 AddDeclRef(Arg.getAsDecl(), Record);
3800 break;
3801 case TemplateArgument::Integral:
3802 AddAPSInt(*Arg.getAsIntegral(), Record);
3803 AddTypeRef(Arg.getIntegralType(), Record);
3804 break;
3805 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003806 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3807 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003808 case TemplateArgument::TemplateExpansion:
3809 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003810 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3811 Record.push_back(*NumExpansions + 1);
3812 else
3813 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003814 break;
3815 case TemplateArgument::Expression:
3816 AddStmt(Arg.getAsExpr());
3817 break;
3818 case TemplateArgument::Pack:
3819 Record.push_back(Arg.pack_size());
3820 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3821 I != E; ++I)
3822 AddTemplateArgument(*I, Record);
3823 break;
3824 }
3825}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003826
3827void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003828ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003829 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003830 assert(TemplateParams && "No TemplateParams!");
3831 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3832 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3833 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3834 Record.push_back(TemplateParams->size());
3835 for (TemplateParameterList::const_iterator
3836 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3837 P != PEnd; ++P)
3838 AddDeclRef(*P, Record);
3839}
3840
3841/// \brief Emit a template argument list.
3842void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003843ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003844 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003845 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003846 Record.push_back(TemplateArgs->size());
3847 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003848 AddTemplateArgument(TemplateArgs->get(i), Record);
3849}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003850
3851
3852void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003853ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003854 Record.push_back(Set.size());
3855 for (UnresolvedSetImpl::const_iterator
3856 I = Set.begin(), E = Set.end(); I != E; ++I) {
3857 AddDeclRef(I.getDecl(), Record);
3858 Record.push_back(I.getAccess());
3859 }
3860}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003861
Sebastian Redla4232eb2010-08-18 23:56:21 +00003862void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003863 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003864 Record.push_back(Base.isVirtual());
3865 Record.push_back(Base.isBaseOfClass());
3866 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003867 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003868 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003869 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003870 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3871 : SourceLocation(),
3872 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003873}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003874
Douglas Gregor7c789c12010-10-29 22:39:52 +00003875void ASTWriter::FlushCXXBaseSpecifiers() {
3876 RecordData Record;
3877 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3878 Record.clear();
3879
3880 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00003881 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003882 if (Index == CXXBaseSpecifiersOffsets.size())
3883 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3884 else {
3885 if (Index > CXXBaseSpecifiersOffsets.size())
3886 CXXBaseSpecifiersOffsets.resize(Index + 1);
3887 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3888 }
3889
3890 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3891 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3892 Record.push_back(BEnd - B);
3893 for (; B != BEnd; ++B)
3894 AddCXXBaseSpecifier(*B, Record);
3895 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003896
3897 // Flush any expressions that were written as part of the base specifiers.
3898 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003899 }
3900
3901 CXXBaseSpecifiersToWrite.clear();
3902}
3903
Sean Huntcbb67482011-01-08 20:30:50 +00003904void ASTWriter::AddCXXCtorInitializers(
3905 const CXXCtorInitializer * const *CtorInitializers,
3906 unsigned NumCtorInitializers,
3907 RecordDataImpl &Record) {
3908 Record.push_back(NumCtorInitializers);
3909 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3910 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003911
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003912 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00003913 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003914 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3915 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00003916 } else if (Init->isDelegatingInitializer()) {
3917 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3918 AddDeclRef(Init->getTargetConstructor(), Record);
3919 } else if (Init->isMemberInitializer()){
3920 Record.push_back(CTOR_INITIALIZER_MEMBER);
3921 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003922 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00003923 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3924 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003925 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003926
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003927 AddSourceLocation(Init->getMemberLocation(), Record);
3928 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003929 AddSourceLocation(Init->getLParenLoc(), Record);
3930 AddSourceLocation(Init->getRParenLoc(), Record);
3931 Record.push_back(Init->isWritten());
3932 if (Init->isWritten()) {
3933 Record.push_back(Init->getSourceOrder());
3934 } else {
3935 Record.push_back(Init->getNumArrayIndices());
3936 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3937 AddDeclRef(Init->getArrayIndex(i), Record);
3938 }
3939 }
3940}
3941
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003942void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3943 assert(D->DefinitionData);
3944 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3945 Record.push_back(Data.UserDeclaredConstructor);
3946 Record.push_back(Data.UserDeclaredCopyConstructor);
3947 Record.push_back(Data.UserDeclaredCopyAssignment);
3948 Record.push_back(Data.UserDeclaredDestructor);
3949 Record.push_back(Data.Aggregate);
3950 Record.push_back(Data.PlainOldData);
3951 Record.push_back(Data.Empty);
3952 Record.push_back(Data.Polymorphic);
3953 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00003954 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00003955 Record.push_back(Data.HasNoNonEmptyBases);
3956 Record.push_back(Data.HasPrivateFields);
3957 Record.push_back(Data.HasProtectedFields);
3958 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00003959 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00003960 Record.push_back(Data.HasTrivialDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003961 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003962 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003963 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003964 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003965 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003966 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003967 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003968 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00003969 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003970 Record.push_back(Data.DeclaredDefaultConstructor);
3971 Record.push_back(Data.DeclaredCopyConstructor);
3972 Record.push_back(Data.DeclaredCopyAssignment);
3973 Record.push_back(Data.DeclaredDestructor);
3974
3975 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003976 if (Data.NumBases > 0)
3977 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3978 Record);
3979
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003980 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3981 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003982 if (Data.NumVBases > 0)
3983 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3984 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003985
3986 AddUnresolvedSet(Data.Conversions, Record);
3987 AddUnresolvedSet(Data.VisibleConversions, Record);
3988 // Data.Definition is the owning decl, no need to write it.
3989 AddDeclRef(Data.FirstFriend, Record);
3990}
3991
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003992void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003993 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003994 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003995 assert(FirstDeclID == NextDeclID &&
3996 FirstTypeID == NextTypeID &&
3997 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003998 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003999 FirstMacroID == NextMacroID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004000 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004001
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004002 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004003
4004 FirstDeclID += Chain->getTotalNumDecls();
4005 FirstTypeID += Chain->getTotalNumTypes();
4006 FirstIdentID += Chain->getTotalNumIdentifiers();
4007 FirstSelectorID += Chain->getTotalNumSelectors();
4008 FirstMacroID += Chain->getTotalNumMacroDefinitions();
4009 NextDeclID = FirstDeclID;
4010 NextTypeID = FirstTypeID;
4011 NextIdentID = FirstIdentID;
4012 NextSelectorID = FirstSelectorID;
4013 NextMacroID = FirstMacroID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004014}
4015
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004016void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004017 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004018 if (II->hasMacroDefinition())
4019 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004020}
4021
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004022void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004023 // Always take the highest-numbered type index. This copes with an interesting
4024 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004025 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004026 // keep the higher-numbered entry so that we can properly write it out to
4027 // the AST file.
4028 TypeIdx &StoredIdx = TypeIdxs[T];
4029 if (Idx.getIndex() >= StoredIdx.getIndex())
4030 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004031}
4032
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004033void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00004034 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004035}
Sebastian Redl5d050072010-08-04 17:20:04 +00004036
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004037void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004038 SelectorIDs[S] = ID;
4039}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004040
Michael J. Spencer20249a12010-10-21 03:16:25 +00004041void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004042 MacroDefinition *MD) {
4043 MacroDefinitions[MD] = ID;
4044}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004045
4046void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
4047 assert(D->isDefinition());
4048 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4049 // We are interested when a PCH decl is modified.
4050 if (RD->getPCHLevel() > 0) {
4051 // A forward reference was mutated into a definition. Rewrite it.
4052 // FIXME: This happens during template instantiation, should we
4053 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004054 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004055 }
4056
4057 for (CXXRecordDecl::redecl_iterator
4058 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
4059 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
4060 if (Redecl == RD)
4061 continue;
4062
4063 // We are interested when a PCH decl is modified.
4064 if (Redecl->getPCHLevel() > 0) {
4065 UpdateRecord &Record = DeclUpdates[Redecl];
4066 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
4067 assert(Redecl->DefinitionData);
4068 assert(Redecl->DefinitionData->Definition == D);
4069 AddDeclRef(D, Record); // the DefinitionDecl
4070 }
4071 }
4072 }
4073}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004074void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
4075 // TU and namespaces are handled elsewhere.
4076 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4077 return;
4078
4079 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
4080 return; // Not a source decl added to a DeclContext from PCH.
4081
4082 AddUpdatedDeclContext(DC);
4083}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004084
4085void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
4086 assert(D->isImplicit());
4087 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
4088 return; // Not a source member added to a class from PCH.
4089 if (!isa<CXXMethodDecl>(D))
4090 return; // We are interested in lazily declared implicit methods.
4091
4092 // A decl coming from PCH was modified.
4093 assert(RD->isDefinition());
4094 UpdateRecord &Record = DeclUpdates[RD];
4095 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
4096 AddDeclRef(D, Record);
4097}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004098
4099void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4100 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004101 // The specializations set is kept in the canonical template.
4102 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004103 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4104 return; // Not a source specialization added to a template from PCH.
4105
4106 UpdateRecord &Record = DeclUpdates[TD];
4107 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4108 AddDeclRef(D, Record);
4109}
Douglas Gregor89d99802010-11-30 06:16:57 +00004110
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004111void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4112 const FunctionDecl *D) {
4113 // The specializations set is kept in the canonical template.
4114 TD = TD->getCanonicalDecl();
4115 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4116 return; // Not a source specialization added to a template from PCH.
4117
4118 UpdateRecord &Record = DeclUpdates[TD];
4119 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4120 AddDeclRef(D, Record);
4121}
4122
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004123void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4124 if (D->getPCHLevel() == 0)
4125 return; // Declaration not imported from PCH.
4126
4127 // Implicit decl from a PCH was defined.
4128 // FIXME: Should implicit definition be a separate FunctionDecl?
4129 RewriteDecl(D);
4130}
4131
Sebastian Redlf79a7192011-04-29 08:19:30 +00004132void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4133 if (D->getPCHLevel() == 0)
4134 return;
4135
4136 // Since the actual instantiation is delayed, this really means that we need
4137 // to update the instantiation location.
4138 UpdateRecord &Record = DeclUpdates[D];
4139 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4140 AddSourceLocation(
4141 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4142}
4143
Douglas Gregor89d99802010-11-30 06:16:57 +00004144ASTSerializationListener::~ASTSerializationListener() { }