blob: 482ce4bec7c763a0c3dbf1dd18c63ebad84148de [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"
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000050using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000051using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000052
Sebastian Redlade50002010-07-30 17:03:48 +000053template <typename T, typename Allocator>
54T *data(std::vector<T, Allocator> &v) {
55 return v.empty() ? 0 : &v.front();
56}
57template <typename T, typename Allocator>
58const T *data(const std::vector<T, Allocator> &v) {
59 return v.empty() ? 0 : &v.front();
60}
61
Douglas Gregor2cf26342009-04-09 22:27:44 +000062//===----------------------------------------------------------------------===//
63// Type serialization
64//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000065
Douglas Gregor2cf26342009-04-09 22:27:44 +000066namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000067 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000068 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000069 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000070
71 public:
72 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000073 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000074
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000075 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000076 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000077
78 void VisitArrayType(const ArrayType *T);
79 void VisitFunctionType(const FunctionType *T);
80 void VisitTagType(const TagType *T);
81
82#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
83#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000084#include "clang/AST/TypeNodes.def"
85 };
86}
87
Sebastian Redl3397c552010-08-18 23:56:27 +000088void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000089 assert(false && "Built-in types are never serialized");
90}
91
Sebastian Redl3397c552010-08-18 23:56:27 +000092void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000093 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000094 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000114 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000115}
116
Sebastian Redl3397c552010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000118 Writer.AddTypeRef(T->getPointeeType(), Record);
119 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000124 Writer.AddTypeRef(T->getElementType(), Record);
125 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000126 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 VisitArrayType(T);
131 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000132 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000137 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000138}
139
Sebastian Redl3397c552010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000141 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000142 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
143 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000144 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000145 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146}
147
Sebastian Redl3397c552010-08-18 23:56:27 +0000148void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000149 Writer.AddTypeRef(T->getElementType(), Record);
150 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000151 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000152 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153}
154
Sebastian Redl3397c552010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000157 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000158}
159
Sebastian Redl3397c552010-08-18 23:56:27 +0000160void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000161 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000162 FunctionType::ExtInfo C = T->getExtInfo();
163 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000164 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000165 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000166 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167}
168
Sebastian Redl3397c552010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000170 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000171 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000172}
173
Sebastian Redl3397c552010-08-18 23:56:27 +0000174void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175 VisitFunctionType(T);
176 Record.push_back(T->getNumArgs());
177 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
178 Writer.AddTypeRef(T->getArgType(I), Record);
179 Record.push_back(T->isVariadic());
180 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000181 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000182 Record.push_back(T->getExceptionSpecType());
183 if (T->getExceptionSpecType() == EST_Dynamic) {
184 Record.push_back(T->getNumExceptions());
185 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
186 Writer.AddTypeRef(T->getExceptionType(I), Record);
187 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
188 Writer.AddStmt(T->getNoexceptExpr());
189 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000190 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000191}
192
Sebastian Redl3397c552010-08-18 23:56:27 +0000193void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000194 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000195 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000196}
John McCalled976492009-12-04 22:46:56 +0000197
Sebastian Redl3397c552010-08-18 23:56:27 +0000198void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000200 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
201 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000202 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000203}
204
Sebastian Redl3397c552010-08-18 23:56:27 +0000205void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000206 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000207 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208}
209
Sebastian Redl3397c552010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000211 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000212 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000213}
214
Sebastian Redl3397c552010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000216 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000217 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000218}
219
Richard Smith34b41d92011-02-20 03:19:35 +0000220void ASTTypeWriter::VisitAutoType(const AutoType *T) {
221 Writer.AddTypeRef(T->getDeducedType(), Record);
222 Code = TYPE_AUTO;
223}
224
Sebastian Redl3397c552010-08-18 23:56:27 +0000225void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000226 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000228 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229 "Cannot serialize in the middle of a type definition");
230}
231
Sebastian Redl3397c552010-08-18 23:56:27 +0000232void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000233 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000234 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000235}
236
Sebastian Redl3397c552010-08-18 23:56:27 +0000237void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000238 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000239 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000240}
241
John McCall9d156a72011-01-06 01:58:22 +0000242void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
243 Writer.AddTypeRef(T->getModifiedType(), Record);
244 Writer.AddTypeRef(T->getEquivalentType(), Record);
245 Record.push_back(T->getAttrKind());
246 Code = TYPE_ATTRIBUTED;
247}
248
Mike Stump1eb44332009-09-09 15:08:12 +0000249void
Sebastian Redl3397c552010-08-18 23:56:27 +0000250ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000251 const SubstTemplateTypeParmType *T) {
252 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
253 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000254 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000255}
256
257void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000258ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
259 const SubstTemplateTypeParmPackType *T) {
260 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
261 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
262 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
263}
264
265void
Sebastian Redl3397c552010-08-18 23:56:27 +0000266ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000267 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000268 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000269 Writer.AddTemplateName(T->getTemplateName(), Record);
270 Record.push_back(T->getNumArgs());
271 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
272 ArgI != ArgE; ++ArgI)
273 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000274 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
275 : T->getCanonicalTypeInternal(),
276 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000277 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000278}
279
280void
Sebastian Redl3397c552010-08-18 23:56:27 +0000281ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000282 VisitArrayType(T);
283 Writer.AddStmt(T->getSizeExpr());
284 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000285 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000286}
287
288void
Sebastian Redl3397c552010-08-18 23:56:27 +0000289ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000290 const DependentSizedExtVectorType *T) {
291 // FIXME: Serialize this type (C++ only)
292 assert(false && "Cannot serialize dependent sized extended vector types");
293}
294
295void
Sebastian Redl3397c552010-08-18 23:56:27 +0000296ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000297 Record.push_back(T->getDepth());
298 Record.push_back(T->getIndex());
299 Record.push_back(T->isParameterPack());
300 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl3397c552010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000306 Record.push_back(T->getKeyword());
307 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
308 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000309 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
310 : T->getCanonicalTypeInternal(),
311 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000312 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313}
314
315void
Sebastian Redl3397c552010-08-18 23:56:27 +0000316ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000318 Record.push_back(T->getKeyword());
319 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
320 Writer.AddIdentifierRef(T->getIdentifier(), Record);
321 Record.push_back(T->getNumArgs());
322 for (DependentTemplateSpecializationType::iterator
323 I = T->begin(), E = T->end(); I != E; ++I)
324 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000325 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000326}
327
Douglas Gregor7536dd52010-12-20 02:24:11 +0000328void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
329 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000330 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
331 Record.push_back(*NumExpansions + 1);
332 else
333 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000334 Code = TYPE_PACK_EXPANSION;
335}
336
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000337void ASTTypeWriter::VisitParenType(const ParenType *T) {
338 Writer.AddTypeRef(T->getInnerType(), Record);
339 Code = TYPE_PAREN;
340}
341
Sebastian Redl3397c552010-08-18 23:56:27 +0000342void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000343 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000344 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
345 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000346 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000347}
348
Sebastian Redl3397c552010-08-18 23:56:27 +0000349void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000350 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000351 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000352 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000353}
354
Sebastian Redl3397c552010-08-18 23:56:27 +0000355void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000356 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000357 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000358}
359
Sebastian Redl3397c552010-08-18 23:56:27 +0000360void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000361 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000362 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000363 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000364 E = T->qual_end(); I != E; ++I)
365 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000366 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000367}
368
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000369void
Sebastian Redl3397c552010-08-18 23:56:27 +0000370ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000371 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000372 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000373}
374
John McCalla1ee0c52009-10-16 21:56:05 +0000375namespace {
376
377class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000378 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000379 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000380
381public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000382 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000383 : Writer(Writer), Record(Record) { }
384
John McCall51bd8032009-10-18 01:05:36 +0000385#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000386#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000387 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000388#include "clang/AST/TypeLocNodes.def"
389
John McCall51bd8032009-10-18 01:05:36 +0000390 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
391 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000392};
393
394}
395
John McCall51bd8032009-10-18 01:05:36 +0000396void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
397 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000398}
John McCall51bd8032009-10-18 01:05:36 +0000399void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000400 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
401 if (TL.needsExtraLocalData()) {
402 Record.push_back(TL.getWrittenTypeSpec());
403 Record.push_back(TL.getWrittenSignSpec());
404 Record.push_back(TL.getWrittenWidthSpec());
405 Record.push_back(TL.hasModeAttr());
406 }
John McCalla1ee0c52009-10-16 21:56:05 +0000407}
John McCall51bd8032009-10-18 01:05:36 +0000408void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000410}
John McCall51bd8032009-10-18 01:05:36 +0000411void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000413}
John McCall51bd8032009-10-18 01:05:36 +0000414void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000416}
John McCall51bd8032009-10-18 01:05:36 +0000417void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000419}
John McCall51bd8032009-10-18 01:05:36 +0000420void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000422}
John McCall51bd8032009-10-18 01:05:36 +0000423void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000425 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000426}
John McCall51bd8032009-10-18 01:05:36 +0000427void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
428 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
429 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
430 Record.push_back(TL.getSizeExpr() ? 1 : 0);
431 if (TL.getSizeExpr())
432 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000433}
John McCall51bd8032009-10-18 01:05:36 +0000434void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
435 VisitArrayTypeLoc(TL);
436}
437void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
438 VisitArrayTypeLoc(TL);
439}
440void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
441 VisitArrayTypeLoc(TL);
442}
443void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
444 DependentSizedArrayTypeLoc TL) {
445 VisitArrayTypeLoc(TL);
446}
447void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
448 DependentSizedExtVectorTypeLoc TL) {
449 Writer.AddSourceLocation(TL.getNameLoc(), Record);
450}
451void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
452 Writer.AddSourceLocation(TL.getNameLoc(), Record);
453}
454void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
456}
457void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000458 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
459 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000460 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000461 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
462 Writer.AddDeclRef(TL.getArg(i), Record);
463}
464void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
465 VisitFunctionTypeLoc(TL);
466}
467void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
468 VisitFunctionTypeLoc(TL);
469}
John McCalled976492009-12-04 22:46:56 +0000470void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
John McCall51bd8032009-10-18 01:05:36 +0000473void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getNameLoc(), Record);
475}
476void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000477 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
478 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
479 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000480}
481void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000482 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
483 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
484 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
485 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000486}
487void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
Richard Smith34b41d92011-02-20 03:19:35 +0000490void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getNameLoc(), Record);
492}
John McCall51bd8032009-10-18 01:05:36 +0000493void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getNameLoc(), Record);
495}
496void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getNameLoc(), Record);
498}
John McCall9d156a72011-01-06 01:58:22 +0000499void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
501 if (TL.hasAttrOperand()) {
502 SourceRange range = TL.getAttrOperandParensRange();
503 Writer.AddSourceLocation(range.getBegin(), Record);
504 Writer.AddSourceLocation(range.getEnd(), Record);
505 }
506 if (TL.hasAttrExprOperand()) {
507 Expr *operand = TL.getAttrExprOperand();
508 Record.push_back(operand ? 1 : 0);
509 if (operand) Writer.AddStmt(operand);
510 } else if (TL.hasAttrEnumOperand()) {
511 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
512 }
513}
John McCall51bd8032009-10-18 01:05:36 +0000514void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getNameLoc(), Record);
516}
John McCall49a832b2009-10-18 09:09:24 +0000517void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
518 SubstTemplateTypeParmTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000521void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
522 SubstTemplateTypeParmPackTypeLoc TL) {
523 Writer.AddSourceLocation(TL.getNameLoc(), Record);
524}
John McCall51bd8032009-10-18 01:05:36 +0000525void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
526 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000527 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
528 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
529 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
530 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000531 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
532 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000533}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000534void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
536 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
537}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000538void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000539 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000540 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000541}
John McCall3cb0ebd2010-03-10 03:28:59 +0000542void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
Douglas Gregor4714c122010-03-31 17:34:00 +0000545void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000546 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000547 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
John McCall33500952010-06-11 00:33:02 +0000550void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
551 DependentTemplateSpecializationTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000553 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
556 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
557 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000558 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
559 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000560}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000561void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
563}
John McCall51bd8032009-10-18 01:05:36 +0000564void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
565 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000566}
567void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
568 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000569 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
570 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
571 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
572 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000573}
John McCall54e14c42009-10-22 22:37:11 +0000574void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000576}
John McCalla1ee0c52009-10-16 21:56:05 +0000577
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000578//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000579// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000580//===----------------------------------------------------------------------===//
581
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000582static void EmitBlockID(unsigned ID, const char *Name,
583 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000584 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000585 Record.clear();
586 Record.push_back(ID);
587 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
588
589 // Emit the block name if present.
590 if (Name == 0 || Name[0] == 0) return;
591 Record.clear();
592 while (*Name)
593 Record.push_back(*Name++);
594 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
595}
596
597static void EmitRecordID(unsigned ID, const char *Name,
598 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000599 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000600 Record.clear();
601 Record.push_back(ID);
602 while (*Name)
603 Record.push_back(*Name++);
604 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000605}
606
607static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000608 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000609#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000610 RECORD(STMT_STOP);
611 RECORD(STMT_NULL_PTR);
612 RECORD(STMT_NULL);
613 RECORD(STMT_COMPOUND);
614 RECORD(STMT_CASE);
615 RECORD(STMT_DEFAULT);
616 RECORD(STMT_LABEL);
617 RECORD(STMT_IF);
618 RECORD(STMT_SWITCH);
619 RECORD(STMT_WHILE);
620 RECORD(STMT_DO);
621 RECORD(STMT_FOR);
622 RECORD(STMT_GOTO);
623 RECORD(STMT_INDIRECT_GOTO);
624 RECORD(STMT_CONTINUE);
625 RECORD(STMT_BREAK);
626 RECORD(STMT_RETURN);
627 RECORD(STMT_DECL);
628 RECORD(STMT_ASM);
629 RECORD(EXPR_PREDEFINED);
630 RECORD(EXPR_DECL_REF);
631 RECORD(EXPR_INTEGER_LITERAL);
632 RECORD(EXPR_FLOATING_LITERAL);
633 RECORD(EXPR_IMAGINARY_LITERAL);
634 RECORD(EXPR_STRING_LITERAL);
635 RECORD(EXPR_CHARACTER_LITERAL);
636 RECORD(EXPR_PAREN);
637 RECORD(EXPR_UNARY_OPERATOR);
638 RECORD(EXPR_SIZEOF_ALIGN_OF);
639 RECORD(EXPR_ARRAY_SUBSCRIPT);
640 RECORD(EXPR_CALL);
641 RECORD(EXPR_MEMBER);
642 RECORD(EXPR_BINARY_OPERATOR);
643 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
644 RECORD(EXPR_CONDITIONAL_OPERATOR);
645 RECORD(EXPR_IMPLICIT_CAST);
646 RECORD(EXPR_CSTYLE_CAST);
647 RECORD(EXPR_COMPOUND_LITERAL);
648 RECORD(EXPR_EXT_VECTOR_ELEMENT);
649 RECORD(EXPR_INIT_LIST);
650 RECORD(EXPR_DESIGNATED_INIT);
651 RECORD(EXPR_IMPLICIT_VALUE_INIT);
652 RECORD(EXPR_VA_ARG);
653 RECORD(EXPR_ADDR_LABEL);
654 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000655 RECORD(EXPR_CHOOSE);
656 RECORD(EXPR_GNU_NULL);
657 RECORD(EXPR_SHUFFLE_VECTOR);
658 RECORD(EXPR_BLOCK);
659 RECORD(EXPR_BLOCK_DECL_REF);
660 RECORD(EXPR_OBJC_STRING_LITERAL);
661 RECORD(EXPR_OBJC_ENCODE);
662 RECORD(EXPR_OBJC_SELECTOR_EXPR);
663 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
664 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
665 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
666 RECORD(EXPR_OBJC_KVC_REF_EXPR);
667 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000668 RECORD(STMT_OBJC_FOR_COLLECTION);
669 RECORD(STMT_OBJC_CATCH);
670 RECORD(STMT_OBJC_FINALLY);
671 RECORD(STMT_OBJC_AT_TRY);
672 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
673 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000674 RECORD(EXPR_CXX_OPERATOR_CALL);
675 RECORD(EXPR_CXX_CONSTRUCT);
676 RECORD(EXPR_CXX_STATIC_CAST);
677 RECORD(EXPR_CXX_DYNAMIC_CAST);
678 RECORD(EXPR_CXX_REINTERPRET_CAST);
679 RECORD(EXPR_CXX_CONST_CAST);
680 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
681 RECORD(EXPR_CXX_BOOL_LITERAL);
682 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000683 RECORD(EXPR_CXX_TYPEID_EXPR);
684 RECORD(EXPR_CXX_TYPEID_TYPE);
685 RECORD(EXPR_CXX_UUIDOF_EXPR);
686 RECORD(EXPR_CXX_UUIDOF_TYPE);
687 RECORD(EXPR_CXX_THIS);
688 RECORD(EXPR_CXX_THROW);
689 RECORD(EXPR_CXX_DEFAULT_ARG);
690 RECORD(EXPR_CXX_BIND_TEMPORARY);
691 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
692 RECORD(EXPR_CXX_NEW);
693 RECORD(EXPR_CXX_DELETE);
694 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
695 RECORD(EXPR_EXPR_WITH_CLEANUPS);
696 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
697 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
698 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
699 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
700 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
701 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
702 RECORD(EXPR_CXX_NOEXCEPT);
703 RECORD(EXPR_OPAQUE_VALUE);
704 RECORD(EXPR_BINARY_TYPE_TRAIT);
705 RECORD(EXPR_PACK_EXPANSION);
706 RECORD(EXPR_SIZEOF_PACK);
707 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000708 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000709#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000710}
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Sebastian Redla4232eb2010-08-18 23:56:21 +0000712void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000713 RecordData Record;
714 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000716#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
717#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Sebastian Redl3397c552010-08-18 23:56:27 +0000719 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000720 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000721 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000722 RECORD(TYPE_OFFSET);
723 RECORD(DECL_OFFSET);
724 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000725 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000726 RECORD(IDENTIFIER_OFFSET);
727 RECORD(IDENTIFIER_TABLE);
728 RECORD(EXTERNAL_DEFINITIONS);
729 RECORD(SPECIAL_TYPES);
730 RECORD(STATISTICS);
731 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000732 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000733 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
734 RECORD(SELECTOR_OFFSETS);
735 RECORD(METHOD_POOL);
736 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000737 RECORD(SOURCE_LOCATION_OFFSETS);
738 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000739 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000740 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000741 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000742 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000743 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000744 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000745 RECORD(TU_UPDATE_LEXICAL);
746 RECORD(REDECLS_UPDATE_LATEST);
747 RECORD(SEMA_DECL_REFS);
748 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
749 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
750 RECORD(DECL_REPLACEMENTS);
751 RECORD(UPDATE_VISIBLE);
752 RECORD(DECL_UPDATE_OFFSETS);
753 RECORD(DECL_UPDATES);
754 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
755 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000756 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000757 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000758 RECORD(FP_PRAGMA_OPTIONS);
759 RECORD(OPENCL_EXTENSIONS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000760
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000762 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000763 RECORD(SM_SLOC_FILE_ENTRY);
764 RECORD(SM_SLOC_BUFFER_ENTRY);
765 RECORD(SM_SLOC_BUFFER_BLOB);
766 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
767 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000769 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000770 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000771 RECORD(PP_MACRO_OBJECT_LIKE);
772 RECORD(PP_MACRO_FUNCTION_LIKE);
773 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000774
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000775 // Decls and Types block.
776 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000777 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000778 RECORD(TYPE_COMPLEX);
779 RECORD(TYPE_POINTER);
780 RECORD(TYPE_BLOCK_POINTER);
781 RECORD(TYPE_LVALUE_REFERENCE);
782 RECORD(TYPE_RVALUE_REFERENCE);
783 RECORD(TYPE_MEMBER_POINTER);
784 RECORD(TYPE_CONSTANT_ARRAY);
785 RECORD(TYPE_INCOMPLETE_ARRAY);
786 RECORD(TYPE_VARIABLE_ARRAY);
787 RECORD(TYPE_VECTOR);
788 RECORD(TYPE_EXT_VECTOR);
789 RECORD(TYPE_FUNCTION_PROTO);
790 RECORD(TYPE_FUNCTION_NO_PROTO);
791 RECORD(TYPE_TYPEDEF);
792 RECORD(TYPE_TYPEOF_EXPR);
793 RECORD(TYPE_TYPEOF);
794 RECORD(TYPE_RECORD);
795 RECORD(TYPE_ENUM);
796 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000797 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000798 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000799 RECORD(TYPE_DECLTYPE);
800 RECORD(TYPE_ELABORATED);
801 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
802 RECORD(TYPE_UNRESOLVED_USING);
803 RECORD(TYPE_INJECTED_CLASS_NAME);
804 RECORD(TYPE_OBJC_OBJECT);
805 RECORD(TYPE_TEMPLATE_TYPE_PARM);
806 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
807 RECORD(TYPE_DEPENDENT_NAME);
808 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
809 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
810 RECORD(TYPE_PAREN);
811 RECORD(TYPE_PACK_EXPANSION);
812 RECORD(TYPE_ATTRIBUTED);
813 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000814 RECORD(DECL_TRANSLATION_UNIT);
815 RECORD(DECL_TYPEDEF);
816 RECORD(DECL_ENUM);
817 RECORD(DECL_RECORD);
818 RECORD(DECL_ENUM_CONSTANT);
819 RECORD(DECL_FUNCTION);
820 RECORD(DECL_OBJC_METHOD);
821 RECORD(DECL_OBJC_INTERFACE);
822 RECORD(DECL_OBJC_PROTOCOL);
823 RECORD(DECL_OBJC_IVAR);
824 RECORD(DECL_OBJC_AT_DEFS_FIELD);
825 RECORD(DECL_OBJC_CLASS);
826 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
827 RECORD(DECL_OBJC_CATEGORY);
828 RECORD(DECL_OBJC_CATEGORY_IMPL);
829 RECORD(DECL_OBJC_IMPLEMENTATION);
830 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
831 RECORD(DECL_OBJC_PROPERTY);
832 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000833 RECORD(DECL_FIELD);
834 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000835 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000836 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000837 RECORD(DECL_FILE_SCOPE_ASM);
838 RECORD(DECL_BLOCK);
839 RECORD(DECL_CONTEXT_LEXICAL);
840 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000841 RECORD(DECL_NAMESPACE);
842 RECORD(DECL_NAMESPACE_ALIAS);
843 RECORD(DECL_USING);
844 RECORD(DECL_USING_SHADOW);
845 RECORD(DECL_USING_DIRECTIVE);
846 RECORD(DECL_UNRESOLVED_USING_VALUE);
847 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
848 RECORD(DECL_LINKAGE_SPEC);
849 RECORD(DECL_CXX_RECORD);
850 RECORD(DECL_CXX_METHOD);
851 RECORD(DECL_CXX_CONSTRUCTOR);
852 RECORD(DECL_CXX_DESTRUCTOR);
853 RECORD(DECL_CXX_CONVERSION);
854 RECORD(DECL_ACCESS_SPEC);
855 RECORD(DECL_FRIEND);
856 RECORD(DECL_FRIEND_TEMPLATE);
857 RECORD(DECL_CLASS_TEMPLATE);
858 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
859 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
860 RECORD(DECL_FUNCTION_TEMPLATE);
861 RECORD(DECL_TEMPLATE_TYPE_PARM);
862 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
863 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
864 RECORD(DECL_STATIC_ASSERT);
865 RECORD(DECL_CXX_BASE_SPECIFIERS);
866 RECORD(DECL_INDIRECTFIELD);
867 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
868
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000869 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
870 RECORD(PPD_MACRO_INSTANTIATION);
871 RECORD(PPD_MACRO_DEFINITION);
872 RECORD(PPD_INCLUSION_DIRECTIVE);
873
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000874 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000875 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000876#undef RECORD
877#undef BLOCK
878 Stream.ExitBlock();
879}
880
Douglas Gregore650c8c2009-07-07 00:12:59 +0000881/// \brief Adjusts the given filename to only write out the portion of the
882/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000883///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000884/// \param Filename the file name to adjust.
885///
886/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
887/// the returned filename will be adjusted by this system root.
888///
889/// \returns either the original filename (if it needs no adjustment) or the
890/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000891static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000892adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
893 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Douglas Gregore650c8c2009-07-07 00:12:59 +0000895 if (!isysroot)
896 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Douglas Gregore650c8c2009-07-07 00:12:59 +0000898 // Verify that the filename and the system root have the same prefix.
899 unsigned Pos = 0;
900 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
901 if (Filename[Pos] != isysroot[Pos])
902 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregore650c8c2009-07-07 00:12:59 +0000904 // We hit the end of the filename before we hit the end of the system root.
905 if (!Filename[Pos])
906 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Douglas Gregore650c8c2009-07-07 00:12:59 +0000908 // If the file name has a '/' at the current position, skip over the '/'.
909 // We distinguish sysroot-based includes from absolute includes by the
910 // absence of '/' at the beginning of sysroot-based includes.
911 if (Filename[Pos] == '/')
912 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregore650c8c2009-07-07 00:12:59 +0000914 return Filename + Pos;
915}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000916
Sebastian Redl3397c552010-08-18 23:56:27 +0000917/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000918void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
919 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000920 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000921
Douglas Gregore650c8c2009-07-07 00:12:59 +0000922 // Metadata
923 const TargetInfo &Target = Context.Target;
924 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000925 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000926 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000927 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
928 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000929 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
930 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
931 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000932 // Target triple or chained PCH name
933 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000934 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregore650c8c2009-07-07 00:12:59 +0000936 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000937 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
938 Record.push_back(VERSION_MAJOR);
939 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000940 Record.push_back(CLANG_VERSION_MAJOR);
941 Record.push_back(CLANG_VERSION_MINOR);
942 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000943 // FIXME: This writes the absolute path for chained headers.
944 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
945 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Douglas Gregorb64c1932009-05-12 01:31:05 +0000947 // Original file name
948 SourceManager &SM = Context.getSourceManager();
949 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
950 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000951 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000952 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
953 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
954
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000955 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000957 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000958
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000959 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000960 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000962 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000963 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000964 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000965 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000966
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000967 // Original PCH directory
968 if (!OutputFile.empty() && OutputFile != "-") {
969 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
970 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
972 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
973
974 llvm::SmallString<128> OutputPath(OutputFile);
975
976 llvm::sys::fs::make_absolute(OutputPath);
977 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
978
979 RecordData Record;
980 Record.push_back(ORIGINAL_PCH_DIR);
981 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
982 }
983
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000984 // Repository branch/version information.
985 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000986 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000987 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
988 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000989 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000990 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000991 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
992 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000993}
994
995/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000996void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000997 RecordData Record;
998 Record.push_back(LangOpts.Trigraphs);
999 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1000 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1001 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1002 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001003 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001004 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1005 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1006 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1007 Record.push_back(LangOpts.C99); // C99 Support
1008 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001009 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1010 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001011 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1012 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001013 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001015 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1016 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001017 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001018 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001019 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001020 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001021 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001022 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1023 // properties enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001024 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001026 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001027 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1028 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001029 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001030 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001031 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001032 Record.push_back(LangOpts.CXXExceptions);
1033 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001034
Douglas Gregor6f755502011-02-01 15:15:22 +00001035 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001036 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1037 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1038 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1039
Chris Lattnerea5ce472009-04-27 07:35:58 +00001040 // Whether static initializers are protected by locks.
1041 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001042 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001043 Record.push_back(LangOpts.Blocks); // block extension to C
1044 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1045 // they are unused.
1046 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1047 // (modulo the platform support).
1048
Chris Lattnera4d71452010-06-26 21:25:03 +00001049 Record.push_back(LangOpts.getSignedOverflowBehavior());
1050 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001051
1052 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001053 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001054 // defined.
1055 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1056 // opposed to __DYNAMIC__).
1057 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1058
1059 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1060 // used (instead of C99 semantics).
1061 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001062 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1063 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001064 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1065 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001066 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001067 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1068 // to the smallest integer type with
1069 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001070 Record.push_back(LangOpts.getGCMode());
1071 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001072 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001073 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001074 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001075 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001076 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001077 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001078 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001079 Record.push_back(LangOpts.SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00001080 Record.push_back(LangOpts.MRTD);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001081 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001082}
1083
Douglas Gregor14f79002009-04-10 03:52:48 +00001084//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001085// stat cache Serialization
1086//===----------------------------------------------------------------------===//
1087
1088namespace {
1089// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001090class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001091public:
1092 typedef const char * key_type;
1093 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner74e976b2010-11-23 19:28:12 +00001095 typedef struct stat data_type;
1096 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001097
1098 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001099 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
1102 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001103 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1104 data_type_ref Data) {
1105 unsigned StrLen = strlen(path);
1106 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001107 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001108 clang::io::Emit8(Out, DataLen);
1109 return std::make_pair(StrLen + 1, DataLen);
1110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001112 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1113 Out.write(path, KeyLen);
1114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Chris Lattner74e976b2010-11-23 19:28:12 +00001116 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001117 data_type_ref Data, unsigned DataLen) {
1118 using namespace clang::io;
1119 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Chris Lattner74e976b2010-11-23 19:28:12 +00001121 Emit32(Out, (uint32_t) Data.st_ino);
1122 Emit32(Out, (uint32_t) Data.st_dev);
1123 Emit16(Out, (uint16_t) Data.st_mode);
1124 Emit64(Out, (uint64_t) Data.st_mtime);
1125 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001126
1127 assert(Out.tell() - Start == DataLen && "Wrong data length");
1128 }
1129};
1130} // end anonymous namespace
1131
Sebastian Redl3397c552010-08-18 23:56:27 +00001132/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001133void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001134 // Build the on-disk hash table containing information about every
1135 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001136 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001137 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001138 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001139 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001140 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1141 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001142 Generator.insert(Filename, Stat->second);
1143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001145 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001146 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001147 uint32_t BucketOffset;
1148 {
1149 llvm::raw_svector_ostream Out(StatCacheData);
1150 // Make sure that no bucket is at offset 0
1151 clang::io::Emit32(Out, 0);
1152 BucketOffset = Generator.Emit(Out);
1153 }
1154
1155 // Create a blob abbreviation
1156 using namespace llvm;
1157 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001158 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1162 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1163
1164 // Write the stat cache
1165 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001166 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001167 Record.push_back(BucketOffset);
1168 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001169 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001170}
1171
1172//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001173// Source Manager Serialization
1174//===----------------------------------------------------------------------===//
1175
1176/// \brief Create an abbreviation for the SLocEntry that refers to a
1177/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001178static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001179 using namespace llvm;
1180 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001181 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001186 // FileEntry fields.
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001190 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001191}
1192
1193/// \brief Create an abbreviation for the SLocEntry that refers to a
1194/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001195static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001196 using namespace llvm;
1197 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001198 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001204 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001205}
1206
1207/// \brief Create an abbreviation for the SLocEntry that refers to a
1208/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001209static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001210 using namespace llvm;
1211 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001212 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001214 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001215}
1216
1217/// \brief Create an abbreviation for the SLocEntry that refers to an
1218/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001219static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001228 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001229}
1230
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001231namespace {
1232 // Trait used for the on-disk hash table of header search information.
1233 class HeaderFileInfoTrait {
1234 ASTWriter &Writer;
1235 HeaderSearch &HS;
1236
1237 public:
1238 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1239 : Writer(Writer), HS(HS) { }
1240
1241 typedef const char *key_type;
1242 typedef key_type key_type_ref;
1243
1244 typedef HeaderFileInfo data_type;
1245 typedef const data_type &data_type_ref;
1246
1247 static unsigned ComputeHash(const char *path) {
1248 // The hash is based only on the filename portion of the key, so that the
1249 // reader can match based on filenames when symlinking or excess path
1250 // elements ("foo/../", "../") change the form of the name. However,
1251 // complete path is still the key.
1252 return llvm::HashString(llvm::sys::path::filename(path));
1253 }
1254
1255 std::pair<unsigned,unsigned>
1256 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1257 data_type_ref Data) {
1258 unsigned StrLen = strlen(path);
1259 clang::io::Emit16(Out, StrLen);
1260 unsigned DataLen = 1 + 2 + 4;
1261 clang::io::Emit8(Out, DataLen);
1262 return std::make_pair(StrLen + 1, DataLen);
1263 }
1264
1265 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1266 Out.write(path, KeyLen);
1267 }
1268
1269 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1270 data_type_ref Data, unsigned DataLen) {
1271 using namespace clang::io;
1272 uint64_t Start = Out.tell(); (void)Start;
1273
1274 unsigned char Flags = (Data.isImport << 3)
1275 | (Data.DirInfo << 1)
1276 | Data.Resolved;
1277 Emit8(Out, (uint8_t)Flags);
1278 Emit16(Out, (uint16_t) Data.NumIncludes);
1279
1280 if (!Data.ControllingMacro)
1281 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1282 else
1283 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1284 assert(Out.tell() - Start == DataLen && "Wrong data length");
1285 }
1286 };
1287} // end anonymous namespace
1288
1289/// \brief Write the header search block for the list of files that
1290///
1291/// \param HS The header search structure to save.
1292///
1293/// \param Chain Whether we're creating a chained AST file.
1294void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1295 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1296 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1297
1298 if (FilesByUID.size() > HS.header_file_size())
1299 FilesByUID.resize(HS.header_file_size());
1300
1301 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1302 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1303 llvm::SmallVector<const char *, 4> SavedStrings;
1304 unsigned NumHeaderSearchEntries = 0;
1305 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1306 const FileEntry *File = FilesByUID[UID];
1307 if (!File)
1308 continue;
1309
1310 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1311 if (HFI.External && Chain)
1312 continue;
1313
1314 // Turn the file name into an absolute path, if it isn't already.
1315 const char *Filename = File->getName();
1316 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1317
1318 // If we performed any translation on the file name at all, we need to
1319 // save this string, since the generator will refer to it later.
1320 if (Filename != File->getName()) {
1321 Filename = strdup(Filename);
1322 SavedStrings.push_back(Filename);
1323 }
1324
1325 Generator.insert(Filename, HFI, GeneratorTrait);
1326 ++NumHeaderSearchEntries;
1327 }
1328
1329 // Create the on-disk hash table in a buffer.
1330 llvm::SmallString<4096> TableData;
1331 uint32_t BucketOffset;
1332 {
1333 llvm::raw_svector_ostream Out(TableData);
1334 // Make sure that no bucket is at offset 0
1335 clang::io::Emit32(Out, 0);
1336 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1337 }
1338
1339 // Create a blob abbreviation
1340 using namespace llvm;
1341 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1342 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1343 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1344 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1346 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1347
1348 // Write the stat cache
1349 RecordData Record;
1350 Record.push_back(HEADER_SEARCH_TABLE);
1351 Record.push_back(BucketOffset);
1352 Record.push_back(NumHeaderSearchEntries);
1353 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1354
1355 // Free all of the strings we had to duplicate.
1356 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1357 free((void*)SavedStrings[I]);
1358}
1359
Douglas Gregor14f79002009-04-10 03:52:48 +00001360/// \brief Writes the block containing the serialized form of the
1361/// source manager.
1362///
1363/// TODO: We should probably use an on-disk hash table (stored in a
1364/// blob), indexed based on the file name, so that we only create
1365/// entries for files that we actually need. In the common case (no
1366/// errors), we probably won't have to create file entries for any of
1367/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001368void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001369 const Preprocessor &PP,
1370 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001371 RecordData Record;
1372
Chris Lattnerf04ad692009-04-10 17:16:57 +00001373 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001374 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001375
1376 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001377 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1378 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1379 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1380 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001381
Douglas Gregorbd945002009-04-13 16:31:14 +00001382 // Write the line table.
1383 if (SourceMgr.hasLineTable()) {
1384 LineTableInfo &LineTable = SourceMgr.getLineTable();
1385
1386 // Emit the file names
1387 Record.push_back(LineTable.getNumFilenames());
1388 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1389 // Emit the file name
1390 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001391 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001392 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1393 Record.push_back(FilenameLen);
1394 if (FilenameLen)
1395 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1396 }
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Douglas Gregorbd945002009-04-13 16:31:14 +00001398 // Emit the line entries
1399 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1400 L != LEnd; ++L) {
1401 // Emit the file ID
1402 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregorbd945002009-04-13 16:31:14 +00001404 // Emit the line entries
1405 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001406 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001407 LEEnd = L->second.end();
1408 LE != LEEnd; ++LE) {
1409 Record.push_back(LE->FileOffset);
1410 Record.push_back(LE->LineNo);
1411 Record.push_back(LE->FilenameID);
1412 Record.push_back((unsigned)LE->FileKind);
1413 Record.push_back(LE->IncludeOffset);
1414 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001415 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001416 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001417 }
1418
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001419 // Write out the source location entry table. We skip the first
1420 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001421 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001422 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001423 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1424 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1425 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1426 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001427 // Get this source location entry.
1428 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001429
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001430 // Record the offset of this source-location entry.
1431 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1432
1433 // Figure out which record code to use.
1434 unsigned Code;
1435 if (SLoc->isFile()) {
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001436 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001437 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001438 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001439 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001440 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001441 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001442 Record.clear();
1443 Record.push_back(Code);
1444
1445 Record.push_back(SLoc->getOffset());
1446 if (SLoc->isFile()) {
1447 const SrcMgr::FileInfo &File = SLoc->getFile();
1448 Record.push_back(File.getIncludeLoc().getRawEncoding());
1449 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1450 Record.push_back(File.hasLineDirectives());
1451
1452 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001453 if (Content->OrigEntry) {
1454 assert(Content->OrigEntry == Content->ContentsEntry &&
1455 "Writing to AST an overriden file is not supported");
1456
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001457 // The source location entry is a file. The blob associated
1458 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Douglas Gregor2d52be52010-03-21 22:49:54 +00001460 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001461 Record.push_back(Content->OrigEntry->getSize());
1462 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001463
Douglas Gregore650c8c2009-07-07 00:12:59 +00001464 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001465 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001466 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001467
1468 // Ask the file manager to fixup the relative path for us. This will
1469 // honor the working directory.
1470 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1471
1472 // FIXME: This call to make_absolute shouldn't be necessary, the
1473 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001474 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001475 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Douglas Gregore650c8c2009-07-07 00:12:59 +00001477 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001478 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001479 } else {
1480 // The source location entry is a buffer. The blob associated
1481 // with this entry contains the contents of the buffer.
1482
1483 // We add one to the size so that we capture the trailing NULL
1484 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1485 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001486 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001487 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001488 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001489 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1490 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001491 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001492 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001493 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001494 llvm::StringRef(Buffer->getBufferStart(),
1495 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001496
1497 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001498 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001499 }
1500 } else {
1501 // The source location entry is an instantiation.
1502 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1503 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1504 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1505 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1506
1507 // Compute the token length for this macro expansion.
1508 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001509 if (I + 1 != N)
1510 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001511 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1512 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1513 }
1514 }
1515
Douglas Gregorc9490c02009-04-16 22:23:12 +00001516 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001517
1518 if (SLocEntryOffsets.empty())
1519 return;
1520
Sebastian Redl3397c552010-08-18 23:56:27 +00001521 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001522 // table is used for lazily loading source-location information.
1523 using namespace llvm;
1524 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001525 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1529 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001531 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001532 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001533 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001534 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1535 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001536 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001537 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001538 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001539
Sebastian Redl3397c552010-08-18 23:56:27 +00001540 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001541 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001542 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001543}
1544
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001545//===----------------------------------------------------------------------===//
1546// Preprocessor Serialization
1547//===----------------------------------------------------------------------===//
1548
Douglas Gregor9c736102011-02-10 18:20:09 +00001549static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1550 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1551 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1552 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1553 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1554 return X.first->getName().compare(Y.first->getName());
1555}
1556
Chris Lattner0b1fb982009-04-10 17:15:23 +00001557/// \brief Writes the block containing the serialized form of the
1558/// preprocessor.
1559///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001560void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001561 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001562
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001563 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1564 if (PP.getCounterValue() != 0) {
1565 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001566 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001567 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001568 }
1569
1570 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001571 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Sebastian Redl3397c552010-08-18 23:56:27 +00001573 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001574 // FIXME: use diagnostics subsystem for localization etc.
1575 if (PP.SawDateOrTime())
1576 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Douglas Gregorecdcb882010-10-20 22:00:55 +00001578
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001579 // Loop over all the macro definitions that are live at the end of the file,
1580 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001581 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001582
Douglas Gregor9c736102011-02-10 18:20:09 +00001583 // Construct the list of macro definitions that need to be serialized.
1584 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1585 MacrosToEmit;
1586 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001587 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1588 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001589 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001590 MacroDefinitionsSeen.insert(I->first);
1591 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1592 }
1593
1594 // Sort the set of macro definitions that need to be serialized by the
1595 // name of the macro, to provide a stable ordering.
1596 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1597 &compareMacroDefinitions);
1598
Douglas Gregor040a8042011-02-11 00:26:14 +00001599 // Resolve any identifiers that defined macros at the time they were
1600 // deserialized, adding them to the list of macros to emit (if appropriate).
1601 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1602 IdentifierInfo *Name
1603 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1604 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1605 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1606 }
1607
Douglas Gregor9c736102011-02-10 18:20:09 +00001608 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1609 const IdentifierInfo *Name = MacrosToEmit[I].first;
1610 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001611 if (!MI)
1612 continue;
1613
Sebastian Redl3397c552010-08-18 23:56:27 +00001614 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001615 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001616 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001617
1618 // FIXME: There is a (probably minor) optimization we could do here, if
1619 // the macro comes from the original PCH but the identifier comes from a
1620 // chained PCH, by storing the offset into the original PCH rather than
1621 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001622 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001623 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001624 continue;
1625
Douglas Gregor9c736102011-02-10 18:20:09 +00001626 AddIdentifierRef(Name, Record);
1627 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001628 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1629 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001631 unsigned Code;
1632 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001633 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001634 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001635 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001637 Record.push_back(MI->isC99Varargs());
1638 Record.push_back(MI->isGNUVarargs());
1639 Record.push_back(MI->getNumArgs());
1640 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1641 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001642 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001643 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001644
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001645 // If we have a detailed preprocessing record, record the macro definition
1646 // ID that corresponds to this macro.
1647 if (PPRec)
1648 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001649
Douglas Gregorc9490c02009-04-16 22:23:12 +00001650 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001651 Record.clear();
1652
Chris Lattnerdf961c22009-04-10 18:08:30 +00001653 // Emit the tokens array.
1654 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1655 // Note that we know that the preprocessor does not have any annotation
1656 // tokens in it because they are created by the parser, and thus can't be
1657 // in a macro definition.
1658 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Chris Lattnerdf961c22009-04-10 18:08:30 +00001660 Record.push_back(Tok.getLocation().getRawEncoding());
1661 Record.push_back(Tok.getLength());
1662
Chris Lattnerdf961c22009-04-10 18:08:30 +00001663 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1664 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001665 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001666 // FIXME: Should translate token kind to a stable encoding.
1667 Record.push_back(Tok.getKind());
1668 // FIXME: Should translate token flags to a stable encoding.
1669 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001671 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001672 Record.clear();
1673 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001674 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001675 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001676 Stream.ExitBlock();
1677
1678 if (PPRec)
1679 WritePreprocessorDetail(*PPRec);
1680}
1681
1682void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1683 if (PPRec.begin(Chain) == PPRec.end(Chain))
1684 return;
1685
1686 // Enter the preprocessor block.
1687 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001688
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001689 // If the preprocessor has a preprocessing record, emit it.
1690 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001691 using namespace llvm;
1692
1693 // Set up the abbreviation for
1694 unsigned InclusionAbbrev = 0;
1695 {
1696 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1697 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1698 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1701 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1702 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1703 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1704 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1705 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1706 }
1707
1708 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1709 RecordData Record;
1710 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1711 EEnd = PPRec.end(Chain);
1712 E != EEnd; ++E) {
1713 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001714
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001715 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1716 // Record this macro definition's location.
1717 MacroID ID = getMacroDefinitionID(MD);
1718
1719 // Don't write the macro definition if it is from another AST file.
1720 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001721 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001722
Douglas Gregor89d99802010-11-30 06:16:57 +00001723 // Notify the serialization listener that we're serializing this entity.
1724 if (SerializationListener)
1725 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001726 Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001727
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001728 unsigned Position = ID - FirstMacroID;
1729 if (Position != MacroDefinitionOffsets.size()) {
1730 if (Position > MacroDefinitionOffsets.size())
1731 MacroDefinitionOffsets.resize(Position + 1);
1732
1733 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1734 } else
1735 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001736
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001737 Record.push_back(IndexBase + NumPreprocessingRecords++);
1738 Record.push_back(ID);
1739 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1740 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1741 AddIdentifierRef(MD->getName(), Record);
1742 AddSourceLocation(MD->getLocation(), Record);
1743 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1744 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001745 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001746
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001747 // Notify the serialization listener that we're serializing this entity.
1748 if (SerializationListener)
1749 SerializationListener->SerializedPreprocessedEntity(*E,
1750 Stream.GetCurrentBitNo());
1751
1752 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1753 Record.push_back(IndexBase + NumPreprocessingRecords++);
1754 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1755 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1756 AddIdentifierRef(MI->getName(), Record);
1757 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1758 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1759 continue;
1760 }
1761
1762 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1763 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1764 Record.push_back(IndexBase + NumPreprocessingRecords++);
1765 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1766 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1767 Record.push_back(ID->getFileName().size());
1768 Record.push_back(ID->wasInQuotes());
1769 Record.push_back(static_cast<unsigned>(ID->getKind()));
1770 llvm::SmallString<64> Buffer;
1771 Buffer += ID->getFileName();
1772 Buffer += ID->getFile()->getName();
1773 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1774 continue;
1775 }
1776
1777 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1778 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001779 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001780
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001781 // Write the offsets table for the preprocessing record.
1782 if (NumPreprocessingRecords > 0) {
1783 // Write the offsets table for identifier IDs.
1784 using namespace llvm;
1785 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001786 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1790 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001791
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001792 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001793 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001794 Record.push_back(NumPreprocessingRecords);
1795 Record.push_back(MacroDefinitionOffsets.size());
1796 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001797 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001798 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1799 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001800}
1801
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001802void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001803 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001804 for (Diagnostic::DiagStatePointsTy::const_iterator
1805 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1806 I != E; ++I) {
1807 const Diagnostic::DiagStatePoint &point = *I;
1808 if (point.Loc.isInvalid())
1809 continue;
1810
1811 Record.push_back(point.Loc.getRawEncoding());
1812 for (Diagnostic::DiagState::iterator
1813 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1814 unsigned diag = I->first, map = I->second;
1815 if (map & 0x10) { // mapping from a diagnostic pragma.
1816 Record.push_back(diag);
1817 Record.push_back(map & 0x7);
1818 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001819 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001820 Record.push_back(-1); // mark the end of the diag/map pairs for this
1821 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001822 }
1823
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001824 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001825 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001826}
1827
Anders Carlssonc8505782011-03-06 18:41:18 +00001828void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1829 if (CXXBaseSpecifiersOffsets.empty())
1830 return;
1831
1832 RecordData Record;
1833
1834 // Create a blob abbreviation for the C++ base specifiers offsets.
1835 using namespace llvm;
1836
1837 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1838 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1841 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1842
1843 // Write the selector offsets table.
1844 Record.clear();
1845 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1846 Record.push_back(CXXBaseSpecifiersOffsets.size());
1847 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
1848 (const char *)CXXBaseSpecifiersOffsets.data(),
1849 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
1850}
1851
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001852//===----------------------------------------------------------------------===//
1853// Type Serialization
1854//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001855
Sebastian Redl3397c552010-08-18 23:56:27 +00001856/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001857void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001858 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001859 if (Idx.getIndex() == 0) // we haven't seen this type before.
1860 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Douglas Gregor97475832010-10-05 18:37:06 +00001862 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001863
Douglas Gregor2cf26342009-04-09 22:27:44 +00001864 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001865 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001866 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001867 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001868 else if (TypeOffsets.size() < Index) {
1869 TypeOffsets.resize(Index + 1);
1870 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001871 }
1872
1873 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Douglas Gregor2cf26342009-04-09 22:27:44 +00001875 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001876 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001877
Douglas Gregora4923eb2009-11-16 21:35:15 +00001878 if (T.hasLocalNonFastQualifiers()) {
1879 Qualifiers Qs = T.getLocalQualifiers();
1880 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001881 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001882 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001883 } else {
1884 switch (T->getTypeClass()) {
1885 // For all of the concrete, non-dependent types, call the
1886 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001887#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001888 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001889#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001890#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001891 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001892 }
1893
1894 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001895 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001896
1897 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001898 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001899}
1900
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001901//===----------------------------------------------------------------------===//
1902// Declaration Serialization
1903//===----------------------------------------------------------------------===//
1904
Douglas Gregor2cf26342009-04-09 22:27:44 +00001905/// \brief Write the block containing all of the declaration IDs
1906/// lexically declared within the given DeclContext.
1907///
1908/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1909/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001910uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001911 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001912 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001913 return 0;
1914
Douglas Gregorc9490c02009-04-16 22:23:12 +00001915 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001916 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001917 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001918 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001919 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1920 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001921 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001922
Douglas Gregor25123082009-04-22 22:34:57 +00001923 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001924 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001925 reinterpret_cast<char*>(Decls.data()),
1926 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001927 return Offset;
1928}
1929
Sebastian Redla4232eb2010-08-18 23:56:21 +00001930void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001931 using namespace llvm;
1932 RecordData Record;
1933
1934 // Write the type offsets array
1935 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001936 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1939 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1940 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001941 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001942 Record.push_back(TypeOffsets.size());
1943 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001944 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001945 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1946
1947 // Write the declaration offsets array
1948 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001949 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1952 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1953 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001954 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001955 Record.push_back(DeclOffsets.size());
1956 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001957 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001958 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1959}
1960
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001961//===----------------------------------------------------------------------===//
1962// Global Method Pool and Selector Serialization
1963//===----------------------------------------------------------------------===//
1964
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001965namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001966// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001967class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001968 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001969
1970public:
1971 typedef Selector key_type;
1972 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Sebastian Redl5d050072010-08-04 17:20:04 +00001974 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001975 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001976 ObjCMethodList Instance, Factory;
1977 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001978 typedef const data_type& data_type_ref;
1979
Sebastian Redl3397c552010-08-18 23:56:27 +00001980 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001982 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001983 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001984 }
Mike Stump1eb44332009-09-09 15:08:12 +00001985
1986 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001987 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1988 data_type_ref Methods) {
1989 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1990 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001991 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1992 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001993 Method = Method->Next)
1994 if (Method->Method)
1995 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001996 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001997 Method = Method->Next)
1998 if (Method->Method)
1999 DataLen += 4;
2000 clang::io::Emit16(Out, DataLen);
2001 return std::make_pair(KeyLen, DataLen);
2002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Douglas Gregor83941df2009-04-25 17:48:32 +00002004 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002005 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002006 assert((Start >> 32) == 0 && "Selector key offset too large");
2007 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002008 unsigned N = Sel.getNumArgs();
2009 clang::io::Emit16(Out, N);
2010 if (N == 0)
2011 N = 1;
2012 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002013 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002014 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2015 }
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002017 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002018 data_type_ref Methods, unsigned DataLen) {
2019 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002020 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002021 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002022 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002023 Method = Method->Next)
2024 if (Method->Method)
2025 ++NumInstanceMethods;
2026
2027 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002028 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002029 Method = Method->Next)
2030 if (Method->Method)
2031 ++NumFactoryMethods;
2032
2033 clang::io::Emit16(Out, NumInstanceMethods);
2034 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002035 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002036 Method = Method->Next)
2037 if (Method->Method)
2038 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002039 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002040 Method = Method->Next)
2041 if (Method->Method)
2042 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002043
2044 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002045 }
2046};
2047} // end anonymous namespace
2048
Sebastian Redl059612d2010-08-03 21:58:15 +00002049/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002050///
2051/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002052/// in an on-disk hash table indexed by the selector. The hash table also
2053/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002054void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002055 using namespace llvm;
2056
Sebastian Redl059612d2010-08-03 21:58:15 +00002057 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002058 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002059 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002060 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002061 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002062 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002063 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002064 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Sebastian Redl059612d2010-08-03 21:58:15 +00002066 // Create the on-disk hash table representation. We walk through every
2067 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002068 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002069 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002070 I = SelectorIDs.begin(), E = SelectorIDs.end();
2071 I != E; ++I) {
2072 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002073 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002074 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002075 I->second,
2076 ObjCMethodList(),
2077 ObjCMethodList()
2078 };
2079 if (F != SemaRef.MethodPool.end()) {
2080 Data.Instance = F->second.first;
2081 Data.Factory = F->second.second;
2082 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002083 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002084 // changed.
2085 if (Chain && I->second < FirstSelectorID) {
2086 // Selector already exists. Did it change?
2087 bool changed = false;
2088 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2089 M = M->Next) {
2090 if (M->Method->getPCHLevel() == 0)
2091 changed = true;
2092 }
2093 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2094 M = M->Next) {
2095 if (M->Method->getPCHLevel() == 0)
2096 changed = true;
2097 }
2098 if (!changed)
2099 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002100 } else if (Data.Instance.Method || Data.Factory.Method) {
2101 // A new method pool entry.
2102 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002103 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002104 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002105 }
2106
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002107 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002108 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002109 uint32_t BucketOffset;
2110 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002111 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002112 llvm::raw_svector_ostream Out(MethodPool);
2113 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002114 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002115 BucketOffset = Generator.Emit(Out, Trait);
2116 }
2117
2118 // Create a blob abbreviation
2119 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002120 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2124 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2125
Douglas Gregor83941df2009-04-25 17:48:32 +00002126 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002127 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002128 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002129 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002130 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002131 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002132
2133 // Create a blob abbreviation for the selector table offsets.
2134 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002135 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00002137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2138 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2139
2140 // Write the selector offsets table.
2141 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002142 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002143 Record.push_back(SelectorOffsets.size());
2144 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002145 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00002146 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002147 }
2148}
2149
Sebastian Redl3397c552010-08-18 23:56:27 +00002150/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002151void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002152 using namespace llvm;
2153 if (SemaRef.ReferencedSelectors.empty())
2154 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002155
Fariborz Jahanian32019832010-07-23 19:11:11 +00002156 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002157
Sebastian Redl3397c552010-08-18 23:56:27 +00002158 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002159 // very tricky to fix, and given that @selector shouldn't really appear in
2160 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002161 for (DenseMap<Selector, SourceLocation>::iterator S =
2162 SemaRef.ReferencedSelectors.begin(),
2163 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2164 Selector Sel = (*S).first;
2165 SourceLocation Loc = (*S).second;
2166 AddSelectorRef(Sel, Record);
2167 AddSourceLocation(Loc, Record);
2168 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002169 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002170}
2171
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002172//===----------------------------------------------------------------------===//
2173// Identifier Table Serialization
2174//===----------------------------------------------------------------------===//
2175
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002176namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002177class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002178 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002179 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002180
Douglas Gregora92193e2009-04-28 21:18:29 +00002181 /// \brief Determines whether this is an "interesting" identifier
2182 /// that needs a full IdentifierInfo structure written into the hash
2183 /// table.
2184 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2185 return II->isPoisoned() ||
2186 II->isExtensionToken() ||
2187 II->hasMacroDefinition() ||
2188 II->getObjCOrBuiltinID() ||
2189 II->getFETokenInfo<void>();
2190 }
2191
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002192public:
2193 typedef const IdentifierInfo* key_type;
2194 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002196 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002197 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002198
Sebastian Redl3397c552010-08-18 23:56:27 +00002199 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002200 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002201
2202 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002203 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002204 }
Mike Stump1eb44332009-09-09 15:08:12 +00002205
2206 std::pair<unsigned,unsigned>
2207 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002208 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002209 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002210 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2211 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002212 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002213 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002214 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002215 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002216 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2217 DEnd = IdentifierResolver::end();
2218 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002219 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002220 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002221 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002222 // We emit the key length after the data length so that every
2223 // string is preceded by a 16-bit length. This matches the PTH
2224 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002225 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002226 return std::make_pair(KeyLen, DataLen);
2227 }
Mike Stump1eb44332009-09-09 15:08:12 +00002228
2229 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002230 unsigned KeyLen) {
2231 // Record the location of the key data. This is used when generating
2232 // the mapping from persistent IDs to strings.
2233 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002234 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002235 }
Mike Stump1eb44332009-09-09 15:08:12 +00002236
2237 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002238 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002239 if (!isInterestingIdentifier(II)) {
2240 clang::io::Emit32(Out, ID << 1);
2241 return;
2242 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002243
Douglas Gregora92193e2009-04-28 21:18:29 +00002244 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002245 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002246 bool hasMacroDefinition =
2247 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002248 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002249 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002250 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2251 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2252 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002253 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002254 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002255 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002256
Douglas Gregor37e26842009-04-21 23:56:24 +00002257 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002258 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002259
Douglas Gregor668c1a42009-04-21 22:25:48 +00002260 // Emit the declaration IDs in reverse order, because the
2261 // IdentifierResolver provides the declarations as they would be
2262 // visible (e.g., the function "stat" would come before the struct
2263 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2264 // adds declarations to the end of the list (so we need to see the
2265 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002266 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00002267 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002268 IdentifierResolver::end());
2269 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2270 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002271 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002272 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002273 }
2274};
2275} // end anonymous namespace
2276
Sebastian Redl3397c552010-08-18 23:56:27 +00002277/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002278///
2279/// The identifier table consists of a blob containing string data
2280/// (the actual identifiers themselves) and a separate "offsets" index
2281/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002282void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002283 using namespace llvm;
2284
2285 // Create and write out the blob that contains the identifier
2286 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002287 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002288 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002289 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002290
Douglas Gregor92b059e2009-04-28 20:33:11 +00002291 // Look for any identifiers that were named while processing the
2292 // headers, but are otherwise not needed. We add these to the hash
2293 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002294 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002295 // file.
2296 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2297 IDEnd = PP.getIdentifierTable().end();
2298 ID != IDEnd; ++ID)
2299 getIdentifierRef(ID->second);
2300
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002301 // Create the on-disk hash table representation. We only store offsets
2302 // for identifiers that appear here for the first time.
2303 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002304 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002305 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2306 ID != IDEnd; ++ID) {
2307 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002308 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002309 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002310 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002311
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002312 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002313 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002314 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002315 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002316 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002317 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002318 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002319 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002320 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002321 }
2322
2323 // Create a blob abbreviation
2324 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002325 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002328 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002329
2330 // Write the identifier table
2331 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002332 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002333 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002334 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002335 }
2336
2337 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002338 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002339 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2342 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2343
2344 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002345 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002346 Record.push_back(IdentifierOffsets.size());
2347 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002348 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002349 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002350}
2351
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002352//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002353// DeclContext's Name Lookup Table Serialization
2354//===----------------------------------------------------------------------===//
2355
2356namespace {
2357// Trait used for the on-disk hash table used in the method pool.
2358class ASTDeclContextNameLookupTrait {
2359 ASTWriter &Writer;
2360
2361public:
2362 typedef DeclarationName key_type;
2363 typedef key_type key_type_ref;
2364
2365 typedef DeclContext::lookup_result data_type;
2366 typedef const data_type& data_type_ref;
2367
2368 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2369
2370 unsigned ComputeHash(DeclarationName Name) {
2371 llvm::FoldingSetNodeID ID;
2372 ID.AddInteger(Name.getNameKind());
2373
2374 switch (Name.getNameKind()) {
2375 case DeclarationName::Identifier:
2376 ID.AddString(Name.getAsIdentifierInfo()->getName());
2377 break;
2378 case DeclarationName::ObjCZeroArgSelector:
2379 case DeclarationName::ObjCOneArgSelector:
2380 case DeclarationName::ObjCMultiArgSelector:
2381 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2382 break;
2383 case DeclarationName::CXXConstructorName:
2384 case DeclarationName::CXXDestructorName:
2385 case DeclarationName::CXXConversionFunctionName:
2386 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2387 break;
2388 case DeclarationName::CXXOperatorName:
2389 ID.AddInteger(Name.getCXXOverloadedOperator());
2390 break;
2391 case DeclarationName::CXXLiteralOperatorName:
2392 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2393 case DeclarationName::CXXUsingDirective:
2394 break;
2395 }
2396
2397 return ID.ComputeHash();
2398 }
2399
2400 std::pair<unsigned,unsigned>
2401 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2402 data_type_ref Lookup) {
2403 unsigned KeyLen = 1;
2404 switch (Name.getNameKind()) {
2405 case DeclarationName::Identifier:
2406 case DeclarationName::ObjCZeroArgSelector:
2407 case DeclarationName::ObjCOneArgSelector:
2408 case DeclarationName::ObjCMultiArgSelector:
2409 case DeclarationName::CXXConstructorName:
2410 case DeclarationName::CXXDestructorName:
2411 case DeclarationName::CXXConversionFunctionName:
2412 case DeclarationName::CXXLiteralOperatorName:
2413 KeyLen += 4;
2414 break;
2415 case DeclarationName::CXXOperatorName:
2416 KeyLen += 1;
2417 break;
2418 case DeclarationName::CXXUsingDirective:
2419 break;
2420 }
2421 clang::io::Emit16(Out, KeyLen);
2422
2423 // 2 bytes for num of decls and 4 for each DeclID.
2424 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2425 clang::io::Emit16(Out, DataLen);
2426
2427 return std::make_pair(KeyLen, DataLen);
2428 }
2429
2430 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2431 using namespace clang::io;
2432
2433 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2434 Emit8(Out, Name.getNameKind());
2435 switch (Name.getNameKind()) {
2436 case DeclarationName::Identifier:
2437 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2438 break;
2439 case DeclarationName::ObjCZeroArgSelector:
2440 case DeclarationName::ObjCOneArgSelector:
2441 case DeclarationName::ObjCMultiArgSelector:
2442 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2443 break;
2444 case DeclarationName::CXXConstructorName:
2445 case DeclarationName::CXXDestructorName:
2446 case DeclarationName::CXXConversionFunctionName:
2447 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2448 break;
2449 case DeclarationName::CXXOperatorName:
2450 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2451 Emit8(Out, Name.getCXXOverloadedOperator());
2452 break;
2453 case DeclarationName::CXXLiteralOperatorName:
2454 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2455 break;
2456 case DeclarationName::CXXUsingDirective:
2457 break;
2458 }
2459 }
2460
2461 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2462 data_type Lookup, unsigned DataLen) {
2463 uint64_t Start = Out.tell(); (void)Start;
2464 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2465 for (; Lookup.first != Lookup.second; ++Lookup.first)
2466 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2467
2468 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2469 }
2470};
2471} // end anonymous namespace
2472
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002473/// \brief Write the block containing all of the declaration IDs
2474/// visible from the given DeclContext.
2475///
2476/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002477/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002478uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2479 DeclContext *DC) {
2480 if (DC->getPrimaryContext() != DC)
2481 return 0;
2482
2483 // Since there is no name lookup into functions or methods, don't bother to
2484 // build a visible-declarations table for these entities.
2485 if (DC->isFunctionOrMethod())
2486 return 0;
2487
2488 // If not in C++, we perform name lookup for the translation unit via the
2489 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2490 // FIXME: In C++ we need the visible declarations in order to "see" the
2491 // friend declarations, is there a way to do this without writing the table ?
2492 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2493 return 0;
2494
2495 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002496 if (DC->hasExternalVisibleStorage())
2497 DC->MaterializeVisibleDeclsFromExternalStorage();
2498 else
2499 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002500
2501 // Serialize the contents of the mapping used for lookup. Note that,
2502 // although we have two very different code paths, the serialized
2503 // representation is the same for both cases: a declaration name,
2504 // followed by a size, followed by references to the visible
2505 // declarations that have that name.
2506 uint64_t Offset = Stream.GetCurrentBitNo();
2507 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2508 if (!Map || Map->empty())
2509 return 0;
2510
2511 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2512 ASTDeclContextNameLookupTrait Trait(*this);
2513
2514 // Create the on-disk hash table representation.
2515 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2516 D != DEnd; ++D) {
2517 DeclarationName Name = D->first;
2518 DeclContext::lookup_result Result = D->second.getLookupResult();
2519 Generator.insert(Name, Result, Trait);
2520 }
2521
2522 // Create the on-disk hash table in a buffer.
2523 llvm::SmallString<4096> LookupTable;
2524 uint32_t BucketOffset;
2525 {
2526 llvm::raw_svector_ostream Out(LookupTable);
2527 // Make sure that no bucket is at offset 0
2528 clang::io::Emit32(Out, 0);
2529 BucketOffset = Generator.Emit(Out, Trait);
2530 }
2531
2532 // Write the lookup table
2533 RecordData Record;
2534 Record.push_back(DECL_CONTEXT_VISIBLE);
2535 Record.push_back(BucketOffset);
2536 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2537 LookupTable.str());
2538
2539 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2540 ++NumVisibleDeclContexts;
2541 return Offset;
2542}
2543
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002544/// \brief Write an UPDATE_VISIBLE block for the given context.
2545///
2546/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2547/// DeclContext in a dependent AST file. As such, they only exist for the TU
2548/// (in C++) and for namespaces.
2549void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002550 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2551 if (!Map || Map->empty())
2552 return;
2553
2554 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2555 ASTDeclContextNameLookupTrait Trait(*this);
2556
2557 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002558 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2559 D != DEnd; ++D) {
2560 DeclarationName Name = D->first;
2561 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002562 // For any name that appears in this table, the results are complete, i.e.
2563 // they overwrite results from previous PCHs. Merging is always a mess.
2564 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002565 }
2566
2567 // Create the on-disk hash table in a buffer.
2568 llvm::SmallString<4096> LookupTable;
2569 uint32_t BucketOffset;
2570 {
2571 llvm::raw_svector_ostream Out(LookupTable);
2572 // Make sure that no bucket is at offset 0
2573 clang::io::Emit32(Out, 0);
2574 BucketOffset = Generator.Emit(Out, Trait);
2575 }
2576
2577 // Write the lookup table
2578 RecordData Record;
2579 Record.push_back(UPDATE_VISIBLE);
2580 Record.push_back(getDeclID(cast<Decl>(DC)));
2581 Record.push_back(BucketOffset);
2582 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2583}
2584
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002585/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2586void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2587 RecordData Record;
2588 Record.push_back(Opts.fp_contract);
2589 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2590}
2591
2592/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2593void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2594 if (!SemaRef.Context.getLangOptions().OpenCL)
2595 return;
2596
2597 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2598 RecordData Record;
2599#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2600#include "clang/Basic/OpenCLExtensions.def"
2601 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2602}
2603
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002604//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002605// General Serialization Routines
2606//===----------------------------------------------------------------------===//
2607
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002608/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002609void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002610 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002611 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2612 const Attr * A = *i;
2613 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2614 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002615
Sean Huntcf807c42010-08-18 23:23:40 +00002616#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002617
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002618 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002619}
2620
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002621void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002622 Record.push_back(Str.size());
2623 Record.insert(Record.end(), Str.begin(), Str.end());
2624}
2625
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002626void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2627 RecordDataImpl &Record) {
2628 Record.push_back(Version.getMajor());
2629 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2630 Record.push_back(*Minor + 1);
2631 else
2632 Record.push_back(0);
2633 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2634 Record.push_back(*Subminor + 1);
2635 else
2636 Record.push_back(0);
2637}
2638
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002639/// \brief Note that the identifier II occurs at the given offset
2640/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002641void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002642 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002643 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002644 // up earlier in the chain and thus don't need an offset.
2645 if (ID >= FirstIdentID)
2646 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002647}
2648
Douglas Gregor83941df2009-04-25 17:48:32 +00002649/// \brief Note that the selector Sel occurs at the given offset
2650/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002651void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002652 unsigned ID = SelectorIDs[Sel];
2653 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002654 // Don't record offsets for selectors that are also available in a different
2655 // file.
2656 if (ID < FirstSelectorID)
2657 return;
2658 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002659}
2660
Sebastian Redla4232eb2010-08-18 23:56:21 +00002661ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002662 : Stream(Stream), Chain(0), SerializationListener(0),
2663 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002664 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002665 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002666 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2667 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002668 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002669 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2670 NextCXXBaseSpecifiersID(1)
2671{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002672}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002673
Sebastian Redla4232eb2010-08-18 23:56:21 +00002674void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002675 const std::string &OutputFile,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002676 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002677 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002678 Stream.Emit((unsigned)'C', 8);
2679 Stream.Emit((unsigned)'P', 8);
2680 Stream.Emit((unsigned)'C', 8);
2681 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002683 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002684
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002685 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002686 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002687 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002688 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002689}
2690
Sebastian Redla4232eb2010-08-18 23:56:21 +00002691void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002692 const char *isysroot,
2693 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002694 using namespace llvm;
2695
2696 ASTContext &Context = SemaRef.Context;
2697 Preprocessor &PP = SemaRef.PP;
2698
Douglas Gregor2cf26342009-04-09 22:27:44 +00002699 // The translation unit is the first declaration we'll emit.
2700 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002701 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002702 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002703
Douglas Gregor2deaea32009-04-22 18:49:13 +00002704 // Make sure that we emit IdentifierInfos (and any attached
2705 // declarations) for builtins.
2706 {
2707 IdentifierTable &Table = PP.getIdentifierTable();
2708 llvm::SmallVector<const char *, 32> BuiltinNames;
2709 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2710 Context.getLangOptions().NoBuiltin);
2711 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2712 getIdentifierRef(&Table.get(BuiltinNames[I]));
2713 }
2714
Chris Lattner63d65f82009-09-08 18:19:27 +00002715 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002716 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002717 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002718 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002719 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2720 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002721 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002722
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002723 // Build a record containing all of the file scoped decls in this file.
2724 RecordData UnusedFileScopedDecls;
2725 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2726 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002727
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002728 RecordData WeakUndeclaredIdentifiers;
2729 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2730 WeakUndeclaredIdentifiers.push_back(
2731 SemaRef.WeakUndeclaredIdentifiers.size());
2732 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2733 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2734 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2735 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2736 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2737 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2738 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2739 }
2740 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002741
Douglas Gregor14c22f22009-04-22 22:18:58 +00002742 // Build a record containing all of the locally-scoped external
2743 // declarations in this header file. Generally, this record will be
2744 // empty.
2745 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002746 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002747 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002748 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002749 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2750 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2751 TD != TDEnd; ++TD)
2752 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2753
Douglas Gregorb81c1702009-04-27 20:06:05 +00002754 // Build a record containing all of the ext_vector declarations.
2755 RecordData ExtVectorDecls;
2756 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2757 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2758
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002759 // Build a record containing all of the VTable uses information.
2760 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002761 if (!SemaRef.VTableUses.empty()) {
2762 VTableUses.push_back(SemaRef.VTableUses.size());
2763 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2764 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2765 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2766 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2767 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002768 }
2769
2770 // Build a record containing all of dynamic classes declarations.
2771 RecordData DynamicClasses;
2772 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2773 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2774
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002775 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002776 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002777 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002778 I = SemaRef.PendingInstantiations.begin(),
2779 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2780 AddDeclRef(I->first, PendingInstantiations);
2781 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002782 }
2783 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2784 "There are local ones at end of translation unit!");
2785
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002786 // Build a record containing some declaration references.
2787 RecordData SemaDeclRefs;
2788 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2789 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2790 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2791 }
2792
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002793 RecordData CUDASpecialDeclRefs;
2794 if (Context.getcudaConfigureCallDecl()) {
2795 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2796 }
2797
Sebastian Redl3397c552010-08-18 23:56:27 +00002798 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002799 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002800 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002801 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002802 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002803 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002804 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002805 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002806 // Write the record of special types.
2807 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002808
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002809 AddTypeRef(Context.getBuiltinVaListType(), Record);
2810 AddTypeRef(Context.getObjCIdType(), Record);
2811 AddTypeRef(Context.getObjCSelType(), Record);
2812 AddTypeRef(Context.getObjCProtoType(), Record);
2813 AddTypeRef(Context.getObjCClassType(), Record);
2814 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2815 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2816 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002817 AddTypeRef(Context.getjmp_bufType(), Record);
2818 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002819 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2820 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002821 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002822 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002823 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2824 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002825 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002826 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Douglas Gregor366809a2009-04-26 03:49:13 +00002828 // Keep writing types and declarations until all types and
2829 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002830 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002831 WriteDeclsBlockAbbrevs();
2832 while (!DeclTypesToEmit.empty()) {
2833 DeclOrType DOT = DeclTypesToEmit.front();
2834 DeclTypesToEmit.pop();
2835 if (DOT.isType())
2836 WriteType(DOT.getType());
2837 else
2838 WriteDecl(Context, DOT.getDecl());
2839 }
2840 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002841
Douglas Gregor813a97b2009-10-17 17:25:45 +00002842 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002843 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002844 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002845 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002846 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002847 WriteFPPragmaOptions(SemaRef.getFPOptions());
2848 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002849
Sebastian Redl1476ed42010-07-16 16:36:56 +00002850 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002851 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002852
Anders Carlssonc8505782011-03-06 18:41:18 +00002853 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00002854
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002855 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002856 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002857 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002858
2859 // Write the record containing tentative definitions.
2860 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002861 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002862
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002863 // Write the record containing unused file scoped decls.
2864 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002865 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002866
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002867 // Write the record containing weak undeclared identifiers.
2868 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002869 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002870 WeakUndeclaredIdentifiers);
2871
Douglas Gregor14c22f22009-04-22 22:18:58 +00002872 // Write the record containing locally-scoped external definitions.
2873 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002874 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002875 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002876
2877 // Write the record containing ext_vector type names.
2878 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002879 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002881 // Write the record containing VTable uses information.
2882 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002883 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002884
2885 // Write the record containing dynamic classes declarations.
2886 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002887 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002888
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002889 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002890 if (!PendingInstantiations.empty())
2891 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002892
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002893 // Write the record containing declaration references of Sema.
2894 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002895 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002896
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002897 // Write the record containing CUDA-specific declaration references.
2898 if (!CUDASpecialDeclRefs.empty())
2899 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2900
Douglas Gregor3e1af842009-04-17 22:13:46 +00002901 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002902 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002903 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002904 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002905 Record.push_back(NumLexicalDeclContexts);
2906 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002907 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002908 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002909}
2910
Sebastian Redla4232eb2010-08-18 23:56:21 +00002911void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002912 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002913 using namespace llvm;
2914
2915 ASTContext &Context = SemaRef.Context;
2916 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002917
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002918 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002919 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002920 WriteMetadata(Context, isysroot, "");
Sebastian Redl1476ed42010-07-16 16:36:56 +00002921 if (StatCalls && !isysroot)
2922 WriteStatCache(*StatCalls);
2923 // FIXME: Source manager block should only write new stuff, which could be
2924 // done by tracking the largest ID in the chain
2925 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002926
2927 // The special types are in the chained PCH.
2928
2929 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002930 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002931 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002932 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002933 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2934 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002935 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002936 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002937 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002938 else if ((*I)->isChangedSinceDeserialization())
2939 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002940 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002941 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002942 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002943 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002944 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2945 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2946 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002947 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002948 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2949 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002950 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002951 // And a visible updates block for the DeclContexts.
2952 Abv = new llvm::BitCodeAbbrev();
2953 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2954 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2955 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2956 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2957 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2958 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002959
Sebastian Redl083abdf2010-07-27 23:01:28 +00002960 // Build a record containing all of the new tentative definitions in this
2961 // file, in TentativeDefinitions order.
2962 RecordData TentativeDefinitions;
2963 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2964 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2965 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2966 }
2967
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002968 // Build a record containing all of the file scoped decls in this file.
2969 RecordData UnusedFileScopedDecls;
2970 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2971 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2972 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002973 }
2974
Sebastian Redl40566802010-08-05 18:21:25 +00002975 // We write the entire table, overwriting the tables from the chain.
2976 RecordData WeakUndeclaredIdentifiers;
2977 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2978 WeakUndeclaredIdentifiers.push_back(
2979 SemaRef.WeakUndeclaredIdentifiers.size());
2980 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2981 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2982 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2983 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2984 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2985 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2986 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2987 }
2988 }
2989
Sebastian Redl083abdf2010-07-27 23:01:28 +00002990 // Build a record containing all of the locally-scoped external
2991 // declarations in this header file. Generally, this record will be
2992 // empty.
2993 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002994 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002995 // nondeterminstic!
2996 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2997 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2998 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2999 TD != TDEnd; ++TD) {
3000 if (TD->second->getPCHLevel() == 0)
3001 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3002 }
3003
3004 // Build a record containing all of the ext_vector declarations.
3005 RecordData ExtVectorDecls;
3006 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3007 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3008 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3009 }
3010
Sebastian Redl40566802010-08-05 18:21:25 +00003011 // Build a record containing all of the VTable uses information.
3012 // We write everything here, because it's too hard to determine whether
3013 // a use is new to this part.
3014 RecordData VTableUses;
3015 if (!SemaRef.VTableUses.empty()) {
3016 VTableUses.push_back(SemaRef.VTableUses.size());
3017 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3018 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3019 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3020 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3021 }
3022 }
3023
3024 // Build a record containing all of dynamic classes declarations.
3025 RecordData DynamicClasses;
3026 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3027 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3028 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3029
3030 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003031 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00003032 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003033 I = SemaRef.PendingInstantiations.begin(),
3034 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00003035 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003036 AddDeclRef(I->first, PendingInstantiations);
3037 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003038 }
3039 }
3040 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3041 "There are local ones at end of translation unit!");
3042
3043 // Build a record containing some declaration references.
3044 // It's not worth the effort to avoid duplication here.
3045 RecordData SemaDeclRefs;
3046 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3047 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3048 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3049 }
3050
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003051 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003052 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003053 for (DeclsToRewriteTy::iterator
3054 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3055 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003056 while (!DeclTypesToEmit.empty()) {
3057 DeclOrType DOT = DeclTypesToEmit.front();
3058 DeclTypesToEmit.pop();
3059 if (DOT.isType())
3060 WriteType(DOT.getType());
3061 else
3062 WriteDecl(Context, DOT.getDecl());
3063 }
3064 Stream.ExitBlock();
3065
Sebastian Redl083abdf2010-07-27 23:01:28 +00003066 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003067 WriteSelectors(SemaRef);
3068 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003069 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003070 WriteFPPragmaOptions(SemaRef.getFPOptions());
3071 WriteOpenCLExtensions(SemaRef);
3072
Sebastian Redl1476ed42010-07-16 16:36:56 +00003073 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003074 // FIXME: For chained PCH only write the new mappings (we currently
3075 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003076 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003077
Anders Carlssonc8505782011-03-06 18:41:18 +00003078 WriteCXXBaseSpecifiersOffsets();
3079
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003080 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003081 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003082 RecordData FirstLatestDeclIDs;
3083 for (FirstLatestDeclMap::iterator
3084 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3085 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3086 "Expected first & second to be in different PCHs");
3087 AddDeclRef(I->first, FirstLatestDeclIDs);
3088 AddDeclRef(I->second, FirstLatestDeclIDs);
3089 }
3090 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003091 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003092
Sebastian Redl083abdf2010-07-27 23:01:28 +00003093 // Write the record containing external, unnamed definitions.
3094 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003095 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003096
3097 // Write the record containing tentative definitions.
3098 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003099 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003100
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003101 // Write the record containing unused file scoped decls.
3102 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003103 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003104
Sebastian Redl40566802010-08-05 18:21:25 +00003105 // Write the record containing weak undeclared identifiers.
3106 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003107 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003108 WeakUndeclaredIdentifiers);
3109
Sebastian Redl083abdf2010-07-27 23:01:28 +00003110 // Write the record containing locally-scoped external definitions.
3111 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003112 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003113 LocallyScopedExternalDecls);
3114
3115 // Write the record containing ext_vector type names.
3116 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003117 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003118
Sebastian Redl40566802010-08-05 18:21:25 +00003119 // Write the record containing VTable uses information.
3120 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003121 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003122
3123 // Write the record containing dynamic classes declarations.
3124 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003125 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003126
3127 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003128 if (!PendingInstantiations.empty())
3129 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003130
3131 // Write the record containing declaration references of Sema.
3132 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003133 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003134
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003135 // Write the updates to DeclContexts.
3136 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3137 I = UpdatedDeclContexts.begin(),
3138 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003139 I != E; ++I)
3140 WriteDeclContextVisibleUpdate(*I);
3141
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003142 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003143
Sebastian Redl083abdf2010-07-27 23:01:28 +00003144 Record.clear();
3145 Record.push_back(NumStatements);
3146 Record.push_back(NumMacros);
3147 Record.push_back(NumLexicalDeclContexts);
3148 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003149 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003150 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003151 Stream.ExitBlock();
3152}
3153
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003154void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003155 if (DeclUpdates.empty())
3156 return;
3157
3158 RecordData OffsetsRecord;
3159 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3160 for (DeclUpdateMap::iterator
3161 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3162 const Decl *D = I->first;
3163 UpdateRecord &URec = I->second;
3164
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003165 if (DeclsToRewrite.count(D))
3166 continue; // The decl will be written completely,no need to store updates.
3167
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003168 uint64_t Offset = Stream.GetCurrentBitNo();
3169 Stream.EmitRecord(DECL_UPDATES, URec);
3170
3171 OffsetsRecord.push_back(GetDeclRef(D));
3172 OffsetsRecord.push_back(Offset);
3173 }
3174 Stream.ExitBlock();
3175 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3176}
3177
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003178void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003179 if (ReplacedDecls.empty())
3180 return;
3181
3182 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003183 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003184 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3185 Record.push_back(I->first);
3186 Record.push_back(I->second);
3187 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003188 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003189}
3190
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003191void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003192 Record.push_back(Loc.getRawEncoding());
3193}
3194
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003195void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003196 AddSourceLocation(Range.getBegin(), Record);
3197 AddSourceLocation(Range.getEnd(), Record);
3198}
3199
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003200void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003201 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003202 const uint64_t *Words = Value.getRawData();
3203 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003204}
3205
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003206void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003207 Record.push_back(Value.isUnsigned());
3208 AddAPInt(Value, Record);
3209}
3210
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003211void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003212 AddAPInt(Value.bitcastToAPInt(), Record);
3213}
3214
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003215void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003216 Record.push_back(getIdentifierRef(II));
3217}
3218
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003219IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003220 if (II == 0)
3221 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003222
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003223 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003224 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003225 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003226 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003227}
3228
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003229MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003230 if (MD == 0)
3231 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003232
3233 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003234 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003235 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003236 return ID;
3237}
3238
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003239void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003240 Record.push_back(getSelectorRef(SelRef));
3241}
3242
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003243SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003244 if (Sel.getAsOpaquePtr() == 0) {
3245 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003246 }
3247
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003248 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003249 if (SID == 0 && Chain) {
3250 // This might trigger a ReadSelector callback, which will set the ID for
3251 // this selector.
3252 Chain->LoadSelector(Sel);
3253 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003254 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003255 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003256 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003257 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003258}
3259
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003260void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003261 AddDeclRef(Temp->getDestructor(), Record);
3262}
3263
Douglas Gregor7c789c12010-10-29 22:39:52 +00003264void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3265 CXXBaseSpecifier const *BasesEnd,
3266 RecordDataImpl &Record) {
3267 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3268 CXXBaseSpecifiersToWrite.push_back(
3269 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3270 Bases, BasesEnd));
3271 Record.push_back(NextCXXBaseSpecifiersID++);
3272}
3273
Sebastian Redla4232eb2010-08-18 23:56:21 +00003274void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003275 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003276 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003277 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003278 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003279 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003280 break;
3281 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003282 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003283 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003284 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003285 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003286 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003287 break;
3288 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003289 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003290 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003291 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003292 break;
John McCall833ca992009-10-29 08:12:44 +00003293 case TemplateArgument::Null:
3294 case TemplateArgument::Integral:
3295 case TemplateArgument::Declaration:
3296 case TemplateArgument::Pack:
3297 break;
3298 }
3299}
3300
Sebastian Redla4232eb2010-08-18 23:56:21 +00003301void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003302 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003303 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003304
3305 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3306 bool InfoHasSameExpr
3307 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3308 Record.push_back(InfoHasSameExpr);
3309 if (InfoHasSameExpr)
3310 return; // Avoid storing the same expr twice.
3311 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003312 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3313 Record);
3314}
3315
Douglas Gregordc355712011-02-25 00:36:19 +00003316void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3317 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003318 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003319 AddTypeRef(QualType(), Record);
3320 return;
3321 }
3322
Douglas Gregordc355712011-02-25 00:36:19 +00003323 AddTypeLoc(TInfo->getTypeLoc(), Record);
3324}
3325
3326void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3327 AddTypeRef(TL.getType(), Record);
3328
John McCalla1ee0c52009-10-16 21:56:05 +00003329 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003330 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003331 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003332}
3333
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003334void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003335 Record.push_back(GetOrCreateTypeID(T));
3336}
3337
3338TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003339 return MakeTypeID(T,
3340 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3341}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003342
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003343TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003344 return MakeTypeID(T,
3345 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003346}
3347
3348TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3349 if (T.isNull())
3350 return TypeIdx();
3351 assert(!T.getLocalFastQualifiers());
3352
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003353 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003354 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003355 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003356 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003357 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003358 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003359 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003360 return Idx;
3361}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003362
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003363TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003364 if (T.isNull())
3365 return TypeIdx();
3366 assert(!T.getLocalFastQualifiers());
3367
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003368 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3369 assert(I != TypeIdxs.end() && "Type not emitted!");
3370 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003371}
3372
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003373void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003374 Record.push_back(GetDeclRef(D));
3375}
3376
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003377DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003378 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003379 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003380 }
Douglas Gregor97475832010-10-05 18:37:06 +00003381 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003382 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003383 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003384 // We haven't seen this declaration before. Give it a new ID and
3385 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003386 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003387 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003388 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3389 // We don't add it to the replacement collection here, because we don't
3390 // have the offset yet.
3391 DeclTypesToEmit.push(const_cast<Decl *>(D));
3392 // Reset the flag, so that we don't add this decl multiple times.
3393 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003394 }
3395
Sebastian Redl681d7232010-07-27 00:17:23 +00003396 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003397}
3398
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003399DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003400 if (D == 0)
3401 return 0;
3402
3403 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3404 return DeclIDs[D];
3405}
3406
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003407void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003408 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003409 Record.push_back(Name.getNameKind());
3410 switch (Name.getNameKind()) {
3411 case DeclarationName::Identifier:
3412 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3413 break;
3414
3415 case DeclarationName::ObjCZeroArgSelector:
3416 case DeclarationName::ObjCOneArgSelector:
3417 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003418 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003419 break;
3420
3421 case DeclarationName::CXXConstructorName:
3422 case DeclarationName::CXXDestructorName:
3423 case DeclarationName::CXXConversionFunctionName:
3424 AddTypeRef(Name.getCXXNameType(), Record);
3425 break;
3426
3427 case DeclarationName::CXXOperatorName:
3428 Record.push_back(Name.getCXXOverloadedOperator());
3429 break;
3430
Sean Hunt3e518bd2009-11-29 07:34:05 +00003431 case DeclarationName::CXXLiteralOperatorName:
3432 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3433 break;
3434
Douglas Gregor2cf26342009-04-09 22:27:44 +00003435 case DeclarationName::CXXUsingDirective:
3436 // No extra data to emit
3437 break;
3438 }
3439}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003440
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003441void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003442 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003443 switch (Name.getNameKind()) {
3444 case DeclarationName::CXXConstructorName:
3445 case DeclarationName::CXXDestructorName:
3446 case DeclarationName::CXXConversionFunctionName:
3447 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3448 break;
3449
3450 case DeclarationName::CXXOperatorName:
3451 AddSourceLocation(
3452 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3453 Record);
3454 AddSourceLocation(
3455 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3456 Record);
3457 break;
3458
3459 case DeclarationName::CXXLiteralOperatorName:
3460 AddSourceLocation(
3461 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3462 Record);
3463 break;
3464
3465 case DeclarationName::Identifier:
3466 case DeclarationName::ObjCZeroArgSelector:
3467 case DeclarationName::ObjCOneArgSelector:
3468 case DeclarationName::ObjCMultiArgSelector:
3469 case DeclarationName::CXXUsingDirective:
3470 break;
3471 }
3472}
3473
3474void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003475 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003476 AddDeclarationName(NameInfo.getName(), Record);
3477 AddSourceLocation(NameInfo.getLoc(), Record);
3478 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3479}
3480
3481void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003482 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003483 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003484 Record.push_back(Info.NumTemplParamLists);
3485 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3486 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3487}
3488
Sebastian Redla4232eb2010-08-18 23:56:21 +00003489void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003490 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003491 // Nested name specifiers usually aren't too long. I think that 8 would
3492 // typically accomodate the vast majority.
3493 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3494
3495 // Push each of the NNS's onto a stack for serialization in reverse order.
3496 while (NNS) {
3497 NestedNames.push_back(NNS);
3498 NNS = NNS->getPrefix();
3499 }
3500
3501 Record.push_back(NestedNames.size());
3502 while(!NestedNames.empty()) {
3503 NNS = NestedNames.pop_back_val();
3504 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3505 Record.push_back(Kind);
3506 switch (Kind) {
3507 case NestedNameSpecifier::Identifier:
3508 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3509 break;
3510
3511 case NestedNameSpecifier::Namespace:
3512 AddDeclRef(NNS->getAsNamespace(), Record);
3513 break;
3514
Douglas Gregor14aba762011-02-24 02:36:08 +00003515 case NestedNameSpecifier::NamespaceAlias:
3516 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3517 break;
3518
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003519 case NestedNameSpecifier::TypeSpec:
3520 case NestedNameSpecifier::TypeSpecWithTemplate:
3521 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3522 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3523 break;
3524
3525 case NestedNameSpecifier::Global:
3526 // Don't need to write an associated value.
3527 break;
3528 }
3529 }
3530}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003531
Douglas Gregordc355712011-02-25 00:36:19 +00003532void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3533 RecordDataImpl &Record) {
3534 // Nested name specifiers usually aren't too long. I think that 8 would
3535 // typically accomodate the vast majority.
3536 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3537
3538 // Push each of the nested-name-specifiers's onto a stack for
3539 // serialization in reverse order.
3540 while (NNS) {
3541 NestedNames.push_back(NNS);
3542 NNS = NNS.getPrefix();
3543 }
3544
3545 Record.push_back(NestedNames.size());
3546 while(!NestedNames.empty()) {
3547 NNS = NestedNames.pop_back_val();
3548 NestedNameSpecifier::SpecifierKind Kind
3549 = NNS.getNestedNameSpecifier()->getKind();
3550 Record.push_back(Kind);
3551 switch (Kind) {
3552 case NestedNameSpecifier::Identifier:
3553 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3554 AddSourceRange(NNS.getLocalSourceRange(), Record);
3555 break;
3556
3557 case NestedNameSpecifier::Namespace:
3558 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3559 AddSourceRange(NNS.getLocalSourceRange(), Record);
3560 break;
3561
3562 case NestedNameSpecifier::NamespaceAlias:
3563 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3564 AddSourceRange(NNS.getLocalSourceRange(), Record);
3565 break;
3566
3567 case NestedNameSpecifier::TypeSpec:
3568 case NestedNameSpecifier::TypeSpecWithTemplate:
3569 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3570 AddTypeLoc(NNS.getTypeLoc(), Record);
3571 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3572 break;
3573
3574 case NestedNameSpecifier::Global:
3575 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3576 break;
3577 }
3578 }
3579}
3580
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003581void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003582 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003583 Record.push_back(Kind);
3584 switch (Kind) {
3585 case TemplateName::Template:
3586 AddDeclRef(Name.getAsTemplateDecl(), Record);
3587 break;
3588
3589 case TemplateName::OverloadedTemplate: {
3590 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3591 Record.push_back(OvT->size());
3592 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3593 I != E; ++I)
3594 AddDeclRef(*I, Record);
3595 break;
3596 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003597
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003598 case TemplateName::QualifiedTemplate: {
3599 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3600 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3601 Record.push_back(QualT->hasTemplateKeyword());
3602 AddDeclRef(QualT->getTemplateDecl(), Record);
3603 break;
3604 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003605
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003606 case TemplateName::DependentTemplate: {
3607 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3608 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3609 Record.push_back(DepT->isIdentifier());
3610 if (DepT->isIdentifier())
3611 AddIdentifierRef(DepT->getIdentifier(), Record);
3612 else
3613 Record.push_back(DepT->getOperator());
3614 break;
3615 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003616
3617 case TemplateName::SubstTemplateTemplateParmPack: {
3618 SubstTemplateTemplateParmPackStorage *SubstPack
3619 = Name.getAsSubstTemplateTemplateParmPack();
3620 AddDeclRef(SubstPack->getParameterPack(), Record);
3621 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3622 break;
3623 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003624 }
3625}
3626
Michael J. Spencer20249a12010-10-21 03:16:25 +00003627void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003628 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003629 Record.push_back(Arg.getKind());
3630 switch (Arg.getKind()) {
3631 case TemplateArgument::Null:
3632 break;
3633 case TemplateArgument::Type:
3634 AddTypeRef(Arg.getAsType(), Record);
3635 break;
3636 case TemplateArgument::Declaration:
3637 AddDeclRef(Arg.getAsDecl(), Record);
3638 break;
3639 case TemplateArgument::Integral:
3640 AddAPSInt(*Arg.getAsIntegral(), Record);
3641 AddTypeRef(Arg.getIntegralType(), Record);
3642 break;
3643 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003644 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3645 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003646 case TemplateArgument::TemplateExpansion:
3647 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003648 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3649 Record.push_back(*NumExpansions + 1);
3650 else
3651 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003652 break;
3653 case TemplateArgument::Expression:
3654 AddStmt(Arg.getAsExpr());
3655 break;
3656 case TemplateArgument::Pack:
3657 Record.push_back(Arg.pack_size());
3658 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3659 I != E; ++I)
3660 AddTemplateArgument(*I, Record);
3661 break;
3662 }
3663}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003664
3665void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003666ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003667 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003668 assert(TemplateParams && "No TemplateParams!");
3669 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3670 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3671 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3672 Record.push_back(TemplateParams->size());
3673 for (TemplateParameterList::const_iterator
3674 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3675 P != PEnd; ++P)
3676 AddDeclRef(*P, Record);
3677}
3678
3679/// \brief Emit a template argument list.
3680void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003681ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003682 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003683 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003684 Record.push_back(TemplateArgs->size());
3685 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003686 AddTemplateArgument(TemplateArgs->get(i), Record);
3687}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003688
3689
3690void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003691ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003692 Record.push_back(Set.size());
3693 for (UnresolvedSetImpl::const_iterator
3694 I = Set.begin(), E = Set.end(); I != E; ++I) {
3695 AddDeclRef(I.getDecl(), Record);
3696 Record.push_back(I.getAccess());
3697 }
3698}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003699
Sebastian Redla4232eb2010-08-18 23:56:21 +00003700void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003701 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003702 Record.push_back(Base.isVirtual());
3703 Record.push_back(Base.isBaseOfClass());
3704 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003705 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003706 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003707 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003708 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3709 : SourceLocation(),
3710 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003711}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003712
Douglas Gregor7c789c12010-10-29 22:39:52 +00003713void ASTWriter::FlushCXXBaseSpecifiers() {
3714 RecordData Record;
3715 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3716 Record.clear();
3717
3718 // Record the offset of this base-specifier set.
3719 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3720 if (Index == CXXBaseSpecifiersOffsets.size())
3721 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3722 else {
3723 if (Index > CXXBaseSpecifiersOffsets.size())
3724 CXXBaseSpecifiersOffsets.resize(Index + 1);
3725 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3726 }
3727
3728 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3729 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3730 Record.push_back(BEnd - B);
3731 for (; B != BEnd; ++B)
3732 AddCXXBaseSpecifier(*B, Record);
3733 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003734
3735 // Flush any expressions that were written as part of the base specifiers.
3736 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003737 }
3738
3739 CXXBaseSpecifiersToWrite.clear();
3740}
3741
Sean Huntcbb67482011-01-08 20:30:50 +00003742void ASTWriter::AddCXXCtorInitializers(
3743 const CXXCtorInitializer * const *CtorInitializers,
3744 unsigned NumCtorInitializers,
3745 RecordDataImpl &Record) {
3746 Record.push_back(NumCtorInitializers);
3747 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3748 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003749
3750 Record.push_back(Init->isBaseInitializer());
3751 if (Init->isBaseInitializer()) {
3752 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3753 Record.push_back(Init->isBaseVirtual());
3754 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003755 Record.push_back(Init->isIndirectMemberInitializer());
3756 if (Init->isIndirectMemberInitializer())
3757 AddDeclRef(Init->getIndirectMember(), Record);
3758 else
3759 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003760 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003761
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003762 AddSourceLocation(Init->getMemberLocation(), Record);
3763 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003764 AddSourceLocation(Init->getLParenLoc(), Record);
3765 AddSourceLocation(Init->getRParenLoc(), Record);
3766 Record.push_back(Init->isWritten());
3767 if (Init->isWritten()) {
3768 Record.push_back(Init->getSourceOrder());
3769 } else {
3770 Record.push_back(Init->getNumArrayIndices());
3771 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3772 AddDeclRef(Init->getArrayIndex(i), Record);
3773 }
3774 }
3775}
3776
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003777void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3778 assert(D->DefinitionData);
3779 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3780 Record.push_back(Data.UserDeclaredConstructor);
3781 Record.push_back(Data.UserDeclaredCopyConstructor);
3782 Record.push_back(Data.UserDeclaredCopyAssignment);
3783 Record.push_back(Data.UserDeclaredDestructor);
3784 Record.push_back(Data.Aggregate);
3785 Record.push_back(Data.PlainOldData);
3786 Record.push_back(Data.Empty);
3787 Record.push_back(Data.Polymorphic);
3788 Record.push_back(Data.Abstract);
3789 Record.push_back(Data.HasTrivialConstructor);
3790 Record.push_back(Data.HasTrivialCopyConstructor);
3791 Record.push_back(Data.HasTrivialCopyAssignment);
3792 Record.push_back(Data.HasTrivialDestructor);
3793 Record.push_back(Data.ComputedVisibleConversions);
3794 Record.push_back(Data.DeclaredDefaultConstructor);
3795 Record.push_back(Data.DeclaredCopyConstructor);
3796 Record.push_back(Data.DeclaredCopyAssignment);
3797 Record.push_back(Data.DeclaredDestructor);
3798
3799 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003800 if (Data.NumBases > 0)
3801 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3802 Record);
3803
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003804 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3805 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003806 if (Data.NumVBases > 0)
3807 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3808 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003809
3810 AddUnresolvedSet(Data.Conversions, Record);
3811 AddUnresolvedSet(Data.VisibleConversions, Record);
3812 // Data.Definition is the owning decl, no need to write it.
3813 AddDeclRef(Data.FirstFriend, Record);
3814}
3815
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003816void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003817 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003818 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003819 assert(FirstDeclID == NextDeclID &&
3820 FirstTypeID == NextTypeID &&
3821 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003822 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003823 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003824 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003825 "Setting chain after writing has started.");
3826 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003827
3828 FirstDeclID += Chain->getTotalNumDecls();
3829 FirstTypeID += Chain->getTotalNumTypes();
3830 FirstIdentID += Chain->getTotalNumIdentifiers();
3831 FirstSelectorID += Chain->getTotalNumSelectors();
3832 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003833 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003834 NextDeclID = FirstDeclID;
3835 NextTypeID = FirstTypeID;
3836 NextIdentID = FirstIdentID;
3837 NextSelectorID = FirstSelectorID;
3838 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003839 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003840}
3841
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003842void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003843 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003844 if (II->hasMacroDefinition())
3845 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003846}
3847
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003848void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003849 // Always take the highest-numbered type index. This copes with an interesting
3850 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003851 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003852 // keep the higher-numbered entry so that we can properly write it out to
3853 // the AST file.
3854 TypeIdx &StoredIdx = TypeIdxs[T];
3855 if (Idx.getIndex() >= StoredIdx.getIndex())
3856 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003857}
3858
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003859void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003860 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003861}
Sebastian Redl5d050072010-08-04 17:20:04 +00003862
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003863void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003864 SelectorIDs[S] = ID;
3865}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003866
Michael J. Spencer20249a12010-10-21 03:16:25 +00003867void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003868 MacroDefinition *MD) {
3869 MacroDefinitions[MD] = ID;
3870}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003871
3872void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3873 assert(D->isDefinition());
3874 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3875 // We are interested when a PCH decl is modified.
3876 if (RD->getPCHLevel() > 0) {
3877 // A forward reference was mutated into a definition. Rewrite it.
3878 // FIXME: This happens during template instantiation, should we
3879 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003880 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003881 }
3882
3883 for (CXXRecordDecl::redecl_iterator
3884 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3885 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3886 if (Redecl == RD)
3887 continue;
3888
3889 // We are interested when a PCH decl is modified.
3890 if (Redecl->getPCHLevel() > 0) {
3891 UpdateRecord &Record = DeclUpdates[Redecl];
3892 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3893 assert(Redecl->DefinitionData);
3894 assert(Redecl->DefinitionData->Definition == D);
3895 AddDeclRef(D, Record); // the DefinitionDecl
3896 }
3897 }
3898 }
3899}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003900void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3901 // TU and namespaces are handled elsewhere.
3902 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3903 return;
3904
3905 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3906 return; // Not a source decl added to a DeclContext from PCH.
3907
3908 AddUpdatedDeclContext(DC);
3909}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003910
3911void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3912 assert(D->isImplicit());
3913 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3914 return; // Not a source member added to a class from PCH.
3915 if (!isa<CXXMethodDecl>(D))
3916 return; // We are interested in lazily declared implicit methods.
3917
3918 // A decl coming from PCH was modified.
3919 assert(RD->isDefinition());
3920 UpdateRecord &Record = DeclUpdates[RD];
3921 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3922 AddDeclRef(D, Record);
3923}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003924
3925void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3926 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003927 // The specializations set is kept in the canonical template.
3928 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003929 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3930 return; // Not a source specialization added to a template from PCH.
3931
3932 UpdateRecord &Record = DeclUpdates[TD];
3933 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3934 AddDeclRef(D, Record);
3935}
Douglas Gregor89d99802010-11-30 06:16:57 +00003936
3937ASTSerializationListener::~ASTSerializationListener() { }